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 2939e37e authored by Dahua Lin's avatar Dahua Lin

rename fit to fit_mle, and delegate fit to fit_mle

parent e81a3020
......@@ -77,24 +77,24 @@ As of v0.0.0, the following distributions have been implemented:
N = 100_000
fit(Bernoulli, rand(Bernoulli(0.7), N))
fit_mle(Bernoulli, rand(Bernoulli(0.7), N))
fit(Beta, rand(Beta(1.3, 3.7), N))
fit_mle(Beta, rand(Beta(1.3, 3.7), N))
fit(Binomial, rand(Binomial(N, 0.3)), N)
fit_mle(Binomial, rand(Binomial(N, 0.3)), N)
fit(DiscreteUniform, rand(DiscreteUniform(300_000, 700_000), N))
fit_mle(DiscreteUniform, rand(DiscreteUniform(300_000, 700_000), N))
fit(Exponential, rand(Exponential(0.1), N))
fit_mle(Exponential, rand(Exponential(0.1), N))
fit(Gamma, rand(Gamma(7.9, 3.1), N))
fit_mle(Gamma, rand(Gamma(7.9, 3.1), N))
fit(Geometric, rand(Geometric(0.1), N))
fit_mle(Geometric, rand(Geometric(0.1), N))
fit(Laplace, rand(Laplace(10.0, 3.0), N))
fit_mle(Laplace, rand(Laplace(10.0, 3.0), N))
fit(Normal, rand(Normal(11.3, 5.7), N))
fit_mle(Normal, rand(Normal(11.3, 5.7), N))
fit(Poisson, rand(Poisson(19.0), N))
fit_mle(Poisson, rand(Poisson(19.0), N))
fit(Uniform, rand(Uniform(1.1, 98.3), N))
fit_mle(Uniform, rand(Uniform(1.1, 98.3), N))
......@@ -84,7 +84,9 @@ export # types
devresid, # vector of squared deviance residuals
dim, # sample dimension of multivariate distribution
entropy, # entropy of distribution in nats
fit, # fit a distribution to data
fit, # fit a distribution to data (using default method)
fit_mle, # fit a distribution to data using MLE
fit_map, # fit a distribution to data using MAP
freecumulant, # free cumulants of distribution
insupport, # predicate, is x in the support of the distribution?
invlogccdf, # complementary quantile based on log probability
......
......@@ -281,3 +281,9 @@ for f in (:pdf, :logpdf, :cdf, :logcdf, :ccdf, :logccdf, :quantile,
end
end
end
# Fitting
fit{D <: Distribution}(d::Type{D}, x::Array) = fit_mle(d, x)
......@@ -141,7 +141,7 @@ function var(d::Dirichlet)
return S
end
function fit{T <: Real}(::Type{Dirichlet}, P::Matrix{T})
function fit_mle{T <: Real}(::Type{Dirichlet}, P::Matrix{T})
K, N = size(P)
alpha = zeros(Float64, K)
......
......@@ -140,6 +140,14 @@ function var(d::Multinomial)
return S
end
function fit(::Type{Multinomial}, X::Matrix)
return Multinomial(sum(X[:, 1]), vec(mean(X, 2)))
function fit_mle{T<:Real}(::Type{Multinomial}, X::Matrix{T})
ns = vec(sum(X, 1))
if !(all(ns .== ns[1]))
error("Each sample in X should have the same number of trials.")
end
n = int(ns[1])
p = vec(mean(X, 2)) * (1.0 / n)
Multinomial(n, p)
end
......@@ -153,7 +153,7 @@ function var(d::MultivariateNormal)
return U'U
end
function fit{T <: Real}(::Type{MultivariateNormal}, X::Matrix{T})
function fit_mle{T <: Real}(::Type{MultivariateNormal}, X::Matrix{T})
MultivariateNormal(vec(mean(X, 2)), cov(X'))
end
......
......@@ -63,7 +63,7 @@ skewness(d::Bernoulli) = (1.0 - 2.0 * d.prob) / std(d)
var(d::Bernoulli) = d.prob * (1.0 - d.prob)
function fit(::Type{Bernoulli}, x::Array)
function fit_mle(::Type{Bernoulli}, x::Array)
for i in 1:length(x)
if !insupport(Bernoulli(), x[i])
error("Bernoulli observations must be in {0, 1}")
......
......@@ -73,7 +73,7 @@ function var(d::Beta)
return d.alpha * d.beta / (ab * ab * (ab + 1.0))
end
function fit(::Type{Beta}, x::Array)
function fit_mle(::Type{Beta}, x::Array)
for i in 1:length(x)
if !insupport(Beta(), x[i])
error("Bernoulli observations must be in [0,1]")
......@@ -85,3 +85,5 @@ function fit(::Type{Beta}, x::Array)
b = (1.0 - x_bar) * (((x_bar * (1.0 - x_bar)) / v_bar) - 1.0)
return Beta(a, b)
end
......@@ -53,9 +53,21 @@ skewness(d::Binomial) = (1.0 - 2.0 * d.prob) / std(d)
var(d::Binomial) = d.size * d.prob * (1.0 - d.prob)
function fit(::Type{Binomial}, x::Real, n::Real)
if x > n || x < 0
error("For binomial observations, x must lie in [0, n]")
function fit_mle{T<:Real}(::Type{Binomial}, n::Integer, x::Array{T})
# a series of experiments, each experiment has n trials
# x[i] is the number of successes in the i-th experiment
sx = 0.
for xi in x
if xi < 0 || xi > n
error("Each element in x must be in [0, n].")
end
sx += xi
end
return Binomial(int(n), x / n)
Binomial(int(n), sx / (n * length(x)))
end
fit(::Type{Binomial}, n::Integer, x::Array) = fit_mle(Binomial, n, x)
......@@ -99,7 +99,10 @@ function var(d::Categorical, m::Number)
return s
end
function fit{T <: Real}(::Type{Categorical}, x::Array{T})
function fit_mle{T <: Real}(::Type{Categorical}, x::Array{T})
# Counts for all categories
return Categorical()
end
......@@ -38,7 +38,7 @@ skewness(d::Cauchy) = NaN
var(d::Cauchy) = NaN
function fit{T <: Real}(::Type{Cauchy}, x::Array{T})
function fit_mle{T <: Real}(::Type{Cauchy}, x::Array{T})
c = median(x)
l, u = iqr(x)
return Cauchy(c, (u - l) / 2.0)
......
......@@ -69,6 +69,10 @@ skewness(d::DiscreteUniform) = 0.0
var(d::DiscreteUniform) = ((d.b - d.a + 1.0)^2 - 1.0) / 12.0
function fit{T <: Real}(::Type{DiscreteUniform}, x::Array{T})
function fit_mle{T <: Real}(::Type{DiscreteUniform}, x::Array{T})
DiscreteUniform(min(x), max(x))
end
fit(::Type{DiscreteUniform}, x::Array) = fit_mle(DiscreteUniform, x)
......@@ -67,7 +67,8 @@ skewness(d::EmpiricalUnivariateDistribution) = NaN
var(d::EmpiricalUnivariateDistribution) = d.var
function fit{T <: Real}(::Type{EmpiricalUnivariateDistribution},
function fit_mle{T <: Real}(::Type{EmpiricalUnivariateDistribution},
x::Vector{T})
EmpiricalUnivariateDistribution(x)
end
......@@ -98,7 +98,7 @@ skewness(d::Exponential) = 2.0
var(d::Exponential) = d.scale * d.scale
function fit(::Type{Exponential}, x::Array)
function fit_mle(::Type{Exponential}, x::Array)
for i in 1:length(x)
if !insupport(Exponential(), x[i])
error("Exponential observations must be non-negative values")
......@@ -106,3 +106,7 @@ function fit(::Type{Exponential}, x::Array)
end
return Exponential(mean(x))
end
fit(::Type{Exponential}, x::Array) = fit_mle(Exponential, x)
......@@ -97,7 +97,7 @@ skewness(d::Gamma) = 2.0 / sqrt(d.shape)
var(d::Gamma) = d.shape * d.scale * d.scale
function fit(::Type{Gamma}, x::Array)
function fit_mle(::Type{Gamma}, x::Array)
a_old = 0.5 / (log(mean(x)) - mean(log(x)))
a_new = 0.5 / (log(mean(x)) - mean(log(x)))
......@@ -117,3 +117,4 @@ function fit(::Type{Gamma}, x::Array)
Gamma(a_new, mean(x) / a_new)
end
......@@ -52,4 +52,4 @@ skewness(d::Geometric) = (2.0 - d.prob) / sqrt(1.0 - d.prob)
var(d::Geometric) = (1.0 - d.prob) / d.prob^2
fit(::Type{Geometric}, x::Array) = Geometric(1.0 / mean(x))
fit_mle(::Type{Geometric}, x::Array) = Geometric(1.0 / mean(x))
......@@ -68,7 +68,7 @@ std(d::Laplace) = sqrt(2.0) * d.scale
var(d::Laplace) = 2.0 * d.scale^2
function fit(::Type{Laplace}, x::Array)
function fit_mle(::Type{Laplace}, x::Array)
n = length(x)
a = median(x)
deviations = 0.0
......@@ -78,3 +78,5 @@ function fit(::Type{Laplace}, x::Array)
b = deviations / n
Laplace(a, b)
end
......@@ -42,7 +42,7 @@ function var(d::logNormal)
return (exp(sigsq) - 1) * exp(2d.meanlog + sigsq)
end
function fit{T <: Real}(::Type{logNormal}, x::Array{T})
function fit_mle{T <: Real}(::Type{logNormal}, x::Array{T})
lx = log(x)
return logNormal(mean(lx), std(lx))
end
......@@ -47,4 +47,4 @@ std(d::Normal) = d.std
var(d::Normal) = d.std^2
fit{T <: Real}(::Type{Normal}, x::Array{T}) = Normal(mean(x), std(x))
fit_mle{T <: Real}(::Type{Normal}, x::Array{T}) = Normal(mean(x), std(x))
......@@ -60,7 +60,7 @@ skewness(d::Poisson) = 1.0 / sqrt(d.lambda)
var(d::Poisson) = d.lambda
function fit(::Type{Poisson}, x::Array)
function fit_mle(::Type{Poisson}, x::Array)
for i in 1:length(x)
if !insupport(Poisson(), x[i])
error("Poisson observations must be non-negative integers")
......
......@@ -45,6 +45,6 @@ function var(d::Uniform)
return w * w / 12.0
end
function fit{T <: Real}(::Type{Uniform}, x::Vector{T})
function fit_mle{T <: Real}(::Type{Uniform}, x::Vector{T})
Uniform(min(x), max(x))
end
......@@ -4,7 +4,7 @@ fit(Bernoulli, rand(Bernoulli(0.7), N))
fit(Beta, rand(Beta(1.3, 3.7), N))
fit(Binomial, rand(Binomial(N, 0.3)), N)
fit(Binomial, 100, rand(Binomial(100, 0.3), N))
fit(DiscreteUniform, rand(DiscreteUniform(300_000, 700_000), N))
......
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