<?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/"
	>

<channel>
	<title>BoxHeadRoom &#187; NumPy</title>
	<atom:link href="http://boxheadroom.com/tag/numpy/feed" rel="self" type="application/rss+xml" />
	<link>http://boxheadroom.com</link>
	<description>蝸牛の一歩</description>
	<lastBuildDate>Tue, 07 Feb 2012 13:41:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NumPyで標準偏差,偏差値</title>
		<link>http://boxheadroom.com/2009/01/27/standard_deviation</link>
		<comments>http://boxheadroom.com/2009/01/27/standard_deviation#comments</comments>
		<pubDate>Mon, 26 Jan 2009 19:54:52 +0000</pubDate>
		<dc:creator>boxheadroom</dc:creator>
				<category><![CDATA[PC]]></category>
		<category><![CDATA[NumPy]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://boxheadroom.com/?p=2388</guid>
		<description><![CDATA[
マーケット番組でよく登場するチャート、ボリンジャーバンド(Bollinger bands)。
これに使われている標準偏差を演算ライブラリNumPyで計算するのはどうやるのか調べてみますた。



株価の値動きが、正規分布　１σ（シグマ）の中にほぼ納まるのを利用して、反発したり下落したりするポイントを予測するのに使われるチャートです。
ボリンジャーバンドの考えを、株価以外の計測データに使えないかなー、ということで、まずは一歩目、python＋NumPyで標準偏差や偏差値の計算する方法を調べてみました。
今回はボリンジャーバンド等を描くところまでは行わず、ごく普通に標準偏差や偏差値を計算するところまでのみ。
こちらのページのデータを使って試してみました
平均と標準偏差（経済指標のかんどころ）
計算結果を照らし合わせてみます。
使用関数はコチラで調べました
Numpy Example List


# -*- coding: utf8 -*-
import numpy
#数学の得点
sugaku=numpy.array([61,74,55,85,68,72,64,80,82,59])
#国語の得点
kokugo=numpy.array([79,81,77,78,83,80,82,78,80,82])

#偏差値を計算する関数は無いようなので作る
def std_score(a):
    return numpy.round_(50+10*(a-numpy.average(a))/numpy.std(a))

#一通り計算、表示
def test(label,a):
    print label
    print u"　得点",a
    print u"　合計",numpy.sum(a)
    print u"　平均(average)",numpy.average(a)
    print u"　分散( variance)",numpy.var(a)
    print u"　標準偏差(standard deviation )",numpy.std(a)
    print u"　偏差値(standard score)\n [...]]]></description>
			<content:encoded><![CDATA[<div id="pukiwiki_content3" class="pukiwiki_content">
<p>マーケット番組でよく登場するチャート、<a href="http://www.google.co.jp/search?ie=utf8&amp;oe=utf8&amp;q=%E3%83%9C%E3%83%AA%E3%83%B3%E3%82%B8%E3%83%A3%E3%83%BC%E3%83%90%E3%83%B3%E3%83%89&amp;lr=lang_ja&amp;hl=ja" title="ググる:ボリンジャーバンド" rel="nofollow">ボリンジャーバンド(Bollinger bands)</a>。<br />
これに使われている標準偏差を演算ライブラリ<a href="http://www.google.co.jp/search?ie=utf8&amp;oe=utf8&amp;q=numpy&amp;lr=lang_ja&amp;hl=ja" title="ググる:numpy" rel="nofollow">NumPy</a>で計算するのはどうやるのか調べてみますた。</p>
</div>
<p><span id="more-2388"></span></p>
<div id="pukiwiki_content4" class="pukiwiki_content">
<p>株価の値動きが、正規分布　１σ（シグマ）の中にほぼ納まるのを利用して、反発したり下落したりするポイントを予測するのに使われるチャートです。</p>
<p>ボリンジャーバンドの考えを、<strong>株価以外の計測データ</strong>に使えないかなー、ということで、まずは一歩目、python＋NumPyで標準偏差や偏差値の計算する方法を調べてみました。</p>
<p>今回はボリンジャーバンド等を描くところまでは行わず、ごく普通に標準偏差や偏差値を計算するところまでのみ。</p>
<p>こちらのページのデータを使って試してみました<br />
<a href="http://www.cap.or.jp/~toukei/kandokoro/html/14/14_2.htm" rel="nofollow">平均と標準偏差（経済指標のかんどころ）</a><br />
計算結果を照らし合わせてみます。</p>
<p>使用関数はコチラで調べました<br />
<a href="http://www.scipy.org/Numpy_Example_List" rel="nofollow">Numpy Example List</a></p>
</div>
<pre class="code" style="font-size:120%;">
# -*- coding: utf8 -*-
import numpy
#数学の得点
sugaku=numpy.array([61,74,55,85,68,72,64,80,82,59])
#国語の得点
kokugo=numpy.array([79,81,77,78,83,80,82,78,80,82])

#偏差値を計算する関数は無いようなので作る
def std_score(a):
    return numpy.round_(50+10*(a-numpy.average(a))/numpy.std(a))

#一通り計算、表示
def test(label,a):
    print label
    print u"　得点",a
    print u"　合計",numpy.sum(a)
    print u"　平均(average)",numpy.average(a)
    print u"　分散( variance)",numpy.var(a)
    print u"　標準偏差(standard deviation )",numpy.std(a)
    print u"　偏差値(standard score)\n    ",std_score(a)

test(u"数学",sugaku)
test(u"国語",kokugo)
</pre>
<p>結果。　一応あってるみたいです</p>
<pre class="code" style="font-size:120%;">
数学
　得点 [61 74 55 85 68 72 64 80 82 59]
　合計 700
　平均(average) 70.0
　分散( variance) 95.6
　標準偏差(standard deviation ) 9.77752524926
　偏差値(standard score)
     [ 41.  54.  35.  65.  48.  52.  44.  60.  62.  39.]
国語
　得点 [79 81 77 78 83 80 82 78 80 82]
　合計 800
　平均(average) 80.0
　分散( variance) 3.6
　標準偏差(standard deviation ) 1.8973665961
　偏差値(standard score)
     [ 45.  55.  34.  39.  66.  50.  61.  39.  50.  61.]
</pre>
<div id="pukiwiki_content5" class="pukiwiki_content">
<h2 id="content_1_0">移動平均</h2>
<p>移動平均線や、ボリンジャーバンドを書こうと思うと、日にちごとに再計算が必要になります。</p>
<p>7日間の移動平均をとるとして</p>
<p>&quot;データ＝[1,2,3,4,5,6,7,8,9, ...] #８個<br />
合計値1＝1+2+3+4+5+6+7 = 28<br />
平均値1 (av)=合計値１ / 7 = 4<br />
　<br />
合計値2=合計値1 &#8211; 1 + 8  = 28-1+8=35</p>
<p># ２～８の合計を全て再計算する必要は無い<br />
　<br />
平均値2=合計値2 / 7 =  35 / 7 =5</p>
<p>と、最古のデータを引いて、最新のデータを足す、という作業を繰り返せば、移動平均の計算量が若干減るのですが（200日移動平均線を描くと、少しは計算量に差が出来るかも）<br />
標準偏差の計算には平均値が使われてるんで、それが毎回変化するとなると、、、、ややこしいことをするよりは、numpyの std関数一発で全部再計算したほうが速そうな気がします。<br />
試してないですけれども。</p>
</div>

	Tags: <a href="http://boxheadroom.com/tag/numpy" title="NumPy" rel="tag">NumPy</a>, <a href="http://boxheadroom.com/tag/python" title="Python" rel="tag">Python</a><br />

	<h4>Related posts</h4>
	<ul class="st-related-posts">
	<li><a href="http://boxheadroom.com/2011/09/16/namedlist2" title="Pythonで名前つきリスト その2 (9月 16, 2011)">Pythonで名前つきリスト その2</a> (0)</li>
	<li><a href="http://boxheadroom.com/2011/02/11/urllib2_range" title="【Py】webページの先頭数バイトだけ取得 (2月 11, 2011)">【Py】webページの先頭数バイトだけ取得</a> (0)</li>
	<li><a href="http://boxheadroom.com/2010/10/29/wget_py" title="wget.py webをまとめて取得　などなど (10月 29, 2010)">wget.py webをまとめて取得　などなど</a> (0)</li>
	<li><a href="http://boxheadroom.com/2010/09/21/py_cui_progress_ba" title="CUIでプログレスバーもどき (9月 21, 2010)">CUIでプログレスバーもどき</a> (0)</li>
	<li><a href="http://boxheadroom.com/2010/09/18/matplotlib_delaunay" title="ドロネー三角形 matplotlibで (9月 18, 2010)">ドロネー三角形 matplotlibで</a> (0)</li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://boxheadroom.com/2009/01/27/standard_deviation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

