{"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":"code","source":"import numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\n#plt.style.use('fivethirtyeight')\nplt.rcParams.update({'font.size': 12})\n#plt.rcParams[\"figure.figsize\"] = (12, 4)\nfrom datetime import date","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-06-02T03:03:48.589613Z","iopub.execute_input":"2022-06-02T03:03:48.590161Z","iopub.status.idle":"2022-06-02T03:03:48.632552Z","shell.execute_reply.started":"2022-06-02T03:03:48.590066Z","shell.execute_reply":"2022-06-02T03:03:48.631453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Install GluonTS","metadata":{}},{"cell_type":"code","source":"!pip install -q gluonts\n!pip install anvil-uplink","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-06-02T03:03:48.66621Z","iopub.execute_input":"2022-06-02T03:03:48.666676Z","iopub.status.idle":"2022-06-02T03:04:30.582822Z","shell.execute_reply.started":"2022-06-02T03:03:48.666646Z","shell.execute_reply":"2022-06-02T03:04:30.581669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For our data we shall be using the dataset provided by the kaggle [G-Research Crypto Forecasting](https://www.kaggle.com/c/g-research-crypto-forecasting) competition. We shall extract the data corresponding to the Bitcoin cryptocurrency. From this Bitcoin data we shall use only one datapoint each day, arbitrarily chosen to be the **High** value at 9:00 a.m.","metadata":{}},{"cell_type":"code","source":"dfs = []\ntrain = pd.read_csv(\"../input/fuck-me/BTC.csv\")\n# extract the data corresponding to Bitcoin (Asset_ID = 1)\nbitcoin = train.query(\"Asset_ID == 1\").reset_index(drop = True)\nbitcoin['timestamp'] = pd.to_datetime(bitcoin['timestamp'], unit='s')\nbitcoin = bitcoin.set_index('timestamp')\n# extract the \"High\" value at 9:00 a.m. daily\nindexer_9am = bitcoin.index.indexer_at_time('9:00:00')\nvalues_at_9am = bitcoin.iloc[indexer_9am]\nhigh_values_at_9am = values_at_9am[[\"High\"]]\n# take a look\nhigh_values_at_9am\nbitcoin","metadata":{"execution":{"iopub.status.busy":"2022-06-02T03:04:30.584943Z","iopub.execute_input":"2022-06-02T03:04:30.58521Z","iopub.status.idle":"2022-06-02T03:04:30.661964Z","shell.execute_reply.started":"2022-06-02T03:04:30.585182Z","shell.execute_reply":"2022-06-02T03:04:30.661014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We shall forecast three months (90 days) worth of data, based on the 180 days prior to the start of the forecasting period","metadata":{}},{"cell_type":"code","source":"freq = \"1D\"             # the frequency of our data, here daily\ncontext_length    = 2400 # train on this number of days\nprediction_length = 140 # predict these many days, these are removed from the end of the training data","metadata":{"execution":{"iopub.status.busy":"2022-06-02T03:04:30.66362Z","iopub.execute_input":"2022-06-02T03:04:30.664475Z","iopub.status.idle":"2022-06-02T03:04:30.66962Z","shell.execute_reply.started":"2022-06-02T03:04:30.66443Z","shell.execute_reply":"2022-06-02T03:04:30.668755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Convert our dataframe into a GluonTS dataset","metadata":{}},{"cell_type":"code","source":"from gluonts.dataset.common import ListDataset\n\ndata_list = [{\"start\": \"2017-01-01 09:00:00\", \"target\": high_values_at_9am[c].values} for c in high_values_at_9am.columns]\ntrain_ds  = ListDataset(data_iter=data_list,freq=freq)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T03:04:30.671658Z","iopub.execute_input":"2022-06-02T03:04:30.671932Z","iopub.status.idle":"2022-06-02T03:04:30.819747Z","shell.execute_reply.started":"2022-06-02T03:04:30.671902Z","shell.execute_reply":"2022-06-02T03:04:30.818882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from gluonts.model.simple_feedforward  import SimpleFeedForwardEstimator\n# to use the DeepAREstimator \n# from gluonts.model.deepar import DeepAREstimator\nfrom gluonts.mx.distribution.student_t import StudentTOutput\nfrom gluonts.mx import Trainer\n\nestimator = SimpleFeedForwardEstimator(num_hidden_dimensions=[15],\n                                       freq=freq,\n                                       context_length=context_length,\n                                       prediction_length=prediction_length,\n                                       distr_output=StudentTOutput(),\n                                       trainer=Trainer(epochs=12,\n                                                       learning_rate=1e-4,\n                                                       num_batches_per_epoch=20,\n                                                       patience=20))\n\npredictor = estimator.train(train_ds)\nprint(\"Done\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-06-02T03:06:18.813494Z","iopub.execute_input":"2022-06-02T03:06:18.813765Z","iopub.status.idle":"2022-06-02T03:06:29.145549Z","shell.execute_reply.started":"2022-06-02T03:06:18.813737Z","shell.execute_reply":"2022-06-02T03:06:29.144919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from gluonts.evaluation import make_evaluation_predictions\n\nforecast_it, ts_it = make_evaluation_predictions(\n    dataset=train_ds,  # dataset\n    predictor=predictor,  # predictor\n    num_samples=2000,  # number of sample paths we want for evaluation\n)\n\nforecasts = list(forecast_it)\ntss = list(ts_it)","metadata":{"execution":{"iopub.status.busy":"2022-06-02T03:06:29.147023Z","iopub.execute_input":"2022-06-02T03:06:29.147383Z","iopub.status.idle":"2022-06-02T03:06:29.290891Z","shell.execute_reply.started":"2022-06-02T03:06:29.147353Z","shell.execute_reply":"2022-06-02T03:06:29.289796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_prob_forecasts(ts_entry, forecast_entry):\n    \n    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas\n    from matplotlib.figure import Figure\n    plot_length = context_length + prediction_length\n    prediction_intervals = (50.0, 90.0)\n    legend = [\"ground truth\", \"median prediction\"] + [f\"{k}% prediction interval\" for k in prediction_intervals][::-1]\n\n    fig, ax = plt.subplots(1, 1, figsize=(18, 7))\n    ts_entry[-plot_length:].plot(ax=ax)  # plot the time series\n    forecast_entry.plot(prediction_intervals=prediction_intervals, color='cadetblue')\n    plt.axvline(forecast_entry.start_date, color='g', lw=1) # end of train dataset\n    plt.grid(which=\"major\")\n    plt.legend(legend, loc=\"upper left\")\n    plt.show();\n    plt.savefig('foo.png')\n    return plt\n    \nplot_prob_forecasts(tss[0], forecasts[0])\n\n","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-06-02T03:06:29.292445Z","iopub.execute_input":"2022-06-02T03:06:29.292771Z","iopub.status.idle":"2022-06-02T03:06:29.817809Z","shell.execute_reply.started":"2022-06-02T03:06:29.292736Z","shell.execute_reply":"2022-06-02T03:06:29.816948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}