-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from mrc-ide/mrc-5185
DSL speculative docs
- Loading branch information
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
--- | ||
title: "Probabilistic DSL" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{Probabilistic DSL} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
%\VignetteEncoding{UTF-8} | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
`mcstate2` includes a simple probabilistic domain-specific language (DSL) that is inspired by `stan` and [Statistical Rethinking](https://xcelab.net/rm/). It is designed to make some tasks a bit easier, particularly when defining priors for your model. We expect that this DSL is not sufficiently advanced to represent most interesting models but it may get more clever and flexible in the future. In particular we do not expect the DSL to be useful in writing likelihood functions for comparison to data; we expect that if your model is simple enough for this you would be better off using `stan` or some similarly flexible system. | ||
|
||
```{r} | ||
library(mcstate2) | ||
``` | ||
|
||
# Some simple examples | ||
|
||
In chapter 4 of Statistical Rethinking, we build a regression model of height with parameters $\alpha$, $\beta$ and $\sigma$. We can define the model for the prior probability of this model in mcstate by running | ||
|
||
```{r} | ||
prior <- mcstate_dsl({ | ||
alpha ~ Normal(178, 20) | ||
beta ~ Normal(0, 10) | ||
sigma ~ Uniform(0, 50) | ||
}) | ||
``` | ||
|
||
This will define a new `mcstate_model()` object that represents the prior, but with all the bits that we might need depending on how we want to use it: | ||
|
||
We have model parameters | ||
|
||
```{r} | ||
prior$parameters | ||
``` | ||
|
||
These are defined in the order that they appear in your definition (so `alpha` is first and `sigma` is last) | ||
|
||
We can compute the domain for your model: | ||
|
||
```{r} | ||
prior$domain | ||
``` | ||
|
||
We can draw samples from the model if we provide an [mcstate_rng] object | ||
|
||
```{r} | ||
rng <- mcstate_rng$new() | ||
theta <- prior$direct_sample(rng) | ||
theta | ||
``` | ||
|
||
We can compute the (log) density at a point in parameter space | ||
|
||
```{r} | ||
prior$density(theta) | ||
``` | ||
|
||
The computed properties for the model are: | ||
|
||
```{r} | ||
prior$properties | ||
``` |