Skip to content

Commit 459b2b4

Browse files
authored
Small fixes for FitMvNormal (#257)
* Some fixes for MvNormal * Bump patch version * Add tests * Modify condition
1 parent 7cf71e6 commit 459b2b4

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "OnlineStats"
22
uuid = "a15396b6-48d5-5d58-9928-6d29437db91e"
3-
version = "1.6.0"
3+
version = "1.6.1"
44

55
[deps]
66
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/stats/distributions.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,14 @@ nobs(o::FitMvNormal) = nobs(o.cov)
198198
_fit!(o::FitMvNormal, y) = _fit!(o.cov, y)
199199
function value(o::FitMvNormal)
200200
c = cov(o.cov)
201-
if isposdef(c)
201+
if isposdef(c) || (iszero(c) && nobs(o) > 1)
202202
return mean(o.cov), c
203203
else
204204
return zeros(nvars(o)), Matrix(1.0I, nvars(o), nvars(o))
205205
end
206206
end
207207
_merge!(o::FitMvNormal, o2::FitMvNormal) = _merge!(o.cov, o2.cov)
208+
209+
Statistics.mean(o::FitMvNormal) = mean(o.cov)
210+
Statistics.var(o::FitMvNormal) = var(o.cov)
211+
Statistics.cov(o::FitMvNormal) = cov(o.cov)

test/runtests.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,14 @@ end
127127
end
128128
@testset "FitMvNormal" begin
129129
@test value(FitMvNormal(2)) == (zeros(2), Matrix(I, 2, 2))
130-
a, b = mergevals(FitMvNormal(2), OnlineStatsBase.eachrow([y y2]), OnlineStatsBase.eachrow([y2 y]))
131-
@test a[1] b[1]
132-
@test a[2] b[2]
130+
a, b = mergestats(FitMvNormal(2), OnlineStatsBase.eachrow([y y2]), OnlineStatsBase.eachrow([y2 y]))
131+
@test value(a)[1] value(b)[1]
132+
@test value(a)[2] value(b)[2]
133+
134+
@test all(mean(a) .≈ mean(ys))
135+
@test all(var(a) .≈ var(ys))
136+
@test cov(a)[1] cov(a)[4] var(ys)
137+
@test cov(a)[2] cov(a)[3]
133138
end
134139
end
135140
#-----------------------------------------------------------------------# FastNode

0 commit comments

Comments
 (0)