def simulate_serial(nsim, nobs, ntoss, simpars): # initialize arrays to store simulation results MomentsAll = np.zeros((6, 7, nsim)) EEmatAll = np.zeros((3, nsim)) # perform simulations for s in range(0, nsim): # draw randowm errors eps = np.random.randn(nobs) * sigma # initialize variables z = np.zeros((nobs + 1)) k = np.zeros((nobs + 1)) y = np.zeros(nobs) r = np.zeros(nobs) w = np.zeros(nobs) i = np.zeros(nobs) c = np.zeros(nobs) # set starting values k[0] = kbar z[0] = eps[0] # iteratively generate data for t in range(0, nobs): z[t + 1] = example_lfunc(z[t], eps[t], lpars) k[t + 1] = example_tfunc(k[t], z[t], tpars) y[t], c[t], i[t], r[t], w[t] = \ example_def(k[t+1], k[t], z[t], param) # discard las observation for k & z k = k[0:nobs] z = z[0:nobs] # stack data into a single array for moments calculation data = np.stack((y, c, i, r, w, k, z), axis=1) # discard first ntoss observations data = data[ntoss:nobs, :] # find moments (Moments, MomNames) = calcmom(data, means=True, stds=True, relstds=True, corrs=True, autos=True, cvars=True) # find Euler Errors Xdata = k.reshape(len(k), 1) Zdata = z.reshape(len(z), 1) EErrs = EEcalc(Xdata, Zdata, efunc, epars, tfunc, tpars, lfunc, lpars) MaxAbsEE = np.max(np.abs(EErrs)) MeanAbsEE = np.mean(np.abs(EErrs)) RootMeanSqEE = (np.mean(EErrs**2))**.5 EEmat = np.stack((MaxAbsEE, MeanAbsEE, RootMeanSqEE)) # Add results of current simulation to results arrays MomentsAll[:, :, s] = Moments EEmatAll[:, s] = EEmat return MomentsAll, EEmatAll, MomNames
y = np.zeros(nobs) r = np.zeros(nobs) w = np.zeros(nobs) i = np.zeros(nobs) c = np.zeros(nobs) k[0] = kbar * kstart z[0] = eps[0] for t in range(0, nobs): z[t + 1] = example_lfunc(z[t], eps[t], lpars) k[t + 1] = example_tfunc(k[t], z[t], tpars) y[t], c[t], i[t], r[t], w[t] = example_def(k[t + 1], k[t], z[t], param) k = k[0:nobs] z = z[0:nobs] # find Euler Errors Xdata = k.reshape(len(k), 1) Zdata = z.reshape(len(z), 1) efunc = example_efunc epars = param tfunc = example_tfunc lfunc = example_lfunc EErrs = EEcalc(Xdata, Zdata, efunc, epars, tfunc, tpars, lfunc, lpars) MaxAbsEE = np.max(np.abs(EErrs)) MeanAbsEE = np.mean(np.abs(EErrs)) RootMeanSqEE = (np.mean(EErrs**2))**.5 print 'Euler Error Summary Statistics' print 'Maximum Absolute Euler Error:', MaxAbsEE print 'Mean Absolute Euler Error:', MeanAbsEE print 'Root Mean Squared Euler Error:', RootMeanSqEE