{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Forecasting Crypto Prices with the Informer and Flow Forecast\n\n[Flow Forecast is a open source deep learning for time series forecasting, classification, and anomaly built in PyTorch](https://github.com/AIStream-Peelout/flow-forecast). FF features state of the art time series forecasting models like the [Informer]() which we will use to forecast crypto prices. ","metadata":{}},{"cell_type":"code","source":"from datetime import datetime\nimport os\n!git clone https://github.com/AIStream-Peelout/flow-forecast.git -b add_minute_temporal\nos.chdir(\"flow-forecast\")\n!python setup.py develop","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-09T04:14:37.398786Z","iopub.execute_input":"2021-12-09T04:14:37.399327Z","iopub.status.idle":"2021-12-09T04:14:45.151875Z","shell.execute_reply.started":"2021-12-09T04:14:37.399249Z","shell.execute_reply":"2021-12-09T04:14:45.151005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reformatting the CSV\nBefore even get started we will need to reformat the CSV into FF format. For now due to memory constraints We will only forecast one asset at a time. I will put up another example later on Google Colaboratory later where will forecast all 11 assets at one time.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\ntrain_df = pd.read_csv(\"../../input/g-research-crypto-forecasting/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-12-09T04:14:45.154901Z","iopub.execute_input":"2021-12-09T04:14:45.155436Z","iopub.status.idle":"2021-12-09T04:15:41.922321Z","shell.execute_reply.started":"2021-12-09T04:14:45.15539Z","shell.execute_reply":"2021-12-09T04:15:41.921328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\ntrain_df[\"datetime\"] = pd.to_datetime(train_df[\"timestamp\"], unit='s')\ntrain_df.to_csv(\"i.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-12-09T04:15:41.923709Z","iopub.execute_input":"2021-12-09T04:15:41.92398Z","iopub.status.idle":"2021-12-09T04:21:50.230547Z","shell.execute_reply.started":"2021-12-09T04:15:41.923929Z","shell.execute_reply":"2021-12-09T04:21:50.229686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# g = train_df.groupby('Asset_ID').cumcount()\n# df = train_df.set_index(['timestamp', g]).unstack().sort_index(level=1, axis=1)\n# This ran out of memory ^ \nsingle_train = train_df[train_df[\"Asset_ID\"]==0]\nsingle_train.to_csv(\"asset_0.csv\")\nsingle_train.dropna()","metadata":{"execution":{"iopub.status.busy":"2021-12-09T04:21:50.232605Z","iopub.execute_input":"2021-12-09T04:21:50.232883Z","iopub.status.idle":"2021-12-09T04:22:19.09568Z","shell.execute_reply.started":"2021-12-09T04:21:50.23284Z","shell.execute_reply":"2021-12-09T04:22:19.09489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"single_train","metadata":{"execution":{"iopub.status.busy":"2021-12-09T04:22:19.096901Z","iopub.execute_input":"2021-12-09T04:22:19.097182Z","iopub.status.idle":"2021-12-09T04:22:19.11892Z","shell.execute_reply.started":"2021-12-09T04:22:19.097146Z","shell.execute_reply":"2021-12-09T04:22:19.11799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Defining the Configuration File\n\n##### The first thing to do to start any FF experiment is to define a FF configuration file. Here since we want to use the Informer model we have to supply the proper parameters to initialize it. \n- model_params: We will include \n","metadata":{}},{"cell_type":"code","source":"def make_config_file(csv_path, weight_path=None, pretrained=[]):\n  run = wandb.init(project=\"bitcoin_forecasts\")\n  wandb_config = run.config\n  the_wandb_c = run.config\n  the_config4 =  {\n    \"model_name\": \"Informer\",\n    \"use_decoder\": True,\n    \"model_type\": \"PyTorch\",\n    \"model_params\": {\n      \"n_time_series\":8,\n      \"dec_in\":8,\n      \"c_out\": 8,\n      \"seq_len\":wandb_config[\"forecast_history\"],\n      \"label_len\":30, \n      \"out_len\":24,\n      \"factor\":2\n    },\n    \"dataset_params\":\n    {  \"class\": \"TemporalLoader\",\n      \"temporal_feats\": [\"month\", \"day\", \"day_of_week\", \"hour\", \"minute\"],\n       \"no_scale\": True,\n       \"num_workers\":4,\n       \"pin_memory\": True,\n       \"training_path\": csv_path,\n       \"validation_path\": csv_path,\n       \"test_path\": csv_path,\n       \"batch_size\":wandb_config[\"batch_size\"],\n       \"forecast_test_len\":100,\n       \"forecast_history\":wandb_config[\"forecast_history\"],\n       \"forecast_length\":24,\n       \"scaler\": \"StandardScaler\",\n       \"train_start\":1000,\n       \"train_end\": 50000,\n       \"valid_start\":50001,\n       \"valid_end\": 57000,\n       \"sort_column\": \"datetime\",\n       \"test_start\": 57000,\n       \"test_end\":58000,\n       \"target_col\": [\"Target\"],\n       \"relevant_cols\": [\"Target\", \"Count\", \"Open\", \"High\", \"Low\", \"Close\", \"Volume\", \"VWAP\"], \n       \"interpolate\":{\n           \"method\":\"back_forward_generic\",\n           \"params\":{\n               \"relevant_columns\":[\"Target\", \"Count\", \"Open\", \"High\", \"Low\", \"Close\", \"Volume\", \"VWAP\"]\n           }\n\n       },\n     \"feature_param\":\n     {\n         \"datetime_params\":{\n            \"month\": \"numerical\",\n            \"day\": \"numerical\",\n            \"day_of_week\": \"numerical\",\n            \"hour\":\"numerical\",\n            \"minute\": \"numerical\"\n         }\n     }\n    },\n    \"training_params\":\n    {\n       \"criterion\":\"MSE\",\n       \"optimizer\": \"Adam\",\n       #\"criterion_params\":{\"baseline_method\":\"mean\"},\n    \"optim_params\":{\n       \"lr\": the_wandb_c[\"lr\"]\n    },\n       \"epochs\": 10,\n       \"batch_size\":wandb_config[\"batch_size\"]\n    },\n    \"early_stopping\":{\n        \"patience\":3\n    },\n    \"GCS\": False,\n    \"wandb\":False,\n    \"sweep\": True,\n    \"forward_params\":{},\n   \"metrics\":[\"MSE\"],\n   \"inference_params\":\n   {     \n         \"datetime_start\":\"2020-05-31\",\n          \"hours_to_forecast\":336, \n          \"num_prediction_samples\": 20,\n          \"test_csv_path\":csv_path,\n          \"decoder_params\":{\n            \"decoder_function\": \"simple_decode\", \n            \"unsqueeze_dim\": 1},\n          \"dataset_params\":{\n             \"file_path\": csv_path,\n             \"sort_column\": \"datetime\",\n             \"scaling\": \"StandardScaler\",\n             \"forecast_history\": wandb_config[\"forecast_history\"],\n             \"forecast_length\":24,\n             \"relevant_cols\": [\"Target\", \"Count\", \"Open\", \"High\", \"Low\", \"Close\", \"Volume\", \"VWAP\"],\n             \"target_col\": [\"Target\"],\n             \"interpolate_param\":{\n                 \"method\":\"back_forward_generic\",\n                 \"params\":{\"relevant_columns\":[\"Target\", \"Count\", \"Open\", \"High\", \"Low\", \"Close\", \"Volume\", \"VWAP\"]}\n                 }, \n            \"feature_params\":\n        {\n         \"datetime_params\":{\n            \"month\": \"numerical\",\n            \"day\": \"numerical\",\n            \"day_of_week\": \"numerical\",\n            \"hour\":\"numerical\",\n             \"minute\": \"numerical\"\n         \n     }\n             }\n          }\n          } \n    }\n\n      \n  if weight_path:\n    the_config4[\"weight_path\"] = weight_path\n  wandb.config.update(the_config4)\n  print(\"config made\")\n  return the_config4\n  \nwandb_sweep_config_full = {\n  \"name\": \"Default sweep\",\n  \"method\": \"grid\",\n  \"parameters\": {\n        \"batch_size\": {\n            \"values\": [100, 250, 400]\n        },\n        \"lr\":{\n            \"values\":[0.01, 0.00001, 0.00001, 0.00005]\n        },\n        \"forecast_history\":{\n            \"values\":[48, 96]\n        }\n        \n}}","metadata":{"execution":{"iopub.status.busy":"2021-12-09T04:22:19.120549Z","iopub.execute_input":"2021-12-09T04:22:19.120949Z","iopub.status.idle":"2021-12-09T04:22:19.139914Z","shell.execute_reply.started":"2021-12-09T04:22:19.120911Z","shell.execute_reply":"2021-12-09T04:22:19.139171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training the Forecasting Model","metadata":{}},{"cell_type":"code","source":"import wandb\nfrom flood_forecast.trainer import train_function\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nos.environ[\"WANDB_API_KEY\"] = user_secrets.get_secret(\"WANDB_KEY\")\nsweep_full = wandb.sweep(wandb_sweep_config_full, project=\"bitcoin_forecasts\")\nwandb.agent(sweep_full, lambda:train_function(\"PyTorch\", make_config_file(\"asset_0.csv\")))\n  ","metadata":{"execution":{"iopub.status.busy":"2021-12-09T04:22:19.141349Z","iopub.execute_input":"2021-12-09T04:22:19.141866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}