Bayesian Machine Learning in Quant Trading: Updating Beliefs with Conjugate Priors
An interactive guide to using Beta-Binomial models, prior probability estimation, and dynamic posterior updating for trade simulation
Environment preparation
import warnings
warnings.filterwarnings('ignore')The cell is just setting up the notebook environment by turning off warning messages. First it imports the warnings module, which is Python’s built-in way of controlling how runtime warnings are handled. Then it tells Python to ignore warnings from that point on, so any non-fatal notices that might otherwise appear in later cells will be hidden. There is no visible output because nothing is being computed or displayed here; the effect is simply a change in the notebook’s behavior for the rest of the session, making the later output cleaner and less cluttered.
%matplotlib inline
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
import scipy.stats as stats
from matplotlib.ticker import FuncFormatterThe cell sets up the main tools needed for the analysis and plotting that come later. The first line tells Matplotlib to display figures directly inside the notebook rather than in a separate window, which is why any plots produced afterward will appear inline below their cells. The remaining lines import the numerical, data-handling, plotting, and statistical libraries that the notebook relies on: NumPy for array-based computation, pandas for working with tabular data, Matplotlib and Seaborn for visualization, SciPy’s statistics module for probability distributions, and a tick formatter for controlling how axis labels are displayed.
Nothing is shown as output here because the cell is only preparing the environment. Its effect is foundational rather than visible: it makes sure the later cells can generate data, compute Beta distributions, and format the figures cleanly without needing to repeat these imports each time.
import matplotlib as mpl
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']The purpose here is to adjust Matplotlib’s text rendering so that any labels, titles, or annotations drawn later can use LaTeX formatting. First the Matplotlib library is imported under its usual shorthand, and then a global plotting setting is changed to turn LaTeX text rendering on. That means Matplotlib will send text elements through a LaTeX engine instead of using its default text system, which often produces more polished mathematical notation. The next line adds the amsmath package to the LaTeX preamble, so later plots can use a wider range of math formatting commands and display equations more cleanly.
There is no saved output because nothing is being plotted or printed here. The cell simply prepares the plotting environment in the background, changing configuration rather than producing visible results right away. The effect will show up later when figures are rendered, especially in any mathematical labels or axis annotations that rely on LaTeX styling.
np.random.seed(42)
sns.set_style('dark')This cell sets up the notebook’s visual and random-number behavior so the later results are repeatable and consistently styled. The first line fixes the random seed, which means any simulated data generated afterward will come out the same every time the notebook is run. That is important for a teaching example, because it keeps the figures and numerical results stable instead of changing from one run to the next. The second line switches Seaborn to a dark plotting style, so the charts that appear later use a darker background and a matching color palette. There is no saved output because these are configuration commands rather than calculations or displays; they quietly change the environment in the background and prepare the notebook for the analysis and plots that follow.


