Skip to content

Commit

Permalink
fixed prompt_beta test
Browse files Browse the repository at this point in the history
  • Loading branch information
bbcho committed Oct 3, 2023
1 parent e60d7fc commit 1a5e918
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
4 changes: 3 additions & 1 deletion R/run_RTL_examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
42 changes: 22 additions & 20 deletions notebooks/scratchpad.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 77,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -480,27 +480,27 @@
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.9508</td>\n",
" <td>0.8923</td>\n",
" <td>0.9765</td>\n",
" <td>0.9665</td>\n",
" <td>0.9518</td>\n",
" <td>0.9727</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.9223</td>\n",
" <td>0.8599</td>\n",
" <td>0.9585</td>\n",
" <td>0.9300</td>\n",
" <td>0.9012</td>\n",
" <td>0.9436</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.8971</td>\n",
" <td>0.8334</td>\n",
" <td>0.9399</td>\n",
" <td>0.8943</td>\n",
" <td>0.8512</td>\n",
" <td>0.9137</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.8746</td>\n",
" <td>0.8110</td>\n",
" <td>0.9230</td>\n",
" <td>0.8612</td>\n",
" <td>0.8057</td>\n",
" <td>0.8858</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
Expand All @@ -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"
}
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pytest/data/promptBeta.json
Original file line number Diff line number Diff line change
@@ -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}]
8 changes: 5 additions & 3 deletions pytest/test_risktools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a5e918

Please sign in to comment.