@@ -32,11 +32,9 @@ _unscaled_covzm(x::DenseMatrix, wv::AbstractWeights, dims::Integer) =
32
32
_symmetrize! (unscaled_covzm (x, _scalevars (x, values (wv), dims), dims))
33
33
34
34
"""
35
- scattermat(X, [wv::AbstractWeights] ; mean=nothing, dims=1)
35
+ scattermat(X; mean=nothing, dims=1[, weights::AbstractWeights] )
36
36
37
37
Compute the scatter matrix, which is an unnormalized covariance matrix.
38
- A weighting vector `wv` can be specified to weight
39
- the estimate.
40
38
41
39
# Arguments
42
40
* `mean=nothing`: a known mean value. `nothing` indicates that the mean is
@@ -45,62 +43,33 @@ the estimate.
45
43
* `dims=1`: the dimension along which the variables are organized.
46
44
When `dims = 1`, the variables are considered columns with observations in rows;
47
45
when `dims = 2`, variables are in rows with observations in columns.
48
- """
49
- function scattermat end
50
-
51
-
52
- """
53
- cov(X, w::AbstractWeights, vardim=1; mean=nothing, corrected=false)
54
-
55
- Compute the weighted covariance matrix. Similar to `var` and `std` the biased covariance
56
- matrix (`corrected=false`) is computed by multiplying `scattermat(X, w)` by
57
- ``\\ frac{1}{\\ sum{w}}`` to normalize. However, the unbiased covariance matrix
58
- (`corrected=true`) is dependent on the type of weights used:
59
- * `AnalyticWeights`: ``\\ frac{1}{\\ sum w - \\ sum {w^2} / \\ sum w}``
60
- * `FrequencyWeights`: ``\\ frac{1}{\\ sum{w} - 1}``
61
- * `ProbabilityWeights`: ``\\ frac{n}{(n - 1) \\ sum w}`` where ``n`` equals `count(!iszero, w)`
62
- * `Weights`: `ArgumentError` (bias correction not supported)
63
- """
64
- cov
65
-
66
- scattermat (x:: DenseMatrix ; mean= nothing , dims:: Int = 1 ) =
67
- _scattermatm (x, mean, dims)
68
- _scattermatm (x:: DenseMatrix , :: Nothing , dims:: Int ) =
69
- _unscaled_covzm (x .- mean (x, dims= dims), dims)
70
- _scattermatm (x:: DenseMatrix , mean, dims:: Int = 1 ) =
46
+ * `weights`: optional weights for observations.
47
+ """
48
+ scattermat (x:: DenseMatrix ; mean= nothing , dims:: Int = 1 ,
49
+ weights:: Union{AbstractWeights, Nothing} = nothing ) =
50
+ _scattermatm (x, weights, mean, dims)
51
+ _scattermatm (x:: DenseMatrix , weights:: Nothing , mean:: Nothing , dims:: Int ) =
52
+ _unscaled_covzm (x .- Statistics. mean (x, dims= dims), dims)
53
+ _scattermatm (x:: DenseMatrix , weights:: Nothing , mean, dims:: Int = 1 ) =
71
54
_unscaled_covzm (x .- mean, dims)
72
55
73
- scattermat (x:: DenseMatrix , wv:: AbstractWeights ; mean= nothing , dims:: Int = 1 ) =
74
- _scattermatm (x, wv, mean, dims)
75
- _scattermatm (x:: DenseMatrix , wv:: AbstractWeights , :: Nothing , dims:: Int ) =
76
- _unscaled_covzm (x .- mean (x, wv, dims= dims), wv, dims)
77
- _scattermatm (x:: DenseMatrix , wv:: AbstractWeights , mean, dims:: Int ) =
78
- _unscaled_covzm (x .- mean, wv, dims)
56
+ _scattermatm (x:: DenseMatrix , weights:: AbstractWeights , mean:: Nothing , dims:: Int ) =
57
+ _unscaled_covzm (x .- Statistics. mean (x, weights= weights, dims= dims), weights, dims)
58
+ _scattermatm (x:: DenseMatrix , weights:: AbstractWeights , mean, dims:: Int ) =
59
+ _unscaled_covzm (x .- mean, weights, dims)
79
60
80
61
# # weighted cov
81
- covm (x:: DenseMatrix , mean, w :: AbstractWeights , dims:: Int = 1 ;
62
+ covm (x:: DenseMatrix , mean, weights :: AbstractWeights , dims:: Int = 1 ;
82
63
corrected:: Bool = true ) =
83
- rmul! (scattermat (x, w , mean= mean, dims= dims), varcorrection (w, depcheck ( :covm , corrected)))
84
-
64
+ rmul! (scattermat (x, weights = weights , mean= mean, dims= dims),
65
+ varcorrection (weights, corrected))
85
66
86
- cov (x:: DenseMatrix , w:: AbstractWeights , dims:: Int = 1 ; corrected:: Bool = true ) =
87
- covm (x, mean (x, w, dims= dims), w, dims; corrected= depcheck (:cov , corrected))
88
-
89
- function corm (x:: DenseMatrix , mean, w:: AbstractWeights , vardim:: Int = 1 )
90
- c = covm (x, mean, w, vardim; corrected= false )
91
- s = stdm (x, w, mean, vardim; corrected= false )
67
+ function corm (x:: DenseMatrix , mean, weights:: AbstractWeights , vardim:: Int = 1 )
68
+ c = covm (x, mean, weights, vardim; corrected= false )
69
+ s = std (x, mean= mean, weights= weights, dims= vardim, corrected= false )
92
70
cov2cor! (c, s)
93
71
end
94
72
95
- """
96
- cor(X, w::AbstractWeights, dims=1)
97
-
98
- Compute the Pearson correlation matrix of `X` along the dimension
99
- `dims` with a weighting `w` .
100
- """
101
- cor (x:: DenseMatrix , w:: AbstractWeights , dims:: Int = 1 ) =
102
- corm (x, mean (x, w, dims= dims), w, dims)
103
-
104
73
"""
105
74
cov2cor(C, s)
106
75
@@ -156,7 +125,8 @@ cov(ce::CovarianceEstimator, x::AbstractVector, y::AbstractVector) =
156
125
error (" cov is not defined for $(typeof (ce)) , $(typeof (x)) and $(typeof (y)) " )
157
126
158
127
"""
159
- cov(ce::CovarianceEstimator, X::AbstractMatrix, [w::AbstractWeights]; mean=nothing, dims::Int=1)
128
+ cov(ce::CovarianceEstimator, X::AbstractMatrix; mean=nothing, dims::Int=1,
129
+ [weights::AbstractWeights])
160
130
161
131
Compute the covariance matrix of the matrix `X` along dimension `dims`
162
132
using estimator `ce`. A weighting vector `w` can be specified.
@@ -170,18 +140,16 @@ The keyword argument `mean` can be:
170
140
* when `dims=2`, an `AbstractVector` of length `N` or an `AbstractMatrix`
171
141
of size `(N,1)`.
172
142
"""
173
- cov (ce:: CovarianceEstimator , X:: AbstractMatrix ; mean= nothing , dims:: Int = 1 ) =
143
+ cov (ce:: CovarianceEstimator , X:: AbstractMatrix ; mean= nothing , dims:: Int = 1 ,
144
+ weights:: Union{AbstractWeights, Nothing} = nothing ) =
174
145
error (" cov is not defined for $(typeof (ce)) and $(typeof (X)) " )
175
146
176
- cov (ce:: CovarianceEstimator , X:: AbstractMatrix , w:: AbstractWeights ; mean= nothing , dims:: Int = 1 ) =
177
- error (" cov is not defined for $(typeof (ce)) , $(typeof (X)) and $(typeof (w)) " )
178
-
179
147
"""
180
148
SimpleCovariance(;corrected::Bool=false)
181
149
182
150
Simple covariance estimator. Estimation calls `cov(x; corrected=corrected)`,
183
- `cov(x, y; corrected=corrected)` or `cov(X, w, dims; corrected=corrected)`
184
- where `x`, `y` are vectors, `X` is a matrix and `w ` is a weighting vector.
151
+ `cov(x, y; corrected=corrected)` or `cov(X, dims=dims, weights=weights, corrected=corrected)`
152
+ where `x`, `y` are vectors, `X` is a matrix and `weights ` is a weighting vector.
185
153
"""
186
154
struct SimpleCovariance <: CovarianceEstimator
187
155
corrected:: Bool
@@ -194,20 +162,13 @@ cov(sc::SimpleCovariance, x::AbstractVector) =
194
162
cov (sc:: SimpleCovariance , x:: AbstractVector , y:: AbstractVector ) =
195
163
cov (x, y; corrected= sc. corrected)
196
164
197
- function cov (sc:: SimpleCovariance , X:: AbstractMatrix ; dims:: Int = 1 , mean= nothing )
198
- dims ∈ (1 , 2 ) || throw (ArgumentError (" Argument dims can only be 1 or 2 (given: $dims )" ))
199
- if mean === nothing
200
- return cov (X; dims= dims, corrected= sc. corrected)
201
- else
202
- return covm (X, mean, dims, corrected= sc. corrected)
203
- end
204
- end
205
-
206
- function cov (sc:: SimpleCovariance , X:: AbstractMatrix , w:: AbstractWeights ; dims:: Int = 1 , mean= nothing )
165
+ function cov (sc:: SimpleCovariance , X:: AbstractMatrix ;
166
+ dims:: Int = 1 ,
167
+ weights:: Union{AbstractWeights, Nothing} = nothing ,
168
+ mean= nothing )
207
169
dims ∈ (1 , 2 ) || throw (ArgumentError (" Argument dims can only be 1 or 2 (given: $dims )" ))
208
170
if mean === nothing
209
- return cov (X, w, dims, corrected= sc. corrected)
210
- else
211
- return covm (X, mean, w, dims, corrected= sc. corrected)
171
+ mean = Statistics. mean (X, dims= dims, weights= weights)
212
172
end
173
+ return covm (X, mean, weights, dims, corrected= sc. corrected)
213
174
end
0 commit comments