Skip to content

Commit

Permalink
More quick fixes. Tests pass except for docs
Browse files Browse the repository at this point in the history
Docs need to be updated. Better delineation between a table and a dataframe
Sampling works, inference hasent been touched
  • Loading branch information
hamzaelsaawy committed Aug 24, 2017
1 parent b943e99 commit ed19c87
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
8 changes: 7 additions & 1 deletion src/DiscreteBayesNet/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ for f in [:names, :unique, :size, :eltype, :setindex!, :getindex]
@eval (Base.$f)(t::Table, x...) = $f(t.potential, x...)
end

nrow(t::Table) = nrow(t.potential)
for s in [:(==), :(!=)]
@eval (Base.$s)(t::Table, f::DataFrame) = $s(t.potential, f)
@eval (Base.$s)(f::DataFrame, t::Table) = $s(f, t.potential)
@eval (Base.$s)(t1::Table, t2::Table) = $s(t1.potential, t2.potential)
end

DataFrames.nrow(t::Table) = nrow(t.potential)

"""
Table multiplication
Expand Down
4 changes: 2 additions & 2 deletions test/test_discrete_bayes_nets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ let
push!(bn, DiscreteCPD(:b, [:a], [2], [Categorical([0.5,0.5]),Categorical([0.2,0.8])]))

T = table(bn, :a)
@test T == DataFrame(a=[1,2], p=[0.4,0.6]) |> Table
@test T == DataFrame(a=[1,2], p=[0.4,0.6])

T = table(bn, :b)
@test T == DataFrame(a=[1,2,1,2], b=[1,1,2,2], p=[0.5,0.2,0.5,0.8]) |> Table
@test T == DataFrame(a=[1,2,1,2], b=[1,1,2,2], p=[0.5,0.2,0.5,0.8])

data = DataFrame(a=[1,1,1,1,2,2,2,2],
b=[1,2,1,2,1,1,1,2])
Expand Down
38 changes: 20 additions & 18 deletions test/test_sampling.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
let
# A → C ← B
bn = BayesNet()
push!(bn, StaticCPD(:a, Categorical([1.0,0.0])))
push!(bn, StaticCPD(:b, Categorical([0.0,1.0])))
push!(bn, CategoricalCPD{Bernoulli}(:c, [:a, :b], [2,2], [Bernoulli(0.1), Bernoulli(0.2), Bernoulli(1.0), Bernoulli(0.4)]))
# A → C ← B
bn = BayesNet()
push!(bn, StaticCPD(:a, Categorical([1.0,0.0])))
push!(bn, StaticCPD(:b, Categorical([0.0,1.0])))
push!(bn, CategoricalCPD{Bernoulli}(:c, [:a, :b], [2,2],
[Bernoulli(0.1), Bernoulli(0.2), Bernoulli(1.0), Bernoulli(0.4)]))

@test rand(bn) == Dict(:a=>1, :b=>2, :c=>1)
@test rand(bn) == Dict(:a=>1, :b=>2, :c=>1)

t1 = rand(bn, 5)
@test size(t1) == (5,3)
@test t1[:a] == [1,1,1,1,1]
@test t1[:b] == [2,2,2,2,2]
t1 = rand(bn, 5)
@test size(t1) == (5,3)
@test t1[:a] == [1,1,1,1,1]
@test t1[:b] == [2,2,2,2,2]

t2 = rand(bn, 5, Assignment(:c=>1))
@test size(t1) == (5,3)
t2 = rand(bn, 5, Assignment(:c=>1))
@test size(t1) == (5,3)

t3 = rand(bn, 5, :c=>1, :b=>2)
@test size(t1) == (5,3)
t3 = rand(bn, 5, :c=>1, :b=>2)
@test size(t1) == (5,3)

t4 = rand(bn, LikelihoodWeightedSampler(bn, 10, :c=>1), 5)
t4 = rand(bn, LikelihoodWeightedSampler(:c=>1), 5)
# is there a test here?

t5 = rand(bn, GibbsSampler(Assignment(:c=>1), burn_in=5), 5)
@test t5[:a] == [1,1,1,1,1]
@test t5[:b] == [2,2,2,2,2]
t5 = rand(bn, GibbsSampler(Assignment(:c=>1), burn_in=5), 5)
@test t5[:a] == [1,1,1,1,1]
@test t5[:b] == [2,2,2,2,2]
end
4 changes: 2 additions & 2 deletions test/test_tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ end

let
# estimation
df = estimate(Table(DataFrame(
df = estimate(DataFrame(
A = [false, false, true, true, true]
)))
))
@test elementwise_isapprox(df[:p], [2/5, 3/5])

# TODO: properly test this
Expand Down

0 comments on commit ed19c87

Please sign in to comment.