{"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":"2021-12-15T12:25:33.027142Z","iopub.execute_input":"2021-12-15T12:25:33.027526Z","iopub.status.idle":"2021-12-15T12:25:33.090729Z","shell.execute_reply.started":"2021-12-15T12:25:33.027431Z","shell.execute_reply":"2021-12-15T12:25:33.090125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom datetime import datetime\nimport matplotlib.pyplot as plt\nimport time\nimport statsmodels.api as sm\nfrom statsmodels.graphics.tsaplots import plot_acf\nfrom statsmodels.tsa.stattools import coint\nfrom statsmodels.tsa.arima_model import ARMA\nfrom sklearn import preprocessing\nfrom statsmodels.tsa.statespace.sarimax import SARIMAX\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.linear_model import Lasso\nfrom sklearn.linear_model import Ridge\nfrom sklearn.model_selection import GridSearchCV\nfrom sklearn.preprocessing import MinMaxScaler\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import LSTM\nfrom keras.layers import Dropout\nfrom sklearn.ensemble import RandomForestRegressor\nfrom sklearn.metrics import r2_score\nimport seaborn as sns\nfrom sklearn.svm import SVR\nimport xgboost as xgb\nfrom sklearn.metrics import mean_absolute_error\nfrom sklearn.metrics import mean_squared_error","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:33.092217Z","iopub.execute_input":"2021-12-15T12:25:33.092479Z","iopub.status.idle":"2021-12-15T12:25:41.805127Z","shell.execute_reply.started":"2021-12-15T12:25:33.092432Z","shell.execute_reply":"2021-12-15T12:25:41.803994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bit = pd.read_csv('../input/dataset-for-my-notebook/export_bitcoin.csv')\neth = pd.read_csv('../input/dataset-for-my-notebook/export_ethereum.csv')\nbit.timestamp = pd.to_datetime(bit.timestamp, unit = 's')\neth.timestamp = pd.to_datetime(eth.timestamp, unit = 's')\nbit.set_index('timestamp', inplace = True)\neth.set_index('timestamp', inplace = True)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:30:15.958251Z","iopub.execute_input":"2021-12-15T12:30:15.958599Z","iopub.status.idle":"2021-12-15T12:30:26.452663Z","shell.execute_reply.started":"2021-12-15T12:30:15.958565Z","shell.execute_reply":"2021-12-15T12:30:26.451432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BTC = bit['2021':]\nBTC = BTC[['Count', 'Close', 'Volume']]\nB = BTC.Close.resample('15T').std()\nBTC = BTC.resample('15T').first()\nBTC['std_btc'] = B\nBTC['Return_BTC'] = BTC.Close.pct_change()\nBTC['Target_BTC'] = BTC.Return_BTC.shift(-1)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.203088Z","iopub.status.idle":"2021-12-15T12:25:42.203777Z","shell.execute_reply.started":"2021-12-15T12:25:42.203493Z","shell.execute_reply":"2021-12-15T12:25:42.203523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BTC.columns = ['Count_BTC', 'Close_BTC', 'Volume_BTC', 'std_BTC', 'Return_BTC', 'Target_BTC']","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.20521Z","iopub.status.idle":"2021-12-15T12:25:42.205833Z","shell.execute_reply.started":"2021-12-15T12:25:42.205571Z","shell.execute_reply":"2021-12-15T12:25:42.205598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ETH = eth['2021':]\nETH = ETH[['Count', 'Close', 'Volume']]\nE = ETH.Close.resample('15T').std()\nETH = ETH.resample('15T').first()\nETH['std_btc'] = E\nETH['Return_ETH'] = ETH.Close.pct_change()\nETH['Target_ETH'] = ETH.Return_ETH.shift(-1)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.20724Z","iopub.status.idle":"2021-12-15T12:25:42.207859Z","shell.execute_reply.started":"2021-12-15T12:25:42.207602Z","shell.execute_reply":"2021-12-15T12:25:42.207628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ETH.columns = ['Count_ETH', 'Close_ETH', 'Volume_ETH', 'std_ETH', 'Return_ETH', 'Target_ETH']","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.209144Z","iopub.status.idle":"2021-12-15T12:25:42.209763Z","shell.execute_reply.started":"2021-12-15T12:25:42.209508Z","shell.execute_reply":"2021-12-15T12:25:42.209534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Assets = pd.concat([BTC, ETH], axis = 1).dropna()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.210879Z","iopub.status.idle":"2021-12-15T12:25:42.211705Z","shell.execute_reply.started":"2021-12-15T12:25:42.211421Z","shell.execute_reply":"2021-12-15T12:25:42.211452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(Assets.corr(), cmap = 'coolwarm')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.213457Z","iopub.status.idle":"2021-12-15T12:25:42.214677Z","shell.execute_reply.started":"2021-12-15T12:25:42.214364Z","shell.execute_reply":"2021-12-15T12:25:42.214396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = Assets[['Count_BTC', 'Volume_BTC', 'std_BTC',\n       'Return_BTC', 'Count_ETH', 'Volume_ETH',\n       'std_ETH', 'Return_ETH']]\ny = Assets['Target_BTC']\nX_train, X_test, y_train, y_test = train_test_split(X, y,\ntest_size = 0.3)\nreg_all = LinearRegression()\nreg_all.fit(X_train, y_train)\ny_pred = reg_all.predict(X_test)\nreg_all.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.215868Z","iopub.status.idle":"2021-12-15T12:25:42.216364Z","shell.execute_reply.started":"2021-12-15T12:25:42.216114Z","shell.execute_reply":"2021-12-15T12:25:42.216139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"param_grid = {'alpha': np.arange(start = 0.01, stop = 0.05, step = 0.001)}\nridge = Ridge()\nridge_cv = GridSearchCV(ridge, param_grid, cv=5)\nridge_cv.fit(X, y)\nprint(ridge_cv.best_params_)\nprint(ridge_cv.best_score_)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.217974Z","iopub.status.idle":"2021-12-15T12:25:42.218434Z","shell.execute_reply.started":"2021-12-15T12:25:42.218185Z","shell.execute_reply":"2021-12-15T12:25:42.218209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y,\ntest_size = 0.3)\nlasso = Lasso(alpha=0.001)\nlasso_coef = lasso.fit(X_train, y_train).coef_\nlasso_pred = lasso.predict(X_test)\nlasso.score(X_test, y_test)\nnames = X_train.columns\nplt.plot(range(len(names)), lasso_coef)\nplt.xticks(range(len(names)), names, rotation=60)\nplt.ylabel('Coefficients')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.219596Z","iopub.status.idle":"2021-12-15T12:25:42.220083Z","shell.execute_reply.started":"2021-12-15T12:25:42.219799Z","shell.execute_reply":"2021-12-15T12:25:42.219825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yBTC = Assets.Target_BTC\nshifts = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\nmany_shiftsBTC = {'lag_BTC{}'.format(ii): yBTC.shift(ii) for ii in shifts}\nyETH = Assets.Target_ETH\nmany_shiftsETH = {'lag_ETH{}'.format(ii): yETH.shift(ii) for ii in shifts}\nmany_shiftsBTC = pd.DataFrame(many_shiftsBTC)\nmany_shiftsETH = pd.DataFrame(many_shiftsETH)\nX = Assets.join(many_shiftsBTC)\nX = X.join(many_shiftsETH)\nX = X.dropna()\ny = X['Target_BTC']\nX = X[['Count_BTC', 'Volume_BTC', 'std_BTC',\n       'Return_BTC', 'Count_ETH', 'Volume_ETH',\n       'std_ETH', 'Return_ETH', 'lag_BTC1', 'lag_BTC2',\n       'lag_BTC3', 'lag_BTC4', 'lag_BTC5', 'lag_BTC6', 'lag_BTC7', 'lag_BTC8',\n       'lag_BTC9', 'lag_BTC10', 'lag_BTC11', 'lag_BTC12', 'lag_BTC13',\n       'lag_BTC14', 'lag_BTC15', 'lag_BTC16', 'lag_BTC17', 'lag_BTC18',\n       'lag_BTC19', 'lag_BTC20', 'lag_ETH1', 'lag_ETH2', 'lag_ETH3',\n       'lag_ETH4', 'lag_ETH5', 'lag_ETH6', 'lag_ETH7', 'lag_ETH8', 'lag_ETH9',\n       'lag_ETH10', 'lag_ETH11', 'lag_ETH12', 'lag_ETH13', 'lag_ETH14',\n       'lag_ETH15', 'lag_ETH16', 'lag_ETH17', 'lag_ETH18', 'lag_ETH19',\n       'lag_ETH20']]","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.221253Z","iopub.status.idle":"2021-12-15T12:25:42.221707Z","shell.execute_reply.started":"2021-12-15T12:25:42.221461Z","shell.execute_reply":"2021-12-15T12:25:42.221486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X.iloc[:20000,:].values \ny_train = y.iloc[:20000, ].values\nX_test = X.iloc[20000:, ].values\ny_test = y.iloc[20000:, ].values\nX_train, X_test = np.array(X_train), np.array(X_test)\ny_train, y_test = np.array(y_train), np.array(y_test)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.22301Z","iopub.status.idle":"2021-12-15T12:25:42.223473Z","shell.execute_reply.started":"2021-12-15T12:25:42.223219Z","shell.execute_reply":"2021-12-15T12:25:42.223244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scX = MinMaxScaler(feature_range=(0,1))\nscy = MinMaxScaler(feature_range=(0,1))\nX_train_scaled = scX.fit_transform(X_train)\ny_train_scaled = scy.fit_transform(y_train.reshape(-1,1))\nX_train_scaled = np.reshape(X_train_scaled, (X_train_scaled.shape[0], X_train_scaled.shape[1], 1))\n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.224716Z","iopub.status.idle":"2021-12-15T12:25:42.225193Z","shell.execute_reply.started":"2021-12-15T12:25:42.224945Z","shell.execute_reply":"2021-12-15T12:25:42.224971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(LSTM(units = 200, return_sequences= True, input_shape = (X_train_scaled.shape[1], 1)))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(units = 200, return_sequences = True))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(units = 200, return_sequences=True))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(units = 200))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(units = 1))\nmodel.compile(optimizer = 'adam', loss = 'mean_squared_error')\n\nmodel.fit(X_train_scaled, y_train_scaled, epochs = 5, batch_size=10)\nX_test_scaled = scX.fit_transform(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.228248Z","iopub.status.idle":"2021-12-15T12:25:42.229451Z","shell.execute_reply.started":"2021-12-15T12:25:42.229139Z","shell.execute_reply":"2021-12-15T12:25:42.229171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted = model.predict(X_test_scaled)\nreturnBTC_predicted = scy.inverse_transform(predicted)\n\ndf = pd.DataFrame({'Real Values':y_test.reshape(-1), 'Predicted Values':returnBTC_predicted.reshape(-1)})\n\n\n# # Visualising the LSTM Results  \n\nplt.scatter(returnBTC_predicted, y_test, color = 'red', alpha=0.5)\nplt.title('LSTM')\nplt.xlabel('Prediction')\nplt.ylabel('Real')\nplt.show()\nprint(mean_absolute_error(df['Real Values'], df['Predicted Values']))\nprint(mean_absolute_error(df['Real Values'], np.zeros((5225,))))\nprint(mean_absolute_error(df['Real Values'], df['Predicted Values'])/mean_absolute_error(df['Real Values'], np.zeros((5225,))), 'MAE ratio')","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.231387Z","iopub.status.idle":"2021-12-15T12:25:42.231818Z","shell.execute_reply.started":"2021-12-15T12:25:42.231614Z","shell.execute_reply":"2021-12-15T12:25:42.231639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"Random Forest Regression","metadata":{}},{"cell_type":"code","source":"X = Assets[['Count_BTC', 'Volume_BTC', 'std_BTC',\n       'Return_BTC', 'Count_ETH', 'Volume_ETH',\n       'std_ETH', 'Return_ETH']].values\ny = Assets['Target_BTC'].values","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.232977Z","iopub.status.idle":"2021-12-15T12:25:42.233581Z","shell.execute_reply.started":"2021-12-15T12:25:42.233374Z","shell.execute_reply":"2021-12-15T12:25:42.233398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.reshape(-1, 8)\ny = y.reshape(-1,1)\n\nscX = MinMaxScaler()\nscy = MinMaxScaler()\nX_scaled = scX.fit_transform(X)\n\nX_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size = 0.05,  shuffle=False)\n\ny_train_scaled = scy.fit_transform(y_train)\n\n\n# Fitting Random Forest Regression to the dataset\nregressor = RandomForestRegressor()\nparam_dist = {\"max_depth\": [10, 25,50, 75],\n              \"n_estimators\": [10, 100, 200], \n              \"criterion\": ['absolute_error']\n             }\ngrid_search = GridSearchCV(regressor, n_jobs=-1, param_grid=param_dist, cv = 3, verbose=5)\ngrid_search.fit(X_train,y_train)\ngrid_search.best_estimator_\n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.234984Z","iopub.status.idle":"2021-12-15T12:25:42.235313Z","shell.execute_reply.started":"2021-12-15T12:25:42.235137Z","shell.execute_reply":"2021-12-15T12:25:42.235158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"regressor = RandomForestRegressor(n_estimators = 10, random_state = 0)\nregressor.fit(X_train.reshape(-1,8), y_train_scaled.reshape(-1,1))\n\ny_pred = regressor.predict(X_test.reshape(-1,8))\ny_pred = scy.inverse_transform(y_pred.reshape(-1,1))\ndf = pd.DataFrame({'Real Values':y_test.reshape(-1), 'Predicted Values':y_pred.reshape(-1)})\n\n\n# # Visualising the Random Forest Regression Results  \n\nplt.scatter(y_pred, y_test, color = 'red', alpha=0.5)\nplt.scatter(y_train, y_train, color = 'blue', alpha = 0.4)\nplt.title('Random Forest Regression')\nplt.xlabel('Prediction')\nplt.ylabel('Revenue')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.236714Z","iopub.status.idle":"2021-12-15T12:25:42.237309Z","shell.execute_reply.started":"2021-12-15T12:25:42.237012Z","shell.execute_reply":"2021-12-15T12:25:42.237042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(r2_score(df['Predicted Values'], df['Real Values']))\nprint(mean_absolute_error(df['Real Values'], df['Predicted Values']))\nprint(mean_absolute_error(df['Real Values'], np.zeros((1263,))))","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.238791Z","iopub.status.idle":"2021-12-15T12:25:42.239294Z","shell.execute_reply.started":"2021-12-15T12:25:42.239039Z","shell.execute_reply":"2021-12-15T12:25:42.239064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"XGBoost","metadata":{}},{"cell_type":"code","source":"data = Assets\ndata['Target_BTC'] = (Assets['Target_BTC']>0)*1\n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.240878Z","iopub.status.idle":"2021-12-15T12:25:42.241359Z","shell.execute_reply.started":"2021-12-15T12:25:42.241114Z","shell.execute_reply":"2021-12-15T12:25:42.241138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = data[['Count_BTC', 'Close_BTC', 'Volume_BTC', 'std_BTC', 'Return_BTC',\n       'Target_BTC', 'Count_ETH', 'Close_ETH', 'Volume_ETH',\n       'std_ETH', 'Return_ETH']]\ntrain, test, y_train, y_test = train_test_split(data.drop([\"Target_BTC\"], axis=1), data[\"Target_BTC\"],\n                                                random_state=10, test_size=0.20)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.244112Z","iopub.status.idle":"2021-12-15T12:25:42.244489Z","shell.execute_reply.started":"2021-12-15T12:25:42.244308Z","shell.execute_reply":"2021-12-15T12:25:42.244331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost as xgb\nfrom sklearn import metrics\n\ndef auc(m, train, test): \n    return (metrics.roc_auc_score(y_train,m.predict_proba(train)[:,1]),\n                            metrics.roc_auc_score(y_test,m.predict_proba(test)[:,1]))\n\n# Parameter Tuning\nmodel = xgb.XGBClassifier()\nparam_dist = {\"max_depth\": [10,30,50],\n              \"min_child_weight\" : [1,3,6],\n              \"n_estimators\": [200],\n              \"learning_rate\": [0.05, 0.1,0.16],}\ngrid_search = GridSearchCV(model, param_grid=param_dist, cv = 3, \n                                   verbose=10, n_jobs=-1)\ngrid_search.fit(train, y_train)\ngrid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.246015Z","iopub.status.idle":"2021-12-15T12:25:42.246583Z","shell.execute_reply.started":"2021-12-15T12:25:42.24638Z","shell.execute_reply":"2021-12-15T12:25:42.246403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.XGBClassifier(max_depth=10, min_child_weight=3,  n_estimators=200,\\\n                          n_jobs=-1 , verbose=1,learning_rate=0.05)\nmodel.fit(train,y_train)\nauc(model, train, test)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.247815Z","iopub.status.idle":"2021-12-15T12:25:42.248202Z","shell.execute_reply.started":"2021-12-15T12:25:42.248028Z","shell.execute_reply":"2021-12-15T12:25:42.248052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test)\nprint(confusion_matrix(y_test, y_pred))\naccuracy_score(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.249767Z","iopub.status.idle":"2021-12-15T12:25:42.250134Z","shell.execute_reply.started":"2021-12-15T12:25:42.249958Z","shell.execute_reply":"2021-12-15T12:25:42.249979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Light GBM","metadata":{}},{"cell_type":"code","source":"def auc2(m, train, test): \n    return (metrics.roc_auc_score(y_train,m.predict(train)),\n                            metrics.roc_auc_score(y_test,m.predict(test)))\n\nlg = lgb.LGBMClassifier(silent=False)\nparam_dist = {\"max_depth\": [25,50, 75],\n              \"learning_rate\" : [0.01,0.05,0.1],\n              \"num_leaves\": [300,900,1200],\n              \"n_estimators\": [200]\n             }\ngrid_search = GridSearchCV(lg, n_jobs=-1, param_grid=param_dist, cv = 3, scoring=\"roc_auc\", verbose=5)\ngrid_search.fit(train,y_train)\ngrid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.251398Z","iopub.status.idle":"2021-12-15T12:25:42.252067Z","shell.execute_reply.started":"2021-12-15T12:25:42.251853Z","shell.execute_reply":"2021-12-15T12:25:42.251877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"XGB Regression","metadata":{}},{"cell_type":"code","source":"d_train = lgb.Dataset(train, label=y_train)\nparams = {\"max_depth\": 25, \"learning_rate\" : 0.01, \"num_leaves\": 900,  \"n_estimators\": 200}\n\n\nmodel2 = lgb.train(params, d_train)\nauc2(model2, train, test)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.253454Z","iopub.status.idle":"2021-12-15T12:25:42.253777Z","shell.execute_reply.started":"2021-12-15T12:25:42.253595Z","shell.execute_reply":"2021-12-15T12:25:42.253612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.XGBRegressor()\nparam_dist = {\"max_depth\": [10,30,50],\n              \"subsample\" : [0.2,0.5,0.8,1],\n              \"n_estimators\": [200],\n              \"learning_rate\": [0.05, 0.1,0.16],}\ngrid_search = GridSearchCV(model, param_grid=param_dist, cv = 3, \n                                   verbose=10, n_jobs=-1)\ngrid_search.fit(train, y_train)\ngrid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.255162Z","iopub.status.idle":"2021-12-15T12:25:42.255487Z","shell.execute_reply.started":"2021-12-15T12:25:42.255321Z","shell.execute_reply":"2021-12-15T12:25:42.255342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = xgb.XGBRegressor(max_depth=10, subsample=0.5,  n_estimators=200,\\\n                          n_jobs=-1 , verbose=1,learning_rate=0.05)\nmodel.fit(train,y_train)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.256705Z","iopub.status.idle":"2021-12-15T12:25:42.257051Z","shell.execute_reply.started":"2021-12-15T12:25:42.256853Z","shell.execute_reply":"2021-12-15T12:25:42.256876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test)\nprint(r2_score(y_pred, y_test), 'R^2 for testing data')\ny_pred_train = model.predict(train)\nprint(r2_score(y_pred_train, y_train), 'R^2 for training data')\nprint(mean_absolute_error(y_test, y_pred), 'MAE for test')\nprint(mean_absolute_error(y_test, np.zeros((5049,))), 'MAE for RW')","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.258229Z","iopub.status.idle":"2021-12-15T12:25:42.258542Z","shell.execute_reply.started":"2021-12-15T12:25:42.258375Z","shell.execute_reply":"2021-12-15T12:25:42.258397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = Assets\ndata = data[['Count_BTC', 'Close_BTC', 'Volume_BTC', 'std_BTC', 'Return_BTC',\n       'Target_BTC', 'Count_ETH', 'Close_ETH', 'Volume_ETH',\n       'std_ETH', 'Return_ETH']]\ntrain, test, y_train, y_test = train_test_split(data.drop([\"Target_BTC\"], axis=1), data[\"Target_BTC\"],\n                                                random_state=10, test_size=0.20)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.260104Z","iopub.status.idle":"2021-12-15T12:25:42.26043Z","shell.execute_reply.started":"2021-12-15T12:25:42.260261Z","shell.execute_reply":"2021-12-15T12:25:42.260282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lg = lgb.LGBMRegressor(silent=False)\nparam_dist = {\"max_depth\": [25,50, 75],\n              \"learning_rate\" : [0.01,0.05,0.1],\n              \"num_leaves\": [300,900,1200],\n              \"n_estimators\": [100, 200]\n             }\ngrid_search = GridSearchCV(lg, n_jobs=-1, param_grid=param_dist, cv = 3, verbose=5)\ngrid_search.fit(train,y_train)\ngrid_search.best_estimator_","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.262015Z","iopub.status.idle":"2021-12-15T12:25:42.262327Z","shell.execute_reply.started":"2021-12-15T12:25:42.262153Z","shell.execute_reply":"2021-12-15T12:25:42.262168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = lgb.LGBMRegressor(max_depth=25, n_estimators=200, num_leaves=300,\n                          n_jobs=-1 , verbose=1,learning_rate=0.01)\nmodel.fit(train,y_train)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.263425Z","iopub.status.idle":"2021-12-15T12:25:42.263748Z","shell.execute_reply.started":"2021-12-15T12:25:42.263575Z","shell.execute_reply":"2021-12-15T12:25:42.263596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test)\nprint(r2_score(y_pred, y_test), 'R^2 for testing data')\n\ny_pred_train = model.predict(train)\nprint(r2_score(y_pred_train, y_train), 'R^2 for training data')\nprint(mean_absolute_error(y_test, y_pred), 'MAE for testing')\nprint(mean_absolute_error(y_test, np.zeros((5049,))), 'MAE for RW')\nprint(mean_absolute_error(y_test, y_pred)/mean_absolute_error(y_test, np.zeros((5049,))), 'MAE ratio')","metadata":{"execution":{"iopub.status.busy":"2021-12-15T12:25:42.265157Z","iopub.status.idle":"2021-12-15T12:25:42.265483Z","shell.execute_reply.started":"2021-12-15T12:25:42.265323Z","shell.execute_reply":"2021-12-15T12:25:42.265339Z"},"trusted":true},"execution_count":null,"outputs":[]}]}