You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,22 +72,19 @@ There are examples available to get you started in the `examples/` folder. Alter
72
72
73
73
Here we show a simple example of how to use RxInfer.jl for Bayesian inference problems. In this example we want to estimate a bias of a coin in a form of a probability distribution in a coin flip simulation.
74
74
75
-
Let's start by creating some dataset. For simplicity in this example we will use static pre-generated dataset. Each sample can be thought of as the outcome of single flip which is either heads or tails (1 or 0). We will assume that our virtual coin is biased, and lands heads up on 75% of the trials (on average).
76
-
77
75
First let's setup our environment by importing all needed packages:
78
-
79
76
```julia
80
77
using RxInfer, Random
81
78
```
82
79
83
-
Next, let's define our dataset:
80
+
We start by creating some dataset. For simplicity in this example we will use static pre-generated dataset. Each sample can be thought of as the outcome of single flip which is either heads or tails (1 or 0). We will assume that our virtual coin is biased, and lands heads up on 75% of the trials (on average).
84
81
85
82
```julia
86
83
n =500# Number of coin flips
87
84
p =0.75# Bias of a coin
88
85
89
86
distribution =Bernoulli(p)
90
-
dataset =float.(rand(distribution, n))
87
+
dataset =rand(distribution, n)
91
88
```
92
89
93
90
### Model specification
@@ -119,10 +116,10 @@ The joint probability is given by the multiplication of the likelihood and the p
Now let's see how to specify this model using GraphPPL's package syntax.
119
+
Now let's see how to specify this model using [GraphPPL's package](https://github.com/ReactiveBayes/GraphPPL.jl) syntax:
123
120
```julia
124
121
# GraphPPL.jl export `@model` macro for model specification
125
-
# It accepts a regular Julia function and builds an FFG under the hood
122
+
# It accepts a regular Julia function and builds a factor graph under the hood
126
123
@modelfunctioncoin_model(y, a, b)
127
124
# We endow θ parameter of our model with some prior
128
125
θ ~Beta(a, b)
@@ -134,18 +131,21 @@ Now let's see how to specify this model using GraphPPL's package syntax.
134
131
end
135
132
```
136
133
137
-
Alternatively, we could use a broadcasting syntax.
134
+
In short, the `@model` macro converts a textual description of a probabilistic model into a corresponding [Factor Graph](https://en.wikipedia.org/wiki/Factor_graph) (FG). In the example above, the $\theta \sim \mathrm{Beta}(a, b)$ expression creates latent variable $θ$ and assigns it as an output of $\mathrm{Beta}$ node in the corresponding FFG. The `~` operation can be understood as _"is modelled by"_. Next, we model each data point `y[i]` as $\mathrm{Bernoulli}$ distribution with $\theta$ as its parameter.
135
+
136
+
> [!TIP]
137
+
> Alternatively, we could use the broadcasting operation:
138
138
```julia
139
139
@modelfunctioncoin_model(y, a, b)
140
140
θ ~Beta(a, b)
141
141
y .~Bernoulli(θ)
142
142
end
143
143
```
144
144
145
-
As you can see, `RxInfer` offers a model specification syntax that resembles closely to the mathematical equations defined above. The $\theta \sim \mathrm{Beta}(2.0, 7.0)$ expression creates random variable $θ$ and assigns it as an output of $\mathrm{Beta}$ node in the corresponding FFG.
145
+
As you can see, `RxInfer`in combination with `GraphPPL`offers a model specification syntax that resembles closely to the mathematical equations defined above.
146
146
147
147
> [!NOTE]
148
-
> `RxInfer.jl` uses `GraphPPL.jl` for model and constraints specification. `GraphPPL.jl` API has been changed in version `4.0.0`. See [Migration Guide](https://reactivebayes.github.io/GraphPPL.jl/stable/) for more details.
148
+
> `GraphPPL.jl` API has been changed in version `4.0.0`. See [Migration Guide](https://reactivebayes.github.io/GraphPPL.jl/stable/) for more details.
0 commit comments