Skip to content

Commit 43a7c6f

Browse files
committed
Added documentation about corrected argument in cov and mean_and_cov docstring. Also, added deprecation for cov corrected=false behaviour.
1 parent c1aef99 commit 43a7c6f

File tree

3 files changed

+87
-44
lines changed

3 files changed

+87
-44
lines changed

src/cov.jl

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ function _scalevars(x::DenseMatrix, s::DenseVector, vardim::Int)
2222
end
2323

2424
## scatter matrix
25-
26-
scattermat_zm(x::DenseMatrix, vardim::Int) = Base.unscaled_covzm(x, vardim)
27-
28-
scattermat_zm(x::DenseMatrix, wv::AbstractWeights, vardim::Int) =
29-
_symmetrize!(Base.unscaled_covzm(x, _scalevars(x, values(wv), vardim), vardim))
30-
31-
3225
"""
3326
scattermat(X, [wv::AbstractWeights]; mean=nothing, vardim=1)
3427
@@ -44,28 +37,6 @@ that the data are centered and hence there's no need to subtract the mean.
4437
When `vardim = 1`, the variables are considered columns with observations in rows;
4538
when `vardim = 2`, variables are in rows with observations in columns.
4639
"""
47-
function scattermat end
48-
49-
50-
"""
51-
cov(X, wv::AbstractWeights; mean=nothing, vardim=1)
52-
53-
Compute the weighted covariance matrix. By default, the covariance
54-
matrix is normalized by the sum of the weights. That is, `cov(X, wv)`
55-
is equivalent to `scattermat(X, wv) / sum(wv)`.
56-
"""
57-
cov
58-
59-
60-
"""
61-
mean_and_cov(x, [wv::AbstractWeights]; vardim=1) -> (mean, cov)
62-
63-
Return the mean and covariance matrix as a tuple. A weighting
64-
vector `wv` can be specified. `vardim` that designates whether
65-
the variables are columns in the matrix (`1`) or rows (`2`).
66-
"""
67-
function mean_and_cov end
68-
6940
scattermatm(x::DenseMatrix, mean, vardim::Int=1) =
7041
scattermat_zm(x .- mean, vardim)
7142

@@ -78,19 +49,61 @@ scattermat(x::DenseMatrix, vardim::Int=1) =
7849
scattermat(x::DenseMatrix, wv::AbstractWeights, vardim::Int=1) =
7950
scattermatm(x, Base.mean(x, wv, vardim), wv, vardim)
8051

81-
## weighted cov
82-
Base.covm(x::DenseMatrix, mean, wv::AbstractWeights, vardim::Int=1, corrected::Bool=false) =
83-
scale!(scattermatm(x, mean, wv, vardim), varcorrection(wv, corrected))
52+
scattermat_zm(x::DenseMatrix, vardim::Int) = Base.unscaled_covzm(x, vardim)
53+
54+
scattermat_zm(x::DenseMatrix, wv::AbstractWeights, vardim::Int) =
55+
_symmetrize!(Base.unscaled_covzm(x, _scalevars(x, values(wv), vardim), vardim))
56+
57+
"""
58+
cov(X, wv::AbstractWeights, [vardim, corrected])
8459
85-
Base.cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int=1; corrected=false) =
60+
Compute the weighted covariance matrix. Similar to `var` and `std` the biased covariance
61+
matrix (`corrected=false`) can be computed by multiplying `scattermat(X, wv)` by
62+
``\frac{1}{\sum{w}}`` to normalize. However, the unbiased covariance matrix
63+
(`corrected=true`) is dependent on the type of weights used:
64+
65+
* AnalyticWeights: ``\\frac{1}{\sum w - \sum {w^2} / \sum{w}^2}``
66+
* FrequencyWeights: ``\\frac{1}{\sum{w} - 1}``
67+
* ProbabilityWeights: ``\\frac{n}{(n - 1) \sum w}`` where `n = length(w)`
68+
"""
69+
Base.cov(x::DenseMatrix, wv::AbstractWeights, corrected::Bool) =
70+
Base.covm(x, Base.mean(x, wv, 1), wv, 1, corrected)
71+
72+
Base.cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int, corrected::Bool) =
8673
Base.covm(x, Base.mean(x, wv, vardim), wv, vardim, corrected)
8774

88-
function mean_and_cov(x::DenseMatrix, vardim::Int=1; corrected=false)
75+
Base.covm(x::DenseMatrix, mean, wv::AbstractWeights, corrected::Bool) =
76+
scale!(scattermatm(x, mean, wv, 1), varcorrection(wv, corrected))
77+
78+
Base.covm(x::DenseMatrix, mean, wv::AbstractWeights, vardim::Int, corrected::Bool) =
79+
scale!(scattermatm(x, mean, wv, vardim), varcorrection(wv, corrected))
80+
81+
"""
82+
mean_and_cov(x, [wv::AbstractWeights, vardim, corrected]) -> (mean, cov)
83+
84+
Return the mean and covariance matrix as a tuple. A weighting
85+
vector `wv` can be specified. `vardim` that designates whether
86+
the variables are columns in the matrix (`1`) or rows (`2`).
87+
Finally, bias correction can be applied to the covariance calculation if
88+
`corrected=true`.
89+
See `cov` documentation for more details.
90+
"""
91+
function mean_and_cov(x::DenseMatrix, corrected::Bool=true)
92+
m = mean(x, 1)
93+
return m, Base.covm(x, m, 1, corrected)
94+
end
95+
96+
function mean_and_cov(x::DenseMatrix, vardim::Int, corrected::Bool=true)
8997
m = mean(x, vardim)
9098
return m, Base.covm(x, m, vardim, corrected)
9199
end
92100

93-
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int=1; corrected=false)
101+
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, corrected::Bool)
102+
m = mean(x, wv, 1)
103+
return m, Base.cov(x, wv, 1, corrected)
104+
end
105+
106+
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int, corrected::Bool)
94107
m = mean(x, wv, vardim)
95-
return m, Base.cov(x, wv, vardim; corrected=corrected)
108+
return m, Base.cov(x, wv, vardim, corrected)
96109
end

src/deprecates.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,33 @@ function mean_and_std(A::RealArray, wv::AbstractWeights, dim::Int)
156156
s = stdm(A, wv, m, dim, true)
157157
m, s
158158
end
159+
160+
function Base.cov(x::DenseMatrix, wv::AbstractWeights)
161+
Base.depwarn(_correction_dep_msg("`cov`"), :cov)
162+
Base.covm(x, Base.mean(x, wv, 1), wv, 1, false)
163+
end
164+
165+
function Base.cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int)
166+
Base.depwarn(_correction_dep_msg("`cov`"), :cov)
167+
Base.covm(x, Base.mean(x, wv, vardim), wv, vardim, false)
168+
end
169+
170+
function Base.covm(x::DenseMatrix, mean, wv::AbstractWeights)
171+
Base.depwarn(_correction_dep_msg("`covm`"), :covm)
172+
scale!(scattermatm(x, mean, wv, 1), varcorrection(wv, false))
173+
end
174+
175+
function Base.covm(x::DenseMatrix, mean, wv::AbstractWeights, vardim::Int)
176+
Base.depwarn(_correction_dep_msg("`covm`"), :covm)
177+
scale!(scattermatm(x, mean, wv, vardim), varcorrection(wv, false))
178+
end
179+
180+
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights)
181+
m = mean(x, wv, 1)
182+
return m, Base.cov(x, wv, 1)
183+
end
184+
185+
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, vardim::Int)
186+
m = mean(x, wv, vardim)
187+
return m, Base.cov(x, wv, vardim)
188+
end

test/cov.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ Sz2w = X * diagm(w2) * X'
6060

6161
# weighted covariance
6262

63-
@test cov(X, wv1; corrected=false) S1w ./ sum(wv1)
64-
@test cov(X, wv2, 2; corrected=false) S2w ./ sum(wv2)
63+
@test cov(X, wv1, false) S1w ./ sum(wv1)
64+
@test cov(X, wv2, 2, false) S2w ./ sum(wv2)
6565

6666
@test Base.covm(X, 0, wv1, 1, false) Sz1w ./ sum(wv1)
6767
@test Base.covm(X, 0, wv2, 2, false) Sz2w ./ sum(wv2)
@@ -74,18 +74,18 @@ Sz2w = X * diagm(w2) * X'
7474

7575
# mean_and_cov
7676

77-
(m, C) = mean_and_cov(X, 1; corrected=false)
77+
(m, C) = mean_and_cov(X, 1, false)
7878
@test m == mean(X, 1)
7979
@test C == cov(X, 1, false)
8080

81-
(m, C) = mean_and_cov(X, 2; corrected=false)
81+
(m, C) = mean_and_cov(X, 2, false)
8282
@test m == mean(X, 2)
8383
@test C == cov(X, 2, false)
8484

85-
(m, C) = mean_and_cov(X, wv1, 1; corrected=false)
85+
(m, C) = mean_and_cov(X, wv1, 1, false)
8686
@test m == mean(X, wv1, 1)
87-
@test C == cov(X, wv1, 1; corrected=false)
87+
@test C == cov(X, wv1, 1, false)
8888

89-
(m, C) = mean_and_cov(X, wv2, 2; corrected=false)
89+
(m, C) = mean_and_cov(X, wv2, 2, false)
9090
@test m == mean(X, wv2, 2)
91-
@test C == cov(X, wv2, 2; corrected=false)
91+
@test C == cov(X, wv2, 2, false)

0 commit comments

Comments
 (0)