{"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.10.14"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":84493,"databundleVersionId":9871156,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":4.669361,"end_time":"2024-10-10T13:05:46.686069","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-10-10T13:05:42.016708","version":"2.6.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\nimport pandas as pd\nimport polars as pl\n\nimport kaggle_evaluation.jane_street_inference_server","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2024-12-15T08:55:52.415886Z","iopub.execute_input":"2024-12-15T08:55:52.416281Z","iopub.status.idle":"2024-12-15T08:55:53.293666Z","shell.execute_reply.started":"2024-12-15T08:55:52.416234Z","shell.execute_reply":"2024-12-15T08:55:53.292332Z"},"papermill":{"duration":1.223703,"end_time":"2024-10-10T13:05:45.825911","exception":false,"start_time":"2024-10-10T13:05:44.602208","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"The evaluation API requires that you set up a server which will respond to inference requests. We have already defined the server; you just need write the predict function. When we evaluate your submission on the hidden test set the client defined in `jane_street_gateway` will run in a different container with direct access to the hidden test set and hand off the data timestep by timestep.\n\n\n\nYour code will always have access to the published copies of the files.","metadata":{"papermill":{"duration":0.002051,"end_time":"2024-10-10T13:05:45.83073","exception":false,"start_time":"2024-10-10T13:05:45.828679","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def R_mol(y_true,y_pred,w=1):\n    return w*(y_true-y_pred)*(y_true-y_pred);","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:55:53.296233Z","iopub.execute_input":"2024-12-15T08:55:53.296690Z","iopub.status.idle":"2024-12-15T08:55:53.301604Z","shell.execute_reply.started":"2024-12-15T08:55:53.296655Z","shell.execute_reply":"2024-12-15T08:55:53.300472Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def R_den(y_true,w=1):\n    return w*y_true;","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:55:53.303031Z","iopub.execute_input":"2024-12-15T08:55:53.303358Z","iopub.status.idle":"2024-12-15T08:55:53.316146Z","shell.execute_reply.started":"2024-12-15T08:55:53.303306Z","shell.execute_reply":"2024-12-15T08:55:53.314983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\npath = \"/kaggle/input/jane-street-real-time-market-data-forecasting\"\nsamples = [] \n\n# Load a data from each file:\nr = range(10)\nfor i in r:\n    file_path = f\"{path}/train.parquet/partition_id={i}/part-0.parquet\"\n    part = pd.read_parquet(file_path)\n    part = part[['date_id','time_id','symbol_id','weight','responder_6']]\n    samples.append(part)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:55:53.317985Z","iopub.execute_input":"2024-12-15T08:55:53.318322Z","iopub.status.idle":"2024-12-15T08:57:23.843045Z","shell.execute_reply.started":"2024-12-15T08:55:53.318291Z","shell.execute_reply":"2024-12-15T08:57:23.841634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_df = pd.concat(samples, ignore_index=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:57:23.845458Z","iopub.execute_input":"2024-12-15T08:57:23.845819Z","iopub.status.idle":"2024-12-15T08:57:24.140214Z","shell.execute_reply.started":"2024-12-15T08:57:23.845786Z","shell.execute_reply":"2024-12-15T08:57:24.139193Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_offline = sample_df[sample_df['date_id']<1698-180]\ntest_offline = sample_df[sample_df['date_id']>=1698-180].reset_index(drop=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:57:24.141586Z","iopub.execute_input":"2024-12-15T08:57:24.141940Z","iopub.status.idle":"2024-12-15T08:57:26.064330Z","shell.execute_reply.started":"2024-12-15T08:57:24.141909Z","shell.execute_reply":"2024-12-15T08:57:26.063249Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_offline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:57:26.065505Z","iopub.execute_input":"2024-12-15T08:57:26.065842Z","iopub.status.idle":"2024-12-15T08:57:26.085597Z","shell.execute_reply.started":"2024-12-15T08:57:26.065811Z","shell.execute_reply":"2024-12-15T08:57:26.084570Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_offline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:57:26.086735Z","iopub.execute_input":"2024-12-15T08:57:26.087057Z","iopub.status.idle":"2024-12-15T08:57:26.099780Z","shell.execute_reply.started":"2024-12-15T08:57:26.087027Z","shell.execute_reply":"2024-12-15T08:57:26.098547Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 取中位数做预测","metadata":{}},{"cell_type":"code","source":"train = train_offline[-180:]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:57:26.101046Z","iopub.execute_input":"2024-12-15T08:57:26.101446Z","iopub.status.idle":"2024-12-15T08:57:26.114307Z","shell.execute_reply.started":"2024-12-15T08:57:26.101389Z","shell.execute_reply":"2024-12-15T08:57:26.113120Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_offline[-180:]['responder_6'].median()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T09:14:02.216430Z","iopub.execute_input":"2024-12-15T09:14:02.216882Z","iopub.status.idle":"2024-12-15T09:14:02.225563Z","shell.execute_reply.started":"2024-12-15T09:14:02.216830Z","shell.execute_reply":"2024-12-15T09:14:02.224414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['responder_6'].median()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:57:26.115617Z","iopub.execute_input":"2024-12-15T08:57:26.116045Z","iopub.status.idle":"2024-12-15T08:57:26.130164Z","shell.execute_reply.started":"2024-12-15T08:57:26.115993Z","shell.execute_reply":"2024-12-15T08:57:26.129015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_offline['responder_6_pred'] = train['responder_6'].median()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:57:26.131570Z","iopub.execute_input":"2024-12-15T08:57:26.131991Z","iopub.status.idle":"2024-12-15T08:57:26.145681Z","shell.execute_reply.started":"2024-12-15T08:57:26.131934Z","shell.execute_reply":"2024-12-15T08:57:26.144325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_offline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:57:26.146977Z","iopub.execute_input":"2024-12-15T08:57:26.147345Z","iopub.status.idle":"2024-12-15T08:57:26.161571Z","shell.execute_reply.started":"2024-12-15T08:57:26.147313Z","shell.execute_reply":"2024-12-15T08:57:26.160612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_mol = test_offline.apply(lambda row : R_mol(row['responder_6'],row['responder_6_pred']),axis=1).sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:57:26.163036Z","iopub.execute_input":"2024-12-15T08:57:26.163440Z","iopub.status.idle":"2024-12-15T08:58:50.685413Z","shell.execute_reply.started":"2024-12-15T08:57:26.163396Z","shell.execute_reply":"2024-12-15T08:58:50.683744Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_den = test_offline.apply(lambda row : R_den(row['responder_6']),axis=1).sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:58:50.691693Z","iopub.execute_input":"2024-12-15T08:58:50.692135Z","iopub.status.idle":"2024-12-15T08:59:53.098680Z","shell.execute_reply.started":"2024-12-15T08:58:50.692086Z","shell.execute_reply":"2024-12-15T08:59:53.097570Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result = 1-test_mol/test_den","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T08:59:53.100146Z","iopub.execute_input":"2024-12-15T08:59:53.100581Z","iopub.status.idle":"2024-12-15T08:59:53.106239Z","shell.execute_reply.started":"2024-12-15T08:59:53.100505Z","shell.execute_reply":"2024-12-15T08:59:53.105076Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T09:00:02.537568Z","iopub.execute_input":"2024-12-15T09:00:02.538556Z","iopub.status.idle":"2024-12-15T09:00:02.547746Z","shell.execute_reply.started":"2024-12-15T09:00:02.538475Z","shell.execute_reply":"2024-12-15T09:00:02.546552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"lags_ : pl.DataFrame | None = None\n\n\n# Replace this function with your inference code.\n# You can return either a Pandas or Polars dataframe, though Polars is recommended.\n# Each batch of predictions (except the very first) must be returned within 1 minute of the batch features being provided.\ndef predict(test: pl.DataFrame, lags: pl.DataFrame | None) -> pl.DataFrame | pd.DataFrame:\n    \"\"\"Make a prediction.\"\"\"\n    # All the responders from the previous day are passed in at time_id == 0. We save them in a global variable for access at every time_id.\n    # Use them as extra features, if you like.\n    global lags_\n    if lags is not None:\n        lags_ = lags\n    # Replace this section with your own predictions\n    predictions = test.select(\n        'row_id',\n        pl.lit(sample_df[-180:]['responder_6'].median()).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(test)\n\n    return predictions","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2024-12-15T09:20:25.832319Z","iopub.execute_input":"2024-12-15T09:20:25.833787Z","iopub.status.idle":"2024-12-15T09:20:25.844535Z","shell.execute_reply.started":"2024-12-15T09:20:25.833689Z","shell.execute_reply":"2024-12-15T09:20:25.843304Z"},"papermill":{"duration":0.015917,"end_time":"2024-10-10T13:05:45.848958","exception":false,"start_time":"2024-10-10T13:05:45.833041","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"When your notebook is run on the hidden test set, inference_server.serve must be called within 15 minutes of the notebook starting or the gateway will throw an error. If you need more than 15 minutes to load your model you can do so during the very first `predict` call, which does not have the usual 1 minute response deadline.","metadata":{"papermill":{"duration":0.00196,"end_time":"2024-10-10T13:05:45.853279","exception":false,"start_time":"2024-10-10T13:05:45.851319","status":"completed"},"tags":[]}},{"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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2024-12-15T09:20:29.810620Z","iopub.execute_input":"2024-12-15T09:20:29.811030Z","iopub.status.idle":"2024-12-15T09:20:29.964757Z","shell.execute_reply.started":"2024-12-15T09:20:29.810993Z","shell.execute_reply":"2024-12-15T09:20:29.963736Z"},"papermill":{"duration":0.308219,"end_time":"2024-10-10T13:05:46.163573","exception":false,"start_time":"2024-10-10T13:05:45.855354","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}