Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
domokane committed Oct 6, 2024
1 parent f7757a2 commit 1efa6a7
Show file tree
Hide file tree
Showing 25 changed files with 90 additions and 83 deletions.
11 changes: 8 additions & 3 deletions financepy/market/curves/discount_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ def dfs(self):
###########################################################################
def _zero_to_df(
self,
value_dt: Date,
rates: float | np.ndarray,
times: float | np.ndarray,
freq_type: FrequencyTypes,
dc_type: DayCountTypes,
):
"""Convert a zero rate with a specified compounding frequency to a discount
factor for a single maturity date or a list of dates."""

# This is not used in the code so we set it to None
dc_type = None

if isinstance(times, float):
times = np.array([times])

Expand Down Expand Up @@ -131,8 +136,8 @@ def _zero_to_df(

def _df_to_zero(
self,
dfs: (float, np.ndarray),
maturity_dts: (Date, list),
dfs: float | np.ndarray,
maturity_dts: Date | list,
freq_type: FrequencyTypes,
dc_type: DayCountTypes,
):
Expand Down Expand Up @@ -310,7 +315,7 @@ def df(self, dt: (list, Date), day_count=DayCountTypes.ACT_ACT_ISDA):

###########################################################################

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

Expand Down
4 changes: 2 additions & 2 deletions financepy/market/curves/discount_curve_flat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DiscountCurveFlat(DiscountCurve):

def __init__(self,
value_dt: Date,
flat_rate: (float, np.ndarray),
flat_rate: float | np.ndarray,
freq_type: FrequencyTypes = FrequencyTypes.CONTINUOUS,
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA):
""" Create a discount curve which is flat. This is very useful for
Expand Down Expand Up @@ -79,7 +79,7 @@ def bump(self,
###############################################################################

def df(self,
dts: (Date, list)):
dts: Date | list):
""" Return discount factors given a single or vector of dts. The
discount factor depends on the rate and this in turn depends on its
compounding frequency, and it defaults to continuous compounding. It
Expand Down
4 changes: 2 additions & 2 deletions financepy/market/curves/discount_curve_ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def zero_rate(self,
###########################################################################

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

Expand All @@ -113,7 +113,7 @@ def _zero_rate(self,
###########################################################################

def df(self,
dates: (Date, list)):
dates: Date | list):
""" Return discount factors given a single or vector of dates. The
discount factor depends on the rate and this in turn depends on its
compounding frequency and it defaults to continuous compounding. It
Expand Down
4 changes: 2 additions & 2 deletions financepy/market/curves/discount_curve_nss.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def zero_rate(self,
###########################################################################

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

def df(self,
dates: (Date, list)):
dates: Date | list):
""" Return discount factors given a single or vector of dates. The
discount factor depends on the rate and this in turn depends on its
compounding frequency and it defaults to continuous compounding. It
Expand Down
4 changes: 2 additions & 2 deletions financepy/market/curves/discount_curve_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DiscountCurvePoly(DiscountCurve):

def __init__(self,
value_dt: Date,
coefficients: (list, np.ndarray),
coefficients: list | np.ndarray,
freq_type: FrequencyTypes = FrequencyTypes.CONTINUOUS,
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA):
""" Create zero rate curve parametrised using a cubic curve from
Expand Down Expand Up @@ -83,7 +83,7 @@ def zero_rate(self,
###########################################################################

def _zero_rate(self,
times: (float, np.ndarray)):
times: float | np.ndarray):
""" Calculate the zero rate to maturity date but with times as inputs.
This function is used internally and should be discouraged for external
use. The compounding frequency defaults to that specified in the
Expand Down
4 changes: 2 additions & 2 deletions financepy/market/curves/discount_curve_pwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DiscountCurvePWF(DiscountCurve):
def __init__(self,
value_dt: Date,
zero_dts: list,
zero_rates: (list, np.ndarray),
zero_rates: list | np.ndarray,
freq_type: FrequencyTypes = FrequencyTypes.CONTINUOUS,
day_count_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA):
""" Creates a discount curve using a vector of times and zero rates
Expand Down Expand Up @@ -121,7 +121,7 @@ def _fwd(self,
###########################################################################

def df(self,
dates: (Date, list)):
dates: Date | list):
""" Return discount factors given a single or vector of dates. The
discount factor depends on the rate and this in turn depends on its
compounding frequency and it defaults to continuous compounding. It
Expand Down
8 changes: 4 additions & 4 deletions financepy/market/curves/discount_curve_pwl.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class DiscountCurvePWL(DiscountCurve):

def __init__(self,
value_dt: Date,
zero_dts: (Date, list),
zero_rates: (list, np.ndarray),
zero_dts: Date | list,
zero_rates: list | np.ndarray,
freq_type: FrequencyTypes = FrequencyTypes.CONTINUOUS,
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA):
""" Curve is defined by a vector of increasing times and zero rates."""
Expand Down Expand Up @@ -102,7 +102,7 @@ def _zero_rate(self,
###########################################################################

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

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

Expand Down
78 changes: 40 additions & 38 deletions financepy/market/curves/discount_curve_zeros.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,28 @@
# TODO: Fix up __repr__ function
###############################################################################


class DiscountCurveZeros(DiscountCurve):
""" This is a curve calculated from a set of dates and zero rates. As we
"""This is a curve calculated from a set of dates and zero rates. As we
have rates as inputs, we need to specify the corresponding compounding
frequency. Also to go from rates and dates to discount factors we need to
compute the year fraction correctly and for this we require a day count
convention. Finally, we need to interpolate the zero rate for the times
between the zero rates given and for this we must specify an interpolation
convention. The class inherits methods from FinDiscountCurve. """

###############################################################################

def __init__(self,
value_dt: Date,
zero_dts: list,
zero_rates: (list, np.ndarray),
freq_type: FrequencyTypes = FrequencyTypes.ANNUAL,
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA,
interp_type: InterpTypes = InterpTypes.FLAT_FWD_RATES):
""" Create the discount curve from a vector of dates and zero rates
convention. The class inherits methods from FinDiscountCurve."""

###############################################################################

def __init__(
self,
value_dt: Date,
zero_dts: list,
zero_rates: list | np.ndarray,
freq_type: FrequencyTypes = FrequencyTypes.ANNUAL,
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA,
interp_type: InterpTypes = InterpTypes.FLAT_FWD_RATES,
):
"""Create the discount curve from a vector of dates and zero rates
factors. The first date is the curve anchor. Then a vector of zero
dates and then another same-length vector of rates. The rate is to the
corresponding date. We must specify the compounding frequency of the
Expand All @@ -59,8 +62,7 @@ def __init__(self,
raise FinError("Unknown Frequency type " + str(freq_type))

if dc_type not in DayCountTypes:
raise FinError("Unknown Cap Floor DayCountRule type " +
str(dc_type))
raise FinError("Unknown Cap Floor DayCountRule type " + str(dc_type))

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

dfs = self._zero_to_df(self.value_dt,
self._zero_rates,
self._times,
self.freq_type,
self.dc_type)
dfs = self._zero_to_df(
self.value_dt, self._zero_rates, self._times, self.freq_type, self.dc_type
)

self._dfs = np.array(dfs)

self._interp_type = interp_type
self._interpolator = Interpolator(self._interp_type)
self._interpolator.fit(self._times, self._dfs)

# ###############################################################################
# ###############################################################################

# def bump(self, bump_size):
# """ Calculate the continuous forward rate at the forward date. """
# def bump(self, bump_size):
# """ Calculate the continuous forward rate at the forward date. """

# times = self.times.copy()
# discount_factors = self._discount_factors.copy()
# times = self.times.copy()
# discount_factors = self._discount_factors.copy()

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

# disc_curve = FinDiscountCurve(self.value_dt, times,
# discount_factors,
# self._interp_type)
# disc_curve = FinDiscountCurve(self.value_dt, times,
# discount_factors,
# self._interp_type)

# return disc_curve
# return disc_curve

###############################################################################
###############################################################################

def __repr__(self):

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

return s

###############################################################################
###############################################################################

def _print(self):
""" Simple print function for backward compatibility. """
"""Simple print function for backward compatibility."""
print(self)


###############################################################################
2 changes: 1 addition & 1 deletion financepy/market/curves/interpolator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class InterpTypes(Enum):
# TODO: GET RID OF THIS FUNCTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
###############################################################################

def interpolate(t: (float, np.ndarray), # time or array of times
def interpolate(t: float | np.ndarray, # time or array of times
times: np.ndarray, # Vector of times on grid
dfs: np.ndarray, # Vector of discount factors
method: int): # Interpolation method which is value of enum
Expand Down
4 changes: 2 additions & 2 deletions financepy/market/volatility/equity_vol_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ def __init__(
discount_curve: DiscountCurve,
dividend_curve: DiscountCurve,
expiry_dts: list,
strikes: (list, np.ndarray),
volatility_grid: (list, np.ndarray),
strikes: list | np.ndarray,
volatility_grid: list | np.ndarray,
vol_func_type=VolFuncTypes.CLARK,
fin_solver_type=FinSolverTypes.NELDER_MEAD,
):
Expand Down
6 changes: 3 additions & 3 deletions financepy/market/volatility/fx_vol_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,9 @@ def __init__(
domestic_curve: DiscountCurve,
foreign_curve: DiscountCurve,
tenors: list,
atm_vols: (list, np.ndarray),
ms_25_delta_vols: (list, np.ndarray),
rr_25_delta_vols: (list, np.ndarray),
atm_vols: list | np.ndarray,
ms_25_delta_vols: list | np.ndarray,
rr_25_delta_vols: list | np.ndarray,
atm_method: FinFXATMMethod = FinFXATMMethod.FWD_DELTA_NEUTRAL,
delta_method: FinFXDeltaMethod = FinFXDeltaMethod.SPOT_DELTA,
vol_func_type: VolFuncTypes = VolFuncTypes.CLARK,
Expand Down
10 changes: 5 additions & 5 deletions financepy/market/volatility/fx_vol_surface_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,11 +1219,11 @@ def __init__(
domestic_curve: DiscountCurve,
foreign_curve: DiscountCurve,
tenors: list,
atm_vols: (list, np.ndarray),
ms_25_delta_vols: (list, np.ndarray),
rr_25_delta_vols: (list, np.ndarray),
ms_10_delta_vols: (list, np.ndarray),
rr_10_delta_vols: (list, np.ndarray),
atm_vols: list | np.ndarray,
ms_25_delta_vols: list | np.ndarray,
rr_25_delta_vols: list | np.ndarray,
ms_10_delta_vols: list | np.ndarray,
rr_10_delta_vols: list | np.ndarray,
alpha: float,
atm_method: FinFXATMMethod = FinFXATMMethod.FWD_DELTA_NEUTRAL,
delta_method: FinFXDeltaMethod = FinFXDeltaMethod.SPOT_DELTA,
Expand Down
2 changes: 1 addition & 1 deletion financepy/market/volatility/swaption_vol_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def __init__(
self,
value_dt: Date,
expiry_dts: list,
fwd_swap_rates: (list, np.ndarray),
fwd_swap_rates: list | np.ndarray,
strike_grid: np.ndarray,
vol_grid: np.ndarray,
vol_func_type: VolFuncTypes = VolFuncTypes.SABR,
Expand Down
2 changes: 1 addition & 1 deletion financepy/models/black_scholes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BlackScholesTypes(Enum):
class BlackScholes(Model):

def __init__(self,
volatility: (float, np.ndarray),
volatility: float | np.ndarray,
bs_type: BlackScholesTypes = BlackScholesTypes.DEFAULT,
num_steps_per_year=52,
num_paths=10000,
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/equity/equity_barrier_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
def value(
self,
value_dt: Date,
stock_price: (float, np.ndarray),
stock_price: float | np.ndarray,
discount_curve: DiscountCurve,
dividend_curve: DiscountCurve,
model,
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/equity/equity_digital_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(
def value(
self,
value_dt: Date,
s: (float, np.ndarray),
s: float | np.ndarray,
discount_curve: DiscountCurve,
dividend_curve: DiscountCurve,
model,
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/equity/equity_one_touch_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def __init__(
def value(
self,
value_dt: Date,
stock_price: (float, np.ndarray),
stock_price: float | np.ndarray,
discount_curve: DiscountCurve,
dividend_curve: DiscountCurve,
model,
Expand Down
Loading

0 comments on commit 1efa6a7

Please sign in to comment.