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
44Column-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
88Return a vector (p).
@@ -19,37 +19,31 @@ colsum(X)
1919colsum(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
3330end
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
4640end
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
5145Column-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
5549Return a vector (p).
@@ -66,15 +60,15 @@ colmean(X)
6660colmean(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
7670Column-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
8074Return a vector (p).
@@ -99,15 +93,15 @@ colnorm(X)
9993colnorm(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
109103Column-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
113107See function `colnorm`.
@@ -124,37 +118,31 @@ colnorm2(X)
124118colnorm2(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
138129end
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
151139end
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
156144Column-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
160148Return a vector (p).
@@ -171,31 +159,27 @@ colvar(X)
171159colvar(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
182168end
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
192176end
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
197181Column-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
201185Return a vector (p).
@@ -212,15 +196,15 @@ colstd(X)
212196colstd(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
222206Column-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
226210Return a vector (p).
@@ -237,14 +221,14 @@ colprt(X)
237221colprt(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
246230Column-wise medians of a matrix.
247- * `X` : Data (n, p).
231+ * `X` : Matrix (n, p).
248232
249233Return a vector (p).
250234
@@ -258,20 +242,18 @@ X = rand(n, p)
258242colmed(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
269251end
270252
271253"""
272- colmad(X)
254+ colmad(X::Matrix{Q}) where Q <: AbstractFloat
273255Column-wise median absolute deviations (MAD) of a matrix.
274- * `X` : Data (n, p).
256+ * `X` : Matrix (n, p).
275257
276258Return a vector (p).
277259
@@ -285,17 +267,15 @@ X = rand(n, p)
285267colmad(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
296276end
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
318298colsumskip (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
329308end
309+
330310colmeanskip (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+
332313colstdskip (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+
334316colvarskip (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