{"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# 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-05-04T04:38:56.068133Z","iopub.execute_input":"2022-05-04T04:38:56.068462Z","iopub.status.idle":"2022-05-04T04:38:56.078161Z","shell.execute_reply.started":"2022-05-04T04:38:56.068416Z","shell.execute_reply":"2022-05-04T04:38:56.077248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Importing required libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom datetime import datetime\nfrom sklearn.model_selection import GridSearchCV, train_test_split\nfrom sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import mean_squared_error\nfrom matplotlib import pyplot as plt\nimport gresearch_crypto\nfrom sklearn.preprocessing import StandardScaler\nimport warnings\nwarnings.filterwarnings('ignore')\nfrom sklearn.svm import SVR\nfrom sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor, AdaBoostRegressor\nfrom xgboost import XGBRegressor","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:38:56.080489Z","iopub.execute_input":"2022-05-04T04:38:56.080713Z","iopub.status.idle":"2022-05-04T04:38:56.091866Z","shell.execute_reply.started":"2022-05-04T04:38:56.080684Z","shell.execute_reply":"2022-05-04T04:38:56.090811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Reading the input files","metadata":{}},{"cell_type":"code","source":"asset_details = pd.read_csv(\"../input/g-research-crypto-forecasting/asset_details.csv\")\ntrain_data = pd.read_csv(\"../input/g-research-crypto-forecasting/train.csv\")\ntarget = 'Target'","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:38:56.093181Z","iopub.execute_input":"2022-05-04T04:38:56.093616Z","iopub.status.idle":"2022-05-04T04:39:28.615741Z","shell.execute_reply.started":"2022-05-04T04:38:56.093584Z","shell.execute_reply":"2022-05-04T04:39:28.614952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_data.groupby(['Asset_ID']).head(2000).reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:28.617868Z","iopub.execute_input":"2022-05-04T04:39:28.618340Z","iopub.status.idle":"2022-05-04T04:39:30.281240Z","shell.execute_reply.started":"2022-05-04T04:39:28.618297Z","shell.execute_reply":"2022-05-04T04:39:30.280294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Transformation","metadata":{}},{"cell_type":"code","source":"def transform_data(df, scaler_obj=None):\n    df = df.merge(asset_details, on='Asset_ID', how='left')\n    df['time_format'] = pd.to_datetime(df['timestamp'], unit='s')\n    df['upper_shadow'] = df['High'] / df[['Close', 'Open']].max(axis=1)\n    df['lower_shadow'] = df[['Close', 'Open']].min(axis=1) / df['Low']\n    df['high_low'] = df['High'] / df['Low']\n    mean_price = df[['Low', 'High', 'Open', 'Close']].mean(axis=1)\n    median_price = df[['Low', 'High', 'Open', 'Close']].median(axis=1)\n    df['high_mean'] = df['High'] / mean_price\n    df['low_mean'] = df['Low'] / mean_price\n    df['high_median'] = df['High'] / median_price\n    df['low_median'] = df['Low'] / median_price\n    df['volume_count'] = df['Volume'] / (df['Count'] + 1)\n    features = ['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP', 'Weight', 'upper_shadow', 'lower_shadow', 'high_low', 'high_mean', 'high_median', 'low_mean', 'low_median', 'volume_count']\n    if scaler_obj:\n        scaled_values = scaler_obj.transform(df[features])\n    else:\n        scaler_obj = StandardScaler()\n        scaled_values = scaler_obj.fit_transform(df[features])\n    return pd.DataFrame(scaled_values, columns=features), scaler_obj","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.282543Z","iopub.execute_input":"2022-05-04T04:39:30.282776Z","iopub.status.idle":"2022-05-04T04:39:30.292732Z","shell.execute_reply.started":"2022-05-04T04:39:30.282750Z","shell.execute_reply":"2022-05-04T04:39:30.291922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fitting the model and finding the mean squarred error","metadata":{}},{"cell_type":"code","source":"def calculate_mse(cv_model, cv_params, x_train, x_test, y_train, y_test):\n    gs_model = GridSearchCV(cv_model, cv_params, cv=3)\n    gs_model.fit(x_train, y_train)\n    best_model = gs_model.best_estimator_\n    y_pred = best_model.predict(x_test)\n    print(mean_squared_error(y_pred, y_test))\n    return best_model, mean_squared_error(y_pred, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.293762Z","iopub.execute_input":"2022-05-04T04:39:30.294042Z","iopub.status.idle":"2022-05-04T04:39:30.307758Z","shell.execute_reply.started":"2022-05-04T04:39:30.294016Z","shell.execute_reply":"2022-05-04T04:39:30.306821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Linear Regression","metadata":{}},{"cell_type":"code","source":"cv_model = LinearRegression()\ncv_params = {}","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.309405Z","iopub.execute_input":"2022-05-04T04:39:30.309785Z","iopub.status.idle":"2022-05-04T04:39:30.321021Z","shell.execute_reply.started":"2022-05-04T04:39:30.309753Z","shell.execute_reply":"2022-05-04T04:39:30.319995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SVM","metadata":{}},{"cell_type":"code","source":"cv_model = SVR()\ncv_params = {'max_iter':[100, 200, 250], 'C':np.logspace(1, 7, 15)}","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.322206Z","iopub.execute_input":"2022-05-04T04:39:30.322590Z","iopub.status.idle":"2022-05-04T04:39:30.333815Z","shell.execute_reply.started":"2022-05-04T04:39:30.322546Z","shell.execute_reply":"2022-05-04T04:39:30.332909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Random Forest","metadata":{}},{"cell_type":"code","source":"cv_model = RandomForestRegressor()\ncv_params = {'ccp_alpha': np.logspace(-2,-3, 12), 'random_state':[1], 'n_estimators':[50, 100], 'n_jobs':[4]}","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.336406Z","iopub.execute_input":"2022-05-04T04:39:30.337138Z","iopub.status.idle":"2022-05-04T04:39:30.347182Z","shell.execute_reply.started":"2022-05-04T04:39:30.337102Z","shell.execute_reply":"2022-05-04T04:39:30.346128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Gradient Boosting Regressor","metadata":{}},{"cell_type":"code","source":"cv_model = GradientBoostingRegressor()\ncv_params = {'ccp_alpha':np.logspace(-1, -2, 4), 'loss':['ls'], 'random_state': [1]}","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.348371Z","iopub.execute_input":"2022-05-04T04:39:30.348984Z","iopub.status.idle":"2022-05-04T04:39:30.360326Z","shell.execute_reply.started":"2022-05-04T04:39:30.348945Z","shell.execute_reply":"2022-05-04T04:39:30.359392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ada Boosting Regressor","metadata":{}},{"cell_type":"code","source":"# cv_model = AdaBoostRegressor()\n# cv_params = {}","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.362243Z","iopub.execute_input":"2022-05-04T04:39:30.362970Z","iopub.status.idle":"2022-05-04T04:39:30.372218Z","shell.execute_reply.started":"2022-05-04T04:39:30.362933Z","shell.execute_reply":"2022-05-04T04:39:30.371481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## XGBRegressor","metadata":{}},{"cell_type":"code","source":"# cv_model = XGBRegressor()\n# cv_params = {'n_jobs':[4]}","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.373364Z","iopub.execute_input":"2022-05-04T04:39:30.373958Z","iopub.status.idle":"2022-05-04T04:39:30.384923Z","shell.execute_reply.started":"2022-05-04T04:39:30.373917Z","shell.execute_reply":"2022-05-04T04:39:30.384238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_model.get_params()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.386183Z","iopub.execute_input":"2022-05-04T04:39:30.386594Z","iopub.status.idle":"2022-05-04T04:39:30.403378Z","shell.execute_reply.started":"2022-05-04T04:39:30.386551Z","shell.execute_reply":"2022-05-04T04:39:30.402505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transforming test data","metadata":{}},{"cell_type":"code","source":"train_data.replace([np.inf, -np.inf], np.nan, inplace=True)\ntrain_data.dropna(inplace=True)\nx_train, x_test, y_train, y_test = train_test_split(train_data, train_data[target], test_size=0.2, random_state=1)\nfeature_df, scaler_obj = transform_data(x_train)\nx_test, scaler_obj = transform_data(x_test, scaler_obj)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.404918Z","iopub.execute_input":"2022-05-04T04:39:30.405578Z","iopub.status.idle":"2022-05-04T04:39:30.467981Z","shell.execute_reply.started":"2022-05-04T04:39:30.405536Z","shell.execute_reply":"2022-05-04T04:39:30.467275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model, mse = calculate_mse(cv_model, cv_params, feature_df, x_test, y_train, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:39:30.469216Z","iopub.execute_input":"2022-05-04T04:39:30.469551Z","iopub.status.idle":"2022-05-04T04:40:50.735120Z","shell.execute_reply.started":"2022-05-04T04:39:30.469522Z","shell.execute_reply":"2022-05-04T04:40:50.734216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:40:50.736528Z","iopub.execute_input":"2022-05-04T04:40:50.736965Z","iopub.status.idle":"2022-05-04T04:40:50.743366Z","shell.execute_reply.started":"2022-05-04T04:40:50.736928Z","shell.execute_reply":"2022-05-04T04:40:50.742606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Mean Squared Error Values\n* Linear Regression -> 8.979067012125199e-05\n* SVR -> 0.00029163926796389863\n* Random Forest Regressor -> 9.053569723455033e-05\n* Gradient Boosting Regressor -> 9.053079163170843e-05\n* AdaBoost Regressor -> 0.00014881919823361302\n* XGB Regressor -> 8.08510457127688e-05","metadata":{}},{"cell_type":"markdown","source":"## Preparing the test environment","metadata":{}},{"cell_type":"markdown","source":"**Commented out the test environment to avoid errors**","metadata":{}},{"cell_type":"code","source":"# import gresearch_crypto\n# env = gresearch_crypto.make_env()\n# iter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:40:50.744757Z","iopub.execute_input":"2022-05-04T04:40:50.745252Z","iopub.status.idle":"2022-05-04T04:40:50.755984Z","shell.execute_reply.started":"2022-05-04T04:40:50.745219Z","shell.execute_reply":"2022-05-04T04:40:50.754964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Running against the test environment","metadata":{}},{"cell_type":"code","source":"# for (test_df, sample_prediction_df) in iter_test:\n#     scaled_data, scaled_obj = transform_data(test_df, scaler_obj)\n#     y_pred = best_model.predict(scaled_data)\n#     sample_prediction_df['Target'] = y_pred\n#     env.predict(sample_prediction_df)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T04:40:50.757307Z","iopub.execute_input":"2022-05-04T04:40:50.757600Z","iopub.status.idle":"2022-05-04T04:40:50.769708Z","shell.execute_reply.started":"2022-05-04T04:40:50.757547Z","shell.execute_reply":"2022-05-04T04:40:50.768874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}