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 708a1552 authored by Dahua Lin's avatar Dahua Lin

Constructor clean up (fix #109).

parent 9a3485bb
......@@ -98,6 +98,7 @@ export # types
isplatykurtic, # Is excess kurtosis > 0.0?
isleptokurtic, # Is excess kurtosis < 0.0?
ismesokurtic, # Is excess kurtosis = 0.0?
isprobvec, # Is a probability vector?
kde, # Kernel density estimator
kurtosis, # kurtosis of the distribution
# linkfun, # link function mapping mu to eta, the linear predictor
......
......@@ -8,7 +8,9 @@ immutable Dirichlet <: ContinuousMultivariateDistribution
lmnB::Float64 = 0.0
for i in 1:length(alpha)
ai = alpha[i]
ai >= 0.0 || throw(DomainError())
if ai <= 0.
throw(ArgumentError("alpha must be a positive vector."))
end
alpha0 += ai
lmnB += lgamma(ai)
end
......
......@@ -2,21 +2,14 @@ immutable Multinomial <: DiscreteMultivariateDistribution
n::Int
prob::Vector{Float64}
function Multinomial{T <: Real}(n::Integer, p::Vector{T})
p = float(p)
if n <= 0
error("Multinomial: n must be positive")
end
sump = 0.0
for i in 1:length(p)
if p[i] < 0.0
error("Multinomial: probabilities must be non-negative")
function Multinomial(n::Integer, p::Vector{Float64}; check::Bool=true)
if check
if n <= 0
throw(ArgumentError("n must be a positive integer."))
end
if !isprobvec(p)
throw(ArgumentError("p must be a probability vector."))
end
sump += p[i]
end
if abs(sump - 1.) > 1.0e-12
multiply!(p, inv(sump))
end
new(int(n), p)
end
......@@ -202,5 +195,5 @@ function fit_mle{T<:Real}(::Type{Multinomial}, X::Matrix{T})
end
n = int(ns[1])
p = vec(mean(X, 2)) * (1.0 / n)
Multinomial(n, p)
Multinomial(n, p; check=false)
end
immutable Categorical <: DiscreteUnivariateDistribution
K::Int
prob::Vector{Float64}
function Categorical{T <: Real}(p::Vector{T})
k = length(p)
k > 1 || error("Categorical: there must be at least one category")
pv = Array(Float64, k)
sump = 0.0
for i in 1:k
tmp = float64(p[i])
tmp >= 0 || error("Categorical: probabilities must be non-negative")
pv[i] = tmp
sump += tmp
end
sump > 0 || error("Categorical: sum(p) > 0")
for i in 1:k
pv[i] /= sump
function Categorical(p::Vector{Float64}; check::Bool=true)
if check && !isprobvec(p)
throw(ArgumentError("p must be a probability vector."))
end
new(k, pv)
new(length(p), p)
end
end
......@@ -150,10 +140,21 @@ end
function fit_mle{T<:Real}(::Type{Categorical}, k::Integer, x::Array{T})
w = zeros(Int, k)
for i in 1:length(x)
n = length(x)
for i in 1:n
w[x[i]] += 1
end
Categorical(w) # the constructor will normalize w
p = Array(Float64, k)
c = 1.0 / n
for i = 1:k
p[i] = w[i] * c
end
Categorical(w; check=false) # the constructor will normalize w
end
fit_mle{T<:Real}(::Type{Categorical}, x::Array{T}) = fit_mle(Categorical, max(x), x)
# Utility functions
function isprobvec(p::Vector{Float64})
s = 0.
for i = 1:length(p)
pi = p[i]
s += pi
if pi < 0
return false
end
end
return abs(s - 1.0) <= 1.0e-12
end
function _randu(Ku::Uint, U::Uint) # ~ U[0:Ku-1]
x = rand(Uint)
while x > U
......
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