Skip to content

Commit

Permalink
added ir test
Browse files Browse the repository at this point in the history
  • Loading branch information
bbcho committed Oct 2, 2023
1 parent f38ca88 commit 5cfe31d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
12 changes: 11 additions & 1 deletion notebooks/scratchpad.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
1 change: 1 addition & 0 deletions pytest/archive/ir_df_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"yield":0.02354749,"maturity":0,"discountfactor":1,"discountfactor_plus":1,"discountfactor_minus":1,"_row":"...1"},{"yield":0.02354749,"maturity":0.00273973,"discountfactor":0.99993549,"discountfactor_plus":0.99990809,"discountfactor_minus":0.99996288,"_row":"FedsFunds0"},{"yield":0.0221,"maturity":0.08333333,"discountfactor":0.99816003,"discountfactor_plus":0.99732857,"discountfactor_minus":0.99899217,"_row":"FED/RIFLGFCM01_N_B"},{"yield":0.0258,"maturity":0.25,"discountfactor":0.99357076,"discountfactor_plus":0.99108993,"discountfactor_minus":0.99605779,"_row":"FED/RIFLGFCM03_N_B"},{"yield":0.031,"maturity":0.5,"discountfactor":0.98461951,"discountfactor_plus":0.9797087,"discountfactor_minus":0.98955493,"_row":"FED/RIFLGFCM06_N_B"},{"yield":0.032091,"maturity":1,"discountfactor":0.96841845,"discountfactor_plus":0.95878253,"discountfactor_minus":0.97815122,"_row":"SVENY01"},{"yield":0.032486,"maturity":2,"discountfactor":0.9370937,"discountfactor_plus":0.918538,"discountfactor_minus":0.95602425,"_row":"SVENY02"},{"yield":0.031748,"maturity":3,"discountfactor":0.90915107,"discountfactor_plus":0.8822816,"discountfactor_minus":0.93683885,"_row":"SVENY03"},{"yield":0.030702,"maturity":4,"discountfactor":0.88443346,"discountfactor_plus":0.84975433,"discountfactor_minus":0.92052787,"_row":"SVENY04"},{"yield":0.029739,"maturity":5,"discountfactor":0.86183193,"discountfactor_plus":0.81979989,"discountfactor_minus":0.906019,"_row":"SVENY05"},{"yield":0.029014,"maturity":6,"discountfactor":0.84022632,"discountfactor_plus":0.79129534,"discountfactor_minus":0.89218301,"_row":"SVENY06"},{"yield":0.028557,"maturity":7,"discountfactor":0.81881345,"discountfactor_plus":0.7634566,"discountfactor_minus":0.87818412,"_row":"SVENY07"},{"yield":0.028347,"maturity":8,"discountfactor":0.79709931,"discountfactor_plus":0.73581541,"discountfactor_minus":0.86348738,"_row":"SVENY08"},{"yield":0.028337,"maturity":9,"discountfactor":0.77489093,"discountfactor_plus":0.70819698,"discountfactor_minus":0.84786572,"_row":"SVENY09"},{"yield":0.028479,"maturity":10,"discountfactor":0.75217219,"discountfactor_plus":0.68059355,"discountfactor_minus":0.83127883,"_row":"SVENY10"},{"yield":0.028727,"maturity":11,"discountfactor":0.72906164,"discountfactor_plus":0.6531183,"discountfactor_minus":0.81383552,"_row":"SVENY11"},{"yield":0.029045,"maturity":12,"discountfactor":0.70571769,"discountfactor_plus":0.62591544,"discountfactor_minus":0.79569447,"_row":"SVENY12"},{"yield":0.029404,"maturity":13,"discountfactor":0.68232309,"discountfactor_plus":0.59914478,"discountfactor_minus":0.7770489,"_row":"SVENY13"},{"yield":0.029782,"maturity":14,"discountfactor":0.65905519,"discountfactor_plus":0.57295506,"discountfactor_minus":0.75809392,"_row":"SVENY14"},{"yield":0.030165,"maturity":15,"discountfactor":0.63605197,"discountfactor_plus":0.54745501,"discountfactor_minus":0.73898696,"_row":"SVENY15"},{"yield":0.030542,"maturity":16,"discountfactor":0.6134405,"discountfactor_plus":0.52273951,"discountfactor_minus":0.7198791,"_row":"SVENY16"},{"yield":0.030905,"maturity":17,"discountfactor":0.59132766,"discountfactor_plus":0.49888234,"discountfactor_minus":0.70090354,"_row":"SVENY17"},{"yield":0.031251,"maturity":18,"discountfactor":0.56977257,"discountfactor_plus":0.47591405,"discountfactor_minus":0.68214161,"_row":"SVENY18"},{"yield":0.031578,"maturity":19,"discountfactor":0.54882151,"discountfactor_plus":0.45385296,"discountfactor_minus":0.6636622,"_row":"SVENY19"},{"yield":0.031885,"maturity":20,"discountfactor":0.52850659,"discountfactor_plus":0.4327046,"discountfactor_minus":0.64551941,"_row":"SVENY20"},{"yield":0.032171,"maturity":21,"discountfactor":0.5088556,"discountfactor_plus":0.41247033,"discountfactor_minus":0.62776399,"_row":"SVENY21"},{"yield":0.032438,"maturity":22,"discountfactor":0.48985983,"discountfactor_plus":0.39312172,"discountfactor_minus":0.61040293,"_row":"SVENY22"},{"yield":0.032687,"maturity":23,"discountfactor":0.47151659,"discountfactor_plus":0.37463577,"discountfactor_minus":0.59345078,"_row":"SVENY23"},{"yield":0.032918,"maturity":24,"discountfactor":0.45383027,"discountfactor_plus":0.35699554,"discountfactor_minus":0.57693135,"_row":"SVENY24"},{"yield":0.033134,"maturity":25,"discountfactor":0.43676936,"discountfactor_plus":0.34015632,"discountfactor_minus":0.56082296,"_row":"SVENY25"},{"yield":0.033334,"maturity":26,"discountfactor":0.4203431,"discountfactor_plus":0.32410621,"discountfactor_minus":0.54515561,"_row":"SVENY26"},{"yield":0.033521,"maturity":27,"discountfactor":0.40451478,"discountfactor_plus":0.30879829,"discountfactor_minus":0.52989998,"_row":"SVENY27"},{"yield":0.033695,"maturity":28,"discountfactor":0.3892786,"discountfactor_plus":0.29421044,"discountfactor_minus":0.51506612,"_row":"SVENY28"},{"yield":0.033858,"maturity":29,"discountfactor":0.37460543,"discountfactor_plus":0.28030359,"discountfactor_minus":0.50063299,"_row":"SVENY29"},{"yield":0.03401,"maturity":30,"discountfactor":0.36048678,"discountfactor_plus":0.26705517,"discountfactor_minus":0.48660625,"_row":"SVENY30"}]
2 changes: 1 addition & 1 deletion pytest/data/ir_df_us.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"yield":0.02354749,"maturity":0,"discountfactor":1,"discountfactor_plus":1,"discountfactor_minus":1,"_row":"...1"},{"yield":0.02354749,"maturity":0.00273973,"discountfactor":0.99993549,"discountfactor_plus":0.99990809,"discountfactor_minus":0.99996288,"_row":"FedsFunds0"},{"yield":0.0221,"maturity":0.08333333,"discountfactor":0.99816003,"discountfactor_plus":0.99732857,"discountfactor_minus":0.99899217,"_row":"FED/RIFLGFCM01_N_B"},{"yield":0.0258,"maturity":0.25,"discountfactor":0.99357076,"discountfactor_plus":0.99108993,"discountfactor_minus":0.99605779,"_row":"FED/RIFLGFCM03_N_B"},{"yield":0.031,"maturity":0.5,"discountfactor":0.98461951,"discountfactor_plus":0.9797087,"discountfactor_minus":0.98955493,"_row":"FED/RIFLGFCM06_N_B"},{"yield":0.032091,"maturity":1,"discountfactor":0.96841845,"discountfactor_plus":0.95878253,"discountfactor_minus":0.97815122,"_row":"SVENY01"},{"yield":0.032486,"maturity":2,"discountfactor":0.9370937,"discountfactor_plus":0.918538,"discountfactor_minus":0.95602425,"_row":"SVENY02"},{"yield":0.031748,"maturity":3,"discountfactor":0.90915107,"discountfactor_plus":0.8822816,"discountfactor_minus":0.93683885,"_row":"SVENY03"},{"yield":0.030702,"maturity":4,"discountfactor":0.88443346,"discountfactor_plus":0.84975433,"discountfactor_minus":0.92052787,"_row":"SVENY04"},{"yield":0.029739,"maturity":5,"discountfactor":0.86183193,"discountfactor_plus":0.81979989,"discountfactor_minus":0.906019,"_row":"SVENY05"},{"yield":0.029014,"maturity":6,"discountfactor":0.84022632,"discountfactor_plus":0.79129534,"discountfactor_minus":0.89218301,"_row":"SVENY06"},{"yield":0.028557,"maturity":7,"discountfactor":0.81881345,"discountfactor_plus":0.7634566,"discountfactor_minus":0.87818412,"_row":"SVENY07"},{"yield":0.028347,"maturity":8,"discountfactor":0.79709931,"discountfactor_plus":0.73581541,"discountfactor_minus":0.86348738,"_row":"SVENY08"},{"yield":0.028337,"maturity":9,"discountfactor":0.77489093,"discountfactor_plus":0.70819698,"discountfactor_minus":0.84786572,"_row":"SVENY09"},{"yield":0.028479,"maturity":10,"discountfactor":0.75217219,"discountfactor_plus":0.68059355,"discountfactor_minus":0.83127883,"_row":"SVENY10"},{"yield":0.028727,"maturity":11,"discountfactor":0.72906164,"discountfactor_plus":0.6531183,"discountfactor_minus":0.81383552,"_row":"SVENY11"},{"yield":0.029045,"maturity":12,"discountfactor":0.70571769,"discountfactor_plus":0.62591544,"discountfactor_minus":0.79569447,"_row":"SVENY12"},{"yield":0.029404,"maturity":13,"discountfactor":0.68232309,"discountfactor_plus":0.59914478,"discountfactor_minus":0.7770489,"_row":"SVENY13"},{"yield":0.029782,"maturity":14,"discountfactor":0.65905519,"discountfactor_plus":0.57295506,"discountfactor_minus":0.75809392,"_row":"SVENY14"},{"yield":0.030165,"maturity":15,"discountfactor":0.63605197,"discountfactor_plus":0.54745501,"discountfactor_minus":0.73898696,"_row":"SVENY15"},{"yield":0.030542,"maturity":16,"discountfactor":0.6134405,"discountfactor_plus":0.52273951,"discountfactor_minus":0.7198791,"_row":"SVENY16"},{"yield":0.030905,"maturity":17,"discountfactor":0.59132766,"discountfactor_plus":0.49888234,"discountfactor_minus":0.70090354,"_row":"SVENY17"},{"yield":0.031251,"maturity":18,"discountfactor":0.56977257,"discountfactor_plus":0.47591405,"discountfactor_minus":0.68214161,"_row":"SVENY18"},{"yield":0.031578,"maturity":19,"discountfactor":0.54882151,"discountfactor_plus":0.45385296,"discountfactor_minus":0.6636622,"_row":"SVENY19"},{"yield":0.031885,"maturity":20,"discountfactor":0.52850659,"discountfactor_plus":0.4327046,"discountfactor_minus":0.64551941,"_row":"SVENY20"},{"yield":0.032171,"maturity":21,"discountfactor":0.5088556,"discountfactor_plus":0.41247033,"discountfactor_minus":0.62776399,"_row":"SVENY21"},{"yield":0.032438,"maturity":22,"discountfactor":0.48985983,"discountfactor_plus":0.39312172,"discountfactor_minus":0.61040293,"_row":"SVENY22"},{"yield":0.032687,"maturity":23,"discountfactor":0.47151659,"discountfactor_plus":0.37463577,"discountfactor_minus":0.59345078,"_row":"SVENY23"},{"yield":0.032918,"maturity":24,"discountfactor":0.45383027,"discountfactor_plus":0.35699554,"discountfactor_minus":0.57693135,"_row":"SVENY24"},{"yield":0.033134,"maturity":25,"discountfactor":0.43676936,"discountfactor_plus":0.34015632,"discountfactor_minus":0.56082296,"_row":"SVENY25"},{"yield":0.033334,"maturity":26,"discountfactor":0.4203431,"discountfactor_plus":0.32410621,"discountfactor_minus":0.54515561,"_row":"SVENY26"},{"yield":0.033521,"maturity":27,"discountfactor":0.40451478,"discountfactor_plus":0.30879829,"discountfactor_minus":0.52989998,"_row":"SVENY27"},{"yield":0.033695,"maturity":28,"discountfactor":0.3892786,"discountfactor_plus":0.29421044,"discountfactor_minus":0.51506612,"_row":"SVENY28"},{"yield":0.033858,"maturity":29,"discountfactor":0.37460543,"discountfactor_plus":0.28030359,"discountfactor_minus":0.50063299,"_row":"SVENY29"},{"yield":0.03401,"maturity":30,"discountfactor":0.36048678,"discountfactor_plus":0.26705517,"discountfactor_minus":0.48660625,"_row":"SVENY30"}]
[{"yield":0.00081102,"maturity":0,"discountfactor":1,"discountfactor_plus":1,"discountfactor_minus":1,"_row":"...1"},{"yield":0.00081102,"maturity":0.00273973,"discountfactor":0.99999778,"discountfactor_plus":0.99997038,"discountfactor_minus":1.00002518,"_row":"FedsFunds0"},{"yield":0.0003,"maturity":0.08333333,"discountfactor":0.999975,"discountfactor_plus":0.99914203,"discountfactor_minus":1.00080866,"_row":"FED/RIFLGFCM01_N_B"},{"yield":0.0005,"maturity":0.25,"discountfactor":0.99987501,"discountfactor_plus":0.99737844,"discountfactor_minus":1.00237782,"_row":"FED/RIFLGFCM03_N_B"},{"yield":0.0013,"maturity":0.5,"discountfactor":0.99935021,"discountfactor_plus":0.99436593,"discountfactor_minus":1.00435947,"_row":"FED/RIFLGFCM06_N_B"},{"yield":0.00355,"maturity":1,"discountfactor":0.99645629,"discountfactor_plus":0.98654139,"discountfactor_minus":1.00647085,"_row":"SVENY01"},{"yield":0.007026,"maturity":2,"discountfactor":0.98604627,"discountfactor_plus":0.96652124,"discountfactor_minus":1.00596572,"_row":"SVENY02"},{"yield":0.009338,"maturity":3,"discountfactor":0.97237475,"discountfactor_plus":0.94363674,"discountfactor_minus":1.00198797,"_row":"SVENY03"},{"yield":0.010906,"maturity":4,"discountfactor":0.95731384,"discountfactor_plus":0.91977703,"discountfactor_minus":0.99638256,"_row":"SVENY04"},{"yield":0.012005,"maturity":5,"discountfactor":0.94174099,"discountfactor_plus":0.89581174,"discountfactor_minus":0.99002508,"_row":"SVENY05"},{"yield":0.012812,"maturity":6,"discountfactor":0.92600838,"discountfactor_plus":0.87208185,"discountfactor_minus":0.98326954,"_row":"SVENY06"},{"yield":0.01344,"maturity":7,"discountfactor":0.91020994,"discountfactor_plus":0.84867413,"discountfactor_minus":0.97620761,"_row":"SVENY07"},{"yield":0.013962,"maturity":8,"discountfactor":0.89431609,"discountfactor_plus":0.8255578,"discountfactor_minus":0.96880105,"_row":"SVENY08"},{"yield":0.014419,"maturity":9,"discountfactor":0.87829654,"discountfactor_plus":0.8027026,"discountfactor_minus":0.96100949,"_row":"SVENY09"},{"yield":0.014837,"maturity":10,"discountfactor":0.86211207,"discountfactor_plus":0.78007126,"discountfactor_minus":0.95278119,"_row":"SVENY10"},{"yield":0.01523,"maturity":11,"discountfactor":0.84575124,"discountfactor_plus":0.75765283,"discountfactor_minus":0.94409357,"_row":"SVENY11"},{"yield":0.015605,"maturity":12,"discountfactor":0.82922811,"discountfactor_plus":0.73545936,"discountfactor_minus":0.93495208,"_row":"SVENY12"},{"yield":0.015966,"maturity":13,"discountfactor":0.81256611,"discountfactor_plus":0.71351059,"discountfactor_minus":0.92537335,"_row":"SVENY13"},{"yield":0.016314,"maturity":14,"discountfactor":0.79580906,"discountfactor_plus":0.69184316,"discountfactor_minus":0.91539831,"_row":"SVENY14"},{"yield":0.016649,"maturity":15,"discountfactor":0.77900719,"discountfactor_plus":0.6704977,"discountfactor_minus":0.90507723,"_row":"SVENY15"},{"yield":0.016971,"maturity":16,"discountfactor":0.76220784,"discountfactor_plus":0.64951068,"discountfactor_minus":0.89445919,"_row":"SVENY16"},{"yield":0.017279,"maturity":17,"discountfactor":0.74546805,"discountfactor_plus":0.62892517,"discountfactor_minus":0.8836069,"_row":"SVENY17"},{"yield":0.017572,"maturity":18,"discountfactor":0.72884368,"discountfactor_plus":0.60878142,"discountfactor_minus":0.87258431,"_row":"SVENY18"},{"yield":0.017851,"maturity":19,"discountfactor":0.71236205,"discountfactor_plus":0.5890943,"discountfactor_minus":0.86142352,"_row":"SVENY19"},{"yield":0.018115,"maturity":20,"discountfactor":0.69607351,"discountfactor_plus":0.56989679,"discountfactor_minus":0.85018611,"_row":"SVENY20"},{"yield":0.018365,"maturity":21,"discountfactor":0.67999829,"discountfactor_plus":0.5511959,"discountfactor_minus":0.83889897,"_row":"SVENY21"},{"yield":0.018601,"maturity":22,"discountfactor":0.66416677,"discountfactor_plus":0.53300632,"discountfactor_minus":0.82760276,"_row":"SVENY22"},{"yield":0.018823,"maturity":23,"discountfactor":0.64860654,"discountfactor_plus":0.51533969,"discountfactor_minus":0.8163362,"_row":"SVENY23"},{"yield":0.019032,"maturity":24,"discountfactor":0.63332725,"discountfactor_plus":0.49819286,"discountfactor_minus":0.80511673,"_row":"SVENY24"},{"yield":0.019229,"maturity":25,"discountfactor":0.61833494,"discountfactor_plus":0.48155973,"discountfactor_minus":0.79395777,"_row":"SVENY25"},{"yield":0.019414,"maturity":26,"discountfactor":0.60364802,"discountfactor_plus":0.46544376,"discountfactor_minus":0.78288928,"_row":"SVENY26"},{"yield":0.019589,"maturity":27,"discountfactor":0.58925102,"discountfactor_plus":0.44982215,"discountfactor_minus":0.77189789,"_row":"SVENY27"},{"yield":0.019752,"maturity":28,"discountfactor":0.57518934,"discountfactor_plus":0.43471875,"discountfactor_minus":0.76105017,"_row":"SVENY28"},{"yield":0.019907,"maturity":29,"discountfactor":0.56141045,"discountfactor_plus":0.42008299,"discountfactor_minus":0.75028436,"_row":"SVENY29"},{"yield":0.020052,"maturity":30,"discountfactor":0.54795616,"discountfactor_plus":0.40593591,"discountfactor_minus":0.73966345,"_row":"SVENY30"}]
54 changes: 26 additions & 28 deletions pytest/test_risktools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
pass

# Github Actions CI Env Vars
up = {"m*": {"user": "", "pass": ""}, "eia": "", "quandl": ""}
up = {"m*": {"user": "", "pass": ""}, "eia": "", "quandl": None}

up["eia"] = os.getenv("EIA", upf['eia'])
up["quandl"] = os.getenv("QUANDL", upf["quandl"])
Expand Down Expand Up @@ -108,33 +108,31 @@ def test_get_prices():


def test_ir_df_us():
pass
# df = _load_json("ir_df_us.json")
# df = df[
# [
# "yield",
# "maturity",
# "discountfactor",
# "discountfactor_plus",
# "discountfactor_minus",
# ]
# ]
# ir = rt.ir_df_us(quandl_key=up["quandl"], date=test_date)
# ir = ir[
# [
# "yield",
# "maturity",
# "discountfactor",
# "discountfactor_plus",
# "discountfactor_minus",
# ]
# ].reset_index(drop=True)

# print(df)

# assert df.round(4).equals(
# ir.round(4)
# ), "ir_df_us test failed, returned dataframe does not equal RTL results"

df = _load_json("./data/ir_df_us.json")
df = df[
[
"yield",
"maturity",
"discountfactor",
"discountfactor_plus",
"discountfactor_minus",
]
]
ir = rt.ir_df_us(quandl_key=up["quandl"], date='2021-12-18')
ir = ir[
[
"yield",
"maturity",
"discountfactor",
"discountfactor_plus",
"discountfactor_minus",
]
].reset_index(drop=True)

assert df.round(4).equals(
ir.round(4)
), "ir_df_us test failed, returned dataframe does not equal RTL results"


def test_bond():
Expand Down
4 changes: 2 additions & 2 deletions src/risktools/_main_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ def ir_df_us(quandl_key=None, ir_sens=0.01, date=None):
x.columns = ["maturity", "yield"]
x["index"] = x["maturity"]
x["yield"] /= 100
x["maturity"] = x.maturity.str.extract("(\d+)").astype("int")
x["maturity"] = x.maturity.str.extract("(\d+)").astype("float")

# change maturity numbers to year fraction for first four rows
x.iloc[1:4, x.columns.get_loc("maturity")] /= 12
x.iloc[1:4, x.columns.get_loc("maturity")] /= 12.0
x.iloc[0, x.columns.get_loc("maturity")] = 1 / 365
# x.maturity[1:4] /= 12
# x.maturity[0] = 1/365
Expand Down

0 comments on commit 5cfe31d

Please sign in to comment.