@@ -22,13 +22,6 @@ function _scalevars(x::DenseMatrix, s::DenseVector, vardim::Int)
22
22
end
23
23
24
24
# # 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
-
32
25
"""
33
26
scattermat(X, [wv::AbstractWeights]; mean=nothing, vardim=1)
34
27
@@ -44,28 +37,6 @@ that the data are centered and hence there's no need to subtract the mean.
44
37
When `vardim = 1`, the variables are considered columns with observations in rows;
45
38
when `vardim = 2`, variables are in rows with observations in columns.
46
39
"""
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
-
69
40
scattermatm (x:: DenseMatrix , mean, vardim:: Int = 1 ) =
70
41
scattermat_zm (x .- mean, vardim)
71
42
@@ -78,19 +49,61 @@ scattermat(x::DenseMatrix, vardim::Int=1) =
78
49
scattermat (x:: DenseMatrix , wv:: AbstractWeights , vardim:: Int = 1 ) =
79
50
scattermatm (x, Base. mean (x, wv, vardim), wv, vardim)
80
51
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])
84
59
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
+ ``\f rac{1}{\s um{w}}`` to normalize. However, the unbiased covariance matrix
63
+ (`corrected=true`) is dependent on the type of weights used:
64
+
65
+ * AnalyticWeights: ``\\ frac{1}{\s um w - \s um {w^2} / \s um{w}^2}``
66
+ * FrequencyWeights: ``\\ frac{1}{\s um{w} - 1}``
67
+ * ProbabilityWeights: ``\\ frac{n}{(n - 1) \s um 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 ) =
86
73
Base. covm (x, Base. mean (x, wv, vardim), wv, vardim, corrected)
87
74
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 )
89
97
m = mean (x, vardim)
90
98
return m, Base. covm (x, m, vardim, corrected)
91
99
end
92
100
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 )
94
107
m = mean (x, wv, vardim)
95
- return m, Base. cov (x, wv, vardim; corrected = corrected)
108
+ return m, Base. cov (x, wv, vardim, corrected)
96
109
end
0 commit comments