Online Bayesian Learning in PyMCUpdating Posterior Beliefs Repeatedly
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Bayesian learning is a statistical method that updates the probability for a hypothesis as more evidence or information becomes available. Online Bayesian learning refers to the iterative process of updating the posterior distribution as new data comes in, which is critical in environments where data is dynamically changing or where computational resources are limited. PyMC is a Python library that provides tools to easily build Bayesian models and perform approximate inference. In this article, we will explore how to perform Online Bayesian Learning using PyMC, delving into the technical aspects and offering examples to illustrate the process.
Understanding Bayesian Learning
In Bayesian statistics, we use the Bayes' theorem to update our beliefs:
Here: • is the posterior probability of hypothesis given data . • is the likelihood of data given hypothesis . • is the prior probability of the hypothesis. • is the evidence probability.
In online learning, this theorem is applied iteratively: each time new data is received, the posterior becomes the new prior.
PyMC: A Brief Overview
PyMC is a probabilistic programming library in Python that allows for Bayesian analysis through the construction of probabilistic models. It leverages Markov Chain Monte Carlo (MCMC) and Variational Inference for obtaining samples from the posterior distribution.
Setting Up a Simple Model
Before diving into online learning, let's set up a basic Bayesian model in PyMC. We'll analyze a simple problem: estimating the bias of a coin.
• Prior & Likelihood: We start with a conjugate prior, Beta distribution, which updates to a new Beta distribution.
• Updating: This process updates the parameters of the Beta distribution, specifically the alpha
and beta
, based on incoming data.
• Sequential Updates: As each new data batch arrives, we update the posterior and use it as the new prior for subsequent calculations.
• Computational Efficiency: Online updates allow for efficient handling of large data sets or streams of data that are impractical to store or process all at once.
• Hyperparameter Tuning: Careful choice of priors can influence the posterior distribution, depending on initial certainty or domain knowledge.
• Scalability: This method scales well with PyMC’s sampling methods, enabling real-time updates in practical applications.
Related reading
- Online Learning with Tensorflow
- Only absolute URLs are supported when loading Keras model in Tensorflow.js with loadLayersModel
- onnxruntime inference is way slower than pytorch on GPU
- Open Alternatives to Google Prediction API
- Optimal algorithm to calculate the result of a continued fraction
- Optimal weights subset sum using backtracking
- Open Source Neural Network Library
- OpenAI GPT-2 model use with TensorFlow JS

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.