Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Commit d29fb2fd authored by Dahua Lin's avatar Dahua Lin

Some renaming of types for multivariate normal distributions

- The generic type that can take different covariance types is renamed to ``GenericMultivariateNormal``
- ``MultivariateNormal`` is now alias of ``GenericMultivariateNormal{PDMat}``, which refers to those with full covariance
- introduce ``DiagNormal`` as the alias of ``GenericMultivariateNormal{PDiagMat}``, which refers to those with diagonal covariance
- introduce ``IsoNormal`` as the alias of ``GenericMultivariateNormal{ScalMat}``, which refers to those with isotropic covariance
- introduce a convenient function ``gmvnormal`` to construct normal distributions of different types using simplified syntax
parent 6f5a267a
......@@ -36,6 +36,7 @@ export
Chi,
Chisq,
Cosine,
DiagNormal,
Dirichlet,
DiscreteUniform,
DoubleExponential,
......@@ -52,7 +53,8 @@ export
HyperGeometric,
InverseWishart,
InverseGamma,
InverseGaussian,
InverseGaussian,
IsoNormal,
Kolmogorov,
KSDist,
KSOneSided,
......@@ -103,6 +105,7 @@ export
fit_mle!, # fit a distribution to data using MLE (inplace update to initial guess)
fit_map, # fit a distribution to data using MAP
freecumulant, # free cumulants of distribution
gmvnormal, # a generic function to construct multivariate normal distributions
insupport, # predicate, is x in the support of the distribution?
invlogccdf, # complementary quantile based on log probability
invlogcdf, # quantile based on log probability
......
This diff is collapsed.
immutable Normal <: ContinuousUnivariateDistribution
μ::Float64
σ::Float64
function Normal(μ::Real, σ::Real)
σ > zero(σ) || error("std.dev. must be positive")
new(float64(μ), float64(σ))
end
Normal(μ::Real) = Normal(float64(μ), 1.0)
Normal() = Normal(0.0, 1.0)
end
Normal(μ::Real) = Normal(float64(μ), 1.0)
Normal() = Normal(0.0, 1.0)
@_jl_dist_2p Normal norm
......
......@@ -2,6 +2,15 @@
type NoArgCheck end
function allfinite{T<:Real}(x::Array{T})
for i = 1 : length(x)
if !(isfinite(x[i]))
return false
end
end
return true
end
function isprobvec(p::Vector{Float64})
s = 0.
for i = 1:length(p)
......
......@@ -4,8 +4,6 @@ import NumericExtensions
import NumericExtensions.ScalMat
import NumericExtensions.PDiagMat
import NumericExtensions.PDMat
import NumericExtensions.vbroadcast
import NumericExtensions.Subtract
using Distributions
using Base.Test
......@@ -21,32 +19,32 @@ x = rand(3, 100)
# SGauss
gs = MvNormal(mu, sqrt(2.0))
@test isa(gs, MvNormal{ScalMat})
gs = gmvnormal(mu, sqrt(2.0))
@test isa(gs, IsoNormal)
@test dim(gs) == 3
@test mean(gs) == mode(gs) == mu
@test_approx_eq cov(gs) diagm(fill(2.0, 3))
@test var(gs) == diag(cov(gs))
@test_approx_eq entropy(gs) 0.5 * logdet(2π * e * cov(gs))
gsz = MvNormal(3, sqrt(2.0))
@test isa(gsz, MvNormal{ScalMat})
gsz = gmvnormal(3, sqrt(2.0))
@test isa(gsz, IsoNormal)
@test dim(gsz) == 3
@test mean(gsz) == zeros(3)
@test gsz.zeromean
# DGauss
gd = MvNormal(mu, sqrt(va))
@test isa(gd, MvNormal{PDiagMat})
gd = gmvnormal(mu, sqrt(va))
@test isa(gd, DiagNormal)
@test dim(gd) == 3
@test mean(gd) == mode(gd) == mu
@test_approx_eq cov(gd) diagm(va)
@test var(gd) == diag(cov(gd))
@test_approx_eq entropy(gd) 0.5 * logdet(2π * e * cov(gd))
gdz = MvNormal(PDiagMat(va))
@test isa(gdz, MvNormal{PDiagMat})
gdz = gmvnormal(PDiagMat(va))
@test isa(gdz, DiagNormal)
@test dim(gdz) == 3
@test mean(gdz) == zeros(3)
@test gdz.zeromean
......@@ -54,7 +52,7 @@ gdz = MvNormal(PDiagMat(va))
# Gauss
gf = MvNormal(mu, C)
@test isa(gf, MvNormal{PDMat})
@test isa(gf, MvNormal)
@test dim(gf) == 3
@test mean(gf) == mode(gf) == mu
@test cov(gf) == C
......@@ -62,7 +60,7 @@ gf = MvNormal(mu, C)
@test_approx_eq entropy(gf) 0.5 * logdet(2π * e * cov(gf))
gfz = MvNormal(C)
@test isa(gfz, MvNormal{PDMat})
@test isa(gfz, MvNormal)
@test dim(gfz) == 3
@test mean(gfz) == zeros(3)
@test gfz.zeromean
......@@ -191,36 +189,26 @@ mu, C = _gauss_mle(x, w)
@test_approx_eq mean(g) mu
@test_approx_eq cov(g) C
g = fit(MvNormal{ScalMat}, x)
g = fit(IsoNormal, x)
mu, C = _gauss_mle(x)
@test_approx_eq g.μ mu
@test_approx_eq g.Σ.value mean(diag(C))
g = fit_mle(MvNormal{ScalMat}, x, w)
g = fit_mle(IsoNormal, x, w)
mu, C = _gauss_mle(x, w)
@test_approx_eq g.μ mu
@test_approx_eq g.Σ.value mean(diag(C))
g = fit(MvNormal{PDiagMat}, x)
g = fit(DiagNormal, x)
mu, C = _gauss_mle(x)
@test_approx_eq g.μ mu
@test_approx_eq g.Σ.diag diag(C)
g = fit_mle(MvNormal{PDiagMat}, x, w)
g = fit_mle(DiagNormal, x, w)
mu, C = _gauss_mle(x, w)
@test_approx_eq g.μ mu
@test_approx_eq g.Σ.diag diag(C)
g = fit(MvNormal{PDMat}, x)
mu, C = _gauss_mle(x)
@test_approx_eq g.μ mu
@test_approx_eq g.Σ.mat C
g = fit_mle(MvNormal{PDMat}, x, w)
mu, C = _gauss_mle(x, w)
@test_approx_eq g.μ mu
@test_approx_eq g.Σ.mat C
##### Sufficient statistics type
n = 3
......@@ -233,9 +221,10 @@ ssw = suffstats(MvNormal, X, w)
s_t = sum(X, 2)
ws_t = sum(Xw, 2)
tmp = vbroadcast(Subtract(), X, s_t./n, 1)
tmp = X .- (s_t ./ n)
ss_t = tmp*tmp'
tmp = vbroadcast(Subtract(), X, sum(Xw,2)./sum(w), 1)
tmp = X .- sum(Xw, 2) ./ sum(w)
wss_t = (tmp*diagm(w))*tmp'
tw_t = length(w)
wtw_t = sum(w)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment