Skip to content

Commit

Permalink
Add notes, code, tests, and chapter on effective spread🍕 (#184)
Browse files Browse the repository at this point in the history
Adresses #7 and #10.
  • Loading branch information
KarelZe authored Feb 26, 2023
1 parent 57adf8e commit 12041e7
Show file tree
Hide file tree
Showing 33 changed files with 696 additions and 162 deletions.
2 changes: 1 addition & 1 deletion notebooks/4.0a-mb-classical-rules.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2312,7 +2312,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4 (tags/v3.9.4:1f2e308, Apr 6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)]"
"version": "3.9.4"
},
"vscode": {
"interpreter": {
Expand Down
File renamed without changes.
133 changes: 133 additions & 0 deletions notebooks/4.0e-mb-effective-spread.ipynb
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
}
58 changes: 31 additions & 27 deletions references/obsidian/.obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{
"id": "aeb1f3ebc9e06223",
"type": "tabs",
"dimension": 55.5595798623687,
"children": [
{
"id": "f35a250acbe1a2dc",
Expand All @@ -22,6 +23,7 @@
{
"id": "90bbdd3c2ee01632",
"type": "tabs",
"dimension": 44.44042013763129,
"children": [
{
"id": "dad26e0868f82374",
Expand All @@ -36,18 +38,6 @@
},
"pinned": true
}
},
{
"id": "00542bf949f61a9b",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "📖chapters/🚏Exploratory Data Analysis.md",
"mode": "source",
"source": false
}
}
}
]
}
Expand Down Expand Up @@ -107,6 +97,7 @@
"state": {
"type": "backlink",
"state": {
"file": "📖chapters/🪦graveyard of ideas.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -133,7 +124,9 @@
"type": "leaf",
"state": {
"type": "outline",
"state": {}
"state": {
"file": "📖chapters/🪦graveyard of ideas.md"
}
}
},
{
Expand Down Expand Up @@ -163,26 +156,37 @@
"markdown-importer:Open format converter": false
}
},
"active": "f35a250acbe1a2dc",
"active": "dad26e0868f82374",
"lastOpenFiles": [
"🖼️Media/effective-spead-options.png",
"📑notes/🍕Application study notes.md",
"📖chapters/🍕Application study.md",
"📥Inbox/@petersonEvaluationBiasesExecution2003.md",
"📥Inbox/@hagstromerBiasEffectiveBidask2021.md",
"📥Inbox/@Piwowar_2006.md",
"📥Inbox/@ellisAccuracyTradeClassification2000.md",
"📑notes/🍔Layer norm notes.md",
"📥Inbox/@leeMarketIntegrationPrice1993.md",
"📥Inbox/@petersenPostedEffectiveSpreads1994.md",
"📖chapters/🪦graveyard of ideas.md",
"📑notes/🔢Trade Initiator notes.md",
"📖chapters/🔢Trade initiator.md",
"📖chapters/🔢Quote rule.md",
"📑notes/🔢Depth rule notes.md",
"📥Inbox/@goettlerEquilibriumDynamicLimit2005.md",
"📥Inbox/@muravyevOptionsTradingCosts2020.md",
"📖chapters/🛌Token Embedding.md",
"🖼️Media/asymmetric-spread.png",
"🖼️Media/midpoint-spread.png",
"🖼️Media/eff-spread-finucane.png",
"📑notes/🛌 Token embeddings notes.md",
"📖chapters/💤Embeddings For Tabular Data.md",
"📖chapters/👨‍🍳Tain-Test-split.md",
"📑notes/👨‍🍳Train-Test-split notes.md",
"🖼️Media/viz-training-schemes.png",
"📖chapters/🚏Exploratory Data Analysis.md",
"📥Inbox/@lopezdepradoAdvancesFinancialMachine2018.md",
"📥Inbox/@nagelMachineLearningAsset2021.md",
"📖chapters/🧭Kernel SHAP.md",
"📥Inbox/@kaufmanLeakageDataMining2012.md",
"📖chapters/🍪Selection Of Supervised Approaches.md",
"📥Inbox/@raschkaModelEvaluationModel2020.md",
"📖chapters/🧭Evaluation metric.md",
"📖chapters/🪦graveyard of ideas.md",
"📖chapters/🤖FTTransformer.md",
"📑notes/🤖FTTransformer notes.md",
"📖chapters/🤖TabTransformer.md",
"📖chapters/🍔Layer Norm.md",
"📑notes/🤖TabTransformer notes.md",
"📑notes/🍔Layer norm notes.md",
"📖chapters/🧭Attention Map.md"
"📖chapters/🧭Kernel SHAP.md"
]
}
Loading

0 comments on commit 12041e7

Please sign in to comment.