Skip to content

Commit

Permalink
Covariances and correlations
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Sep 25, 2021
1 parent 502ba76 commit 96aba7f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 42 deletions.
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Statistics = "20745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
Documenter = "0.27"
14 changes: 13 additions & 1 deletion docs/src/cov.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# Covariances and Correlations

Functions to computing various types of correlations and covariances are provided.
Functions to computing various types of covariances and correlations are provided.

## Covariance, Correlation and Scatter Matrix

```@docs
cov
cor
scattermat
cov2cor
cor2cov
CovarianceEstimator
SimpleCovariance
```

## Partial Correlation

```@docs
partialcor
```

## Autocovariance and Autocorrelation
Expand Down
18 changes: 15 additions & 3 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using Base: has_offset_axes, require_one_based_indexing

using Printf: @printf

export cor, cov, std, stdm, var, varm, mean!, mean,
export std, stdm, var, varm, mean!, mean,
median!, median, middle, quantile!, quantile,
# moments.jl
skewness, kurtosis,
Expand All @@ -22,12 +22,24 @@ export cor, cov, std, stdm, var, varm, mean!, mean,
weights, aweights, fweights, pweights,
# scalarstats.jl
geomean, harmmean, genmean, mode, modes, percentile, span, variation, sem, mad, mad!,
iqr, genvar, totalvar, entropy, renyientropy, crossentropy, kldivergence, describe

iqr, genvar, totalvar, entropy, renyientropy, crossentropy, kldivergence, describe,
zscore, zscore!,
# cov.jl
cor, cov, scattermat, cov2cor, cor2cov, CovarianceEstimator, SimpleCovariance,
# partialcor.jl
partialcor,
# signalcorr.jl
autocov!, autocov, autocor!, autocor, crosscov!, crosscov, crosscor!, crosscor,
pacf!, pacf

include("common.jl")
include("weights.jl")
include("wsum.jl")
include("moments.jl")
include("scalarstats.jl")
include("cov.jl")
include("partialcor.jl")
include("signalcorr.jl")

##### mean #####

Expand Down
15 changes: 1 addition & 14 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,4 @@ const RealFP = Union{Float32, Float64}
fptype(::Type{T}) where {T<:Union{Float32,Bool,Int8,UInt8,Int16,UInt16}} = Float32
fptype(::Type{T}) where {T<:Union{Float64,Int32,UInt32,Int64,UInt64,Int128,UInt128}} = Float64
fptype(::Type{Complex{Float32}}) = Complex{Float32}
fptype(::Type{Complex{Float64}}) = Complex{Float64}

# A convenient typealias for deprecating default corrected Bool
const DepBool = Union{Bool, Nothing}

function depcheck(fname::Symbol, b::DepBool)
if b == nothing
msg = "$fname will default to corrected=true in the future. Use corrected=false for previous behaviour."
Base.depwarn(msg, fname)
false
else
b
end
end
fptype(::Type{Complex{Float64}}) = Complex{Float64}
26 changes: 2 additions & 24 deletions src/cov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ matrix (`corrected=false`) is computed by multiplying `scattermat(X, w)` by
"""
cov


"""
mean_and_cov(x, [wv::AbstractWeights,] vardim=1; corrected=false) -> (mean, cov)
Return the mean and covariance matrix as a tuple. A weighting
vector `wv` can be specified. `vardim` that designates whether
the variables are columns in the matrix (`1`) or rows (`2`).
Finally, bias correction is applied to the covariance calculation if
`corrected=true`. See [`cov`](@ref) documentation for more details.
"""
function mean_and_cov end

scattermat(x::DenseMatrix; mean=nothing, dims::Int=1) =
_scattermatm(x, mean, dims)
_scattermatm(x::DenseMatrix, ::Nothing, dims::Int) =
Expand All @@ -91,11 +79,11 @@ _scattermatm(x::DenseMatrix, wv::AbstractWeights, mean, dims::Int) =

## weighted cov
covm(x::DenseMatrix, mean, w::AbstractWeights, dims::Int=1;
corrected::DepBool=nothing) =
corrected::Bool=true) =
rmul!(scattermat(x, w, mean=mean, dims=dims), varcorrection(w, depcheck(:covm, corrected)))


cov(x::DenseMatrix, w::AbstractWeights, dims::Int=1; corrected::DepBool=nothing) =
cov(x::DenseMatrix, w::AbstractWeights, dims::Int=1; corrected::Bool=true) =
covm(x, mean(x, w, dims=dims), w, dims; corrected=depcheck(:cov, corrected))

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

function mean_and_cov(x::DenseMatrix, dims::Int=1; corrected::Bool=true)
m = mean(x, dims=dims)
return m, covm(x, m, dims, corrected=corrected)
end
function mean_and_cov(x::DenseMatrix, wv::AbstractWeights, dims::Int=1;
corrected::DepBool=nothing)
m = mean(x, wv, dims=dims)
return m, cov(x, wv, dims; corrected=depcheck(:mean_and_cov, corrected))
end

"""
cov2cor(C, s)
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -891,3 +891,6 @@ end
include("weights.jl")
include("wsum.jl")
include("moments.jl")
include("cov.jl")
include("partialcor.jl")
include("signalcorr.jl")

0 comments on commit 96aba7f

Please sign in to comment.