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.

54

Figure 5.2: Using PyMC3 to fit a Bayesian GLM linear regression model to simulated data

The key point here is that we do not receive a single point estimate for a regression line, i.e.

"a line of best fit", as in the frequentist case. Instead we receive a distribution of likely regression

lines.

We can plot these lines using a method of the glm library called plot_posterior_predictive.

The method takes a trace object and the number of lines to plot (samples).

Firstly we use the seaborn lmplot method, this time with the fit_reg parameter set to

False to stop the frequentist regression line being drawn. Then we plot 100 sampled posterior

predictive regression lines. Finally, we plot the "true" regression line using the original β 0 = 1

and β 1 = 2 parameters. The code snippet below produces such a plot:

..

..

if __name__ == "__main__":

..

..

# Plot a sample of posterior regression lines

sns.lmplot(x="x", y="y", data=df, size=10, fit_reg=False)

plt.xlim(0.0, 1.0)

plt.ylim(0.0, 4.0)

pm.glm.plot_posterior_predictive(trace, samples=100)

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

y = beta_0 + beta_1*x

plt.plot(x, y, label="True Regression Line", lw=3., c="green")

plt.legend(loc=0)

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

Saved successfully!

Ooh no, something went wrong!