<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>MACHINE LEARNING &#8211; FinLab</title>
	<atom:link href="https://www.finlab.tw/tag/machine-learning/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.finlab.tw</link>
	<description>深入淺出的量化投資，讓你在在茫茫股海中，找到專屬於自己的投資方法</description>
	<lastBuildDate>Wed, 25 May 2022 07:49:28 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.9</generator>

<image>
	<url>https://www.finlab.tw/wp-content/uploads/2020/07/favicon.png</url>
	<title>MACHINE LEARNING &#8211; FinLab</title>
	<link>https://www.finlab.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">179699571</site>	<item>
		<title>ROE怎麼看? 機器學習告訴你！</title>
		<link>https://www.finlab.tw/roe%e6%80%8e%e9%ba%bc%e7%9c%8b-%e6%a9%9f%e5%99%a8%e5%ad%b8%e7%bf%92%e5%91%8a%e8%a8%b4%e4%bd%a0/</link>
					<comments>https://www.finlab.tw/roe%e6%80%8e%e9%ba%bc%e7%9c%8b-%e6%a9%9f%e5%99%a8%e5%ad%b8%e7%bf%92%e5%91%8a%e8%a8%b4%e4%bd%a0/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 23:05:39 +0000</pubDate>
				<category><![CDATA[AI看股票]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[機器學習]]></category>
		<category><![CDATA[程式交易]]></category>
		<category><![CDATA[股票]]></category>
		<category><![CDATA[股票分析]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=794</guid>

					<description><![CDATA[機器學習究竟能不能輔助人類買賣股票？答案一定是可以的，我們可以藉由機器學習歸納出結果，來優化我們的選股方式，今天這篇文章沒有非常高深的數學，也沒有困難的程式，只會有做完研究的心得，幫助大家選股更順利！]]></description>
										<content:encoded><![CDATA[
<p>機器學習究竟能不能輔助人類買賣股票？答案一定是可以的，我們可以藉由機器學習歸納出結果，來優化我們的選股方式，今天這篇文章沒有非常高深的數學，也沒有困難的程式，只會有做完研究的心得，幫助大家選股更順利！<br><a href="https://www.finlab.tw/ROE%E6%80%8E%E9%BA%BC%E7%9C%8B-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E5%91%8A%E8%A8%B4%E4%BD%A0/thumbnail.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img width="1024" height="669" src="http://34.96.136.135/wp-content/uploads/2020/07/thumbnail-1-5-1024x669.png" alt="thumbnail 1 5" class="wp-image-796" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-5-1024x669.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-5-300x196.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-5-768x502.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-5.png 1518w" sizes="(max-width: 1024px) 100vw, 1024px" title="ROE怎麼看? 機器學習告訴你！ 1"></figure></div>



<p>這篇文章中，我把市值跟ROE拿來做比較，並且利用SVM來訓練判斷，看看市值跟ROE對於股價漲跌的關連！</p>



<h3 id="什麼是SVM？">什麼是SVM？</h3>



<p>SVM的全名叫做&nbsp;<a href="https://en.wikipedia.org/wiki/Support_vector_machine" rel="noreferrer noopener" target="_blank">Support Vector Machine</a>，而這類的機器學習，會用 hyperplane 將空間切開，進而將空間中的座標進行分類，所以當我們有2個features時，就可以將第一個feature當作 x 軸，第二個 feature 當作 y 軸，然後顏色代表漲跌，畫出一張圖：<a href="https://www.finlab.tw/ROE%E6%80%8E%E9%BA%BC%E7%9C%8B-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E5%91%8A%E8%A8%B4%E4%BD%A0/svm.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="356" src="http://34.96.136.135/wp-content/uploads/2020/07/svm-1024x356.png" alt="svm" class="wp-image-797" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/svm-1024x356.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/svm-300x104.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/svm-768x267.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/svm-1536x534.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/svm.png 1560w" sizes="(max-width: 1024px) 100vw, 1024px" title="ROE怎麼看? 機器學習告訴你！ 2"></figure></div>



<p>其中的綠色點，就是代表下跌的股票，而紅色的點，則代表了上漲的股票，而hyperplane 就是黃色的時線，將這個平面上畫出精準的一刀，切成兩個部分，右半部份，代表SVM覺得未來會上漲，左半部份代表SVM覺得未來會下跌的股票。</p>



<p>以上是由視覺的方式來說明SVM，數學的部分，其實大家國中高中都有學過，其實就是點與 hyperplane 的長度，SVM會計算「點到線」之間的長度，來決定誤差。當有越多點歸類錯誤的時候，SVM就會將這些「點到線的距離加總」，當作是錯誤總分，越大越不好，於是演算法不斷的移動這個切割線（hyperplane），來達到最完美的狀態，使得錯誤總分最低。</p>



<h3 id="SVM-的-Kernel-Trick">SVM 的 Kernel Trick</h3>



<p>我們上述的都是一條直線的 hyperplane ，但其實它不一定得是直的，只要能夠切分出不同的區域來分類即可。下圖就是使用linear的kernel跟曲線的kernel的範例：<a href="https://www.finlab.tw/ROE%E6%80%8E%E9%BA%BC%E7%9C%8B-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E5%91%8A%E8%A8%B4%E4%BD%A0/kernel.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="345" src="http://34.96.136.135/wp-content/uploads/2020/07/kernel-1-1024x345.png" alt="kernel 1" class="wp-image-799" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/kernel-1-1024x345.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/kernel-1-300x101.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/kernel-1-768x259.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/kernel-1.png 1234w" sizes="(max-width: 1024px) 100vw, 1024px" title="ROE怎麼看? 機器學習告訴你！ 3"></figure></div>



<p>接下來，我們就來使用不同的kernel，來將股票做歸類，看它們究竟會上漲還是下跌？我們所使用的 features 只有：ROE、市值<br>方便我們以圖形化的方式呈現並理解，以下是結果：</p>



<h3 id="使用線性的kernel">使用線性的kernel</h3>



<p>此時 hyperplane 就是很簡單的一條線。雖然是簡單的一條線，但它還是非常有用處，我們可以藉由這條線的斜率，來瞭解 ROE 跟市值的關係：<a href="https://www.finlab.tw/ROE%E6%80%8E%E9%BA%BC%E7%9C%8B-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E5%91%8A%E8%A8%B4%E4%BD%A0/linear.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="548" src="http://34.96.136.135/wp-content/uploads/2020/07/linear-1-1024x548.png" alt="linear 1" class="wp-image-801" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/linear-1-1024x548.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/linear-1-300x160.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/linear-1-768x411.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/linear-1.png 1234w" sizes="(max-width: 1024px) 100vw, 1024px" title="ROE怎麼看? 機器學習告訴你！ 4"></figure></div>



<p>程式自動最佳化後，上圖就是程式找到一刀最完美的切法，</p>



<ul><li>黃色三角形為上漲的股票</li><li>藍色正方形為下跌的股票</li><li>黃色平面是SVM認為會上漲的股票</li><li>藍色平面是SVM認為會下跌的股票</li></ul>



<p>這邊的市值跟ROE都已經經過preprocessing，所以跟我們平常看到的數值不太一樣，我們由上圖可以看出程式在想什麼：</p>



<h4 id="機器學習：市值越高的股票，ROE也要越高">機器學習：市值越高的股票，ROE也要越高</h4>



<p>可以看到上圖中，斜率是正的，代表當市值越高時，SVM 會傾向於選擇 ROE 高一點的股票，而且此條線偏上方，代表ROE比較高比較好，所以我們可以做一些歸納；</p>



<h4 id="假如有一檔股票「市值高，但是ROE低」，不會被選進來">假如有一檔股票「市值高，但是ROE低」，不會被選進來</h4>



<p>這樣的想法算滿合理的，因為大家對市值高的股票期望比較高，或是公司的規模比較大，但此公司坐擁資源竟然還沒辦法為股東賺錢，於是期望可能轉換為失望，所以SVM表示看跌。</p>



<p>當然這個理由是我自己加的，SVM 是無情的最佳化結果而已。<br>另外我們也可以看到：</p>



<h4 id="對於「市值低，ROE低」的公司，SVM會比較寬容">對於「市值低，ROE低」的公司，SVM會比較寬容</h4>



<p>一檔「市值低，ROE低」的公司，SVM會比較寬容一些些（但還是有一定的門檻）只是不如市值高的公司那麼嚴苛罷了。</p>



<p>我們來看一下以此種邏輯回測的結果，紅色為預測會漲的股票，綠色為預測會跌的股票，每季獲利的平均<a href="https://www.finlab.tw/ROE%E6%80%8E%E9%BA%BC%E7%9C%8B-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E5%91%8A%E8%A8%B4%E4%BD%A0/eq1.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="375" height="242" src="http://34.96.136.135/wp-content/uploads/2020/07/eq1-1-1.png" alt="eq1 1 1" class="wp-image-802" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/eq1-1-1.png 375w, https://www.finlab.tw/wp-content/uploads/2020/07/eq1-1-1-300x194.png 300w" sizes="(max-width: 375px) 100vw, 375px" title="ROE怎麼看? 機器學習告訴你！ 5"></figure></div>



<p>效果很不錯，但是這個邏輯是否是完美的呢？<br>我們再來使用不同的kernel，試試看：</p>



<h3 id="使用曲線的kernel">使用曲線的kernel</h3>



<p>下圖為使用不同的kernel，所做出來的分類結果，我們使用2次多項式來對股票進行分類：<a href="https://www.finlab.tw/ROE%E6%80%8E%E9%BA%BC%E7%9C%8B-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E5%91%8A%E8%A8%B4%E4%BD%A0/poly.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="539" src="http://34.96.136.135/wp-content/uploads/2020/07/poly-1024x539.png" alt="poly" class="wp-image-803" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/poly-1024x539.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/poly-300x158.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/poly-768x404.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/poly.png 1224w" sizes="(max-width: 1024px) 100vw, 1024px" title="ROE怎麼看? 機器學習告訴你！ 6"></figure></div>



<p>怎麼分出來完全不一樣呀！我們來看第二個實驗，SVM又告訴了我們什麼：</p>



<h4 id="市值高的股票-ROE-不要太高">市值高的股票 ROE 不要太高</h4>



<p>這其實還是有些道理的，ROE 太高的股票不要買，這個是很多實驗都有做出來的結果，因為大公司的獲利，好歹也是龐大而穩定的系統，ROE高是會有上限的，不可能一直成長，當今天ROE已經很高時，代表接下來不會更高了，甚至還有可能會衰退。</p>



<p>所以買市值高的股票時，SVM傾向「ROE 不要太高」！</p>



<h4 id="市值低的股票-ROE-越高越好">市值低的股票 ROE 越高越好</h4>



<p>然而本實驗的另一個特點，就是SVM統計出來，發現市值低的股票ROE高是沒有關係的，至於原因可能是因為，市值低的股票，大家對於該公司期望並不高，在此狀況下，高 ROE 的數值跌破大家的眼鏡，所以造成長期的後市看好。（只是推測）</p>



<p>所以買市值低的股票時，SVM傾向「ROE 盡量高一點」！</p>



<h4 id="市值中間的股票，不要買">市值中間的股票，不要買</h4>



<p>這個條件是最詭異的一個，我還是不要亂解釋好了XDD。</p>



<h3 id="回測結果">回測結果</h3>



<p>這樣的邏輯可以獲利嗎？我們可以看以下的結果，紅色為持有預測會漲的股票，綠色為持有預測會跌的股票，每季獲利的平均：<a href="https://www.finlab.tw/ROE%E6%80%8E%E9%BA%BC%E7%9C%8B-%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E5%91%8A%E8%A8%B4%E4%BD%A0/eq2.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="375" height="242" src="http://34.96.136.135/wp-content/uploads/2020/07/eq2-4.png" alt="eq2 4" class="wp-image-804" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/eq2-4.png 375w, https://www.finlab.tw/wp-content/uploads/2020/07/eq2-4-300x194.png 300w" sizes="(max-width: 375px) 100vw, 375px" title="ROE怎麼看? 機器學習告訴你！ 7"></figure></div>



<p>也是有不錯的獲利！<br>利用 SVM 來分辨漲跌的股票，真的還滿有趣的，本文利用了兩種不同的 kernel：</p>



<ul><li>linear</li><li>polynomial<br>來進行分類，結果顯示 linear 的分類，跑出來的回測獲利比較優秀，但由這兩種不同的kernel，製作出來的模型，都可以拿來獲利。</li></ul>



<p>雖然兩個模型的闡述「相互矛盾」「有點道理、但又有點詭異」，就像是投資界眾說紛紜的理論一樣，所以以後看到相互矛盾的論點，其實不一定誰好誰壞，甚至說不定都能夠獲利，這就是股票市場的有趣之處。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/roe%e6%80%8e%e9%ba%bc%e7%9c%8b-%e6%a9%9f%e5%99%a8%e5%ad%b8%e7%bf%92%e5%91%8a%e8%a8%b4%e4%bd%a0/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">794</post-id>	</item>
		<item>
		<title>論文導讀：利用CNN神經網路來交易ETF</title>
		<link>https://www.finlab.tw/cnn-time-series-image-conversion-approach/</link>
					<comments>https://www.finlab.tw/cnn-time-series-image-conversion-approach/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:54 +0000</pubDate>
				<category><![CDATA[AI看股票]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[機器學習]]></category>
		<category><![CDATA[選股]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=943</guid>

					<description><![CDATA[文章導讀：機器學習用來投資一直都是非常熱門的學問，近年來深度學習模型開始受到非常多的矚目，尤其是在電腦視覺方面，所以接下來導讀的這篇文章，就是提供了一個全新的方式，將目前最火紅的 視覺神經網路CNN，用來預測股票的漲跌。]]></description>
										<content:encoded><![CDATA[
<p>文章導讀：機器學習用來投資一直都是非常熱門的學問，近年來深度學習模型開始受到非常多的矚目，尤其是在電腦視覺方面，所以接下來導讀的這篇文章，就是提供了一個全新的方式，將目前最火紅的 視覺神經網路：Convolutional Neural Network（CNN）用來預測股票的漲跌。實驗解果表示效果比一般的神經網路要好。<a href="https://www.finlab.tw/cnn-time-series-image-conversion-approach/title.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="706" src="http://34.96.136.135/wp-content/uploads/2020/07/title-1024x706.png" alt="title" class="wp-image-946" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/title-1024x706.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/title-300x207.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/title-768x530.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/title-1536x1059.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/title.png 1636w" sizes="(max-width: 1024px) 100vw, 1024px" title="論文導讀：利用CNN神經網路來交易ETF 8"></figure></div>



<p>你可能會問，以time series來說，明明神經網路可以使用 LSTM，為何要用 CNN？LSTM應該比較適合處理時間序列呀！（畢竟LSTM是拿來處理文字字串的神經網路，字串也是一種時間序列，所以通常預測股價，大家會用LSTM）。但文章中說明，目前CNN用於圖像處理比起其他的模型，被研究的更透徹，也有大量的研發，所以說不定效果更好，所以採用了 CNN 神經網路來預測股價的漲跌</p>



<p>他們用15個技術指標 X 15 天，產生出一個 15&#215;15 的影像，將這個影像丟入 CNN 神經網路中做訓練，下圖就是神經網路所「看到」的input：<a href="https://www.finlab.tw/cnn-time-series-image-conversion-approach/input.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="445" src="http://34.96.136.135/wp-content/uploads/2020/07/input-1024x445.png" alt="input" class="wp-image-948" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/input-1024x445.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/input-300x130.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/input-768x334.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/input.png 1500w" sizes="(max-width: 1024px) 100vw, 1024px" title="論文導讀：利用CNN神經網路來交易ETF 9"></figure></div>



<p>想想對於人類來說，這個影像對我們來講沒什麼意義，但是對於電腦來說，就可以拿來判斷股價的漲跌喔！還滿酷的！我只是在想為何是15&#215;15為何不將圖像變的大一點？</p>



<p>我們會將上面這些圖片稱之為features，也就是電腦用來判斷買賣的資訊，電腦用這些資訊，就可以預測交易訊號，也就是這個神經網路的label。作者提供了一個簡單的方式，使用rolling window，去找每段時間的最大值最小值，以下是大致上的程式碼，有興趣的話可以深入研究，不過我們在這邊就點到為止。<a href="https://www.finlab.tw/cnn-time-series-image-conversion-approach/label.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="740" src="http://34.96.136.135/wp-content/uploads/2020/07/label-1-1024x740.png" alt="label 1" class="wp-image-950" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/label-1-1024x740.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/label-1-300x217.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/label-1-768x555.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/label-1-1536x1111.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/label-1.png 1712w" sizes="(max-width: 1024px) 100vw, 1024px" title="論文導讀：利用CNN神經網路來交易ETF 10"></figure></div>



<p>上述的方法，會產生（-1, 0, 1）的訊號，分別代表（賣、不做動作、買），但是由上述程式碼產生出來的訊號，必然會有imbalance的問題，也就是三種class的數量不一致，所以本篇文章使用random resampling的方式，隨機取出相同數量的 label 對應到的 testcase，來避免 0 太多的問題。</p>



<p>此篇 paper 中用了美股的 ETF 和 道瓊 30 檔成分股當訓練的標的。<br>有了 feature 跟 label，很理所當然的，我們就可以開始訓練了！<br>此篇 paper 的神經網路如下：<a href="https://www.finlab.tw/cnn-time-series-image-conversion-approach/cnn.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="427" src="http://34.96.136.135/wp-content/uploads/2020/07/cnn-1-1024x427.png" alt="cnn 1" class="wp-image-952" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/cnn-1-1024x427.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/cnn-1-300x125.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/cnn-1-768x320.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/cnn-1-1536x640.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/cnn-1.png 1546w" sizes="(max-width: 1024px) 100vw, 1024px" title="論文導讀：利用CNN神經網路來交易ETF 11"></figure></div>



<ul><li>input layer (15&#215;15),</li><li>two convolutional layers (15x15x32, 15x15x64),</li><li>max pooling (7x7x64),</li><li>two dropout (0.25, 0.50),</li><li>fully connected layers (128),</li><li>output layer (3)</li></ul>



<p>上面的架構中，input就是單純的 15 x 15 的圖片，output 有3個，分別是買、賣、不做動作。<br>中間的 convolutional layers 跟 pooling<br>作者說由於training dataset不大的關係，所以神經網路不用太大，不然會造成overfitting的狀況。</p>



<p>這篇 paper 使用 walk forward 的方式來測試，好處是可以產生時間較長的回測結果，<br>以下就是他們的回測績效（其中兩檔ETF）：<a href="https://www.finlab.tw/cnn-time-series-image-conversion-approach/backtest.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="325" src="http://34.96.136.135/wp-content/uploads/2020/07/backtest-2-1024x325.png" alt="backtest 2" class="wp-image-953" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/backtest-2-1024x325.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/backtest-2-300x95.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/backtest-2-768x244.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/backtest-2-1536x487.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/backtest-2.png 1588w" sizes="(max-width: 1024px) 100vw, 1024px" title="論文導讀：利用CNN神經網路來交易ETF 12"></figure></div>



<p>而他們也有將效果跟其他的神經網路作比較，幾種常用的方法：</p>



<ul><li>CNN-TAr 作者使用的方法</li><li>BaHr 所謂的 buy and Hold 長期持有</li><li>RSIr 利用 RSI技術指標做出來的策略</li><li>SMAr 均線策略</li><li>LSTMr 神經網路策略pip</li><li>MLPr 一般神經網路策略</li></ul>



<p>下圖就是商品（每一個row）和不同策略（每一個column）所對應到的年報酬率：<a href="https://www.finlab.tw/cnn-time-series-image-conversion-approach/compare.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="385" src="http://34.96.136.135/wp-content/uploads/2020/07/compare-1-1024x385.png" alt="compare 1" class="wp-image-954" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/compare-1-1024x385.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/compare-1-300x113.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/compare-1-768x289.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/compare-1.png 1282w" sizes="(max-width: 1024px) 100vw, 1024px" title="論文導讀：利用CNN神經網路來交易ETF 13"></figure></div>



<p>可以發現這種神經網路的效果比其他的更好！在大部分的商品都有比較好的年報酬率喔～</p>



<p>更多內容都可以參考該<a href="https://www.researchgate.net/profile/Omer_Sezer/publication/324802031_Algorithmic_Financial_Trading_with_Deep_Convolutional_Neural_Networks_Time_Series_to_Image_Conversion_Approach/links/5ae4ade9a6fdcc3bea95d2fd/Algorithmic-Financial-Trading-with-Deep-Convolutional-Neural-Networks-Time-Series-to-Image-Conversion-Approach.pdf" rel="noreferrer noopener" target="_blank">論文的連結</a>喔！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/cnn-time-series-image-conversion-approach/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">943</post-id>	</item>
		<item>
		<title>用Machine learning 學習看技術指標</title>
		<link>https://www.finlab.tw/machine-learning%ef%bc%9a%e4%bd%bf%e7%94%a8%e6%8a%80%e8%a1%93%e6%8c%87%e6%a8%99%e9%a0%90%e6%b8%ac%e5%a4%a7%e7%9b%a4/</link>
					<comments>https://www.finlab.tw/machine-learning%ef%bc%9a%e4%bd%bf%e7%94%a8%e6%8a%80%e8%a1%93%e6%8c%87%e6%a8%99%e9%a0%90%e6%b8%ac%e5%a4%a7%e7%9b%a4/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:39 +0000</pubDate>
				<category><![CDATA[AI看股票]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[機器學習]]></category>
		<category><![CDATA[程式交易]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=788</guid>

					<description><![CDATA[這篇文章是印度指數預測，2015年發表的就能有191個citation，算是很有名的paper之一，全文特點是只使用了技術指標，來預測大盤每天漲跌，提出了一個有效的優化方式。]]></description>
										<content:encoded><![CDATA[
<p><a href="https://www.sciencedirect.com/science/article/pii/S0957417414004473" rel="noreferrer noopener" target="_blank">這篇文章是印度指數預測</a>，2015年發表的就能有191個citation，算是很有名的paper之一，全文特點是只使用了技術指標，來預測大盤每天漲跌，提出了一個有效的優化方式。<a href="https://www.finlab.tw/Machine-learning%EF%BC%9A%E4%BD%BF%E7%94%A8%E6%8A%80%E8%A1%93%E6%8C%87%E6%A8%99%E9%A0%90%E6%B8%AC%E5%A4%A7%E7%9B%A4/thumbnail.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="992" height="640" src="http://34.96.136.135/wp-content/uploads/2020/07/thumbnail-7.png" alt="thumbnail 7" class="wp-image-789" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-7.png 992w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-7-300x194.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-7-768x495.png 768w" sizes="(max-width: 992px) 100vw, 992px" title="用Machine learning 學習看技術指標 14"></figure></div>



<p>這篇paper的作者非常的認真，總共訓練四種不同的模型：Artificial Neural Network (ANN), support vector machine (SVM), random forest (RF) 和 Naive-Bayes (NB)。</p>



<p>這邊假如看不懂沒有關係，可以想像「模型」就是「電腦的大腦」，負責學習每種指標怎麼對應到未來的漲跌。之後有空再來慢慢介紹這些不同的「大腦」背後的數學原理。</p>



<p>此論文主要建構製作兩組traning data：</p>



<ul><li>包含十種技術指標</li><li>為這十種技術指標的看漲看跌<br>前者為 float （被normalize成-1～1）<br>而後者為 boolean （也就是只有輸入True和False）。</li></ul>



<p>究竟這些排列組合會迸出什麼樣的火花呢？</p>



<h3 id="選擇模型">選擇模型</h3>



<p>研究發現Random Forest的表現是最好的，而ANN差了一點，<br>這是滿正常的，畢竟data set還是太小，神經網路很難訓練的非常好。<br>至於傳統的NB則是最差的。</p>



<p>其實也可以考慮一下LSTM，說不定效果也不錯？<br>（但因為LSTM還是基於ANN，所以有可能遇到traning set太小的問題）</p>



<h3 id="選擇traning-data">選擇traning data</h3>



<p>如同上面所述，此篇論文總共先用了十種技術指標，以下是這十種常見的指標，為了讓文章不會太冗長，我就不一一解釋了，之後有空再來介紹每種指標的用法。<a href="https://www.finlab.tw/Machine-learning%EF%BC%9A%E4%BD%BF%E7%94%A8%E6%8A%80%E8%A1%93%E6%8C%87%E6%A8%99%E9%A0%90%E6%B8%AC%E5%A4%A7%E7%9B%A4/p1.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="408" src="http://34.96.136.135/wp-content/uploads/2020/07/p1-1024x408.png" alt="p1" class="wp-image-790" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/p1-1024x408.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/p1-300x120.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/p1-768x306.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/p1-1536x612.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/p1.png 1906w" sizes="(max-width: 1024px) 100vw, 1024px" title="用Machine learning 學習看技術指標 15"></figure></div>



<p>這篇paper會將這些技術指標的數值，每一項做normalize在-1～1之間，這樣的優化主要是為了ANN和SVM。以下是這個model的架構：</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="549" src="http://34.96.136.135/wp-content/uploads/2020/07/p2-1024x549.png" alt="p2" class="wp-image-791" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/p2-1024x549.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/p2-300x161.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/p2-768x412.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/p2.png 1320w" sizes="(max-width: 1024px) 100vw, 1024px" title="用Machine learning 學習看技術指標 16"></figure></div>



<p>另外，全文的特點是使用了提出了 「Trend Deterministic Data Preparation」，看起來非常的厲害，簡單講就是將這些指標的數值究竟是「看漲」還是「看跌」拿來訓練，而不是訓練指標本身的數值。</p>



<p>例如均線好了，與其拿均線的值來訓練，不如看「股價跟均線的關係」，假如股價在均線之上，就是「看漲」（True），反之則「看跌」（False）。其他指標都能以此類推！</p>



<p>以下就是新增了一層資料的修改的架構：</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="512" src="http://34.96.136.135/wp-content/uploads/2020/07/p3-1024x512.png" alt="p3" class="wp-image-792" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/p3-1024x512.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/p3-300x150.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/p3-768x384.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/p3.png 1500w" sizes="(max-width: 1024px) 100vw, 1024px" title="用Machine learning 學習看技術指標 17"></figure></div>



<p>最後發現使用了「Trend Deterministic Data Preparation」<br>效果好很多！</p>



<h3 id="結論">結論</h3>



<p>這篇文章中，可以大概知道每一種model對於技術性指標的數據的效果如何。並且了解了用「Trend Deterministic Data Preparation」來進行優化。經過了非常多參數的比對，發現使用RF model做出來的策略效果最好。然而這只限於頻率為「天」的股價，假如頻率不是「天」，則可能會不一樣～。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/machine-learning%ef%bc%9a%e4%bd%bf%e7%94%a8%e6%8a%80%e8%a1%93%e6%8c%87%e6%a8%99%e9%a0%90%e6%b8%ac%e5%a4%a7%e7%9b%a4/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">788</post-id>	</item>
		<item>
		<title>論文導讀：利用MI-LSTM預測股價</title>
		<link>https://www.finlab.tw/%e5%88%a9%e7%94%a8mi-lstm%e9%a0%90%e6%b8%ac%e8%82%a1%e5%83%b9/</link>
					<comments>https://www.finlab.tw/%e5%88%a9%e7%94%a8mi-lstm%e9%a0%90%e6%b8%ac%e8%82%a1%e5%83%b9/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:39 +0000</pubDate>
				<category><![CDATA[AI看股票]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[機器學習]]></category>
		<category><![CDATA[程式交易]]></category>
		<category><![CDATA[量化投資]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=834</guid>

					<description><![CDATA[這篇文章是2018年剛發表的paper，算是非常新但是滿有趣的方法，針對一般的股票建構一個預測隔日價格的LSTM模型，以下就是他的方法思路。]]></description>
										<content:encoded><![CDATA[
<p>這篇文章是2018年剛發表的paper，算是非常新但是滿有趣的方法，針對一般的股票建構一個預測隔日價格的LSTM模型，以下就是他的方法思路。</p>



<h3 id="動機">動機</h3>



<p>股價預測一直都是非常熱門但具有挑戰性的機器學習題目，通常這類的文章不乏是使用各種的資訊，例如股價、技術指標、公司獲利、新聞資訊，並將這些資料餵給一些模型，例如 linear regression, SVM, 或是 NN 來預測未來股價。雖然這些方法被證實是可以使用的，但是預測能力不佳。因為當中有些資訊不但對於預測未來股價沒有幫助，反而會擾亂預測的準確度。</p>



<h3 id="此篇文章的貢獻">此篇文章的貢獻</h3>



<p>這篇文章提出了 multi-input LSTM model，來使用額外的神經網路額外架構，從低相關連的資訊找出有價值的，並且濾掉不必要的雜訊。並且利用了額外的LSTM邏輯來建構「mainstream」的結構，並用「mainstream」所計算出來的資訊，來遺忘或記憶有用的資訊（gates）。此架構比原本的LSTM效果要更好。</p>



<h3 id="MI-LSTM">MI-LSTM</h3>



<p>這是個特別針對金融股價預測所研發的架構，其原理是先找出想要預測的timeseries，並且找出N條高度正相關的timeseries（以股票而言，就可能是同類型的公司股價），還有N條最負相關的timeseries當作是此預測模型的輸入資料。有了這些資料後，下圖就是MILSTM的整體架構，其中 <strong>Y</strong>&nbsp;是欲預測的股票的歷史股價，而&nbsp;<img loading="lazy" width="13" height="16" src="" alt="" title="論文導讀：利用MI-LSTM預測股價 18">​​\bold{X}_n^i<strong>X</strong>​<em>n</em>​<em>i</em> ​​就是第 i 個高度正相關的timeseries，而\bold{X}_n^i<strong>X</strong>​<em>n</em>​<em>i</em>​​&nbsp;就是高度負相關的 timeseries。最後&nbsp;<strong><strong>X</strong></strong><em><strong><em><em><sub>i</sub></em></em></strong></em>&nbsp;是大盤的 timeseries。<a href="https://www.finlab.tw/%E5%88%A9%E7%94%A8MI-LSTM%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/structure.jpg"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="905" height="395" src="http://34.96.136.135/wp-content/uploads/2020/07/structure.jpg" alt="structure" class="wp-image-835" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/structure.jpg 905w, https://www.finlab.tw/wp-content/uploads/2020/07/structure-300x131.jpg 300w, https://www.finlab.tw/wp-content/uploads/2020/07/structure-768x335.jpg 768w" sizes="(max-width: 905px) 100vw, 905px" title="論文導讀：利用MI-LSTM預測股價 19"></figure></div>



<p>上圖從左到右的解說如下</p>



<ul><li>LSTM：此圖中的最左邊部分，就是簡單的LSTM計算而已，假如想要知道什麼是LSTM，可以參考<a rel="noreferrer noopener" href="https://colah.github.io/posts/2015-08-Understanding-LSTMs/" target="_blank">這篇關於LSTM的詳細解說</a>。</li><li>Average：往右，可以看到高度正負相關的timeseries的LSTM，額外經過平均的運算，至於為什麼要這樣做，相信是效果比較好的緣故，當然實做時也可以拿掉、或是使用不一樣的的計算方式。</li><li>Multi-Input LSTM：而比較有趣的地方，在於其中紅色的Multi-Input LSTM這個部分，在於它像是一個變種的LSTM架構，多了很多的 gate（請參考LSTM文章來瞭解什麼是「gate」）。此MILSTM可以額外展開如下圖，可以發現跟一般的LSTM長的差不多，但是多了有顏色的三條資訊流，並且此三條資訊流都有額外的gates來篩選要儲存的資訊。</li></ul>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="493" src="http://34.96.136.135/wp-content/uploads/2020/07/MIstructure-1024x493.jpg" alt="MIstructure" class="wp-image-836" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/MIstructure-1024x493.jpg 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/MIstructure-300x144.jpg 300w, https://www.finlab.tw/wp-content/uploads/2020/07/MIstructure-768x370.jpg 768w, https://www.finlab.tw/wp-content/uploads/2020/07/MIstructure.jpg 1047w" sizes="(max-width: 1024px) 100vw, 1024px" title="論文導讀：利用MI-LSTM預測股價 20"></figure></div>



<ul><li><a href="https://www.finlab.tw/%E5%88%A9%E7%94%A8MI-LSTM%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/MIstructure.jpg"></a>接下來是Attention Layer，其實跟LSTM原本的Attention是非常類似的，還是非常建議現瞭解<a rel="noreferrer noopener" href="https://colah.github.io/posts/2015-08-Understanding-LSTMs/" target="_blank">LSTM是什麼</a>，不然到這邊你應該已經頭很昏了XD。</li></ul>



<p>實驗結果</p>



<p>這篇文章使用了300檔股票的歷史股價進行訓練，單一一檔的股票歷史資料一定不足，但是假如300檔串在一起，則樣本數應該就足夠了，既然都已經發了期刊，當然實驗結果要比別人好，所以以下是實驗的結果，當中的MSE就是誤差（mean square error）的意思：<a href="https://www.finlab.tw/%E5%88%A9%E7%94%A8MI-LSTM%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/table.jpg"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1023" height="287" src="http://34.96.136.135/wp-content/uploads/2020/07/table.jpg" alt="table" class="wp-image-837" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/table.jpg 1023w, https://www.finlab.tw/wp-content/uploads/2020/07/table-300x84.jpg 300w, https://www.finlab.tw/wp-content/uploads/2020/07/table-768x215.jpg 768w" sizes="(max-width: 1023px) 100vw, 1023px" title="論文導讀：利用MI-LSTM預測股價 21"></figure></div>



<p>可以發現MI-LSTM獲得了最小的實驗誤差，相比起其它的model效果更好，而其它的model屬於LSTM的變形，這邊就先不介紹了。可以參考這篇LSTM文章。接下來，他們進一步分析了MI-LSTM中，每一類input的效果，種共有以下四類：</p>



<ol><li>Mainstream：欲預測的股票其歷史股價</li><li>Positive：高度正相關的一群股票</li><li>Negative：高度付相關的一群股票</li><li>Index：大盤</li></ol>



<p>下圖就是這四種不同的資訊流在神經網路中，通過softmax的時候的權重隨著訓練次數的變化：</p>



<p>最後可以發現Mainstream跟Index的資訊是最有效的，大盤對於股價的影響是非常大的，與投資時的狀況符合一致，大盤漲跌對於個股的影響很大。另外可以發現Positive跟Negative的權重被降低了，代表雖然這些資訊有很價值，但是當中也有很多的雜訊，所以權重被有效的分配。</p>



<h3 id="選股策略">選股策略</h3>



<p>這篇文章提出了一種選股的策略，對於所有股票，都判斷明日的漲跌，並且每日選出漲跌最佳的20檔股票，不考慮手續費，可以得到以下每段時間的歷史報酬：<a href="https://www.finlab.tw/%E5%88%A9%E7%94%A8MI-LSTM%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/table2.jpg"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="981" height="175" src="http://34.96.136.135/wp-content/uploads/2020/07/table2.jpg" alt="table2" class="wp-image-838" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/table2.jpg 981w, https://www.finlab.tw/wp-content/uploads/2020/07/table2-300x54.jpg 300w, https://www.finlab.tw/wp-content/uploads/2020/07/table2-768x137.jpg 768w" sizes="(max-width: 981px) 100vw, 981px" title="論文導讀：利用MI-LSTM預測股價 22"></figure></div>



<p>可以看到MI-LSTM的效果比傳統的LSTM和大盤還要好，當然考慮了手續費後效果可能會打很多折扣，甚至是虧損。要是我的話，會想辦法增加預測時間的長度（但預測難度也可能會增加），從每天變成每個禮拜，這樣手續費可能比較不那麼誇張多。但我猜他們可能也有做過類似的實驗，但沒有成功？或是他們覺得這樣已經很好了？</p>



<p>實做上建議使用pytorch或是tensorflow會比較簡單，這篇文章只是簡單的介紹其架構，然而要寫程式的話，還是利用paper中的數學公式，會比直接看架構圖更好理解和實做，會比較好喔！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/%e5%88%a9%e7%94%a8mi-lstm%e9%a0%90%e6%b8%ac%e8%82%a1%e5%83%b9/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">834</post-id>	</item>
		<item>
		<title>超簡單用Python預測股價</title>
		<link>https://www.finlab.tw/%e8%b6%85%e7%b0%a1%e5%96%ae-machine-learning-%e9%a0%90%e6%b8%ac%e8%82%a1%e5%83%b9/</link>
					<comments>https://www.finlab.tw/%e8%b6%85%e7%b0%a1%e5%96%ae-machine-learning-%e9%a0%90%e6%b8%ac%e8%82%a1%e5%83%b9/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:38 +0000</pubDate>
				<category><![CDATA[財經PYTHON教學]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[程式交易]]></category>
		<category><![CDATA[股價預測]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=735</guid>

					<description><![CDATA[多虧了python，用最先進的統計模型來預測股價，程式交易超簡單！不用安裝程式，今天我們雲端寫code，適合完完全全的初學者！非常多非常多的人都用 python 在開發程式，因此，有很多高深數學軟體，都會有很多民間高手來實做，而我們只要會用就好了！學python從這篇開始！]]></description>
										<content:encoded><![CDATA[
<p>多虧了python，用最先進的統計模型來預測股價，程式交易超簡單！<br>不用安裝程式，今天我們雲端寫code，適合完完全全的初學者！<br>非常多非常多的人都用 python 在開發程式，<br>因此，有很多高深數學軟體，都會有很多民間高手來實做，<br>而我們只要會用就好了！學python從這篇開始！<br><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j4.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="770" height="498" src="http://34.96.136.135/wp-content/uploads/2020/07/j4.png" alt="j4" class="wp-image-736" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j4.png 770w, https://www.finlab.tw/wp-content/uploads/2020/07/j4-300x194.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/j4-768x497.png 768w" sizes="(max-width: 770px) 100vw, 770px" title="超簡單用Python預測股價 23"></figure></div>



<p>我們還是來稍微科普一下相關的程式交易知識好了</p>



<h3 id="什麼是model（模型）">什麼是model（模型）</h3>



<p>模型就是一種能夠預測真實資料的數學公式，<br>通常會是一個function，例如 f 好了，<br>假如我們有一條時間序列 f(t) = at + b<br>只要知道了變數 a 跟 b，就可以計算出 t 每一刻的 f(t) 是多少</p>



<p>要怎麼知道 a 跟 b 是多少？<br>只要看其中兩個歷史時刻的位置即可：例如我們知道 f(1) = 1 且 f(2) = 2<br>則我們可以推算出 a = 1 且 b = 0，<br>並且預知 f(3) = 3 、 f(4) = 4 、 f(5) = 5 …</p>



<p>股價模型也是一樣，但是這個模型會比較複雜，不會只有 a、b 而已<br>不過規則是一樣的，我們要用歷史股價去計算類似 a、b 的變數，通常稱之為 fitting<br>找到 a、b 後就能夠用模型來預測股價了！</p>



<h3 id="用什麼模型預測股價呢？">用什麼模型預測股價呢？</h3>



<p>Facebook 最近推出了一個新型的時間序列預測模型Prophet，中文叫作預言家，<br>預言家可以預測一些週期性的時間序列<br>例如下圖，一個FB每天PO文的數量圖，隨時間關係圖：<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/pp1.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="947" height="491" src="http://34.96.136.135/wp-content/uploads/2020/07/pp1.png" alt="pp1" class="wp-image-737" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/pp1.png 947w, https://www.finlab.tw/wp-content/uploads/2020/07/pp1-300x156.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/pp1-768x398.png 768w" sizes="(max-width: 947px) 100vw, 947px" title="超簡單用Python預測股價 24"></figure></div>



<p>截圖自<a href="https://peerj.com/preprints/3190.pdf" rel="noreferrer noopener" target="_blank">fb論文</a></p>



<p>圖中，每個時間都只會對應到一個點，就是每天的PO文數量<br>我們可以觀察到一些現象：</p>



<h3 id="以每週來檢視">以每週來檢視</h3>



<p>上圖中，顏色的不同代表每週的變化量，禮拜六看起來明顯就比較少人用FB，<br>（週末都跑去玩了？XD）</p>



<h3 id="以每年來檢視">以每年來檢視</h3>



<p>我們也可以發現，以年度來說，暑假時PO文數會下降<br>（大家都去放暑假了？）</p>



<p>每個週期下都有各自的規律，傳統的模型能夠成功預測嗎？</p>



<h3 id="傳統模型的限制">傳統模型的限制</h3>



<p>其實已經有很多模型了，但為什麼FB還要推出一個新的呢？<br>原因是，以前的模型並沒有辦法多週期性的預測 f(t) ：<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/pp2.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="773" height="671" src="http://34.96.136.135/wp-content/uploads/2020/07/pp2.png" alt="pp2" class="wp-image-738" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/pp2.png 773w, https://www.finlab.tw/wp-content/uploads/2020/07/pp2-300x260.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/pp2-768x667.png 768w" sizes="(max-width: 773px) 100vw, 773px" title="超簡單用Python預測股價 25"></figure></div>



<p>這張圖的每個row是不同的模型<br>圖中每條「實線」就是預測模型的結果，<br>可以看出以往的模型，預測出來結果並不是很準</p>



<h3 id="預言家模型">預言家模型</h3>



<p>下圖中，可以發現預言家模型比上述的模型預測的更準確<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/pp3.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="946" height="373" src="http://34.96.136.135/wp-content/uploads/2020/07/pp3.png" alt="pp3" class="wp-image-739" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/pp3.png 946w, https://www.finlab.tw/wp-content/uploads/2020/07/pp3-300x118.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/pp3-768x303.png 768w" sizes="(max-width: 946px) 100vw, 946px" title="超簡單用Python預測股價 26"></figure></div>



<p>預言家模型最厲害的地方，<br>就是用多條 Fourier Series，去擬合（fitting）時間序列<br>在這邊就不把可怕的公式放上來了，<br>有興趣可以參考&nbsp;<a href="https://peerj.com/preprints/3190.pdf" rel="noreferrer noopener" target="_blank">fb論文</a><br>可以將週期調整至「每年」「每季」「每週」！<br>模型簡單可以寫成這樣：</p>



<p>f(t) = year(t) + season(t) + week(t) + trend(t)</p>



<p>甚至在fitting完（找到a、b等參數後）可以將 f(t) 的成分拆解出來看：<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/pp5.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="950" height="741" src="http://34.96.136.135/wp-content/uploads/2020/07/pp5.png" alt="pp5" class="wp-image-740" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/pp5.png 950w, https://www.finlab.tw/wp-content/uploads/2020/07/pp5-300x234.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/pp5-768x599.png 768w" sizes="(max-width: 950px) 100vw, 950px" title="超簡單用Python預測股價 27"></figure></div>



<p>而最後的 f(t) 就是這些曲線的疊加，<br>由上圖第三張，我們可以額外發現，聖誕節的時候，使用FB的人數下降非常快<br>外國的聖誕節，就是與家人團聚的時光，（像是我們的農曆新年）<br>所以不太會用手機的緣故</p>



<p>這篇論文大意就講到這裡，<br>詳情可以參考：<a href="https://peerj.com/preprints/3190.pdf" rel="noreferrer noopener" target="_blank">此論文</a></p>



<h3 id="用預言家預測股票">用預言家預測股票</h3>



<p>看完了學術的部分，接下來我們就實際的來使用預言家模型預測股價吧！<br>要預測的精準，我們要找有週期性的股票來分析，<br>這邊我就隨性的拿台積電當作是標的來預測囉～</p>



<h3 id="超簡單環境設定">超簡單環境設定</h3>



<p>安裝python、安裝jupyter？<br>不用，google提供了強大的雲端notebook，讓我們可以直接玩程式：<br><a href="https://colab.research.google.com/" rel="noreferrer noopener" target="_blank">https://colab.research.google.com/</a><br>打開連結，開啟新的notebook，在雲端寫程式！<br>當然你有python也可以開自己的，快的多</p>



<p>這次為了方便大家學習，我已經將環境建設好了！<br>在任意cell輸入：</p>



<pre class="wp-block-code"><code class="">!git clone https://github.com/koreal6803/Stocker.git
!pip install -r Stocker/requirements.txt</code></pre>



<p>即可完成！<br>需要一段安裝時間，使用google colab的話，可以去休息一下，喝個水再回來XD<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j0.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="67" src="http://34.96.136.135/wp-content/uploads/2020/07/j0-1024x67.png" alt="j0" class="wp-image-741" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j0-1024x67.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/j0-300x20.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/j0-768x50.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/j0.png 1240w" sizes="(max-width: 1024px) 100vw, 1024px" title="超簡單用Python預測股價 28"></figure></div>



<p>上述代碼中，每行開頭的驚嘆號，可以讓你把jupyter 當作 terminal 在用，<br>（不過假如windows用戶沒有git可能就無法…點上面google 線上程式連結）</p>



<h3 id="去除煩人的-warrning">去除煩人的 warrning</h3>



<p>接下來，我們先來刪除惱人的warrning，最近pandas越來越煩<br>一堆warrning…看了心煩直接刪除</p>



<pre class="wp-block-code"><code class="">import warnings

### 去除煩人的 warrning
warnings.filterwarnings('ignore')</code></pre>



<h3 id="讀入台積電股價">讀入台積電股價</h3>



<p>接下來就讀入附帶的台積點股價：</p>



<pre class="wp-block-code"><code class="">import pandas as pd

### 讀入series
df = pd.read_csv('Stocker/price.csv', index_col='date', parse_dates=['date'])
price = df.squeeze()
price.head()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="403" height="151" src="http://34.96.136.135/wp-content/uploads/2020/07/j1.png" alt="j1" class="wp-image-742" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j1.png 403w, https://www.finlab.tw/wp-content/uploads/2020/07/j1-300x112.png 300w" sizes="(max-width: 403px) 100vw, 403px" title="超簡單用Python預測股價 29"></figure></div>



<p><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j1.png"></a></p>



<p>其中的squeeze就是將dataframe變成series的function，<br>你可以用</p>



<pre class="wp-block-code"><code class="">print(df)
print(df.squeeze)</code></pre>



<p>來查看其中差別</p>



<h3 id="創建Stocker">創建Stocker</h3>



<p>這個Stocker並不是我寫的package，<br>而是利用<a href="https://github.com/WillKoehrsen/Data-Analysis/tree/master/stocker" rel="noreferrer noopener" target="_blank">國外別人寫好的</a><br>，再額外修改成更方便的版本</p>



<p>用法很簡單，把股價的series丟進去就可以了：</p>



<pre class="wp-block-code"><code class="">from Stocker.stocker import Stocker
tsmc = Stocker(price)</code></pre>



<h3 id="預測中期走勢">預測中期走勢</h3>



<p>接下來我們就可以用它來預測中期走勢，只需要一行</p>



<pre class="wp-block-code"><code class="">model, model_data = tsmc.create_prophet_model(days=90)</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="694" height="467" src="http://34.96.136.135/wp-content/uploads/2020/07/j2.png" alt="j2" class="wp-image-743" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j2.png 694w, https://www.finlab.tw/wp-content/uploads/2020/07/j2-300x202.png 300w" sizes="(max-width: 694px) 100vw, 694px" title="超簡單用Python預測股價 30"></figure></div>



<p><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j2.png"></a>其中綠色的就是預測出來的股價，而淺綠色區域就是信賴區間，股價大約就會落於此區間內，<br>程式中的信賴區間為80％，也代表了，每五天就約有一天的股價會落於信賴區間之外，<br>可以看到預測時間越久（沒實際股價的地方），不確定性越大，所以綠色的區間也就稍微變大<br>可以用來當作布林區間的感覺</p>



<h3 id="這樣預測準確嗎？">這樣預測準確嗎？</h3>



<p>不確定，所以我們可以把時間還原到一年前，來看看假如當時預測的結果，跟至今是不是有差別，<br>我們不讓Stocker使用近一年的股價來fitting，出來的曲線跟實際的股價做比較：</p>



<pre class="wp-block-code"><code class="">tsmc.evaluate_prediction()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="790" height="467" src="http://34.96.136.135/wp-content/uploads/2020/07/j3.png" alt="j3" class="wp-image-744" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j3.png 790w, https://www.finlab.tw/wp-content/uploads/2020/07/j3-300x177.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/j3-768x454.png 768w" sizes="(max-width: 790px) 100vw, 790px" title="超簡單用Python預測股價 31"></figure></div>



<p><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j3.png"></a>上圖中，紅色虛線就是開始預測的當下，紅色線以後的時間都是預言家預測的結果<br>而藍色則為預測的真實股價，黃色則是信賴區間</p>



<h3 id="不準怎麼辦？">不準怎麼辦？</h3>



<p>沒關係，我們可以調整短中長期的參數，讓它預測短中長期股價：</p>



<pre class="wp-block-code"><code class="">tsmc.changepoint_prior_analysis(changepoint_priors=[0.001, 0.05, 0.1, 0.2])</code></pre>



<p>下圖中，可以看到藍色的線比較偏趨勢，不隨當下股價起舞，算是長期的趨勢預測<br>而黃色則是非常貼近股價，算是短期的預測<br>使用者可以自行調整參數，來決定短中長線的應用<a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j4.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="770" height="498" src="http://34.96.136.135/wp-content/uploads/2020/07/j4-1.png" alt="j4 1" class="wp-image-745" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j4-1.png 770w, https://www.finlab.tw/wp-content/uploads/2020/07/j4-1-300x194.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/j4-1-768x497.png 768w" sizes="(max-width: 770px) 100vw, 770px" title="超簡單用Python預測股價 32"></figure></div>



<h3 id="拿來預測股價了！">拿來預測股價了！</h3>



<p>我們藉由上述的參數枚舉，可以知道哪一個參數（<code>changepoint_prior</code>）是最好的<br>我們可以藉由執行<code>predict_future</code>&nbsp;來預測股價囉！</p>



<pre class="wp-block-code"><code class="">tsmc.predict_future(days=100)</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="761" height="603" src="http://34.96.136.135/wp-content/uploads/2020/07/j5.png" alt="j5" class="wp-image-746" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/j5.png 761w, https://www.finlab.tw/wp-content/uploads/2020/07/j5-300x238.png 300w" sizes="(max-width: 761px) 100vw, 761px" title="超簡單用Python預測股價 33"></figure></div>



<p><a href="https://www.finlab.tw/%E8%B6%85%E7%B0%A1%E5%96%AE-Machine-Learning-%E9%A0%90%E6%B8%AC%E8%82%A1%E5%83%B9/j5.png"></a>藉由Stocker，我們可以很輕易的預測股價，<br>至於其準確性，還有待各位看官們回測看看囉！</p>



<p>有上左方課程的同學，可以很輕易的使用Stocker來檢驗歷史報酬率是多少！<br>可以試著找到最佳的參數，並盡量選擇有週期性的股票～<br>期待你們的成果！</p>



<p>此程式的一些連結：<br><a href="https://github.com/koreal6803/Stocker" rel="noreferrer noopener" target="_blank">此程式的github</a><br><a href="https://colab.research.google.com/drive/1KFhAhgtUcVVS1nMKlZicMjzGQZWKA6sc" rel="noreferrer noopener" target="_blank">此程式的notebook</a><br><a href="https://towardsdatascience.com/stock-prediction-in-python-b66555171a2" rel="noreferrer noopener" target="_blank">國外Stocker文章</a><br><a href="https://github.com/WillKoehrsen/Data-Analysis/tree/master/stocker" rel="noreferrer noopener" target="_blank">國外github</a><br><a href="https://github.com/facebook/prophet" rel="noreferrer noopener" target="_blank">預言家github</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/%e8%b6%85%e7%b0%a1%e5%96%ae-machine-learning-%e9%a0%90%e6%b8%ac%e8%82%a1%e5%83%b9/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">735</post-id>	</item>
		<item>
		<title>論文導讀：機器學習與基因演算法選股</title>
		<link>https://www.finlab.tw/%e6%a9%9f%e5%99%a8%e5%ad%b8%e7%bf%92%e8%88%87%e5%9f%ba%e5%9b%a0%e6%bc%94%e7%ae%97%e6%b3%95%e9%81%b8%e8%82%a1/</link>
					<comments>https://www.finlab.tw/%e6%a9%9f%e5%99%a8%e5%ad%b8%e7%bf%92%e8%88%87%e5%9f%ba%e5%9b%a0%e6%bc%94%e7%ae%97%e6%b3%95%e9%81%b8%e8%82%a1/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:38 +0000</pubDate>
				<category><![CDATA[AI看股票]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[機器學習]]></category>
		<category><![CDATA[程式交易]]></category>
		<category><![CDATA[量化投資]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=771</guid>

					<description><![CDATA[feature selection 是機器學習中非常重要的一環，尤其是財經領域的程式交易部分，因為財經領域有各式各樣的指標，又有財報、技術指標，這些指標高達尚千種，要如何選擇features，才能更有效預測股價？]]></description>
										<content:encoded><![CDATA[
<p>feature selection 是機器學習中非常重要的一環，尤其是財經領域的程式交易部分，因為財經領域有各式各樣的指標，又有財報、技術指標，這些指標高達尚千種，要如何選擇features，才能更有效預測股價？</p>



<p>今天介紹一篇<a href="https://arxiv.org/pdf/1806.01743.pdf" rel="noreferrer noopener" target="_blank">論文</a>，使用了基因演算法來幫忙選擇features，以下是更進一步的介紹：<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E8%88%87%E5%9F%BA%E5%9B%A0%E6%BC%94%E7%AE%97%E6%B3%95%E9%81%B8%E8%82%A1/thumbnail.jpg"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="960" height="480" src="http://34.96.136.135/wp-content/uploads/2020/07/thumbnail-1.jpg" alt="thumbnail 1" class="wp-image-772" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1.jpg 960w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-300x150.jpg 300w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-1-768x384.jpg 768w" sizes="(max-width: 960px) 100vw, 960px" title="論文導讀：機器學習與基因演算法選股 34"></figure></div>



<p>對於machine learning的程式交易演算法來說，<br>首先要有features，features可以想像成是指標，每一支股票都會有其features。<br>另外還有labels，也就是我們希望能夠預測的數值，例如股價漲跌的幅度，或是sharp ratio。</p>



<p>只要有了features跟labels，剩下的就簡單了，只要把這些features跟labels都輸入到一個黑盒子裡面，讓它學習，學成之後，只需要每次給它新的features，它就會預判label是多少。這個黑盒子通常稱為model，model有很多種類，最常見的就是deep neural network或是 random forest ，這些我們待會會簡單的介紹。</p>



<p>本篇論文在選擇features的階段，使用了基因演算法，由於財報、技術指標的數字有上千種，該如何選擇呢？第一步驟就是就全部都選擇吧！反正基因演算法會找出有用的features並且刪除無用的，至於基因演算法是如何做到的？它是模仿達爾文物競天擇的方式來找出有用的features：</p>



<h3 id="第一步：決定染色體">第一步：決定染色體</h3>



<p>以數學的方式描述，染色體是一條一維向量，其長度為所有features的長度，其數值為0或1，0代表不選入該feature，1代表選入該feature。</p>



<p>一開始會隨機製作出100條染色體，想必效果就是很隨機的，不是很好。<br>然而生物是會進化的，適者生存、不適者淘汰，要怎麼定義要淘汰的染色體呢？<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E8%88%87%E5%9F%BA%E5%9B%A0%E6%BC%94%E7%AE%97%E6%B3%95%E9%81%B8%E8%82%A1/crossover_bits.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="437" src="http://34.96.136.135/wp-content/uploads/2020/07/crossover_bits-1024x437.png" alt="crossover bits" class="wp-image-773" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/crossover_bits-1024x437.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/crossover_bits-300x128.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/crossover_bits-768x328.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/crossover_bits-1536x655.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/crossover_bits.png 2000w" sizes="(max-width: 1024px) 100vw, 1024px" title="論文導讀：機器學習與基因演算法選股 35"></figure></div>



<h3 id="第二步：決定染色體的成績">第二步：決定染色體的成績</h3>



<p>每一種染色體都代表一組features的選擇，單一染色體拿出來，用染色體所選入的features進行機器學習，最後評估機器學習的成果。在論文中，是使用<a href="https://medium.com/greyatom/lets-learn-about-auc-roc-curve-4a94b4d88152" rel="noreferrer noopener" target="_blank">AUC</a>來當作每個染色體的成績。</p>



<p>當然這時就可以進行適者生存、不適者淘汰的步驟，選出100個成績比較好的染色體，成績越好的染色體越有機會被選出來，但是也有可能成績很好但懷才不遇被篩選掉，這邊此文並沒有很明確的說明機制是什麼，實作上也有很多種方法，最簡單就是直接選成績最好的100個染色體。<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E8%88%87%E5%9F%BA%E5%9B%A0%E6%BC%94%E7%AE%97%E6%B3%95%E9%81%B8%E8%82%A1/selection.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="640" height="282" src="http://34.96.136.135/wp-content/uploads/2020/07/selection.png" alt="selection" class="wp-image-774" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/selection.png 640w, https://www.finlab.tw/wp-content/uploads/2020/07/selection-300x132.png 300w" sizes="(max-width: 640px) 100vw, 640px" title="論文導讀：機器學習與基因演算法選股 36"></figure></div>



<p>這個目的在於，基因演算法的最後，我們希望選出一條染色體，其AUC算出來是最高的，代表它能夠很有效的分別出會漲的股票跟會跌的股票。</p>



<h3 id="第三步：交叉遺傳">第三步：交叉遺傳</h3>



<p>我們會由現有的染色體，藉由排列組合，產生更多的染色體，具體的做法是：產生新的20條染色體，每條染色體皆由某兩條現有染色體的片段所組成，例如染色體的上半部和下半部是由不同染色體所提供的。<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E8%88%87%E5%9F%BA%E5%9B%A0%E6%BC%94%E7%AE%97%E6%B3%95%E9%81%B8%E8%82%A1/crossover.jpg"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="800" height="561" src="http://34.96.136.135/wp-content/uploads/2020/07/crossover.jpg" alt="crossover" class="wp-image-775" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/crossover.jpg 800w, https://www.finlab.tw/wp-content/uploads/2020/07/crossover-300x210.jpg 300w, https://www.finlab.tw/wp-content/uploads/2020/07/crossover-768x539.jpg 768w" sizes="(max-width: 800px) 100vw, 800px" title="論文導讀：機器學習與基因演算法選股 37"></figure></div>



<h3 id="第四步：變異">第四步：變異</h3>



<p>這個步驟中，這120條染色體會稍微的被隨機修改，每條染色體有10％的機率，其當中的一個bit會被反轉（由0變成1，或是1變成0）<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E8%88%87%E5%9F%BA%E5%9B%A0%E6%BC%94%E7%AE%97%E6%B3%95%E9%81%B8%E8%82%A1/mutation.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="351" height="199" src="http://34.96.136.135/wp-content/uploads/2020/07/mutation.png" alt="mutation" class="wp-image-776" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/mutation.png 351w, https://www.finlab.tw/wp-content/uploads/2020/07/mutation-300x170.png 300w" sizes="(max-width: 351px) 100vw, 351px" title="論文導讀：機器學習與基因演算法選股 38"></figure></div>



<p>上圖是一個三個bit變異的例子，有很多種的變異方式，本文中一次只更動一個bit而已。</p>



<h3 id="回到第二步，開始生物演化的循環">回到第二步，開始生物演化的循環</h3>



<p>每經過一次2、3、4步驟，就像是完成了一個世代，隨著是帶的變遷，平均來說，AUC的成績應該會越來越好，直到AUC的成績不再進步，就可以停止演化的過程。而其成果就是步驟二中，選出擁有最高的AUC的染色體，代表這些被選到的features組合在一起，預測能力是最強的！<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92%E8%88%87%E5%9F%BA%E5%9B%A0%E6%BC%94%E7%AE%97%E6%B3%95%E9%81%B8%E8%82%A1/evolution.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="365" src="http://34.96.136.135/wp-content/uploads/2020/07/evolution-1024x365.png" alt="evolution" class="wp-image-777" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/evolution-1024x365.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/evolution-300x107.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/evolution-768x274.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/evolution.png 1280w" sizes="(max-width: 1024px) 100vw, 1024px" title="論文導讀：機器學習與基因演算法選股 39"></figure></div>



<h2 id="機器學習">機器學習</h2>



<p>我們剛剛有說的，機器學習的model就像是黑盒子一樣，文中主要使用兩種model：deep neural network和random forest，都是非常常見的model，兩種model都有其優缺點，不一定哪個比較好。所以本文也提出了stacking的方式，將這兩種黑盒子的結果取平均，得出最後的答案，結果發現比單獨使用這兩種model更好。</p>



<h3 id="總結">總結</h3>



<p>我個人是滿好奇features沒有經過normalize就跑DNN不會有問題嗎？滿奇怪的，感覺DNN應該要額外再preprocessing一下，但是文中並沒有提到，就當作作者忘記說明好了。</p>



<p>我以前的做法是自己憑經驗找有效的features並且放進去測試，這篇文章使用了基因演算法來幫助大家篩選features，進行程式交易，大量的降低了人工feature engineering的時間，當然電腦也是要算很久，雖然我沒試過，但以普通單機電腦來說，估計要好幾天才能跑出最佳解。個人認為這篇論文實做起來是有效的。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/%e6%a9%9f%e5%99%a8%e5%ad%b8%e7%bf%92%e8%88%87%e5%9f%ba%e5%9b%a0%e6%bc%94%e7%ae%97%e6%b3%95%e9%81%b8%e8%82%a1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">771</post-id>	</item>
		<item>
		<title>如何用machine learning學習 總體經濟？</title>
		<link>https://www.finlab.tw/%e6%a9%9f%e5%99%a8%e5%ad%b8%e7%bf%92-%e7%b8%bd%e9%ab%94%e7%b6%93%e6%bf%9f/</link>
					<comments>https://www.finlab.tw/%e6%a9%9f%e5%99%a8%e5%ad%b8%e7%bf%92-%e7%b8%bd%e9%ab%94%e7%b6%93%e6%bf%9f/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:38 +0000</pubDate>
				<category><![CDATA[AI看股票]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[機器學習]]></category>
		<category><![CDATA[程式交易]]></category>
		<category><![CDATA[量化投資]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=779</guid>

					<description><![CDATA[今天這篇paper是介紹總體經濟，現在全球的經濟是連動的，例如中美貿易大戰影響到全球股價，金價油價對於美國股市也有很大的影響，今天這篇paper就是要來研究不同的指數，對於NASDAQ指數的影響。]]></description>
										<content:encoded><![CDATA[
<p>今天<a href="https://pdfs.semanticscholar.org/b68e/8d2f4d2c709bb5919b82effcb6a7bbd3db37.pdf" rel="noreferrer noopener" target="_blank">這篇paper</a>是介紹總體經濟，現在全球的經濟是連動的，例如中美貿易大戰影響到全球股價，金價油價對於美國股市也有很大的影響，今天這篇paper就是要來研究不同的指數，對於NASDAQ指數的影響。</p>



<p>這篇文章利用美股開盤前的所有重要指數，例如油價、貴金屬價格、以及全球股市的對於美股的影響，既然全球市場是連動的，那這其中必定有一些關聯，下圖就是這篇paper所使用的重要指數：<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%B8%BD%E9%AB%94%E7%B6%93%E6%BF%9F/data.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="592" src="http://34.96.136.135/wp-content/uploads/2020/07/data-1-1024x592.png" alt="data 1" class="wp-image-780" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/data-1-1024x592.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/data-1-300x173.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/data-1-768x444.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/data-1.png 1522w" sizes="(max-width: 1024px) 100vw, 1024px" title="如何用machine learning學習 總體經濟？ 40"></figure></div>



<h3 id="建構features">建構features</h3>



<p>這篇文章想要找出股價的趨勢，而不是絕對數值，所以其features除了股價外，也有一段時間股價的變化，這邊先叫做window size w，並且對於價格的變化進行normalize。</p>



<h3 id="分析features">分析features</h3>



<p>我覺得本文值得讚許的地方在於有提供一些簡單的方法做features的分析，可以由這些方法知道哪些features對於預測是非常有幫助的！</p>



<p>論文中使用了cross correlation來分析各種不同商品的價格對於NASDAQ影響。要說明cross correlation，就要先從correlation說起，correlation是兩種不同的數據，其相關性的係數。correlation介於-1到1之間，分別代表這兩種時間序列之間的關聯性：</p>



<ul><li>correlation = -1：代表數據完全負相關，也就是A漲B就跌的概念</li><li>correlation = 0：代表數據之間沒什麼關聯，也就是A漲跌跟B漲跌完全沒關係</li><li>correlation = +1：代表數據之間是正相關，也就是AB同漲同跌的關係</li></ul>



<p>那「cross」又是什麼意思呢？就是將兩組時間序列的時間錯開，差N天，並且觀察其correlation。</p>



<p>下圖就是所有的商品對於NASDAQ的cross correlation 分析，X軸就是差N天的N，而Y軸就是correlation，可以看到，在X = 0 時，NASDAQ跟NASDAQ的相關性是1，代表NASDAQ與自己的指數完全正相關，而很多商品都會在N = 0時有正相關性！<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%B8%BD%E9%AB%94%E7%B6%93%E6%BF%9F/crosscorrelation.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="791" src="http://34.96.136.135/wp-content/uploads/2020/07/crosscorrelation-1024x791.png" alt="crosscorrelation" class="wp-image-781" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/crosscorrelation-1024x791.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/crosscorrelation-300x232.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/crosscorrelation-768x593.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/crosscorrelation.png 1108w" sizes="(max-width: 1024px) 100vw, 1024px" title="如何用machine learning學習 總體經濟？ 41"></figure></div>



<p>所以我們使用N = 0來預測NASDAQ的股價會是最好的，但是N = 0的資料是沒有辦法使用的，因為你不能在當天的開盤就知道NASDAQ的收盤價，這樣就代表用到未來數據了。</p>



<p>所以我們只能退而求其次，用 N &lt; 0 的資料，也就是前N天的資料，我們可以發現N = -1的地方有一點小突起，代表某些資料對於NASDAQ的correlation也是很高的，可以拿來利用！</p>



<p>唯一想吐槽的是這張圖很多顏色都重複，所以根本看不太清楚是哪些商品，拿來預測NASDAQ比較準。</p>



<h3 id="三個維度的-cross-correlation">三個維度的 cross-correlation</h3>



<p>另外，這篇paper也提供了不同股價區間差w對於漲跌的correlation：<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%B8%BD%E9%AB%94%E7%B6%93%E6%BF%9F/crosscorrelation3d.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="745" src="http://34.96.136.135/wp-content/uploads/2020/07/crosscorrelation3d-1024x745.png" alt="crosscorrelation3d" class="wp-image-782" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/crosscorrelation3d-1024x745.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/crosscorrelation3d-300x218.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/crosscorrelation3d-768x559.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/crosscorrelation3d-1536x1118.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/crosscorrelation3d.png 1800w" sizes="(max-width: 1024px) 100vw, 1024px" title="如何用machine learning學習 總體經濟？ 42"></figure></div>



<p>可以發現商品價格與NASDAQ股價之間是有一定的關係的，假如商品之間彼此沒有關聯，這張圖就不會呈現三角形的山丘狀，而是會看起來像是隨機的樣子。</p>



<p>上面這張圖說明了一件事情，股價變化越長時間來觀察(w 越長)，NASDQL跟許多商品的前一天的correlation是越高的，可以藉由提高精準度！</p>



<p>藉由上述的兩個實驗，我們知道股價變化量的重要性，重點不是股價本身，而是股價的漲跌幅度才有辦法更好的預測股價～</p>



<p>除此之外，作者想要進一步知道哪些時間序列對於NASDQL是重要的，所以進行了一個簡單的預測實驗</p>



<h3 id="預測明天的價格">預測明天的價格</h3>



<p>作者也利用每一個單一序列當作單一feature，來預測隔天的漲跌，發現利用DAX這個時間序列來預估NASDQL會得到最好的結果：<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%B8%BD%E9%AB%94%E7%B6%93%E6%BF%9F/importance.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="462" src="http://34.96.136.135/wp-content/uploads/2020/07/importance-1024x462.png" alt="importance" class="wp-image-783" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/importance-1024x462.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/importance-300x135.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/importance-768x346.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/importance.png 1056w" sizes="(max-width: 1024px) 100vw, 1024px" title="如何用machine learning學習 總體經濟？ 43"></figure></div>



<p>作者並沒有說這邊的model是哪一種，我猜應該是SVM，因為feature_size很小。<br>這樣我們就可以找到比較重要跟不重要的時間序列，之所以要這樣是因為此篇paper有用SVM當作model，而SVM比較適用於小feature_size的traning set。</p>



<h3 id="預測N天後的價格">預測N天後的價格</h3>



<p>除了每天的價格外，此paper也研究K天後的價格漲跌，發現K越大，則預測的能力越好，下圖中 K 為 x 軸<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%B8%BD%E9%AB%94%E7%B6%93%E6%BF%9F/multiple_day.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="511" src="http://34.96.136.135/wp-content/uploads/2020/07/multiple_day-1024x511.png" alt="multiple day" class="wp-image-784" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/multiple_day-1024x511.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/multiple_day-300x150.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/multiple_day-768x383.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/multiple_day.png 1126w" sizes="(max-width: 1024px) 100vw, 1024px" title="如何用machine learning學習 總體經濟？ 44"></figure></div>



<h3 id="測試-w-的大小">測試 w 的大小</h3>



<p>接下來，此paper也測試了不同的 w 對於訓練的成效：&nbsp;<a href="https://en.wikipedia.org/wiki/F1_score" rel="noreferrer noopener" target="_blank">f1 score</a>&nbsp;的影響，f1 score是precision跟recall的綜合分數，其中：</p>



<ul><li>precision 是所有 model 顯示為 true，而真正為 true 的比例</li><li>recall 是所有真正為 true 的資料，被 model 標示為 true 的比例</li></ul>



<p>f1 score 就是 precision 和 recall 的 harmonic mean。<a href="https://en.wikipedia.org/wiki/Harmonic_mean#Harmonic_mean_of_two_numbers" rel="noreferrer noopener" target="_blank">harmonic mean</a>同時也被用在電阻的並聯計算上，你可以想像電阻並聯時，假如任何一個電阻太小，電流就會傾向從那裡通過，造成等效電阻很小。同理，你可以想像有一顆電阻叫做precision，另一顆叫做recall，任何一方太小，都會導致f1 score 變得很小。於是 f1 score 就是同時可以兼具 recall 和 accuracy 的一個優化指標。<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%B8%BD%E9%AB%94%E7%B6%93%E6%BF%9F/w.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="394" src="http://34.96.136.135/wp-content/uploads/2020/07/w-1024x394.png" alt="w" class="wp-image-785" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/w-1024x394.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/w-300x115.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/w-768x295.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/w-1536x591.png 1536w, https://www.finlab.tw/wp-content/uploads/2020/07/w-2048x788.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" title="如何用machine learning學習 總體經濟？ 45"></figure></div>



<p>上圖中的兩條線分別是 positive 和 negative 的 f1 score。當 w 在 0 或是 50 時，f1 score 相對來說比較好。</p>



<p>以上就是分析 w、K、等參數怎麼設定比較好的一些分析，當我們得到這些參數，並且建構出預測漲跌的model後，就可以將model產生的結果拿來回測</p>



<h3 id="回測績效">回測績效</h3>



<p>這邊總共用了三種方法來回測：</p>



<ol><li>假如預測會漲，就持續於整段回測週期持有股票</li><li>當預測會漲機率上升時，持有股票，不然則賣出</li><li>當預測會漲時，持有，反之則賣出</li></ol>



<p>以下是回測的結果：<a href="https://www.finlab.tw/%E6%A9%9F%E5%99%A8%E5%AD%B8%E7%BF%92-%E7%B8%BD%E9%AB%94%E7%B6%93%E6%BF%9F/backtest.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="1024" height="664" src="http://34.96.136.135/wp-content/uploads/2020/07/backtest-1-1024x664.png" alt="backtest 1" class="wp-image-786" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/backtest-1-1024x664.png 1024w, https://www.finlab.tw/wp-content/uploads/2020/07/backtest-1-300x195.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/backtest-1-768x498.png 768w, https://www.finlab.tw/wp-content/uploads/2020/07/backtest-1.png 1042w" sizes="(max-width: 1024px) 100vw, 1024px" title="如何用machine learning學習 總體經濟？ 46"></figure></div>



<p>可以發現利用第三種方式，回測的績效比較好，這邊跟我自己做出來的結論是差不多的。</p>



<h3 id="總結">總結</h3>



<p>今天學到的一些知識：</p>



<ol><li>用cross-correlation來分析features</li><li>利用SVM來測驗單個features是否有辦法預測漲跌，進而分析feature的好壞</li><li>使用移動窗格來找出最佳的f1_score</li></ol>



<p>這篇paper利用ML的方式，找出世界上重要的商品價格之間的關聯，並且也有使用SVM或是MART來預測NASDAQ指數的漲跌，個人覺得滿有趣的。經總就應該這樣作研究！</p>



<p>美中不足的是應該可以歸納出一些現象，輔助一般不會程式的投資人，這樣會更好！</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/%e6%a9%9f%e5%99%a8%e5%ad%b8%e7%bf%92-%e7%b8%bd%e9%ab%94%e7%b6%93%e6%bf%9f/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">779</post-id>	</item>
		<item>
		<title>讓Machine Learning幫你看財報！</title>
		<link>https://www.finlab.tw/%e8%ae%93machine-learning%e5%b9%ab%e4%bd%a0%e7%9c%8b%e8%b2%a1%e5%a0%b1%ef%bc%81/</link>
					<comments>https://www.finlab.tw/%e8%ae%93machine-learning%e5%b9%ab%e4%bd%a0%e7%9c%8b%e8%b2%a1%e5%a0%b1%ef%bc%81/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:01 +0000</pubDate>
				<category><![CDATA[AI看股票]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[機器學習]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=453</guid>

					<description><![CDATA[機器學習不是我很專攻的領域，但幸好現在很多強大的package，可以直接使用，假如你不想深入開發model的話，只要把它當作黑盒子就好了，把它插上插頭，讓資料流進去、流出來。很簡單！]]></description>
										<content:encoded><![CDATA[
<p>機器學習不是我很專攻的領域，但幸好現在很多強大的package，可以直接使用，假如你不想深入開發model的話，只要把它當作黑盒子就好了，把它插上插頭，讓資料流進去、流出來。很簡單！<a href="https://www.finlab.tw/%E8%AE%93Machine-Learning%E5%B9%AB%E4%BD%A0%E7%9C%8B%E8%B2%A1%E5%A0%B1%EF%BC%81/thumbnail.jpeg"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="293" height="172" src="http://34.96.136.135/wp-content/uploads/2020/07/thumbnail-1.jpeg" alt="thumbnail 1" class="wp-image-454" title="讓Machine Learning幫你看財報！ 47"></figure></div>



<h3 id="免費訓練資料">免費訓練資料</h3>



<p>最下方免費有獲得2萬筆訓練資料的方式，在坊間一定是要賣錢的，甚至連課程一起賣，但我想說算了啦，反正我就是個草根碼農XDD，希望大家可以一起互相切磋。</p>



<p>之前寫的策略，選股方式都是用條件拼湊的：</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="371" height="70" src="http://34.96.136.135/wp-content/uploads/2020/07/Screenshot-2020-07-09-at-15.45.01.png" alt="Screenshot 2020 07 09 at 15.45.01" class="wp-image-455" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/Screenshot-2020-07-09-at-15.45.01.png 371w, https://www.finlab.tw/wp-content/uploads/2020/07/Screenshot-2020-07-09-at-15.45.01-300x57.png 300w" sizes="(max-width: 371px) 100vw, 371px" title="讓Machine Learning幫你看財報！ 48"></figure></div>



<p>但是現在用machine learning的話，就會寫成這樣：</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="314" height="68" src="http://34.96.136.135/wp-content/uploads/2020/07/Screenshot-2020-07-09-at-15.45.42.png" alt="Screenshot 2020 07 09 at 15.45.42" class="wp-image-456" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/Screenshot-2020-07-09-at-15.45.42.png 314w, https://www.finlab.tw/wp-content/uploads/2020/07/Screenshot-2020-07-09-at-15.45.42-300x65.png 300w" sizes="(max-width: 314px) 100vw, 314px" title="讓Machine Learning幫你看財報！ 49"></figure></div>



<p>這邊用的是分類（classification），假如覺得會上漲，就是1，會下跌就是0。<br>上面的&nbsp;<em>f</em>&nbsp;就是機器學習的model，我們不太知道這個是個怎樣的函數，因為這個model不是一個公式，而是經過資料訓練而來的。這個簡單的範例只有2個features：ROE、PB。只是一個例子而已，下面會有更多的features：</p>



<h3 id="資料">資料</h3>



<p>資料是從2010年底，到2017年，每次財報截止的隔天買入，並在下次財報截止前賣出。<br>至於features就是用<a href="https://www.finlab.tw/tags/%E6%AD%B7%E5%8F%B2%E5%9B%9E%E6%B8%AC/">之前寫文章</a>，覺得好用的：</p>



<ul><li><strong>月營收</strong>&nbsp;買入前12個月</li><li><strong>ROE</strong>&nbsp;買入前12季財報</li><li><strong>本益比</strong>&nbsp;買入前一天</li><li><strong>股價淨值比</strong>&nbsp;買入前一天</li><li><strong>殖利率</strong>&nbsp;最近一期</li><li>股價是否大於季線</li><li>股價是否大於半年線</li><li><strong>負債比率</strong>&nbsp;買入前12季財報</li></ul>



<p>可以到這裡下載歷史資料，假如有人訓練的比我的好的話（應該滿簡單的），歡迎一起討論，我們可以一起合作建構一個初版的平台，或是有人想要train不同的feature，也歡迎留言給我。</p>



<p>除了上述的features，每筆資料還包含：</p>



<ul><li>stock_ID 股票的代號</li><li>buy_date 買入日期</li><li>buy_price 買入開盤價</li><li>sell_date 賣出開盤價</li><li>sell_price 賣出價格</li><li>gain 賣出價/買入價</li></ul>



<p>不確定這些歷史數據，有沒有錯，假如不放心，可以幫我稍微檢驗一下，另外，關於買賣日期，我是參考IFRS制度上市前後不同的財報截止日，然而有些企業遲交財報，就沒辦法了…所以會包含一點未來數據。</p>



<p><a href="http://www.rich01.com/2015/11/blog-post_28.html" rel="noreferrer noopener" target="_blank">這篇文章</a>有講到財報遲交的公司，還是別買的好，而每年大約只有100家公司會遲交財報，以整體資料而言，未來數據應該不會太多（我覺得啦XD，但不知道資料科學家們怎麼看就是了）。</p>



<p>資料僅供學術發展，請勿用於商業用途！</p>



<h3 id="訓練model的方式">訓練model的方式</h3>



<p>由於我想要弄一個快速的prototype，所以先使用random forest model，這樣就不用pre-processing了XDD</p>



<p>Model</p>



<pre class="wp-block-code"><code>from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier(n_estimators=500, max_leaf_nodes=None, class_weight='balanced')</code></pre>



<p>以上是用sklearn裡的模型，連參數什麼都沒給，這樣的效果還OK了。訓練時，training data使用2014年以前的資料，而test data是2015年以後的資料</p>



<h3 id="結果太好了，我不敢相信…">結果太好了，我不敢相信…</h3>



<p>以下是還沒篩選前的獲利分佈圖，這邊的獲利是用倍數來衡量，也就是 賣出價格/買入價格。可以看到，這是一個對稱於1的分佈圖，大於1（賺）跟小於1（賠）其實很接近，其平均值是在1.005左右，你有看到一藍藍的地方嗎？那就是篩選後的標的：<a href="https://www.finlab.tw/%E8%AE%93Machine-Learning%E5%B9%AB%E4%BD%A0%E7%9C%8B%E8%B2%A1%E5%A0%B1%EF%BC%81/dis1.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="378" height="252" src="http://34.96.136.135/wp-content/uploads/2020/07/dis1.png" alt="dis1" class="wp-image-459" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/dis1.png 378w, https://www.finlab.tw/wp-content/uploads/2020/07/dis1-300x200.png 300w" sizes="(max-width: 378px) 100vw, 378px" title="讓Machine Learning幫你看財報！ 50"></figure></div>



<p>下面我們把藍色的地方直接畫一張圖，可以發現篩選後，發現結果好很多，分佈偏向大於1，也就是很多的選擇是有獲利的！<a href="https://www.finlab.tw/%E8%AE%93Machine-Learning%E5%B9%AB%E4%BD%A0%E7%9C%8B%E8%B2%A1%E5%A0%B1%EF%BC%81/dis2.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="382" height="252" src="http://34.96.136.135/wp-content/uploads/2020/07/dis2.png" alt="dis2" class="wp-image-460" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/dis2.png 382w, https://www.finlab.tw/wp-content/uploads/2020/07/dis2-300x198.png 300w" sizes="(max-width: 382px) 100vw, 382px" title="讓Machine Learning幫你看財報！ 51"></figure></div>



<p>平均來到了1.10左右，代表每季買一群股票，平均來說會賺10％左右。且勝率來到了70.2%</p>



<p>對於這樣的結果，我感到有點難過，因為成果太好代表裡面一定有什麼問題，我還是抱著懷疑的態度，各位可以做最嚴格的檢驗，機器學習不是我的專長，願意虛心受教，或是你還希望有什麼額外的features呢？我幾乎都可以幫你生成客製的test data喔！</p>



<h3 id="附註：免費取得訓練資料">附註：免費取得訓練資料</h3>



<p>取得資料的方法很簡單，幫冷清的寒舍小小宣傳一下：</p>



<ol><li>按右下角分享於FB</li><li>將分享截圖傳到&nbsp;<a href="mailto:finlabstaff@gmail.com" rel="noreferrer noopener" target="_blank">finlabstaff@gmail.com</a></li><li>一兩天內即可獲得 data.csv！</li></ol>



<p>依照本blog的含金量、data.csv的完整程度，你一定不會後悔的！</p>



<p>詳細的python教學：<a href="https://www.finlab.tw/%E5%88%A9%E7%94%A8Machine-Learning-%E9%81%B8%E8%82%A1%E6%96%B0%E6%89%8B%E6%95%99%E5%AD%B8/">利用Machine-Learning-選股新手教學</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/%e8%ae%93machine-learning%e5%b9%ab%e4%bd%a0%e7%9c%8b%e8%b2%a1%e5%a0%b1%ef%bc%81/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">453</post-id>	</item>
		<item>
		<title>利用Machine Learning 選股新手教學</title>
		<link>https://www.finlab.tw/%e5%88%a9%e7%94%a8machine-learning-%e9%81%b8%e8%82%a1%e6%96%b0%e6%89%8b%e6%95%99%e5%ad%b8/</link>
					<comments>https://www.finlab.tw/%e5%88%a9%e7%94%a8machine-learning-%e9%81%b8%e8%82%a1%e6%96%b0%e6%89%8b%e6%95%99%e5%ad%b8/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:05:01 +0000</pubDate>
				<category><![CDATA[AI看股票]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[機器學習]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=467</guid>

					<description><![CDATA[其實要machine選股真的很簡單！這篇blog就會帶大家從頭開始，用最簡單的方式，將Machine Learning實做！這邊只是給大家一個簡單的架構，可以自己去嘗試優化，讓它更聰明！以下是用mac或是linux的command line完成的，假如有任何錯誤或不懂的地方歡迎聯絡我喔～]]></description>
										<content:encoded><![CDATA[
<p>最近一直在弄Machine Learning，都快忘記寫blog了，哈！</p>



<p>其實要machine選股真的很簡單！這篇blog就會帶大家從頭開始，用最簡單的方式，將Machine Learning實做！這邊只是給大家一個簡單的架構，可以自己去嘗試優化，讓它更聰明！以下是用mac或是linux的command line完成的，假如有任何錯誤或不懂的地方歡迎聯絡我喔～<a href="https://www.finlab.tw/%E5%88%A9%E7%94%A8Machine-Learning-%E9%81%B8%E8%82%A1%E6%96%B0%E6%89%8B%E6%95%99%E5%AD%B8/thumbnail.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="883" height="557" src="http://34.96.136.135/wp-content/uploads/2020/07/thumbnail.png" alt="thumbnail" class="wp-image-468" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail.png 883w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-300x189.png 300w, https://www.finlab.tw/wp-content/uploads/2020/07/thumbnail-768x484.png 768w" sizes="(max-width: 883px) 100vw, 883px" title="利用Machine Learning 選股新手教學 52"></figure></div>



<h3 id="1-免費取得資料">1. 免費取得資料</h3>



<p>取得資料的方法很簡單，幫冷清的寒舍小小宣傳一下：</p>



<ol><li>分享任一你覺得很有趣的FinLab文章於FB</li><li>將分享截圖傳到&nbsp;<a href="mailto:finlabstaff@gmail.com" rel="noreferrer noopener" target="_blank">finlabstaff@gmail.com</a></li><li>一兩天內即可獲得 data.csv！</li></ol>



<p>依照本blog的含金量、data.csv的完整程度，你一定不會後悔的！</p>



<h3 id="2-架設環境">2. 架設環境</h3>



<p>假如你已經會使用python了，那可以直接跳過這個步驟，假如不會的話，可以照著下面的步驟完成：</p>



<ol><li>到<a href="https://www.python.org/downloads/" rel="noreferrer noopener" target="_blank">官網</a>下載<strong>Python3</strong>，並且安裝</li><li>安裝<a href="https://pip.pypa.io/en/stable/" rel="noreferrer noopener" target="_blank">pip</a></li><li>安裝<a href="https://virtualenv.pypa.io/en/stable/" rel="noreferrer noopener" target="_blank">virtualenv</a></li></ol>



<h4 id="為何要安裝pip呢？">為何要安裝pip呢？</h4>



<p>pip可以幫我們下載Machine Learning所需要的package（也就是python的libraries）。</p>



<h4 id="為何要安裝-virtualenv-呢？">為何要安裝&nbsp;<strong>virtualenv</strong>&nbsp;呢？</h4>



<p><strong>virtualenv</strong>可以幫你產生project的環境，每一個project用自己的libraries，這樣比較不容易有衝突。</p>



<h3 id="3-建置project">3. 建置project</h3>



<p>首先，先用<strong>virtualenv</strong>創建project</p>



<p>create project</p>



<pre class="wp-block-code"><code>virtualenv finance_ml
cd finance_ml</code></pre>



<p>接下來必須要開啟工作環境：</p>



<p>activate and deactivate project</p>



<pre class="wp-block-code"><code>source bin/activate
deactivate</code></pre>



<p>這邊有兩行，一行是開啟環境，一行是結束環境！假如你還要繼續，先不要輸入第二行。<br>將data.csv複製到當前資料夾，並且安裝python packages</p>



<p>install python packages</p>



<pre class="wp-block-code"><code>pip install jupyter
pip install pandas
pip install sklearn
pip install matplotlib</code></pre>



<h3 id="4-開工！">4. 開工！</h3>



<p>首先就是開啟jupyter notebook，並開一個python的環境</p>



<p>start notebook</p>



<pre class="wp-block-code"><code>jupyter notebook</code></pre>



<p>這樣會開啟一個網頁版的GUI介面，只要新增python的notebook就可以用了！<br>接下來進入我們的主題：機器學習程式碼！</p>



<h4 id="A-讀入資料">A. 讀入資料</h4>



<p>這邊我們用 pandas 讀入所需要的資料，index_col可以設定要哪一個columns當作index。這邊的index選擇在 data.csv 中，第一個未命名的column。</p>



<p>read data</p>



<pre class="wp-block-code"><code>import pandas as pd
data = pd.read_csv('data.csv', index_col='Unnamed: 0', parse_dates=&#91;'buy_date', 'sell_date'])</code></pre>



<h4 id="B-處理資料">B.處理資料</h4>



<p>我們將資料分成三類，一類是跑回測用的dbacktest，另一類是機器學習用的dtraning，另外一個是traning完用來評估的dtest。</p>



<p>process data</p>



<pre class="wp-block-code"><code>from sklearn.model_selection import train_test_split

# clear data 將爛資料去掉
data.dropna(how='any', inplace=True)

# 選擇2015年以後的資料作為回測用
dbacktest = data&#91;data&#91;'buy_date'] > '2015']

# 將其於的資料再分成兩類：dtrain 跟 dtest
dataTrainTest = data&#91;data&#91;'sell_date'] &lt; '2015']
dtrain, dtest = train_test_split(dataTrainTest, test_size=0.10, random_state=42)

# 設定我們的feature要用哪些
features = data.columns&#91;5:-1]</code></pre>



<h4 id="C-機器學習訓練資料">C. 機器學習訓練資料</h4>



<p>這邊我們用很簡單的<a rel="noreferrer noopener" href="https://www.stat.berkeley.edu/~breiman/RandomForests/cc_home.htm" target="_blank">隨機森林</a>，來完成的，可以參考連結來學習怎麼樣調整參數，這應該算是機器學習中，前置作業最少的模型了！</p>



<p>learning</p>



<pre class="wp-block-code"><code>
from sklearn.ensemble import RandomForestClassifier

# 創建機器學習的model
rf = RandomForestClassifier(n_estimators=10, criterion='gini', 
                            max_depth=None, min_samples_split=2, 
                            min_samples_leaf=1, min_weight_fraction_leaf=0.0, 
                            max_features='auto', max_leaf_nodes=None, 
                            bootstrap=True, oob_score=False, n_jobs=1, 
                            random_state=None, verbose=0, warm_start=False, 
                            class_weight=None)
# 分類能獲利的股票
rf.fit(dtrain&#91;features], dtrain&#91;'獲利'] > 1)</code></pre>



<h4 id="D-結果">D. 結果</h4>



<p>你說這樣就結束了！？對呀結束了。其實machine learning要簡單真的就只是這樣而已！<br>接下來我們來看一下它預測的准不準：</p>



<p>predict</p>



<pre class="wp-block-code"><code>
result = rf.predict(dtest&#91;features])

print('test data')
print('gain before filtered', dtest&#91;'獲利'].mean())
print('gain after filtered', dtest&#91;'獲利']&#91;result].mean())
print('num stocks', sum(result), '/', len(dtest))

result = rf.predict(dbacktest&#91;features])

print('backtest data')
print('gain before filtered', dbacktest&#91;'獲利'].mean())
print('gain after filtered', dbacktest&#91;'獲利']&#91;result].mean())
print('num stocks', sum(result), '/', len(dbacktest))</code></pre>



<p>假如一切順利就會看到以下結果：</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="353" height="145" src="http://34.96.136.135/wp-content/uploads/2020/07/result.png" alt="result" class="wp-image-477" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/result.png 353w, https://www.finlab.tw/wp-content/uploads/2020/07/result-300x123.png 300w" sizes="(max-width: 353px) 100vw, 353px" title="利用Machine Learning 選股新手教學 53"></figure></div>



<p><a href="https://www.finlab.tw/%E5%88%A9%E7%94%A8Machine-Learning-%E9%81%B8%E8%82%A1%E6%96%B0%E6%89%8B%E6%95%99%E5%AD%B8/result.png"></a><br>這邊的gain是指平均的報酬率，也就是所有股票的價格在賣出時是否賺錢，小於一就是虧，大於一就是賺。</p>



<p>結果看起來還滿猛的！竟然有接近7%的報酬率！</p>



<p>但真的是如此嗎？我們把它畫成隨著時間，本金成長的獲利圖：</p>



<p>backtest</p>



<pre class="wp-block-code"><code>%matplotlib inline

dbacktest&#91;'預測'] = pd.Series(result, index=dbacktest.index)
dates = list(set(dbacktest&#91;'buy_date']))
dates.sort()

history = &#91;]
for d in dates:
    history.append(dbacktest&#91;(dbacktest&#91;'buy_date'] == d) &amp; (dbacktest&#91;'預測'])]&#91;'獲利'].mean())
    
pd.Series(history, index=dates).cumprod().plot()</code></pre>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="407" height="252" src="http://34.96.136.135/wp-content/uploads/2020/07/eq-7.png" alt="eq 7" class="wp-image-479" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/eq-7.png 407w, https://www.finlab.tw/wp-content/uploads/2020/07/eq-7-300x186.png 300w" sizes="(max-width: 407px) 100vw, 407px" title="利用Machine Learning 選股新手教學 54"></figure></div>



<p><a href="https://www.finlab.tw/%E5%88%A9%E7%94%A8Machine-Learning-%E9%81%B8%E8%82%A1%E6%96%B0%E6%89%8B%E6%95%99%E5%AD%B8/eq.png"></a>發現其實沒有想像中那麼好。這是意料之中的結果，畢竟我們的learning時間才幾秒鐘，假如增加n_estimator這個變數，是否可以讓結果更好？假如使用Regression會不會更好？假如改變一下機器學習的參數、或修改一下features，是否可以選到更好的股票呢？就留給各位慢慢玩！以下是可以改進的地方：</p>



<ol><li>feature engineering：將features做修正、刪除、normalize等</li><li>篩選能獲利X%的股票</li><li>classification vs regression：可以兩種都試試看</li><li>不同的model，可以試試SVM、NN、XGBoost等等</li><li>不同的model都有可以調整超多參數來達到優化</li><li>利用early stop 來預防overfitting。</li></ol>



<p>我只是列出最基本的，還有很多可以玩的，這也是我這禮拜正在弄的東西，害我都沒時間寫網誌XDD</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/%e5%88%a9%e7%94%a8machine-learning-%e9%81%b8%e8%82%a1%e6%96%b0%e6%89%8b%e6%95%99%e5%ad%b8/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">467</post-id>	</item>
		<item>
		<title>Machine Learning 表示：看季線最無用！</title>
		<link>https://www.finlab.tw/machine-learning-%e8%a1%a8%e7%a4%ba%ef%bc%9a%e7%9c%8b%e5%ad%a3%e7%b7%9a%e6%9c%80%e7%84%a1%e7%94%a8%ef%bc%81/</link>
					<comments>https://www.finlab.tw/machine-learning-%e8%a1%a8%e7%a4%ba%ef%bc%9a%e7%9c%8b%e5%ad%a3%e7%b7%9a%e6%9c%80%e7%84%a1%e7%94%a8%ef%bc%81/#respond</comments>
		
		<dc:creator><![CDATA[FinLab - 韓承佑]]></dc:creator>
		<pubDate>Wed, 22 Jul 2020 07:04:43 +0000</pubDate>
				<category><![CDATA[AI看股票]]></category>
		<category><![CDATA[MACHINE LEARNING]]></category>
		<category><![CDATA[PYTHON]]></category>
		<category><![CDATA[機器學習]]></category>
		<category><![CDATA[選股]]></category>
		<guid isPermaLink="false">http://34.96.136.135/?p=462</guid>

					<description><![CDATA[最近喜歡做 machine learning 的預測，發現機器學習看財報，預測下季收盤價還滿準的。

Alpha Go 讓人類的圍棋可以到達新境界，機器學習中學習到一些它判斷財報的方法，今天來剖開上一篇教學中，機器學習的大腦，來看一下機器人如何選股！]]></description>
										<content:encoded><![CDATA[
<p>最近喜歡做 machine learning 的預測，發現機器學習看財報，預測下季收盤價還滿準的。</p>



<p>Alpha Go 讓人類的圍棋可以到達新境界，機器學習中學習到一些它判斷財報的方法，今天來剖開上一篇教學中，機器學習的大腦，來看一下機器人如何選股！<a href="https://www.finlab.tw/Machine-Learning-%E8%A1%A8%E7%A4%BA%EF%BC%9A%E7%9C%8B%E5%AD%A3%E7%B7%9A%E6%9C%80%E7%84%A1%E7%94%A8%EF%BC%81/thumbnail.jpeg"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="284" height="177" src="http://34.96.136.135/wp-content/uploads/2020/07/thumbnail-2.jpeg" alt="thumbnail 2" class="wp-image-463" title="Machine Learning 表示：看季線最無用！ 55"></figure></div>



<h3 id="Machine-Learning：季線無用！">Machine Learning：季線無用！</h3>



<p>機器學習的模型就像是一個黑盒子，很難知道他其中的工作原理，只能看到線性化後，每個feature的權重。發現了一個很有趣的狀況，就是 “股價在季線上” 這個條件，權重超低！</p>



<p>假如你買賣股票也跟我的模型一樣，是根據每一季財報來決定，那機器學習的結果，表示不用看季線，因為幾乎沒啥用。</p>



<p>看季線還不如看近10週收盤價！發現把週收盤價納入參考，也可以增進效果。代表長線的技術指標是有用的！</p>



<h3 id="重要的財報數據">重要的財報數據</h3>



<p>以下features的重要程度按順序排列：</p>



<ol><li><strong>月營收增長率</strong>很重要，而且不能只看一個月！</li><li><strong>當季ROE</strong>跟<strong>去年同季ROE</strong>很重要！</li><li><strong>EPS季增長率</strong>也很重要！</li><li><strong>總資產轉換率</strong>&nbsp;較不重要，但也不賴啦！</li><li>其它，重要程度：<strong>股價淨值比</strong>&nbsp;=&nbsp;<strong>負債比率</strong>&nbsp;&gt;&nbsp;<strong>本益比</strong>&nbsp;=&nbsp;<strong>殖利率</strong></li><li><strong>週收盤價</strong>比上面的難用，但好像可以判斷一些東西，但一定是非線性的，所以不知道其中原理XD</li><li>重要程度趨近於0的：<strong>收盤 &gt; 季線</strong></li></ol>



<p>真的很少網站可以像這裡一樣，告訴你哪些是比較有用的數據，哪些比較難用，我們真的要跟機器來學習，一般人都會認為股價淨值比、本益比是最重要的，但是這邊做出了一個不一樣的結論：<strong>營收是否增加</strong>才是股票漲跌的關鍵。</p>



<p>這樣做的用意只是給大家一個概念，也不代表機器學習的結果就得奉為聖旨。</p>



<h3 id="機器學習評分財報">機器學習評分財報</h3>



<p>機器學習模型最後會幫每個股票評分，總共100分，最低0分，50分就是他覺得股票不漲不跌，所以我們將每季評分分成四組，由低到高：<a href="https://www.finlab.tw/Machine-Learning-%E8%A1%A8%E7%A4%BA%EF%BC%9A%E7%9C%8B%E5%AD%A3%E7%B7%9A%E6%9C%80%E7%84%A1%E7%94%A8%EF%BC%81/dis.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="608" height="252" src="http://34.96.136.135/wp-content/uploads/2020/07/dis.png" alt="dis" class="wp-image-464" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/dis.png 608w, https://www.finlab.tw/wp-content/uploads/2020/07/dis-300x124.png 300w" sizes="(max-width: 608px) 100vw, 608px" title="Machine Learning 表示：看季線最無用！ 56"></figure></div>



<p>可以看到有很多的評分都在0分跟100分，因為分數算法的關係，100分的部分可以想成超過一百分！（國軍online）</p>



<p>之後會在將 0 跟 100 分再區分清楚一點～</p>



<h3 id="回測！">回測！</h3>



<p>接下來我們將分數由低到高分4組，分別回測：<a href="https://www.finlab.tw/Machine-Learning-%E8%A1%A8%E7%A4%BA%EF%BC%9A%E7%9C%8B%E5%AD%A3%E7%B7%9A%E6%9C%80%E7%84%A1%E7%94%A8%EF%BC%81/eq.png"></a></p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" width="598" height="267" src="http://34.96.136.135/wp-content/uploads/2020/07/eq-6.png" alt="eq 6" class="wp-image-465" srcset="https://www.finlab.tw/wp-content/uploads/2020/07/eq-6.png 598w, https://www.finlab.tw/wp-content/uploads/2020/07/eq-6-300x134.png 300w" sizes="(max-width: 598px) 100vw, 598px" title="Machine Learning 表示：看季線最無用！ 57"></figure></div>



<p>報酬率由低到高很漂亮的一字排開！分數低果然很差，而分數高真的很好。<br>可以看到50分以上的兩組，其獲利贏過大盤（黃色），而50以下的兩組，其獲利小於大盤。</p>



<h3 id="財報好的公司，抗跌！">財報好的公司，抗跌！</h3>



<p>資優生組別（灰色）幾乎每季都漲！唯一一次跌是在2015年中，當時大盤大跌，但資優生那組並沒有跌非常多。</p>



<h3 id="穩定的報酬率">穩定的報酬率</h3>



<p>大盤三年1.05倍，而高分財報組1.75倍，平均報酬率20％每年。</p>



<h3 id="別用這個策略亂放空！">別用這個策略亂放空！</h3>



<p>從上圖也可以看到一個現象：不要亂放空，不然會慘慘的。就算財報在爛，最差的那組還是沒辦法一直跌，但財報最好的那組可以漲很多！</p>



<h3 id="附註：現在就開始AI選股，免費取得訓練資料和教程">附註：現在就開始AI選股，免費取得訓練資料和教程</h3>



<p>取得訓練資料的方法很簡單，幫冷清的寒舍小小宣傳一下：</p>



<ol><li>分享任一你覺得很有趣的FinLab文章於FB</li><li>將分享截圖傳到&nbsp;<a href="mailto:finlabstaff@gmail.com" rel="noreferrer noopener" target="_blank">finlabstaff@gmail.com</a></li><li>一兩天內即可獲得 data.csv！</li></ol>



<p>依照本blog的含金量、data.csv的完整程度，你一定不會後悔的！</p>



<p>詳細的python教學：<a href="https://www.finlab.tw/%E5%88%A9%E7%94%A8Machine-Learning-%E9%81%B8%E8%82%A1%E6%96%B0%E6%89%8B%E6%95%99%E5%AD%B8/">利用Machine-Learning-選股新手教學</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.finlab.tw/machine-learning-%e8%a1%a8%e7%a4%ba%ef%bc%9a%e7%9c%8b%e5%ad%a3%e7%b7%9a%e6%9c%80%e7%84%a1%e7%94%a8%ef%bc%81/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">462</post-id>	</item>
	</channel>
</rss>
