Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
domokane committed Aug 23, 2023
1 parent d6ee039 commit 3b4fa30
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 47 deletions.
26 changes: 16 additions & 10 deletions tests/test_FinBond.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ def test_bondtutor_example():
issue_date = Date(15, 7, 1990)
maturity_date = Date(15, 7, 1997)
coupon = 0.085
face = ONE_MILLION
ex_div_days = 0
face = 1000000

freq_type = FrequencyTypes.SEMI_ANNUAL
bond = Bond(issue_date, maturity_date,
coupon, freq_type, accrualConvention, face)
coupon, freq_type, accrualConvention, ex_div_days)

dirty_price = bond.dirty_price_from_ytm(settlement_date, y)
assert round(dirty_price, 4) == 108.7696
clean_price = bond.clean_price_from_ytm(settlement_date, y)
assert round(clean_price, 4) == 106.5625
accrued_interest = bond._accrued_interest
accrued_interest = bond.accrued_interest(settlement_date, face)
assert round(accrued_interest, 4) == 22071.8232
ytm = bond.yield_to_maturity(settlement_date, clean_price)
assert round(ytm, 4) == 0.0622
Expand Down Expand Up @@ -72,13 +74,14 @@ def test_bloomberg_us_treasury_example():
freq_type = FrequencyTypes.SEMI_ANNUAL
accrual_type = DayCountTypes.ACT_ACT_ICMA
face = 100.0
ex_div_days = 0

bond = Bond(issue_date,
maturity_date,
coupon,
freq_type,
accrual_type,
face)
ex_div_days)

clean_price = 99.7808417

Expand All @@ -103,7 +106,7 @@ def test_bloomberg_us_treasury_example():
clean_price = bond.clean_price_from_ytm(settlement_date, ytm)
assert round(clean_price, 4) == 99.7825

accrued_interest = bond._accrued_interest
accrued_interest = bond.accrued_interest(settlement_date, face)
assert round(accrued_interest, 4) == 0.4324

accddays = bond._accrued_days
Expand All @@ -130,9 +133,10 @@ def test_bloomberg_apple_corp_example():
freq_type = FrequencyTypes.SEMI_ANNUAL
accrual_type = DayCountTypes.THIRTY_E_360_ISDA
face = 100.0
ex_div_days = 0

bond = Bond(issue_date, maturity_date,
coupon, freq_type, accrual_type, face)
coupon, freq_type, accrual_type, ex_div_days)

clean_price = 101.581564

Expand Down Expand Up @@ -160,7 +164,7 @@ def test_bloomberg_apple_corp_example():
accddays = bond._accrued_days
assert accddays == 68

accrued_interest = bond._accrued_interest
accrued_interest = bond.accrued_interest(settlement_date, face)
assert round(accrued_interest, 4) == 0.51

duration = bond.dollar_duration(settlement_date, ytm)
Expand Down Expand Up @@ -243,6 +247,7 @@ def test_bond_cfets():
Test ytms of bonds in CFETS convention, especially for those in last coupon period and
have 2 or more coupon payments per year.
"""
face = 100.0
test_case_file = 'test_cases_bond_cfets.csv'
df = pd.read_csv('./tests/data/' + test_case_file,
parse_dates=['settlement_date', 'issue_date', 'maturity_date'])
Expand All @@ -258,7 +263,7 @@ def test_bond_cfets():
)
settlement_date = Date(
row.settlement_date.day, row.settlement_date.month, row.settlement_date.year)
accrued_interest = bond.calc_accrued_interest(settlement_date)
accrued_interest = bond.accrued_interest(settlement_date, face)
clean_price = row.dirty_price - accrued_interest
calc_ytm = bond.yield_to_maturity(
settlement_date, clean_price, YTMCalcType.CFETS) * 100
Expand All @@ -283,12 +288,13 @@ def test_key_rate_durations_Bloomberg_example():
maturity_date = Date(31, 7, 2027)
coupon = 2.75/100.0
face = 100.0

ex_div_days = 0

accrual_type, freq_type, settlementDays, exDiv, calendar = get_bond_market_conventions(
BondMarkets.UNITED_STATES)

bond = Bond(issue_date, maturity_date, coupon,
freq_type, accrual_type, face)
freq_type, accrual_type, ex_div_days)

settlement_date = Date(24, 4, 2023)

Expand Down
31 changes: 13 additions & 18 deletions tests/test_FinBondAnnuity.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def test_SemiAnnual_BondAnnuity():
calendar_type,
bus_day_adjust_type,
date_gen_rule_type,
basis_type,
face)
basis_type)

annuity.calculate_payments(settlement_date)

Expand All @@ -45,7 +44,7 @@ def test_SemiAnnual_BondAnnuity():
assert annuity._flow_amounts[0] == 0.0
assert round(annuity._flow_amounts[-1]) == 25278.0

assert annuity.calc_accrued_interest(settlement_date) == 0.0
assert annuity.accrued_interest(settlement_date, face) == 0.0


def test_Quarterly_BondAnnuity():
Expand All @@ -69,8 +68,7 @@ def test_Quarterly_BondAnnuity():
calendar_type,
bus_day_adjust_type,
date_gen_rule_type,
basis_type,
face)
basis_type)

annuity.calculate_payments(settlement_date)

Expand All @@ -83,7 +81,7 @@ def test_Quarterly_BondAnnuity():
assert annuity._flow_amounts[0] == 0.0
assert round(annuity._flow_amounts[-1]) == 12778.0

assert annuity.calc_accrued_interest(settlement_date) == 0.0
assert annuity.accrued_interest(settlement_date, face) == 0.0


def test_Monthly_BondAnnuity():
Expand All @@ -106,10 +104,9 @@ def test_Monthly_BondAnnuity():
calendar_type,
bus_day_adjust_type,
date_gen_rule_type,
basis_type,
face)
basis_type)

annuity.calculate_payments(settlement_date)
annuity.calculate_payments(settlement_date, face)

assert len(annuity._flow_amounts) == 10*12 + 1
assert len(annuity._coupon_dates) == 10*12 + 1
Expand All @@ -120,7 +117,7 @@ def test_Monthly_BondAnnuity():
assert annuity._flow_amounts[0] == 0.0
assert round(annuity._flow_amounts[-1]) == 4028.0

assert annuity.calc_accrued_interest(settlement_date) == 0.0
assert annuity.accrued_interest(settlement_date, face) == 0.0


def test_ForwardGen_BondAnnuity():
Expand All @@ -142,10 +139,9 @@ def test_ForwardGen_BondAnnuity():
calendar_type,
bus_day_adjust_type,
date_gen_rule_type,
basis_type,
face)
basis_type)

annuity.calculate_payments(settlement_date)
annuity.calculate_payments(settlement_date, face)

assert len(annuity._flow_amounts) == 10 * 1 + 1
assert len(annuity._coupon_dates) == 10 * 1 + 1
Expand All @@ -156,7 +152,7 @@ def test_ForwardGen_BondAnnuity():
assert round(annuity._flow_amounts[0]) == 0.0
assert round(annuity._flow_amounts[-1]) == 50694.0

assert annuity.calc_accrued_interest(settlement_date) == 0.0
assert annuity.accrued_interest(settlement_date, face) == 0.0


def test_ForwardGenWithLongEndStub_BondAnnuity():
Expand All @@ -178,10 +174,9 @@ def test_ForwardGenWithLongEndStub_BondAnnuity():
calendar_type,
bus_day_adjust_type,
date_gen_rule_type,
basis_type,
face)
basis_type)

annuity.calculate_payments(settlement_date)
annuity.calculate_payments(settlement_date, face)

assert len(annuity._flow_amounts) == 10 * 2 + 1
assert len(annuity._coupon_dates) == 10 * 2 + 1
Expand All @@ -192,4 +187,4 @@ def test_ForwardGenWithLongEndStub_BondAnnuity():
assert round(annuity._flow_amounts[0]) == 0.0
assert round(annuity._flow_amounts[-1]) == 25417.0

assert annuity.calc_accrued_interest(settlement_date) == 0.0
assert annuity.accrued_interest(settlement_date, face) == 0.0
6 changes: 2 additions & 4 deletions tests/test_FinBondFRN.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def test_bond_frn_1():
maturity_date,
quoted_margin,
freq_type,
accrual_type,
face)
accrual_type)

clean_price = 96.793
resetIbor = 0.0143456 - quoted_margin
Expand Down Expand Up @@ -131,8 +130,7 @@ def test_bond_frn_2():
maturity_date,
quoted_margin,
freq_type,
accrual_type,
face)
accrual_type)

clean_price = 93.08
resetIbor = 0.00537 - quoted_margin
Expand Down
8 changes: 4 additions & 4 deletions tests/test_FinCDS.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def test_par_spread():

def test_value():
v = cds_contract1.value(valuation_date1, issuer_curve1, cdsRecovery)
assert round(v['dirty_pv'], 4) == 168552.827
assert round(v['dirty_pv'], 4) == 168514.596
assert round(v['clean_pv'], 4) == 170677.827

v = cds_contract2.value(valuation_date2, issuer_curve2, cdsRecovery)
Expand All @@ -414,7 +414,7 @@ def test_value():

def test_clean_price():
p = cds_contract1.clean_price(valuation_date1, issuer_curve1, cdsRecovery)
assert round(p, 4) == 82.9326
assert round(p, 4) == 82.936

p = cds_contract2.clean_price(valuation_date2, issuer_curve2, cdsRecovery)
assert round(p, 4) == 119.0221
Expand All @@ -439,7 +439,7 @@ def test_accrued_interest():
def test_protection_leg_pv():
prot_pv = cds_contract1.protection_leg_pv(
valuation_date1, issuer_curve1, cdsRecovery)
assert round(prot_pv, 4) == 273084.8417
assert round(prot_pv, 4) == 273023.5197

prot_pv = cds_contract2.protection_leg_pv(
valuation_date2, issuer_curve2, cdsRecovery)
Expand All @@ -449,7 +449,7 @@ def test_protection_leg_pv():
def test_premium_leg_pv():
premPV = cds_contract1.premium_leg_pv(
valuation_date1, issuer_curve1, cdsRecovery)
assert round(premPV, 4) == 104532.0147
assert round(premPV, 4) == 104508.9236

premPV = cds_contract2.premium_leg_pv(
valuation_date2, issuer_curve2, cdsRecovery)
Expand Down
8 changes: 5 additions & 3 deletions tests/test_FinCDSBasket.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_inhomogeneous_curve():
step_in_date,
basketMaturity,
issuer_curves) * 10000.0
assert round(intrinsicSpd, 4) == 32.0981
assert round(intrinsicSpd, 4) == 32.0971

totalSpd = cdsIndex.total_spread(valuation_date,
step_in_date,
Expand Down Expand Up @@ -79,7 +79,8 @@ def test_gaussian_copula():
issuer_curves,
beta_vector,
libor_curve)
assert round(v1[2] * 10000, 4) == 149.9644

assert round(v1[2] * 10000, 4) == 151.7163
assert round(v2[3] * 10000, 4) == 159.021

ntd = 2
Expand All @@ -101,6 +102,7 @@ def test_gaussian_copula():
issuer_curves,
beta_vector,
libor_curve)

assert round(v1[2] * 10000, 4) == 15.4566
assert round(v2[3] * 10000, 4) == 16.4308

Expand All @@ -123,7 +125,7 @@ def test_student_t():
num_trials,
seed)

assert round(v[2] * 10000, 4) == 132.0799
assert round(v[2] * 10000, 4) == 133.6284

ntd = 2
beta = 0.5
Expand Down
2 changes: 1 addition & 1 deletion tests/test_FinCDSCurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_FinCDSCurve():
assert round(issuer_curve._times[9], 4) == 9.0055
assert round(issuer_curve._values[0], 4) == 1.0
assert round(issuer_curve._values[5], 4) == 0.9249
assert round(issuer_curve._values[9], 4) == 0.8072
assert round(issuer_curve._values[9], 4) == 0.8071

i = 1
maturity_date = curve_date.add_months(12 * i)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_FinCDSIndex.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_cds_index():
assert round(spd, 4) == 48.3748

v = cdsIndexContract.value(valuation_date, issuer_curve, cdsRecovery)
assert round(v['dirty_pv'], 4) == 27019.7241
assert round(v['dirty_pv'], 4) == 27064.9888
assert round(v['clean_pv'], 4) == 32575.2797

p = cdsIndexContract.clean_price(valuation_date, issuer_curve, cdsRecovery)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_FinCDSIndexAdjustHazards.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_performCDSIndexHazardRateAdjustment():
maturity10Y,
issuer_curves) * 10000.0

assert round(averageSpd3Y, 4) == 19.8222
assert round(averageSpd3Y, 4) == 19.8221
assert round(averageSpd5Y, 4) == 36.0357
assert round(averageSpd7Y, 4) == 50.1336
assert round(averageSpd10Y, 4) == 63.6622
Expand Down
2 changes: 1 addition & 1 deletion tests/test_FinCDSIndexAdjustSpreads.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_CDSIndexAdjustSpreads():
maturity10Y,
issuer_curves) * 10000.0

assert round(averageSpd3Y, 4) == 19.8222
assert round(averageSpd3Y, 4) == 19.8221
assert round(averageSpd5Y, 4) == 36.0357
assert round(averageSpd7Y, 4) == 50.1336
assert round(averageSpd10Y, 4) == 63.6622
Expand Down
2 changes: 1 addition & 1 deletion tests/test_FinCDSIndexPortfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_CDSIndexPortfolio():
maturity10Y,
issuer_curves) * 10000.0

assert round(averageSpd3Y, 4) == 19.8222
assert round(averageSpd3Y, 4) == 19.8221
assert round(averageSpd5Y, 4) == 36.0357
assert round(averageSpd7Y, 4) == 50.1336
assert round(averageSpd10Y, 4) == 63.6622
Expand Down
2 changes: 1 addition & 1 deletion tests/test_FinCDSOption.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_cds_option():
volatility = 0.3

strike_result = [
(100, 3.9975),
(100, 4.0007),
(150, 1.5867),
(200, 0.0956),
(300, 0.0)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_FinCDSTranche.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@


def test_homogeneous():

num_credits = 125
spd3Y = 0.0012
spd5Y = 0.0025
Expand All @@ -61,7 +62,7 @@ def test_homogeneous():
trancheMaturity,
issuer_curves) * 10000.0

assert round(intrinsicSpd, 4) == 23.9775
assert round(intrinsicSpd, 4) == 23.9767

method = FinLossDistributionBuilder.RECURSION
v = tranche1.value_bc(
Expand Down Expand Up @@ -123,7 +124,7 @@ def test_heterogeneous():
trancheMaturity,
issuer_curves) * 10000.0

assert round(intrinsicSpd, 4) == 34.3337
assert round(intrinsicSpd, 4) == 34.3326

method = FinLossDistributionBuilder.RECURSION
v = tranche1.value_bc(
Expand Down

0 comments on commit 3b4fa30

Please sign in to comment.