Python for Finance: Analyze Big Financial Data

(Elle) #1
plt.xlabel(‘GDX’)
plt.ylabel(‘GLD’)
plt.colorbar(ticks=mpl.dates.DayLocator(interval= 250 ),
format=mpl.dates.DateFormatter(’%d %b %y’))

Figure 11-23. Scatter plot of prices for GLD and GDX

Let us implement a Bayesian regression on the basis of these two time series. The


parameterizations are essentially the same as in the previous example with dummy data;


we just replace the dummy data with the real data we now have available:


In  [ 39 ]: with pm.Model() as model:
alpha = pm.Normal(‘alpha’, mu= 0 , sd= 20 )
beta = pm.Normal(‘beta’, mu= 0 , sd= 20 )
sigma = pm.Uniform(‘sigma’, lower= 0 , upper= 50 )

y_est   =   alpha   +   beta    *   data[‘GDX’].values

likelihood  =   pm.Normal(‘GLD’,    mu=y_est,   sd=sigma,
observed=data[‘GLD’].values)

start   =   pm.find_MAP()
step = pm.NUTS(state=start)
trace = pm.sample( 100 , step, start=start, progressbar=False)

Figure 11-24 shows the results from the MCMC sampling procedure given the


assumptions about the prior probability distributions for the three parameters:


In  [ 40 ]: fig =   pm.traceplot(trace)
plt.figure(figsize=( 8 , 8 ))
Free download pdf