{"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":"# Time Series Deep Learning\n- TFT\n- NBEATS\n- PatchTST","metadata":{}},{"cell_type":"code","source":"!pip install -q neuralforecast\n!pip show neuralforecast\n\nfrom neuralforecast.models import PatchTST\nfrom neuralforecast import NeuralForecast\nfrom neuralforecast.models import MLP\nfrom neuralforecast.losses.pytorch import MAE\nfrom neuralforecast.tsdataset import TimeSeriesDataset\n\nimport pytorch_lightning as pl\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:16:29.231734Z","iopub.execute_input":"2023-06-15T13:16:29.232116Z","iopub.status.idle":"2023-06-15T13:16:55.223908Z","shell.execute_reply.started":"2023-06-15T13:16:29.232064Z","shell.execute_reply":"2023-06-15T13:16:55.222811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Toy Example","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport pytorch_lightning as pl\nimport matplotlib.pyplot as plt\n\nfrom neuralforecast import NeuralForecast\nfrom neuralforecast.models import MLP\nfrom neuralforecast.losses.pytorch import MQLoss, DistributionLoss\nfrom neuralforecast.tsdataset import TimeSeriesDataset\nfrom neuralforecast.utils import AirPassengers, AirPassengersPanel, AirPassengersStatic, augment_calendar_df\n\nAirPassengersPanel, calendar_cols = augment_calendar_df(df=AirPassengersPanel, freq='M')\n\nY_train_df = AirPassengersPanel[AirPassengersPanel.ds<AirPassengersPanel['ds'].values[-12]] # 132 train\nY_test_df = AirPassengersPanel[AirPassengersPanel.ds>=AirPassengersPanel['ds'].values[-12]].reset_index(drop=True) # 12 test","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:16:55.228070Z","iopub.execute_input":"2023-06-15T13:16:55.228384Z","iopub.status.idle":"2023-06-15T13:16:55.272898Z","shell.execute_reply.started":"2023-06-15T13:16:55.228354Z","shell.execute_reply":"2023-06-15T13:16:55.272012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train_df","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:16:55.274287Z","iopub.execute_input":"2023-06-15T13:16:55.275215Z","iopub.status.idle":"2023-06-15T13:16:55.299079Z","shell.execute_reply.started":"2023-06-15T13:16:55.275171Z","shell.execute_reply":"2023-06-15T13:16:55.298106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_test_df","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:16:55.301911Z","iopub.execute_input":"2023-06-15T13:16:55.302674Z","iopub.status.idle":"2023-06-15T13:16:55.320460Z","shell.execute_reply.started":"2023-06-15T13:16:55.302649Z","shell.execute_reply":"2023-06-15T13:16:55.319421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AirPassengersStatic.head(2)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:16:55.321873Z","iopub.execute_input":"2023-06-15T13:16:55.322290Z","iopub.status.idle":"2023-06-15T13:16:55.335209Z","shell.execute_reply.started":"2023-06-15T13:16:55.322259Z","shell.execute_reply":"2023-06-15T13:16:55.333756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = PatchTST(h=12,\n                 input_size=104,\n                 patch_len=24,\n                 stride=24,\n                 revin=False,\n                 hidden_size=16,\n                 n_heads=4,\n                 scaler_type='robust',\n                 loss=DistributionLoss(distribution='StudentT', level=[80, 90]),\n                 #loss=MAE(),\n                 learning_rate=1e-3,\n                 max_steps=500,\n                 val_check_steps=50,\n                 early_stop_patience_steps=2)\n\nnf = NeuralForecast(\n    models=[model],\n    freq='M'\n)\n# nf.fit(df=Y_train_df, static_df=AirPassengersStatic, val_size=12)\nnf.fit(df=Y_train_df, val_size=12)\nforecasts = nf.predict(futr_df=Y_test_df)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:16:55.336796Z","iopub.execute_input":"2023-06-15T13:16:55.337174Z","iopub.status.idle":"2023-06-15T13:17:09.989729Z","shell.execute_reply.started":"2023-06-15T13:16:55.337143Z","shell.execute_reply":"2023-06-15T13:17:09.988817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nplot_df = pd.concat([Y_train_df, plot_df])\n\nif model.loss.is_distribution_output:\n    plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1)\n    plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True')\n    plt.plot(plot_df['ds'], plot_df['PatchTST-median'], c='blue', label='median')\n    plt.fill_between(x=plot_df['ds'][-12:], \n                    y1=plot_df['PatchTST-lo-90'][-12:].values, \n                    y2=plot_df['PatchTST-hi-90'][-12:].values,\n                    alpha=0.4, label='level 90')\n    plt.grid()\n    plt.legend()\n    plt.plot()\nelse:\n    plot_df = plot_df[plot_df.unique_id=='Airline1'].drop('unique_id', axis=1)\n    plt.plot(plot_df['ds'], plot_df['y'], c='black', label='True')\n    plt.plot(plot_df['ds'], plot_df['PatchTST'], c='blue', label='Forecast')\n    plt.legend()\n    plt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:17:09.991196Z","iopub.execute_input":"2023-06-15T13:17:09.992189Z","iopub.status.idle":"2023-06-15T13:17:10.401971Z","shell.execute_reply.started":"2023-06-15T13:17:09.992155Z","shell.execute_reply":"2023-06-15T13:17:10.401033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"try with our crypto data","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n# import gresearch_crypto\nimport time\nimport datetime\n\nimport pickle\nimport gc\n\nfrom tqdm import tqdm\n\nTRAIN_CSV = '/kaggle/input/g-research-crypto-forecasting/train.csv'\nASSET_DETAILS_CSV = '/kaggle/input/g-research-crypto-forecasting/asset_details.csv'\n\npd.set_option('display.max_rows', 6)\npd.set_option('display.max_columns', 350)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:17:10.403542Z","iopub.execute_input":"2023-06-15T13:17:10.404170Z","iopub.status.idle":"2023-06-15T13:17:10.738272Z","shell.execute_reply.started":"2023-06-15T13:17:10.404135Z","shell.execute_reply":"2023-06-15T13:17:10.737307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reduce_mem_usage(df):\n    \"\"\" iterate through all the columns of a dataframe and modify the data type\n        to reduce memory usage.        \n    \"\"\"\n    start_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n\n    for col in df.columns:\n        col_type = df[col].dtype\n\n        if col_type != object:\n            c_min = df[col].min()\n            c_max = df[col].max()\n            if str(col_type)[:3] == 'int':\n                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n                    df[col] = df[col].astype(np.int8)\n                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n                    df[col] = df[col].astype(np.int16)\n                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n                    df[col] = df[col].astype(np.int32)\n                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n                    df[col] = df[col].astype(np.int64)  \n            else:\n                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n                    df[col] = df[col].astype(np.float16)\n                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n                    df[col] = df[col].astype(np.float32)\n                else:\n                    df[col] = df[col].astype(np.float64)\n        else:\n            df[col] = df[col].astype('category')\n\n    end_mem = df.memory_usage().sum() / 1024**2\n    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:17:10.739602Z","iopub.execute_input":"2023-06-15T13:17:10.740411Z","iopub.status.idle":"2023-06-15T13:17:10.754176Z","shell.execute_reply.started":"2023-06-15T13:17:10.740378Z","shell.execute_reply":"2023-06-15T13:17:10.753007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_asset_details = pd.read_csv(ASSET_DETAILS_CSV).sort_values(\"Asset_ID\")\ndf_asset_details[df_asset_details.Asset_ID.isin([1,6,4])]","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:17:10.757764Z","iopub.execute_input":"2023-06-15T13:17:10.758044Z","iopub.status.idle":"2023-06-15T13:17:10.784575Z","shell.execute_reply.started":"2023-06-15T13:17:10.758020Z","shell.execute_reply":"2023-06-15T13:17:10.783567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assets=[1,6]","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:17:10.786047Z","iopub.execute_input":"2023-06-15T13:17:10.786479Z","iopub.status.idle":"2023-06-15T13:17:10.791142Z","shell.execute_reply.started":"2023-06-15T13:17:10.786444Z","shell.execute_reply":"2023-06-15T13:17:10.790038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ndf_train = pd.read_csv(TRAIN_CSV)\n\n\ndf_train = reduce_mem_usage(df_train)\ndf_train = df_train[df_train.Asset_ID.isin(assets)]\n\ndf_train.rename({'Asset_ID':'unique_id','timestamp':'ds','Target':'y'},axis=1,inplace=True)\ndf_train.ds = pd.to_datetime(df_train.ds, unit='s')\ndf_train","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:17:10.792913Z","iopub.execute_input":"2023-06-15T13:17:10.793249Z","iopub.status.idle":"2023-06-15T13:18:22.321797Z","shell.execute_reply.started":"2023-06-15T13:17:10.793220Z","shell.execute_reply":"2023-06-15T13:18:22.320833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = True\nif sample: df_train = df_train[(df_train.ds<='2018-05-31 23:59:00')&(df_train.y.notnull())]","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:18:22.323212Z","iopub.execute_input":"2023-06-15T13:18:22.323808Z","iopub.status.idle":"2023-06-15T13:18:22.372131Z","shell.execute_reply.started":"2023-06-15T13:18:22.323774Z","shell.execute_reply":"2023-06-15T13:18:22.370986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:18:22.373519Z","iopub.execute_input":"2023-06-15T13:18:22.373990Z","iopub.status.idle":"2023-06-15T13:18:22.400127Z","shell.execute_reply.started":"2023-06-15T13:18:22.373954Z","shell.execute_reply":"2023-06-15T13:18:22.399120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train_df = df_train[ df_train.ds<= '2018-05-30 23:47:00'] \nY_test_df  = df_train[(df_train.ds> '2018-05-30 23:47:00')&(df_train.ds<=  '2018-05-30 23:59:00')].reset_index(drop=True) # 12 mins\nY_eval_df  = df_train[(df_train.ds> '2018-05-30 23:59:00')].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:22:14.416074Z","iopub.execute_input":"2023-06-15T13:22:14.416506Z","iopub.status.idle":"2023-06-15T13:22:14.451871Z","shell.execute_reply.started":"2023-06-15T13:22:14.416473Z","shell.execute_reply":"2023-06-15T13:22:14.450901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(Y_train_df), len(Y_test_df), len(Y_eval_df)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:22:14.686849Z","iopub.execute_input":"2023-06-15T13:22:14.687226Z","iopub.status.idle":"2023-06-15T13:22:14.693783Z","shell.execute_reply.started":"2023-06-15T13:22:14.687193Z","shell.execute_reply":"2023-06-15T13:22:14.692888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del df_train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:22:14.935580Z","iopub.execute_input":"2023-06-15T13:22:14.936687Z","iopub.status.idle":"2023-06-15T13:22:15.390064Z","shell.execute_reply.started":"2023-06-15T13:22:14.936642Z","shell.execute_reply":"2023-06-15T13:22:15.389013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PatchTST parameters\n- **h**: forecast horizon or how far to forecast into the future\n- **input_size**: length of input sequence\n- **patch_len**: length of patch\n- **stride**: how many steps the patch move along the time series to create a new window\n- **hidden_size**: units of embeddings and encoders\n- **n_heads**: number of attention heads\n- **scaler_type**: type of scaler for normalization","metadata":{}},{"cell_type":"code","source":"model = PatchTST(h=12,\n                 input_size=104,\n                 patch_len=24,\n                 stride=24,\n                 hidden_size=16,\n                 n_heads=4,\n                 scaler_type='robust',\n                 loss=MAE(),\n                 learning_rate=1e-3,\n                 max_steps=100,\n                 val_check_steps=50,\n                 early_stop_patience_steps=2)\n\nnf = NeuralForecast(\n    models=[model],\n    freq='1min'\n)\n\n#patchtst doesnt support exogenous variables yet\nnf.fit(df=Y_train_df[['ds','unique_id','y']], val_size=12)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:22:17.837517Z","iopub.execute_input":"2023-06-15T13:22:17.837878Z","iopub.status.idle":"2023-06-15T13:22:23.058968Z","shell.execute_reply.started":"2023-06-15T13:22:17.837848Z","shell.execute_reply":"2023-06-15T13:22:23.058008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forecasts = nf.predict(futr_df=Y_test_df[['ds','unique_id','y']])\n\nY_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nplot_df = pd.concat([Y_train_df, plot_df])\n\n\nplot_df = plot_df[plot_df.unique_id==1].drop('unique_id', axis=1)[-50:]  \nplt.plot(plot_df['ds'], plot_df['y'], c='black', label='True')\nplt.plot(plot_df['ds'], plot_df['PatchTST'], c='blue', label='Forecast')\nplt.legend()\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:22:23.060992Z","iopub.execute_input":"2023-06-15T13:22:23.061445Z","iopub.status.idle":"2023-06-15T13:22:23.552872Z","shell.execute_reply.started":"2023-06-15T13:22:23.061410Z","shell.execute_reply":"2023-06-15T13:22:23.551981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nplot_df = pd.concat([Y_train_df, plot_df])\n\n\nplot_df = plot_df[plot_df.unique_id==6].drop('unique_id', axis=1)[-50:]  \nplt.plot(plot_df['ds'], plot_df['y'], c='black', label='True')\nplt.plot(plot_df['ds'], plot_df['PatchTST'], c='blue', label='Forecast')\nplt.legend()\nplt.title('Ethereum forecast vs actuals for 12 mins')\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:22:23.554395Z","iopub.execute_input":"2023-06-15T13:22:23.555447Z","iopub.status.idle":"2023-06-15T13:22:23.954071Z","shell.execute_reply.started":"2023-06-15T13:22:23.555411Z","shell.execute_reply":"2023-06-15T13:22:23.953121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## eval","metadata":{}},{"cell_type":"code","source":"plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nf\"MAE for PatchTST is {abs(plot_df.y - plot_df.PatchTST).mean():.4f} for last 12 timesteps\"","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:22:38.507365Z","iopub.execute_input":"2023-06-15T13:22:38.507723Z","iopub.status.idle":"2023-06-15T13:22:38.516629Z","shell.execute_reply.started":"2023-06-15T13:22:38.507694Z","shell.execute_reply":"2023-06-15T13:22:38.515554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forecast_eval = nf.predict(df=pd.concat([Y_train_df,Y_test_df]),futr_df=Y_eval_df[['ds','unique_id','y']])\n\nY_hat_df = forecast_eval.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_eval_df, Y_hat_df], axis=1)\n\nf\"MAE for PatchTST is {abs(plot_df.y - plot_df.PatchTST).mean():.4f} for eval timesteps\"","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:05.981410Z","iopub.execute_input":"2023-06-15T13:53:05.981771Z","iopub.status.idle":"2023-06-15T13:53:06.369349Z","shell.execute_reply.started":"2023-06-15T13:53:05.981740Z","shell.execute_reply":"2023-06-15T13:53:06.368316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del model, nf\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:39.986918Z","iopub.execute_input":"2023-06-15T13:53:39.987292Z","iopub.status.idle":"2023-06-15T13:53:40.663137Z","shell.execute_reply.started":"2023-06-15T13:53:39.987261Z","shell.execute_reply":"2023-06-15T13:53:40.662034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Try NBEATS","metadata":{}},{"cell_type":"code","source":"from neuralforecast.models import NBEATS","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:44.179147Z","iopub.execute_input":"2023-06-15T13:53:44.179872Z","iopub.status.idle":"2023-06-15T13:53:44.184512Z","shell.execute_reply.started":"2023-06-15T13:53:44.179836Z","shell.execute_reply":"2023-06-15T13:53:44.183437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = NBEATS(h=12, input_size=104,\n               loss=MAE(),\n               stack_types = ['identity', 'trend', 'seasonality'],\n               max_steps=100,\n               val_check_steps=50,\n               early_stop_patience_steps=2)\n\nnf = NeuralForecast(\n    models=[model],\n    freq='1min'\n)\n\nnf.fit(df=Y_train_df[['ds','unique_id','y']], val_size=12)\nforecasts = nf.predict(futr_df=Y_test_df[['ds','unique_id','y']])","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:44.389158Z","iopub.execute_input":"2023-06-15T13:53:44.390287Z","iopub.status.idle":"2023-06-15T13:53:49.100364Z","shell.execute_reply.started":"2023-06-15T13:53:44.390243Z","shell.execute_reply":"2023-06-15T13:53:49.099456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nplot_df = pd.concat([Y_train_df, plot_df])\n\nplot_df = plot_df[plot_df.unique_id==1].drop('unique_id', axis=1)[-50:]  \nplt.plot(plot_df['ds'], plot_df['y'], c='black', label='True')\nplt.plot(plot_df['ds'], plot_df['NBEATS'], c='blue', label='Forecast')\nplt.title('NBEATS BTC forecast vs actuals for 12 mins')\nplt.legend()\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:49.103360Z","iopub.execute_input":"2023-06-15T13:53:49.104353Z","iopub.status.idle":"2023-06-15T13:53:49.528693Z","shell.execute_reply.started":"2023-06-15T13:53:49.104319Z","shell.execute_reply":"2023-06-15T13:53:49.527685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nplot_df = pd.concat([Y_train_df, plot_df])\n\nplot_df = plot_df[plot_df.unique_id==6].drop('unique_id', axis=1)[-50:]  \nplt.plot(plot_df['ds'], plot_df['y'], c='black', label='True')\nplt.plot(plot_df['ds'], plot_df['NBEATS'], c='blue', label='Forecast')\nplt.title('NBEATS Ethereum forecast vs actuals for 12 mins')\nplt.legend()\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:49.530328Z","iopub.execute_input":"2023-06-15T13:53:49.530715Z","iopub.status.idle":"2023-06-15T13:53:49.912010Z","shell.execute_reply.started":"2023-06-15T13:53:49.530681Z","shell.execute_reply":"2023-06-15T13:53:49.911016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## eval","metadata":{}},{"cell_type":"code","source":"plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nf\"MAE for NBEATS is {abs(plot_df.y - plot_df.NBEATS).mean():.4f}\"","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:49.915251Z","iopub.execute_input":"2023-06-15T13:53:49.915653Z","iopub.status.idle":"2023-06-15T13:53:49.925674Z","shell.execute_reply.started":"2023-06-15T13:53:49.915615Z","shell.execute_reply":"2023-06-15T13:53:49.924438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forecast_eval = nf.predict(df=pd.concat([Y_train_df,Y_test_df]),futr_df=Y_eval_df[['ds','unique_id','y']])\n\nY_hat_df = forecast_eval.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_eval_df, Y_hat_df], axis=1)\n\nf\"MAE for NBEATS is {abs(plot_df.y - plot_df.NBEATS).mean():.4f} for eval timesteps\"","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:49.927378Z","iopub.execute_input":"2023-06-15T13:53:49.927828Z","iopub.status.idle":"2023-06-15T13:53:50.295826Z","shell.execute_reply.started":"2023-06-15T13:53:49.927794Z","shell.execute_reply":"2023-06-15T13:53:50.295167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del model, nf\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:50.296887Z","iopub.execute_input":"2023-06-15T13:53:50.297184Z","iopub.status.idle":"2023-06-15T13:53:50.763896Z","shell.execute_reply.started":"2023-06-15T13:53:50.297159Z","shell.execute_reply":"2023-06-15T13:53:50.762824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Try TFT","metadata":{}},{"cell_type":"code","source":"from neuralforecast.models import TFT","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:55.219283Z","iopub.execute_input":"2023-06-15T13:53:55.219662Z","iopub.status.idle":"2023-06-15T13:53:55.224422Z","shell.execute_reply.started":"2023-06-15T13:53:55.219630Z","shell.execute_reply":"2023-06-15T13:53:55.223301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nf = NeuralForecast(\n    models=[TFT(h=12, input_size=104,\n                loss=MAE(),\n                hist_exog_list=['Count','VWAP','Close','Open','Volume','High','Low'],\n                max_steps=100,\n                val_check_steps=50,\n                early_stop_patience_steps=1,\n                scaler_type='robust'),\n    ],\n    freq='1min'\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:55.501975Z","iopub.execute_input":"2023-06-15T13:53:55.502660Z","iopub.status.idle":"2023-06-15T13:53:55.537429Z","shell.execute_reply.started":"2023-06-15T13:53:55.502626Z","shell.execute_reply":"2023-06-15T13:53:55.536528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nf.fit(df=Y_train_df, val_size=12)\nforecasts = nf.predict(futr_df=Y_test_df)","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:53:57.733376Z","iopub.execute_input":"2023-06-15T13:53:57.733745Z","iopub.status.idle":"2023-06-15T13:54:44.586399Z","shell.execute_reply.started":"2023-06-15T13:53:57.733717Z","shell.execute_reply":"2023-06-15T13:54:44.585491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nplot_df = pd.concat([Y_train_df, plot_df])\n\n\nplot_df = plot_df[plot_df.unique_id==1].drop('unique_id', axis=1)[-50:]  \nplt.plot(plot_df['ds'], plot_df['y'], c='black', label='True')\nplt.plot(plot_df['ds'], plot_df['TFT'], c='blue', label='Forecast')\nplt.title('TFT BTC forecast vs actuals for 12 mins')\nplt.legend()\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:54:44.588506Z","iopub.execute_input":"2023-06-15T13:54:44.588878Z","iopub.status.idle":"2023-06-15T13:54:45.040000Z","shell.execute_reply.started":"2023-06-15T13:54:44.588844Z","shell.execute_reply":"2023-06-15T13:54:45.039040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_hat_df = forecasts.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nplot_df = pd.concat([Y_train_df, plot_df])\n\nplot_df = plot_df[plot_df.unique_id==6].drop('unique_id', axis=1)[-50:]  \nplt.plot(plot_df['ds'], plot_df['y'], c='black', label='True')\nplt.plot(plot_df['ds'], plot_df['TFT'], c='blue', label='Forecast')\nplt.title('TFT Ethereum forecast vs actuals for 12 mins')\nplt.legend()\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:54:45.041408Z","iopub.execute_input":"2023-06-15T13:54:45.041849Z","iopub.status.idle":"2023-06-15T13:54:45.415051Z","shell.execute_reply.started":"2023-06-15T13:54:45.041814Z","shell.execute_reply":"2023-06-15T13:54:45.414070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## eval","metadata":{}},{"cell_type":"code","source":"plot_df = pd.concat([Y_test_df, Y_hat_df], axis=1)\nf\"MAE for TFT is {abs(plot_df.y - plot_df.TFT).mean():.4f}\"","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:54:45.417473Z","iopub.execute_input":"2023-06-15T13:54:45.417927Z","iopub.status.idle":"2023-06-15T13:54:45.427432Z","shell.execute_reply.started":"2023-06-15T13:54:45.417892Z","shell.execute_reply":"2023-06-15T13:54:45.426387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forecast_eval = nf.predict(df=pd.concat([Y_train_df,Y_test_df]),futr_df=Y_eval_df)\n\nY_hat_df = forecast_eval.reset_index(drop=False).drop(columns=['unique_id','ds'])\nplot_df = pd.concat([Y_eval_df, Y_hat_df], axis=1)\n\nf\"MAE for TFT is {abs(plot_df.y - plot_df.TFT).mean():.4f} for eval timesteps\"","metadata":{"execution":{"iopub.status.busy":"2023-06-15T13:55:27.363768Z","iopub.execute_input":"2023-06-15T13:55:27.364170Z","iopub.status.idle":"2023-06-15T13:55:27.732271Z","shell.execute_reply.started":"2023-06-15T13:55:27.364140Z","shell.execute_reply":"2023-06-15T13:55:27.731399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exercise\nTry improving the MAE for the eval set","metadata":{}},{"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":[]}]}