Skip to content

Commit

Permalink
add Beta Bernoulli test, issue #11
Browse files Browse the repository at this point in the history
  • Loading branch information
trappmartin committed Sep 6, 2018
1 parent 4f43287 commit 12d7903
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/distfunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function add!(d::DirichletMultinomial, X::SparseVector{Int, Int})
d.dirty = true
end

function add!(d::BetaBernoulli, X::Integer)
function add!(d::BetaBernoulli, X::Bool)
d.successes += X
d.n += 1
end
Expand Down Expand Up @@ -261,8 +261,8 @@ function posteriorParameters(d::NormalNormal)
end

function posteriorParameters(d::BetaBernoulli)
α = d.α + d.successes
β = d.β + d.n - d.successes
α = d.α0 + d.successes
β = d.β0 + d.n - d.successes
return (α, β)
end

Expand Down
38 changes: 38 additions & 0 deletions test/distributionTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,42 @@ using LinearAlgebra
@test β == β0
end

@testset "Beta-Bernoulli" begin

# prior
α0 = 1.0
β0 = 1.0

# data
x = Bool[0, 1, 0, 0, 1]
N = length(x)

# distribution
d = BetaBernoulli(α0 = α0, β0 = β0)

# test prior
(α, β) = BayesianNonparametrics.posteriorParameters(d)

@test α == α0
@test β == β0

# add data
BayesianNonparametrics.add!(d, x)

# test posterior paramters
(α, β) = BayesianNonparametrics.posteriorParameters(d)

@test α == α0 + sum(x)
@test β == β0 + (N - sum(x))

# remove data
BayesianNonparametrics.remove!(d, x)

# test prior
(α, β) = BayesianNonparametrics.posteriorParameters(d)

@test α == α0
@test β == β0
end

end

0 comments on commit 12d7903

Please sign in to comment.