Skip to content
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

Merged
merged 41 commits into from
Nov 19, 2024
Merged

Starting the monty section #6

merged 41 commits into from
Nov 19, 2024

Conversation

MJomaba
Copy link
Contributor

@MJomaba MJomaba commented Nov 4, 2024

No description provided.

@MJomaba
Copy link
Contributor Author

MJomaba commented Nov 12, 2024

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)),
Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

@MJomaba MJomaba Nov 14, 2024

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.

@MJomaba MJomaba marked this pull request as ready for review November 12, 2024 17:47
@MJomaba MJomaba requested review from richfitz and edknock November 12, 2024 17:47
monty.qmd Outdated Show resolved Hide resolved
monty.qmd Outdated Show resolved Hide resolved
monty.qmd Outdated Show resolved Hide resolved
_quarto.yml Outdated Show resolved Hide resolved

## Working with other packages

Exporting chains to work with other packages
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed file for now.

# # Control properties of the combined model:
# monty_model_combine(likelihood, prior,
# monty_model_properties(has_gradient = FALSE))
```
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped for now

monty.qmd Outdated Show resolved Hide resolved
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.
Copy link
Member

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

Copy link
Contributor Author

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
Copy link
Member

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

Copy link
Contributor Author

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

Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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

Copy link
Contributor Author

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
Copy link
Member

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

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been reworked.

monty.qmd Outdated Show resolved Hide resolved
edknock and others added 5 commits November 13, 2024 13:36
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>
@MJomaba MJomaba requested review from richfitz and edknock November 14, 2024 12:21
@MJomaba
Copy link
Contributor Author

MJomaba commented Nov 14, 2024

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.
Copy link
Contributor

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

Copy link
Contributor Author

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.
Copy link
Contributor

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

Copy link
Contributor Author

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 Show resolved Hide resolved
monty.qmd Outdated Show resolved Hide resolved
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

@MJomaba MJomaba Nov 19, 2024

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

Copy link
Contributor Author

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.
Copy link
Contributor

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"?

Copy link
Contributor Author

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 :)

MJomaba and others added 7 commits November 19, 2024 14:06
Co-authored-by: edknock <47318334+edknock@users.noreply.github.com>
Co-authored-by: edknock <47318334+edknock@users.noreply.github.com>
@MJomaba MJomaba requested a review from edknock November 19, 2024 13:18
@edknock edknock merged commit eead55b into main Nov 19, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants