This repository contains simplified, reusable, model templates for running discrete-time Markov Chain Monte Carlo (MCMC) simulations in both Python and R.
To make the code readable and self-contained, the simulation models the behavioral states of the dear, departed Mister Polly Parrot of Monty Python fame. It serves as a basic framework for tracking how an individual organism or population transitions through probabilistic life-history stages until hitting an absorbing state (Ex-Parrot).
The simulation tracks an organism moving through a finite state space
The transition probability matrix
State 4 (Ex-Parrot) is an absorbing state because
Because raw mathematical simulations can hide sampling bugs or structural biases, both the Python and R implementations include interactive diagnostics to validate the model's health before you interpret the biological results.
Both scripts are explicitly segmented into Execution Cells (# %%). Running the simulation and plotting the diagnostic cells side-by-side reveals two metrics:
- The Trace Plot (Sampling Timeline): Tracks the path of the simulation step-by-step.
- Expected Behavior: A healthy run will show rapid, chaotic "fuzzy caterpillar" noise jumping dynamically between the transient biological states (
Resting,Pining,Stunned) before flatlining permanently at the bottom once the parrot joins the choir invisible. - Failure Modes: A sharp, clean diagonal cliff or bolt at the very start indicates a Burn-in Failure (the simulation started in an unnaturally rare state and took too long to find the true baseline). A slow, lazy wave indicates Low Effective Sample Size (ESS), meaning the model is too "sticky" and isn't moving freely between states.
- Expected Behavior: A healthy run will show rapid, chaotic "fuzzy caterpillar" noise jumping dynamically between the transient biological states (
- The Marginal Posterior Distribution (State Frequency): A categorical histogram showing the final, long-term proportion of time spent in each state. This verifies the empirical probability of an organism occupying a specific health bracket (e.g., the exact percentage of the timeline spent "Pining for the Fjords" versus "Resting").
To run the diagnostics locally, ensure your folders contain the standard environment configurations:
- /r/: Requires
ggplot2andpatchworkinstalled in your R library. - /python/: Requires running
pip install -r requirements.txtto loadnumpy,pandas,matplotlib, andseaborn.