@@ -359,6 +359,52 @@ Confirm this by simulation at a range of $k$ using the default parameters from t
359359```
360360361361362+```{solution-start} ar1p_ex1
363+:class: dropdown
364+```
365+366+Here is one solution:
367+368+```{code-cell} python3
369+from numba import njit
370+from scipy.special import factorial2
371+372+@njit
373+def sample_moments_ar1(k, m=100_000, mu_0=0.0, sigma_0=1.0, seed=1234):
374+ np.random.seed(seed)
375+ sample_sum = 0.0
376+ x = mu_0 + sigma_0 * np.random.randn()
377+ for t in range(m):
378+ sample_sum += (x - mu_star)**k
379+ x = a * x + b + c * np.random.randn()
380+ return sample_sum / m
381+382+def true_moments_ar1(k):
383+ if k % 2 == 0:
384+ return std_star**k * factorial2(k - 1)
385+ else:
386+ return 0
387+388+k_vals = np.arange(6) + 1
389+sample_moments = np.empty_like(k_vals)
390+true_moments = np.empty_like(k_vals)
391+392+for k_idx, k in enumerate(k_vals):
393+ sample_moments[k_idx] = sample_moments_ar1(k)
394+ true_moments[k_idx] = true_moments_ar1(k)
395+396+fig, ax = plt.subplots()
397+ax.plot(k_vals, true_moments, label="true moments")
398+ax.plot(k_vals, sample_moments, label="sample moments")
399+ax.legend()
400+401+plt.show()
402+```
403+404+```{solution-end}
405+```
406+407+362408```{exercise}
363409:label: ar1p_ex2
364410@@ -405,95 +451,6 @@ of these distributions?)
405451```
406452407453408-```{exercise}
409-:label: ar1p_ex3
410-411-In the lecture we discussed the following fact: for the $AR(1)$ process
412-413-$$
414-X_{t+1} = a X_t + b + c W_{t+1}
415-$$
416-417-with $\{ W_t \}$ iid and standard normal,
418-419-$$
420-\psi_t = N(\mu, s^2) \implies \psi_{t+1}
421-= N(a \mu + b, a^2 s^2 + c^2)
422-$$
423-424-Confirm this, at least approximately, by simulation. Let
425-426-- $a = 0.9$
427-- $b = 0.0$
428-- $c = 0.1$
429-- $\mu = -3$
430-- $s = 0.2$
431-432-First, plot $\psi_t$ and $\psi_{t+1}$ using the true
433-distributions described above.
434-435-Second, plot $\psi_{t+1}$ on the same figure (in a different
436-color) as follows:
437-438-1. Generate $n$ draws of $X_t$ from the $N(\mu, s^2)$
439- distribution
440-1. Update them all using the rule
441- $X_{t+1} = a X_t + b + c W_{t+1}$
442-1. Use the resulting sample of $X_{t+1}$ values to produce a
443- density estimate via kernel density estimation.
444-445-Try this for $n=2000$ and confirm that the
446-simulation based estimate of $\psi_{t+1}$ does converge to the
447-theoretical distribution.
448-```
449-450-451-## Solutions
452-453-```{solution-start} ar1p_ex1
454-:class: dropdown
455-```
456-457-```{code-cell} python3
458-from numba import njit
459-from scipy.special import factorial2
460-461-@njit
462-def sample_moments_ar1(k, m=100_000, mu_0=0.0, sigma_0=1.0, seed=1234):
463- np.random.seed(seed)
464- sample_sum = 0.0
465- x = mu_0 + sigma_0 * np.random.randn()
466- for t in range(m):
467- sample_sum += (x - mu_star)**k
468- x = a * x + b + c * np.random.randn()
469- return sample_sum / m
470-471-def true_moments_ar1(k):
472- if k % 2 == 0:
473- return std_star**k * factorial2(k - 1)
474- else:
475- return 0
476-477-k_vals = np.arange(6) + 1
478-sample_moments = np.empty_like(k_vals)
479-true_moments = np.empty_like(k_vals)
480-481-for k_idx, k in enumerate(k_vals):
482- sample_moments[k_idx] = sample_moments_ar1(k)
483- true_moments[k_idx] = true_moments_ar1(k)
484-485-fig, ax = plt.subplots()
486-ax.plot(k_vals, true_moments, label="true moments")
487-ax.plot(k_vals, sample_moments, label="sample moments")
488-ax.legend()
489-490-plt.show()
491-```
492-493-```{solution-end}
494-```
495-496-497454```{solution-start} ar1p_ex2
498455:class: dropdown
499456```
@@ -553,6 +510,48 @@ distribution is smooth but less so otherwise.
553510```
554511555512513+```{exercise}
514+:label: ar1p_ex3
515+516+In the lecture we discussed the following fact: for the $AR(1)$ process
517+518+$$
519+X_{t+1} = a X_t + b + c W_{t+1}
520+$$
521+522+with $\{ W_t \}$ iid and standard normal,
523+524+$$
525+\psi_t = N(\mu, s^2) \implies \psi_{t+1}
526+= N(a \mu + b, a^2 s^2 + c^2)
527+$$
528+529+Confirm this, at least approximately, by simulation. Let
530+531+- $a = 0.9$
532+- $b = 0.0$
533+- $c = 0.1$
534+- $\mu = -3$
535+- $s = 0.2$
536+537+First, plot $\psi_t$ and $\psi_{t+1}$ using the true
538+distributions described above.
539+540+Second, plot $\psi_{t+1}$ on the same figure (in a different
541+color) as follows:
542+543+1. Generate $n$ draws of $X_t$ from the $N(\mu, s^2)$
544+ distribution
545+1. Update them all using the rule
546+ $X_{t+1} = a X_t + b + c W_{t+1}$
547+1. Use the resulting sample of $X_{t+1}$ values to produce a
548+ density estimate via kernel density estimation.
549+550+Try this for $n=2000$ and confirm that the
551+simulation based estimate of $\psi_{t+1}$ does converge to the
552+theoretical distribution.
553+```
554+556555```{solution-start} ar1p_ex3
557556:class: dropdown
558557```