From 1a5e9180a73cd68334509afa8e646e2ee143c4c5 Mon Sep 17 00:00:00 2001 From: Ben Cho Date: Tue, 3 Oct 2023 10:58:36 -0600 Subject: [PATCH] fixed prompt_beta test --- R/run_RTL_examples.R | 4 +++- notebooks/scratchpad.ipynb | 42 +++++++++++++++++++------------------ pytest/data/promptBeta.json | 2 +- pytest/test_risktools.py | 8 ++++--- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/R/run_RTL_examples.R b/R/run_RTL_examples.R index af19aad..0f170c9 100644 --- a/R/run_RTL_examples.R +++ b/R/run_RTL_examples.R @@ -195,7 +195,9 @@ x <- x %>% dplyr::group_by(series) x <- RTL::returns(df = x, retType = "abs", period.return = 1, spread = TRUE) x <- RTL::rolladjust(x = x, commodityname = c("cmewti"), rolltype = c("Last.Trade")) -x <- x %>% dplyr::filter(!grepl("2020-04-20|2020-04-21", date)) +x <- x %>% + dplyr::filter(!grepl("2020-04-20|2020-04-21", date)) %>% + filter((date>='2010-01-04') & (date <= '2022-12-30')) ou <- promptBeta(x = x, period = "all", betatype = "all", output = "betas") write(jsonlite::toJSON(ou), "promptBeta.json") diff --git a/notebooks/scratchpad.ipynb b/notebooks/scratchpad.ipynb index 02b59b5..6605d1c 100644 --- a/notebooks/scratchpad.ipynb +++ b/notebooks/scratchpad.ipynb @@ -442,7 +442,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 77, "metadata": {}, "outputs": [ { @@ -480,27 +480,27 @@ " \n", " \n", " 1\n", - " 0.9508\n", - " 0.8923\n", - " 0.9765\n", + " 0.9665\n", + " 0.9518\n", + " 0.9727\n", " \n", " \n", " 2\n", - " 0.9223\n", - " 0.8599\n", - " 0.9585\n", + " 0.9300\n", + " 0.9012\n", + " 0.9436\n", " \n", " \n", " 3\n", - " 0.8971\n", - " 0.8334\n", - " 0.9399\n", + " 0.8943\n", + " 0.8512\n", + " 0.9137\n", " \n", " \n", " 4\n", - " 0.8746\n", - " 0.8110\n", - " 0.9230\n", + " 0.8612\n", + " 0.8057\n", + " 0.8858\n", " \n", " \n", "\n", @@ -509,13 +509,13 @@ "text/plain": [ " all bull bear\n", "0 1.0000 1.0000 1.0000\n", - "1 0.9508 0.8923 0.9765\n", - "2 0.9223 0.8599 0.9585\n", - "3 0.8971 0.8334 0.9399\n", - "4 0.8746 0.8110 0.9230" + "1 0.9665 0.9518 0.9727\n", + "2 0.9300 0.9012 0.9436\n", + "3 0.8943 0.8512 0.9137\n", + "4 0.8612 0.8057 0.8858" ] }, - "execution_count": 46, + "execution_count": 77, "metadata": {}, "output_type": "execute_result" } @@ -526,10 +526,12 @@ "dfwide = rt.data.open_data(\"dflong\").unstack(0)\n", "col_mask = dfwide.columns[dfwide.columns.str.contains(\"CL\")]\n", "dfwide = dfwide[col_mask]\n", - "dfwide = dfwide[~dfwide.index.isin([\"2020-04-20\", \"2020-04-21\"])]\n", - "dfwide = dfwide.loc['2004-01-02':'2022-12-15',:]\n", + "dfwide = dfwide.dropna(thresh=10, axis=0)\n", + "\n", "x = rt.returns(df=dfwide, ret_type=\"abs\", period_return=1)\n", "x = rt.roll_adjust(df=x, commodity_name=\"cmewti\", roll_type=\"Last_Trade\")\n", + "x = x[~x.index.isin([\"2020-04-20\", \"2020-04-21\"])]\n", + "x = x.loc['2010-01-04':'2022-12-30',:]\n", "\n", "ts = (\n", " rt.prompt_beta(df=x, period=\"all\", beta_type=\"all\", output=\"betas\")\n", diff --git a/pytest/data/promptBeta.json b/pytest/data/promptBeta.json index 038920a..149d199 100644 --- a/pytest/data/promptBeta.json +++ b/pytest/data/promptBeta.json @@ -1 +1 @@ -[{"all":1,"bull":1,"bear":1,"contract":1},{"all":0.9506,"bull":0.8923,"bear":0.9778,"contract":2},{"all":0.921,"bull":0.8597,"bear":0.9574,"contract":3},{"all":0.8953,"bull":0.8327,"bear":0.9387,"contract":4},{"all":0.8724,"bull":0.8099,"bear":0.9213,"contract":5},{"all":0.8521,"bull":0.7902,"bear":0.9057,"contract":6},{"all":0.8331,"bull":0.7718,"bear":0.8913,"contract":7},{"all":0.8154,"bull":0.755,"bear":0.8778,"contract":8},{"all":0.7984,"bull":0.7391,"bear":0.8646,"contract":9},{"all":0.7824,"bull":0.7242,"bear":0.8518,"contract":10},{"all":0.767,"bull":0.7101,"bear":0.8393,"contract":11},{"all":0.7527,"bull":0.6966,"bear":0.8276,"contract":12},{"all":0.7392,"bull":0.6841,"bear":0.8166,"contract":13},{"all":0.7265,"bull":0.6725,"bear":0.8058,"contract":14},{"all":0.7143,"bull":0.6613,"bear":0.7955,"contract":15},{"all":0.7024,"bull":0.6503,"bear":0.7853,"contract":16},{"all":0.6909,"bull":0.6399,"bear":0.7752,"contract":17},{"all":0.6798,"bull":0.6296,"bear":0.7652,"contract":18},{"all":0.6689,"bull":0.6198,"bear":0.7553,"contract":19},{"all":0.6583,"bull":0.6105,"bear":0.7451,"contract":20},{"all":0.6481,"bull":0.6018,"bear":0.7353,"contract":21},{"all":0.6383,"bull":0.5932,"bear":0.7257,"contract":22},{"all":0.6289,"bull":0.5849,"bear":0.7165,"contract":23},{"all":0.6199,"bull":0.577,"bear":0.7078,"contract":24},{"all":0.6112,"bull":0.5694,"bear":0.6994,"contract":25},{"all":0.6029,"bull":0.562,"bear":0.6916,"contract":26},{"all":0.5949,"bull":0.555,"bear":0.684,"contract":27},{"all":0.5872,"bull":0.5481,"bear":0.6767,"contract":28},{"all":0.5806,"bull":0.5417,"bear":0.6702,"contract":29},{"all":0.5735,"bull":0.5358,"bear":0.6673,"contract":30},{"all":0.5666,"bull":0.5322,"bear":0.6611,"contract":31},{"all":0.56,"bull":0.5268,"bear":0.6556,"contract":32},{"all":0.5544,"bull":0.5197,"bear":0.6494,"contract":33},{"all":0.5486,"bull":0.516,"bear":0.6438,"contract":34},{"all":0.5432,"bull":0.5118,"bear":0.6362,"contract":35},{"all":0.5374,"bull":0.5086,"bear":0.6328,"contract":36}] +[{"all":1,"bull":1,"bear":1,"contract":1},{"all":0.9665,"bull":0.9518,"bear":0.9727,"contract":2},{"all":0.93,"bull":0.9012,"bear":0.9436,"contract":3},{"all":0.8943,"bull":0.8512,"bear":0.9137,"contract":4},{"all":0.8612,"bull":0.8057,"bear":0.8858,"contract":5},{"all":0.831,"bull":0.7662,"bear":0.8594,"contract":6},{"all":0.8036,"bull":0.732,"bear":0.8355,"contract":7},{"all":0.779,"bull":0.703,"bear":0.8137,"contract":8},{"all":0.756,"bull":0.6773,"bear":0.7934,"contract":9},{"all":0.7349,"bull":0.6551,"bear":0.7743,"contract":10},{"all":0.7149,"bull":0.6344,"bear":0.7562,"contract":11},{"all":0.6966,"bull":0.6156,"bear":0.7393,"contract":12},{"all":0.6794,"bull":0.5983,"bear":0.7237,"contract":13},{"all":0.6634,"bull":0.5826,"bear":0.709,"contract":14},{"all":0.6479,"bull":0.5674,"bear":0.6946,"contract":15},{"all":0.6328,"bull":0.5528,"bear":0.6801,"contract":16},{"all":0.6181,"bull":0.539,"bear":0.6655,"contract":17},{"all":0.6038,"bull":0.5253,"bear":0.6513,"contract":18},{"all":0.59,"bull":0.5121,"bear":0.6375,"contract":19},{"all":0.5765,"bull":0.4995,"bear":0.6236,"contract":20},{"all":0.5635,"bull":0.4872,"bear":0.6103,"contract":21},{"all":0.551,"bull":0.4755,"bear":0.5973,"contract":22},{"all":0.5388,"bull":0.4639,"bear":0.5845,"contract":23},{"all":0.5272,"bull":0.4527,"bear":0.5724,"contract":24},{"all":0.5157,"bull":0.442,"bear":0.5604,"contract":25},{"all":0.5046,"bull":0.4311,"bear":0.549,"contract":26},{"all":0.4941,"bull":0.4214,"bear":0.5382,"contract":27},{"all":0.484,"bull":0.4115,"bear":0.5279,"contract":28},{"all":0.4743,"bull":0.4023,"bear":0.5184,"contract":29},{"all":0.4648,"bull":0.3934,"bear":0.5087,"contract":30},{"all":0.4558,"bull":0.3852,"bear":0.4991,"contract":31},{"all":0.4469,"bull":0.3772,"bear":0.4898,"contract":32},{"all":0.4386,"bull":0.37,"bear":0.4811,"contract":33},{"all":0.4306,"bull":0.3625,"bear":0.473,"contract":34},{"all":0.4235,"bull":0.3556,"bear":0.4671,"contract":35},{"all":0.4158,"bull":0.3484,"bear":0.4592,"contract":36}] diff --git a/pytest/test_risktools.py b/pytest/test_risktools.py index 4fce8e2..1f54bac 100644 --- a/pytest/test_risktools.py +++ b/pytest/test_risktools.py @@ -314,21 +314,23 @@ def test_garch(): def test_prompt_beta(): - ac = _load_json("./data/promptBeta.json").round(2).drop("contract", axis=1) + ac = _load_json("../pytest/data/promptBeta.json").round(4).drop("contract", axis=1) dfwide = rt.data.open_data("dflong").unstack(0) col_mask = dfwide.columns[dfwide.columns.str.contains("CL")] dfwide = dfwide[col_mask] - dfwide = dfwide[~dfwide.index.isin(["2020-04-20", "2020-04-21"])] + dfwide = dfwide.dropna(thresh=10, axis=0) + x = rt.returns(df=dfwide, ret_type="abs", period_return=1) x = rt.roll_adjust(df=x, commodity_name="cmewti", roll_type="Last_Trade") + x = x[~x.index.isin(["2020-04-20", "2020-04-21"])] + x = x.loc['2010-01-04':'2022-12-30',:] ts = ( rt.prompt_beta(df=x, period="all", beta_type="all", output="betas") .round(4) .reset_index(drop=True) ) - # for some reason the betas are slightly different using the Python sklearn # LinearRegression model. Make sure that the max of the three columns # are less than 0.03. Differences are on the order of 0.001 on any individual