Home Minimal Bayesian Von Mises Model Example
Post
Cancel

Minimal Bayesian Von Mises Model Example

Found this code in my miscellaneous bin folder. Instead of deleting it I thought I would share it here. It is a minimal working example of using the PyMC package to model a Von-Mise distribution with priors put on its parameters. It is an extremely small model given by:

\[\kappa \sim \text{Exponential}(1)\] \[\mu \sim \text{Uniform}\left( -\pi, \pi \right)\] \[\theta \sim \text{VonMises}(\mu, \kappa)\]

And here is the aforementioned Python code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt
import numpy as np
import pymc as pm
import arviz as az

with pm.Model() as model:
    kappa = pm.Exponential('kappa', lam=1)
    mu = pm.Uniform('mu', lower=-np.pi, upper=np.pi)
    theta = pm.VonMises('theta', mu=mu, kappa=kappa, observed=[-2]*10 + [2]*10)

with model:
    data = pm.sample(chains=4)
    az.plot_trace(data)
    plt.tight_layout()
    plt.show()

I can see from observed=[-2]*10 + [2]*10 that I was tinkering with having data at exactly two angles. Maybe I was stress-testing the sampler for how it handles bimodality.

Here is an example output. Because I didn’t set a seed number you may find your plot differs from mine somewhat.

Adding az.summary(data, round_to=2).to_markdown('von_mise_bimdal_test.md') to the code above we can also see a summary of the model parameters. It is not a great effective sample size (ESS) and $\hat r$, especially for $\mu$. Actually, $\kappa$ doesn’t look horrible despite the low ESS. The plot above also suggests that there are a couple of states that the chain switches between, which are likely the two modes.

 meansdhdi_3%hdi_97%mcse_meanmcse_sdess_bulkess_tailr_hat
kappa0.730.350.021.310.010.01707.46587.961
mu0.792.65-3.053.140.380.2776.52678.251.04

If I were to continue developing this model I would look at having a mixture distribution for $\mu$. It would also be nice to get rid of that uniform prior on $\mu$ by replacing it with something a little more informative.

This post is licensed under CC BY 4.0 by the author.

Machine Learning For Optography?

Response To Dustin Fife On Rank-Based Nonparametric Statistics