{"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-01-04T09:32:28.343549Z","iopub.execute_input":"2022-01-04T09:32:28.344225Z","iopub.status.idle":"2022-01-04T09:32:28.384858Z","shell.execute_reply.started":"2022-01-04T09:32:28.344070Z","shell.execute_reply":"2022-01-04T09:32:28.384186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = '/kaggle/input/g-research-crypto-forecasting/train.csv'\ndata_asset_details = '/kaggle/input/g-research-crypto-forecasting/asset_details.csv'\ndata_supplemental_train = '/kaggle/input/g-research-crypto-forecasting/supplemental_train.csv'","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:32:28.386348Z","iopub.execute_input":"2022-01-04T09:32:28.386613Z","iopub.status.idle":"2022-01-04T09:32:28.392173Z","shell.execute_reply.started":"2022-01-04T09:32:28.386579Z","shell.execute_reply":"2022-01-04T09:32:28.391052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport pandas as pd\nimport numpy as np\nimport time\nimport datetime\nimport plotly.graph_objects as go","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:32:28.393956Z","iopub.execute_input":"2022-01-04T09:32:28.394588Z","iopub.status.idle":"2022-01-04T09:32:28.418929Z","shell.execute_reply.started":"2022-01-04T09:32:28.394539Z","shell.execute_reply":"2022-01-04T09:32:28.418184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(data_train, \n                       dtype={'Asset_ID': 'int8', 'Count': 'int32', 'row_id': 'int32', 'Count': 'int32', \n                              'Open': 'float64', 'High': 'float64', 'Low': 'float64', 'Close': 'float64', \n                              'Volume': 'float64', 'VWAP': 'float64'\n                             }\n                      )\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:32:28.421270Z","iopub.execute_input":"2022-01-04T09:32:28.422303Z","iopub.status.idle":"2022-01-04T09:33:32.111594Z","shell.execute_reply.started":"2022-01-04T09:32:28.422252Z","shell.execute_reply":"2022-01-04T09:33:32.109969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gresearch_crypto","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:32.113616Z","iopub.execute_input":"2022-01-04T09:33:32.114006Z","iopub.status.idle":"2022-01-04T09:33:32.143908Z","shell.execute_reply.started":"2022-01-04T09:33:32.113943Z","shell.execute_reply":"2022-01-04T09:33:32.143228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_assets = pd.read_csv(data_asset_details).sort_values(by='Asset_ID')\ndf_assets.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:32.145242Z","iopub.execute_input":"2022-01-04T09:33:32.145588Z","iopub.status.idle":"2022-01-04T09:33:32.173855Z","shell.execute_reply.started":"2022-01-04T09:33:32.145526Z","shell.execute_reply":"2022-01-04T09:33:32.172688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xgboost as xgb\n\n# Two new features from the competition tutorial\ndef upper_shadow(df):\n    return df['High'] - np.maximum(df['Close'], df['Open'])\n\ndef lower_shadow(df):\n    return np.minimum(df['Close'], df['Open']) - df['Low']\n\ndef get_features(df):\n    df_feat = df[['Count', 'Open', 'High', 'Low', 'Close', 'Volume', 'VWAP']].copy()\n    df_feat['Upper_Shadow'] = upper_shadow(df_feat)\n    df_feat['Lower_Shadow'] = lower_shadow(df_feat)\n    \n     ## Adding some more features\n    df_feat[\"Close/Open\"] = df_feat[\"Close\"] / df_feat[\"Open\"] \n    df_feat[\"Close-Open\"] = df_feat[\"Close\"] - df_feat[\"Open\"] \n    df_feat[\"High-Low\"] = df_feat[\"High\"] - df_feat[\"Low\"] \n    df_feat[\"High/Low\"] = df_feat[\"High\"] / df_feat[\"Low\"]\n    \n    df_feat['Mean'] = df_feat[['Open', 'High', 'Low', 'Close']].mean(axis=1)\n\n\n    df_feat[\"Median\"] = df_feat[[\"Open\", \"High\", \"Low\", \"Close\"]].median(axis=1)\n    return df_feat","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:32.175391Z","iopub.execute_input":"2022-01-04T09:33:32.175657Z","iopub.status.idle":"2022-01-04T09:33:33.590961Z","shell.execute_reply.started":"2022-01-04T09:33:32.175628Z","shell.execute_reply":"2022-01-04T09:33:33.589847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:33.592688Z","iopub.execute_input":"2022-01-04T09:33:33.593109Z","iopub.status.idle":"2022-01-04T09:33:33.597966Z","shell.execute_reply.started":"2022-01-04T09:33:33.593047Z","shell.execute_reply":"2022-01-04T09:33:33.597296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skopt import gp_minimize\nfrom skopt.space import Real, Integer\nfrom sklearn.model_selection import train_test_split\nfrom functools import partial","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:33.600414Z","iopub.execute_input":"2022-01-04T09:33:33.600763Z","iopub.status.idle":"2022-01-04T09:33:33.936436Z","shell.execute_reply.started":"2022-01-04T09:33:33.600724Z","shell.execute_reply":"2022-01-04T09:33:33.935166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def m(x, w):\n    return np.sum(x * w) / np.sum(w)\n\ndef cov(x, y, w):\n    return np.sum(w * (x - m(x, w)) * (y - m(y, w))) / np.sum(w)\n\ndef wcc(x, y, w=2.079441541679836):\n    return cov(x, y, w) / np.sqrt(cov(x, x, w) * cov(y, y, w))","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:33.937995Z","iopub.execute_input":"2022-01-04T09:33:33.938342Z","iopub.status.idle":"2022-01-04T09:33:33.945679Z","shell.execute_reply.started":"2022-01-04T09:33:33.938306Z","shell.execute_reply":"2022-01-04T09:33:33.944740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Asset_ID'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:33.947011Z","iopub.execute_input":"2022-01-04T09:33:33.947315Z","iopub.status.idle":"2022-01-04T09:33:34.081810Z","shell.execute_reply.started":"2022-01-04T09:33:33.947271Z","shell.execute_reply":"2022-01-04T09:33:34.080644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weight = df_assets[df_assets['Asset_ID'] == 7]['Weight'].values[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:34.083417Z","iopub.execute_input":"2022-01-04T09:33:34.083720Z","iopub.status.idle":"2022-01-04T09:33:34.093265Z","shell.execute_reply.started":"2022-01-04T09:33:34.083685Z","shell.execute_reply":"2022-01-04T09:33:34.092285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weight","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:34.094776Z","iopub.execute_input":"2022-01-04T09:33:34.095952Z","iopub.status.idle":"2022-01-04T09:33:34.106505Z","shell.execute_reply.started":"2022-01-04T09:33:34.095890Z","shell.execute_reply":"2022-01-04T09:33:34.105574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df_train[df_train['Asset_ID'] == 7].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:34.107880Z","iopub.execute_input":"2022-01-04T09:33:34.108945Z","iopub.status.idle":"2022-01-04T09:33:34.491821Z","shell.execute_reply.started":"2022-01-04T09:33:34.108889Z","shell.execute_reply":"2022-01-04T09:33:34.490794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_proc = get_features(df)\ndf_proc['y'] = df['Target']\ndf_proc = df_proc.dropna(how=\"any\")\n\n\nX = df_proc.drop(\"y\", axis=1)\ny = df_proc[\"y\"]\n\nscaler = StandardScaler()\nX = scaler.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:34.493314Z","iopub.execute_input":"2022-01-04T09:33:34.493595Z","iopub.status.idle":"2022-01-04T09:33:36.643791Z","shell.execute_reply.started":"2022-01-04T09:33:34.493561Z","shell.execute_reply":"2022-01-04T09:33:36.643152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col_names = df_proc.drop('y', axis=1).columns","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:36.645071Z","iopub.execute_input":"2022-01-04T09:33:36.645429Z","iopub.status.idle":"2022-01-04T09:33:36.710602Z","shell.execute_reply.started":"2022-01-04T09:33:36.645400Z","shell.execute_reply":"2022-01-04T09:33:36.709876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.75, random_state=42)\ntarget_col = \"y\"\ndf_train = pd.DataFrame(X_train, columns=col_names)\n#df_train.loc[:, target_col] = y_train\ndf_test = pd.DataFrame(X_test, columns=col_names)\n#df_test.loc[:, target_col] = y_test","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:33:36.711928Z","iopub.execute_input":"2022-01-04T09:33:36.712261Z","iopub.status.idle":"2022-01-04T09:33:37.287848Z","shell.execute_reply.started":"2022-01-04T09:33:36.712220Z","shell.execute_reply":"2022-01-04T09:33:37.287011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\nfrom sklearn.svm import SVR\ngrid={\"degree\": [2, 3, 4, 5, 6, 7], \n      'kernel':['linear', 'poly', 'rbf', 'sigmoid', 'precomputed'],\n     'C': [0.01,0.1,1,10,100, 1000],\n     'epsilon': [0.01, 0.1, 1, 10, 100, 1000]}\nsvr=SVR(max_iter = 5000, tol=1e-6)\nsvr_cv=GridSearchCV(svr,grid,cv=10, estimator=wcc)\nsvr_cv.fit(X_train, y_train)\n\nprint(\"tuned hpyerparameters :(best parameters) \",svr_cv.best_params_)\nprint(\"WCC :\",svr_cv.best_score_)\n\nprediction = svr_cv.predict(X_test)\n\n\nprint('WCC - ',wcc(prediction, y_test))","metadata":{"execution":{"iopub.status.busy":"2022-01-04T09:11:02.72373Z","iopub.execute_input":"2022-01-04T09:11:02.724041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}