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: JuliaBUGS/docs/src/model_macro.md
+25-19Lines changed: 25 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,21 +2,6 @@
2
2
3
3
JuliaBUGS provides the `@model` macro for defining probabilistic models in a Julia-native way. This guide explains how to create and use models effectively.
4
4
5
-
## Core Concepts
6
-
7
-
When defining a model, you work with two main categories of variables:
8
-
9
-
### 1. Stochastic Parameters
10
-
Variables that follow probability distributions, defined with the `~` operator:
11
-
-**Unobserved parameters**: Variables to be sampled during inference
12
-
-**Observed data**: Known values that the model conditions on
13
-
14
-
### 2. Constants and Covariates
15
-
Deterministic inputs that don't have probability distributions:
16
-
-**Covariates/predictors**: Input features like `x` in regression models
17
-
-**Structural constants**: Values that determine model structure (e.g., `N` for array sizes)
18
-
-**Fixed parameters**: Any other non-stochastic inputs
19
-
20
5
## The `@model` Macro
21
6
22
7
The `@model` macro creates a function that returns a `BUGSModel` object. Here's the basic syntax:
@@ -38,12 +23,30 @@ end
38
23
39
24
2.**Remaining arguments**: All constants, covariates, and structural parameters
40
25
26
+
### Two Types of Variables
27
+
28
+
As shown in the above model, we have two main categories of variables:
29
+
30
+
#### 1. Stochastic Parameters
31
+
Variables that follow probability distributions, defined with the `~` operator:
32
+
-**Unobserved parameters**: Variables to be sampled during inference
33
+
-**Observed data**: Known values that the model conditions on
34
+
35
+
#### 2. Constants and Covariates
36
+
Deterministic inputs that don't have probability distributions:
37
+
-**Covariates/predictors**: Input features like `x` in regression models
38
+
-**Structural constants**: Values that determine model structure (e.g., `N` for array sizes)
39
+
-**Fixed parameters**: Any other non-stochastic inputs
40
+
41
41
### Example: Linear Regression
42
42
43
43
```julia
44
+
using Julia
45
+
using JuliaBUGS.BUGSPrimitives
46
+
44
47
@modelfunctionlinear_regression(
45
48
(; y, beta, sigma), # y is data, beta and sigma are parameters
46
-
X, N # X is covariate matrix, N is size
49
+
X, N # X is the covariate matrix, N is the size
47
50
)
48
51
for i in1:N
49
52
y[i] ~dnorm(mu[i], sigma)
@@ -91,14 +94,14 @@ When you provide an `of` type annotation, JuliaBUGS automatically validates the
91
94
# Create model with no observations (sample from prior)
92
95
model =my_model((;), constants...)
93
96
94
-
# Create model with some observed values
97
+
# Create a model with some observed values
95
98
model =my_model((; y = observed_data), constants...)
96
99
97
100
# Create model with all parameters specified
98
101
model =my_model((; param1 = val1, param2 = val2), constants...)
99
102
```
100
103
101
-
### Using `unflatten` for Initialization
104
+
### Using `unflatten` for Initialisation
102
105
103
106
The `unflatten` utility helps create parameter instances with missing values:
104
107
@@ -109,14 +112,17 @@ using JuliaBUGS: unflatten
109
112
params =unflatten(MyParamType, missing)
110
113
model =my_model(params, constants...)
111
114
112
-
# This is useful for models that need initialization
115
+
# This is useful for models that need initialisation
113
116
```
114
117
115
118
## Complete Example: Hierarchical Model
116
119
117
120
Here's a complete example showing all the concepts together:
0 commit comments