{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-17T09:23:17.507438Z","iopub.execute_input":"2022-02-17T09:23:17.507781Z","iopub.status.idle":"2022-02-17T09:23:17.552171Z","shell.execute_reply.started":"2022-02-17T09:23:17.507697Z","shell.execute_reply":"2022-02-17T09:23:17.55129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nfrom plotly.subplots import make_subplots\nimport plotly.graph_objects as go\nfrom fbprophet import Prophet\nfrom sklearn.metrics import mean_absolute_error,mean_squared_error\nfrom fbprophet.plot import add_changepoints_to_plot\nfrom fbprophet.diagnostics import performance_metrics\nfrom fbprophet.diagnostics import cross_validation\nfrom statsmodels.tsa.seasonal import seasonal_decompose\nfrom fbprophet.plot import plot_cross_validation_metric","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:23:18.514834Z","iopub.execute_input":"2022-02-17T09:23:18.515236Z","iopub.status.idle":"2022-02-17T09:23:21.382643Z","shell.execute_reply.started":"2022-02-17T09:23:18.515187Z","shell.execute_reply":"2022-02-17T09:23:21.381688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/train.csv')\nasset_df = pd.read_csv('/kaggle/input/g-research-crypto-forecasting/asset_details.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:23:21.384742Z","iopub.execute_input":"2022-02-17T09:23:21.385086Z","iopub.status.idle":"2022-02-17T09:24:22.362217Z","shell.execute_reply.started":"2022-02-17T09:23:21.38504Z","shell.execute_reply":"2022-02-17T09:24:22.361276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc_df=df.query('Asset_ID==3')\nbtc_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:22.363212Z","iopub.execute_input":"2022-02-17T09:24:22.363416Z","iopub.status.idle":"2022-02-17T09:24:22.862598Z","shell.execute_reply.started":"2022-02-17T09:24:22.36339Z","shell.execute_reply":"2022-02-17T09:24:22.861514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc_df=btc_df.set_index('timestamp')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:22.865114Z","iopub.execute_input":"2022-02-17T09:24:22.865716Z","iopub.status.idle":"2022-02-17T09:24:22.925791Z","shell.execute_reply.started":"2022-02-17T09:24:22.865671Z","shell.execute_reply":"2022-02-17T09:24:22.924902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(btc_df.index[1:]-btc_df.index[:-1]).value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:22.926986Z","iopub.execute_input":"2022-02-17T09:24:22.927237Z","iopub.status.idle":"2022-02-17T09:24:22.96039Z","shell.execute_reply.started":"2022-02-17T09:24:22.927208Z","shell.execute_reply":"2022-02-17T09:24:22.959386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc_df= btc_df.reindex(range(btc_df.index[0],btc_df.index[-1]+60,60),method='pad')\n","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:22.962044Z","iopub.execute_input":"2022-02-17T09:24:22.962934Z","iopub.status.idle":"2022-02-17T09:24:23.354166Z","shell.execute_reply.started":"2022-02-17T09:24:22.962879Z","shell.execute_reply":"2022-02-17T09:24:23.353177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Null values:\")\nbtc_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:23.356162Z","iopub.execute_input":"2022-02-17T09:24:23.356657Z","iopub.status.idle":"2022-02-17T09:24:23.397625Z","shell.execute_reply.started":"2022-02-17T09:24:23.356613Z","shell.execute_reply":"2022-02-17T09:24:23.39705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(btc_df.index[1:]-btc_df.index[:-1]).value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:23.398826Z","iopub.execute_input":"2022-02-17T09:24:23.39977Z","iopub.status.idle":"2022-02-17T09:24:23.445637Z","shell.execute_reply.started":"2022-02-17T09:24:23.399735Z","shell.execute_reply":"2022-02-17T09:24:23.444723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc_df=btc_df.reset_index()\nbtc_df['date_time'] = btc_df['timestamp'].astype('datetime64[s]')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:23.447056Z","iopub.execute_input":"2022-02-17T09:24:23.447349Z","iopub.status.idle":"2022-02-17T09:24:23.653092Z","shell.execute_reply.started":"2022-02-17T09:24:23.447309Z","shell.execute_reply":"2022-02-17T09:24:23.652048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new = btc_df.groupby([pd.Grouper(freq='H', key='date_time')]).mean().reset_index()\ndf_new=df_new.drop(['timestamp'],axis=1)\ndf_new.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:23.656005Z","iopub.execute_input":"2022-02-17T09:24:23.656256Z","iopub.status.idle":"2022-02-17T09:24:23.866092Z","shell.execute_reply.started":"2022-02-17T09:24:23.656222Z","shell.execute_reply":"2022-02-17T09:24:23.865479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:23.867105Z","iopub.execute_input":"2022-02-17T09:24:23.86742Z","iopub.status.idle":"2022-02-17T09:24:23.872306Z","shell.execute_reply.started":"2022-02-17T09:24:23.867393Z","shell.execute_reply":"2022-02-17T09:24:23.871662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure(data=[go.Candlestick(x=df_new['date_time'].iloc[-10000:],\n                open=df_new['Open'].iloc[-10000:],\n                high=df_new['High'].iloc[-10000:],\n                low=df_new['Low'].iloc[-10000:],\n                close=df_new['Close'].iloc[-10000:])])\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:23.87349Z","iopub.execute_input":"2022-02-17T09:24:23.873834Z","iopub.status.idle":"2022-02-17T09:24:24.282741Z","shell.execute_reply.started":"2022-02-17T09:24:23.873807Z","shell.execute_reply":"2022-02-17T09:24:24.271213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = go.Figure(data=[go.Candlestick(x=df_new['date_time'].iloc[-3000:],\n                open=df_new['Open'].iloc[-3000:],\n                high=df_new['High'].iloc[-3000:],\n                low=df_new['Low'].iloc[-3000:],\n                close=df_new['Close'].iloc[-3000:])])\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:24.28383Z","iopub.execute_input":"2022-02-17T09:24:24.284083Z","iopub.status.idle":"2022-02-17T09:24:24.385514Z","shell.execute_reply.started":"2022-02-17T09:24:24.284052Z","shell.execute_reply":"2022-02-17T09:24:24.384537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new=df_new.drop(['Asset_ID','Open','Count','High','Low','Volume','VWAP','Target'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:24.386691Z","iopub.execute_input":"2022-02-17T09:24:24.386916Z","iopub.status.idle":"2022-02-17T09:24:24.392444Z","shell.execute_reply.started":"2022-02-17T09:24:24.38689Z","shell.execute_reply":"2022-02-17T09:24:24.391631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef linear_features(df):\n    df_feat=pd.DataFrame()\n    df_feat['ds'] = df['date_time']\n    df_feat['y'] = df['Close']\n    return df_feat","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:24.393801Z","iopub.execute_input":"2022-02-17T09:24:24.394056Z","iopub.status.idle":"2022-02-17T09:24:24.404774Z","shell.execute_reply.started":"2022-02-17T09:24:24.394029Z","shell.execute_reply":"2022-02-17T09:24:24.403927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_new['Low'] = list(map(lambda z,b: b-0.001 if z<=b else b, train_new.High, train_new.Low ))\n","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:24.406087Z","iopub.execute_input":"2022-02-17T09:24:24.406494Z","iopub.status.idle":"2022-02-17T09:24:24.41691Z","shell.execute_reply.started":"2022-02-17T09:24:24.406413Z","shell.execute_reply":"2022-02-17T09:24:24.416324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=df_new.iloc[0:-2952]\ntest_df=df_new.iloc[-2952:]\n# train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:24.418974Z","iopub.execute_input":"2022-02-17T09:24:24.419884Z","iopub.status.idle":"2022-02-17T09:24:24.42824Z","shell.execute_reply.started":"2022-02-17T09:24:24.419834Z","shell.execute_reply":"2022-02-17T09:24:24.427575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mean_absolute_percentage_error(y_true, y_pred): \n    \"\"\"Calculates MAPE given y_true and y_pred\"\"\"\n    y_true, y_pred = np.array(y_true), np.array(y_pred)\n    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:24.429193Z","iopub.execute_input":"2022-02-17T09:24:24.429707Z","iopub.status.idle":"2022-02-17T09:24:24.441408Z","shell.execute_reply.started":"2022-02-17T09:24:24.429671Z","shell.execute_reply":"2022-02-17T09:24:24.440398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig=plt.figure(figsize=(800, 800))\noutput = seasonal_decompose(df_new.set_index('date_time')[-6000:], model='additive')\nplt.rcParams['figure.figsize'] = (18, 9)\n# creating plot\noutput.plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:24.442766Z","iopub.execute_input":"2022-02-17T09:24:24.442984Z","iopub.status.idle":"2022-02-17T09:24:25.228902Z","shell.execute_reply.started":"2022-02-17T09:24:24.442958Z","shell.execute_reply":"2022-02-17T09:24:25.22791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_li=linear_features(train_df)\nmodel = Prophet(growth='linear',\n                changepoint_prior_scale=0.001,seasonality_prior_scale=0.1,seasonality_mode='additive')\n# model.add_seasonality(name='custom_monthly', period=30.5, fourier_order=24)\n\nmodel.fit(train_li)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:25.230245Z","iopub.execute_input":"2022-02-17T09:24:25.230508Z","iopub.status.idle":"2022-02-17T09:24:56.373142Z","shell.execute_reply.started":"2022-02-17T09:24:25.230477Z","shell.execute_reply":"2022-02-17T09:24:56.372252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = linear_features(df_new)\ny=x.drop('y',axis=1)\ny_pred = model.predict(y)\n# y_pred","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:24:56.374526Z","iopub.execute_input":"2022-02-17T09:24:56.374752Z","iopub.status.idle":"2022-02-17T09:25:14.687374Z","shell.execute_reply.started":"2022-02-17T09:24:56.374724Z","shell.execute_reply":"2022-02-17T09:25:14.686353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_absolute_percentage_error(y_true=df_new['Close'],\n                   y_pred=y_pred['yhat'])\n","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:25:14.688599Z","iopub.execute_input":"2022-02-17T09:25:14.688807Z","iopub.status.idle":"2022-02-17T09:25:14.696216Z","shell.execute_reply.started":"2022-02-17T09:25:14.68878Z","shell.execute_reply":"2022-02-17T09:25:14.695099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(df_new['Close'].values, label='Actual')\nplt.plot(y_pred['yhat'], label=['Predicted'])\nplt.vlines(x=27000, ymin=0,ymax=3, colors='purple', ls='--', lw=2, label='test_train/split')\nplt.legend()\nplt.title('changepoint_scale=0.001')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:25:14.69725Z","iopub.execute_input":"2022-02-17T09:25:14.697731Z","iopub.status.idle":"2022-02-17T09:25:15.023807Z","shell.execute_reply.started":"2022-02-17T09:25:14.697681Z","shell.execute_reply":"2022-02-17T09:25:15.022848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = linear_features(test_df)\ny=x.drop('y',axis=1)\ny_pred = model.predict(y)\n# y_pred","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:25:15.025141Z","iopub.execute_input":"2022-02-17T09:25:15.025968Z","iopub.status.idle":"2022-02-17T09:25:19.83597Z","shell.execute_reply.started":"2022-02-17T09:25:15.025919Z","shell.execute_reply":"2022-02-17T09:25:19.834932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_absolute_percentage_error(y_true=test_df['Close'],\n                   y_pred=y_pred['yhat'])\n","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:25:19.83735Z","iopub.execute_input":"2022-02-17T09:25:19.837665Z","iopub.status.idle":"2022-02-17T09:25:19.845586Z","shell.execute_reply.started":"2022-02-17T09:25:19.837623Z","shell.execute_reply":"2022-02-17T09:25:19.844746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(test_df['Close'].values, label='Actual')\nplt.plot(y_pred['yhat'], label=['Predicted'])\nplt.legend()\nplt.title('prediciton on test')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:25:19.846782Z","iopub.execute_input":"2022-02-17T09:25:19.847112Z","iopub.status.idle":"2022-02-17T09:25:20.11378Z","shell.execute_reply.started":"2022-02-17T09:25:19.847065Z","shell.execute_reply":"2022-02-17T09:25:20.112924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig5 = model.plot(y_pred)\na = add_changepoints_to_plot(fig5.gca(), model, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:25:20.115535Z","iopub.execute_input":"2022-02-17T09:25:20.11613Z","iopub.status.idle":"2022-02-17T09:25:20.533716Z","shell.execute_reply.started":"2022-02-17T09:25:20.116082Z","shell.execute_reply":"2022-02-17T09:25:20.532801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig3 = model.plot_components(y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:25:20.537426Z","iopub.execute_input":"2022-02-17T09:25:20.538321Z","iopub.status.idle":"2022-02-17T09:25:21.68252Z","shell.execute_reply.started":"2022-02-17T09:25:20.538276Z","shell.execute_reply":"2022-02-17T09:25:21.681895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import itertools","metadata":{"execution":{"iopub.status.busy":"2022-02-17T09:25:21.683876Z","iopub.execute_input":"2022-02-17T09:25:21.684319Z","iopub.status.idle":"2022-02-17T09:25:21.687513Z","shell.execute_reply.started":"2022-02-17T09:25:21.684285Z","shell.execute_reply":"2022-02-17T09:25:21.686915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {  \n    'changepoint_prior_scale': [0.001, 0.01, 0.1, 0.5],\n    'seasonality_prior_scale': [0.01, 0.1, 1.0, 10.0],\n    'seasonality_mode':['addtive','multiplicative'],\n#     'changepoint_range':[0.8,0.85,0.9,0.95,1],\n}\n\n# Generate all combinations of parameters\nall_params = [dict(zip(param_grid.keys(), v)) for v in itertools.product(*param_grid.values())]\nmape = []  # Store the RMSEs for each params here\n\n# Use cross validation to evaluate all parameters\nfor params in all_params:\n    m = Prophet(**params).fit(train_li)  # Fit model with given params\n    x = linear_features(test_df)\n    y=x.drop('y',axis=1)\n    y_pr = m.predict(y)\n    mape_value=mean_absolute_percentage_error(y_true=test_df['Close'],y_pred=y_pr['yhat'])\n    mape.append(mape_value)\n    print(\"ONE DONE ....\")\n\n# Find the best parameters\ntuning_results = pd.DataFrame(all_params)\ntuning_results['mape'] = mape\nprint(tuning_results)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find the best parameters\ntuning_results = pd.DataFrame(all_params)\ntuning_results['mape'] = mape\nprint(tuning_results)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_params = all_params[np.argmin(mape)]\nprint(best_params,\":\",min(mape))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}