Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
[deps]
BayesNets = "ba4760a4-c768-5bed-964b-cf806dc591cb"
Compose = "a81c6b42-2e10-5240-aca2-a61377ecd94b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Discretizers = "6e83dbb3-75ca-525b-8ae2-3751f0dd50b4"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
RDatasets = "ce6b1742-4840-55fa-b093-852dadbb1d8b"
TikzGraphs = "b4f28e30-c73f-5eaf-a395-8a9db949a742"
TikzPictures = "37f6aa50-8035-52d0-81c2-5a1d08754b2d"

[compat]
Documenter = "1"
Documenter = "1"
62 changes: 32 additions & 30 deletions docs/src/usage.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
```@meta
CurrentModule = BayesNets
```
# Usage

```@setup bayesnet
using BayesNets, TikzGraphs, TikzPictures
```

```julia
using Random
Random.seed!(0) # seed the random number generator to 0, for a reproducible demonstration
using BayesNets
using TikzGraphs # required to plot tex-formatted graphs (recommended), otherwise GraphPlot.jl is used
using TikzPictures
using Compose
import Compose: SVG
using DataFrames
using Distributions
using Random
using Graphs
Random.seed!(0)
```

## Representation
Expand All @@ -25,8 +27,8 @@ a = \mathcal{N}(0,1) \qquad b = \mathcal{N}(2a +3,1)
bn = BayesNet()
push!(bn, StaticCPD(:a, Normal(1.0)))
push!(bn, LinearGaussianCPD(:b, [:a], [2.0], 3.0, 1.0))
plot = BayesNets.plot(bn)
TikzPictures.save(SVG("plot1"), plot)
p = BayesNets.plot(bn)
draw(SVG(joinpath(@__DIR__, "plot1.svg"), 400, 400), p)
```

![](plot1.svg)
Expand Down Expand Up @@ -56,8 +58,8 @@ cpdA = fit(StaticCPD{Normal}, data, :a)
cpdB = fit(LinearGaussianCPD, data, :b, [:a])

bn2 = BayesNet([cpdA, cpdB])
plot = BayesNets.plot(bn2) # hide
TikzPictures.save(SVG("plot2"), plot) # hide
p = BayesNets.plot(bn2)
draw(SVG(joinpath(@__DIR__, "plot2.svg"), 400, 400), p)
```

![](plot2.svg)
Expand All @@ -66,7 +68,7 @@ Each `CPD` implements four functions:

* `name(cpd)` - obtain the name of the variable target variable
* `parents(cpd)` - obtain the list of parents
* `nparams(cpd` - obtain the number of free parameters in the CPD
* `nparams(cpd)` - obtain the number of free parameters in the CPD
* `cpd(assignment)` - allows calling `cpd()` to obtain the conditional distribution
* `Distributions.fit(Type{CPD}, data, target, parents)`

Expand All @@ -88,8 +90,8 @@ The NamedCategorical distribution allows for String or Symbol return values. The
bn2 = BayesNet()
push!(bn2, StaticCPD(:sighted, NamedCategorical([:bird, :plane, :superman], [0.40, 0.55, 0.05])))
push!(bn2, FunctionalCPD{Bernoulli}(:happy, [:sighted], a->Bernoulli(a == :superman ? 0.95 : 0.2)))
plot = BayesNets.plot(bn2) # hide
TikzPictures.save(SVG("plot3"), plot) # hide
p = BayesNets.plot(bn2)
draw(SVG(joinpath(@__DIR__, "plot3.svg"), 400, 400), p)
```

![](plot3.svg)
Expand All @@ -98,8 +100,8 @@ Variables can be removed by name using `delete!`. A warning will be issued when

```@example bayesnet
delete!(bn2, :happy)
plot = BayesNets.plot(bn2) # hide
TikzPictures.save(SVG("plot4"), plot) # hide
p = BayesNets.plot(bn2)
draw(SVG(joinpath(@__DIR__, "plot4.svg"), 400, 400), p)
```

![](plot4.svg)
Expand Down Expand Up @@ -153,8 +155,8 @@ bn = BayesNet()
push!(bn, StaticCPD(:a, Categorical([0.3,0.7])))
push!(bn, StaticCPD(:b, Categorical([0.6,0.4])))
push!(bn, CategoricalCPD{Bernoulli}(:c, [:a, :b], [2,2], [Bernoulli(0.1), Bernoulli(0.2), Bernoulli(1.0), Bernoulli(0.4)]))
plot = BayesNets.plot(bn) # hide
TikzPictures.save(SVG("plot5"), plot) # hide
p = BayesNets.plot(bn)
draw(SVG(joinpath(@__DIR__, "plot5.svg"), 400, 400), p)
```

![](plot5.svg)
Expand Down Expand Up @@ -202,8 +204,8 @@ b=[1,1,1,2,2,2,2,1,1,2,1,1],
a=[1,1,1,2,1,1,2,1,1,2,1,1])

bn5 = fit(DiscreteBayesNet, data, (:a=>:b, :a=>:c, :b=>:c))
plot = BayesNets.plot(bn5) # hide
TikzPictures.save(SVG("plot6"), plot) # hide
p = BayesNets.plot(bn5)
draw(SVG(joinpath(@__DIR__, "plot6.svg"), 400, 400), p)
```

![](plot6.svg)
Expand All @@ -230,8 +232,8 @@ push!(bn, DiscreteCPD(:c, [:a, :b], [2,2],
Categorical([0.4,0.6]),
]))

plot = BayesNets.plot(bn) # hide
TikzPictures.save(SVG("plot7"), plot) # hide
p = BayesNets.plot(bn)
draw(SVG(joinpath(@__DIR__, "plot7.svg"), 400, 400), p)
```

![](plot7.svg)
Expand Down Expand Up @@ -288,8 +290,8 @@ parameters = K2GraphSearch([:Species, :SepalLength, :SepalWidth, :PetalLength, :
max_n_parents=2)
bn = fit(BayesNet, data, parameters)

plot = BayesNets.plot(bn) # hide
TikzPictures.save(SVG("plot8"), plot) # hide
p = BayesNets.plot(bn)
draw(SVG(joinpath(@__DIR__, "plot8.svg"), 400, 400), p)
```

![](plot8.svg)
Expand Down Expand Up @@ -317,8 +319,8 @@ data = DataFrame(c=[1,1,1,1,2,2,2,2,3,3,3,3],
parameters = GreedyHillClimbing(ScoreComponentCache(data), max_n_parents=3, prior=UniformPrior())
bn = fit(DiscreteBayesNet, data, parameters)

plot = BayesNets.plot(bn) # hide
TikzPictures.save(SVG("plot9"), plot) # hide
p = BayesNets.plot(bn)
draw(SVG(joinpath(@__DIR__, "plot9.svg"), 400, 400), p)
```

![](plot9.svg)
Expand Down Expand Up @@ -358,8 +360,8 @@ Discrete Bayesian Networks can be read from the .XDSL file format.
```@example bayesnet
bn = readxdsl(joinpath(dirname(pathof(BayesNets)), "..", "test", "sample_bn.xdsl"))

plot = BayesNets.plot(bn) # hide
TikzPictures.save(SVG("plot10"), plot) # hide
p = BayesNets.plot(bn)
draw(SVG(joinpath(@__DIR__, "plot10.svg"), 400, 400), p)
```

![](plot10.svg)
Expand All @@ -374,5 +376,5 @@ data = DataFrame(c=[1,1,1,1,2,2,2,2,3,3,3,3],
a=[1,1,1,2,1,1,2,1,1,2,1,1])
g = DAG(3)
add_edge!(g,1,2); add_edge!(g,2,3); add_edge!(g,1,3)
bayesian_score(g, [:a,:b,:c], data)
BayesNets.bayesian_score(g, [:a, :b, :c], data)
```