<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://wiki.swarma.org/index.php?action=history&amp;feed=atom&amp;title=%E4%BD%BF%E7%94%A8HMM%E5%BB%BA%E6%A8%A1%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E6%B3%A2%E5%8A%A8</id>
	<title>使用HMM建模股票价格波动 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.swarma.org/index.php?action=history&amp;feed=atom&amp;title=%E4%BD%BF%E7%94%A8HMM%E5%BB%BA%E6%A8%A1%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E6%B3%A2%E5%8A%A8"/>
	<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E4%BD%BF%E7%94%A8HMM%E5%BB%BA%E6%A8%A1%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E6%B3%A2%E5%8A%A8&amp;action=history"/>
	<updated>2026-11-03T00:34:31Z</updated>
	<subtitle>本wiki的该页面的版本历史</subtitle>
	<generator>MediaWiki 1.35.0</generator>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E4%BD%BF%E7%94%A8HMM%E5%BB%BA%E6%A8%A1%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E6%B3%A2%E5%8A%A8&amp;diff=15476&amp;oldid=prev</id>
		<title>2020年10月16日 (五) 09:51 思无涯咿呀咿呀</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E4%BD%BF%E7%94%A8HMM%E5%BB%BA%E6%A8%A1%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E6%B3%A2%E5%8A%A8&amp;diff=15476&amp;oldid=prev"/>
		<updated>2020-10-16T09:51:57Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left diff-editfont-monospace&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2020年10月16日 (五) 09:51的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l87&quot; &gt;第87行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第87行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==HMM模型拟合==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==HMM模型拟合==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[File:stockmarket_HMM_1.png|400px]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[File:stockmarket_HMM_1.png|400px]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[category:旧词条迁移]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>思无涯咿呀咿呀</name></author>
	</entry>
	<entry>
		<id>https://wiki.swarma.org/index.php?title=%E4%BD%BF%E7%94%A8HMM%E5%BB%BA%E6%A8%A1%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E6%B3%A2%E5%8A%A8&amp;diff=15465&amp;oldid=prev</id>
		<title>思无涯咿呀咿呀：创建页面，内容为“==原理==  HMM的原理参考[http://wiki.swarma.net/index.php/HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B 这里]  使用HMM拟合股票市…”</title>
		<link rel="alternate" type="text/html" href="https://wiki.swarma.org/index.php?title=%E4%BD%BF%E7%94%A8HMM%E5%BB%BA%E6%A8%A1%E8%82%A1%E7%A5%A8%E4%BB%B7%E6%A0%BC%E6%B3%A2%E5%8A%A8&amp;diff=15465&amp;oldid=prev"/>
		<updated>2020-10-16T09:19:33Z</updated>

		<summary type="html">&lt;p&gt;创建页面，内容为“==原理==  HMM的原理参考[http://wiki.swarma.net/index.php/HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B 这里]  使用HMM拟合股票市…”&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==原理==&lt;br /&gt;
&lt;br /&gt;
HMM的原理参考[http://wiki.swarma.net/index.php/HMM%E9%9A%90%E6%80%A7%E9%A9%AC%E5%8F%AF%E5%A4%AB%E6%A8%A1%E5%9E%8B 这里]&lt;br /&gt;
&lt;br /&gt;
使用HMM拟合股票市场数据实验参考[http://cs229.stanford.edu/proj2009/ShinLee.pdf 这篇]论文&lt;br /&gt;
&lt;br /&gt;
==准备数据==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    import datetime&lt;br /&gt;
    import numpy as np&lt;br /&gt;
    import matplotlib.pyplot as plt&lt;br /&gt;
    from matplotlib.finance import quotes_historical_yahoo&lt;br /&gt;
    from matplotlib.dates import YearLocator, MonthLocator, DateFormatter&lt;br /&gt;
    from sklearn.hmm import GaussianHMM&lt;br /&gt;
    &lt;br /&gt;
    # parepare data&lt;br /&gt;
    date1 = datetime.date(1995, 1, 1)  # start date&lt;br /&gt;
    date2 = datetime.date(2012, 1, 6)  # end date&lt;br /&gt;
    # get quotes from yahoo finance&lt;br /&gt;
    quotes = quotes_historical_yahoo(&amp;quot;INTC&amp;quot;, date1, date2)&lt;br /&gt;
    #data: &lt;br /&gt;
    #Date	Open	Close High	Low		Volume    &lt;br /&gt;
    # unpack quotes&lt;br /&gt;
    dates = np.array([q[0] for q in quotes], dtype=int)&lt;br /&gt;
    close_v = np.array([q[2] for q in quotes])&lt;br /&gt;
    volume = np.array([q[5] for q in quotes])[1:]&lt;br /&gt;
    # take diff of close value&lt;br /&gt;
    # this makes len(diff) = len(close_t) - 1&lt;br /&gt;
    # therefore, others quantity also need to be shifted&lt;br /&gt;
    diff = close_v[1:] - close_v[:-1]&lt;br /&gt;
    dates = dates[1:]&lt;br /&gt;
    close_v = close_v[1:]&lt;br /&gt;
    # pack diff and volume for training&lt;br /&gt;
    X = np.column_stack([diff, volume])&lt;br /&gt;
    #plt.plot(close_v,volume,&amp;quot;bo&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==拟合模型==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    #fit Gaussian HMM&lt;br /&gt;
    n_components = 5&lt;br /&gt;
    # make an HMM instance and execute fit&lt;br /&gt;
    model = GaussianHMM(n_components, covariance_type=&amp;quot;diag&amp;quot;, n_iter=1000)&lt;br /&gt;
    model.fit([X])&lt;br /&gt;
    # predict the optimal sequence of internal hidden state&lt;br /&gt;
    hidden_states = model.predict(X)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==打印模型训练的结果并绘图==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
    # print trained parameters and plot&lt;br /&gt;
    &lt;br /&gt;
    print(np.round(model.transmat_,2))&lt;br /&gt;
    print(&amp;quot;means and vars of each hidden state&amp;quot;)&lt;br /&gt;
    for i in range(n_components):&lt;br /&gt;
        print(&amp;quot;%dth hidden state&amp;quot; % i)&lt;br /&gt;
        print(&amp;quot;mean = &amp;quot;, model.means_[i])&lt;br /&gt;
        print(&amp;quot;var = &amp;quot;,np.diag(model.covars_[i])   )&lt;br /&gt;
        print()&lt;br /&gt;
           &lt;br /&gt;
    years = YearLocator()   # every year&lt;br /&gt;
    months = MonthLocator()  # every month&lt;br /&gt;
    yearsFmt = DateFormatter('%Y')&lt;br /&gt;
    fig = plt.figure()&lt;br /&gt;
    ax = fig.add_subplot(111)&lt;br /&gt;
    for i in range(n_components):&lt;br /&gt;
        # use fancy indexing to plot data in each state&lt;br /&gt;
        idx = (hidden_states == i)&lt;br /&gt;
        ax.plot_date(dates[idx], close_v[idx], 'o', label=&amp;quot;%dth hidden state&amp;quot; % i)&lt;br /&gt;
    &lt;br /&gt;
    # format the figure&lt;br /&gt;
    ax.legend()&lt;br /&gt;
    ax.xaxis.set_major_locator(years)&lt;br /&gt;
    ax.xaxis.set_major_formatter(yearsFmt)&lt;br /&gt;
    ax.xaxis.set_minor_locator(months)&lt;br /&gt;
    ax.autoscale_view()&lt;br /&gt;
    ax.fmt_xdata = DateFormatter('%Y-%m-%d')&lt;br /&gt;
    ax.fmt_ydata = lambda x: '$%1.2f' % x&lt;br /&gt;
    ax.grid(True)&lt;br /&gt;
    fig.autofmt_xdate()&lt;br /&gt;
    &lt;br /&gt;
    #plt.plot()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==HMM模型拟合==&lt;br /&gt;
[[File:stockmarket_HMM_1.png|400px]]&lt;/div&gt;</summary>
		<author><name>思无涯咿呀咿呀</name></author>
	</entry>
</feed>