Skip to content

Commit 8de5170

Browse files
committed
rowwise
1 parent 3c5e0b0 commit 8de5170

1 file changed

Lines changed: 41 additions & 41 deletions

File tree

src/_util_colwise.jl

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
2-
colsum(X::Matrix{Q}) where Q <: AbstractFloat
3-
colsum(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
2+
colsum(X::AbstractMatrix{Q}) where Q <: AbstractFloat
3+
colsum(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
44
Column-wise sums of a matrix.
55
* `X` : Matrix (n, p).
66
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
@@ -19,8 +19,8 @@ colsum(X)
1919
colsum(X, w)
2020
```
2121
"""
22-
function colsum(X::Matrix{Q}) where Q <: AbstractFloat
23-
s = similar(X, nco(X))
22+
function colsum(X::AbstractMatrix{Q}) where Q <: AbstractFloat
23+
s = zeros(eltype(X), nco(X))
2424
Threads.@threads for j in axes(X, 2)
2525
@inbounds for i in axes(X, 1)
2626
s[j] += X[i, j]
@@ -29,8 +29,8 @@ function colsum(X::Matrix{Q}) where Q <: AbstractFloat
2929
s
3030
end
3131

32-
function colsum(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
33-
s = similar(X, nco(X))
32+
function colsum(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
33+
s = zeros(eltype(X), nco(X))
3434
Threads.@threads for j in axes(X, 2)
3535
@inbounds for i in axes(X, 1)
3636
s[j] += X[i, j] * weights.values[i]
@@ -40,8 +40,8 @@ function colsum(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: Abstrac
4040
end
4141

4242
"""
43-
colmean(X::Matrix{Q}) where Q <: AbstractFloat
44-
colmean(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
43+
colmean(X::AbstractMatrix{Q}) where Q <: AbstractFloat
44+
colmean(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
4545
Column-wise means of a matrix.
4646
* `X` : Matrix (n, p).
4747
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
@@ -60,13 +60,13 @@ colmean(X)
6060
colmean(X, w)
6161
```
6262
"""
63-
colmean(X::Matrix{Q}) where Q <: AbstractFloat = colsum(X) / nro(X)
63+
colmean(X::AbstractMatrix{Q}) where Q <: AbstractFloat = colsum(X) / nro(X)
6464

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

6767
"""
68-
colnorm(X::Matrix{Q}) where Q <: AbstractFloat
69-
colnorm(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
68+
colnorm(X::AbstractMatrix{Q}) where Q <: AbstractFloat
69+
colnorm(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
7070
Column-wise norms of a matrix.
7171
* `X` : Matrix (n, p).
7272
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
@@ -93,13 +93,13 @@ colnorm(X)
9393
colnorm(X, w)
9494
```
9595
"""
96-
colnorm(X::Matrix{Q}) where Q <: AbstractFloat = sqrt.(colnorm2(X))
96+
colnorm(X::AbstractMatrix{Q}) where Q <: AbstractFloat = sqrt.(colnorm2(X))
9797

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

100100
"""
101-
colnorm2(X::Matrix{Q}) where Q <: AbstractFloat
102-
colnorm2(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
101+
colnorm2(X::AbstractMatrix{Q}) where Q <: AbstractFloat
102+
colnorm2(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
103103
Column-wise squared norms of a matrix.
104104
* `X` : Matrix (n, p).
105105
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
@@ -118,8 +118,8 @@ colnorm2(X)
118118
colnorm2(X, w)
119119
```
120120
"""
121-
function colnorm2(X::Matrix{Q}) where Q <: AbstractFloat
122-
s = similar(X, nco(X))
121+
function colnorm2(X::AbstractMatrix{Q}) where Q <: AbstractFloat
122+
s = zeros(eltype(X), nco(X))
123123
Threads.@threads for j in axes(X, 2)
124124
@inbounds for i in axes(X, 1)
125125
s[j] += X[i, j]^2
@@ -128,8 +128,8 @@ function colnorm2(X::Matrix{Q}) where Q <: AbstractFloat
128128
s
129129
end
130130

131-
function colnorm2(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
132-
s = similar(X, nco(X))
131+
function colnorm2(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
132+
s = zeros(eltype(X), nco(X))
133133
Threads.@threads for j in axes(X, 2)
134134
@inbounds for i in axes(X, 1)
135135
s[j] += X[i, j]^2 * weights.values[i]
@@ -139,8 +139,8 @@ function colnorm2(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: Abstr
139139
end
140140

141141
"""
142-
colvar(X::Matrix{Q}) where Q <: AbstractFloat
143-
colvar(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
142+
colvar(X::AbstractMatrix{Q}) where Q <: AbstractFloat
143+
colvar(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
144144
Column-wise (uncorrected) variances of a matrix.
145145
* `X` : Matrix (n, p).
146146
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
@@ -159,15 +159,15 @@ colvar(X)
159159
colvar(X, w)
160160
```
161161
"""
162-
function colvar(X::Matrix{Q}) where Q <: AbstractFloat
162+
function colvar(X::AbstractMatrix{Q}) where Q <: AbstractFloat
163163
s = similar(X, nco(X))
164164
Threads.@threads for j in axes(X, 2)
165165
s[j] = varv(vcol(X, j))
166166
end
167167
s
168168
end
169169

170-
function colvar(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
170+
function colvar(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
171171
s = similar(X, nco(X))
172172
Threads.@threads for j in axes(X, 2)
173173
s[j] = varv(vcol(X, j), weights)
@@ -176,8 +176,8 @@ function colvar(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: Abstrac
176176
end
177177

178178
"""
179-
colstd(X::Matrix{Q}) where Q <: AbstractFloat
180-
colstd(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
179+
colstd(X::AbstractMatrix{Q}) where Q <: AbstractFloat
180+
colstd(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
181181
Column-wise (uncorrected) standard deviations of a matrix.
182182
* `X` : Matrix (n, p).
183183
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
@@ -196,13 +196,13 @@ colstd(X)
196196
colstd(X, w)
197197
```
198198
"""
199-
colstd(X::Matrix{Q}) where Q <: AbstractFloat = sqrt.(colvar(X))
199+
colstd(X::AbstractMatrix{Q}) where Q <: AbstractFloat = sqrt.(colvar(X))
200200

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

203203
"""
204-
colprt(X::Matrix{Q}) where Q <: AbstractFloat
205-
colprt(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
204+
colprt(X::AbstractMatrix{Q}) where Q <: AbstractFloat
205+
colprt(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
206206
Column-wise (uncorrected) standard deviations of a matrix.
207207
* `X` : Matrix (n, p).
208208
* `weights` : Weights (n) of the observations. Must be of type `ProbabilityWeights` (see e.g., function `pweight`).
@@ -221,12 +221,12 @@ colprt(X)
221221
colprt(X, w)
222222
```
223223
"""
224-
colprt(X::Matrix{Q}) where Q <: AbstractFloat = sqrt.(colstd(X))
224+
colprt(X::AbstractMatrix{Q}) where Q <: AbstractFloat = sqrt.(colstd(X))
225225

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

228228
"""
229-
colmed(X::Matrix{Q}) where Q <: AbstractFloat
229+
colmed(X::AbstractMatrix{Q}) where Q <: AbstractFloat
230230
Column-wise medians of a matrix.
231231
* `X` : Matrix (n, p).
232232
@@ -242,7 +242,7 @@ X = rand(n, p)
242242
colmed(X)
243243
```
244244
"""
245-
function colmed(X::Matrix{Q}) where Q <: AbstractFloat
245+
function colmed(X::AbstractMatrix{Q}) where Q <: AbstractFloat
246246
s = similar(X, nco(X))
247247
Threads.@threads for j in axes(X, 2)
248248
s[j] = Statistics.median(vcol(X, j))
@@ -251,7 +251,7 @@ function colmed(X::Matrix{Q}) where Q <: AbstractFloat
251251
end
252252

253253
"""
254-
colmad(X::Matrix{Q}) where Q <: AbstractFloat
254+
colmad(X::AbstractMatrix{Q}) where Q <: AbstractFloat
255255
Column-wise median absolute deviations (MAD) of a matrix.
256256
* `X` : Matrix (n, p).
257257
@@ -267,15 +267,15 @@ X = rand(n, p)
267267
colmad(X)
268268
```
269269
"""
270-
function colmad(X::Matrix{Q}) where Q <: AbstractFloat
270+
function colmad(X::AbstractMatrix{Q}) where Q <: AbstractFloat
271271
s = similar(X, nco(X))
272272
Threads.@threads for j in axes(X, 2)
273273
s[j] = madv(vcol(X, j))
274274
end
275275
s
276276
end
277277

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

280280
"""
281281
def_colscal(scal::Symbol = :std)
@@ -296,7 +296,7 @@ end
296296
##### Functions skipping missing data
297297

298298
colsumskip(X) = [Base.sum(skipmissing(x)) for x in eachcol(ensure_mat(X))]
299-
function colsumskip(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
299+
function colsumskip(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
300300
X = ensure_mat(X)
301301
v = zeros(Q, nco(X))
302302
@inbounds for j in axes(X, 2)
@@ -308,13 +308,13 @@ function colsumskip(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: Abs
308308
end
309309

310310
colmeanskip(X) = [Statistics.mean(skipmissing(x)) for x in eachcol(ensure_mat(X))]
311-
colmeanskip(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = colsumskip(X, weights)
311+
colmeanskip(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = colsumskip(X, weights)
312312

313313
colstdskip(X) = [Statistics.std(skipmissing(x); corrected = false) for x in eachcol(ensure_mat(X))]
314-
colstdskip(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = sqrt.(colvarskip(X, weights))
314+
colstdskip(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat = sqrt.(colvarskip(X, weights))
315315

316316
colvarskip(X) = [Statistics.var(skipmissing(x); corrected = false) for x in eachcol(ensure_mat(X))]
317-
function colvarskip(X::Matrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
317+
function colvarskip(X::AbstractMatrix{Q}, weights::ProbabilityWeights{Q}) where Q <: AbstractFloat
318318
X = ensure_mat(X)
319319
p = nco(X)
320320
v = colmeanskip(X, weights)

0 commit comments

Comments
 (0)