{"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\nfrom datetime import datetime\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.multioutput import MultiOutputRegressor","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-17T17:35:50.137333Z","iopub.execute_input":"2022-01-17T17:35:50.137648Z","iopub.status.idle":"2022-01-17T17:35:50.142871Z","shell.execute_reply.started":"2022-01-17T17:35:50.137612Z","shell.execute_reply":"2022-01-17T17:35:50.142118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crypto_df = pd.read_csv('../input/g-research-crypto-forecasting/train.csv')\ncrypto_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T15:51:03.417506Z","iopub.execute_input":"2022-01-17T15:51:03.417764Z","iopub.status.idle":"2022-01-17T15:51:56.676295Z","shell.execute_reply.started":"2022-01-17T15:51:03.417730Z","shell.execute_reply":"2022-01-17T15:51:56.675619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset_details = pd.read_csv('../input/g-research-crypto-forecasting/asset_details.csv')\nasset_details","metadata":{"execution":{"iopub.status.busy":"2022-01-17T15:53:21.165355Z","iopub.execute_input":"2022-01-17T15:53:21.165892Z","iopub.status.idle":"2022-01-17T15:53:21.180662Z","shell.execute_reply.started":"2022-01-17T15:53:21.165852Z","shell.execute_reply":"2022-01-17T15:53:21.179739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"btc = crypto_df[crypto_df['Asset_ID'] == 1].set_index('timestamp')\neth = crypto_df[crypto_df['Asset_ID'] == 6].set_index('timestamp')\neth.info(show_counts=True)\nbtc.info(show_counts=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T16:00:35.206435Z","iopub.execute_input":"2022-01-17T16:00:35.206686Z","iopub.status.idle":"2022-01-17T16:00:35.904931Z","shell.execute_reply.started":"2022-01-17T16:00:35.206656Z","shell.execute_reply":"2022-01-17T16:00:35.904027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eth.isna().sum()\nbtc.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T16:00:59.880800Z","iopub.execute_input":"2022-01-17T16:00:59.881108Z","iopub.status.idle":"2022-01-17T16:00:59.951699Z","shell.execute_reply.started":"2022-01-17T16:00:59.881073Z","shell.execute_reply":"2022-01-17T16:00:59.950983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"beg_btc = btc.index[0].astype('datetime64[s]')\nend_btc = btc.index[-1].astype('datetime64[s]')\n\nbeg_eth = eth.index[0].astype('datetime64[s]')\nend_eth = eth.index[-1].astype('datetime64[s]')\n\nprint('BTC Data are from ', beg_btc,'to ',end_btc)\nprint('ETH data are from ', beg_eth,' to ',end_eth)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T16:04:18.004068Z","iopub.execute_input":"2022-01-17T16:04:18.004519Z","iopub.status.idle":"2022-01-17T16:04:18.014554Z","shell.execute_reply.started":"2022-01-17T16:04:18.004483Z","shell.execute_reply":"2022-01-17T16:04:18.013825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(eth.index[1:] - eth.index[:-1]).value_counts().head()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T16:07:09.713068Z","iopub.execute_input":"2022-01-17T16:07:09.713598Z","iopub.status.idle":"2022-01-17T16:07:09.739627Z","shell.execute_reply.started":"2022-01-17T16:07:09.713559Z","shell.execute_reply":"2022-01-17T16:07:09.738858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eth = eth.reindex(range(eth.index[0],eth.index[-1]+60,60),method='pad')\nbtc = btc.reindex(range(btc.index[0],btc.index[-1]+60,60),method='pad')","metadata":{"execution":{"iopub.status.busy":"2022-01-17T16:10:14.453342Z","iopub.execute_input":"2022-01-17T16:10:14.453873Z","iopub.status.idle":"2022-01-17T16:10:14.802144Z","shell.execute_reply.started":"2022-01-17T16:10:14.453834Z","shell.execute_reply":"2022-01-17T16:10:14.801413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = plt.figure(figsize=(15,4))\n\nax = f.add_subplot(121)\nplt.plot(btc['Close'],color='green',label='BTC')\nplt.legend()\nplt.xlabel('Time')\nplt.ylabel('Bitcoin')\n\nax2 = f.add_subplot(122)\nax2.plot(eth['Close'],color='red',label='ETH')\nplt.legend()\nplt.xlabel('Time')\nplt.ylabel('Etherium')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T16:15:31.889093Z","iopub.execute_input":"2022-01-17T16:15:31.889743Z","iopub.status.idle":"2022-01-17T16:15:34.562626Z","shell.execute_reply.started":"2022-01-17T16:15:31.889707Z","shell.execute_reply":"2022-01-17T16:15:34.561926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def log_return(series,periods=1):\n    return np.log(series).diff(periods=periods)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T16:27:41.659716Z","iopub.execute_input":"2022-01-17T16:27:41.659974Z","iopub.status.idle":"2022-01-17T16:27:41.663967Z","shell.execute_reply.started":"2022-01-17T16:27:41.659944Z","shell.execute_reply":"2022-01-17T16:27:41.663058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import scipy.stats as stats\nlret_btc = log_return(btc.Close)[1:]\nlret_eth = log_return(eth.Close)[1:]\nlret_btc.rename('BTC_lret',inplace=True)\nlret_eth.rename('ETH_lret',inplace=True)\ntwo_assets = pd.concat([lret_btc,lret_eth],axis=1)\ntwo_assets","metadata":{"execution":{"iopub.status.busy":"2022-01-17T16:38:11.907069Z","iopub.execute_input":"2022-01-17T16:38:11.907755Z","iopub.status.idle":"2022-01-17T16:38:11.971658Z","shell.execute_reply.started":"2022-01-17T16:38:11.907718Z","shell.execute_reply":"2022-01-17T16:38:11.970976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_time = two_assets.groupby(two_assets.index//(10000*60)).corr().loc[:,'BTC_lret'].loc[:,'ETH_lret']\ncorr_time.plot()\nplt.xticks()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T16:39:29.938935Z","iopub.execute_input":"2022-01-17T16:39:29.939211Z","iopub.status.idle":"2022-01-17T16:39:30.307405Z","shell.execute_reply.started":"2022-01-17T16:39:29.939182Z","shell.execute_reply":"2022-01-17T16:39:30.306738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"upper_shadow = lambda asset: asset.High - np.maximum(asset.Close,asset.Open)\nlower_shadow = lambda asset: np.minimum(asset.Close,asset.Open) - asset.Low\n\nX_btc = pd.concat([log_return(btc.VWAP,periods=5), log_return(btc.VWAP,periods=1).abs(),upper_shadow(btc),lower_shadow(btc)],axis=1)\ny_btc = btc.Target\n\nX_eth = pd.concat([log_return(eth.VWAP,periods=5), log_return(eth.VWAP,periods=1).abs(),upper_shadow(eth),lower_shadow(eth)],axis=1)\ny_eth = eth.Target","metadata":{"execution":{"iopub.status.busy":"2022-01-17T17:03:12.273672Z","iopub.execute_input":"2022-01-17T17:03:12.273920Z","iopub.status.idle":"2022-01-17T17:03:12.465456Z","shell.execute_reply.started":"2022-01-17T17:03:12.273893Z","shell.execute_reply":"2022-01-17T17:03:12.462625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\ntotimestamp = lambda s: np.int32(time.mktime(datetime.strptime(s,\"%d/%m/%Y\").timetuple()))\n\ntrain_window = [totimestamp('01/05/2021'),totimestamp('30/05/2021')]\ntest_window = [totimestamp('01/06/2021'),totimestamp('30/06/2021')]\ntest_window","metadata":{"execution":{"iopub.status.busy":"2022-01-17T17:18:41.468988Z","iopub.execute_input":"2022-01-17T17:18:41.469449Z","iopub.status.idle":"2022-01-17T17:18:41.475546Z","shell.execute_reply.started":"2022-01-17T17:18:41.469416Z","shell.execute_reply":"2022-01-17T17:18:41.474925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_btc_train = X_btc.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()\ny_btc_train = y_btc.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()\n\nX_btc_test = X_btc.loc[test_window[0]:test_window[1]].fillna(0).to_numpy()\ny_btc_test = y_btc.loc[test_window[0]:test_window[1]].fillna(0).to_numpy()\n\nX_eth_train = X_eth.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()\ny_eth_train = y_eth.loc[train_window[0]:train_window[1]].fillna(0).to_numpy()\n\nX_eth_test = X_eth.loc[test_window[0]:test_window[1]].fillna(0).to_numpy()\ny_eth_test = y_eth.loc[test_window[0]:test_window[1]].fillna(0).to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-01-17T17:27:32.546138Z","iopub.execute_input":"2022-01-17T17:27:32.546679Z","iopub.status.idle":"2022-01-17T17:27:32.560836Z","shell.execute_reply.started":"2022-01-17T17:27:32.546639Z","shell.execute_reply":"2022-01-17T17:27:32.560045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\n\nX_btc_train_scaled = scaler.fit_transform(X_btc_train)\nX_btc_test_scaled = scaler.transform(X_btc_test)\n\nX_eth_train_scaled = scaler.fit_transform(X_eth_train)\nX_eth_test_scaled = scaler.transform(X_eth_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-17T17:30:37.736554Z","iopub.execute_input":"2022-01-17T17:30:37.736812Z","iopub.status.idle":"2022-01-17T17:30:37.749465Z","shell.execute_reply.started":"2022-01-17T17:30:37.736783Z","shell.execute_reply":"2022-01-17T17:30:37.748608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = LinearRegression()\nlr.fit(X_btc_train_scaled,y_btc_train)\ny_pred_lr_btc = lr.predict(X_btc_test_scaled)\n\nlr.fit(X_eth_train_scaled,y_eth_train)\ny_pred_lr_eth = lr.predict(X_eth_test_scaled)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-17T17:34:34.392522Z","iopub.execute_input":"2022-01-17T17:34:34.392795Z","iopub.status.idle":"2022-01-17T17:34:34.422118Z","shell.execute_reply.started":"2022-01-17T17:34:34.392767Z","shell.execute_reply":"2022-01-17T17:34:34.421317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Test Score LR Baseline: BTC', f'{np.corrcoef(y_pred_lr_btc,y_btc_test)[0,1]:.2f}')","metadata":{"execution":{"iopub.status.busy":"2022-01-17T17:39:01.428256Z","iopub.execute_input":"2022-01-17T17:39:01.428957Z","iopub.status.idle":"2022-01-17T17:39:01.434727Z","shell.execute_reply.started":"2022-01-17T17:39:01.428920Z","shell.execute_reply":"2022-01-17T17:39:01.433692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}