essentially the same. Note, though, that the computational burden increases substantially
since we have to estimate per random walk sample 1,950 / 50 = 39 parameter pairs
(instead of 1, as before):
In [ 46 ]: import scipy.optimize as sco
with model_randomwalk:
# first optimize random walk
start = pm.find_MAP(vars=[alpha, beta], fmin=sco.fmin_l_bfgs_b)
# sampling
step = pm.NUTS(scaling=start)
trace_rw = pm.sample( 100 , step, start=start, progressbar=False)
In total, we have 100 estimates with 39 time intervals:
In [ 47 ]: np.shape(trace_rw[‘alpha’])
Out[47]: (100, 39)
We can illustrate the evolution of the regression factors alpha and beta over time by
plotting a subset of the estimates and the average over all samples, as in Figure 11-26:
In [ 48 ]: part_dates = np.linspace(min(mpl_dates), max(mpl_dates), 39 )
In [ 49 ]: fig, ax1 = plt.subplots(figsize=( 10 , 5 ))
plt.plot(part_dates, np.mean(trace_rw[‘alpha’], axis= 0 ),
‘b’, lw=2.5, label=‘alpha’)
for i in range( 45 , 55 ):
plt.plot(part_dates, trace_rw[‘alpha’][i], ‘b-.’, lw=0.75)
plt.xlabel(‘date’)
plt.ylabel(‘alpha’)
plt.axis(‘tight’)
plt.grid(True)
plt.legend(loc= 2 )
ax1.xaxis.set_major_formatter(mpl.dates.DateFormatter(’%d %b %y’) )
ax2 = ax1.twinx()
plt.plot(part_dates, np.mean(trace_rw[‘beta’], axis= 0 ),
‘r’, lw=2.5, label=‘beta’)
for i in range( 45 , 55 ):
plt.plot(part_dates, trace_rw[‘beta’][i], ‘r-.’, lw=0.75)
plt.ylabel(‘beta’)
plt.legend(loc= 4 )
fig.autofmt_xdate()
Figure 11-26. Evolution of (mean) alpha and (mean) beta over time (updated estimates over time)
ABSOLUTE PRICE DATA VERSUS RELATIVE RETURN DATA
Both when presenting the PCA analysis implementation and for this example about Bayesian statistics, we’ve
worked with absolute price levels instead of relative (log) return data. This is for illustration purposes only,