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.

53

# Use the No-U-Turn Sampler

step = pm.NUTS()

# Calculate the trace

trace = pm.sample(

iterations, step, start,

random_seed=42, progressbar=True

)

return trace

...

...

if __name__ == "__main__":

...

...

trace = glm_mcmc_inference(df, iterations=5000)

pm.traceplot(trace[500:])

plt.show()

The output of the script is as follows:

Applied log-transform to sd and added transformed sd_log to model.

[---- 11% ] 563 of 5000 complete in 0.5 sec

[--------- 24% ] 1207 of 5000 complete in 1.0 sec

[-------------- 37% ] 1875 of 5000 complete in 1.5 sec

[-----------------51% ] 2561 of 5000 complete in 2.0 sec

[-----------------64%---- ] 3228 of 5000 complete in 2.5 sec

[-----------------78%--------- ] 3920 of 5000 complete in 3.0 sec

[-----------------91%-------------- ] 4595 of 5000 complete in 3.5 sec

[-----------------100%-----------------] 5000 of 5000 complete in 3.8 sec

The traceplot is given in Figure 5.2:

We covered the basics of traceplots in the previous chapter. Recall that Bayesian models

provide a full posterior probability distribution for each of the model parameters, as opposed to

a frequentist point estimate.

On the left side of the panel we can see marginal distributions for each parameter of interest.

Notice that the intercept β 0 distribution has its mode/maximum posterior estimate almost exactly

at 1, close to the true parameter of β 0 = 1. The estimate for the slope β 1 parameter has a

mode at approximately 1.98, close to the true parameter value of β 1 = 2. The ɛ error parameter

associated with the model measurement noise has a mode of approximately 0.465, which is close

to the true value of ɛ = 0.5.

In all cases there is a reasonable variance associated with each marginal posterior, telling us

that there is some degree of uncertainty in each of the values. Were we to simulate more data,

and carry out more samples, this variance would likely decrease.

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

Saved successfully!

Ooh no, something went wrong!