Skip to content

Commit a022f37

Browse files
authored
Merge pull request #3 from gusamarante/feature/cross-section-fwds
Added a function that computes forward curves tom simplify comparisons of between the different curves in the cross section
2 parents a67281e + 2166c63 commit a022f37

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

example.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,21 @@
127127
plt.show()
128128

129129

130-
# TODO mkt implied vs RN implied VS realized (develop)
130+
# Term Premium VS Expected Returns
131+
fig = plt.figure(figsize=(5 * (16 / 9), 5))
132+
133+
fwd_curves = acm.fwd_curve("2024-07-05")
134+
fwd_curves = fwd_curves.reset_index(drop=True)
135+
fwd_curves.index = fwd_curves.index + 1
136+
137+
ax = plt.subplot2grid((1, 1), (0, 0))
138+
ax.plot(fwd_curves, label=fwd_curves.columns)
139+
ax.set_title(f"Forward Curves")
140+
ax.set_xlabel("Maturity in Months")
141+
ax.xaxis.grid(color="grey", linestyle="-", linewidth=0.5, alpha=0.5)
142+
ax.yaxis.grid(color="grey", linestyle="-", linewidth=0.5, alpha=0.5)
143+
ax.tick_params(rotation=90, axis="x")
144+
ax.legend(frameon=True, loc="best")
145+
146+
plt.tight_layout()
147+
plt.show()

pyacm/acm.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,44 @@ def __init__(self, curve, n_factors=5):
7272
self.er_loadings, self.er_hist_m, self.er_hist_d = self._expected_return()
7373
self.z_lambda, self.z_beta = self._inference()
7474

75+
def fwd_curve(self, date=None):
76+
"""
77+
Compute the forward curves for a given date.
78+
79+
Parameters
80+
----------
81+
date : date-like
82+
date in any format that can be interpreted by pandas.to_datetime()
83+
"""
84+
85+
if date is None:
86+
date = self.curve.index[-1]
87+
88+
date = pd.to_datetime(date)
89+
fwd_mkt = self._compute_fwd_curve(self.curve.loc[date])
90+
fwd_miy = self._compute_fwd_curve(self.miy.loc[date])
91+
fwd_rny = self._compute_fwd_curve(self.rny.loc[date])
92+
df = pd.concat(
93+
[
94+
fwd_mkt.rename("Observed"),
95+
fwd_miy.rename("Model Implied"),
96+
fwd_rny.rename("Risk-Neutral"),
97+
],
98+
axis=1,
99+
)
100+
return df
101+
102+
103+
@staticmethod
104+
def _compute_fwd_curve(curve):
105+
aux_curve = curve.reset_index(drop=True)
106+
aux_curve.index = aux_curve.index + 1
107+
factor = (1 + aux_curve) ** (aux_curve.index / 12)
108+
fwd_factor = factor / factor.shift(1).fillna(1)
109+
fwds = (fwd_factor ** 12) - 1
110+
fwds = pd.Series(fwds.values, index=curve.index)
111+
return fwds
112+
75113
def _get_excess_returns(self):
76114
ttm = np.arange(1, self.n + 1) / 12
77115
log_prices = - self.curve_monthly * ttm

0 commit comments

Comments
 (0)