Skip to content

Commit 1efa6a7

Browse files
committed
Bug fix
1 parent f7757a2 commit 1efa6a7

25 files changed

+90
-83
lines changed

financepy/market/curves/discount_curve.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,18 @@ def dfs(self):
9797
###########################################################################
9898
def _zero_to_df(
9999
self,
100+
value_dt: Date,
100101
rates: float | np.ndarray,
101102
times: float | np.ndarray,
102103
freq_type: FrequencyTypes,
104+
dc_type: DayCountTypes,
103105
):
104106
"""Convert a zero rate with a specified compounding frequency to a discount
105107
factor for a single maturity date or a list of dates."""
106108

109+
# This is not used in the code so we set it to None
110+
dc_type = None
111+
107112
if isinstance(times, float):
108113
times = np.array([times])
109114

@@ -131,8 +136,8 @@ def _zero_to_df(
131136

132137
def _df_to_zero(
133138
self,
134-
dfs: (float, np.ndarray),
135-
maturity_dts: (Date, list),
139+
dfs: float | np.ndarray,
140+
maturity_dts: Date | list,
136141
freq_type: FrequencyTypes,
137142
dc_type: DayCountTypes,
138143
):
@@ -310,7 +315,7 @@ def df(self, dt: (list, Date), day_count=DayCountTypes.ACT_ACT_ISDA):
310315

311316
###########################################################################
312317

313-
def _df(self, t: (float, np.ndarray)):
318+
def _df(self, t: float | np.ndarray):
314319
"""Hidden function to calculate a discount factor from a time or a
315320
vector of times. Discourage usage in favour of passing in dates."""
316321

financepy/market/curves/discount_curve_flat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DiscountCurveFlat(DiscountCurve):
3232

3333
def __init__(self,
3434
value_dt: Date,
35-
flat_rate: (float, np.ndarray),
35+
flat_rate: float | np.ndarray,
3636
freq_type: FrequencyTypes = FrequencyTypes.CONTINUOUS,
3737
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA):
3838
""" Create a discount curve which is flat. This is very useful for
@@ -79,7 +79,7 @@ def bump(self,
7979
###############################################################################
8080

8181
def df(self,
82-
dts: (Date, list)):
82+
dts: Date | list):
8383
""" Return discount factors given a single or vector of dts. The
8484
discount factor depends on the rate and this in turn depends on its
8585
compounding frequency, and it defaults to continuous compounding. It

financepy/market/curves/discount_curve_ns.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def zero_rate(self,
9797
###########################################################################
9898

9999
def _zero_rate(self,
100-
times: (float, np.ndarray)):
100+
times: float | np.ndarray):
101101
""" Zero rate for Nelson-Siegel curve parametrisation. This means that
102102
the t vector must use the curve day count."""
103103

@@ -113,7 +113,7 @@ def _zero_rate(self,
113113
###########################################################################
114114

115115
def df(self,
116-
dates: (Date, list)):
116+
dates: Date | list):
117117
""" Return discount factors given a single or vector of dates. The
118118
discount factor depends on the rate and this in turn depends on its
119119
compounding frequency and it defaults to continuous compounding. It

financepy/market/curves/discount_curve_nss.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def zero_rate(self,
107107
###########################################################################
108108

109109
def _zero_rate(self,
110-
times: (float, np.ndarray)):
110+
times: float | np.ndarray):
111111
""" Calculation of zero rates given a single time or a numpy vector of
112112
times. This function can return a single zero rate or a vector of zero
113113
rates. The compounding frequency must be provided. """
@@ -127,7 +127,7 @@ def _zero_rate(self,
127127
###########################################################################
128128

129129
def df(self,
130-
dates: (Date, list)):
130+
dates: Date | list):
131131
""" Return discount factors given a single or vector of dates. The
132132
discount factor depends on the rate and this in turn depends on its
133133
compounding frequency and it defaults to continuous compounding. It

financepy/market/curves/discount_curve_poly.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DiscountCurvePoly(DiscountCurve):
2727

2828
def __init__(self,
2929
value_dt: Date,
30-
coefficients: (list, np.ndarray),
30+
coefficients: list | np.ndarray,
3131
freq_type: FrequencyTypes = FrequencyTypes.CONTINUOUS,
3232
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA):
3333
""" Create zero rate curve parametrised using a cubic curve from
@@ -83,7 +83,7 @@ def zero_rate(self,
8383
###########################################################################
8484

8585
def _zero_rate(self,
86-
times: (float, np.ndarray)):
86+
times: float | np.ndarray):
8787
""" Calculate the zero rate to maturity date but with times as inputs.
8888
This function is used internally and should be discouraged for external
8989
use. The compounding frequency defaults to that specified in the

financepy/market/curves/discount_curve_pwf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DiscountCurvePWF(DiscountCurve):
2727
def __init__(self,
2828
value_dt: Date,
2929
zero_dts: list,
30-
zero_rates: (list, np.ndarray),
30+
zero_rates: list | np.ndarray,
3131
freq_type: FrequencyTypes = FrequencyTypes.CONTINUOUS,
3232
day_count_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA):
3333
""" Creates a discount curve using a vector of times and zero rates
@@ -121,7 +121,7 @@ def _fwd(self,
121121
###########################################################################
122122

123123
def df(self,
124-
dates: (Date, list)):
124+
dates: Date | list):
125125
""" Return discount factors given a single or vector of dates. The
126126
discount factor depends on the rate and this in turn depends on its
127127
compounding frequency and it defaults to continuous compounding. It

financepy/market/curves/discount_curve_pwl.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class DiscountCurvePWL(DiscountCurve):
2626

2727
def __init__(self,
2828
value_dt: Date,
29-
zero_dts: (Date, list),
30-
zero_rates: (list, np.ndarray),
29+
zero_dts: Date | list,
30+
zero_rates: list | np.ndarray,
3131
freq_type: FrequencyTypes = FrequencyTypes.CONTINUOUS,
3232
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA):
3333
""" Curve is defined by a vector of increasing times and zero rates."""
@@ -102,7 +102,7 @@ def _zero_rate(self,
102102
###########################################################################
103103

104104
def df(self,
105-
dates: (Date, list)):
105+
dates: Date | list):
106106
""" Return discount factors given a single or vector of dates. The
107107
discount factor depends on the rate and this in turn depends on its
108108
compounding frequency and it defaults to continuous compounding. It
@@ -127,7 +127,7 @@ def df(self,
127127
###########################################################################
128128

129129
# def _df(self,
130-
# t: (float, np.ndarray)):
130+
# t: float | np.ndarray):
131131
# """ Returns the discount factor at time t taking into account the
132132
# piecewise flat zero rate curve and the compunding frequency. """
133133

financepy/market/curves/discount_curve_zeros.py

+40-38
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,28 @@
2020
# TODO: Fix up __repr__ function
2121
###############################################################################
2222

23+
2324
class DiscountCurveZeros(DiscountCurve):
24-
""" This is a curve calculated from a set of dates and zero rates. As we
25+
"""This is a curve calculated from a set of dates and zero rates. As we
2526
have rates as inputs, we need to specify the corresponding compounding
2627
frequency. Also to go from rates and dates to discount factors we need to
2728
compute the year fraction correctly and for this we require a day count
2829
convention. Finally, we need to interpolate the zero rate for the times
2930
between the zero rates given and for this we must specify an interpolation
30-
convention. The class inherits methods from FinDiscountCurve. """
31-
32-
###############################################################################
33-
34-
def __init__(self,
35-
value_dt: Date,
36-
zero_dts: list,
37-
zero_rates: (list, np.ndarray),
38-
freq_type: FrequencyTypes = FrequencyTypes.ANNUAL,
39-
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA,
40-
interp_type: InterpTypes = InterpTypes.FLAT_FWD_RATES):
41-
""" Create the discount curve from a vector of dates and zero rates
31+
convention. The class inherits methods from FinDiscountCurve."""
32+
33+
###############################################################################
34+
35+
def __init__(
36+
self,
37+
value_dt: Date,
38+
zero_dts: list,
39+
zero_rates: list | np.ndarray,
40+
freq_type: FrequencyTypes = FrequencyTypes.ANNUAL,
41+
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA,
42+
interp_type: InterpTypes = InterpTypes.FLAT_FWD_RATES,
43+
):
44+
"""Create the discount curve from a vector of dates and zero rates
4245
factors. The first date is the curve anchor. Then a vector of zero
4346
dates and then another same-length vector of rates. The rate is to the
4447
corresponding date. We must specify the compounding frequency of the
@@ -59,8 +62,7 @@ def __init__(self,
5962
raise FinError("Unknown Frequency type " + str(freq_type))
6063

6164
if dc_type not in DayCountTypes:
62-
raise FinError("Unknown Cap Floor DayCountRule type " +
63-
str(dc_type))
65+
raise FinError("Unknown Cap Floor DayCountRule type " + str(dc_type))
6466

6567
self.value_dt = value_dt
6668
self.freq_type = freq_type
@@ -74,38 +76,36 @@ def __init__(self,
7476
if test_monotonicity(self._times) is False:
7577
raise FinError("Times or dates are not sorted in increasing order")
7678

77-
dfs = self._zero_to_df(self.value_dt,
78-
self._zero_rates,
79-
self._times,
80-
self.freq_type,
81-
self.dc_type)
79+
dfs = self._zero_to_df(
80+
self.value_dt, self._zero_rates, self._times, self.freq_type, self.dc_type
81+
)
8282

8383
self._dfs = np.array(dfs)
8484

8585
self._interp_type = interp_type
8686
self._interpolator = Interpolator(self._interp_type)
8787
self._interpolator.fit(self._times, self._dfs)
8888

89-
# ###############################################################################
89+
# ###############################################################################
9090

91-
# def bump(self, bump_size):
92-
# """ Calculate the continuous forward rate at the forward date. """
91+
# def bump(self, bump_size):
92+
# """ Calculate the continuous forward rate at the forward date. """
9393

94-
# times = self.times.copy()
95-
# discount_factors = self._discount_factors.copy()
94+
# times = self.times.copy()
95+
# discount_factors = self._discount_factors.copy()
9696

97-
# n = len(self.times)
98-
# for i in range(0, n):
99-
# t = times[i]
100-
# discount_factors[i] = discount_factors[i] * np.exp(-bump_size*t)
97+
# n = len(self.times)
98+
# for i in range(0, n):
99+
# t = times[i]
100+
# discount_factors[i] = discount_factors[i] * np.exp(-bump_size*t)
101101

102-
# disc_curve = FinDiscountCurve(self.value_dt, times,
103-
# discount_factors,
104-
# self._interp_type)
102+
# disc_curve = FinDiscountCurve(self.value_dt, times,
103+
# discount_factors,
104+
# self._interp_type)
105105

106-
# return disc_curve
106+
# return disc_curve
107107

108-
###############################################################################
108+
###############################################################################
109109

110110
def __repr__(self):
111111

@@ -118,15 +118,17 @@ def __repr__(self):
118118
s += label_to_string("DATES", "ZERO RATES")
119119
num_points = len(self._times)
120120
for i in range(0, num_points):
121-
s += label_to_string("%12s" % self._zero_dts[i],
122-
"%10.7f" % self._zero_rates[i])
121+
s += label_to_string(
122+
"%12s" % self._zero_dts[i], "%10.7f" % self._zero_rates[i]
123+
)
123124

124125
return s
125126

126-
###############################################################################
127+
###############################################################################
127128

128129
def _print(self):
129-
""" Simple print function for backward compatibility. """
130+
"""Simple print function for backward compatibility."""
130131
print(self)
131132

133+
132134
###############################################################################

financepy/market/curves/interpolator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class InterpTypes(Enum):
3434
# TODO: GET RID OF THIS FUNCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
3535
###############################################################################
3636

37-
def interpolate(t: (float, np.ndarray), # time or array of times
37+
def interpolate(t: float | np.ndarray, # time or array of times
3838
times: np.ndarray, # Vector of times on grid
3939
dfs: np.ndarray, # Vector of discount factors
4040
method: int): # Interpolation method which is value of enum

financepy/market/volatility/equity_vol_surface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ def __init__(
268268
discount_curve: DiscountCurve,
269269
dividend_curve: DiscountCurve,
270270
expiry_dts: list,
271-
strikes: (list, np.ndarray),
272-
volatility_grid: (list, np.ndarray),
271+
strikes: list | np.ndarray,
272+
volatility_grid: list | np.ndarray,
273273
vol_func_type=VolFuncTypes.CLARK,
274274
fin_solver_type=FinSolverTypes.NELDER_MEAD,
275275
):

financepy/market/volatility/fx_vol_surface.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,9 @@ def __init__(
540540
domestic_curve: DiscountCurve,
541541
foreign_curve: DiscountCurve,
542542
tenors: list,
543-
atm_vols: (list, np.ndarray),
544-
ms_25_delta_vols: (list, np.ndarray),
545-
rr_25_delta_vols: (list, np.ndarray),
543+
atm_vols: list | np.ndarray,
544+
ms_25_delta_vols: list | np.ndarray,
545+
rr_25_delta_vols: list | np.ndarray,
546546
atm_method: FinFXATMMethod = FinFXATMMethod.FWD_DELTA_NEUTRAL,
547547
delta_method: FinFXDeltaMethod = FinFXDeltaMethod.SPOT_DELTA,
548548
vol_func_type: VolFuncTypes = VolFuncTypes.CLARK,

financepy/market/volatility/fx_vol_surface_plus.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1219,11 +1219,11 @@ def __init__(
12191219
domestic_curve: DiscountCurve,
12201220
foreign_curve: DiscountCurve,
12211221
tenors: list,
1222-
atm_vols: (list, np.ndarray),
1223-
ms_25_delta_vols: (list, np.ndarray),
1224-
rr_25_delta_vols: (list, np.ndarray),
1225-
ms_10_delta_vols: (list, np.ndarray),
1226-
rr_10_delta_vols: (list, np.ndarray),
1222+
atm_vols: list | np.ndarray,
1223+
ms_25_delta_vols: list | np.ndarray,
1224+
rr_25_delta_vols: list | np.ndarray,
1225+
ms_10_delta_vols: list | np.ndarray,
1226+
rr_10_delta_vols: list | np.ndarray,
12271227
alpha: float,
12281228
atm_method: FinFXATMMethod = FinFXATMMethod.FWD_DELTA_NEUTRAL,
12291229
delta_method: FinFXDeltaMethod = FinFXDeltaMethod.SPOT_DELTA,

financepy/market/volatility/swaption_vol_surface.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def __init__(
387387
self,
388388
value_dt: Date,
389389
expiry_dts: list,
390-
fwd_swap_rates: (list, np.ndarray),
390+
fwd_swap_rates: list | np.ndarray,
391391
strike_grid: np.ndarray,
392392
vol_grid: np.ndarray,
393393
vol_func_type: VolFuncTypes = VolFuncTypes.SABR,

financepy/models/black_scholes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class BlackScholesTypes(Enum):
4242
class BlackScholes(Model):
4343

4444
def __init__(self,
45-
volatility: (float, np.ndarray),
45+
volatility: float | np.ndarray,
4646
bs_type: BlackScholesTypes = BlackScholesTypes.DEFAULT,
4747
num_steps_per_year=52,
4848
num_paths=10000,

financepy/products/equity/equity_barrier_option.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(
5757
def value(
5858
self,
5959
value_dt: Date,
60-
stock_price: (float, np.ndarray),
60+
stock_price: float | np.ndarray,
6161
discount_curve: DiscountCurve,
6262
dividend_curve: DiscountCurve,
6363
model,

financepy/products/equity/equity_digital_option.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def __init__(
6767
def value(
6868
self,
6969
value_dt: Date,
70-
s: (float, np.ndarray),
70+
s: float | np.ndarray,
7171
discount_curve: DiscountCurve,
7272
dividend_curve: DiscountCurve,
7373
model,

financepy/products/equity/equity_one_touch_option.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def __init__(
154154
def value(
155155
self,
156156
value_dt: Date,
157-
stock_price: (float, np.ndarray),
157+
stock_price: float | np.ndarray,
158158
discount_curve: DiscountCurve,
159159
dividend_curve: DiscountCurve,
160160
model,

0 commit comments

Comments
 (0)