13.08.2022 Views

advanced-algorithmic-trading

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

44

# 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

# Use PyMC3 to construct a model context

basic_model = pymc3.Model()

with basic_model:

# Define our prior belief about the fairness

# of the coin using a Beta distribution

theta = pymc3.Beta("theta", alpha=alpha, beta=beta)

# Define the Bernoulli likelihood function

y = pymc3.Binomial("y", n=n, p=theta, observed=z)

# Carry out the MCMC analysis using the Metropolis algorithm

# Use Maximum A Posteriori (MAP) optimisation as initial value for MCMC

start = pymc3.find_MAP()

# Use the Metropolis algorithm (as opposed to NUTS or HMC, etc.)

step = pymc3.Metropolis()

# Calculate the trace

trace = pymc3.sample(

iterations, step, start, random_seed=1, progressbar=True

)

# Plot the posterior histogram from MCMC analysis

bins=50

plt.hist(

trace["theta"], bins,

histtype="step", normed=True,

label="Posterior (MCMC)", color="red"

)

# Plot the analytic prior and posterior beta distributions

x = np.linspace(0, 1, 100)

plt.plot(

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

Saved successfully!

Ooh no, something went wrong!