Skip to content

Commit 96aba7f

Browse files
committed
Covariances and correlations
1 parent 502ba76 commit 96aba7f

File tree

6 files changed

+35
-42
lines changed

6 files changed

+35
-42
lines changed

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
Statistics = "20745b16-79ce-11e8-11f9-7d13ad32a3b2"
34

45
[compat]
56
Documenter = "0.27"

docs/src/cov.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
# Covariances and Correlations
22

3-
Functions to computing various types of correlations and covariances are provided.
3+
Functions to computing various types of covariances and correlations are provided.
44

55
## Covariance, Correlation and Scatter Matrix
6+
67
```@docs
78
cov
89
cor
10+
scattermat
11+
cov2cor
12+
cor2cov
13+
CovarianceEstimator
14+
SimpleCovariance
15+
```
16+
17+
## Partial Correlation
18+
19+
```@docs
20+
partialcor
921
```
1022

1123
## Autocovariance and Autocorrelation

src/Statistics.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ using Base: has_offset_axes, require_one_based_indexing
1313

1414
using Printf: @printf
1515

16-
export cor, cov, std, stdm, var, varm, mean!, mean,
16+
export std, stdm, var, varm, mean!, mean,
1717
median!, median, middle, quantile!, quantile,
1818
# moments.jl
1919
skewness, kurtosis,
@@ -22,12 +22,24 @@ export cor, cov, std, stdm, var, varm, mean!, mean,
2222
weights, aweights, fweights, pweights,
2323
# scalarstats.jl
2424
geomean, harmmean, genmean, mode, modes, percentile, span, variation, sem, mad, mad!,
25-
iqr, genvar, totalvar, entropy, renyientropy, crossentropy, kldivergence, describe
26-
25+
iqr, genvar, totalvar, entropy, renyientropy, crossentropy, kldivergence, describe,
26+
zscore, zscore!,
27+
# cov.jl
28+
cor, cov, scattermat, cov2cor, cor2cov, CovarianceEstimator, SimpleCovariance,
29+
# partialcor.jl
30+
partialcor,
31+
# signalcorr.jl
32+
autocov!, autocov, autocor!, autocor, crosscov!, crosscov, crosscor!, crosscor,
33+
pacf!, pacf
34+
35+
include("common.jl")
2736
include("weights.jl")
2837
include("wsum.jl")
2938
include("moments.jl")
3039
include("scalarstats.jl")
40+
include("cov.jl")
41+
include("partialcor.jl")
42+
include("signalcorr.jl")
3143

3244
##### mean #####
3345

src/common.jl

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,4 @@ const RealFP = Union{Float32, Float64}
2525
fptype(::Type{T}) where {T<:Union{Float32,Bool,Int8,UInt8,Int16,UInt16}} = Float32
2626
fptype(::Type{T}) where {T<:Union{Float64,Int32,UInt32,Int64,UInt64,Int128,UInt128}} = Float64
2727
fptype(::Type{Complex{Float32}}) = Complex{Float32}
28-
fptype(::Type{Complex{Float64}}) = Complex{Float64}
29-
30-
# A convenient typealias for deprecating default corrected Bool
31-
const DepBool = Union{Bool, Nothing}
32-
33-
function depcheck(fname::Symbol, b::DepBool)
34-
if b == nothing
35-
msg = "$fname will default to corrected=true in the future. Use corrected=false for previous behaviour."
36-
Base.depwarn(msg, fname)
37-
false
38-
else
39-
b
40-
end
41-
end
28+
fptype(::Type{Complex{Float64}}) = Complex{Float64}

src/cov.jl

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ matrix (`corrected=false`) is computed by multiplying `scattermat(X, w)` by
6363
"""
6464
cov
6565

66-
67-
"""
68-
mean_and_cov(x, [wv::AbstractWeights,] vardim=1; corrected=false) -> (mean, cov)
69-
70-
Return the mean and covariance matrix as a tuple. A weighting
71-
vector `wv` can be specified. `vardim` that designates whether
72-
the variables are columns in the matrix (`1`) or rows (`2`).
73-
Finally, bias correction is applied to the covariance calculation if
74-
`corrected=true`. See [`cov`](@ref) documentation for more details.
75-
"""
76-
function mean_and_cov end
77-
7866
scattermat(x::DenseMatrix; mean=nothing, dims::Int=1) =
7967
_scattermatm(x, mean, dims)
8068
_scattermatm(x::DenseMatrix, ::Nothing, dims::Int) =
@@ -91,11 +79,11 @@ _scattermatm(x::DenseMatrix, wv::AbstractWeights, mean, dims::Int) =
9179

9280
## weighted cov
9381
covm(x::DenseMatrix, mean, w::AbstractWeights, dims::Int=1;
94-
corrected::DepBool=nothing) =
82+
corrected::Bool=true) =
9583
rmul!(scattermat(x, w, mean=mean, dims=dims), varcorrection(w, depcheck(:covm, corrected)))
9684

9785

98-
cov(x::DenseMatrix, w::AbstractWeights, dims::Int=1; corrected::DepBool=nothing) =
86+
cov(x::DenseMatrix, w::AbstractWeights, dims::Int=1; corrected::Bool=true) =
9987
covm(x, mean(x, w, dims=dims), w, dims; corrected=depcheck(:cov, corrected))
10088

10189
function corm(x::DenseMatrix, mean, w::AbstractWeights, vardim::Int=1)
@@ -113,16 +101,6 @@ Compute the Pearson correlation matrix of `X` along the dimension
113101
cor(x::DenseMatrix, w::AbstractWeights, dims::Int=1) =
114102
corm(x, mean(x, w, dims=dims), w, dims)
115103

116-
function mean_and_cov(x::DenseMatrix, dims::Int=1; corrected::Bool=true)
117-
m = mean(x, dims=dims)
118-
return m, covm(x, m, dims, corrected=corrected)
119-
end
120-
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, dims::Int=1;
121-
corrected::DepBool=nothing)
122-
m = mean(x, wv, dims=dims)
123-
return m, cov(x, wv, dims; corrected=depcheck(:mean_and_cov, corrected))
124-
end
125-
126104
"""
127105
cov2cor(C, s)
128106

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,6 @@ end
891891
include("weights.jl")
892892
include("wsum.jl")
893893
include("moments.jl")
894+
include("cov.jl")
895+
include("partialcor.jl")
896+
include("signalcorr.jl")

0 commit comments

Comments
 (0)