Skip to content

Commit

Permalink
Update to changes in Base's cov and cor
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasnoack committed Oct 9, 2015
1 parent a7de8c0 commit 3e9739d
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 56 deletions.
68 changes: 51 additions & 17 deletions src/cov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,60 @@ end

scattermat_zm(x::DenseMatrix, vardim::Int) = Base.unscaled_covzm(x, vardim)

scattermat_zm(x::DenseMatrix, wv::WeightVec, vardim::Int) =
scattermat_zm(x::DenseMatrix, wv::WeightVec, vardim::Int) =
_symmetrize!(Base.unscaled_covzm(x, _scalevars(x, values(wv), vardim), vardim))

function scattermat(x::DenseMatrix; mean=nothing, vardim::Int=1)
mean == 0 ? scattermat_zm(x, vardim) :
mean == nothing ? scattermat_zm(x .- Base.mean(x, vardim), vardim) :
scattermat_zm(x .- mean, vardim)
end
if VERSION < v"0.4.0-dev+660"
function scattermat(x::DenseMatrix; mean=nothing, vardim::Int=1)
mean == 0 ? scattermat_zm(x, vardim) :
mean == nothing ? scattermat_zm(x .- Base.mean(x, vardim), vardim) :
scattermat_zm(x .- mean, vardim)
end

function scattermat(x::DenseMatrix, wv::WeightVec; mean=nothing, vardim::Int=1)
mean == 0 ? scattermat_zm(x, wv, vardim) :
mean == nothing ? scattermat_zm(x .- Base.mean(x, wv, vardim), wv, vardim) :
scattermat_zm(x .- mean, wv, vardim)
end
function scattermat(x::DenseMatrix, wv::WeightVec; mean=nothing, vardim::Int=1)
mean == 0 ? scattermat_zm(x, wv, vardim) :
mean == nothing ? scattermat_zm(x .- Base.mean(x, wv, vardim), wv, vardim) :
scattermat_zm(x .- mean, wv, vardim)
end

## weighted cov
Base.cov(x::DenseMatrix, wv::WeightVec; mean=nothing, vardim::Int=1) =
scale!(scattermat(x, wv; mean=mean, vardim=vardim), inv(sum(wv)))

function mean_and_cov(x::DenseMatrix; vardim::Int=1)
m = mean(x, vardim)
return m, Base.covm(x, m; vardim=vardim)
end
function mean_and_cov(x::DenseMatrix, wv::WeightVec; vardim::Int=1)
m = mean(x, wv, vardim)
return m, Base.cov(x, wv; mean=m, vardim=vardim)
end
else
scattermatm(x::DenseMatrix, mean, vardim::Int=1) =
scattermat_zm(x .- mean, vardim)

scattermatm(x::DenseMatrix, mean, wv::WeightVec, vardim::Int=1) =
scattermat_zm(x .- mean, wv, vardim)

scattermat(x::DenseMatrix, vardim::Int=1) =
scattermatm(x, Base.mean(x, vardim), vardim)

scattermat(x::DenseMatrix, wv::WeightVec, vardim::Int=1) =
scattermatm(x, Base.mean(x, wv, vardim), wv, vardim)

## weighted cov
## weighted cov
Base.covm(x::DenseMatrix, mean, wv::WeightVec, vardim::Int=1) =
scale!(scattermatm(x, mean, wv, vardim), inv(sum(wv)))

Base.cov(x::DenseMatrix, wv::WeightVec; mean=nothing, vardim::Int=1) =
scale!(scattermat(x, wv; mean=mean, vardim=vardim), inv(sum(wv)))
Base.cov(x::DenseMatrix, wv::WeightVec, vardim::Int=1) =
Base.covm(x, Base.mean(x, wv, vardim), wv, vardim)

mean_and_cov(x::DenseMatrix; vardim::Int=1) = (m = mean(x, vardim); (m, Base.covm(x, m; vardim=vardim)))
mean_and_cov(x::DenseMatrix, wv::WeightVec; vardim::Int=1) =
(m = mean(x, wv, vardim); (m, Base.cov(x, wv; mean=m, vardim=vardim)))
function mean_and_cov(x::DenseMatrix, vardim::Int=1)
m = mean(x, vardim)
return m, Base.covm(x, m, vardim)
end
function mean_and_cov(x::DenseMatrix, wv::WeightVec, vardim::Int=1)
m = mean(x, wv, vardim)
return m, Base.cov(x, wv, vardim)
end
end
136 changes: 97 additions & 39 deletions test/cov.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,61 +31,119 @@ Sz2w = X * diagm(w2) * X'

## scattermat

@test_approx_eq scattermat(X) S1
@test_approx_eq scattermat(X; vardim=2) S2
if VERSION < v"0.4.0-dev+660"
@test_approx_eq scattermat(X) S1
@test_approx_eq scattermat(X; vardim=2) S2

@test_approx_eq scattermat(X; mean=0) Sz1
@test_approx_eq scattermat(X; mean=0, vardim=2) Sz2
@test_approx_eq scattermat(X; mean=0) Sz1
@test_approx_eq scattermat(X; mean=0, vardim=2) Sz2

@test_approx_eq scattermat(X; mean=mean(X,1)) S1
@test_approx_eq scattermat(X; mean=mean(X,2), vardim=2) S2
@test_approx_eq scattermat(X; mean=mean(X,1)) S1
@test_approx_eq scattermat(X; mean=mean(X,2), vardim=2) S2

@test_approx_eq scattermat(X; mean=zeros(1,8)) Sz1
@test_approx_eq scattermat(X; mean=zeros(3), vardim=2) Sz2
@test_approx_eq scattermat(X; mean=zeros(1,8)) Sz1
@test_approx_eq scattermat(X; mean=zeros(3), vardim=2) Sz2

## weighted scatter mat
## weighted scatter mat

@test_approx_eq scattermat(X, wv1) S1w
@test_approx_eq scattermat(X, wv2; vardim=2) S2w
@test_approx_eq scattermat(X, wv1) S1w
@test_approx_eq scattermat(X, wv2; vardim=2) S2w

@test_approx_eq scattermat(X, wv1; mean=0) Sz1w
@test_approx_eq scattermat(X, wv2; mean=0, vardim=2) Sz2w
@test_approx_eq scattermat(X, wv1; mean=0) Sz1w
@test_approx_eq scattermat(X, wv2; mean=0, vardim=2) Sz2w

@test_approx_eq scattermat(X, wv1; mean=mean(X, wv1, 1)) S1w
@test_approx_eq scattermat(X, wv2; mean=mean(X, wv2, 2), vardim=2) S2w
@test_approx_eq scattermat(X, wv1; mean=mean(X, wv1, 1)) S1w
@test_approx_eq scattermat(X, wv2; mean=mean(X, wv2, 2), vardim=2) S2w

@test_approx_eq scattermat(X, wv1; mean=zeros(1,8)) Sz1w
@test_approx_eq scattermat(X, wv2; mean=zeros(3), vardim=2) Sz2w
@test_approx_eq scattermat(X, wv1; mean=zeros(1,8)) Sz1w
@test_approx_eq scattermat(X, wv2; mean=zeros(3), vardim=2) Sz2w
else
@test_approx_eq scattermat(X) S1
@test_approx_eq scattermat(X, 2) S2

# weighted covariance
@test_approx_eq StatsBase.scattermatm(X, 0) Sz1
@test_approx_eq StatsBase.scattermatm(X, 0, 2) Sz2

@test_approx_eq cov(X, wv1) S1w ./ sum(wv1)
@test_approx_eq cov(X, wv2; vardim=2) S2w ./ sum(wv2)
@test_approx_eq StatsBase.scattermatm(X, mean(X,1)) S1
@test_approx_eq StatsBase.scattermatm(X, mean(X,2), 2) S2

@test_approx_eq cov(X, wv1; mean=0) Sz1w ./ sum(wv1)
@test_approx_eq cov(X, wv2; mean=0, vardim=2) Sz2w ./ sum(wv2)
@test_approx_eq StatsBase.scattermatm(X, zeros(1,8)) Sz1
@test_approx_eq StatsBase.scattermatm(X, zeros(3), 2) Sz2

@test_approx_eq cov(X, wv1; mean=mean(X, wv1, 1)) S1w ./ sum(wv1)
@test_approx_eq cov(X, wv2; mean=mean(X, wv2, 2), vardim=2) S2w ./ sum(wv2)
## weighted scatter mat

@test_approx_eq cov(X, wv1; mean=zeros(1,8)) Sz1w ./ sum(wv1)
@test_approx_eq cov(X, wv2; mean=zeros(3), vardim=2) Sz2w ./ sum(wv2)
@test_approx_eq scattermat(X, wv1) S1w
@test_approx_eq scattermat(X, wv2, 2) S2w

# mean_and_cov
@test_approx_eq StatsBase.scattermatm(X, 0, wv1) Sz1w
@test_approx_eq StatsBase.scattermatm(X, 0, wv2, 2) Sz2w

@test_approx_eq StatsBase.scattermatm(X, mean(X, wv1, 1), wv1) S1w
@test_approx_eq StatsBase.scattermatm(X, mean(X, wv2, 2), wv2, 2) S2w

@test_approx_eq StatsBase.scattermatm(X, zeros(1,8), wv1) Sz1w
@test_approx_eq StatsBase.scattermatm(X, zeros(3), wv2, 2) Sz2w
end

# weighted covariance

if VERSION < v"0.4.0-dev+660"
@test_approx_eq cov(X, wv1) S1w ./ sum(wv1)
@test_approx_eq cov(X, wv2; vardim=2) S2w ./ sum(wv2)

@test_approx_eq cov(X, wv1; mean=0) Sz1w ./ sum(wv1)
@test_approx_eq cov(X, wv2; mean=0, vardim=2) Sz2w ./ sum(wv2)

(m, C) = mean_and_cov(X; vardim=1)
@test m == mean(X, 1)
@test C == cov(X; vardim=1)
@test_approx_eq cov(X, wv1; mean=mean(X, wv1, 1)) S1w ./ sum(wv1)
@test_approx_eq cov(X, wv2; mean=mean(X, wv2, 2), vardim=2) S2w ./ sum(wv2)

(m, C) = mean_and_cov(X; vardim=2)
@test m == mean(X, 2)
@test C == cov(X; vardim=2)
@test_approx_eq cov(X, wv1; mean=zeros(1,8)) Sz1w ./ sum(wv1)
@test_approx_eq cov(X, wv2; mean=zeros(3), vardim=2) Sz2w ./ sum(wv2)
else
@test_approx_eq cov(X, wv1) S1w ./ sum(wv1)
@test_approx_eq cov(X, wv2, 2) S2w ./ sum(wv2)

(m, C) = mean_and_cov(X, wv1; vardim=1)
@test m == mean(X, wv1, 1)
@test C == cov(X, wv1; vardim=1)
@test_approx_eq Base.covm(X, 0, wv1) Sz1w ./ sum(wv1)
@test_approx_eq Base.covm(X, 0, wv2, 2) Sz2w ./ sum(wv2)

(m, C) = mean_and_cov(X, wv2; vardim=2)
@test m == mean(X, wv2, 2)
@test C == cov(X, wv2; vardim=2)
@test_approx_eq Base.covm(X, mean(X, wv1, 1), wv1) S1w ./ sum(wv1)
@test_approx_eq Base.covm(X, mean(X, wv2, 2), wv2, 2) S2w ./ sum(wv2)

@test_approx_eq Base.covm(X, zeros(1,8), wv1) Sz1w ./ sum(wv1)
@test_approx_eq Base.covm(X, zeros(3), wv2, 2) Sz2w ./ sum(wv2)
end

# mean_and_cov
if VERSION < v"0.4.0-dev+660"
(m, C) = mean_and_cov(X; vardim=1)
@test m == mean(X, 1)
@test C == cov(X; vardim=1)

(m, C) = mean_and_cov(X; vardim=2)
@test m == mean(X, 2)
@test C == cov(X; vardim=2)

(m, C) = mean_and_cov(X, wv1; vardim=1)
@test m == mean(X, wv1, 1)
@test C == cov(X, wv1; vardim=1)

(m, C) = mean_and_cov(X, wv2; vardim=2)
@test m == mean(X, wv2, 2)
@test C == cov(X, wv2; vardim=2)
else
(m, C) = mean_and_cov(X, 1)
@test m == mean(X, 1)
@test C == cov(X, 1)

(m, C) = mean_and_cov(X, 2)
@test m == mean(X, 2)
@test C == cov(X, 2)

(m, C) = mean_and_cov(X, wv1, 1)
@test m == mean(X, wv1, 1)
@test C == cov(X, wv1, 1)

(m, C) = mean_and_cov(X, wv2, 2)
@test m == mean(X, wv2, 2)
@test C == cov(X, wv2, 2)
end

0 comments on commit 3e9739d

Please sign in to comment.