{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Code starts here","metadata":{}},{"cell_type":"code","source":"!pip install shap","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T08:49:44.523077Z","iopub.execute_input":"2026-07-17T08:49:44.523374Z","iopub.status.idle":"2026-07-17T08:49:50.833190Z","shell.execute_reply.started":"2026-07-17T08:49:44.523346Z","shell.execute_reply":"2026-07-17T08:49:50.831796Z"}},"outputs":[{"name":"stdout","text":"Requirement already satisfied: shap in /usr/local/lib/python3.12/dist-packages (0.51.0)\nRequirement already satisfied: numpy>=2 in /usr/local/lib/python3.12/dist-packages (from shap) (2.0.2)\nRequirement already satisfied: scipy in /usr/local/lib/python3.12/dist-packages (from shap) (1.16.3)\nRequirement already satisfied: scikit-learn in /usr/local/lib/python3.12/dist-packages (from shap) (1.6.1)\nRequirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (from shap) (2.3.3)\nRequirement already satisfied: tqdm>=4.27.0 in /usr/local/lib/python3.12/dist-packages (from shap) (4.67.3)\nRequirement already satisfied: packaging>20.9 in /usr/local/lib/python3.12/dist-packages (from shap) (26.1)\nRequirement already satisfied: slicer==0.0.8 in /usr/local/lib/python3.12/dist-packages (from shap) (0.0.8)\nRequirement already satisfied: numba in /usr/local/lib/python3.12/dist-packages (from shap) (0.60.0)\nRequirement already satisfied: llvmlite in /usr/local/lib/python3.12/dist-packages (from shap) (0.43.0)\nRequirement already satisfied: cloudpickle in /usr/local/lib/python3.12/dist-packages (from shap) (3.1.2)\nRequirement already satisfied: typing-extensions in /usr/local/lib/python3.12/dist-packages (from shap) (4.15.0)\nRequirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.12/dist-packages (from pandas->shap) (2.9.0.post0)\nRequirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas->shap) (2025.2)\nRequirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas->shap) (2026.1)\nRequirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn->shap) (1.5.3)\nRequirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn->shap) (3.6.0)\nRequirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil>=2.8.2->pandas->shap) (1.17.0)\n","output_type":"stream"}],"execution_count":1},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T08:54:10.736633Z","iopub.execute_input":"2026-07-17T08:54:10.737029Z","iopub.status.idle":"2026-07-17T08:54:15.526190Z","shell.execute_reply.started":"2026-07-17T08:54:10.736979Z","shell.execute_reply":"2026-07-17T08:54:15.524991Z"}},"outputs":[],"execution_count":2},{"cell_type":"code","source":"class OptionPricingLSTM(nn.Module):\n def __init__(self,input_size,hidden_size=128, num_layers=2,dropout=0.3):\n super().__init__()\n self.lstm=nn.LSTM(input_size=input_size,\n hidden_size=hidden_size,\n num_layers=num_layers,\n batch_first=True,\n dropout=dropout if num_layers>1 else 0.0)\n\n self.head=nn.Sequential(nn.Dropout(dropout),\n nn.Linear(hidden_size,64),\n nn.ReLU(),\n nn.Dropout(0.2),\n nn.Linear(64,1))\n\n def forward(self,x):\n lstm_out,_=self.lstm(x)\n last_step = lstm_out[:, -1, :]\n return self.head(last_step).squeeze(-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T08:54:15.527867Z","iopub.execute_input":"2026-07-17T08:54:15.528352Z","iopub.status.idle":"2026-07-17T08:54:15.536983Z","shell.execute_reply.started":"2026-07-17T08:54:15.528320Z","shell.execute_reply":"2026-07-17T08:54:15.535665Z"}},"outputs":[],"execution_count":3},{"cell_type":"code","source":"FEATURES = [\n \"implied_volatility\", \"delta\", \"gamma\", \"theta\",\n \"vega\", \"rho\", \"tte_days\", \"log_moneyness\",\n \"strike\", \"type_enc\", \"volume\", \"open_interest\",\n \"regime\"\n]\nTARGET = \"mark\"\nSEQ_LEN = 6\n\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(f\"Using device: {device}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T08:54:19.441299Z","iopub.execute_input":"2026-07-17T08:54:19.441659Z","iopub.status.idle":"2026-07-17T08:54:19.448030Z","shell.execute_reply.started":"2026-07-17T08:54:19.441626Z","shell.execute_reply":"2026-07-17T08:54:19.447094Z"}},"outputs":[{"name":"stdout","text":"Using device: cpu\n","output_type":"stream"}],"execution_count":4},{"cell_type":"code","source":"model=torch.load('/kaggle/input/models/pranavahuja2005/regime-option-lstm/pytorch/default/1/model_with_regime.pt',map_location=device,weights_only=False)\nmodel.to(device)\nmodel.eval()\nmodel","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T08:54:21.346465Z","iopub.execute_input":"2026-07-17T08:54:21.346817Z","iopub.status.idle":"2026-07-17T08:54:21.399530Z","shell.execute_reply.started":"2026-07-17T08:54:21.346786Z","shell.execute_reply":"2026-07-17T08:54:21.398552Z"}},"outputs":[{"execution_count":5,"output_type":"execute_result","data":{"text/plain":"OptionPricingLSTM(\n (lstm): LSTM(13, 128, num_layers=2, batch_first=True, dropout=0.3)\n (head): Sequential(\n (0): Dropout(p=0.3, inplace=False)\n (1): Linear(in_features=128, out_features=64, bias=True)\n (2): ReLU()\n (3): Dropout(p=0.2, inplace=False)\n (4): Linear(in_features=64, out_features=1, bias=True)\n )\n)"},"metadata":{}}],"execution_count":5},{"cell_type":"code","source":"df_full = pd.read_csv(\"hf://datasets/major-year-project/dataset_with_regimes/datasets_with_regimes.csv\")\ndf_full['regime_name'] = ['expansion' if r == 0 else 'transition' if r == 1 else 'contraction' for r in df_full['regime']]\ndf_full['date'] = pd.to_datetime(df_full['date'])\ndf_full['expiration'] = pd.to_datetime(df_full['expiration'])\ndf_full['tte_days'] = (df_full['expiration'] - df_full['date']).dt.days\ndf_full[\"log_moneyness\"] = np.log(df_full[\"strike\"] / df_full[\"strike\"].median()).fillna(0)\ndf_full[\"type_enc\"] = (df_full[\"type\"].str.lower() == \"call\").astype(int)\ndf_full = df_full[df_full[\"mark\"] > 0].copy()\ndf_full = df_full.dropna(subset=[\"mark\", \"implied_volatility\", \"delta\", \"gamma\", \"theta\", \"vega\", \"rho\"])\ndf_full = df_full[FEATURES + [TARGET, \"date\", \"contractID\", \"regime_name\"]].dropna()\ndf_full = df_full.sort_values(\"date\").reset_index(drop=True)\nprint(f\"Full dataset shape: {df_full.shape}\")\nprint(df_full[\"regime_name\"].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T08:54:24.687314Z","iopub.execute_input":"2026-07-17T08:54:24.687707Z","iopub.status.idle":"2026-07-17T08:54:46.479663Z","shell.execute_reply.started":"2026-07-17T08:54:24.687677Z","shell.execute_reply":"2026-07-17T08:54:46.478639Z"}},"outputs":[{"name":"stdout","text":"Full dataset shape: (2292798, 17)\nregime_name\ncontraction 1014420\nexpansion 738808\ntransition 539570\nName: count, dtype: int64\n","output_type":"stream"}],"execution_count":6},{"cell_type":"code","source":"df_full.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-17T08:55:01.181528Z","iopub.execute_input":"2026-07-17T08:55:01.182187Z","iopub.status.idle":"2026-07-17T08:55:01.230554Z","shell.execute_reply.started":"2026-07-17T08:55:01.182138Z","shell.execute_reply":"2026-07-17T08:55:01.228844Z"}},"outputs":[{"execution_count":7,"output_type":"execute_result","data":{"text/plain":" implied_volatility delta gamma theta vega rho tte_days \\\n0 1.24983 0.99396 0.00055 -0.32269 0.00423 0.01093 0 \n1 0.16556 0.83374 0.00466 -0.08910 0.80702 1.60530 171 \n2 0.18370 -0.18316 0.00446 -0.03254 0.85820 -0.43508 171 \n3 0.16647 0.83756 0.00456 -0.08887 0.79497 1.60972 171 \n4 0.18461 -0.17939 0.00438 -0.03244 0.84708 -0.42610 171 \n\n log_moneyness strike type_enc volume open_interest regime mark \\\n0 -0.218156 402.0 1 130 1 0 70.77 \n1 -0.134675 437.0 1 0 1002 0 51.41 \n2 -0.136966 436.0 0 1 721 0 6.30 \n3 -0.136966 436.0 1 2 1140 0 52.27 \n4 -0.139262 435.0 0 40 26055 0 6.17 \n\n date contractID regime_name \n0 2024-01-02 SPY240102C00402000 expansion \n1 2024-01-02 SPY240621C00437000 expansion \n2 2024-01-02 SPY240621P00436000 expansion \n3 2024-01-02 SPY240621C00436000 expansion \n4 2024-01-02 SPY240621P00435000 expansion ","text/html":"
| \n | implied_volatility | \ndelta | \ngamma | \ntheta | \nvega | \nrho | \ntte_days | \nlog_moneyness | \nstrike | \ntype_enc | \nvolume | \nopen_interest | \nregime | \nmark | \ndate | \ncontractID | \nregime_name | \n
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n1.24983 | \n0.99396 | \n0.00055 | \n-0.32269 | \n0.00423 | \n0.01093 | \n0 | \n-0.218156 | \n402.0 | \n1 | \n130 | \n1 | \n0 | \n70.77 | \n2024-01-02 | \nSPY240102C00402000 | \nexpansion | \n
| 1 | \n0.16556 | \n0.83374 | \n0.00466 | \n-0.08910 | \n0.80702 | \n1.60530 | \n171 | \n-0.134675 | \n437.0 | \n1 | \n0 | \n1002 | \n0 | \n51.41 | \n2024-01-02 | \nSPY240621C00437000 | \nexpansion | \n
| 2 | \n0.18370 | \n-0.18316 | \n0.00446 | \n-0.03254 | \n0.85820 | \n-0.43508 | \n171 | \n-0.136966 | \n436.0 | \n0 | \n1 | \n721 | \n0 | \n6.30 | \n2024-01-02 | \nSPY240621P00436000 | \nexpansion | \n
| 3 | \n0.16647 | \n0.83756 | \n0.00456 | \n-0.08887 | \n0.79497 | \n1.60972 | \n171 | \n-0.136966 | \n436.0 | \n1 | \n2 | \n1140 | \n0 | \n52.27 | \n2024-01-02 | \nSPY240621C00436000 | \nexpansion | \n
| 4 | \n0.18461 | \n-0.17939 | \n0.00438 | \n-0.03244 | \n0.84708 | \n-0.42610 | \n171 | \n-0.139262 | \n435.0 | \n0 | \n40 | \n26055 | \n0 | \n6.17 | \n2024-01-02 | \nSPY240621P00435000 | \nexpansion | \n
StandardScaler()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
StandardScaler()