@@ -50,7 +50,7 @@ function scattermat end
50
50
51
51
52
52
"""
53
- cov(X, w::AbstractWeights, vardim=1; mean=nothing, corrected=false)
53
+ cov(X, w::AbstractWeights, vardim=1; mean=nothing, corrected=false)
54
54
55
55
Compute the weighted covariance matrix. Similar to `var` and `std` the biased covariance
56
56
matrix (`corrected=false`) is computed by multiplying `scattermat(X, w)` by
@@ -154,6 +154,15 @@ function cor2cov!(C::AbstractMatrix, s::AbstractArray)
154
154
return C
155
155
end
156
156
157
+ """
158
+ _cov2cor!(C)
159
+
160
+ Compute the correlation matrix from the covariance matrix `C`, in-place.
161
+
162
+ The leading diagonal is used to determine the standard deviations by which to normalise.
163
+ """
164
+ _cov2cor! (C:: AbstractMatrix ) = cov2cor! (C, sqrt .(diag (C)))
165
+
157
166
"""
158
167
CovarianceEstimator
159
168
@@ -198,6 +207,60 @@ cov(ce::CovarianceEstimator, X::AbstractMatrix; mean=nothing, dims::Int=1) =
198
207
cov (ce:: CovarianceEstimator , X:: AbstractMatrix , w:: AbstractWeights ; mean= nothing , dims:: Int = 1 ) =
199
208
error (" cov is not defined for $(typeof (ce)) , $(typeof (X)) and $(typeof (w)) " )
200
209
210
+ """
211
+ var(ce::CovarianceEstimator, x::AbstractVector; mean=nothing)
212
+
213
+ Compute the variance of the vector `x` using the estimator `ce`.
214
+ """
215
+ var (ce:: CovarianceEstimator , x:: AbstractVector ; kwargs... ) = cov (ce, x; kwargs... )
216
+
217
+ """
218
+ std(ce::CovarianceEstimator, x::AbstractVector; mean=nothing)
219
+
220
+ Compute the standard deviation of the vector `x` using the estimator `ce`.
221
+ """
222
+ std (ce:: CovarianceEstimator , x:: AbstractVector ; kwargs... ) = sqrt (var (ce, x; kwargs... ))
223
+
224
+ """
225
+ cor(ce::CovarianceEstimator, x::AbstractVector, y::AbstractVector)
226
+
227
+ Compute the correlation of the vectors `x` and `y` using estimator `ce`.
228
+ """
229
+ function cor (ce:: CovarianceEstimator , x:: AbstractVector , y:: AbstractVector )
230
+ # Here we allow `ce` to see both `x` and `y` simultaneously, and allow it to compute
231
+ # a full covariance matrix, from which we will extract the correlation.
232
+ #
233
+ # Whilst in some cases it might be equivalent (and possibly more efficient) to use:
234
+ # cov(ce, x, y) / (std(ce, x) * std(ce, y)),
235
+ # this need not apply in general.
236
+ return cor (ce, hcat (x, y))[1 , 2 ]
237
+ end
238
+
239
+ """
240
+ cor(
241
+ ce::CovarianceEstimator, X::AbstractMatrix, [w::AbstractWeights];
242
+ mean=nothing, dims::Int=1
243
+ )
244
+
245
+ Compute the correlation matrix of the matrix `X` along dimension `dims`
246
+ using estimator `ce`. A weighting vector `w` can be specified.
247
+ The keyword argument `mean` can be:
248
+
249
+ * `nothing` (default) in which case the mean is estimated and subtracted
250
+ from the data `X`,
251
+ * a precalculated mean in which case it is subtracted from the data `X`.
252
+ Assuming `size(X)` is `(N,M)`, `mean` can either be:
253
+ * when `dims=1`, an `AbstractMatrix` of size `(1,M)`,
254
+ * when `dims=2`, an `AbstractVector` of length `N` or an `AbstractMatrix`
255
+ of size `(N,1)`.
256
+ """
257
+ function cor (ce:: CovarianceEstimator , X:: AbstractMatrix ; kwargs... )
258
+ return _cov2cor! (cov (ce, X; kwargs... ))
259
+ end
260
+ function cor (ce:: CovarianceEstimator , X:: AbstractMatrix , w:: AbstractWeights ; kwargs... )
261
+ return _cov2cor! (cov (ce, X, w; kwargs... ))
262
+ end
263
+
201
264
"""
202
265
SimpleCovariance(;corrected::Bool=false)
203
266
0 commit comments