-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add notes, code, tests, and chapter on effective spread🍕 (#184)
- Loading branch information
Showing
33 changed files
with
696 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import numpy.typing as npt\n", | ||
"from sklearn.metrics import make_scorer\n", | ||
"from sklearn.utils import check_consistent_length" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def effective_spread(\n", | ||
" y_pred: npt.NDArray, trade_price: npt.NDArray, fundamental_value: npt.NDArray\n", | ||
") -> np.float64:\n", | ||
" \"\"\"\n", | ||
" Calculate the effective spread given by:\n", | ||
" $$\n", | ||
" S_{i,t} = 2 (P_{i,t} - V_{i,t}) D_{i,t}\n", | ||
" $$\n", | ||
"\n", | ||
" Args:\n", | ||
" y_pred (npt.NDArray): indicator if the trade is a buy or sell\n", | ||
" trade_price (npt.NDArray): trade price\n", | ||
" fundamental_value (npt.NDArray): fundamental value e. g., bid-ask midpoint.\n", | ||
" Returns:\n", | ||
" float: average effective spread\n", | ||
" \"\"\"\n", | ||
" check_consistent_length(y_pred, trade_price, fundamental_value)\n", | ||
" return np.mean(2 * (trade_price - fundamental_value) * y_pred)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"y_pred = np.random.choice([-1,1], size=(10))\n", | ||
"trade_price = np.random.rand(10) * 100\n", | ||
"fundamental_value = np.random.rand(10) * 100\n", | ||
"\n", | ||
"eff_sp = effective_spread(y_pred, trade_price, fundamental_value)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([-83.57555436, -94.25680187, 92.10142797, 41.24263376,\n", | ||
" 168.57843754, 94.69759222, 39.67382461, 81.81819241,\n", | ||
" 135.25950003, -79.62206844])" | ||
] | ||
}, | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"2* (trade_price - fundamental_value) * y_pred" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"39.591718386351225" | ||
] | ||
}, | ||
"execution_count": 9, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"eff_sp" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"score = make_scorer(effective_spread, greater_is_better=True)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.4" | ||
}, | ||
"orig_nbformat": 4, | ||
"vscode": { | ||
"interpreter": { | ||
"hash": "f8ea8b642289b706932f10b33ee389827410dbaef0ce2c5bf73615e8d3267d88" | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.