{"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":"2023-01-12T13:52:58.482155Z","iopub.execute_input":"2023-01-12T13:52:58.482812Z","iopub.status.idle":"2023-01-12T13:52:58.511061Z","shell.execute_reply.started":"2023-01-12T13:52:58.482724Z","shell.execute_reply":"2023-01-12T13:52:58.509594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pyforest\nfrom pyforest import *","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:52:58.513270Z","iopub.execute_input":"2023-01-12T13:52:58.513971Z","iopub.status.idle":"2023-01-12T13:53:14.284861Z","shell.execute_reply.started":"2023-01-12T13:52:58.513937Z","shell.execute_reply":"2023-01-12T13:53:14.283716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lazy_imports()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:53:14.286423Z","iopub.execute_input":"2023-01-12T13:53:14.286830Z","iopub.status.idle":"2023-01-12T13:53:14.298648Z","shell.execute_reply.started":"2023-01-12T13:53:14.286798Z","shell.execute_reply":"2023-01-12T13:53:14.297455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:53:14.301607Z","iopub.execute_input":"2023-01-12T13:53:14.302033Z","iopub.status.idle":"2023-01-12T13:53:14.307905Z","shell.execute_reply.started":"2023-01-12T13:53:14.301994Z","shell.execute_reply":"2023-01-12T13:53:14.306970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=pd.read_csv(\"/kaggle/input/g-research-crypto-forecasting/train.csv\")\nasset= pd.read_csv(\"/kaggle/input/g-research-crypto-forecasting/asset_details.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:53:14.309347Z","iopub.execute_input":"2023-01-12T13:53:14.309854Z","iopub.status.idle":"2023-01-12T13:54:20.567522Z","shell.execute_reply.started":"2023-01-12T13:53:14.309814Z","shell.execute_reply":"2023-01-12T13:54:20.566390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:20.569150Z","iopub.execute_input":"2023-01-12T13:54:20.569921Z","iopub.status.idle":"2023-01-12T13:54:20.590636Z","shell.execute_reply.started":"2023-01-12T13:54:20.569877Z","shell.execute_reply":"2023-01-12T13:54:20.589362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"asset[[\"Asset_ID\",\"Asset_Name\"]].sort_values(\"Asset_ID\")","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:20.592149Z","iopub.execute_input":"2023-01-12T13:54:20.592571Z","iopub.status.idle":"2023-01-12T13:54:20.611256Z","shell.execute_reply.started":"2023-01-12T13:54:20.592540Z","shell.execute_reply":"2023-01-12T13:54:20.609823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:20.613261Z","iopub.execute_input":"2023-01-12T13:54:20.613686Z","iopub.status.idle":"2023-01-12T13:54:20.633696Z","shell.execute_reply.started":"2023-01-12T13:54:20.613654Z","shell.execute_reply":"2023-01-12T13:54:20.632478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d= train_df.loc[train_df[\"Asset_ID\"]==1]\ntrain_d.drop(\"Asset_ID\", axis=1, inplace=True)\ntrain_d[\"timestamp\"]= pd.to_datetime(train_d[\"timestamp\"], origin = \"unix\", unit=\"s\")\ntrain_d.set_index(\"timestamp\", inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:20.635367Z","iopub.execute_input":"2023-01-12T13:54:20.635952Z","iopub.status.idle":"2023-01-12T13:54:21.080688Z","shell.execute_reply.started":"2023-01-12T13:54:20.635906Z","shell.execute_reply":"2023-01-12T13:54:21.079782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_d.drop([\"Count\",\"Volume\",\"VWAP\",\"Target\"],inplace=True,axis=1)\ntrain_d","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:21.084468Z","iopub.execute_input":"2023-01-12T13:54:21.085004Z","iopub.status.idle":"2023-01-12T13:54:21.126250Z","shell.execute_reply.started":"2023-01-12T13:54:21.084972Z","shell.execute_reply":"2023-01-12T13:54:21.124988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d[\"Close\"].iloc[::1000].plot(figsize=(15,5), title=\"BTC/USD\")","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:21.127899Z","iopub.execute_input":"2023-01-12T13:54:21.128339Z","iopub.status.idle":"2023-01-12T13:54:21.631039Z","shell.execute_reply.started":"2023-01-12T13:54:21.128305Z","shell.execute_reply":"2023-01-12T13:54:21.629830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:21.632222Z","iopub.execute_input":"2023-01-12T13:54:21.632538Z","iopub.status.idle":"2023-01-12T13:54:21.656864Z","shell.execute_reply.started":"2023-01-12T13:54:21.632510Z","shell.execute_reply":"2023-01-12T13:54:21.655806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_d.index[1:] - train_d.index[:-1]).value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:21.658100Z","iopub.execute_input":"2023-01-12T13:54:21.658439Z","iopub.status.idle":"2023-01-12T13:54:21.709794Z","shell.execute_reply.started":"2023-01-12T13:54:21.658410Z","shell.execute_reply":"2023-01-12T13:54:21.708575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.index = pd.to_datetime(train_d.index)\ntrain_d=train_d.reindex(index=pd.date_range(train_d.index[0],train_d.index[-1], freq=\"1min\"), method=\"ffill\")\n\ntrain_d\n","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:21.711269Z","iopub.execute_input":"2023-01-12T13:54:21.711618Z","iopub.status.idle":"2023-01-12T13:54:22.062701Z","shell.execute_reply.started":"2023-01-12T13:54:21.711587Z","shell.execute_reply":"2023-01-12T13:54:22.061467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Original index range:\", train_d.index[0], \"-\", train_d.index[-1])\nprint(\"New index range:\", train_d.index[0], \"-\", train_d.index[-1])\n","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:22.063990Z","iopub.execute_input":"2023-01-12T13:54:22.064465Z","iopub.status.idle":"2023-01-12T13:54:22.071076Z","shell.execute_reply.started":"2023-01-12T13:54:22.064423Z","shell.execute_reply":"2023-01-12T13:54:22.069878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d[\"Target\"]= np.log(train_d[\"Close\"].shift(-15,freq=\"min\")/train_d[\"Close\"])\ntrain_d\n","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:22.072743Z","iopub.execute_input":"2023-01-12T13:54:22.073062Z","iopub.status.idle":"2023-01-12T13:54:23.142377Z","shell.execute_reply.started":"2023-01-12T13:54:22.073033Z","shell.execute_reply":"2023-01-12T13:54:23.141210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:23.145849Z","iopub.execute_input":"2023-01-12T13:54:23.146224Z","iopub.status.idle":"2023-01-12T13:54:23.174519Z","shell.execute_reply.started":"2023-01-12T13:54:23.146191Z","shell.execute_reply":"2023-01-12T13:54:23.173209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.dtypes\n","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:23.175996Z","iopub.execute_input":"2023-01-12T13:54:23.176454Z","iopub.status.idle":"2023-01-12T13:54:23.185570Z","shell.execute_reply.started":"2023-01-12T13:54:23.176407Z","shell.execute_reply":"2023-01-12T13:54:23.184508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:23.187488Z","iopub.execute_input":"2023-01-12T13:54:23.187944Z","iopub.status.idle":"2023-01-12T13:54:23.375338Z","shell.execute_reply.started":"2023-01-12T13:54:23.187897Z","shell.execute_reply":"2023-01-12T13:54:23.373998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_d[\"Target\"].iloc[::1000].plot(figsize=(15, 5),  title='Target')","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:23.376861Z","iopub.execute_input":"2023-01-12T13:54:23.377253Z","iopub.status.idle":"2023-01-12T13:54:26.293280Z","shell.execute_reply.started":"2023-01-12T13:54:23.377213Z","shell.execute_reply":"2023-01-12T13:54:26.292121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lags = [15, 30, 60, 60 * 2, 60 * 4, 60 * 24] # In minutes: 15min, 30min, 1h, 2h, 4h, 24h\nmas = [15, 60, 60 * 24, 60 * 24 * 7]         # In minutes: 15min, 1h, 1d, 7d\n\ndef create_features(df):\n    df= train_d.copy()\n    for lag in lags:\n        df[\"log_return_{}\".format(lag)]=np.log(df.Close.shift(lag, freq='min') / df.Close)\n    for ma in mas:\n        df[\"log_close_{}\".format(ma)]=np.log(df.Close / df.Close.rolling(window=ma).mean())\n    df['log_high/low'] = np.log(df.High / df.Low)\n    df['hour'] = df.index.hour\n    df['dayofweek'] = df.index.dayofweek\n    df['month'] = df.index.month\n    df['year'] = df.index.year\n    df.dropna(inplace=True)\n    \n    return df","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:26.294729Z","iopub.execute_input":"2023-01-12T13:54:26.295749Z","iopub.status.idle":"2023-01-12T13:54:26.306230Z","shell.execute_reply.started":"2023-01-12T13:54:26.295705Z","shell.execute_reply":"2023-01-12T13:54:26.304500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df= create_features(train_d)\ndf","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:26.307883Z","iopub.execute_input":"2023-01-12T13:54:26.308361Z","iopub.status.idle":"2023-01-12T13:54:32.779368Z","shell.execute_reply.started":"2023-01-12T13:54:26.308316Z","shell.execute_reply":"2023-01-12T13:54:32.778189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FEATURE = ['log_return_15', 'log_return_30', 'log_return_60', 'log_return_120', 'log_return_240', 'log_return_1440', 'log_close_ma_15', 'log_close_ma_60', 'log_close_ma_1440', 'log_close_ma_10080', 'log_high/low', 'hour', 'dayofweek', 'month', 'year']\nTARGET = 'Target'\n\nFEATURES = ['log_return_15', 'log_return_30', 'log_return_60', 'log_return_120', 'log_return_240', 'log_return_1440', 'log_high/low', 'hour', 'dayofweek', 'month', 'year']\n","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:32.781096Z","iopub.execute_input":"2023-01-12T13:54:32.781455Z","iopub.status.idle":"2023-01-12T13:54:32.787846Z","shell.execute_reply.started":"2023-01-12T13:54:32.781419Z","shell.execute_reply":"2023-01-12T13:54:32.786683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nfrom sklearn.preprocessing import StandardScaler","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:32.789111Z","iopub.execute_input":"2023-01-12T13:54:32.789577Z","iopub.status.idle":"2023-01-12T13:54:33.294301Z","shell.execute_reply.started":"2023-01-12T13:54:32.789540Z","shell.execute_reply":"2023-01-12T13:54:33.293161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"Close\"].loc[:'2021-03'].iloc[::1000].plot(figsize=(15, 5), label='train',  title='BTC/USD train/val split')\ndf[\"Close\"].loc['2021-04-08':].iloc[::1000].plot(label='val')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:33.295765Z","iopub.execute_input":"2023-01-12T13:54:33.296093Z","iopub.status.idle":"2023-01-12T13:54:37.310604Z","shell.execute_reply.started":"2023-01-12T13:54:33.296062Z","shell.execute_reply":"2023-01-12T13:54:37.309826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#x=df[FEATURE]\n#y=df[TARGET]\n#X_train,y_train,X_val,y_val = train_test_split(x,y, test_size=0.3,random_state=1 )","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:37.311973Z","iopub.execute_input":"2023-01-12T13:54:37.312475Z","iopub.status.idle":"2023-01-12T13:54:37.316287Z","shell.execute_reply.started":"2023-01-12T13:54:37.312442Z","shell.execute_reply":"2023-01-12T13:54:37.315491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#X_train=X_train.to_numpy()\n#y_train=y_train.to_numpy()\n#X_val=X_val.to_numpy()\n#y_val=y_val.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:37.317499Z","iopub.execute_input":"2023-01-12T13:54:37.318058Z","iopub.status.idle":"2023-01-12T13:54:37.327934Z","shell.execute_reply.started":"2023-01-12T13:54:37.318026Z","shell.execute_reply":"2023-01-12T13:54:37.326797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = df[FEATURES].loc[:'2021-03'].to_numpy()\ny_train = df[TARGET].loc[:'2021-03'].to_numpy()\nX_val = df[FEATURES].loc['2021-04-08':].to_numpy()\ny_val = df[TARGET].loc['2021-04-08':].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:37.334933Z","iopub.execute_input":"2023-01-12T13:54:37.335304Z","iopub.status.idle":"2023-01-12T13:54:37.505400Z","shell.execute_reply.started":"2023-01-12T13:54:37.335273Z","shell.execute_reply":"2023-01-12T13:54:37.504434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()\nX_train_scaled = scaler.fit_transform(X_train)\nX_val_scaled = scaler.transform(X_val)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:37.506867Z","iopub.execute_input":"2023-01-12T13:54:37.507426Z","iopub.status.idle":"2023-01-12T13:54:37.777875Z","shell.execute_reply.started":"2023-01-12T13:54:37.507382Z","shell.execute_reply":"2023-01-12T13:54:37.776768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reg = LinearRegression()\nreg.fit(X_train_scaled, y_train)\ny_pred = reg.predict(X_val_scaled)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:37.779200Z","iopub.execute_input":"2023-01-12T13:54:37.779526Z","iopub.status.idle":"2023-01-12T13:54:38.347873Z","shell.execute_reply.started":"2023-01-12T13:54:37.779498Z","shell.execute_reply":"2023-01-12T13:54:38.346067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Correlation between prediction and ground truth:', np.corrcoef(y_val, y_pred)[0, 1])","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:38.350032Z","iopub.execute_input":"2023-01-12T13:54:38.350900Z","iopub.status.idle":"2023-01-12T13:54:38.377280Z","shell.execute_reply.started":"2023-01-12T13:54:38.350840Z","shell.execute_reply":"2023-01-12T13:54:38.375387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import KFold\n\nn_splits = 6\nfolds = KFold(n_splits=n_splits)\n\nfig, axs = plt.subplots(n_splits, 1, figsize=(15, 5 * n_splits), sharex=True)\nfor split_idx, (train_idx, val_idx) in enumerate(folds.split(df[FEATURES])):\n    # Train set is not necessarily contiguous. We'll plot the parts separately so the figure looks nicer.\n    if split_idx > 0:\n        df.Close.iloc[train_idx[train_idx < val_idx[0]]].iloc[::1000].plot(ax=axs[split_idx],   label='train')\n    if split_idx < n_splits - 1:\n        df.Close.iloc[train_idx[train_idx > val_idx[-1]]].iloc[::1000].plot(ax=axs[split_idx],   label='train')\n    df.Close.iloc[val_idx].iloc[::1000].plot(ax=axs[split_idx],   label='val')\n    axs[split_idx].set_ylabel('Price ($)')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:54:38.379487Z","iopub.execute_input":"2023-01-12T13:54:38.380558Z","iopub.status.idle":"2023-01-12T13:55:11.727586Z","shell.execute_reply.started":"2023-01-12T13:54:38.380481Z","shell.execute_reply":"2023-01-12T13:55:11.726622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cv(folds, params):\n    # Record rmse and corr for each fold\n    train_rmse, train_corr, val_rmse, val_corr = [], [], [], []\n    # Collect the trained models for each fold\n    models = []\n    for split_idx, (train_idx, val_idx) in enumerate(folds.split(df[FEATURES])):\n        print('Current fold:', split_idx + 1, '/', n_splits)\n        # Locate train and validation sets for this fold\n        train_set = lgb.Dataset(df[FEATURES].iloc[train_idx], df.Target.iloc[train_idx])\n        val_set = lgb.Dataset(df[FEATURES].iloc[val_idx], df.Target.iloc[val_idx])\n        # Training the model\n        model = lgb.train(\n            params=params,\n            train_set=train_set,\n            valid_sets=[train_set, val_set],\n            valid_names=['train', 'val'],\n            num_boost_round=5000,              # Number of boosting iterations (if we don't stop early)\n            feval=corr,                        # Measure correlation in addition to rmse\n            callbacks=[\n                lgb.early_stopping(50),        # Stop training if metrics don't improve in 50 iterations\n                lgb.log_evaluation(100)        # Print metrics every 100 iterations\n            ]\n        )\n        # Record results from this fold\n        train_rmse.append(model.best_score['train']['rmse'])\n        train_corr.append(model.best_score['train']['corr'])\n        val_rmse.append(model.best_score['val']['rmse'])\n        val_corr.append(model.best_score['val']['corr'])\n        models.append(model)\n        print('==============================================================')\n    print('Average over folds:')\n    print(f'\\ttrain\\'s rmse = {np.mean(train_rmse):.5f} ± {np.std(train_rmse):.5f}, corr = {np.mean(train_corr):.5f} ± {np.std(train_corr):.5f}')\n    print(f'\\tval\\'s   rmse = {np.mean(val_rmse):.5f} ± {np.std(val_rmse):.5f}, corr = {np.mean(val_corr):.5f} ± {np.std(val_corr):.5f}')\n    return train_rmse, train_corr, val_rmse, val_corr, models","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:55:11.728542Z","iopub.execute_input":"2023-01-12T13:55:11.728844Z","iopub.status.idle":"2023-01-12T13:55:11.740975Z","shell.execute_reply.started":"2023-01-12T13:55:11.728816Z","shell.execute_reply":"2023-01-12T13:55:11.740022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import TimeSeriesSplit\n\ntss = TimeSeriesSplit(n_splits=n_splits, gap=60 * 24 * 7, test_size=len(df) // (n_splits + 2))\n\nfig, axs = plt.subplots(n_splits, 1, figsize=(15, 5 * n_splits), sharex=True, sharey=True)\nfor split_idx, (train_idx, val_idx) in enumerate(tss.split(df[FEATURES])):\n    df.Close.iloc[train_idx].iloc[::1000].plot(ax=axs[split_idx], label='train')\n    df.Close.iloc[val_idx].iloc[::1000].plot(ax=axs[split_idx], label='val')\n    axs[split_idx].set_ylabel('Price ($)')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:55:11.742851Z","iopub.execute_input":"2023-01-12T13:55:11.743559Z","iopub.status.idle":"2023-01-12T13:55:38.220873Z","shell.execute_reply.started":"2023-01-12T13:55:11.743525Z","shell.execute_reply":"2023-01-12T13:55:38.219816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import lightgbm as lgb\n\ndef corr(y_preds, eval_data):\n    '''\n    Metric for use during training, gives the correlation between\n    predicted target and true value.\n    This is supposed to return a tuple:\n    (Name of metric, value of metric, whether higher is better)\n    '''\n    y = eval_data.label\n    corr = np.corrcoef(y_preds, y)[0, 1]\n    return 'corr', corr, True","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:55:38.222080Z","iopub.execute_input":"2023-01-12T13:55:38.222488Z","iopub.status.idle":"2023-01-12T13:55:39.049739Z","shell.execute_reply.started":"2023-01-12T13:55:38.222460Z","shell.execute_reply":"2023-01-12T13:55:39.048738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = {\n    'objective': 'regression',     # We are doing regression\n    'metric': 'rmse',              # Optimize RMSE\n    'learning_rate': 0.05,         # Learning rate for gradient boosting\n    'max_depth': 5,                # Max depth for regression trees\n    'num_leaves': 5,               # Number of leaves in a single decision tree\n    'verbose': -1                  # Print errors and warnings only\n}\n\n\ntrain_rmse, train_corr, val_rmse, val_corr, models = cv(tss, params)","metadata":{"execution":{"iopub.status.busy":"2023-01-12T13:55:39.051180Z","iopub.execute_input":"2023-01-12T13:55:39.051497Z","iopub.status.idle":"2023-01-12T13:56:23.136126Z","shell.execute_reply.started":"2023-01-12T13:55:39.051468Z","shell.execute_reply":"2023-01-12T13:56:23.135119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}