{"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-05T10:37:52.858346Z","iopub.execute_input":"2022-01-05T10:37:52.858757Z","iopub.status.idle":"2022-01-05T10:37:52.897086Z","shell.execute_reply.started":"2022-01-05T10:37:52.85862Z","shell.execute_reply":"2022-01-05T10:37:52.895123Z"},"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-05T10:37:52.898357Z","iopub.execute_input":"2022-01-05T10:37:52.898575Z","iopub.status.idle":"2022-01-05T10:37:52.903327Z","shell.execute_reply.started":"2022-01-05T10:37:52.898549Z","shell.execute_reply":"2022-01-05T10:37:52.902336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport pandas as pd\nimport numpy as np\nimport lightgbm as lgb\nimport time\nimport datetime\nimport plotly.graph_objects as go","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:37:52.904824Z","iopub.execute_input":"2022-01-05T10:37:52.905153Z","iopub.status.idle":"2022-01-05T10:37:54.973722Z","shell.execute_reply.started":"2022-01-05T10:37:52.905121Z","shell.execute_reply":"2022-01-05T10:37:54.972903Z"},"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-05T10:37:54.975322Z","iopub.execute_input":"2022-01-05T10:37:54.975905Z","iopub.status.idle":"2022-01-05T10:38:51.123376Z","shell.execute_reply.started":"2022-01-05T10:37:54.975863Z","shell.execute_reply":"2022-01-05T10:38:51.122574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gresearch_crypto","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.124631Z","iopub.execute_input":"2022-01-05T10:38:51.129357Z","iopub.status.idle":"2022-01-05T10:38:51.154966Z","shell.execute_reply.started":"2022-01-05T10:38:51.129304Z","shell.execute_reply":"2022-01-05T10:38:51.15424Z"},"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-05T10:38:51.155972Z","iopub.execute_input":"2022-01-05T10:38:51.156629Z","iopub.status.idle":"2022-01-05T10:38:51.178901Z","shell.execute_reply.started":"2022-01-05T10:38:51.156594Z","shell.execute_reply":"2022-01-05T10:38:51.178076Z"},"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-05T10:38:51.179854Z","iopub.execute_input":"2022-01-05T10:38:51.180518Z","iopub.status.idle":"2022-01-05T10:38:51.264666Z","shell.execute_reply.started":"2022-01-05T10:38:51.180481Z","shell.execute_reply":"2022-01-05T10:38:51.263944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.265835Z","iopub.execute_input":"2022-01-05T10:38:51.266241Z","iopub.status.idle":"2022-01-05T10:38:51.270471Z","shell.execute_reply.started":"2022-01-05T10:38:51.266199Z","shell.execute_reply":"2022-01-05T10:38:51.269338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import f1_score\nimport pandas as pd\nfrom 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-05T10:38:51.271811Z","iopub.execute_input":"2022-01-05T10:38:51.272501Z","iopub.status.idle":"2022-01-05T10:38:51.516157Z","shell.execute_reply.started":"2022-01-05T10:38:51.272456Z","shell.execute_reply":"2022-01-05T10:38:51.515215Z"},"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):\n    return cov(x, y, w) / np.sqrt(cov(x, x, w) * cov(y, y, w))","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.519112Z","iopub.execute_input":"2022-01-05T10:38:51.519352Z","iopub.status.idle":"2022-01-05T10:38:51.527711Z","shell.execute_reply.started":"2022-01-05T10:38:51.519323Z","shell.execute_reply":"2022-01-05T10:38:51.526726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"space = [\n    Real(0.5, 0.8, name=\"colsample_bytree\"),\n    Real(0.001, 0.5, name=\"learning_rate\"),\n    Integer(6, 15, name=\"max_depth\"),\n    Integer(100, 1000, name=\"n_estimators\"),\n    Real(0.5, 0.95, name=\"subsample\"),\n]","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.52907Z","iopub.execute_input":"2022-01-05T10:38:51.529564Z","iopub.status.idle":"2022-01-05T10:38:51.544014Z","shell.execute_reply.started":"2022-01-05T10:38:51.529516Z","shell.execute_reply":"2022-01-05T10:38:51.54316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ratio = 0.75","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.546426Z","iopub.execute_input":"2022-01-05T10:38:51.546846Z","iopub.status.idle":"2022-01-05T10:38:51.556191Z","shell.execute_reply.started":"2022-01-05T10:38:51.546649Z","shell.execute_reply":"2022-01-05T10:38:51.555357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['Asset_ID'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.557522Z","iopub.execute_input":"2022-01-05T10:38:51.55818Z","iopub.status.idle":"2022-01-05T10:38:51.685623Z","shell.execute_reply.started":"2022-01-05T10:38:51.558145Z","shell.execute_reply":"2022-01-05T10:38:51.68488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weight = df_assets[df_assets['Asset_ID'] == 12]['Weight'].values[0]","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.686904Z","iopub.execute_input":"2022-01-05T10:38:51.687193Z","iopub.status.idle":"2022-01-05T10:38:51.692532Z","shell.execute_reply.started":"2022-01-05T10:38:51.687166Z","shell.execute_reply":"2022-01-05T10:38:51.69172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"weight","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.693786Z","iopub.execute_input":"2022-01-05T10:38:51.693989Z","iopub.status.idle":"2022-01-05T10:38:51.705246Z","shell.execute_reply.started":"2022-01-05T10:38:51.693964Z","shell.execute_reply":"2022-01-05T10:38:51.704559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df_train[df_train['Asset_ID'] == 12].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.706759Z","iopub.execute_input":"2022-01-05T10:38:51.707594Z","iopub.status.idle":"2022-01-05T10:38:51.979718Z","shell.execute_reply.started":"2022-01-05T10:38:51.707518Z","shell.execute_reply":"2022-01-05T10:38:51.978755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:51.980992Z","iopub.execute_input":"2022-01-05T10:38:51.981658Z","iopub.status.idle":"2022-01-05T10:38:52.003626Z","shell.execute_reply.started":"2022-01-05T10:38:51.981614Z","shell.execute_reply":"2022-01-05T10:38:52.002643Z"},"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\")\ndf_proc = df_proc.reset_index(drop=True)\ndf_proc = df_proc.drop(labels=np.where(np.isinf(df_proc))[0], axis=0)\ndf_proc = df_proc.reset_index(drop=True)\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-05T10:38:52.004832Z","iopub.execute_input":"2022-01-05T10:38:52.005062Z","iopub.status.idle":"2022-01-05T10:38:53.828322Z","shell.execute_reply.started":"2022-01-05T10:38:52.005034Z","shell.execute_reply":"2022-01-05T10:38:53.827201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_proc","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:53.829531Z","iopub.execute_input":"2022-01-05T10:38:53.82979Z","iopub.status.idle":"2022-01-05T10:38:53.858294Z","shell.execute_reply.started":"2022-01-05T10:38:53.829761Z","shell.execute_reply":"2022-01-05T10:38:53.857728Z"},"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-05T10:38:53.859433Z","iopub.execute_input":"2022-01-05T10:38:53.85981Z","iopub.status.idle":"2022-01-05T10:38:53.912382Z","shell.execute_reply.started":"2022-01-05T10:38:53.859761Z","shell.execute_reply":"2022-01-05T10:38:53.911546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col_names","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:53.913512Z","iopub.execute_input":"2022-01-05T10:38:53.913782Z","iopub.status.idle":"2022-01-05T10:38:53.926447Z","shell.execute_reply.started":"2022-01-05T10:38:53.913749Z","shell.execute_reply":"2022-01-05T10:38:53.925865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:53.927639Z","iopub.execute_input":"2022-01-05T10:38:53.928049Z","iopub.status.idle":"2022-01-05T10:38:53.939001Z","shell.execute_reply.started":"2022-01-05T10:38:53.928006Z","shell.execute_reply":"2022-01-05T10:38:53.938143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:53.940422Z","iopub.execute_input":"2022-01-05T10:38:53.940906Z","iopub.status.idle":"2022-01-05T10:38:53.950798Z","shell.execute_reply.started":"2022-01-05T10:38:53.940865Z","shell.execute_reply":"2022-01-05T10:38:53.949754Z"},"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=1 - train_ratio, 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-05T10:38:53.952097Z","iopub.execute_input":"2022-01-05T10:38:53.952487Z","iopub.status.idle":"2022-01-05T10:38:54.718035Z","shell.execute_reply.started":"2022-01-05T10:38:53.952444Z","shell.execute_reply":"2022-01-05T10:38:54.716993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:54.71926Z","iopub.execute_input":"2022-01-05T10:38:54.719504Z","iopub.status.idle":"2022-01-05T10:38:54.725261Z","shell.execute_reply.started":"2022-01-05T10:38:54.719468Z","shell.execute_reply":"2022-01-05T10:38:54.724194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:54.726587Z","iopub.execute_input":"2022-01-05T10:38:54.727068Z","iopub.status.idle":"2022-01-05T10:38:54.737667Z","shell.execute_reply.started":"2022-01-05T10:38:54.727024Z","shell.execute_reply":"2022-01-05T10:38:54.73676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def return_model_assessment(args, X_train, y_train, X_test, w):\n    global models, train_scores, test_scores, curr_model_hyper_params\n    params = {curr_model_hyper_params[i]: args[i] for i, j in enumerate(curr_model_hyper_params)}\n    model = xgb.XGBRegressor(missing=-999, random_state=2022)\n    model.set_params(**params)\n    fitted_model = model.fit(X_train, y_train, sample_weight=None)\n    models.append(fitted_model)\n    train_predictions = model.predict(X_train)\n    test_predictions = model.predict(X_test)\n    train_score = wcc(train_predictions, y_train, w)\n    test_score = wcc(test_predictions, y_test, w)\n    train_scores.append(train_score)\n    test_scores.append(test_score)\n    return 1 - test_score","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:54.738761Z","iopub.execute_input":"2022-01-05T10:38:54.739647Z","iopub.status.idle":"2022-01-05T10:38:54.749576Z","shell.execute_reply.started":"2022-01-05T10:38:54.739611Z","shell.execute_reply":"2022-01-05T10:38:54.748775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = []\ntrain_scores = []\ntest_scores = []\ncurr_model_hyper_params = ['colsample_bytree', 'learning_rate',\n                        'max_depth', 'n_estimators', 'subsample']\nobjective_function = partial(return_model_assessment, X_train=X_train, y_train=y_train, X_test=X_test, w=weight)\n\n# running the algorithm\nn_calls = 10 # number of times you want to train your model\nresults = gp_minimize(objective_function, space, base_estimator=None, n_calls=10, n_random_starts=n_calls-1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:38:54.753995Z","iopub.execute_input":"2022-01-05T10:38:54.754235Z","iopub.status.idle":"2022-01-05T10:39:38.133067Z","shell.execute_reply.started":"2022-01-05T10:38:54.754207Z","shell.execute_reply":"2022-01-05T10:39:38.131482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(results)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:39:38.13416Z","iopub.status.idle":"2022-01-05T10:39:38.134991Z","shell.execute_reply.started":"2022-01-05T10:39:38.13472Z","shell.execute_reply":"2022-01-05T10:39:38.134749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.express as px\nmetrics = pd.DataFrame(train_scores + test_scores)\nmetrics.loc[:,'dataset'] = [\"train_score\"]*n_calls + [\"test_score\"]*n_calls\nmetrics.loc[:,'Iteration Number'] = list(range(1,n_calls+1)) + list(range(1,n_calls+1))\nmetrics.columns = [\"WCC\", \"dataset\", \"Iteration Number\"]\nfig = px.line(metrics, x=\"Iteration Number\", y=\"WCC\", color=\"dataset\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-05T10:39:38.136858Z","iopub.status.idle":"2022-01-05T10:39:38.137714Z","shell.execute_reply.started":"2022-01-05T10:39:38.13738Z","shell.execute_reply":"2022-01-05T10:39:38.137412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(results.x)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}