13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

40

4.5.2 Inferring a Binomial Proportion with PyMC3

We are now going to carry out the same analysis using the numerical Markov Chain Monte Carlo

method instead.

Firstly, we need to install PyMC3:

pip install pymc3

Once installed, the next task is to import the necessary libraries, which include Matplotlib,

Numpy, Scipy and PyMC3 itself. We also set the graphical style of the Matplotlib output to be

similar to the ggplot2 graphing library from the R statistical language:

import matplotlib.pyplot as plt

import numpy as np

import pymc3

import scipy.stats as stats

plt.style.use("ggplot")

The next step is to set the prior parameters, as well as the number of coin flip trials carried

out and heads returned. We also specify, for completeness, the parameters of the analyticallycalculated

posterior beta distribution, which we will use for comparison with our MCMC approach.

In addition we specify that we want to carry out 100,000 iterations of the Metropolis

algorithm:

# Parameter values for prior and analytic posterior

n = 50

z = 10

alpha = 12

beta = 12

alpha_post = 22

beta_post = 52

# How many iterations of the Metropolis

# algorithm to carry out for MCMC

iterations = 100000

Now we actually define our beta distribution prior and Bernoulli likelihood model. PyMC3

has a very clean API for carrying this out. It uses a Python with context to assign all of

the parameters, step sizes and starting values to a pymc3.Model instance (which I have called

basic_model, as per the PyMC3 tutorial).

Firstly, we specify the theta parameter as a beta distribution, taking the prior alpha and

beta values as parameters. Remember that our particular values of α = 12 and β = 12 imply a

prior mean µ = 0.5 and a prior s.d. σ = 0.1.

We then define the Bernoulli likelihood function, specifying the fairness parameter p=theta,

the number of trials n=n and the observed heads observed=z, all taken from the parameters

specified above.

At this stage we can find an optimal starting value for the Metropolis algorithm using the

PyMC3 Maximum A Posteriori (MAP) optimisation, the details of which we will omit here.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!