@@ -32,11 +32,9 @@ _unscaled_covzm(x::DenseMatrix, wv::AbstractWeights, dims::Integer) =
3232 _symmetrize! (unscaled_covzm (x, _scalevars (x, values (wv), dims), dims))
3333
3434"""
35- scattermat(X, [wv::AbstractWeights] ; mean=nothing, dims=1)
35+ scattermat(X; mean=nothing, dims=1[, weights::AbstractWeights] )
3636
3737Compute the scatter matrix, which is an unnormalized covariance matrix.
38- A weighting vector `wv` can be specified to weight
39- the estimate.
4038
4139# Arguments
4240* `mean=nothing`: a known mean value. `nothing` indicates that the mean is
@@ -45,62 +43,33 @@ the estimate.
4543* `dims=1`: the dimension along which the variables are organized.
4644 When `dims = 1`, the variables are considered columns with observations in rows;
4745 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 ) =
7154 _unscaled_covzm (x .- mean, dims)
7255
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)
7960
8061# # weighted cov
81- covm (x:: DenseMatrix , mean, w :: AbstractWeights , dims:: Int = 1 ;
62+ covm (x:: DenseMatrix , mean, weights :: AbstractWeights , dims:: Int = 1 ;
8263 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))
8566
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 )
9270 cov2cor! (c, s)
9371end
9472
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-
10473"""
10574 cov2cor(C, s)
10675
@@ -156,7 +125,8 @@ cov(ce::CovarianceEstimator, x::AbstractVector, y::AbstractVector) =
156125 error (" cov is not defined for $(typeof (ce)) , $(typeof (x)) and $(typeof (y)) " )
157126
158127"""
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])
160130
161131Compute the covariance matrix of the matrix `X` along dimension `dims`
162132using estimator `ce`. A weighting vector `w` can be specified.
@@ -170,18 +140,16 @@ The keyword argument `mean` can be:
170140 * when `dims=2`, an `AbstractVector` of length `N` or an `AbstractMatrix`
171141 of size `(N,1)`.
172142"""
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 ) =
174145 error (" cov is not defined for $(typeof (ce)) and $(typeof (X)) " )
175146
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-
179147"""
180148 SimpleCovariance(;corrected::Bool=false)
181149
182150Simple 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.
185153"""
186154struct SimpleCovariance <: CovarianceEstimator
187155 corrected:: Bool
@@ -194,20 +162,13 @@ cov(sc::SimpleCovariance, x::AbstractVector) =
194162cov (sc:: SimpleCovariance , x:: AbstractVector , y:: AbstractVector ) =
195163 cov (x, y; corrected= sc. corrected)
196164
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 )
207169 dims ∈ (1 , 2 ) || throw (ArgumentError (" Argument dims can only be 1 or 2 (given: $dims )" ))
208170 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)
212172 end
173+ return covm (X, mean, weights, dims, corrected= sc. corrected)
213174end
0 commit comments