Skip to content

Commit

Permalink
Spring cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
domokane committed Mar 12, 2024
1 parent ca9b526 commit 140b616
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 76 deletions.
2 changes: 1 addition & 1 deletion financepy/models/bachelier.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def value(self,
###############################################################################

def __repr__(self):
s = label_to_string("OBJECT TYPE", type(self)._name__)
s = label_to_string("OBJECT TYPE", type(self).__name__)
s += label_to_string("VOLATILITY", self.volatility)
return s

Expand Down
20 changes: 10 additions & 10 deletions financepy/models/bdt_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ def bermudan_swaption_tree_fast(t_exp, t_mat,

# Tree flows go all the way out to the swap maturity date
for i in range(0, num_cpns):
tcpn = cpn_times[i]
n = int(tcpn/_dt + 0.50)
t_cpn = cpn_times[i]
n = int(t_cpn/_dt + 0.50)
ttree = _tree_times[n]
df_flow = _uinterpolate(tcpn, _df_times, _df_values, INTERP_TYPE)
df_flow = _uinterpolate(t_cpn, _df_times, _df_values, INTERP_TYPE)
df_tree = _uinterpolate(ttree, _df_times, _df_values, INTERP_TYPE)
fixed_leg_flows[n] += cpn_flows[i] * 1.0 * df_flow / df_tree
float_leg_values[n] = strike_price # * df_flow / df_tree
Expand Down Expand Up @@ -281,15 +281,15 @@ def american_bond_option_tree_fast(t_exp, t_mat,
# Tree flows go all the way out to the bond maturity date
# Do not include first coupon as it is the previous coupon and is negative
for i in range(1, num_cpns):
tcpn = cpn_times[i]
t_cpn = cpn_times[i]

if tcpn < 0.0:
if t_cpn < 0.0:
print(cpn_times)
raise FinError("Coupon times must be positive.")

n = int(tcpn/_dt + 0.50)
n = int(t_cpn/_dt + 0.50)
ttree = _tree_times[n]
df_flow = _uinterpolate(tcpn, _df_times, _df_values, INTERP_TYPE)
df_flow = _uinterpolate(t_cpn, _df_times, _df_values, INTERP_TYPE)
df_tree = _uinterpolate(ttree, _df_times, _df_values, INTERP_TYPE)
tree_flows[n] += cpn_flows[i] * 1.0 * df_flow / df_tree

Expand Down Expand Up @@ -456,10 +456,10 @@ def callable_puttable_bond_tree_fast(cpn_times, cpn_flows,

num_cpns = len(cpn_times)
for i in range(0, num_cpns):
tcpn = cpn_times[i]
n = int(tcpn/_dt + 0.50)
t_cpn = cpn_times[i]
n = int(t_cpn/_dt + 0.50)
ttree = _tree_times[n]
df_flow = _uinterpolate(tcpn, _df_times, _df_values, INTERP_TYPE)
df_flow = _uinterpolate(t_cpn, _df_times, _df_values, INTERP_TYPE)
df_tree = _uinterpolate(ttree, _df_times, _df_values, INTERP_TYPE)
tree_flows[n] += cpn_flows[i] * 1.0 * df_flow / df_tree

Expand Down
26 changes: 13 additions & 13 deletions financepy/models/bk_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ def bermudan_swaption_tree_fast(t_exp, t_mat,

# swap fixed leg flows go all the way out to the swap maturity date
for i in range(0, num_cpns):
tcpn = cpn_times[i]
n = int(tcpn/_dt + 0.50)
t_cpn = cpn_times[i]
n = int(t_cpn/_dt + 0.50)
ttree = _tree_times[n]
df_flow = _uinterpolate(tcpn, _df_times, _df_values, interp)
df_flow = _uinterpolate(t_cpn, _df_times, _df_values, interp)
df_tree = _uinterpolate(ttree, _df_times, _df_values, interp)
fixed_leg_flows[n] += cpn_flows[i] * 1.0 * df_flow / df_tree
float_leg_values[n] = strike_price # * df_flow / df_tree
Expand All @@ -195,12 +195,12 @@ def bermudan_swaption_tree_fast(t_exp, t_mat,

fixed_pv = 0.0
for n in range(0, num_cpns):
tcpn = cpn_times[n]
df = _uinterpolate(tcpn, _df_times, _df_values, interp)
t_cpn = cpn_times[n]
df = _uinterpolate(t_cpn, _df_times, _df_values, interp)
flow = cpn_flows[n]
pv_flow = flow * df
fixed_pv += pv_flow
print("++", n, tcpn, df, flow, fixed_pv)
print("++", n, t_cpn, df, flow, fixed_pv)
fixed_pv += df
df_tree = _uinterpolate(t_exp, _df_times, _df_values, interp)
floatpv = df_tree
Expand Down Expand Up @@ -394,14 +394,14 @@ def american_bond_option_tree_fast(t_exp, t_mat,
# Tree flows go all the way out to the bond maturity date
# Do not include first coupon as it is the previous coupon and is negative
for i in range(1, num_cpns):
tcpn = cpn_times[i]
t_cpn = cpn_times[i]

if tcpn < 0.0:
if t_cpn < 0.0:
raise FinError("Coupon times must be positive.")

n = int(tcpn/_dt + 0.50)
n = int(t_cpn/_dt + 0.50)
ttree = _tree_times[n]
df_flow = _uinterpolate(tcpn, _df_times, _df_values, interp)
df_flow = _uinterpolate(t_cpn, _df_times, _df_values, interp)
df_tree = _uinterpolate(ttree, _df_times, _df_values, interp)
tree_flows[n] += cpn_flows[i] * 1.0 * df_flow / df_tree

Expand Down Expand Up @@ -615,10 +615,10 @@ def callable_puttable_bond_tree_fast(cpn_times, cpn_flows,

num_cpns = len(cpn_times)
for i in range(0, num_cpns):
tcpn = cpn_times[i]
n = int(tcpn/_dt + 0.50)
t_cpn = cpn_times[i]
n = int(t_cpn/_dt + 0.50)
ttree = _tree_times[n]
df_flow = _uinterpolate(tcpn, _df_times, _df_values, interp)
df_flow = _uinterpolate(t_cpn, _df_times, _df_values, interp)
df_tree = _uinterpolate(ttree, _df_times, _df_values, interp)
tree_flows[n] += cpn_flows[i] * 1.0 * df_flow / df_tree

Expand Down
2 changes: 1 addition & 1 deletion financepy/models/black.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def vega(self,
###############################################################################

def __repr__(self):
s = label_to_string("OBJECT TYPE", type(self)._name__)
s = label_to_string("OBJECT TYPE", type(self).__name__)
s += label_to_string("VOLATILITY", self.volatility)
s += label_to_string("IMPLEMENTATION", self.implementation_type)
s += label_to_string("NUMSTEPS", self.num_steps)
Expand Down
54 changes: 27 additions & 27 deletions financepy/models/hw_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ def american_bond_option_tree_fast(t_exp,
# Flows that fall on the expiry date included. The tree only goes out to
# the expiry date so coupons after this date do not go onto the tree.
for i in range(0, num_cpns):
tcpn = cpn_times[i]
if tcpn <= t_exp:
n = int(tcpn/dt + 0.50)
t_cpn = cpn_times[i]
if t_cpn <= t_exp:
n = int(t_cpn/dt + 0.50)
ttree = _tree_times[n]
df_flow = _uinterpolate(tcpn, _df_times, _df_values, INTERP)
df_flow = _uinterpolate(t_cpn, _df_times, _df_values, INTERP)
df_tree = _uinterpolate(ttree, _df_times, _df_values, INTERP)
tree_flows[n] += cpn_amounts[i] * 1.0 * df_flow / df_tree

Expand Down Expand Up @@ -435,10 +435,10 @@ def bermudan_swaption_tree_fast(t_exp, t_mat, strike_price, face_amount,

# Tree flows go all the way out to the bond maturity date
for i in range(0, num_cpns):
tcpn = cpn_times[i]
n = int(round(tcpn/_dt, 0))
t_cpn = cpn_times[i]
n = int(round(t_cpn/_dt, 0))
ttree = _tree_times[n]
df_flow = _uinterpolate(tcpn, _df_times, _df_values, INTERP)
df_flow = _uinterpolate(t_cpn, _df_times, _df_values, INTERP)
df_tree = _uinterpolate(ttree, _df_times, _df_values, INTERP)
fixed_leg_flows[n] += cpn_flows[i] * 1.0 * df_flow / df_tree
float_leg_values[n] = strike_price * df_flow / df_tree
Expand Down Expand Up @@ -634,10 +634,10 @@ def callable_puttable_bond_tree_fast(cpn_times, cpn_flows,

num_cpns = len(cpn_times)
for i in range(0, num_cpns):
tcpn = cpn_times[i]
n = int(round(tcpn/dt, 0))
t_cpn = cpn_times[i]
n = int(round(t_cpn/dt, 0))
ttree = _tree_times[n]
df_flow = _uinterpolate(tcpn, _df_times, _df_values, INTERP)
df_flow = _uinterpolate(t_cpn, _df_times, _df_values, INTERP)
df_tree = _uinterpolate(ttree, _df_times, _df_values, INTERP)
tree_flows[n] += cpn_flows[i] * 1.0 * df_flow / df_tree

Expand Down Expand Up @@ -811,20 +811,20 @@ def fwd_dirty_bond_price(r_t, *args):

for i in range(1, num_flows):

tcpn = cpn_times[i]
t_cpn = cpn_times[i]
cpn = cpn_amounts[i]

if tcpn > t_exp:
ptcpn = _uinterpolate(tcpn, df_times, df_values, INTERP)
zcb = p_fast(t_exp, tcpn, r_t, dt, pt_exp, ptdelta, ptcpn,
if t_cpn > t_exp:
pt_cpn = _uinterpolate(t_cpn, df_times, df_values, INTERP)
zcb = p_fast(t_exp, t_cpn, r_t, dt, pt_exp, ptdelta, pt_cpn,
self.sigma, self.a)
pv = pv + zcb * cpn
# print("TCPN", tcpn, "ZCB", zcb, "CPN", cpn, "PV", pv)
# print("TCPN", t_cpn, "ZCB", zcb, "CPN", cpn, "PV", pv)

if tcpn >= t_exp:
if t_cpn >= t_exp:
pv = pv + zcb

# print("TCPN", tcpn, "ZCB", zcb, "PRI", 1.0, "PV", pv)
# print("TCPN", t_cpn, "ZCB", zcb, "PRI", 1.0, "PV", pv)

accd = accrued_interpolator(t_exp, cpn_times, cpn_amounts)
# print("Accrued:", accd)
Expand Down Expand Up @@ -959,17 +959,17 @@ def european_bond_option_jamshidian(self,
# Adjust strike to handle
for i in range(0, num_cpns):

tcpn = cpn_times[i]
t_cpn = cpn_times[i]
cpn = cpn_amounts[i]

if tcpn >= t_exp: # coupons on the expiry date are included
if t_cpn >= t_exp: # coupons on the expiry date are included

ptcpn = _uinterpolate(tcpn, df_times, df_values, INTERP)
pt_cpn = _uinterpolate(t_cpn, df_times, df_values, INTERP)

strike = p_fast(t_exp, tcpn, rstar, dt, pt_exp, ptdelta,
ptcpn, self.sigma, self.a)
strike = p_fast(t_exp, t_cpn, rstar, dt, pt_exp, ptdelta,
pt_cpn, self.sigma, self.a)

v = self.option_on_zcb(t_exp, tcpn, strike, 1.0,
v = self.option_on_zcb(t_exp, t_cpn, strike, 1.0,
df_times, df_values)

call = v['call']
Expand Down Expand Up @@ -1021,15 +1021,15 @@ def european_bond_option_expiry_only(self,

for i in range(0, num_cpns):

tcpn = cpn_times[i]
t_cpn = cpn_times[i]
cpn = cpn_amounts[i]

if tcpn >= t_exp:
if t_cpn >= t_exp:

ptcpn = _uinterpolate(tcpn, self.df_times, self.dfs,
pt_cpn = _uinterpolate(t_cpn, self.df_times, self.dfs,
INTERP)

zcb = p_fast(t_exp, tcpn, r_t, dt, pt_exp, ptdelta, ptcpn,
zcb = p_fast(t_exp, t_cpn, r_t, dt, pt_exp, ptdelta, pt_cpn,
self.sigma, self.a)

pv += cpn * zcb
Expand Down
26 changes: 13 additions & 13 deletions financepy/products/bonds/bond_callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,24 @@ def __init__(self,
raise FinError("Call date after bond maturity date")

if len(call_dts) > 0:
dtprev = call_dts[0]
dt_prev = call_dts[0]
for dt in call_dts[1:]:
if dt <= dtprev:
if dt <= dt_prev:
raise FinError("Call dates not increasing")
else:
dtprev = dt
dt_prev = dt

for dt in put_dts:
if dt > self.maturity_dt:
raise FinError("Put date after bond maturity date")

if len(put_dts) > 0:
dtprev = put_dts[0]
dt_prev = put_dts[0]
for dt in put_dts[1:]:
if dt <= dtprev:
if dt <= dt_prev:
raise FinError("Put dates not increasing")
else:
dtprev = dt
dt_prev = dt

for px in call_prices:
if px < 0.0:
Expand Down Expand Up @@ -192,10 +192,10 @@ def value(self,
face_amount)
model.num_time_steps -= 1

v_bondwithoption = (v1['bondwithoption'] + v2['bondwithoption'])/2
v_bondpure = (v1['bondpure'] + v2['bondpure'])/2
v_bond_with_option = (v1['bondwithoption'] + v2['bondwithoption']) / 2
v_bond_pure = (v1['bondpure'] + v2['bondpure']) / 2

return {'bondwithoption': v_bondwithoption, 'bondpure': v_bondpure}
return {'bondwithoption': v_bond_with_option, 'bondpure': v_bond_pure}

elif isinstance(model, BKTree):

Expand All @@ -215,18 +215,18 @@ def value(self,
face_amount)
model.num_time_steps -= 1

v_bondwithoption = (v1['bondwithoption'] + v2['bondwithoption'])/2
v_bondpure = (v1['bondpure'] + v2['bondpure'])/2
v_bond_with_option = (v1['bondwithoption'] + v2['bondwithoption']) / 2
v_bond_pure = (v1['bondpure'] + v2['bondpure']) / 2

return {'bondwithoption': v_bondwithoption, 'bondpure': v_bondpure}
return {'bondwithoption': v_bond_with_option, 'bondpure': v_bond_pure}
else:
raise FinError("Unknown model type")

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

def __repr__(self):

s = label_to_string("OBJECT TYPE", type(self)._name__)
s = label_to_string("OBJECT TYPE", type(self).__name__)
s += label_to_string("ISSUE DATE", self.issue_dt)
s += label_to_string("MATURITY DATE", self.maturity_dt)
s += label_to_string("COUPON", self.cpn)
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/bonds/bond_convertible.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def accrued_interest(self,
previous coupon date and the settlement date. """

if settle_dt != self.settle_dt:
self.calculate_cpn_dts(settle_dt)
self._calculate_cpn_dts(settle_dt)

if len(self.cpn_dts) == 0:
raise FinError("Accrued interest - not enough flow dates.")
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/bonds/bond_frn.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def print_payments(self,
settle_dt: Date):
""" Print a list of the unadjusted coupon payment dates used in
analytic calculations for the bond. """
self.calculate_cpn_dts()
self._calculate_cpn_dts()
for dt in self.cpn_dts[1:-1]:
print(dt)

Expand Down
4 changes: 2 additions & 2 deletions financepy/products/bonds/bond_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def total_invoice_amount(self,
' The total invoice amount paid to take delivery of bond. '

if bond.accrued_int is None:
bond.calculate_cpn_dts(settle_dt)
bond._calculate_cpn_dts(settle_dt)

pip = self.principal_invoice_price(bond, futures_price)
accrued = bond.accrued_int * self.contract_size / 100.0
Expand Down Expand Up @@ -131,7 +131,7 @@ def delivery_gain_loss(self,
###############################################################################

def __repr__(self):
s = label_to_string("OBJECT TYPE", type(self).name__)
s = label_to_string("OBJECT TYPE", type(self).__name__)
s += label_to_string("TICKER NAME", self.ticker_name)
s += label_to_string("FIRST DELIVERY DATE", self.first_delivery_dt)
s += label_to_string("LAST DELIVERY DATE", self.last_delivery_dt)
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/bonds/bond_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def value(self,
###############################################################################

def __repr__(self):
s = label_to_string("OBJECT TYPE", type(self).name__)
s = label_to_string("OBJECT TYPE", type(self).__name__)
s += label_to_string("EXPIRY DATE", self.expiry_dt)
s += label_to_string("STRIKE", self.strike_price)
s += label_to_string("OPTION TYPE", self.option_type)
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/bonds/bond_zero.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def option_adjusted_spread(self,

if isinstance(clean_price, float) or isinstance(clean_price, np.float64):
clean_prices = np.array([clean_price])
elif isinstance(clean_price, list) or isinstanc(clean_price, np.ndarray):
elif isinstance(clean_price, list) or isinstance(clean_price, np.ndarray):
clean_prices = np.array(clean_price)
else:
raise FinError("Unknown type for clean_price "
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/credit/cds_basket.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def value_1f_gaussian_homo(self,
def __repr__(self):
""" print out details of the CDS contract and all of the calculated
cash flows """
s = label_to_string("OBJECT TYPE", type(self).name__)
s = label_to_string("OBJECT TYPE", type(self).__name__)
s += label_to_string("STEP-IN DATE", self.step_in_dt)
s += label_to_string("MATURITY", self.maturity_dt)
s += label_to_string("NOTIONAL", self.notional)
Expand Down
1 change: 1 addition & 0 deletions financepy/products/credit/cds_tranche.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# TODO: Add __repr__ method

from math import sqrt

import numpy as np

from ...models.gauss_copula_onefactor import tranch_surv_prob_gaussian
Expand Down
Loading

0 comments on commit 140b616

Please sign in to comment.