{"id":15627,"date":"2021-05-11T00:38:38","date_gmt":"2021-05-10T16:38:38","guid":{"rendered":"https:\/\/www.tejwin.com\/?post_type=insight&#038;p=15627"},"modified":"2023-08-04T10:37:46","modified_gmt":"2023-08-04T02:37:46","slug":"predict-the-market","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/","title":{"rendered":"Predict The Market?!"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized caption-align-center\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/\/image-228-1024x683.png\" alt=\"\" class=\"wp-image-15628\" width=\"838\" height=\"558\" srcset=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/image-228-1024x683.png 1024w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-228-300x200.png 300w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-228-150x100.png 150w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-228-768x512.png 768w, https:\/\/www.tejwin.com\/wp-content\/uploads\/image-228.png 1400w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><figcaption class=\"wp-element-caption\">photo by Unsplash<\/figcaption><\/figure>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69f3233fe08cb\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"ez-toc-cssicon\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69f3233fe08cb\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Preface\" >Preface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Highlights_of_this_article\" >Highlights of this article<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Links_related_to_this_article\" >Links related to this article<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Linear_Regression\" >Linear Regression<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Import_packages\" >Import packages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Data_extraction\" >Data extraction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Test\" >Test<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Linear_regression_analysis\" >Linear regression analysis<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Consider_more_lags\" >Consider more lags<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Summary\" >Summary<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Logistic_regression\" >Logistic regression<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#How_to_use_logistic_regression_to_predict_return\" >How to use logistic regression to predict return?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Logistic_regression_analysis\" >Logistic regression analysis<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Accuracy\" >Accuracy<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.tejwin.com\/en\/insight\/predict-the-market\/#Links_related_to_this_article_again\" >Links related to this article again!<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"1a4e\"><span class=\"ez-toc-section\" id=\"Preface\"><\/span>Preface<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"7868\">Machine learning(ML), deep learning(DL), and artificial intelligence have been widely used by industries recently. Applications such as&nbsp;<strong>image recognition and voice recognition<\/strong>&nbsp;are common in our life.<\/p>\n\n\n\n<p id=\"6b19\">However, wall street, the world center of finance has already recruited many statistics, mathematics even physics experts as algorithmic engineers and quantitative traders to exploit trading strategies. There had many cross-domain integrations in the finance field, and one of the most famous is the&nbsp;<strong>option pricing model, Black-Scholes Model(BS model)<\/strong>. It uses the Brownian motion, which is the physic term to describe the random fluctuations in a particle\u2019s position inside a fluid domain to quantify the daily price movement of options. Although there had some flaw in the model\u2019s assumption, it still got the honor of&nbsp;<strong>the Nobel prize in economics<\/strong>&nbsp;at that time. In this case, we could know the contribution of the BS model to both academies and practices.<\/p>\n\n\n\n<p id=\"4db7\">In this article,&nbsp;<strong>we would guide you to the world of machine learning, and introduce how to use it to predict market movement~<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f8c5\"><span class=\"ez-toc-section\" id=\"Highlights_of_this_article\"><\/span><strong>Highlights of this article <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\ud83c\udf1f Linear regression Intro\/application<\/li>\n\n\n\n<li>\ud83c\udf1f Logistic regression Intro\/application<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ba01\"><span class=\"ez-toc-section\" id=\"Links_related_to_this_article\"><\/span><strong>Links related to this article<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>1\ufe0f\u20e3 API Official Website:&nbsp;<\/strong><a href=\"https:\/\/api.tej.com.tw\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ API Official Website<\/strong><\/a><\/li>\n\n\n\n<li><strong>2\ufe0f\u20e3 The Product Package:&nbsp;<\/strong><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_caseIntro\/1\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ E-SHOP<\/strong><\/a><\/li>\n\n\n\n<li><strong>3\ufe0f\u20e3 Source Code:&nbsp;<\/strong><a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Quant\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ GITHUB<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"f8d8\"><span class=\"ez-toc-section\" id=\"Linear_Regression\"><\/span><strong>Linear Regression<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"3517\">Linear regression is the statistical method to find the pattern of the data and further draw a line to fit the data. While using linear regression, we only need to determine the<strong>&nbsp;independent variables (X value)&nbsp;<\/strong>and&nbsp;<strong>dependent variables<\/strong>&nbsp;<strong>(Y value).<\/strong>&nbsp;More intuitively, for example,&nbsp;<strong>if we want to use today\u2019s return to predict the future return<\/strong>, to meet this purpose,&nbsp;<strong>we take the yesterday return as the independent variable(X), the tomorrow return as dependent variables (Y).<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1Q1kif-WmdnDNlgu6fi0JcA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Linear regression formula<\/figcaption><\/figure>\n\n\n\n<p id=\"0cb2\">We can find the best line to fit the data according to the formula above. The score measures by the average distance among points to the straight, called mean square error.&nbsp;<strong>The linear regression aims to find the best combination of the coefficients (\u03b20, \u03b21) that minimize the mean square error.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1_1O-GYtEopVvaYCsWefGo1ww.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Linear Regression \u2014 example<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ab64\"><span class=\"ez-toc-section\" id=\"Import_packages\"><\/span><strong>Import packages <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nplt.style.use('seaborn')\nplt.rcParams&#91;'font.sans-serif'] = &#91;'Microsoft JhengHei']\nimport tejapi\ntejapi.ApiConfig.api_key = 'your key'\ntejapi.ApiConfig.ignoretz = True<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2139\"><span class=\"ez-toc-section\" id=\"Data_extraction\"><\/span><strong>Data extraction <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p id=\"6a93\">Grabbing the daily stock price and return of MEDIATEK (2454), the data sourced from TEJ API. In this episode, we use the securities trading database and securities return database to get the close-adjusted price and daily return.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = tejapi.get('TWN\/EWPRCD',\n                  coid='2454',\n                  paginate = True,\n                  chinese_column_name = True\n                 )\nreturn_ = tejapi.get('TWN\/EWPRCD2',\n                  coid='2454',\n                  paginate = True,\n                  chinese_column_name = True\n                 )\ndata&#91;'\u5831\u916c\u7387\uff05'] = return_&#91;'\u65e5\u5831\u916c\u7387(%)']<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"718c\"><span class=\"ez-toc-section\" id=\"Test\"><\/span><strong>Test <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p id=\"22cb\">Grabbing the daily stock price and return of&nbsp;<strong>MEDIATEK (2454)<\/strong>,&nbsp;<strong>the data sourced from TEJ API.<\/strong>&nbsp;<strong>To get the close-adjusted price and daily return, we extract the data from the securities trading table and the securities return table. (<\/strong><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_caseIntro\/2\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>Data source<\/strong><\/a><strong>)<\/strong><\/p>\n\n\n\n<p id=\"2be8\">The figure below shows that stock prices grow along with time. In other words, the overall trend of the stock price is upward, and we can also say&nbsp;<strong>stock price is non-stationary data.<\/strong>&nbsp;So, if we use yesterday\u2019s stock price as X, today\u2019s stock price as Y.<\/p>\n\n\n\n<p id=\"ccc3\">Even though the statistic result is significant, but just a coincidence. There doesn\u2019t have a strong relation between X and Y. This kind of circumstance so-called&nbsp;<strong>spurious regression<\/strong>. (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Spurious_relationship\" rel=\"noreferrer noopener\" target=\"_blank\">reference<\/a>)<\/p>\n\n\n\n<p id=\"dbcf\">\u23f0 The solution to fix non-stationary data:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Make the first-order difference on the stock prices.<\/li>\n<\/ul>\n\n\n\n<p id=\"633c\">The first-order difference means to calculate the stock return.&nbsp;<strong>After calculation, the trend of the data has been removed. Now, we can put the data into the linear regression model.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1Kt0MG8Wqy9OZktdtv2JfIw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">stock price \u2014 non-stationary data<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image is-resized caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/10oYbqtSzSLJZHoZ1QK-uew.png\" alt=\"\" width=\"841\" height=\"421\"\/><figcaption class=\"wp-element-caption\">daily return&nbsp;<strong>\u2014<\/strong>&nbsp;stationary data<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8065\"><span class=\"ez-toc-section\" id=\"Linear_regression_analysis\"><\/span><strong>Linear regression analysis <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p id=\"6fb9\">According to the graph below, we realize that it\u2019s hard to predict return by linear regression model.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u7528\u6628\u5929(\u7b2ct-1\u65e5)\u7684\u5831\u916c\u7387\u53bb\u9810\u6e2c\u4eca\u5929(\u7b2ct\u65e5)\u7684\u5831\u916c\u7387\n# \u56e0\u524d\u5169\u500b\u4f4d\u7f6e\u7684\u503c\u70baNA\uff0c\u6545\u53d6\u7b2c3\u500b\u4f4d\u7f6e\u4ee5\u5f8c\u7684\u6578\u503c\nx = data&#91;'\u5831\u916c\u7387\uff05'].shift(1)&#91;2:].values\ny = data&#91;'\u5831\u916c\u7387\uff05']&#91;2:].values\nreg = np.polyfit(x,y,deg=1)\n# \u7e6a\u5716\nplt.figure(figsize=(10,6))\nplt.plot(x, y, 'bo', label='data')\nplt.plot(x, np.polyval(reg,x),'r',label='linear regression')\nplt.legend(loc=0)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1E0EwHUwdiu2avRtTB0ZARw.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">yesterday\u2019s return vs today\u2019s return<\/figcaption><\/figure>\n\n\n\n<p id=\"8fda\">The summary report shows that the T statistics is greater than 2, so yesterday\u2019s return(x1) has a significant effect on today\u2019s return, so we should include x1 into our model.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/19VgdkVzp9rHp0xfWEALmwg.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">summary report<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"77bd\"><span class=\"ez-toc-section\" id=\"Consider_more_lags\"><\/span><strong>Consider more lags<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p id=\"1eeb\">We think<strong>&nbsp;the information contains in x1 is not enough for us to well predicted the future return<\/strong>. Therefore, we adjust our model,&nbsp;<strong>consider more lag into the model. Finally, we take lag 2, 3-period return into account.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1KrU_0QsPabgbJp7apQxNYQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">modified formula<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code># \u843d\u5f8c\u4e00\u671f\u81f3\u4e09\u671f\u7684\u5831\u916c\u7387\ndata&#91;'R_t-1'] = data&#91;'\u5831\u916c\u7387\uff05'].shift(1)\ndata&#91;'R_t-2'] = data&#91;'\u5831\u916c\u7387\uff05'].shift(2)\ndata&#91;'R_t-3'] = data&#91;'\u5831\u916c\u7387\uff05'].shift(3)\nx = data&#91;&#91;'R_t-1','R_t-2','R_t-3']]&#91;4:].values\ny = data&#91;'\u5831\u916c\u7387\uff05']&#91;4:].values\nreg_t3 = np.linalg.lstsq(x,y, rcond=None)&#91;0]\n# \u7e6a\u5716\nplt.figure(figsize=(10,6))\nplt.plot(y&#91;1000:1300],label = 'true_value')\nplt.plot(np.dot(x, reg_t3)&#91;1000:1300],color = 'green',label = 'predict')\nplt.legend()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/16hj5Xo2JV-JfSdFo9FlHEQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">predict the result<\/figcaption><\/figure>\n\n\n\n<p id=\"b369\"><strong>According to the summary report, the T statistics of x1 and x3 are higher than 2, so those two variables should include in our model.<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1FE8AUAItMCJXgfjvzdGcbQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">summary report<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5475\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span><strong>Summary <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p id=\"89b4\">The part of linear regression has come to an end. Forecasting the market has always been a popular topic in finance and statistics. Interested friends can refer to relevant literature or explore other effective factors yourselves!!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"92f5\"><span class=\"ez-toc-section\" id=\"Logistic_regression\"><\/span><strong>Logistic regression<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"1313\">Although logistic regression contains a regression in its name, it\u2019s a kind of<strong>&nbsp;classification.<\/strong><\/p>\n\n\n\n<p id=\"d8e0\">The difference between classification and regression their outputs. The classification outputs&nbsp;<strong>a discrete variable<\/strong>(such as 0,1,2, etc.)\uff1bthe regression outputs&nbsp;<strong>a continuous variable.<\/strong>&nbsp;(such as 0,0.1,0.5,1.2, etc.)<\/p>\n\n\n\n<p id=\"e560\">The concept of linear regression is to find a line whose average distance of points is minimization.<strong>&nbsp;The logistic regression aims to find a line that can classify data accurately the most.<\/strong>&nbsp;The illustrations below show the ideal condition, the model miss 2 in classify group 1, and miss 1 in classify group 0.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1fb65ooypLkeUXIHCnOqF8Q.png\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1r2lpDazOuho1HOCJY0G03g.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">logistic regression \u2014 ideal condition<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"de0b\"><span class=\"ez-toc-section\" id=\"How_to_use_logistic_regression_to_predict_return\"><\/span><strong>How to use logistic regression to predict return?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p id=\"d593\">The predicting ability of the linear regression is not good, and hard to predict the actual value.&nbsp;<strong>Therefore, we try to simplify the problem. Change the target from its value to market movement(up and down).<\/strong>&nbsp;After all, if we could know the market movement tomorrow, we can construct a trading strategy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"f3a1\"><span class=\"ez-toc-section\" id=\"Logistic_regression_analysis\"><\/span><strong>Logistic regression analysis <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"5369\">np.where(condition, Ture(ouput), False(output))\uff1a<br>While return is higher than 0, ouput 1, otherwise, -1.<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>data&#91;'signal'] = np.where(data&#91;'\u5831\u916c\u7387\uff05']&gt;0,1,-1)\n# \u7e6a\u5716\ndata&#91;:100].plot(x = '\u65e5\u671f',\n                y = 'signal',\n                style = 'ro',\n                figsize = (10,6)\n               )<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1mJzuGA4GOM_Npi0Q-SLoTA.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">scatter<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn import linear_model\nlm = linear_model.LogisticRegression(solver = 'lbfgs')\nR_t1 = x.reshape(1,-1).T\nsignal = y\nlm.fit(R_t1,signal)\npreditions = lm.predict(R_t1)\nplt.figure(figsize = (10, 6))\nplt.plot(x, y, 'ro', label = 'data')\nplt.plot(x, preditions, 'b', label = 'preditions')\nplt.legend(loc = 0)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" src=\"https:\/\/www.tejwin.com\/wp-content\/uploads\/1KQm6-E9Ck8WFebQicKq_tQ.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">predict result<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7942\"><span class=\"ez-toc-section\" id=\"Accuracy\"><\/span><strong>Accuracy <\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p id=\"9508\">The accuracy of prediction is 0.5471.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from sklearn.metrics import accuracy_score\npreditions = lm.predict(R_t1)\naccuracy_score(preditions, y)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3167\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p id=\"dd3e\">After implement machine learning projection by yourself,&nbsp;<strong>did you find that it is difficult?<\/strong>&nbsp;\ud83d\ude0e\ud83d\ude0e Although the accuracy of the model is not high, don\u2019t be discouraged about it.&nbsp;<strong>Predicting the market is not the only way to make money in the market<\/strong>. In the future, we will share the reasons why you can make money without prediction, so please pay attention to our Medium continuously.<\/p>\n\n\n\n<p id=\"ba5f\">If you like this topic, please click \ud83d\udc4f below, giving us more support and encouragement. Additionally, if you have any questions or suggestions, please leave a message or email us. We will try our best to reply to you.\ud83d\udc4d\ud83d\udc4d<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9fa2\"><span class=\"ez-toc-section\" id=\"Links_related_to_this_article_again\"><\/span><strong>Links related to this article again!<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>1\ufe0f\u20e3 API Official Website:&nbsp;<\/strong><a href=\"https:\/\/api.tej.com.tw\/\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ API Official Website<\/strong><\/a><\/li>\n\n\n\n<li><strong>2\ufe0f\u20e3 The Product Package:&nbsp;<\/strong><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_caseIntro\/1\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ E-SHOP<\/strong><\/a><\/li>\n\n\n\n<li><strong>3\ufe0f\u20e3 Source Code:&nbsp;<\/strong><a href=\"https:\/\/github.com\/tejtw\/TEJAPI_Python_Medium_Quant\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TEJ GITHUB<\/strong><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Preface Machine learning(ML), deep learning(DL), and artificial intelligence have been widely used by industries recently. Applications such as&nbsp;image recognition and voice recognition&nbsp;are common in our life. However, wall street, the world center of finance has already recruited many statistics, mathematics even physics experts as algorithmic engineers and quantitative traders to exploit trading strategies. There had [&hellip;]<\/p>\n","protected":false},"featured_media":15629,"template":"","tags":[2604,2371,3008],"insight-category":[690,50],"class_list":["post-15627","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-machine-learning","tag-python","tag-tejapi-quant","insight-category-data-analysis","insight-category-fintech"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/15627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"version-history":[{"count":0,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/15627\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/15629"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=15627"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=15627"},{"taxonomy":"insight-category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight-category?post=15627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}