Skip to content

Commit

Permalink
Merge pull request JuliaStats#447 from JuliaStats/sb/mixture-cov
Browse files Browse the repository at this point in the history
add covariance for multivariate mixtures
  • Loading branch information
simonbyrne committed Jan 21, 2016
2 parents ae0a846 + b1e2980 commit 710fae9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/mixtures/mixturemodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ function var(d::UnivariateMixture)
return v
end

function cov(d::MultivariateMixture)
K = ncomponents(d)
p = probs(d)
m = zeros(length(d))
md = zeros(length(d))
V = zeros(length(d),length(d))

for i = 1:K
pi = p[i]
if pi > 0.0
c = component(d, i)
BLAS.axpy!(pi, mean(c), m)
BLAS.axpy!(pi, cov(c), V)
end
end
for i = 1:K
pi = p[i]
if pi > 0.0
c = component(d, i)
# todo: use more in-place operations
md = mean(c) - m
BLAS.axpy!(pi, md*md', V)
end
end
return V
end



#### show

Expand Down
1 change: 1 addition & 0 deletions test/mixture.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function test_mixture(g::MultivariateMixture, n::Int, ns::Int)
@test isa(Xs, Matrix{Float64})
@test size(Xs) == (length(g), ns)
@test_approx_eq_eps vec(mean(Xs, 2)) mean(g) 0.01
@test_approx_eq_eps cov(Xs, vardim=2) cov(g) 0.01
end


Expand Down

0 comments on commit 710fae9

Please sign in to comment.