Skip to content

Commit 3c5e0b0

Browse files
committed
colwise
1 parent 2f54f70 commit 3c5e0b0

4 files changed

Lines changed: 167 additions & 181 deletions

File tree

src/_util_center_scale.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
fcenter(X, v)
3-
fcenter!(X::AbstractArray{Q}, v::Vector{Q}) where Q <: AbstractFloat
3+
fcenter!(X::Matrix{Q}, v::Vector{Q}) where Q <: AbstractFloat
44
Center each column of a matrix.
55
* `X` : Data (n, p).
66
* `v` : Centering vector (p).
@@ -21,15 +21,15 @@ function fcenter(X, v)
2121
zX
2222
end
2323

24-
function fcenter!(X::AbstractArray{Q}, v::Vector{Q}) where Q <: AbstractFloat
24+
function fcenter!(X::Matrix{Q}, v::Vector{Q}) where Q <: AbstractFloat
2525
@inbounds for j in axes(X, 2), i in axes(X, 1)
2626
X[i, j] -= v[j]
2727
end
2828
end
2929

3030
"""
3131
fscale(X, v)
32-
fscale!(X::AbstractArray{Q}, v::Vector{Q}) where Q <: AbstractFloat
32+
fscale!(X::Matrix{Q}, v::Vector{Q}) where Q <: AbstractFloat
3333
Scale each column of a matrix.
3434
* `X` : Data (n, p).
3535
* `v` : Scaling vector (p).
@@ -48,15 +48,15 @@ function fscale(X, v)
4848
zX
4949
end
5050

51-
function fscale!(X::AbstractArray{Q}, v::Vector{Q}) where Q <: AbstractFloat
51+
function fscale!(X::Matrix{Q}, v::Vector{Q}) where Q <: AbstractFloat
5252
@inbounds for j in axes(X, 2), i in axes(X, 1)
5353
X[i, j] /= v[j]
5454
end
5555
end
5656

5757
"""
5858
fcscale(X, u, v)
59-
fcscale!(X::AbstractArray{Q}, u::Vector{Q}, v::Vector{Q}) where Q <: AbstractFloat
59+
fcscale!(X::Matrix{Q}, u::Vector{Q}, v::Vector{Q}) where Q <: AbstractFloat
6060
Center and scale each column of a matrix.
6161
* `X` : Data (n, p).
6262
* `u` : Centering vector (p).
@@ -79,7 +79,7 @@ function fcscale(X, u, v)
7979
zX
8080
end
8181

82-
function fcscale!(X::AbstractArray{Q}, u::Vector{Q}, v::Vector{Q}) where Q <: AbstractFloat
82+
function fcscale!(X::Matrix{Q}, u::Vector{Q}, v::Vector{Q}) where Q <: AbstractFloat
8383
@inbounds for j in axes(X, 2), i in axes(X, 1)
8484
X[i, j] = (X[i, j] - u[j]) / v[j]
8585
end

src/_util_colwise.jl

Lines changed: 71 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""
2-
colsum(X)
3-
colsum(X, weights::ProbabilityWeights)
2+
colsum(X::Matrix{Q}) where Q <: AbstractFloat
3+
colsum(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
44
Column-wise sums of a matrix.
5-
* `X` : Data (n, p).
5+
* `X` : Matrix (n, p).
66
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
77
88
Return a vector (p).
@@ -19,37 +19,31 @@ colsum(X)
1919
colsum(X, w)
2020
```
2121
"""
22-
function colsum(X)
23-
X = ensure_mat(X)
24-
Q = eltype(X)
25-
n, p = size(X)
26-
s = zeros(Q, p)
27-
Threads.@threads for j = 1:p
28-
@inbounds for i in 1:n
22+
function colsum(X::Matrix{Q}) where Q <: AbstractFloat
23+
s = similar(X, nco(X))
24+
Threads.@threads for j in axes(X, 2)
25+
@inbounds for i in axes(X, 1)
2926
s[j] += X[i, j]
3027
end
3128
end
3229
s
3330
end
3431

35-
function colsum(X, weights::ProbabilityWeights)
36-
X = ensure_mat(X)
37-
Q = eltype(X)
38-
n, p = size(X)
39-
s = zeros(Q, p)
40-
Threads.@threads for j = 1:p
41-
@inbounds for i in 1:n
32+
function colsum(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
33+
s = similar(X, nco(X))
34+
Threads.@threads for j in axes(X, 2)
35+
@inbounds for i in axes(X, 1)
4236
s[j] += X[i, j] * weights.values[i]
4337
end
4438
end
4539
s
4640
end
4741

4842
"""
49-
colmean(X)
50-
colmean(X, weights::ProbabilityWeights)
43+
colmean(X::Matrix{Q}) where Q <: AbstractFloat
44+
colmean(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
5145
Column-wise means of a matrix.
52-
* `X` : Data (n, p).
46+
* `X` : Matrix (n, p).
5347
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
5448
5549
Return a vector (p).
@@ -66,15 +60,15 @@ colmean(X)
6660
colmean(X, w)
6761
```
6862
"""
69-
colmean(X) = colsum(X) / nro(X)
63+
colmean(X::Matrix{Q}) where Q <: AbstractFloat = colsum(X) / nro(X)
7064

71-
colmean(X, weights::ProbabilityWeights) = colsum(X, weights)
65+
colmean(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = colsum(X, weights)
7266

7367
"""
74-
colnorm(X)
75-
colnorm(X, weights::ProbabilityWeights)
68+
colnorm(X::Matrix{Q}) where Q <: AbstractFloat
69+
colnorm(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
7670
Column-wise norms of a matrix.
77-
* `X` : Data (n, p).
71+
* `X` : Matrix (n, p).
7872
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
7973
8074
Return a vector (p).
@@ -99,15 +93,15 @@ colnorm(X)
9993
colnorm(X, w)
10094
```
10195
"""
102-
colnorm(X) = sqrt.(colnorm2(X))
96+
colnorm(X::Matrix{Q}) where Q <: AbstractFloat = sqrt.(colnorm2(X))
10397

104-
colnorm(X, weights::ProbabilityWeights) = sqrt.(colnorm2(X, weights))
98+
colnorm(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = sqrt.(colnorm2(X, weights))
10599

106100
"""
107-
colnorm2(X)
108-
colnorm2(X, weights::ProbabilityWeights)
101+
colnorm2(X::Matrix{Q}) where Q <: AbstractFloat
102+
colnorm2(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
109103
Column-wise squared norms of a matrix.
110-
* `X` : Data (n, p).
104+
* `X` : Matrix (n, p).
111105
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
112106
113107
See function `colnorm`.
@@ -124,37 +118,31 @@ colnorm2(X)
124118
colnorm2(X, w)
125119
```
126120
"""
127-
function colnorm2(X)
128-
X = ensure_mat(X)
129-
Q = eltype(X)
130-
n, p = size(X)
131-
s = zeros(Q, p)
132-
Threads.@threads for j = 1:p
133-
@inbounds for i in 1:n
121+
function colnorm2(X::Matrix{Q}) where Q <: AbstractFloat
122+
s = similar(X, nco(X))
123+
Threads.@threads for j in axes(X, 2)
124+
@inbounds for i in axes(X, 1)
134125
s[j] += X[i, j]^2
135126
end
136127
end
137128
s
138129
end
139130

140-
function colnorm2(X, weights::ProbabilityWeights)
141-
X = ensure_mat(X)
142-
Q = eltype(X)
143-
n, p = size(X)
144-
s = zeros(Q, p)
145-
Threads.@threads for j = 1:p
146-
@inbounds for i in 1:n
131+
function colnorm2(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
132+
s = similar(X, nco(X))
133+
Threads.@threads for j in axes(X, 2)
134+
@inbounds for i in axes(X, 1)
147135
s[j] += X[i, j]^2 * weights.values[i]
148136
end
149137
end
150138
s
151139
end
152140

153141
"""
154-
colvar(X)
155-
colvar(X, weights::ProbabilityWeights)
142+
colvar(X::Matrix{Q}) where Q <: AbstractFloat
143+
colvar(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
156144
Column-wise (uncorrected) variances of a matrix.
157-
* `X` : Data (n, p).
145+
* `X` : Matrix (n, p).
158146
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
159147
160148
Return a vector (p).
@@ -171,31 +159,27 @@ colvar(X)
171159
colvar(X, w)
172160
```
173161
"""
174-
function colvar(X)
175-
X = ensure_mat(X)
176-
p = nco(X)
177-
s = similar(X, p)
178-
Threads.@threads for j = 1:p
162+
function colvar(X::Matrix{Q}) where Q <: AbstractFloat
163+
s = similar(X, nco(X))
164+
Threads.@threads for j in axes(X, 2)
179165
s[j] = varv(vcol(X, j))
180166
end
181167
s
182168
end
183169

184-
function colvar(X, weights::ProbabilityWeights)
185-
X = ensure_mat(X)
186-
p = nco(X)
187-
s = similar(X, p)
188-
Threads.@threads for j = 1:p
170+
function colvar(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
171+
s = similar(X, nco(X))
172+
Threads.@threads for j in axes(X, 2)
189173
s[j] = varv(vcol(X, j), weights)
190174
end
191175
s
192176
end
193177

194178
"""
195-
colstd(X)
196-
colstd(X, weights::ProbabilityWeights)
179+
colstd(X::Matrix{Q}) where Q <: AbstractFloat
180+
colstd(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
197181
Column-wise (uncorrected) standard deviations of a matrix.
198-
* `X` : Data (n, p).
182+
* `X` : Matrix (n, p).
199183
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
200184
201185
Return a vector (p).
@@ -212,15 +196,15 @@ colstd(X)
212196
colstd(X, w)
213197
```
214198
"""
215-
colstd(X) = sqrt.(colvar(X))
199+
colstd(X::Matrix{Q}) where Q <: AbstractFloat = sqrt.(colvar(X))
216200

217-
colstd(X, weights::ProbabilityWeights) = sqrt.(colvar(X, weights))
201+
colstd(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = sqrt.(colvar(X, weights))
218202

219203
"""
220-
colprt(X)
221-
colprt(X, weights::ProbabilityWeights)
204+
colprt(X::Matrix{Q}) where Q <: AbstractFloat
205+
colprt(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
222206
Column-wise (uncorrected) standard deviations of a matrix.
223-
* `X` : Data (n, p).
207+
* `X` : Matrix (n, p).
224208
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
225209
226210
Return a vector (p).
@@ -237,14 +221,14 @@ colprt(X)
237221
colprt(X, w)
238222
```
239223
"""
240-
colprt(X) = sqrt.(colstd(X))
224+
colprt(X::Matrix{Q}) where Q <: AbstractFloat = sqrt.(colstd(X))
241225

242-
colprt(X, weights::ProbabilityWeights) = sqrt.(colstd(X, weights))
226+
colprt(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = sqrt.(colstd(X, weights))
243227

244228
"""
245-
colmed(X)
229+
colmed(X::Matrix{Q}) where Q <: AbstractFloat
246230
Column-wise medians of a matrix.
247-
* `X` : Data (n, p).
231+
* `X` : Matrix (n, p).
248232
249233
Return a vector (p).
250234
@@ -258,20 +242,18 @@ X = rand(n, p)
258242
colmed(X)
259243
```
260244
"""
261-
function colmed(X)
262-
X = ensure_mat(X)
263-
p = nco(X)
264-
s = similar(X, p)
265-
Threads.@threads for j = 1:p
245+
function colmed(X::Matrix{Q}) where Q <: AbstractFloat
246+
s = similar(X, nco(X))
247+
Threads.@threads for j in axes(X, 2)
266248
s[j] = Statistics.median(vcol(X, j))
267249
end
268250
s
269251
end
270252

271253
"""
272-
colmad(X)
254+
colmad(X::Matrix{Q}) where Q <: AbstractFloat
273255
Column-wise median absolute deviations (MAD) of a matrix.
274-
* `X` : Data (n, p).
256+
* `X` : Matrix (n, p).
275257
276258
Return a vector (p).
277259
@@ -285,17 +267,15 @@ X = rand(n, p)
285267
colmad(X)
286268
```
287269
"""
288-
function colmad(X)
289-
X = ensure_mat(X)
290-
p = nco(X)
291-
s = similar(X, p)
292-
Threads.@threads for j = 1:p
270+
function colmad(X::Matrix{Q}) where Q <: AbstractFloat
271+
s = similar(X, nco(X))
272+
Threads.@threads for j in axes(X, 2)
293273
s[j] = madv(vcol(X, j))
294274
end
295275
s
296276
end
297277

298-
colmad(X, weights::ProbabilityWeights) = colmad(X) # for consistency when weights
278+
colmad(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = colmad(X) # for consistency when weights
299279

300280
"""
301281
def_colscal(scal::Symbol = :std)
@@ -316,23 +296,25 @@ end
316296
##### Functions skipping missing data
317297

318298
colsumskip(X) = [Base.sum(skipmissing(x)) for x in eachcol(ensure_mat(X))]
319-
function colsumskip(X, weights::ProbabilityWeights)
299+
function colsumskip(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
320300
X = ensure_mat(X)
321-
p = nco(X)
322-
v = zeros(p)
323-
@inbounds for j = 1:p
301+
v = zeros(Q, nco(X))
302+
@inbounds for j in axes(X, 2)
324303
s = ismissing.(vcol(X, j))
325304
w = pweight(rmrow(weights.values, s))
326305
v[j] = sum(w.values .* rmrow(X[:, j], s))
327306
end
328307
v
329308
end
309+
330310
colmeanskip(X) = [Statistics.mean(skipmissing(x)) for x in eachcol(ensure_mat(X))]
331-
colmeanskip(X, weights::ProbabilityWeights) = colsumskip(X, weights)
311+
colmeanskip(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = colsumskip(X, weights)
312+
332313
colstdskip(X) = [Statistics.std(skipmissing(x); corrected = false) for x in eachcol(ensure_mat(X))]
333-
colstdskip(X, weights::ProbabilityWeights) = sqrt.(colvarskip(X, weights))
314+
colstdskip(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = sqrt.(colvarskip(X, weights))
315+
334316
colvarskip(X) = [Statistics.var(skipmissing(x); corrected = false) for x in eachcol(ensure_mat(X))]
335-
function colvarskip(X, weights::ProbabilityWeights)
317+
function colvarskip(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
336318
X = ensure_mat(X)
337319
p = nco(X)
338320
v = colmeanskip(X, weights)

0 commit comments

Comments
 (0)