BayesNets.jl icon indicating copy to clipboard operation
BayesNets.jl copied to clipboard

Infer BN joint probability with evidence

Open SEICS opened this issue 3 years ago • 3 comments

Hello,

I wonder what is the syntax to query a Bayesian Network's (BN) joint probability with some evidence? The docs seem a little bit too brief and lots of syntaxes are not mentioned.

Based on this infer statement: infer(ExactInference(), bn, [:distance], evidence=Assignment(:carrier_id=>15))

I have tried to omit the query objects (e.g., [:a,:b]) from the infer statement: infer(ExactInference(), bn, evidence=Assignment(:carrier_id=>15))

and BayesNets.jl output compilation errors: MethodError: no method matching infer(::ExactInference, ::DiscreteBayesNet; evidence=Dict{Symbol, Any}(:carrier_id => 15))

I wonder if such feature is implemented or will it be on plan? Really looking forward to your reply! Thank you so much!

SEICS avatar Jul 08 '22 16:07 SEICS

A call to infer produces a joint distribution over all given target variables (which cannot include evidence variables). To get the effect you requested, just expand [:distance] to include all variables in the BN other than carrier_id.

tawheeler avatar Jul 10 '22 01:07 tawheeler

Thank you for the reply. Sorry again, assuming that I have a BN as "year -> distance -> origin_id", and I have observed origin_id=>1, I wonder how can I get the joint distribution for this BN with this evidence?

What I have tried:

result = infer(ExactInference(), bn, [:year,:distance], evidence=Assignment(:origin_id=>1)).potential
result = sum(result, dims=(1, 2))

and it gives me:

julia> include("/Users/jw/Desktop/test.jl")
[1.0;;]

which means I got the joint distribution of this BN as 1. This sum up to one should not happen because of the evidence. I wonder what did I do wrong? Thank you for help!

SEICS avatar Jul 11 '22 17:07 SEICS

The result of infer is going to be a factor table, which will have normalized probability values. Summing over them will always produce 1.

If it isn't displaying properly, please try viewing the factor in IJulia, or maybe printing it out after converting it to a DataFrame. convert(DataFrame, ϕ).

tawheeler avatar Jul 12 '22 00:07 tawheeler