Skip to content

Commit b97468b

Browse files
committed
Fixed bug in df_t
1 parent b349979 commit b97468b

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

financepy/market/curves/composite_discount_curve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, child_curves: List[DiscountCurve]):
4444

4545
###############################################################################
4646

47-
def _df(self, t: Union[float, np.ndarray]):
47+
def df_t(self, t: Union[float, np.ndarray]):
4848
"""
4949
Return discount factors given a single or vector of dates.
5050
ParentRate = Sum of children rates => Parent DF = product of children dfs

financepy/market/curves/discount_curve_pwf_onf.py

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@
2222

2323

2424
class DiscountCurvePWFONF(DiscountCurve):
25-
""" Curve with piece-wise flat instantaneous (ON) fwd rates. Curve is made up of a series of sections with each having
25+
"""Curve with piece-wise flat instantaneous (ON) fwd rates. Curve is made up of a series of sections with each having
2626
a flat instantaneous forward rate. The default compounding assumption is
27-
continuous. The class inherits methods from DiscountCurve. """
28-
29-
def __init__(self,
30-
value_dt: Date,
31-
knot_dates: list,
32-
onfwd_rates: Union[list, np.ndarray]):
33-
"""
27+
continuous. The class inherits methods from DiscountCurve."""
28+
29+
def __init__(
30+
self,
31+
value_dt: Date,
32+
knot_dates: list,
33+
onfwd_rates: Union[list, np.ndarray],
34+
):
35+
"""
3436
Creates a discount curve using a vector of times and ON fwd rates
3537
The fwd rate is right-continuous i.e. a given value in the input is applied to the left of that date until the previous date exclusive
3638
The last fwd rate is extrapolated into the future
@@ -52,25 +54,37 @@ def __init__(self,
5254
self._freq_type = FrequencyTypes.CONTINUOUS
5355
self._day_count_type = DayCountTypes.SIMPLE
5456

55-
dc_times = times_from_dates(self._knot_dates,
56-
self.value_dt,
57-
self._day_count_type)
57+
dc_times = times_from_dates(
58+
self._knot_dates, self.value_dt, self._day_count_type
59+
)
5860

5961
self._times = np.atleast_1d(dc_times)
6062

6163
# it is easier to deal in log(dfs), log(df[Ti]) = -\int_0^T_i f(u) du
62-
self._logdfs = - \
63-
np.cumsum(np.diff(self._times, prepend=0.0) * self._onfwd_rates)
64+
self._logdfs = -np.cumsum(
65+
np.diff(self._times, prepend=0.0) * self._onfwd_rates
66+
)
6467
self._logdfs_interp = interpolate.interp1d(
65-
np.concatenate(([0.0], self._times)), np.concatenate(([0.0], self._logdfs)), kind='linear', bounds_error=False, fill_value='extrapolate')
68+
np.concatenate(([0.0], self._times)),
69+
np.concatenate(([0.0], self._logdfs)),
70+
kind="linear",
71+
bounds_error=False,
72+
fill_value="extrapolate",
73+
)
6674

6775
if test_monotonicity(self._times) is False:
6876
raise FinError("Times are not sorted in increasing order")
6977

7078
###############################################################################
7179

7280
@classmethod
73-
def brick_wall_curve(cls, valuation_date: Date, start_date: Date, end_date: Date, level: float = 1.0*g_basis_point):
81+
def brick_wall_curve(
82+
cls,
83+
valuation_date: Date,
84+
start_date: Date,
85+
end_date: Date,
86+
level: float = 1.0 * g_basis_point,
87+
):
7488
"""Generate a discount curve of the shape f(t) = level*1_{startdate < t <= enddate} where f(.) is the instantaneous forward rate
7589
Mostly useful for applying bumps to other discount_curve's, see composite_discount_curve.py
7690
Args:
@@ -82,23 +96,24 @@ def brick_wall_curve(cls, valuation_date: Date, start_date: Date, end_date: Date
8296
Returns:
8397
DiscountCurve: discount curve of the required shape
8498
"""
85-
knot_dates = [start_date, end_date, end_date.add_tenor('1D')]
99+
knot_dates = [start_date, end_date, end_date.add_tenor("1D")]
86100
onfwd_rates = [0.0, level, 0.0]
87101
return cls(valuation_date, knot_dates, onfwd_rates)
88102

89103
###############################################################################
90104

91105
@classmethod
92-
def flat_curve(cls, valuation_date: Date, level: float = 1.0*g_basis_point):
93-
knot_dates = [valuation_date.add_tenor('1Y')]
106+
def flat_curve(
107+
cls, valuation_date: Date, level: float = 1.0 * g_basis_point
108+
):
109+
knot_dates = [valuation_date.add_tenor("1Y")]
94110
onfwd_rates = [level]
95111
return cls(valuation_date, knot_dates, onfwd_rates)
96112

97113
###############################################################################
98114

99-
def _zero_rate(self,
100-
times: Union[float, np.ndarray, list]):
101-
"""
115+
def _zero_rate(self, times: Union[float, np.ndarray, list]):
116+
"""
102117
Piecewise flat instantaneous (ON) fwd rate is the same as linear logDfs
103118
"""
104119

@@ -109,25 +124,23 @@ def _zero_rate(self,
109124

110125
times = np.maximum(times, g_small)
111126
ldfs = self._logdfs_interp(times)
112-
zero_rates = -ldfs/times
127+
zero_rates = -ldfs / times
113128
return zero_rates
114129

115130
###############################################################################
116131

117-
def _df(self, t: Union[float, np.ndarray]):
118-
""" Return discount factors given a single or vector of times in years. The
132+
def df_t(self, t: Union[float, np.ndarray]):
133+
"""Return discount factors given a single or vector of times in years. The
119134
discount factor depends on the rate and this in turn depends on its
120135
compounding frequency and it defaults to continuous compounding. It
121136
also depends on the day count convention. This was set in the
122-
construction of the curve to be ACT_ACT_ISDA. """
137+
construction of the curve to be ACT_ACT_ISDA."""
123138

124139
zero_rates = self._zero_rate(t)
125140

126-
df = self._zero_to_df(self.value_dt,
127-
zero_rates,
128-
t,
129-
self._freq_type,
130-
self._day_count_type)
141+
df = self._zero_to_df(
142+
self.value_dt, zero_rates, t, self._freq_type, self._day_count_type
143+
)
131144

132145
return df
133146

@@ -145,7 +158,8 @@ def __repr__(self):
145158
###############################################################################
146159

147160
def _print(self):
148-
""" Simple print function for backward compatibility. """
161+
"""Simple print function for backward compatibility."""
149162
print(self)
150163

164+
151165
###############################################################################

0 commit comments

Comments
 (0)