Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making BayesNets Bayesian #146

Open
itsdfish opened this issue Aug 13, 2022 · 2 comments
Open

Making BayesNets Bayesian #146

itsdfish opened this issue Aug 13, 2022 · 2 comments

Comments

@itsdfish
Copy link

Hello,

I would like to use BayesNets.jl with Turing.jl to perform Bayesian parameter estimation. The primary requirement is that a library interacting with Turing is set up generically to support ForwardDiff.Dual types and Float64 in order to allow automatic differentiation to compute gradients. As shown below, I found that DiscreteCPD does not work because it is hardcoded to accept Float64. See this line.

A simple fix is to change that line to

const DiscreteCPD = CategoricalCPD

I tried adding constraints but it threw an error.

const DiscreteCPD = CategoricalCPD{Categorical{T,Vector{T}}} where {T<:Real}

I was wondering if you would be willing to change the line (I could also open a PR), and whether there are additional places that might need to be changed in the code.

Thanks!

MWE

using BayesNets, TikzGraphs
using ForwardDiff

c = ForwardDiff.Dual(.5,.4)
snc = ForwardDiff.Dual(.5,.4)
sc = ForwardDiff.Dual(.5,.4)

bn = BayesNet([DiscreteCPD(:c, [c,(1-c)])])
push!(bn, 
    DiscreteCPD(
        :s,  [:c], [2], 
    [Categorical([snc,(1-snc)]), 
    Categorical([sc,(1-sc)])]
    ),
)

Error

ERROR: TypeError: in typeassert, expected Float64, got a value of type ForwardDiff.Dual{Nothing, Float64, 1}
Stacktrace:
  [1] setindex!(A::Vector{Float64}, x::ForwardDiff.Dual{Nothing, Float64, 1}, i1::Int64)
    @ Base ./array.jl:903
  [2] _unsafe_copyto!(dest::Vector{Float64}, doffs::Int64, src::Vector{ForwardDiff.Dual{Nothing, Float64, 1}}, soffs::Int64, n::Int64)
    @ Base ./array.jl:253
  [3] unsafe_copyto!
    @ ./array.jl:307 [inlined]
  [4] _copyto_impl!
    @ ./array.jl:331 [inlined]
  [5] copyto!
    @ ./array.jl:317 [inlined]
  [6] copyto!
    @ ./array.jl:343 [inlined]
  [7] copyto_axcheck!
    @ ./abstractarray.jl:1104 [inlined]
  [8] Vector{Float64}(x::Vector{ForwardDiff.Dual{Nothing, Float64, 1}})
    @ Base ./array.jl:563
  [9] convert
    @ ./array.jl:554 [inlined]
 [10] convert
    @ ~/.julia/packages/Distributions/1PkiH/src/univariate/discrete/discretenonparametric.jl:45 [inlined]
 [11] setindex!
    @ ./array.jl:903 [inlined]
 [12] _unsafe_copyto!(dest::Vector{Categorical{Float64, Vector{Float64}}}, doffs::Int64, src::Vector{Categorical{ForwardDiff.Dual{Nothing, Float64, 1}, Vector{ForwardDiff.Dual{Nothing, Float64, 1}}}}, soffs::Int64, n::Int64)
    @ Base ./array.jl:253
 [13] unsafe_copyto!
    @ ./array.jl:307 [inlined]
 [14] _copyto_impl!
    @ ./array.jl:331 [inlined]
 [15] copyto!
    @ ./array.jl:317 [inlined]
 [16] copyto!
    @ ./array.jl:343 [inlined]
 [17] copyto_axcheck!
    @ ./abstractarray.jl:1104 [inlined]
 [18] Vector{Categorical{Float64, Vector{Float64}}}(x::Vector{Categorical{ForwardDiff.Dual{Nothing, Float64, 1}, Vector{ForwardDiff.Dual{Nothing, Float64, 1}}}})
    @ Base ./array.jl:563
 [19] convert
    @ ./array.jl:554 [inlined]
 [20] DiscreteCPD(target::Symbol, parents::Vector{Symbol}, parental_ncategories::Vector{Int64}, distributions::Vector{Categorical{ForwardDiff.Dual{Nothing, Float64, 1}, Vector{ForwardDiff.Dual{Nothing, Float64, 1}}}})
    @ BayesNets.CPDs ~/.julia/packages/BayesNets/hZr0W/src/CPDs/categorical_cpd.jl:22
 [21] top-level scope
    @ Untitled-3:13
@mykelk
Copy link
Member

mykelk commented Aug 13, 2022

That would be awesome! We certainly would welcome a PR!

@mykelk
Copy link
Member

mykelk commented Aug 13, 2022

Please also add some tests to the test script as well. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants