{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"},{"sourceId":9801075,"sourceType":"datasetVersion","datasetId":6006872},{"sourceId":9806342,"sourceType":"datasetVersion","datasetId":6010899},{"sourceId":9838282,"sourceType":"datasetVersion","datasetId":6035149},{"sourceId":10049657,"sourceType":"datasetVersion","datasetId":6191821},{"sourceId":10139918,"sourceType":"datasetVersion","datasetId":6258261},{"sourceId":10139922,"sourceType":"datasetVersion","datasetId":6258265},{"sourceId":10253875,"sourceType":"datasetVersion","datasetId":6297065},{"sourceId":10304887,"sourceType":"datasetVersion","datasetId":6378806},{"sourceId":10351700,"sourceType":"datasetVersion","datasetId":6410107},{"sourceId":203900450,"sourceType":"kernelVersion"},{"sourceId":215616115,"sourceType":"kernelVersion"},{"sourceId":216017958,"sourceType":"kernelVersion"},{"sourceId":216577393,"sourceType":"kernelVersion"}],"dockerImageVersionId":30787,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true},"papermill":{"default_parameters":{},"duration":80.344101,"end_time":"2024-10-26T03:27:42.952247","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-10-26T03:26:22.608146","version":"2.6.0"},"widgets":{"application/vnd.jupyter.widget-state+json":{"state":{"252dd2de87de42f4becd877fbbafc26b":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_ff55584ae0ab4f39ae471f314eaad988","placeholder":"​","style":"IPY_MODEL_b8d338c473aa4dc3ba25f37a997a9037","value":" 1/1 [00:00&lt;00:00, 33.79it/s]"}},"48a2731fb59b4ce8ace5b53d6f0e3337":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HTMLView","description":"","description_tooltip":null,"layout":"IPY_MODEL_a8dbc79c7c5a48318466a102c55801bb","placeholder":"​","style":"IPY_MODEL_ebd06aaaa7024a1684ba1b9fe89358cf","value":"100%"}},"56da652f3eeb42aca986e6fd815629dc":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"5bb4e0df01c44716afebab25aafe9f5f":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"FloatProgressModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"FloatProgressModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"ProgressView","bar_style":"success","description":"","description_tooltip":null,"layout":"IPY_MODEL_f4e41234b0cc4f3e9313d971f5aadc9f","max":1,"min":0,"orientation":"horizontal","style":"IPY_MODEL_6408698650f74dd699bcc914b850e396","value":1}},"6408698650f74dd699bcc914b850e396":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"ProgressStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"ProgressStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","bar_color":null,"description_width":""}},"9cb493ec04fc4ed391f5ac28cc84500e":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"HBoxModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"1.5.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_48a2731fb59b4ce8ace5b53d6f0e3337","IPY_MODEL_5bb4e0df01c44716afebab25aafe9f5f","IPY_MODEL_252dd2de87de42f4becd877fbbafc26b"],"layout":"IPY_MODEL_56da652f3eeb42aca986e6fd815629dc"}},"a8dbc79c7c5a48318466a102c55801bb":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"b8d338c473aa4dc3ba25f37a997a9037":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"ebd06aaaa7024a1684ba1b9fe89358cf":{"model_module":"@jupyter-widgets/controls","model_module_version":"1.5.0","model_name":"DescriptionStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"1.5.0","_model_name":"DescriptionStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"StyleView","description_width":""}},"f4e41234b0cc4f3e9313d971f5aadc9f":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"ff55584ae0ab4f39ae471f314eaad988":{"model_module":"@jupyter-widgets/base","model_module_version":"1.2.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"1.2.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"1.2.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"overflow_x":null,"overflow_y":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}}},"version_major":2,"version_minor":0}}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Additional\n\n- 2025/01/03 : add weights ensemble of public notebook\n    - https://www.kaggle.com/code/yunsuxiaozi/js-ridge-baseline?scriptVersionId=202739388\n    - https://www.kaggle.com/code/hideyukizushi/js-nnx5-xgbx5-weighted-blend-lb-0-0078/notebook?scriptVersionId=215419804\n    - https://www.kaggle.com/code/i2nfinit3y/jane-street-tabm-ft-transformer-inference?scriptVersionId=213715783","metadata":{}},{"cell_type":"markdown","source":"# <div style=\"color:white;display:fill;border-radius:5px;background-color:#3371ff;text-align:center;letter-spacing:0.1px;overflow:hidden;padding:20px;color:white;overflow:hidden;margin:0;font-size:100%\">Imports and installation</div>","metadata":{}},{"cell_type":"code","source":"!pip install rtdl_num_embeddings -q --no-index --find-links=/kaggle/input/jane-street-import/rtdl_num_embeddings","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:31.711785Z","iopub.execute_input":"2025-01-09T20:31:31.712145Z","iopub.status.idle":"2025-01-09T20:31:40.665300Z","shell.execute_reply.started":"2025-01-09T20:31:31.712115Z","shell.execute_reply":"2025-01-09T20:31:40.664008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os, sys, gc\nimport pickle\nimport dill\nimport numpy as np\nimport pandas as pd\nimport polars as pl\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom pytorch_lightning import (LightningDataModule, LightningModule, Trainer)\n\nfrom sklearn.metrics import r2_score\n\nimport torch.optim\nfrom torch.utils.data import Dataset, DataLoader, TensorDataset\nfrom sklearn.model_selection import train_test_split\nimport math\nfrom tqdm import tqdm\nfrom collections import OrderedDict\nfrom tabm_reference import Model, make_parameter_groups\n\nimport warnings\nimport joblib\nfrom pytorch_lightning.callbacks import Callback\nimport gc\n\nimport lightgbm as lgb\nfrom lightgbm import Booster\nfrom xgboost import XGBRegressor\nfrom catboost import CatBoostRegressor\n\nimport warnings\nwarnings.filterwarnings('ignore')\npd.options.display.max_columns = None\n\nsys.path.append(\"/kaggle/input/jane-street-real-time-market-data-forecasting\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:40.667927Z","iopub.execute_input":"2025-01-09T20:31:40.668863Z","iopub.status.idle":"2025-01-09T20:31:40.679067Z","shell.execute_reply.started":"2025-01-09T20:31:40.668812Z","shell.execute_reply":"2025-01-09T20:31:40.678123Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <div style=\"color:white;display:fill;border-radius:5px;background-color:#3371ff;text-align:center;letter-spacing:0.1px;overflow:hidden;padding:20px;color:white;overflow:hidden;margin:0;font-size:100%\">Top Public Notebook</div>","metadata":{}},{"cell_type":"markdown","source":"## JS|NNx5+XGBx5(MyTrain+Pub)|WeightBlend|LB.0.0078 (hideyukizushi)\n\n`LB: 0.0078` https://www.kaggle.com/code/yunsuxiaozi/js2024-starter?scriptVersionId=206770572","metadata":{"papermill":{"duration":0.022173,"end_time":"2024-10-26T03:27:39.285949","exception":false,"start_time":"2024-10-26T03:27:39.263776","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class CONFIG:\n    \"\"\"Configuration class for model parameters\"\"\"\n    seed = 42  # Random seed for reproducibility\n    target_col = \"responder_6\"  # Target variable name\n    # Features: 79 base features + 9 lagged features\n    feature_cols = [f\"feature_{idx:02d}\" for idx in range(79)] + [f\"responder_{idx}_lag_1\" for idx in range(9)]\n    # Paths to pre-trained models\n    model_paths = [\n        \"/kaggle/input/js-xs-nn-trained-model\",  # Neural Network models\n        \"/kaggle/input/js-with-lags-trained-xgb/result.pkl\", # XGBoost model\n        \"/kaggle/input/als-e-106-pp0-40-xgb-5fold/result0.pkl\",\n        \"/kaggle/input/als-e-106-pp0-40-xgb-5fold/result1.pkl\",\n        \"/kaggle/input/als-e-106-pp0-40-xgb-5fold/result2.pkl\",\n        \"/kaggle/input/als-e-106-pp0-40-xgb-5fold/result3.pkl\",\n        \"/kaggle/input/als-e-106-pp0-40-xgb-5fold/result4.pkl\"  \n    ]\n\n# Load validation data\nvalid = pl.scan_parquet(f\"/kaggle/input/js24-preprocessing-create-lags/validation.parquet/\").collect().to_pandas()\n\n# Load XGBoost model\nxgb_model = None\nwith open(CONFIG.model_paths[2], \"rb\") as fp:\n    result = pickle.load(fp)\n    xgb_model = result[\"model\"]\nxgb_feature_cols = [\"symbol_id\", \"time_id\"] + CONFIG.feature_cols\n\nxgb_model2 = None\nwith open(CONFIG.model_paths[4], \"rb\") as fp:\n    result = pickle.load(fp)\n    xgb_model2 = result[\"model\"]\n\nxgb_model4 = None\nwith open(CONFIG.model_paths[6], \"rb\") as fp:\n    result = pickle.load(fp)\n    xgb_model4 = result[\"model\"]\n\nxgb_model5 = None\nwith open(CONFIG.model_paths[1], \"rb\") as fp:\n    result = pickle.load(fp)\n    xgb_model5 = result[\"model\"]\n\ndef r2_val(y_true, y_pred, sample_weight):\n    \"\"\"\n    Calculate weighted R² score\n    Args:\n        y_true: True values\n        y_pred: Predicted values\n        sample_weight: Weights for each sample\n    Returns:\n        Weighted R² score\n    \"\"\"\n    r2 = 1 - np.average((y_pred - y_true) ** 2, weights=sample_weight) / (np.average((y_true) ** 2, weights=sample_weight) + 1e-38)\n    return r2\n\nclass NN(LightningModule):\n    \"\"\"Neural Network model using PyTorch Lightning\"\"\"\n    \n    def __init__(self, input_dim, hidden_dims, dropouts, lr, weight_decay):\n        \"\"\"\n        Initialize the neural network\n        Args:\n            input_dim: Input feature dimension\n            hidden_dims: List of hidden layer dimensions\n            dropouts: List of dropout rates\n            lr: Learning rate\n            weight_decay: Weight decay for regularization\n        \"\"\"\n        super().__init__()\n        self.save_hyperparameters()\n        \n        # Build network architecture\n        layers = []\n        in_dim = input_dim\n        for i, hidden_dim in enumerate(hidden_dims):\n            layers.append(nn.BatchNorm1d(in_dim))  # Batch normalization\n            if i > 0:\n                layers.append(nn.SiLU())  # SiLU activation (except first layer)\n            if i < len(dropouts):\n                layers.append(nn.Dropout(dropouts[i]))  # Dropout for regularization\n            layers.append(nn.Linear(in_dim, hidden_dim))  # Linear layer\n            in_dim = hidden_dim\n            \n        # Output layer\n        layers.append(nn.Linear(in_dim, 1))\n        layers.append(nn.Tanh())  # Tanh activation for bounded output\n        \n        self.model = nn.Sequential(*layers)\n        self.lr = lr\n        self.weight_decay = weight_decay\n        self.validation_step_outputs = []\n\n    def forward(self, x):\n        \"\"\"Forward pass with scaling\"\"\"\n        return 5 * self.model(x).squeeze(-1)  # Scale output to [-5, 5] range\n\n    def training_step(self, batch):\n        \"\"\"Single training step\"\"\"\n        x, y, w = batch\n        y_hat = self(x)\n        loss = F.mse_loss(y_hat, y, reduction='none') * w  # Weighted MSE loss\n        loss = loss.mean()\n        self.log('train_loss', loss, on_step=False, on_epoch=True, batch_size=x.size(0))\n        return loss\n\n    def validation_step(self, batch):\n        \"\"\"Single validation step\"\"\"\n        x, y, w = batch\n        y_hat = self(x)\n        loss = F.mse_loss(y_hat, y, reduction='none') * w\n        loss = loss.mean()\n        self.log('val_loss', loss, on_step=False, on_epoch=True, batch_size=x.size(0))\n        self.validation_step_outputs.append((y_hat, y, w))\n        return loss\n\n    def on_validation_epoch_end(self):\n        \"\"\"Compute validation metrics at epoch end\"\"\"\n        if not self.trainer.sanity_checking:\n            y = torch.cat([x[1] for x in self.validation_step_outputs]).cpu().numpy()\n            prob = torch.cat([x[0] for x in self.validation_step_outputs]).cpu().numpy()\n            weights = torch.cat([x[2] for x in self.validation_step_outputs]).cpu().numpy()\n            val_r_square = r2_val(y, prob, weights)\n            self.log(\"val_r_square\", val_r_square, prog_bar=True, on_step=False, on_epoch=True)\n        self.validation_step_outputs.clear()\n\n    def configure_optimizers(self):\n        \"\"\"Configure optimizer and learning rate scheduler\"\"\"\n        optimizer = torch.optim.Adam(self.parameters(), lr=self.lr, weight_decay=self.weight_decay)\n        scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(\n            optimizer, \n            mode='min', \n            factor=0.5, \n            patience=5, \n            verbose=True\n        )\n        return {\n            'optimizer': optimizer,\n            'lr_scheduler': {\n                'scheduler': scheduler,\n                'monitor': 'val_loss',\n            }\n        }\n\n    def on_train_epoch_end(self):\n        \"\"\"Log metrics at end of training epoch\"\"\"\n        if not self.trainer.sanity_checking:\n            epoch = self.trainer.current_epoch\n            metrics = {k: v.item() if isinstance(v, torch.Tensor) else v \n                      for k, v in self.trainer.logged_metrics.items()}\n            formatted_metrics = {k: f\"{v:.5f}\" for k, v in metrics.items()}\n            print(f\"Epoch {epoch}: {formatted_metrics}\")\n\n# Load ensemble of models (5-fold cross-validation)\nN_folds = 5\nmodels = []\nfor fold in range(N_folds):\n    checkpoint_path = f\"{CONFIG.model_paths[0]}/nn_{fold}.model\"\n    model = NN.load_from_checkpoint(checkpoint_path)\n    models.append(model.to(\"cuda:0\"))","metadata":{"trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:40.680616Z","iopub.execute_input":"2025-01-09T20:31:40.680880Z","iopub.status.idle":"2025-01-09T20:31:42.047717Z","shell.execute_reply.started":"2025-01-09T20:31:40.680854Z","shell.execute_reply":"2025-01-09T20:31:42.046763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lags_ : pl.DataFrame | None = None\n    \ndef predict_rmf(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    global lags_\n    if lags is not None:\n        lags_ = lags\n\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n    symbol_ids = test.select('symbol_id').to_numpy()[:, 0]\n\n    if not lags is None:\n        lags = lags.group_by([\"date_id\", \"symbol_id\"], maintain_order=True).last() # pick up last record of previous date\n        test = test.join(lags, on=[\"date_id\", \"symbol_id\"],  how=\"left\")\n    else:\n        test = test.with_columns(\n            ( pl.lit(0.0).alias(f'responder_{idx}_lag_1') for idx in range(9) )\n        )\n    \n    preds = np.zeros((test.shape[0],))\n    test_input = test[CONFIG.feature_cols].to_pandas()\n    test_input = test_input.fillna(method = 'ffill').fillna(0)\n    test_input = torch.FloatTensor(test_input.values).to(\"cuda:0\")\n    with torch.no_grad():\n        for i, nn_model in enumerate(tqdm(models)):\n            nn_model.eval()\n            preds += nn_model(test_input).cpu().numpy() / 5\n    \n    predictions = \\\n    test.select('row_id').\\\n    with_columns(\n        pl.Series(\n            name   = 'responder_6', \n            values = np.clip(preds, a_min = -5, a_max = 5),\n            dtype  = pl.Float64,\n        )\n    )\n\n    # The predict function must return a DataFrame\n    assert isinstance(predictions, pl.DataFrame | pd.DataFrame)\n    # with columns 'row_id', 'responer_6'\n    assert list(predictions.columns) == ['row_id', 'responder_6']\n    # and as many rows as the test data.\n    assert len(predictions) == len(test)\n\n    return predictions","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# # Clear validation data from memory to free up space\n# del valid\n# gc.collect()\n\n# # Global variable to store lagged features\n# lags_: pl.DataFrame | None = None\n\n# def predict_nn_xgb(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n#     \"\"\"\n#     Make predictions using ensemble of XGBoost and Neural Network models\n    \n#     Args:\n#         test: DataFrame containing test data\n#         lags: DataFrame containing lagged features (optional)\n        \n#     Returns:\n#         DataFrame with predictions\n#     \"\"\"\n#     global lags_\n    \n#     # Store lags in global variable if provided\n#     if lags is not None:\n#         lags_ = lags\n\n#     # Initialize predictions DataFrame with row_id and placeholder predictions\n#     predictions_nn = test.select('row_id', pl.lit(0.0).alias('responder_6',))\n\n#     # Process lagged features\n#     # Get last record for each date_id and symbol_id combination\n#     lags = lags_.clone().group_by([\"date_id\", \"symbol_id\"], maintain_order=True).last()\n    \n#     # Join test data with lagged features\n#     test = test.join(lags, on=[\"date_id\", \"symbol_id\"], how=\"left\")\n\n#     # Initialize arrays for model predictions\n#     preds_xgb = np.zeros((test.shape[0],))  # XGBoost predictions\n#     preds_nn = np.zeros((test.shape[0],))   # Neural Network predictions\n\n#     # Generate XGBoost predictions\n#     preds_xgb += xgb_model.predict(test[xgb_feature_cols].to_pandas()) * 0.25\n#     preds_xgb += xgb_model2.predict(test[xgb_feature_cols].to_pandas()) * 0.25\n#     preds_xgb += xgb_model4.predict(test[xgb_feature_cols].to_pandas()) * 0.25\n#     preds_xgb += xgb_model5.predict(test[xgb_feature_cols].to_pandas()) * 0.25\n\n#     # Generate Neural Network predictions\n#     # Prepare input data\n#     test_input = test[CONFIG.feature_cols].to_pandas()\n#     # Handle missing values: forward fill then fill remaining with zeros\n#     test_input = test_input.fillna(method='ffill').fillna(0)\n#     # Convert to PyTorch tensor and move to GPU\n#     test_input = torch.FloatTensor(test_input.values).to(\"cuda:0\")\n\n#     # Generate predictions from Neural Network ensemble\n#     with torch.no_grad():  # Disable gradient calculation for inference\n#         for i, nn_model in enumerate(models):\n#             nn_model.eval()  # Set model to evaluation mode\n#             # Average predictions from all models\n#             preds_nn += nn_model(test_input).cpu().numpy() / len(models)\n\n#     # Combine predictions with equal weights (50% XGBoost, 50% Neural Network)\n#     preds = 0.55 * preds_xgb + 0.45 * preds_nn\n\n#     # Create final predictions DataFrame\n#     predictions_nn = test.select('row_id').\\\n#         with_columns(\n#             pl.Series(\n#                 name='responder_6',\n#                 values=np.clip(preds, a_min=-5, a_max=5),  # Clip predictions to [-5, 5] range\n#                 dtype=pl.Float64,\n#             )\n#         )\n\n#     return predictions_nn","metadata":{"_kg_hide-input":true,"papermill":{"duration":0.031534,"end_time":"2024-10-26T03:27:39.453592","exception":false,"start_time":"2024-10-26T03:27:39.422058","status":"completed"},"tags":[],"_kg_hide-output":false,"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:42.049455Z","iopub.execute_input":"2025-01-09T20:31:42.049742Z","iopub.status.idle":"2025-01-09T20:31:42.329289Z","shell.execute_reply.started":"2025-01-09T20:31:42.049716Z","shell.execute_reply":"2025-01-09T20:31:42.328484Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Jane Street | TabM/FT-Transformer inference (i2nfinit3y)\n`LB: 0.0074` https://www.kaggle.com/code/i2nfinit3y/jane-street-tabm-ft-transformer-inference?scriptVersionId=213715783","metadata":{}},{"cell_type":"code","source":"# Create list of feature names from 0-78, excluding feature_61\nfeature_list = [f\"feature_{idx:02d}\" for idx in range(79) if idx != 61]\n\n# Define target column name\ntarget_col = \"responder_6\" \n\n# Create list of features for testing, combining feature_list with lagged responder features\nfeature_test = feature_list + [f\"responder_{idx}_lag_1\" for idx in range(9)] \n\n# Define categorical features\nfeature_cat = [\"feature_09\", \"feature_10\", \"feature_11\"]\n\n# Define continuous features by excluding categorical ones from feature_test\nfeature_cont = [item for item in feature_test if item not in feature_cat]\n\n# Set batch size for model training\nbatch_size = 8192\n\n# Create list of features to standardize (continuous features + lagged responder features)\nstd_feature = [i for i in feature_list if i not in feature_cat] + [f\"responder_{idx}_lag_1\" for idx in range(9)]\n\n# Load pre-computed statistics for standardization\ndata_stats = joblib.load(\"/kaggle/input/my-own-js/data_stats.pkl\")\nmeans = data_stats['mean']\nstds = data_stats['std']\n\ndef standardize(df, feature_cols, means, stds):\n    \"\"\"\n    Standardize features using pre-computed means and standard deviations\n    Args:\n        df: Input dataframe\n        feature_cols: List of columns to standardize\n        means: Dictionary of mean values\n        stds: Dictionary of standard deviation values\n    Returns:\n        Standardized dataframe\n    \"\"\"\n    return df.with_columns([\n        ((pl.col(col) - means[col]) / stds[col]).alias(col) for col in feature_cols\n    ])\n\n# Dictionary mappings for categorical variables encoding\ncategory_mappings = {'feature_09': {2: 0, 4: 1, 9: 2, 11: 3, 12: 4, 14: 5, 15: 6, 25: 7, 26: 8, 30: 9, 34: 10, 42: 11, 44: 12, 46: 13, 49: 14, 50: 15, 57: 16, 64: 17, 68: 18, 70: 19, 81: 20, 82: 21},\n 'feature_10': {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 10: 7, 12: 8},\n 'feature_11': {9: 0, 11: 1, 13: 2, 16: 3, 24: 4, 25: 5, 34: 6, 40: 7, 48: 8, 50: 9, 59: 10, 62: 11, 63: 12, 66: 13,\n  76: 14, 150: 15, 158: 16, 159: 17, 171: 18, 195: 19, 214: 20, 230: 21, 261: 22, 297: 23, 336: 24, 376: 25, 388: 26, 410: 27, 522: 28, 534: 29, 539: 30},\n 'symbol_id': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19,\n  20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29, 30: 30, 31: 31, 32: 32, 33: 33, 34: 34, 35: 35, 36: 36, 37: 37, 38: 38},\n 'time_id' : {i : i for i in range(968)}}\n\ndef encode_column(df, column, mapping):\n    \"\"\"\n    Encode categorical columns using provided mapping\n    Args:\n        df: Input dataframe\n        column: Column name to encode\n        mapping: Dictionary with encoding mappings\n    Returns:\n        Dataframe with encoded column\n    \"\"\"\n    max_value = max(mapping.values())  \n    \n    def encode_category(category):\n        # Return max_value + 1 for any unseen categories\n        return mapping.get(category, max_value + 1)  \n    \n    return df.with_columns(\n        pl.col(column).map_elements(encode_category).alias(column)\n    )\n\nclass R2Loss(nn.Module):\n    \"\"\"\n    Custom R-squared loss function for PyTorch\n    R² = 1 - (MSE / variance of y)\n    \"\"\"\n    def __init__(self):\n        super(R2Loss, self).__init__()\n\n    def forward(self, y_pred, y_true):\n        # Calculate MSE\n        mse_loss = torch.sum((y_pred - y_true) ** 2)\n        # Calculate variance of true values\n        var_y = torch.sum(y_true ** 2)\n        # Calculate R² loss (adding small epsilon to avoid division by zero)\n        loss = mse_loss / (var_y + 1e-38)\n        return loss\n\nclass NN(LightningModule):\n    \"\"\"\n    Neural Network model using PyTorch Lightning\n    Implements a custom architecture with continuous and categorical inputs\n    \"\"\"\n    def __init__(self, n_cont_features, cat_cardinalities, n_classes, lr, weight_decay):\n        super().__init__()\n        self.save_hyperparameters()\n        self.k = 16  # Number of ensemble members\n\n        # Initialize the main model architecture\n        self.model = Model(\n                n_num_features=n_cont_features,\n                cat_cardinalities=cat_cardinalities,\n                n_classes=n_classes,\n                backbone={\n                    'type': 'MLP',\n                    'n_blocks': 3,\n                    'd_block': 512,\n                    'dropout': 0.25,\n                },\n                bins=None,\n                num_embeddings=None,\n                arch_type='tabm',\n                k=self.k,\n            )\n        \n        # Set learning parameters\n        self.lr = lr\n        self.weight_decay = weight_decay\n        \n        # Initialize lists to store outputs during training and validation\n        self.training_step_outputs = []\n        self.validation_step_outputs = []\n        \n        # Define loss function\n        self.loss_fn = R2Loss()\n\n    def forward(self, x_cont, x_cat):\n        \"\"\"Forward pass of the model\"\"\"\n        return self.model(x_cont, x_cat).squeeze(-1)\n\n    def training_step(self, batch):\n        \"\"\"\n        Perform a single training step\n        Args:\n            batch: Tuple containing (continuous features, categorical features, \n                   target values, weights, weighted targets)\n        \"\"\"\n        x_cont, x_cat, y, w, w_y = batch\n        \n        # Add random noise to continuous features for regularization\n        x_cont = x_cont + torch.randn_like(x_cont) * 0.02\n        \n        # Get model predictions\n        y_hat = self(x_cont, x_cat)\n        \n        # Calculate loss\n        loss = self.loss_fn(y_hat.flatten(0, 1), y.repeat_interleave(self.k))\n        \n        # Log training loss\n        self.log('train_loss', loss, on_step=True, on_epoch=True, \n                prog_bar=True, logger=True, batch_size=x_cont.size(0))\n        \n        # Store outputs for epoch-end calculations\n        self.training_step_outputs.append((y_hat.mean(1), y, w))\n        \n        return loss\n\n    def validation_step(self, batch):\n        \"\"\"\n        Perform a single validation step\n        Similar to training_step but used for validation\n        \"\"\"\n        x_cont, x_cat, y, w, w_y = batch\n        x_cont = x_cont + torch.randn_like(x_cont) * 0.02\n        y_hat = self(x_cont, x_cat)\n        \n        loss = self.loss_fn(y_hat.flatten(0, 1), y.repeat_interleave(self.k))\n        \n        self.log('val_loss', loss, on_step=False, on_epoch=True, \n                prog_bar=True, logger=True, batch_size=x_cont.size(0))\n        \n        self.validation_step_outputs.append((y_hat.mean(1), y, w))\n        return loss\n\n    def on_validation_epoch_end(self):\n        \"\"\"Calculate validation metrics at the end of each validation epoch\"\"\"\n        y = torch.cat([x[1] for x in self.validation_step_outputs]).cpu().numpy()\n        \n        if self.trainer.sanity_checking:\n            prob = torch.cat([x[0] for x in self.validation_step_outputs]).cpu().numpy()\n        else:\n            prob = torch.cat([x[0] for x in self.validation_step_outputs]).cpu().numpy()\n            weights = torch.cat([x[2] for x in self.validation_step_outputs]).cpu().numpy()\n            \n            # Calculate R² score for validation\n            val_r_square = r2_val(y, prob, weights)\n            self.log(\"val_r_square\", val_r_square, prog_bar=True, on_step=False, on_epoch=True)\n        \n        self.validation_step_outputs.clear()\n\n    def configure_optimizers(self):\n        \"\"\"Configure the optimizer for training\"\"\"\n        optimizer = torch.optim.AdamW(\n            make_parameter_groups(self.model), \n            lr=self.lr, \n            weight_decay=self.weight_decay\n        )\n        return {'optimizer': optimizer}\n\n    def on_train_epoch_end(self):\n        \"\"\"Calculate and log training metrics at the end of each training epoch\"\"\"\n        if self.trainer.sanity_checking:\n            return\n            \n        # Gather all outputs from training steps\n        y = torch.cat([x[1] for x in self.training_step_outputs]).cpu().numpy()\n        prob = torch.cat([x[0] for x in self.training_step_outputs]).detach().cpu().numpy()\n        weights = torch.cat([x[2] for x in self.training_step_outputs]).cpu().numpy()\n        \n        # Calculate R² score for training\n        train_r_square = r2_val(y, prob, weights)\n        self.log(\"train_r_square\", train_r_square, prog_bar=True, on_step=False, on_epoch=True)\n        \n        self.training_step_outputs.clear()\n        \n        # Print epoch metrics\n        epoch = self.trainer.current_epoch\n        metrics = {k: v.item() if isinstance(v, torch.Tensor) else v \n                  for k, v in self.trainer.logged_metrics.items()}\n        formatted_metrics = {k: f\"{v:.5f}\" for k, v in metrics.items()}\n        print(f\"Epoch {epoch}: {formatted_metrics}\")\n\n        \nclass custom_args:\n    \"\"\"\n    Custom arguments class to store model and training configuration\n    Acts as a configuration container similar to argparse.Namespace\n    \"\"\"\n    def __init__(self):\n        # GPU Configuration\n        self.usegpu = True\n        self.gpuid = 0\n        \n        # Random seed for reproducibility\n        self.seed = 42\n        \n        # Model Configuration\n        self.model = 'nn'  # Neural network model type\n        \n        # Wandb logging configuration\n        self.use_wandb = False\n        self.project = 'js-tabm-with-lags'\n        \n        # Data and loading configuration\n        self.dname = \"./input_df/\"  # Data directory\n        self.loader_workers = 10    # Number of workers for data loading\n        self.bs = 8192             # Batch size\n        \n        # Model hyperparameters\n        self.lr = 1e-3             # Learning rate\n        self.weight_decay = 8e-4    # Weight decay for regularization\n        \n        # Feature configuration\n        self.n_cont_features = 84   # Number of continuous features\n        self.n_cat_features = 5     # Number of categorical features\n        self.n_classes = None       # Number of classes (None for regression)\n        \n        # Categorical feature cardinalities\n        # [feature_09, feature_10, feature_11, symbol_id, time_id]\n        self.cat_cardinalities = [23, 10, 32, 40, 969]\n        \n        # Training configuration\n        self.patience = 7           # Early stopping patience\n        self.max_epochs = 10        # Maximum training epochs\n        self.N_fold = 5            # Number of cross-validation folds\n\n# Create instance of custom arguments\nmy_args = custom_args()\n\n# Set up device (GPU if available, else CPU)\ndevice = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')\n\n# Load pre-trained model from checkpoint and move to appropriate device\nmodel = NN.load_from_checkpoint('/kaggle/input/my-own-js/tabm_epochepoch03.ckpt').to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:42.331094Z","iopub.execute_input":"2025-01-09T20:31:42.331382Z","iopub.status.idle":"2025-01-09T20:31:42.429385Z","shell.execute_reply.started":"2025-01-09T20:31:42.331354Z","shell.execute_reply":"2025-01-09T20:31:42.428708Z"},"jupyter":{"source_hidden":true},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Global variables to store lag features\nlags_ : pl.DataFrame | None = None\n\nlags_history = None\n\ndef predict_tabm(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"\n    Make predictions using the TABM (Tabular Model)\n    \n    Args:\n        test: Input DataFrame containing test features\n        lags: DataFrame containing lagged features\n        \n    Returns:\n        DataFrame with predictions\n    \"\"\"\n    global lags_, lags_history\n    # Update global lags if new ones provided\n    if lags is not None:\n        lags_ = lags\n\n    # Encode categorical features\n    for col in feature_cat + ['symbol_id', 'time_id']:\n        test = encode_column(test, col, category_mappings[col])\n\n    # Initialize predictions DataFrame with row_ids\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n\n    # Extract symbol and time information\n    symbol_ids = test.select('symbol_id').to_numpy()[:, 0]\n    time_id = test.select(\"time_id\").to_numpy()[0]\n    timie_id_array = test.select(\"time_id\").to_numpy()[:, 0]\n    \n    # Handle time_id = 0 case (first prediction)\n    if time_id == 0:\n        # Convert time_id and symbol_id to integers\n        lags = lags.with_columns(pl.col('time_id').cast(pl.Int64))\n        lags = lags.with_columns(pl.col('symbol_id').cast(pl.Int64)) \n        # Store full lags history and filter for time_id 0\n        lags_history = lags\n        lags = lags.filter(pl.col(\"time_id\") == 0)  \n        test = test.join(lags, on=[\"time_id\", \"symbol_id\"],  how=\"left\")\n    else:\n        # Filter lags for current time_id\n        lags = lags_history.filter(pl.col(\"time_id\") == time_id)\n        test = test.join(lags, on=[\"time_id\", \"symbol_id\"],  how=\"left\")\n\n    # Fill missing values with 0\n    test = test.with_columns([\n        pl.col(col).fill_null(0) for col in feature_list + [f\"responder_{idx}_lag_1\" for idx in range(9)] \n    ])\n    \n    # Standardize features\n    test = standardize(test, std_feature, means, stds)\n\n    # Convert to numpy array and then to torch tensors\n    X_test = test[feature_test].to_numpy()\n    X_test_tensor = torch.tensor(X_test, dtype=torch.float32).to(device)\n    symbol_tensor = torch.tensor(symbol_ids, dtype=torch.float32).to(device)\n    time_tensor = torch.tensor(timie_id_array, dtype=torch.float32).to(device)\n    \n    # Separate categorical and continuous features\n    X_cat = X_test_tensor[:, [9, 10, 11]]\n    X_cont = X_test_tensor[:, [i for i in range(X_test_tensor.shape[1]) if i not in [9, 10, 11]]]\n\n    # Combine categorical features with symbol and time information\n    X_cat = (torch.concat([X_cat, symbol_tensor.unsqueeze(-1), time_tensor.unsqueeze(-1)], axis=1)).to(torch.int64)\n\n    # Make predictions\n    model.eval()\n    with torch.no_grad():\n        \n        outputs = model(X_cont, X_cat)\n        # Assuming the model outputs a tensor of shape (batch_size, 1)\n        preds = outputs.squeeze(-1).cpu().numpy()\n        preds = preds.mean(1)\n\n    # Create final predictions DataFrame\n    predictions = \\\n    test.select('row_id').\\\n    with_columns(\n        pl.Series(\n            name   = 'responder_6', \n            values = np.clip(preds, a_min = -5, a_max = 5),\n            dtype  = pl.Float64,\n        )\n    )\n\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:42.430449Z","iopub.execute_input":"2025-01-09T20:31:42.430765Z","iopub.status.idle":"2025-01-09T20:31:42.444321Z","shell.execute_reply.started":"2025-01-09T20:31:42.430736Z","shell.execute_reply":"2025-01-09T20:31:42.443487Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_path = \"/kaggle/input/js-2024-13-01-11\"\nfinal_feature = ['symbol_id','sin_time_id','cos_time_id','sin_time_id_halfday','cos_time_id_halfday'] + [f'feature_{i:02}' for i in range(79)]\n\nlgb = Booster(model_file=f\"{model_path}/lgb.model\")\ncat = CatBoostRegressor()\ncat.load_model(f\"{model_path}/cat.model\")\nxgb = XGBRegressor()\nxgb.load_model(f\"{model_path}/xgb.model\")\n\ndef predict_ensemble(test,lags):\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n    test=test.to_pandas()\n    test['sin_time_id']=np.sin(2*np.pi*test['time_id']/967)\n    test['cos_time_id']=np.cos(2*np.pi*test['time_id']/967)\n    test['sin_time_id_halfday']=np.sin(2*np.pi*test['time_id']/483)\n    test['cos_time_id_halfday']=np.cos(2*np.pi*test['time_id']/483)\n    test=test.fillna(-1)\n    test=test[final_feature]\n    eps=1e-10\n    test_preds=0.55*lgb.predict(test)+0.2*cat.predict(test)+0.25*xgb.predict(test)\n    test_preds=np.clip(test_preds,-5+eps,5-eps)\n    predictions = predictions.with_columns(pl.Series('responder_6', test_preds.ravel()))\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:42.445354Z","iopub.execute_input":"2025-01-09T20:31:42.446039Z","iopub.status.idle":"2025-01-09T20:31:42.498865Z","shell.execute_reply.started":"2025-01-09T20:31:42.445998Z","shell.execute_reply":"2025-01-09T20:31:42.497759Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## JS Ridge baseline (yunsuxiaozi)\n\n`LB: 0.0042` https://www.kaggle.com/code/yunsuxiaozi/js-ridge-baseline?scriptVersionId=202938352","metadata":{}},{"cell_type":"code","source":"def load_from_dill(model_name, model_path=None, file_ext='.dill'):\n    \"\"\"\n    Load a model from a dill file\n    \n    Args:\n        model_name: Name of the model file (without extension)\n        model_path: Directory path containing the model file\n        file_ext: File extension (default: '.dill')\n        \n    Returns:\n        Loaded model object\n    \"\"\"\n    model_object = None\n    # Open and load the model file using dill\n    with open(f\"{model_path}/{model_name}{file_ext}\", \"rb\") as file_handle:\n        model_object = dill.load(file_handle)\n    return model_object\n\n# Load pre-trained Ridge Regression model\nrdg = load_from_dill(\n    model_name='Ridge', \n    model_path=\"/kaggle/input/jsridgev01011635\"\n)\n\ndef predict_ridge(test, lags):\n    \"\"\"\n    Make predictions using Ridge Regression model\n    \n    Args:\n        test: DataFrame containing test data\n        lags: DataFrame containing lagged features (unused in this function)\n        \n    Returns:\n        DataFrame with predictions\n    \"\"\"\n    # Select the 79 numerical features\n    cols = [f'feature_{i:02}' for i in range(79)]\n\n    # Initialize predictions DataFrame with row_id and placeholder predictions\n    predictions = test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n\n    # Generate predictions:\n    # 1. Select required features\n    # 2. Convert to pandas\n    # 3. Fill missing values with 3\n    # 4. Make predictions using Ridge model\n    test_preds = rdg.predict(test[cols].to_pandas().fillna(3).values)\n\n    # Add predictions to result DataFrame\n    predictions = predictions.with_columns(pl.Series('responder_6', test_preds.ravel()))\n\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:42.500026Z","iopub.execute_input":"2025-01-09T20:31:42.500357Z","iopub.status.idle":"2025-01-09T20:31:42.508815Z","shell.execute_reply.started":"2025-01-09T20:31:42.500327Z","shell.execute_reply":"2025-01-09T20:31:42.507623Z"},"jupyter":{"source_hidden":true},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# XGB","metadata":{}},{"cell_type":"code","source":"from tqdm.auto import tqdm\nfrom matplotlib import pyplot as plt\nimport pickle\n\nfrom sklearn.linear_model import BayesianRidge\nfrom sklearn.metrics import r2_score\nfrom catboost import CatBoostRegressor\nfrom sklearn.ensemble import VotingRegressor","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:42.510123Z","iopub.execute_input":"2025-01-09T20:31:42.510776Z","iopub.status.idle":"2025-01-09T20:31:42.522793Z","shell.execute_reply.started":"2025-01-09T20:31:42.510734Z","shell.execute_reply":"2025-01-09T20:31:42.521778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CONFIG2:\n    debug = False\n    seed = 42\n    target_col = \"responder_6\"\n    lag_cols_rename = { f\"responder_{idx}_lag_1\" : f\"responder_{idx}\" for idx in range(9)}\n    lag_target_cols_name = [f\"responder_{idx}\" for idx in range(9)]\n    lag_cols_original = [\"date_id\", \"time_id\", \"symbol_id\"] + [f\"responder_{idx}\" for idx in range(9)]\n    model_path = \"/kaggle/input/janestreet-public-model/xgb_001.pkl\"\n    lag_ndays = 4\n\ndef create_agg_list(day, columns):\n    agg_mean_list = [pl.col(c).mean().name.suffix(f\"_mean_{day}d\") for c in columns]\n    agg_std_list = [pl.col(c).std().name.suffix(f\"_std_{day}d\") for c in columns]\n    agg_max_list = [pl.col(c).max().name.suffix(f\"_max_{day}d\") for c in columns]\n    agg_last_list = [pl.col(c).last().name.suffix(f\"_last_{day}d\") for c in columns]\n    agg_list = agg_mean_list + agg_std_list + agg_max_list + agg_last_list\n    return agg_list\n\nwith open( CONFIG2.model_path, \"rb\") as fp:\n    result = pickle.load(fp)\n    \nx_model = result[\"model\"]\nfeatures = result[\"features\"]\n\nhistory = pl.scan_parquet(\n    \"/kaggle/input/jane-street-real-time-market-data-forecasting/train.parquet\"\n).select(['date_id','time_id','symbol_id'] + [f\"responder_{idx}\" for idx in range(9)]).filter(\n    (pl.col(\"date_id\")>=(1698 - CONFIG2.lag_ndays))&(pl.col(\"date_id\")<1698)\n)\n\nhistory = history.with_columns(\n    date_id = (pl.col(\"date_id\") - pl.lit(1698)).cast(pl.Int16)\n)\nhistory = history.collect()\n\nhistory_column_types = {\n    'date_id': pl.Int16,\n    'time_id': pl.Int16,\n    'symbol_id': pl.Int16\n}\nfeature_column_types = {}\nfor f in [f\"feature_{idx:02d}\" for idx in range(79)]:\n    feature_column_types[f] = pl.Float32\n\nresponder_column_types = {}\nfor f in [f\"responder_{idx}\" for idx in range(9)]:\n    responder_column_types[f] = pl.Float32\n\nhistory = history.cast(history_column_types)\nhistory = history.cast(responder_column_types)\n\n\n\ndef predict_xgb(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \n    global history\n    global lags_infer\n    \n    symbol_ids = test.select(\"symbol_id\").to_numpy()[:, 0]\n    current_date = test.select(\"date_id\").to_numpy()[:, 0][0]\n\n    if lags is not None:\n        \n        lags = lags.rename(CONFIG2.lag_cols_rename)\n        lags = lags.cast(history_column_types)\n        lags = lags.cast(responder_column_types)\n\n        history = pl.concat([history, lags])\n       \n        history = history.filter(pl.col(\"date_id\") > (current_date - CONFIG2.lag_ndays))\n       \n        agg_list = create_agg_list(1, CONFIG2.lag_target_cols_name)\n        shift_n_data = history.filter(pl.col(\"date_id\") == current_date)\n        lags_infer = shift_n_data.group_by([\"date_id\", \"symbol_id\"], maintain_order=True).agg(agg_list)\n  \n    \n    test = test.cast(history_column_types)\n    test = test.cast(feature_column_types)\n    \n    X_test = test.join(lags_infer, on=[\"date_id\", \"symbol_id\"], how=\"left\")\n    \n    preds = np.zeros((X_test.shape[0],))\n    preds += x_model.predict(X_test[features].to_pandas().values)\n    preds = np.clip(preds, a_min=-5, a_max=5)\n    \n    predictions = (\n        test.select('row_id').with_columns(\n            pl.Series(name='responder_6', values=preds, dtype=pl.Float64)\n        )\n    )\n\n    assert isinstance(predictions, pl.DataFrame | pd.DataFrame)\n    assert list(predictions.columns) == ['row_id', 'responder_6']\n    assert len(predictions) == len(test)\n\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:42.526058Z","iopub.execute_input":"2025-01-09T20:31:42.526458Z","iopub.status.idle":"2025-01-09T20:31:42.659215Z","shell.execute_reply.started":"2025-01-09T20:31:42.526427Z","shell.execute_reply":"2025-01-09T20:31:42.658071Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# LGB","metadata":{}},{"cell_type":"code","source":"from sklearn.base import BaseEstimator, RegressorMixin\n\ndef save_model(model, path):\n    with open(path, \"wb\") as f:\n        pickle.dump(model, f)\n\n\ndef load_model(path):\n    with open(path, \"rb\") as f:\n        model = pickle.load(f)\n    return model\n\nclass VotingModel(BaseEstimator, RegressorMixin):\n    \"\"\"\n    A voting ensemble model that averages predictions from multiple estimators.\n\n    Parameters:\n    - estimators: List of estimators to include in the voting ensemble\n\n    Methods:\n    - fit(X, y=None): No training is performed as it's just an aggregator.\n    - predict(X): Returns the average prediction from all included estimators.\n    - predict_proba(X): Returns the average class probabilities from all included estimators.\n    \"\"\"\n\n    def __init__(self, estimators):\n        \"\"\"\n        Initializes the VotingModel with a list of estimators.\n\n        Parameters:\n        - estimators: List of estimators to include in the voting ensemble\n        \"\"\"\n        super().__init__()\n        self.estimators = estimators\n\n    def fit(self, X, y=None):\n        \"\"\"Fits the voting model (no operation).\"\"\"\n        return self\n\n    def predict(self, X):\n        \"\"\"Returns the average prediction from all included estimators.\"\"\"\n        y_preds = [estimator.predict(X) for estimator in self.estimators]\n        return np.mean(y_preds, axis=0)\n\n    def predict_proba(self, X):\n        \"\"\"Returns the average class probabilities from all included estimators.\"\"\"\n        y_preds = [estimator.predict_proba(X) for estimator in self.estimators]\n        return np.mean(y_preds, axis=0)\n\nmodel_lgb = load_model(r'/kaggle/input/lgbm-1129-1941-0179/LGBM_1129_1941_0179.pkl')\nfeature_names = model_lgb.feature_name()\n\ndef predict_lgb(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n        \"\"\"Make a prediction.\"\"\"\n        global lags_\n        if lags is not None:\n            lags_ = lags\n\n        predictions = test.select(\n            'row_id',\n            pl.lit(0.0).alias('responder_6'),\n        )\n     \n        feat = test[feature_names].to_pandas()\n\n        preds = model_lgb.predict(feat) - 0.003\n\n\n        predictions = test.select('row_id').\\\n                    with_columns(\n                        pl.Series(\n                            name='responder_6',  # 预测结果列的名称\n                            values=np.clip(preds, a_min=-5, a_max=5),  # 将预测结果限制在 -5 到 5 的范围内\n                            dtype=pl.Float64,  # 预测列的数据类型为 Float64\n                        )\n                    )\n        \n        \n        # The predict function must return a DataFrame\n        assert isinstance(predictions, pl.DataFrame | pd.DataFrame)\n        # with columns 'row_id', 'responer_6'\n        assert list(predictions.columns) == ['row_id', 'responder_6']\n        # and as many rows as the test data.\n        assert len(predictions) == len(test)\n\n        return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:31:42.660783Z","iopub.execute_input":"2025-01-09T20:31:42.661147Z","iopub.status.idle":"2025-01-09T20:31:42.716347Z","shell.execute_reply.started":"2025-01-09T20:31:42.661107Z","shell.execute_reply":"2025-01-09T20:31:42.715242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict_nn_lgb(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame:\n    try:\n        pdA = predict_lgb(test, lags).to_pandas().rename(columns={'responder_6': 'responder_A'})\n        pdB = predict_rmf(test, lags).to_pandas().rename(columns={'responder_6': 'responder_B'})\n    except ValueError as e:\n        print(\"Error during model predictions:\", e)\n        return test.select('row_id', pl.lit(0.0).alias('responder_6'))\n\n    # Step 3: Merge predictions\n    pds = pd.merge(pdA, pdB, on=['row_id'])\n\n\n    # Step 4: Combine using weights\n    pds['responder_6'] = pds['responder_A']*0.5 +pds['responder_B']*0.5 \n    # Step 5: Prepare final predictions\n    predictions = test.select('row_id', pl.lit(0.0).alias('responder_6'))\n    pred = pds['responder_6'].to_numpy()\n    predictions = predictions.with_columns(pl.Series('responder_6', pred.ravel()))\n    display(predictions)\n    return predictions","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# <div style=\"color:white;display:fill;border-radius:5px;background-color:#3371ff;text-align:center;letter-spacing:0.1px;overflow:hidden;padding:20px;color:white;overflow:hidden;margin:0;font-size:100%\">Ensemble notebook</div>","metadata":{}},{"cell_type":"code","source":"def predict_tabm_xgb(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame:\n    try:\n        pdA = predict_xgb(test, lags).to_pandas().rename(columns={'responder_6': 'responder_A'})\n        pdB = predict_tabm(test, lags).to_pandas().rename(columns={'responder_6': 'responder_B'})\n    except ValueError as e:\n        print(\"Error during model predictions:\", e)\n        return test.select('row_id', pl.lit(0.0).alias('responder_6'))\n\n    # Step 3: Merge predictions\n    pds = pd.merge(pdA, pdB, on=['row_id'])\n\n\n    # Step 4: Combine using weights\n    pds['responder_6'] = pds['responder_A']*0.5 +pds['responder_B']*0.5 \n    # Step 5: Prepare final predictions\n    predictions = test.select('row_id', pl.lit(0.0).alias('responder_6'))\n    pred = pds['responder_6'].to_numpy()\n    predictions = predictions.with_columns(pl.Series('responder_6', pred.ravel()))\n    display(predictions)\n    return predictions","metadata":{"_kg_hide-input":false,"_kg_hide-output":false,"papermill":{"duration":0.351292,"end_time":"2024-10-26T03:27:42.101707","exception":false,"start_time":"2024-10-26T03:27:41.750415","status":"completed"},"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:32:09.441876Z","iopub.execute_input":"2025-01-09T20:32:09.442483Z","iopub.status.idle":"2025-01-09T20:32:09.452240Z","shell.execute_reply.started":"2025-01-09T20:32:09.442447Z","shell.execute_reply":"2025-01-09T20:32:09.451455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"\n    Make ensemble predictions combining three different models:\n    - Neural Network + XGBoost ensemble\n    - Ridge Regression\n    - Tabular Model (TABM)\n    \n    Args:\n        test: DataFrame containing test features\n        lags: DataFrame containing lagged features\n        \n    Returns:\n        DataFrame with weighted ensemble predictions\n    \"\"\"\n    # Get predictions from each model/ensemble\n    pd_nn_xgb = predict_nn_lgb(test, lags).to_pandas()  # Neural Network + XGBoost predictions\n    pd_ridge = predict_ridge(test, lags).to_pandas()     # Ridge Regression predictions \n    pd_tabm = predict_tabm_xgb(test, lags).to_pandas()       # Tabular Model predictions\n    pd_ensemble = predict_ensemble(test, lags).to_pandas()\n    \n    # Rename prediction columns to avoid naming conflicts during merge\n    pd_nn_xgb = pd_nn_xgb.rename(columns={'responder_6': 'col_nn_xgb'})\n    pd_ridge = pd_ridge.rename(columns={'responder_6': 'col_ridge'})\n    pd_tabm = pd_tabm.rename(columns={'responder_6': 'col_tabm'})\n    pd_ensemble = pd_ensemble.rename(columns={'responder_6': 'col_ensemble'})\n    \n    # Merge predictions from all models based on row_id\n    pds = pd.merge(pd_nn_xgb, pd_ridge, on=['row_id'])\n    pds = pd.merge(pds, pd_tabm, on=['row_id'])\n    pds = pd.merge(pds, pd_ensemble, on=['row_id'])\n    \n    # Define ensemble weights for each model\n    e_weights = [0.4,  # Weight for Neural Network + XGBoost\n                0.1,   # Weight for Ridge Regression\n                0.25,\n                0.25]   \n    \n    \n    # Create weighted ensemble predictions\n    pds['responder_6'] = (\n        pds['col_nn_xgb'] * e_weights[0] + \n        pds['col_ridge'] * e_weights[1] +    \n        pds['col_tabm'] * e_weights[2] +\n        pds['col_ensemble'] * e_weights[3]\n    )\n    \n    # Format final predictions DataFrame\n    predictions = test.select('row_id', pl.lit(0.0).alias('responder_6'))\n    pred = pds['responder_6'].to_numpy()\n    predictions = predictions.with_columns(pl.Series('responder_6', pred.ravel()))\n    \n    return predictions","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import kaggle_evaluation.jane_street_inference_server\n\n# Initialize the Jane Street inference server with our predict function\ninference_server = kaggle_evaluation.jane_street_inference_server.JSInferenceServer(predict)\n\n# Check if this is running in competition environment\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    # If in competition environment, serve predictions in real-time\n    inference_server.serve()\nelse:\n    # If running locally, use test data from provided parquet files\n    inference_server.run_local_gateway(\n        (\n            # Path to test data parquet file\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/test.parquet',\n            # Path to lagged features parquet file\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/lags.parquet',\n        )\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-09T20:32:09.453543Z","iopub.execute_input":"2025-01-09T20:32:09.453824Z","iopub.status.idle":"2025-01-09T20:32:09.731412Z","shell.execute_reply.started":"2025-01-09T20:32:09.453800Z","shell.execute_reply":"2025-01-09T20:32:09.729973Z"}},"outputs":[],"execution_count":null}]}