1+ ``` @meta
2+ CurrentModule = BayesNets
3+ ```
14# Usage
25
36``` @setup bayesnet
4- using BayesNets, TikzGraphs, TikzPictures
5- ```
6-
7- ``` julia
8- using Random
9- Random. seed! (0 ) # seed the random number generator to 0, for a reproducible demonstration
107using BayesNets
11- using TikzGraphs # required to plot tex-formatted graphs (recommended), otherwise GraphPlot.jl is used
12- using TikzPictures
8+ using DataFrames
9+ using Distributions
10+ using Random
11+ using Graphs
12+ Random.seed!(0)
1313```
1414
1515## Representation
@@ -25,11 +25,11 @@ a = \mathcal{N}(0,1) \qquad b = \mathcal{N}(2a +3,1)
2525bn = BayesNet()
2626push!(bn, StaticCPD(:a, Normal(1.0)))
2727push!(bn, LinearGaussianCPD(:b, [:a], [2.0], 3.0, 1.0))
28- plot = BayesNets.plot(bn)
29- TikzPictures.save(SVG("plot1"), plot)
28+ p = BayesNets.plot(bn)
29+ p
3030```
3131
32- ![ ] ( plot1.svg )
32+
3333
3434## Conditional Probability Distributions
3535
@@ -56,17 +56,17 @@ cpdA = fit(StaticCPD{Normal}, data, :a)
5656cpdB = fit(LinearGaussianCPD, data, :b, [:a])
5757
5858bn2 = BayesNet([cpdA, cpdB])
59- plot = BayesNets.plot(bn2) # hide
60- TikzPictures.save(SVG("plot2"), plot) # hide
59+ p = BayesNets.plot(bn2)
60+ p
6161```
6262
63- ![ ] ( plot2.svg )
63+
6464
6565Each ` CPD ` implements four functions:
6666
6767* ` name(cpd) ` - obtain the name of the variable target variable
6868* ` parents(cpd) ` - obtain the list of parents
69- * ` nparams(cpd ` - obtain the number of free parameters in the CPD
69+ * ` nparams(cpd) ` - obtain the number of free parameters in the CPD
7070* ` cpd(assignment) ` - allows calling ` cpd() ` to obtain the conditional distribution
7171* ` Distributions.fit(Type{CPD}, data, target, parents) `
7272
@@ -88,22 +88,20 @@ The NamedCategorical distribution allows for String or Symbol return values. The
8888bn2 = BayesNet()
8989push!(bn2, StaticCPD(:sighted, NamedCategorical([:bird, :plane, :superman], [0.40, 0.55, 0.05])))
9090push!(bn2, FunctionalCPD{Bernoulli}(:happy, [:sighted], a->Bernoulli(a == :superman ? 0.95 : 0.2)))
91- plot = BayesNets.plot(bn2) # hide
92- TikzPictures.save(SVG("plot3"), plot) # hide
91+ p = BayesNets.plot(bn2)
92+ p
9393```
9494
95- ![ ] ( plot3.svg )
95+
9696
9797Variables can be removed by name using ` delete! ` . A warning will be issued when removing a CPD with children.
9898
9999``` @example bayesnet
100100delete!(bn2, :happy)
101- plot = BayesNets.plot(bn2) # hide
102- TikzPictures.save(SVG("plot4"), plot) # hide
101+ p = BayesNets.plot(bn2)
102+ p
103103```
104104
105- ![ ] ( plot4.svg )
106-
107105## Likelihood
108106
109107A Bayesian Network represents a joint probability distribution, $P(x_1, x_2, \ldots, x_n)$.
@@ -153,11 +151,10 @@ bn = BayesNet()
153151push!(bn, StaticCPD(:a, Categorical([0.3,0.7])))
154152push!(bn, StaticCPD(:b, Categorical([0.6,0.4])))
155153push!(bn, CategoricalCPD{Bernoulli}(:c, [:a, :b], [2,2], [Bernoulli(0.1), Bernoulli(0.2), Bernoulli(1.0), Bernoulli(0.4)]))
156- plot = BayesNets.plot(bn) # hide
157- TikzPictures.save(SVG("plot5"), plot) # hide
154+ p = BayesNets.plot(bn)
155+ p
158156```
159157
160- ![ ] ( plot5.svg )
161158
162159``` julia
163160rand (bn, RejectionSampler (:c => 1 ), 5 )
@@ -202,11 +199,11 @@ b=[1,1,1,2,2,2,2,1,1,2,1,1],
202199a=[1,1,1,2,1,1,2,1,1,2,1,1])
203200
204201bn5 = fit(DiscreteBayesNet, data, (:a=>:b, :a=>:c, :b=>:c))
205- plot = BayesNets.plot(bn5) # hide
206- TikzPictures.save(SVG("plot6"), plot) # hide
202+ p = BayesNets.plot(bn5)
203+ p
207204```
208205
209- ![ ] ( plot6.svg )
206+
210207
211208Fitting a ``` DiscreteCPD ``` , which is a ``` CategoricalCPD{Categorical} ``` , can be done with a specified number of categories. This prevents cases where your test data does not provide an example for every category.
212209
@@ -230,11 +227,11 @@ push!(bn, DiscreteCPD(:c, [:a, :b], [2,2],
230227 Categorical([0.4,0.6]),
231228 ]))
232229
233- plot = BayesNets.plot(bn) # hide
234- TikzPictures.save(SVG("plot7"), plot) # hide
230+ p = BayesNets.plot(bn)
231+ p
235232```
236233
237- ![ ] ( plot7.svg )
234+
238235
239236``` @example bayesnet
240237ϕ = infer(bn, :c, evidence=Assignment(:b=>1))
@@ -288,11 +285,11 @@ parameters = K2GraphSearch([:Species, :SepalLength, :SepalWidth, :PetalLength, :
288285 max_n_parents=2)
289286bn = fit(BayesNet, data, parameters)
290287
291- plot = BayesNets.plot(bn) # hide
292- TikzPictures.save(SVG("plot8"), plot) # hide
288+ p = BayesNets.plot(bn)
289+ p
293290```
294291
295- ![ ] ( plot8.svg )
292+
296293
297294CPD types can also be specified per-node. Note that complete CPD definitions are required - simply using ` StaticCPD ` is insufficient as you need the target distribution type as well, as in ` StaticCPD{Categorical} ` .
298295
@@ -317,11 +314,11 @@ data = DataFrame(c=[1,1,1,1,2,2,2,2,3,3,3,3],
317314parameters = GreedyHillClimbing(ScoreComponentCache(data), max_n_parents=3, prior=UniformPrior())
318315bn = fit(DiscreteBayesNet, data, parameters)
319316
320- plot = BayesNets.plot(bn) # hide
321- TikzPictures.save(SVG("plot9"), plot) # hide
317+ p = BayesNets.plot(bn)
318+ p
322319```
323320
324- ![ ] ( plot9.svg )
321+
325322
326323We can specify the number of categories for each variable in case it cannot be correctly inferred:
327324
@@ -345,10 +342,10 @@ count(bn, :a, data)
345342statistics(bn.dag, data)
346343```
347344``` @example bayesnet
348- table(bn, :b)
345+ BayesNets. table(bn, :b)
349346```
350347``` @example bayesnet
351- table(bn, :c, :a=>1)
348+ BayesNets. table(bn, :c, :a=>1)
352349```
353350
354351## Reading from XDSL
@@ -358,11 +355,11 @@ Discrete Bayesian Networks can be read from the .XDSL file format.
358355``` @example bayesnet
359356bn = readxdsl(joinpath(dirname(pathof(BayesNets)), "..", "test", "sample_bn.xdsl"))
360357
361- plot = BayesNets.plot(bn) # hide
362- TikzPictures.save(SVG("plot10"), plot) # hide
358+ p = BayesNets.plot(bn)
359+ p
363360```
364361
365- ![ ] ( plot10.svg )
362+
366363
367364## Bayesian Score for a Network Structure
368365
@@ -374,5 +371,5 @@ data = DataFrame(c=[1,1,1,1,2,2,2,2,3,3,3,3],
374371 a=[1,1,1,2,1,1,2,1,1,2,1,1])
375372g = DAG(3)
376373add_edge!(g,1,2); add_edge!(g,2,3); add_edge!(g,1,3)
377- bayesian_score(g, [:a,:b,:c], data)
374+ BayesNets. bayesian_score(g, [:a, :b, :c], data)
378375```
0 commit comments