Skip to content

Commit

Permalink
move doc for register_primitives to a separate page
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxd3 committed Feb 27, 2024
1 parent de2532e commit 51b9438
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ makedocs(;
"General" => "api.md",
"Functions" => "functions.md",
"Distributions" => "distributions.md",
"User-defined Functions and Distributions" => "user_defined_functions.md"
],
"Plotting" => "graph_plotting.md",
"R Interface" => "R_interface.md",
Expand Down
27 changes: 0 additions & 27 deletions docs/src/example.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Example: Logistic Regression with Random Effects

## Example: Logistic Regression with Random Effects
We will use the [Seeds](https://chjackson.github.io/openbugsdoc/Examples/Seeds.html) model for demonstration.
This example concerns the proportion of seeds that germinated on each of 21 plates. Here, we transform the data into a `NamedTuple`:

Expand Down Expand Up @@ -107,32 +106,6 @@ model{
By default, `@bugs` will translate R-style variable names like `a.b.c` to `a_b_c`, user can pass `false` as the second argument to disable this.
We still encourage users to write new programs using the Julia-native syntax, because of better debuggability and perks like syntax highlighting.

### Using Self-defined Functions and Distributions
Users can register their own functions and distributions with macros. However, note that any functions used must be _pure_ mathematical functions, i.e., side-effect free.

```julia
julia> # Should be restricted to pure functions that do simple operations
@register_primitive function f(x)
return x + 1
end

julia> JuliaBUGS.f(2)
3
```

Users can also `introduce` a function into `JuliaBUGS`, by

```julia
julia> f(x) = x + 1

julia> @register_primitive(f)

julia> JuliaBUGS.f(1)
2
```

After registering the function or distributions, they can be used just like any other functions or distributions provided by BUGS.

## Compilation

For now, the `compile` function will create a `BUGSModel`, which implements [`LogDensityProblems.jl`](https://github.com/tpapp/LogDensityProblems.jl) interface.
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The package aims to support modelling and inference for probabilistic programs w

This project is still in its early stage, with many key components needing to be completed.

Please refer to the [Github project page](https://github.com/TuringLang/SymbolicPPL.jl) for usage information and a complete example.
Please refer to the [example](https://turinglang.org/JuliaBUGS.jl/stable/example/) for usage information and a complete example.

## What is BUGS?

Expand Down
29 changes: 29 additions & 0 deletions docs/src/user_defined_functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Define and Use Your Own Functions and Distributions

For now, out of the box, JuliaBUGS only allows functions and distributions defined in `BUGSPrimitives` to be used in the model.
With the `@register_primitive` macro, users can register their own functions and distributions with JuliaBUGS. It is important to ensure that any functions used are _pure_ mathematical functions.
This implies that such functions should not alter any external state including but not limited to modifying global variables, writing data to files. (Printing might be okay, but do at discretion.)

```julia
julia> JuliaBUGS.@register_primitive function f(x)
return x + 1
end
f (generic function with 1 method)

julia> JuliaBUGS.f(2)
3
```

Users can also `introduce` a function into `JuliaBUGS`, by

```julia
julia> f(x) = x + 1
f (generic function with 1 method)

julia> JuliaBUGS.@register_primitive(f);

julia> JuliaBUGS.f(1)
2
```

After registering the function or distributions, they can be used just like any other functions or distributions provided by BUGS.

0 comments on commit 51b9438

Please sign in to comment.