by Aditya Pandya
This is the Python script for performing fast Bayesian linear regression.
It accepts input data (x and y) along with their respective errors and provides best-fit results, including the slope, intercept, intrinsic scatter, and their respective errors.
The script contains various function to perform the task and generate plots.
Here are the important functions provided in this script:
-
MCMC()
Performs the Markov Chain Monte Carlo (MCMC) simulation to sample from the posterior distribution of the model parameters. It returns the complete chain of sampled values. -
get_params()
Executes theMCMC()
function and extracts the best-fit parameters after discarding the burn-in steps. This function supports three modes of output:- Return the best-fit parameters along with their standard deviations.
- Return the full parameter chain values.
- Return the best-fit parameters with their upper and lower bounds.
-
plot_chain()
Runs theMCMC()
function and visualizes the chain output. It supports two modes:- Creating a trace plot of the parameter chains.
- Creates a corner plot to visualize parameter correlations.
This script requires the following Python packages:
- NumPy: For numerical computations.
- Numba: For just-in-time (JIT) compilation, which translates Python functions into optimized machine code at runtime to accelerate numerical computations.
- Matplotlib, Seaborn, and Corner: For data visualizations.
To install Numba, use the following command in your terminal: pip install numba
For detailed documentation on Numba, visit the official Numba documentation.
Along with the Python script, a Jupyter Notebook (FBR_tutorial.ipynb) is provided as a tutorial for understanding the basics of Bayesian linear regression. This notebook contains simplified functions from the script to provides step-by-step guidance for performing Bayesian analysis.
Below is the flowchart of the algorithm used for Bayesian linear regression.