-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Starting the monty section #6
Conversation
…al appearance of pages
…al appearance of pages
…into monty-general
This is a start to the monty section - next step will be to write an example in the odin&monty section based on some real world data from the 2009 flu pandemic. I will also add more odin/monty code in the book in the short term as it is quite wordy at the moment. |
monty.qmd
Outdated
```{r} | ||
l <- seq(from = 0, to = 10, by = 0.1) | ||
plot(l, | ||
exp(Vectorize(l_distribution$density)(l)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@richfitz I need to use Vectorize() here to pass multiple value of the argument - let me know if a better interface can be shown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above. You will need to pass a 1-row matrix in to vectorise , so
l <- matrix(seq(from = 0, to = 10, by = 0.1), 1)
monty_distribution_density(l_distribution, l)
Please do not use $density
in docs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how to make this work. The following code:
fn <- function(l, p, m1, m2) { log(p * dnorm(l, mean = m1) + (1 - p) * dnorm(l, mean = m2)) } l_distribution <- monty_model_function(fn, fixed = list(p = 0.75, m1 = 3, m2 = 7), allow_multiple_parameters = TRUE) l <- matrix(seq(from = 0, to = 10, by = 0.1), 1) monty_model_density(l_distribution, l)
Return the following error,
Error in
packer$unpack(): ! Can't unpack a matrix where the unpacker uses 'fixed' Run
rlang::last_trace() to see where the error occurred.
that I interpret as a conflict with having some parameters set as fixed. I could pass a 101x4 instead with the fixed argument like this (that works fine),
`l_distribution <- monty_model_function(fn,
allow_multiple_parameters = TRUE)
l <- matrix(c(seq(from = 0, to = 10, by = 0.1), rep(0.75,101), rep(3,101), rep(7,101)),4)
monty_model_density(l_distribution, l)`
but then it makes things quite more complex to understand and it's not the same as the monty model has then 4 "parameters" (with a mixed interpretation, as one is the actual sampling variable of interest, and the others are parameters of the distributions in the statistical sense) instead of one.
advanced_monty.qmd
Outdated
|
||
## Working with other packages | ||
|
||
Exporting chains to work with other packages |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be basic, not advanced. perhaps just drop this whole file for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed file for now.
composability.qmd
Outdated
# # Control properties of the combined model: | ||
# monty_model_combine(likelihood, prior, | ||
# monty_model_properties(has_gradient = FALSE)) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure what this file is trying to show, drop for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropped for now
library(monty) | ||
``` | ||
|
||
We can define a simple Gaussian [mixture model](https://en.wikipedia.org/wiki/Mixture_model) of two sub-populations using the `monty_model_function()` from the package. The model represents the distribution of a quantity $l$, sampled with a probability $p$ from a normal distribution of mean $m_{1}$ and with a probability $1-p$ from another normal distribution of mean $m_{2}$. Both subpopulations have variance equal to 1. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sure we could come up with a less abstract example to help people think about this, but this can be done in the next PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there might be a nice biology example with antibody titres and positive vs negative subpopulations - but yes can wait for another iteration.
|
||
While dynamical systems and statistical models serve distinct purposes, they are not strictly separate. In fact, they can be connected through two powerful approaches that bring probabilistic reasoning into dynamical systems: **stochastic processes** and **Bayesian inference**. | ||
|
||
### Stochastic processes: adding uncertainty to dynamical models |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced this belongs here, but leave it here for now. It might belong earlier in some general introduction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree - it touches on odin models, that are upwards in the book.
model.qmd
Outdated
In this way, **stochastic processes** add an uncertainty dimension to the state of dynamical systems, while **Bayesian inference** applies probabilistic modelling to the parameters, merging dynamical and statistical perspectives. Together, these methods provide a rich toolkit for modelling complex systems that evolve over time and accounting for both randomness and uncertainty. | ||
|
||
## Parameters and model complexity | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a missing paragraph here outlining what you are trying to cover in this section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole section has been reworked, for hopefully adding flow and clarity.
model.qmd
Outdated
|
||
## Bayesian or non-Bayesian modelling | ||
|
||
Many statistical modelling tools, such as `stan`, `mcstate`, and `BayesTools`, are inherently Bayesian. However, the `monty` package is **Bayesian-agnostic**: it does not require a Bayesian interpretation. This flexibility allows users to work with probabilistic models either within a Bayesian framework or outside of it, depending on the needs of the analysis. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should drop mcstate
from this, it will just be confusing to people
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
model.qmd
Outdated
|
||
A key concept in Bayesian inference and Monte Carlo methods is the distinction between **normalised** and **unnormalised** probability densities. | ||
|
||
### Unnormalised density |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are just paragraphs, not sections - it would read more smoothly without the headings I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. Done.
|
||
Many statistical modelling tools, such as `stan`, `mcstate`, and `BayesTools`, are inherently Bayesian. However, the `monty` package is **Bayesian-agnostic**: it does not require a Bayesian interpretation. This flexibility allows users to work with probabilistic models either within a Bayesian framework or outside of it, depending on the needs of the analysis. | ||
|
||
## Probability densities: normalised vs. unnormalised |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not feel like it belongs here? Perhaps it belongs in a details section and then linked back to where you want to refer to it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been reworked.
Co-authored-by: edknock <47318334+edknock@users.noreply.github.com>
Co-authored-by: edknock <47318334+edknock@users.noreply.github.com>
Co-authored-by: edknock <47318334+edknock@users.noreply.github.com>
Co-authored-by: edknock <47318334+edknock@users.noreply.github.com>
…Bayesian orientated softwares.
…BayesianTransform sections into paragraphs as suggested in review to improve flow
…ut back with some potential reorganising of the content in a follow-up iteration
…n of matrix and value of variance of proposal distribution
I should have covered all points in the review apart from the one linked with the Vectorize treatment of the density. See comments there: #6 (comment) |
monty.qmd
Outdated
|
||
Hmm, there really is quite a bit of ground to cover here! | ||
The most basic method to define a `monty` model is through a simple R function, as shown in this chapter. More advanced examples are covered in the @sec-monty-models in particular in the context of Bayesian statistics. @sec-monty-dsl introduces the monty DSL for more versatile model building using a small probabilistic DSL similar to BUGs. @sec-monty-combine explains how to compose new monty models by combining two existing ones. The last part of this book starting from @sec-inference demonstrates how to incorporate `odin` models into `monty` workflows. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Referring to sec-monty-combine here, doesn't seem to exist currently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed reference.
monty.qmd
Outdated
|
||
We've discussed a little bit about the philosophy of `monty` and built a simple model using an R function with `monty_function`. The example introduces the basics of defining and sampling from a `monty` model. | ||
|
||
For more advanced applications, refer to @sec-monty-dsl for constructing models using `monty`'s domain-specific language, which enables greater flexibility for complex structures. To combine multiple models, see @sec-monty-combine, which demonstrates how to compose and integrate existing `monty` models. Lastly, @sec-inference illustrates how to incorporate `odin` models into `monty` workflows, expanding the package’s potential for Bayesian inference with more sophisticated, system-based models. Each section provides detailed examples to guide you in leveraging `monty`’s full capabilities. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, referring to sec-monty-combine here when it doesn't seem to exist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed ref.
monty.qmd
Outdated
|
||
## Sampling from our example distribution | ||
|
||
We now want to sample from this model, using the `monty_sample()` function. For this we need to tell `monty` which sampler we want to use to explore our distribution. There are a variety of samplers avalaible and you can learn about them in @sec-samplers. One of the simplest is the random walk [Metropolis-Hastings](https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm) algorithm that should work almost out of the box (though not necesseraly efficiently) in most cases. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now want to sample from this model, using the `monty_sample()` function. For this we need to tell `monty` which sampler we want to use to explore our distribution. There are a variety of samplers avalaible and you can learn about them in @sec-samplers. One of the simplest is the random walk [Metropolis-Hastings](https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm) algorithm that should work almost out of the box (though not necesseraly efficiently) in most cases. | |
We now want to sample from this model, using the `monty_sample()` function. For this we need to tell `monty` which sampler we want to use to explore our distribution. There are a variety of samplers avaliable and you can learn about them in @sec-samplers. One of the simplest is the random walk [Metropolis-Hastings](https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm) algorithm that should work almost out of the box (though not necesseraly efficiently) in most cases. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot - but still not the right spelling due to malignant typo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrected though ;)
monty-dsl.qmd
Outdated
source("common.R") | ||
``` | ||
|
||
The `monty` DSL provides a more intuitive way to define statistical models with `monty`. It is currently relatively basic and focus on providing support for defining priors in Bayesian models. It fully support differentiability allowing to use gradient based samplers on these models. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"focuses on" or "focused on"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went for focuses on :)
…into monty-general
Co-authored-by: edknock <47318334+edknock@users.noreply.github.com>
Co-authored-by: edknock <47318334+edknock@users.noreply.github.com>
…into monty-general
No description provided.