Skip to content

Commit

Permalink
fix warnings; fixes #90
Browse files Browse the repository at this point in the history
  • Loading branch information
mykelk committed Jun 25, 2020
1 parent de291ea commit c2ef898
Show file tree
Hide file tree
Showing 10 changed files with 531 additions and 442 deletions.
890 changes: 489 additions & 401 deletions doc/BayesNets.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/CPDs/static_cpd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function Distributions.fit(::Type{StaticCPD{D}},
parents::NodeNames=NodeName[],
) where {D<:Distribution}

d = fit(D, data[target])
d = fit(D, data[!,target])
StaticCPD(target, parents, d)
end

Expand Down
3 changes: 2 additions & 1 deletion src/DiscreteBayesNet/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ end

Base.convert(::Type{DataFrame}, t::Table) = t.potential

for f in [:names, :unique, :size, :eltype, :setindex!, :getindex]
for f in [:names, :unique, :size, :eltype, :setindex!]
@eval (Base.$f)(t::Table, x...) = $f(t.potential, x...)
end
Base.getindex(t::Table, x...) = getindex(t.potential, !, x...)

for s in [:(==), :(!=)]
@eval (Base.$s)(t::Table, f::DataFrame) = $s(t.potential, f)
Expand Down
2 changes: 1 addition & 1 deletion src/Factors/factors_dataframes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Convert a Factor to a DataFrame
function Base.convert(::Type{DataFrame}, ϕ::Factor)
df = DataFrames.DataFrame(pattern(ϕ))
DataFrames.rename!(df, [f => t for (f, t) = zip(names(df), names(ϕ))] )
df[:potential] = ϕ.potential[:]
df[!,:potential] = ϕ.potential[:]

return df
end
Expand Down
2 changes: 1 addition & 1 deletion src/gibbs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ function gibbs_sample(bn::BayesNet, nsamples::Integer, burn_in::Integer;
# for burn_in_initial_sample use get_weighted_dataframe, should be consistent with the varibale consistent_with
if initial_sample == nothing
rand_samples = get_weighted_dataframe(bn, 50, consistent_with)
if reduce(|, isnan.(convert(Array{AbstractFloat}, rand_samples[:p])))
if reduce(|, isnan.(convert(Array{AbstractFloat}, rand_samples[!,:p])))
error("Gibbs Sampler was unable to find an inital sample with non-zero probability, please supply an inital sample")
end
burn_in_initial_sample = sample_weighted_dataframe(rand_samples)
Expand Down
10 changes: 5 additions & 5 deletions src/learning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function Distributions.fit(::Type{BayesNet}, data::DataFrame, dag::DAG, cpd_type
length(cpd_types) == nv(dag) || throw(DimensionMismatch("dag and cpd_types must have the same length"))

cpds = Array{CPD}(undef, length(cpd_types))
tablenames = names(data)
tablenames = propertynames(data)
for (i, target) in enumerate(tablenames)
C = cpd_types[i]
parents = tablenames[inneighbors(dag, i)]
Expand All @@ -15,7 +15,7 @@ end
function Distributions.fit(::Type{BayesNet}, data::DataFrame, dag::DAG, ::Type{C}) where {C<:CPD}

cpds = Array{C}(undef, nv(dag))
tablenames = names(data)
tablenames = propertynames(data)
for (i, target) in enumerate(tablenames)
parents = tablenames[inneighbors(dag, i)]
cpds[i] = fit(C, data, target, parents)
Expand All @@ -26,7 +26,7 @@ end
Distributions.fit(::Type{BayesNet{T}}, data::DataFrame, dag::DAG) where {T<:CPD} = fit(BayesNet, data, dag, T)

function _get_dag(data::DataFrame, edges::Tuple{Vararg{Pair{NodeName, NodeName}}})
varnames = names(data)
varnames = propertynames(data)
dag = DAG(length(varnames))
for (a,b) in edges
i = findfirst(isequal(a), varnames)
Expand Down Expand Up @@ -109,7 +109,7 @@ As score_component(ScoringFunction, cpd, data), but returns pre-computed values
if they exist, and populates the cache if they don't
"""
function _get_parent_indeces(parents::NodeNames, data::DataFrame)
varnames = names(data)
varnames = propertynames(data)
retval = Array{Int}(undef, length(parents))
for (i,p) in enumerate(parents)
retval[i] = something(findfirst(isequal(p), varnames), 0)
Expand All @@ -124,7 +124,7 @@ function score_component(
)

pinds = _get_parent_indeces(parents(cpd), data)
varnames = names(data)
varnames = propertynames(data)
i = something(findfirst(isequal(name(cpd)), varnames), 0)

if !haskey(cache[i], pinds)
Expand Down
4 changes: 2 additions & 2 deletions test/test_cpds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,6 @@ let
push!(bn, a)
push!(bn, b)
push!(bn, c)
@test mean(rand(bn, 20,:a=>0)[:c]) <= 0.6
@test mean(rand(bn, 20, :a=>1, :b=>1)[:c]) == 1;
@test mean(rand(bn, 20,:a=>0)[!,:c]) <= 0.6
@test mean(rand(bn, 20, :a=>1, :b=>1)[!,:c]) == 1;
end
2 changes: 1 addition & 1 deletion test/test_factors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ name = :N5

ϕ = Factor(bn, name)
df = join(DataFrame(ϕ), table(bn, name).potential, on=names(ϕ))
diff = abs.(df[:p] - df[:potential])
diff = abs.(df[!,:p] - df[!,:potential])

@test all(diff .< 1E-13)
end
Expand Down
50 changes: 25 additions & 25 deletions test/test_gibbs.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let

# A â~F~R C â~F~P B
# A ~F~R C ~F~P B
bn = BayesNet()
push!(bn, StaticCPD(:a, Categorical([1.0,0.0])))
push!(bn, StaticCPD(:b, Categorical([0.0,1.0])))
Expand All @@ -10,16 +10,16 @@ let
variable_order=nothing, time_limit=nothing,
error_if_time_out=true, initial_sample=nothing)
@test size(t5) == (5, 3)
@test t5[:a] == [1,1,1,1,1]
@test t5[:b] == [2,2,2,2,2]
@test t5[:c] == [1,1,1,1,1]
@test t5[!,:a] == [1,1,1,1,1]
@test t5[!,:b] == [2,2,2,2,2]
@test t5[!,:c] == [1,1,1,1,1]

config = GibbsSampler(burn_in=100, thinning=5)
t5 = rand(bn, config, 5)
@test size(t5) == (5, 3)
@test t5[:a] == [1,1,1,1,1]
@test t5[:b] == [2,2,2,2,2]
@test t5[:c] == [1,1,1,1,1]
@test t5[!,:a] == [1,1,1,1,1]
@test t5[!,:b] == [2,2,2,2,2]
@test t5[!,:c] == [1,1,1,1,1]

bn2 = BayesNet()
push!(bn2, StaticCPD(:a, Categorical([0.5,0.5])))
Expand All @@ -31,30 +31,30 @@ let
t6 = gibbs_sample(bn2, 5, 100; thinning=5, consistent_with=Assignment(:c=>1),
variable_order=nothing, time_limit=nothing,
error_if_time_out=true, initial_sample=nothing)
@test t6[:a] == [1,1,1,1,1]
@test t6[:b] == [1,1,1,1,1]
@test t6[:c] == [1,1,1,1,1]
@test t6[!,:a] == [1,1,1,1,1]
@test t6[!,:b] == [1,1,1,1,1]
@test t6[!,:c] == [1,1,1,1,1]

config.evidence = Assignment(:c=>1)
t6 = rand(bn2, config, 5)
@test t6[:a] == [1,1,1,1,1]
@test t6[:b] == [1,1,1,1,1]
@test t6[:c] == [1,1,1,1,1]
@test t6[!,:a] == [1,1,1,1,1]
@test t6[!,:b] == [1,1,1,1,1]
@test t6[!,:c] == [1,1,1,1,1]


t7 = gibbs_sample(bn2, 5, 100; thinning=5, consistent_with=Assignment(:c=>2),
variable_order=nothing, time_limit=nothing,
error_if_time_out=true, initial_sample=nothing)
@test t7[:a] == [2,2,2,2,2]
@test t7[:b] == [1,1,1,1,1]
@test t7[:c] == [2,2,2,2,2]
@test t7[!,:a] == [2,2,2,2,2]
@test t7[!,:b] == [1,1,1,1,1]
@test t7[!,:c] == [2,2,2,2,2]

rand()
config.evidence = Assignment(:c=>2)
t7 = rand(bn2, config, 5)
@test t7[:a] == [2,2,2,2,2]
@test t7[:b] == [1,1,1,1,1]
@test t7[:c] == [2,2,2,2,2]
@test t7[!,:a] == [2,2,2,2,2]
@test t7[!,:b] == [1,1,1,1,1]
@test t7[!,:c] == [2,2,2,2,2]

bn3 = BayesNet()
push!(bn3, StaticCPD(:a, Normal(2.5, 1.5)))
Expand Down Expand Up @@ -107,18 +107,18 @@ let
variable_order=nothing, time_limit=nothing,
error_if_time_out=true, initial_sample=nothing)
@test size(t12) == (5, 3)
@test t12[:a] == [1,1,1,1,1]
@test t12[:b] == [2,2,2,2,2]
@test t12[:c] == [1,1,1,1,1]
@test t12[!,:a] == [1,1,1,1,1]
@test t12[!,:b] == [2,2,2,2,2]
@test t12[!,:c] == [1,1,1,1,1]


t13 = gibbs_sample(d_bn, 5, 100; thinning=5, consistent_with=Assignment(:c=>1),
variable_order=nothing, time_limit=nothing,
error_if_time_out=true, initial_sample=nothing)
@test size(t13) == (5, 3)
@test t13[:a] == [1,1,1,1,1]
@test t13[:b] == [2,2,2,2,2]
@test t13[:c] == [1,1,1,1,1]
@test t13[!,:a] == [1,1,1,1,1]
@test t13[!,:b] == [2,2,2,2,2]
@test t13[!,:c] == [1,1,1,1,1]

# test bad parameters given to gibbs_sample
pass = false
Expand Down
8 changes: 4 additions & 4 deletions test/test_sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ let

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]
@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)
Expand All @@ -23,6 +23,6 @@ let
# 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]
@test t5[!,:a] == [1,1,1,1,1]
@test t5[!,:b] == [2,2,2,2,2]
end

0 comments on commit c2ef898

Please sign in to comment.