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

add probs for Bernoulli and Binomial

parent 67d5eae4
......@@ -81,6 +81,17 @@ function test_samples(s::Sampleable{Univariate, Discrete}, # the sampleable
p0 = pdf(distr, rmin:rmax) # reference probability masses
@assert length(p0) == m
# check the consistency between probs and pdf
if isa(s, Distribution)
if applicable(probs, s, rmin:rmax)
@test_approx_eq probs(s, rmin:rmax) p0
end
if applicable(probs, s)
@assert isfinite(vmin) && isfinite(vmax)
@test_approx_eq probs(s) probs(s, vmin:vmax)
end
end
# determine confidence intervals for counts:
# with probability q, the count will be out of this interval.
#
......
......@@ -18,6 +18,26 @@ end
Bernoulli() = Bernoulli(0.5)
probs(d::Bernoulli) = [d.p0, d.p1]
function probs(d::Bernoulli, rgn::UnitRange)
n = length(rgn)
if n == 0
return Float64[]
elseif n == 1
f = rgn[1]
return f == zero(f) ? [d.p0] :
f == one(f) ? [d.p1] :
throw(BoundsError())
elseif n == 2
f = rgn[1]
return f == zero(f) ? [d.p0, d.p1] :
throw(BoundsError())
else
throw(BoundsError())
end
end
cdf(d::Bernoulli, q::Real) = q >= zero(q) ? (q >= one(q) ? 1.0 : d.p0) : 0.
function entropy(d::Bernoulli)
......
......@@ -16,6 +16,31 @@ maximum(d::Binomial) = d.size
@_jl_dist_2p Binomial binom
function _probs(d::Binomial, f::Int, l::Int)
n = d.size
p = d.prob
b = f - 1
r = Array(Float64, l - b)
r[1] = v = pdf(d, f)
if l > f
c = p / (1.0 - p)
for k = f+1:l
v *= ((n - k + 1) / k * c)
r[k-b] = v
end
end
return r
end
probs(d::Binomial) = _probs(d, 0, d.size)
function probs(d::Binomial, rgn::UnitRange)
f, l = rgn[1], rgn[end]
0 <= f <= l <= d.size || throw(BoundsError())
_probs(d, f, l)
end
function entropy(d::Binomial; approx::Bool=false)
n = d.size
p1 = d.prob
......
......@@ -14,6 +14,7 @@ end
ncategories(d::Categorical) = d.K
probs(d::Categorical) = d.prob
probs(d::Categorical, rgn::UnitRange) = d.prob[rgn]
### handling support
......
......@@ -2,6 +2,8 @@
"Bernoulli(0.9)", 9.0000000000000002e-01, 8.9999999999999983e-02, 3.2508297339144820e-01, 1, 1, 1, -1.0536051565782628e-01, -1.0536051565782628e-01, -1.0536051565782628e-01
"Bernoulli(0.1)", 1.0000000000000001e-01, 9.0000000000000011e-02, 3.2508297339144820e-01, 0, 0, 0, -1.0536051565782631e-01, -1.0536051565782631e-01, -1.0536051565782631e-01
"Binomial(1, 0.5)", 5.0000000000000000e-01, 2.5000000000000000e-01, 6.9314718055994529e-01, 0, 0, 1, -6.9314718055994529e-01, -6.9314718055994529e-01, -6.9314718055994529e-01
"Binomial(5, 0.4)", 2.0000000000000000e+00, 1.2000000000000000e+00, 1.4979981829038540e+00, 1, 2, 3, -1.3501553145040179e+00, -1.0624732420522367e+00, -1.4679383501604009e+00
"Binomial(6, 0.8)", 4.8000000000000007e+00, 9.5999999999999996e-01, 1.3425774508710622e+00, 4, 5, 6, -1.4033998290228298e+00, -9.3339619977709365e-01, -1.3388613078852583e+00
"Binomial(100, 0.1)", 1.0000000000000000e+01, 9.0000000000000000e+00, 2.5112331161825550e+00, 8, 10, 12, -2.1643632354294020e+00, -2.0259739768661866e+00, -2.3147790140625677e+00
"Binomial(100, 0.9)", 9.0000000000000000e+01, 8.9999999999999982e+00, 2.5112331161825514e+00, 88, 90, 92, -2.3147790140625695e+00, -2.0259739768661902e+00, -2.1643632354294020e+00
"DiscreteUniform(0, 4)", 2.0000000000000000e+00, 2.0000000000000000e+00, 1.6094379124341003e+00, 1, 2, 3, -1.6094379124341003e+00, -1.6094379124341003e+00, -1.6094379124341003e+00
......
......@@ -2,6 +2,8 @@ Bernoulli(0.5)
Bernoulli(0.9)
Bernoulli(0.1)
Binomial(1, 0.5)
Binomial(5, 0.4)
Binomial(6, 0.8)
Binomial(100, 0.1)
Binomial(100, 0.9)
DiscreteUniform(0, 4)
......
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