Skip to content

Commit 7f1f7c4

Browse files
committed
fix again
1 parent 5524f3a commit 7f1f7c4

3 files changed

Lines changed: 42 additions & 37 deletions

File tree

lectures/hansen_singleton_1982.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ In addition to what comes with Anaconda, this lecture requires `pandas-datareade
5656
```{code-cell} ipython3
5757
:tags: [hide-output]
5858
59-
!pip install pandas-datareader
59+
!pip install pandas-datareader
6060
```
6161

6262
```{code-cell} ipython3
@@ -65,7 +65,7 @@ import warnings
6565
import matplotlib.pyplot as plt
6666
import numpy as np
6767
import pandas as pd
68-
from IPython.display import Math
68+
from IPython.display import Latex
6969
from numba import njit
7070
from pandas_datareader import data as web
7171
from scipy import stats
@@ -85,7 +85,7 @@ We also define a helper to display DataFrames as LaTeX arrays in the hidden cell
8585
8686
def display_table(df, title=None, fmt=None):
8787
"""
88-
Display a DataFrame as a LaTeX array using IPython Math.
88+
Display a DataFrame as a LaTeX array.
8989
"""
9090
if fmt is None:
9191
fmt = {}
@@ -94,22 +94,25 @@ def display_table(df, title=None, fmt=None):
9494
if col in fmt:
9595
formatted[col] = formatted[col].apply(
9696
lambda x: fmt[col].format(x) if np.isfinite(x) else str(x))
97+
n_cols = len(formatted.columns)
9798
idx_header = r"\text{" + df.index.name + "}" if df.index.name else ""
9899
columns = " & ".join(
99100
[idx_header] + [r"\text{" + c + "}" if "\\" not in c
101+
and "^" not in c and "_" not in c
100102
else c for c in formatted.columns])
101103
rows = r" \\".join(
102104
[" & ".join([str(idx)] + [str(v) for v in row])
103105
for idx, row in zip(formatted.index, formatted.values)])
104-
align = "r" + "c" * len(formatted.columns)
105-
latex = rf"""\begin{{array}}{{{align}}}
106-
{columns} \\
107-
\hline
108-
{rows}
109-
\end{{array}}"""
106+
col_format = "r" + "c" * n_cols
107+
lines = [r"\begin{array}{" + col_format + "}"]
108+
lines.append(columns + r" \\")
109+
lines.append(r"\hline")
110+
lines.append(rows)
111+
lines.append(r"\end{array}")
112+
latex = "\n".join(lines)
110113
if title:
111114
latex = rf"\textbf{{{title}}}" + r"\\" + "\n" + latex
112-
display(Math(latex))
115+
display(Latex("$" + latex + "$"))
113116
```
114117

115118

@@ -689,7 +692,7 @@ def run_gmm_by_lag(
689692
j_prob = float(stats.chi2.cdf(j_stat, df=j_df)) if j_df > 0 else np.nan
690693
rows.append(
691694
{
692-
"n_lags": lag,
695+
"NLAG": lag,
693696
"γ_hat": res.params[0],
694697
"se_γ": res.bse[0],
695698
"β_hat": res.params[1],
@@ -702,7 +705,7 @@ def run_gmm_by_lag(
702705
}
703706
)
704707
705-
table = pd.DataFrame(rows).set_index("n_lags")
708+
table = pd.DataFrame(rows).set_index("NLAG")
706709
return table, results
707710
708711
@@ -727,7 +730,7 @@ def run_two_step_by_lag(
727730
start_params = res["params_step2"]
728731
rows.append(
729732
{
730-
"n_lags": lag,
733+
"NLAG": lag,
731734
"γ_hat": res["params_step2"][0],
732735
"se_γ": res["se_step2"][0],
733736
"β_hat": res["params_step2"][1],
@@ -739,7 +742,7 @@ def run_two_step_by_lag(
739742
"n_obs": res["n_obs"],
740743
}
741744
)
742-
return pd.DataFrame(rows).set_index("n_lags")
745+
return pd.DataFrame(rows).set_index("NLAG")
743746
```
744747

745748

lectures/hansen_singleton_1983.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ from itertools import combinations
7979
import matplotlib.pyplot as plt
8080
import numpy as np
8181
import pandas as pd
82-
from IPython.display import Math
82+
from IPython.display import Latex
8383
from pandas_datareader import data as web
8484
from scipy import stats
8585
from scipy.linalg import LinAlgError, cholesky, solve_triangular
@@ -88,7 +88,7 @@ from statsmodels.stats.stattools import durbin_watson
8888
8989
warnings.filterwarnings(
9090
"ignore", message=".*date_parser.*", category=FutureWarning
91-
)
91+
)
9292
```
9393

9494
We also define a helper to display DataFrames as LaTeX arrays in the hidden cell below
@@ -98,7 +98,7 @@ We also define a helper to display DataFrames as LaTeX arrays in the hidden cell
9898
9999
def display_table(df, title=None, fmt=None):
100100
"""
101-
Display a DataFrame as a LaTeX array using IPython Math.
101+
Display a DataFrame as a LaTeX array.
102102
"""
103103
if fmt is None:
104104
fmt = {}
@@ -107,6 +107,7 @@ def display_table(df, title=None, fmt=None):
107107
if col in fmt:
108108
formatted[col] = formatted[col].apply(
109109
lambda x: fmt[col].format(x) if np.isfinite(x) else str(x))
110+
n_cols = len(formatted.columns)
110111
idx_header = r"\text{" + df.index.name + "}" if df.index.name else ""
111112
columns = " & ".join(
112113
[idx_header] + [r"\text{" + c + "}" if "\\" not in c
@@ -115,15 +116,16 @@ def display_table(df, title=None, fmt=None):
115116
rows = r" \\".join(
116117
[" & ".join([str(idx)] + [str(v) for v in row])
117118
for idx, row in zip(formatted.index, formatted.values)])
118-
align = "r" + "c" * len(formatted.columns)
119-
latex = rf"""\begin{{array}}{{{align}}}
120-
{columns} \\
121-
\hline
122-
{rows}
123-
\end{{array}}"""
119+
col_format = "r" + "c" * n_cols
120+
lines = [r"\begin{array}{" + col_format + "}"]
121+
lines.append(columns + r" \\")
122+
lines.append(r"\hline")
123+
lines.append(rows)
124+
lines.append(r"\end{array}")
125+
latex = "\n".join(lines)
124126
if title:
125127
latex = rf"\textbf{{{title}}}" + r"\\" + "\n" + latex
126-
display(Math(latex))
128+
display(Latex("$" + latex + "$"))
127129
```
128130

129131
## Euler equation
@@ -964,7 +966,7 @@ def run_mle_by_lag(
964966
965967
rows.append(
966968
{
967-
"n_lags": lag,
969+
"NLAG": lag,
968970
"α_hat": fit["params"][0],
969971
"se_α": fit["se"][0],
970972
"β_hat": fit["params"][1],
@@ -974,7 +976,7 @@ def run_mle_by_lag(
974976
}
975977
)
976978
977-
table = pd.DataFrame(rows).set_index("n_lags")
979+
table = pd.DataFrame(rows).set_index("NLAG")
978980
return table, fits
979981
```
980982

@@ -1165,13 +1167,13 @@ def run_unrestricted_var_by_lag(data, lags=(2, 4, 6)):
11651167
fits[lag] = fit
11661168
rows.append(
11671169
{
1168-
"n_lags": lag,
1170+
"NLAG": lag,
11691171
"loglike": fit["loglike"],
11701172
"n_obs": fit["n_obs"],
11711173
}
11721174
)
11731175
1174-
table = pd.DataFrame(rows).set_index("n_lags")
1176+
table = pd.DataFrame(rows).set_index("NLAG")
11751177
return table, fits
11761178
```
11771179

@@ -1191,7 +1193,7 @@ def restricted_vs_unrestricted_lr(mle_fits, unrestricted_fits, lags=(2, 4, 6)):
11911193
lr = likelihood_ratio_test(fit_restricted=fit_r, fit_unrestricted=fit_u, df_diff=df_diff)
11921194
rows.append(
11931195
{
1194-
"n_lags": lag,
1196+
"NLAG": lag,
11951197
"lr_stat": lr["lr_stat"],
11961198
"p_value": lr["p_value"],
11971199
"chi2_cdf": lr["chi2_cdf"],
@@ -1200,10 +1202,10 @@ def restricted_vs_unrestricted_lr(mle_fits, unrestricted_fits, lags=(2, 4, 6)):
12001202
}
12011203
)
12021204
1203-
return pd.DataFrame(rows).set_index("n_lags")
1205+
return pd.DataFrame(rows).set_index("NLAG")
12041206
```
12051207

1206-
## Predictability and the $R^2$ restriction
1208+
## Predictability and the R-squared restriction
12071209

12081210
Section II of {cite:t}`hansen1983stochastic` emphasizes an implication of the restricted system for return predictability.
12091211

@@ -1603,9 +1605,9 @@ for lag in lags:
16031605
unrestricted_fit=unres_fits[lag],
16041606
n_lags=lag,
16051607
)
1606-
pred_rows.append({"n_lags": lag, **metrics})
1608+
pred_rows.append({"NLAG": lag, **metrics})
16071609
1608-
pred_df = pd.DataFrame(pred_rows).set_index("n_lags")
1610+
pred_df = pd.DataFrame(pred_rows).set_index("NLAG")
16091611
pred_pretty = pred_df[
16101612
[
16111613
"alpha_hat",
@@ -1732,9 +1734,9 @@ for lag in lags:
17321734
unrestricted_fit=tbill_unres_fits[lag],
17331735
n_lags=lag,
17341736
)
1735-
tbill_pred_rows.append({"n_lags": lag, **metrics})
1737+
tbill_pred_rows.append({"NLAG": lag, **metrics})
17361738
1737-
tbill_pred_df = pd.DataFrame(tbill_pred_rows).set_index("n_lags")
1739+
tbill_pred_df = pd.DataFrame(tbill_pred_rows).set_index("NLAG")
17381740
tbill_pred_pretty = tbill_pred_df[
17391741
[
17401742
"alpha_hat",
@@ -1798,7 +1800,7 @@ Why does the LR test not reject the model for stocks?
17981800

17991801
As we hinted earlier, one reason may be limited test power when return predictability is small (as reflected in the low $R_R^2$ for stocks).
18001802

1801-
When aggregate stock returns are nearly unpredictable ($R_R^2 \approx 0.02$–$0.06$ in Table 1 of {cite:t}`hansen1983stochastic`), there is almost no predictable variation to constrain.
1803+
When aggregate stock returns are nearly unpredictable, there is almost no predictable variation to constrain.
18021804

18031805
## Residual diagnostics
18041806

lectures/measurement_models.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ kernelspec:
1111
name: python3
1212
---
1313

14-
(sargent_measurement_models)=
14+
(measurement_models)=
1515
```{raw} jupyter
1616
<div id="qe-notebook-header" align="right" style="text-align:right;">
1717
<a href="https://quantecon.org/" title="quantecon.org">

0 commit comments

Comments
 (0)