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.

43

we do not need to compute 100,000 samples. Far fewer would be more than sufficient. However,

it does emphasise the convergence properties of the Metropolis algorithm.

We can also consider a concept known as the trace, which is the vector of samples produced

by the MCMC sampling procedure. We can use the helpful traceplot method to plot both a

kernel density estimate (KDE) of the histogram displayed above, as well as the trace itself.

The trace plot is extremely useful for assessing convergence of an MCMC algorithm and

whether we need to exclude a period of initial samples (known as the burn in). To output the

trace we simply call traceplot with the trace variable:

# Show the trace plot

pymc3.traceplot(trace)

plt.show()

The full trace plot is given in Figure 4.3:

Figure 4.3: Trace plot of the MCMC sampling procedure for the fairness parameter θ

As you can see, the KDE estimate of the posterior belief in the fairness reflects both our prior

belief of θ = 0.5 and our data with a sample fairness of θ = 0.2. In addition we can see that the

MCMC sampling procedure has "converged to the distribution" since the sampling series looks

stationary.

For completeness, here is the full listing:

import matplotlib.pyplot as plt

import numpy as np

import pymc3

import scipy.stats as stats

plt.style.use("ggplot")

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

Saved successfully!

Ooh no, something went wrong!