An implementation of Adams & MacKay 2007 "Bayesian Online Changepoint Detection" for a binomial input. In pure Python.
@TECHREPORT{ adams-mackay-2007,
AUTHOR = {Ryan Prescott Adams and David J.C. MacKay},
TITLE = "{B}ayesian Online Changepoint Detection",
INSTITUTION = "University of Cambridge",
ADDRESS = "Cambridge, UK",
YEAR = "2007",
NOTE = "arXiv:0710.3742v1 [stat.ML]",
URL = "http://arxiv.org/abs/0710.3742"
}
In a nutshell, you observe a sequence of binary trials (ON/OFF, Left/Right, 0/1, ...), which have an underlying structure for their cause:
The goal is to retrieve the moments of the switches which define epochs and the values of the probability biases within each epoch:
-
check out some basics
-
perform inference on experimental data
-
fit best hazard rates
-
some fun with twitter data
-
this code was used in the following publication: "Humans adapt their anticipatory eye movements to the volatility of visual motion properties" (2020) by Pasturel, Montagnini and Prrinet
@article{PasturelMontagniniPerrinet20,
title = {Humans adapt their anticipatory eye movements to the volatility of visual motion properties},
journal = {PLoS Computational Biology},
author = {Pasturel, Chloé and Montagnini, Anna and Perrinet, Laurent U},
date = {2020-01-26},
doi = {10.1101/784116},
keywords = {motion anticipation},
preprint = {https://www.biorxiv.org/content/10.1101/784116v3},
url = {https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1007438},
url_code = {https://github.com/laurentperrinet/PasturelMontagniniPerrinet2020},
url_pdf = {https://www.biorxiv.org/content/biorxiv/early/2019/09/26/784116.full-text.pdf},
year = {2020}
}
- Initialize
-
$P(r_0)= S(r)$ or$P(r_0=0)=1$ and - $ν^{(0)}1 = ν{prior}$ and $χ^{(0)}1 = χ{prior}$
- Observe New Datum
$x_t$ - Evaluate Predictive Probability
$π_{1:t} = P(x |ν^{(r)}_t,χ^{(r)}_t)$ - Calculate Growth Probabilities $P(r_t=r_{t-1}+1, x_{1:t}) = P(r_{t-1}, x_{1:t-1}) π^{(r)}t (1−H(r^{(r)}{t-1}))$
- Calculate Changepoint Probabilities $P(r_t=0, x_{1:t})= \sum_{r_{t-1}} P(r_{t-1}, x_{1:t-1}) π^{(r)}t H(r^{(r)}{t-1})$
- Calculate Evidence
$P(x_{1:t}) = \sum_{r_{t-1}} P (r_t, x_{1:t})$ - Determine Run Length Distribution
$P (r_t | x_{1:t}) = P (r_t, x_{1:t})/P (x_{1:t}) $ - Update Sufficient Statistics :
- $ν^{(0)}{t+1} = ν{prior}$, $χ^{(0)}{t+1} = χ{prior}$
- $ν^{(r+1)}{t+1} = ν^{(r)}{t} +1$, $χ^{(r+1)}{t+1} = χ^{(r)}{t} + u(x_t)$
- Perform Prediction
$P (x_{t+1} | x_{1:t}) = P (x_{t+1}|x_{1:t} , r_t) P (r_t|x_{1:t})$ - go to (2)
An application is to analyze long sequences of binary events, such as detailed in the following example:
- adapted from url='https://github.com/JackKelly/bayesianchangepoint', by Copyright 2013 Jack Kelly (aka Daniel) [email protected] author='Jack Kelly', author_email='[email protected]'. This code is based on the beautifully commented MATLAB implementation that was provided by Ryan Adams (no longer available online). http://hips.seas.harvard.edu/content/bayesian-online-changepoint-detection