{"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-02T15:38:30.269908Z","iopub.execute_input":"2022-02-02T15:38:30.270578Z","iopub.status.idle":"2022-02-02T15:38:30.306766Z","shell.execute_reply.started":"2022-02-02T15:38:30.270454Z","shell.execute_reply":"2022-02-02T15:38:30.306062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combined = pd.read_csv(\"../input/g-research-crypto-forecasting/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-02T15:38:30.308435Z","iopub.execute_input":"2022-02-02T15:38:30.308885Z","iopub.status.idle":"2022-02-02T15:39:31.336809Z","shell.execute_reply.started":"2022-02-02T15:38:30.308845Z","shell.execute_reply":"2022-02-02T15:39:31.335877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset_details = pd.read_csv(\"../input/g-research-crypto-forecasting/asset_details.csv\")\n#asset id maps to the training set with the time series data.  The weight of the cryptocurrency is also listed\nasset_details.index = asset_details[\"Asset_ID\"]\ncombined['datetime'] = pd.to_datetime(combined['timestamp'], unit='s')\ncombined[\"Asset_Name\"] = asset_details.loc[combined.loc[:,\"Asset_ID\"],\"Asset_Name\"].to_numpy()\n#combined.loc[combined[\"Target\"].isna(),\"Target\"]=combined[\"Target\"].mean(skipna=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T15:41:57.407532Z","iopub.execute_input":"2022-02-02T15:41:57.407809Z","iopub.status.idle":"2022-02-02T15:41:59.971576Z","shell.execute_reply.started":"2022-02-02T15:41:57.407782Z","shell.execute_reply":"2022-02-02T15:41:59.970715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calculate_target(data: pd.DataFrame, details: pd.DataFrame, price_column: str):\n    ids = list(details.Asset_ID)\n    asset_names = list(details.Asset_Name)\n    weights = np.array(list(details.Weight))\n\n    all_timestamps = np.sort(data['timestamp'].unique())\n    targets = pd.DataFrame(index=all_timestamps)\n\n    for i, id in enumerate(ids):\n        asset = data[data.Asset_ID == id].set_index(keys='timestamp')\n        price = pd.Series(index=all_timestamps, data=asset[price_column])\n        targets[asset_names[i]] = (\n            price.shift(periods=-16) /\n            price.shift(periods=-1)\n        ) - 1\n    \n    targets['marketReturn'] = np.average(targets.fillna(0), axis=1, weights=weights)\n    \n    m = targets['marketReturn']\n\n    num = targets.multiply(m.values, axis=0).rolling(3734).mean().values\n    denom = m.multiply(m.values, axis=0).rolling(3734).mean().values\n    beta = np.nan_to_num(num.T / denom, nan=0., posinf=0., neginf=0.)\n\n    for i, id in enumerate(ids):\n        targets[\"BetaRolling\"+asset_names[i]] = beta[i,:]\n\n\n\n    targets['datetime'] = pd.to_datetime(targets.index, unit='s')\n    targets[\"year\"] = pd.DatetimeIndex(targets['datetime']).year\n    targets[\"month\"] = pd.DatetimeIndex(targets['datetime']).month\n\n\n    return targets\n\ntargets = (calculate_target(combined,asset_details,\"Close\"))","metadata":{"execution":{"iopub.status.busy":"2022-02-02T15:56:48.040726Z","iopub.execute_input":"2022-02-02T15:56:48.041139Z","iopub.status.idle":"2022-02-02T15:57:00.143220Z","shell.execute_reply.started":"2022-02-02T15:56:48.041110Z","shell.execute_reply":"2022-02-02T15:57:00.142273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def createDatasetCoin(data,suffix):\n    data[\"Target\"+suffix] = data[\"Target\"]\n    return data[[\"Target\"+suffix,\"datetime\"]]\n\ndef createDataset(targets):\n    for coin in asset_details.Asset_Name:\n        data = combined.loc[combined[\"Asset_Name\"]==coin,:].reset_index(drop=True) \n        data = createDatasetCoin(data, coin)\n        targets=pd.merge(targets,data, how=\"left\",on=\"datetime\")\n\n    return targets\n\ntargets = createDataset(targets)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T15:59:04.546531Z","iopub.execute_input":"2022-02-02T15:59:04.546912Z","iopub.status.idle":"2022-02-02T16:00:14.901663Z","shell.execute_reply.started":"2022-02-02T15:59:04.546869Z","shell.execute_reply":"2022-02-02T16:00:14.900912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets=targets.replace([np.inf,-np.inf],np.nan)\ntargets = targets.fillna(method=\"ffill\")\ntargets = targets.fillna(method=\"bfill\")\ntargets = targets[3750:-16]","metadata":{"execution":{"iopub.status.busy":"2022-02-02T16:00:14.903092Z","iopub.execute_input":"2022-02-02T16:00:14.903639Z","iopub.status.idle":"2022-02-02T16:00:17.374975Z","shell.execute_reply.started":"2022-02-02T16:00:14.903609Z","shell.execute_reply":"2022-02-02T16:00:17.374222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = targets.loc[(targets[\"datetime\"]>='2020-06-13 00:00:00') | (targets[\"datetime\"]<'2021-06-13 00:00:00'),:]\ntest = targets.loc[targets[\"datetime\"]>='2021-06-13 00:00:00',:]\nfeatures = [\"Bitcoin\",\"BetaRollingBitcoin\"] ","metadata":{"execution":{"iopub.status.busy":"2022-02-02T16:00:52.931139Z","iopub.execute_input":"2022-02-02T16:00:52.931630Z","iopub.status.idle":"2022-02-02T16:00:53.181477Z","shell.execute_reply.started":"2022-02-02T16:00:52.931580Z","shell.execute_reply":"2022-02-02T16:00:53.180594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#lgbm model to predict a regression var\nimport lightgbm as lgb\ntargetVar = \"Target\"+\"Bitcoin\"\ntrain_data = lgb.Dataset(train.loc[:,features], label=train.loc[:,targetVar])\ntest_data= lgb.Dataset(test.loc[:,features], label=test.loc[:,targetVar])\ntest_set=test.loc[:,features]\nparam = {'num_leaves': 64, 'objective': 'regression', 'learning_rate':.01, 'max_depth':16, 'force_col_wise':True}\nparam['metric'] = ['l1']\n\nnum_round = 500\nlightModel = lgb.train(param, train_data, num_round, valid_sets=[train_data,test_data], early_stopping_rounds=30)\npredsReg=lightModel.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T16:00:54.222461Z","iopub.execute_input":"2022-02-02T16:00:54.222977Z","iopub.status.idle":"2022-02-02T16:01:02.820456Z","shell.execute_reply.started":"2022-02-02T16:00:54.222945Z","shell.execute_reply":"2022-02-02T16:01:02.819744Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.corrcoef(test[targetVar],predsReg)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = targets.loc[(targets[\"datetime\"]>='2020-06-13 00:00:00') | (targets[\"datetime\"]<'2021-06-13 00:00:00'),:]\ntest = targets.loc[targets[\"datetime\"]>='2021-06-13 00:00:00',:]\nfeatures = ['Bitcoin Cash', 'Binance Coin', 'Bitcoin', 'EOS.IO', 'Ethereum Classic',\n       'Ethereum', 'Litecoin', 'Monero', 'TRON', 'Stellar', 'Cardano', 'IOTA',\n       'Maker', 'Dogecoin',\"BetaRollingBitcoin\"] ","metadata":{"execution":{"iopub.status.busy":"2022-02-02T16:02:01.837236Z","iopub.execute_input":"2022-02-02T16:02:01.838049Z","iopub.status.idle":"2022-02-02T16:02:02.200272Z","shell.execute_reply.started":"2022-02-02T16:02:01.838008Z","shell.execute_reply":"2022-02-02T16:02:02.199379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#lgbm model to predict a regression var\nimport lightgbm as lgb\ntargetVar = \"Target\"+\"Bitcoin\"\ntrain_data = lgb.Dataset(train.loc[:,features], label=train.loc[:,targetVar])\ntest_data= lgb.Dataset(test.loc[:,features], label=test.loc[:,targetVar])\ntest_set=test.loc[:,features]\nparam = {'num_leaves': 64, 'objective': 'regression', 'learning_rate':.01, 'max_depth':16, 'force_col_wise':True}\nparam['metric'] = ['l1']\n\nnum_round = 500\nlightModel = lgb.train(param, train_data, num_round, valid_sets=[train_data,test_data], early_stopping_rounds=30)\npredsReg=lightModel.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T16:02:04.894925Z","iopub.execute_input":"2022-02-02T16:02:04.895187Z","iopub.status.idle":"2022-02-02T16:03:07.759712Z","shell.execute_reply.started":"2022-02-02T16:02:04.895160Z","shell.execute_reply":"2022-02-02T16:03:07.758931Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.corrcoef(test[targetVar],predsReg)","metadata":{"execution":{"iopub.status.busy":"2022-02-02T16:03:13.399355Z","iopub.execute_input":"2022-02-02T16:03:13.399819Z","iopub.status.idle":"2022-02-02T16:03:13.408521Z","shell.execute_reply.started":"2022-02-02T16:03:13.399781Z","shell.execute_reply":"2022-02-02T16:03:13.407732Z"},"trusted":true},"execution_count":null,"outputs":[]}]}