{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## This notebook refers to the Jane Street Forecasting Competition.\n\n### Predicting the variable 'responder_6' uses PCA (normalization with StandardScaler). \n\n### Time Series with Cross Validation and split equal to 5.\n\n### 3MM of data lines were used in this analysis.\n\n### Number of components explain 95% of the variance.\n\n### Linear interpolation was used and completely empty columns and the column of the variable 'weight' were removed.","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:40:51.923378Z","iopub.execute_input":"2024-12-31T12:40:51.923691Z","iopub.status.idle":"2024-12-31T12:40:53.993456Z","shell.execute_reply.started":"2024-12-31T12:40:51.923654Z","shell.execute_reply":"2024-12-31T12:40:53.992422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import r2_score\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.model_selection import TimeSeriesSplit","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:40:53.994512Z","iopub.execute_input":"2024-12-31T12:40:53.994931Z","iopub.status.idle":"2024-12-31T12:40:54.368311Z","shell.execute_reply.started":"2024-12-31T12:40:53.994903Z","shell.execute_reply":"2024-12-31T12:40:54.367198Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import polars as pl\nimport kaggle_evaluation.jane_street_inference_server","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:40:54.369358Z","iopub.execute_input":"2024-12-31T12:40:54.369766Z","iopub.status.idle":"2024-12-31T12:40:54.859311Z","shell.execute_reply.started":"2024-12-31T12:40:54.369724Z","shell.execute_reply":"2024-12-31T12:40:54.858271Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:40:54.860349Z","iopub.execute_input":"2024-12-31T12:40:54.860924Z","iopub.status.idle":"2024-12-31T12:40:54.865552Z","shell.execute_reply.started":"2024-12-31T12:40:54.860886Z","shell.execute_reply":"2024-12-31T12:40:54.864454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"path = \"/kaggle/input/jane-street-real-time-market-data-forecasting\"\n\nsamples = []\n\nfor i in range(1):\n    file_path = f\"{path}/train.parquet/partition_id={i}/part-0.parquet\"\n    \n    data = pd.read_parquet(file_path)\n    \n    samples.append(data)\n\ndf1 = pd.concat(samples, ignore_index=True)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-12-31T12:40:54.866709Z","iopub.execute_input":"2024-12-31T12:40:54.867111Z","iopub.status.idle":"2024-12-31T12:40:58.416347Z","shell.execute_reply.started":"2024-12-31T12:40:54.867048Z","shell.execute_reply":"2024-12-31T12:40:58.415394Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df1 = df1.head(3000_000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:40:58.418883Z","iopub.execute_input":"2024-12-31T12:40:58.419169Z","iopub.status.idle":"2024-12-31T12:40:58.423688Z","shell.execute_reply.started":"2024-12-31T12:40:58.419143Z","shell.execute_reply":"2024-12-31T12:40:58.422936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df1.interpolate(method='linear', limit_direction='both')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:40:58.425614Z","iopub.execute_input":"2024-12-31T12:40:58.425991Z","iopub.status.idle":"2024-12-31T12:41:02.576836Z","shell.execute_reply.started":"2024-12-31T12:40:58.425962Z","shell.execute_reply":"2024-12-31T12:41:02.575457Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.dropna(axis=1, how='all')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:02.577989Z","iopub.execute_input":"2024-12-31T12:41:02.578464Z","iopub.status.idle":"2024-12-31T12:41:03.010680Z","shell.execute_reply.started":"2024-12-31T12:41:02.578405Z","shell.execute_reply":"2024-12-31T12:41:03.009489Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.drop('weight', axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:03.011760Z","iopub.execute_input":"2024-12-31T12:41:03.012133Z","iopub.status.idle":"2024-12-31T12:41:03.215198Z","shell.execute_reply.started":"2024-12-31T12:41:03.012094Z","shell.execute_reply":"2024-12-31T12:41:03.214060Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tscv = TimeSeriesSplit(n_splits=5) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:03.216414Z","iopub.execute_input":"2024-12-31T12:41:03.216855Z","iopub.status.idle":"2024-12-31T12:41:03.221524Z","shell.execute_reply.started":"2024-12-31T12:41:03.216810Z","shell.execute_reply":"2024-12-31T12:41:03.220303Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## PCA","metadata":{}},{"cell_type":"code","source":"X = df.drop('responder_6',axis=1)\n\ny = df['responder_6']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:03.222753Z","iopub.execute_input":"2024-12-31T12:41:03.223119Z","iopub.status.idle":"2024-12-31T12:41:03.422000Z","shell.execute_reply.started":"2024-12-31T12:41:03.223093Z","shell.execute_reply":"2024-12-31T12:41:03.420936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"scaler = StandardScaler()\nX_scaled = scaler.fit_transform(df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:03.422998Z","iopub.execute_input":"2024-12-31T12:41:03.423365Z","iopub.status.idle":"2024-12-31T12:41:06.006342Z","shell.execute_reply.started":"2024-12-31T12:41:03.423328Z","shell.execute_reply":"2024-12-31T12:41:06.005297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# PCA\npca = PCA()\npca.fit(X_scaled)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:06.007316Z","iopub.execute_input":"2024-12-31T12:41:06.007819Z","iopub.status.idle":"2024-12-31T12:41:13.695959Z","shell.execute_reply.started":"2024-12-31T12:41:06.007774Z","shell.execute_reply":"2024-12-31T12:41:13.694932Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Variance explained \nexplained_variance_ratio = pca.explained_variance_ratio_\n\n# Number of components (explain 95% of the variance)\ncumulative_variance = np.cumsum(explained_variance_ratio)\nn_components = np.argmax(cumulative_variance >= 0.95) + 1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:13.697240Z","iopub.execute_input":"2024-12-31T12:41:13.697589Z","iopub.status.idle":"2024-12-31T12:41:13.702175Z","shell.execute_reply.started":"2024-12-31T12:41:13.697551Z","shell.execute_reply":"2024-12-31T12:41:13.701217Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=n_components)\npca_res = pca.fit_transform(X_scaled)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:13.702981Z","iopub.execute_input":"2024-12-31T12:41:13.703252Z","iopub.status.idle":"2024-12-31T12:41:37.537759Z","shell.execute_reply.started":"2024-12-31T12:41:13.703229Z","shell.execute_reply":"2024-12-31T12:41:37.536859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca_df = pd.DataFrame(pca_res, columns=[f\"pc{i}\" for i in range(1, pca_res.shape[1] + 1)])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:37.538647Z","iopub.execute_input":"2024-12-31T12:41:37.538977Z","iopub.status.idle":"2024-12-31T12:41:37.543592Z","shell.execute_reply.started":"2024-12-31T12:41:37.538942Z","shell.execute_reply":"2024-12-31T12:41:37.542659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca_df['responder_6'] = y.values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:37.544614Z","iopub.execute_input":"2024-12-31T12:41:37.544998Z","iopub.status.idle":"2024-12-31T12:41:37.564944Z","shell.execute_reply.started":"2024-12-31T12:41:37.544934Z","shell.execute_reply":"2024-12-31T12:41:37.563820Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_pca = pca_df.drop('responder_6', axis=1)\ny = pca_df['responder_6']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:37.566063Z","iopub.execute_input":"2024-12-31T12:41:37.566375Z","iopub.status.idle":"2024-12-31T12:41:38.613019Z","shell.execute_reply.started":"2024-12-31T12:41:37.566344Z","shell.execute_reply":"2024-12-31T12:41:38.612080Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Modeling","metadata":{}},{"cell_type":"code","source":"lags_ : pl.DataFrame | None = None\n\n\n\ndef predict(y_test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"Make a prediction.\"\"\"\n  \n    global lags_\n    if lags is not None:\n        lags_ = lags\n\n    # Replace this section with your own predictions\n    predictions = y_test.select(\n        'row_id',\n        pl.lit(0.0).alias('responder_6'),\n    )\n\n    if isinstance(predictions, pl.DataFrame):\n        assert predictions.columns == ['row_id', 'responder_6']\n    elif isinstance(predictions, pd.DataFrame):\n        assert (predictions.columns == ['row_id', 'responder_6']).all()\n    else:\n        raise TypeError('The predict function must return a DataFrame')\n    # Confirm has as many rows as the test data.\n    assert len(predictions) == len(y_test)\n\n\n\n    return predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:38.613810Z","iopub.execute_input":"2024-12-31T12:41:38.614092Z","iopub.status.idle":"2024-12-31T12:41:38.620457Z","shell.execute_reply.started":"2024-12-31T12:41:38.614069Z","shell.execute_reply":"2024-12-31T12:41:38.619480Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for train_index, test_index in tscv.split(X_pca):\n    X_train, X_test = X_pca.iloc[train_index], X_pca.iloc[test_index]\n    y_train, y_test = y.iloc[train_index], y.iloc[test_index]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:38.623691Z","iopub.execute_input":"2024-12-31T12:41:38.623970Z","iopub.status.idle":"2024-12-31T12:41:39.415763Z","shell.execute_reply.started":"2024-12-31T12:41:38.623944Z","shell.execute_reply":"2024-12-31T12:41:39.414635Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"MMscaler = MinMaxScaler()\nmodel = LinearRegression()\n\nX_train_scaled = MMscaler.fit_transform(X_train)\n\nX_test_scaled = MMscaler.transform(X_test)\n\nmodel.fit(X_train_scaled, y_train)\n\npredictions = model.predict(X_test_scaled)\n\n\nr2 = model.score(X_test_scaled, y_test)\n\nprint(f'R² Score: {r2:.4f}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:39.417039Z","iopub.execute_input":"2024-12-31T12:41:39.417407Z","iopub.status.idle":"2024-12-31T12:41:43.965877Z","shell.execute_reply.started":"2024-12-31T12:41:39.417375Z","shell.execute_reply":"2024-12-31T12:41:43.964088Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:43.966670Z","iopub.execute_input":"2024-12-31T12:41:43.967056Z","iopub.status.idle":"2024-12-31T12:41:43.979329Z","shell.execute_reply.started":"2024-12-31T12:41:43.967019Z","shell.execute_reply":"2024-12-31T12:41:43.977411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inference_server = kaggle_evaluation.jane_street_inference_server.JSInferenceServer(predict)\n\nif os.getenv('KAGGLE_IS_COMPETITION_RERUN'):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        (\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/test.parquet',\n            '/kaggle/input/jane-street-real-time-market-data-forecasting/lags.parquet',\n        )\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-31T12:41:43.980146Z","iopub.execute_input":"2024-12-31T12:41:43.980478Z","iopub.status.idle":"2024-12-31T12:41:44.313795Z","shell.execute_reply.started":"2024-12-31T12:41:43.980444Z","shell.execute_reply":"2024-12-31T12:41:44.312675Z"}},"outputs":[],"execution_count":null}]}