Skip to content

Commit

Permalink
Small fixes for FitMvNormal (#257)
Browse files Browse the repository at this point in the history
* Some fixes for MvNormal

* Bump patch version

* Add tests

* Modify condition
  • Loading branch information
Crown421 authored Apr 20, 2023
1 parent 7cf71e6 commit 459b2b4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "OnlineStats"
uuid = "a15396b6-48d5-5d58-9928-6d29437db91e"
version = "1.6.0"
version = "1.6.1"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
6 changes: 5 additions & 1 deletion src/stats/distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,14 @@ nobs(o::FitMvNormal) = nobs(o.cov)
_fit!(o::FitMvNormal, y) = _fit!(o.cov, y)
function value(o::FitMvNormal)
c = cov(o.cov)
if isposdef(c)
if isposdef(c) || (iszero(c) && nobs(o) > 1)
return mean(o.cov), c
else
return zeros(nvars(o)), Matrix(1.0I, nvars(o), nvars(o))
end
end
_merge!(o::FitMvNormal, o2::FitMvNormal) = _merge!(o.cov, o2.cov)

Statistics.mean(o::FitMvNormal) = mean(o.cov)
Statistics.var(o::FitMvNormal) = var(o.cov)
Statistics.cov(o::FitMvNormal) = cov(o.cov)
11 changes: 8 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,14 @@ end
end
@testset "FitMvNormal" begin
@test value(FitMvNormal(2)) == (zeros(2), Matrix(I, 2, 2))
a, b = mergevals(FitMvNormal(2), OnlineStatsBase.eachrow([y y2]), OnlineStatsBase.eachrow([y2 y]))
@test a[1] b[1]
@test a[2] b[2]
a, b = mergestats(FitMvNormal(2), OnlineStatsBase.eachrow([y y2]), OnlineStatsBase.eachrow([y2 y]))
@test value(a)[1] value(b)[1]
@test value(a)[2] value(b)[2]

@test all(mean(a) .≈ mean(ys))
@test all(var(a) .≈ var(ys))
@test cov(a)[1] cov(a)[4] var(ys)
@test cov(a)[2] cov(a)[3]
end
end
#-----------------------------------------------------------------------# FastNode
Expand Down

3 comments on commit 459b2b4

@Crown421
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshday Can I register the new version?

@joshday
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/82112

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.6.1 -m "<description of version>" 459b2b4cd1d99caf792a734e52f2306a338baf24
git push origin v1.6.1

Please sign in to comment.