{"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-07-08T07:14:44.817545Z","iopub.execute_input":"2022-07-08T07:14:44.818154Z","iopub.status.idle":"2022-07-08T07:14:44.829655Z","shell.execute_reply.started":"2022-07-08T07:14:44.818119Z","shell.execute_reply":"2022-07-08T07:14:44.828833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import mean_squared_error, r2_score\nimport lightgbm as lgbm","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:14:44.831481Z","iopub.execute_input":"2022-07-08T07:14:44.832104Z","iopub.status.idle":"2022-07-08T07:14:44.843188Z","shell.execute_reply.started":"2022-07-08T07:14:44.832061Z","shell.execute_reply":"2022-07-08T07:14:44.841997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = (\n    pd.read_csv(\"/kaggle/input/ai-finance-korea/model_data.csv\", parse_dates=['date'])\n    .set_index(['symbol', 'date'])\n    .sort_index()\n    .dropna()\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:14:44.844590Z","iopub.execute_input":"2022-07-08T07:14:44.844922Z","iopub.status.idle":"2022-07-08T07:15:03.615815Z","shell.execute_reply.started":"2022-07-08T07:14:44.844893Z","shell.execute_reply":"2022-07-08T07:15:03.614068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_columns = ['dollar_vol_rank', 'rsi', 'bb_high', 'bb_low', 'NATR', 'ATR', 'PPO',\n       'MACD', 'r01', 'r05', 'r10', 'r21', 'r42', 'r63', 'sector', 'r01dec',\n       'r05dec', 'r10dec', 'r21dec', 'r42dec', 'r63dec', 'r01q_sector',\n       'r05q_sector', 'r10q_sector', 'r21q_sector', 'r42q_sector',\n       'r63q_sector', 'year', 'month', 'weekday']","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:15:03.617551Z","iopub.execute_input":"2022-07-08T07:15:03.617926Z","iopub.status.idle":"2022-07-08T07:15:03.624564Z","shell.execute_reply.started":"2022-07-08T07:15:03.617885Z","shell.execute_reply":"2022-07-08T07:15:03.623360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = sorted(data.filter(like='_fwd').columns)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:15:03.627732Z","iopub.execute_input":"2022-07-08T07:15:03.628156Z","iopub.status.idle":"2022-07-08T07:15:03.648011Z","shell.execute_reply.started":"2022-07-08T07:15:03.628121Z","shell.execute_reply":"2022-07-08T07:15:03.647130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:15:03.649470Z","iopub.execute_input":"2022-07-08T07:15:03.649984Z","iopub.status.idle":"2022-07-08T07:15:03.655935Z","shell.execute_reply.started":"2022-07-08T07:15:03.649953Z","shell.execute_reply":"2022-07-08T07:15:03.655123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5일 forward return을 Y로 사용","metadata":{}},{"cell_type":"code","source":"X = data.loc[:, feature_columns]","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:15:03.657318Z","iopub.execute_input":"2022-07-08T07:15:03.657820Z","iopub.status.idle":"2022-07-08T07:15:03.828539Z","shell.execute_reply.started":"2022-07-08T07:15:03.657789Z","shell.execute_reply":"2022-07-08T07:15:03.827678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = data[['r05_fwd']]","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:15:03.829924Z","iopub.execute_input":"2022-07-08T07:15:03.830294Z","iopub.status.idle":"2022-07-08T07:15:03.839178Z","shell.execute_reply.started":"2022-07-08T07:15:03.830261Z","shell.execute_reply":"2022-07-08T07:15:03.837823Z"},"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=.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:15:03.840998Z","iopub.execute_input":"2022-07-08T07:15:03.841447Z","iopub.status.idle":"2022-07-08T07:15:04.895393Z","shell.execute_reply.started":"2022-07-08T07:15:03.841413Z","shell.execute_reply":"2022-07-08T07:15:04.893817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Parameter Settings","metadata":{}},{"cell_type":"code","source":"params = {'learning_rate': 0.01, \n          'max_depth': 16, \n          'boosting': 'gbdt', \n          'objective': 'regression', \n          'metric': 'mse', \n          'is_training_metric': True, \n          'num_leaves': 144, \n          'feature_fraction': 0.9, \n          'bagging_fraction': 0.7, \n          'bagging_freq': 5, \n          'seed':2018}","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:15:04.897142Z","iopub.execute_input":"2022-07-08T07:15:04.897596Z","iopub.status.idle":"2022-07-08T07:15:04.905128Z","shell.execute_reply.started":"2022-07-08T07:15:04.897561Z","shell.execute_reply":"2022-07-08T07:15:04.902998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## LightGBM Dataset 형태로 변경","metadata":{}},{"cell_type":"code","source":"train_dataset = lgbm.Dataset(x_train, label=y_train)\ntest_dataset = lgbm.Dataset(x_test, label=y_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:15:04.912560Z","iopub.execute_input":"2022-07-08T07:15:04.913073Z","iopub.status.idle":"2022-07-08T07:15:04.925629Z","shell.execute_reply.started":"2022-07-08T07:15:04.912998Z","shell.execute_reply":"2022-07-08T07:15:04.921969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 훈련","metadata":{}},{"cell_type":"code","source":"model = lgbm.train(params, train_dataset, 1000, test_dataset, verbose_eval=100, early_stopping_rounds=100)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:15:04.927271Z","iopub.execute_input":"2022-07-08T07:15:04.927970Z","iopub.status.idle":"2022-07-08T07:18:07.912740Z","shell.execute_reply.started":"2022-07-08T07:15:04.927928Z","shell.execute_reply":"2022-07-08T07:18:07.911583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 성과분석","metadata":{}},{"cell_type":"code","source":"y_pred = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:18:07.916057Z","iopub.execute_input":"2022-07-08T07:18:07.916857Z","iopub.status.idle":"2022-07-08T07:18:27.718373Z","shell.execute_reply.started":"2022-07-08T07:18:07.916809Z","shell.execute_reply":"2022-07-08T07:18:27.717212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mse = mean_squared_error(y_test, y_pred)\nprint(f\"MSE Error: {mse}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:18:27.721719Z","iopub.execute_input":"2022-07-08T07:18:27.722099Z","iopub.status.idle":"2022-07-08T07:18:27.732046Z","shell.execute_reply.started":"2022-07-08T07:18:27.722058Z","shell.execute_reply":"2022-07-08T07:18:27.731123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r2 = r2_score(y_test, y_pred)\nprint(f\"R2 Score: {r2}\")","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:18:27.733477Z","iopub.execute_input":"2022-07-08T07:18:27.734046Z","iopub.status.idle":"2022-07-08T07:18:27.747512Z","shell.execute_reply.started":"2022-07-08T07:18:27.733992Z","shell.execute_reply":"2022-07-08T07:18:27.746626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 실제값과 예측값 비교","metadata":{}},{"cell_type":"code","source":"y_test['r05_fwd_predict'] = y_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:18:27.749001Z","iopub.execute_input":"2022-07-08T07:18:27.749611Z","iopub.status.idle":"2022-07-08T07:18:27.755394Z","shell.execute_reply.started":"2022-07-08T07:18:27.749574Z","shell.execute_reply":"2022-07-08T07:18:27.754362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# AAPL을 예시\ny_test.loc['AAPL'].plot()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:18:27.756586Z","iopub.execute_input":"2022-07-08T07:18:27.756892Z","iopub.status.idle":"2022-07-08T07:18:28.024887Z","shell.execute_reply.started":"2022-07-08T07:18:27.756866Z","shell.execute_reply":"2022-07-08T07:18:28.023782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Feature 중요도","metadata":{}},{"cell_type":"code","source":"importance_ = model.feature_importance()","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:19:53.697842Z","iopub.execute_input":"2022-07-08T07:19:53.698337Z","iopub.status.idle":"2022-07-08T07:19:53.704546Z","shell.execute_reply.started":"2022-07-08T07:19:53.698302Z","shell.execute_reply":"2022-07-08T07:19:53.703094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_columns","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:20:27.363077Z","iopub.execute_input":"2022-07-08T07:20:27.363492Z","iopub.status.idle":"2022-07-08T07:20:27.370779Z","shell.execute_reply.started":"2022-07-08T07:20:27.363459Z","shell.execute_reply":"2022-07-08T07:20:27.369719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance = pd.DataFrame([importance_], columns=feature_columns, index=['values']).T.sort_values('values')","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:22:39.526778Z","iopub.execute_input":"2022-07-08T07:22:39.527194Z","iopub.status.idle":"2022-07-08T07:22:39.536677Z","shell.execute_reply.started":"2022-07-08T07:22:39.527161Z","shell.execute_reply":"2022-07-08T07:22:39.535901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"importance.plot.barh(figsize=(16, 9))","metadata":{"execution":{"iopub.status.busy":"2022-07-08T07:23:54.391354Z","iopub.execute_input":"2022-07-08T07:23:54.391720Z","iopub.status.idle":"2022-07-08T07:23:54.689241Z","shell.execute_reply.started":"2022-07-08T07:23:54.391689Z","shell.execute_reply":"2022-07-08T07:23:54.688090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}