<?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>Warren Seen &#187; statistics</title>
	<atom:link href="http://warrenseen.com/blog/category/statistics/feed/" rel="self" type="application/rss+xml" />
	<link>http://warrenseen.com/blog</link>
	<description>freelance software developer</description>
	<lastBuildDate>Wed, 03 Jun 2009 23:54:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ruby Cheap Tricks &#8211; Stats</title>
		<link>http://warrenseen.com/blog/2007/02/22/ruby-cheap-tricks-stats/</link>
		<comments>http://warrenseen.com/blog/2007/02/22/ruby-cheap-tricks-stats/#comments</comments>
		<pubDate>Wed, 21 Feb 2007 22:32:01 +0000</pubDate>
		<dc:creator>warren</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://warrenseen.com/blog/2007/02/22/ruby-cheap-tricks-stats/</guid>
		<description><![CDATA[For when it&#8217;s just as quick to roll your own &#8230;


 Ruby [Show Styled Code]:

# In each of these functions, data is just an array of numbers
# average
def avg(data)
  data.inject(0.0) { &#124;sum, i&#124; sum + i } / data.length
end

# median value
def median(data)
  data.sort!
  index = data.length / 2
  (data.length % 2 [...]]]></description>
			<content:encoded><![CDATA[<p>For when it&#8217;s just as quick to roll your own &#8230;<br />
<span id="more-39"></span></p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4c8577392ec7b">
<div class="synthi_header" style="font-weight:bold;"> Ruby <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4c8577392ec7b').style.display='block';document.getElementById('plain_synthi_4c8577392ec7b').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
# In each of these functions, data is just an array of numbers
# average
def avg(data)
  data.inject(0.0) { |sum, i| sum + i } / data.length
end

# median value
def median(data)
  data.sort!
  index = data.length / 2
  (data.length % 2 == 0) ? data[index] : avg(data.values_at(index.floor, index.ceil))
end

# interquartile mean
def iqm(data)
  data.sort!
  first = (data.length/4).truncate
  last = 3 * first
  avg(data[first..last])
end
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4c8577392ec7b">
<div class="synthi_header" style="font-weight:bold;"> Ruby <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4c8577392ec7b').style.display='block';document.getElementById('styled_synthi_4c8577392ec7b').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="ruby" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># In each of these functions, data is just an array of numbers</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># average</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> avg<span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; data.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0.0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |sum, i| sum + i <span style="color:#006600; font-weight:bold;">&#125;</span> / data.<span style="color:#9900CC;">length</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># median value</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> median<span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; data.<span style="color:#9900CC;">sort</span>!</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; index = data.<span style="color:#9900CC;">length</span> / <span style="color:#006666;">2</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>data.<span style="color:#9900CC;">length</span> % <span style="color:#006666;">2</span> == <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? data<span style="color:#006600; font-weight:bold;">&#91;</span>index<span style="color:#006600; font-weight:bold;">&#93;</span> : avg<span style="color:#006600; font-weight:bold;">&#40;</span>data.<span style="color:#9900CC;">values_at</span><span style="color:#006600; font-weight:bold;">&#40;</span>index.<span style="color:#9900CC;">floor</span>, index.<span style="color:#9900CC;">ceil</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;"># interquartile mean</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> iqm<span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; data.<span style="color:#9900CC;">sort</span>!</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; first = <span style="color:#006600; font-weight:bold;">&#40;</span>data.<span style="color:#9900CC;">length</span>/<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">truncate</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; last = <span style="color:#006666;">3</span> * first</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; avg<span style="color:#006600; font-weight:bold;">&#40;</span>data<span style="color:#006600; font-weight:bold;">&#91;</span>first..<span style="color:#9900CC;">last</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
<p><!--1def8278666ed1e705338f87bf9b8486-->
</p>
<p><!--d0890cb21dbba288a190a998ec627c92-->
</p>
<p><!--1def8278666ed1e705338f87bf9b8486--></p>
]]></content:encoded>
			<wfw:commentRss>http://warrenseen.com/blog/2007/02/22/ruby-cheap-tricks-stats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to calculate standard deviation</title>
		<link>http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/</link>
		<comments>http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/#comments</comments>
		<pubDate>Mon, 13 Mar 2006 08:03:41 +0000</pubDate>
		<dc:creator>warren</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/</guid>
		<description><![CDATA[When I started this blog last month, I thought &#8220;Standard Deviation&#8221; was a snappy title. Of course, I also knew about standard deviation as a statistical tool, however I didn&#8217;t expect that this overlap would cause Google search to drive 50+ visitors a month here looking for implementations of the standard deviation formula.
So as a [...]]]></description>
			<content:encoded><![CDATA[<p>When I started this blog last month, I thought &#8220;Standard Deviation&#8221; was a snappy title. Of course, I also knew about standard deviation as a statistical tool, however I didn&#8217;t expect that this overlap would cause Google search to drive 50+ visitors a month here looking for implementations of the <a href="http://en.wikipedia.org/wiki/Standard_Deviation#Definition_and_shortcut_calculation_of_standard_deviation">standard deviation formula</a>.</p>
<p>So as a &#8220;public service&#8221;, here is some code to figure standard deviation in Ruby and Java.</p>
<p>(disclaimer: no warranties as to correctness, particularly to the nth decimal place, don&#8217;t use this to run your home made nuclear reactor or air traffic control system, blah blah, etc, etc <img src='http://warrenseen.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> )</p>
<p><span id="more-29"></span>The algorithm I&#8217;ll be using is &#8220;borrowed&#8221; from wikipedia&#8217;s entry on <a href="http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance">Algorithms to calculate variance</a>. Specifically, I&#8217;ll be using a variant of algorithm II, which is sourced from Knuth, except we&#8217;ll calculate the standard deviation for the population, rather than a sample. </p>
<p>As you should probably know, standard deviation is defined as the square root of the variance. If you didn&#8217;t know this, maybe you should go read about <a href="http://en.wikipedia.org/wiki/Standard_Deviation">standard deviation</a> first.</p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4c8577395ab65">
<div class="synthi_header" style="font-weight:bold;"> Java <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4c8577395ab65').style.display='block';document.getElementById('plain_synthi_4c8577395ab65').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
/**
 * @param population an array, the population
 * @return the variance
 */
public double variance(double[] population) {
	long n = 0;
	double mean = 0;
	double s = 0.0;

	for (double x : population) {
		n++;
		double delta = x - mean;
		mean += delta / n;
		s += delta * (x - mean);
	}
	// if you want to calculate std deviation
	// of a sample change this to (s/(n-1))
	return (s / n);
}

/**
 * @param population an array, the population
 * @return the standard deviation
 */
public double standard_deviation(double[] population) {
	return Math.sqrt(variance(population));
}
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4c8577395ab65">
<div class="synthi_header" style="font-weight:bold;"> Java <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4c8577395ab65').style.display='block';document.getElementById('styled_synthi_4c8577395ab65').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="java" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/**</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * @param population an array, the population</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * @return the variance</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> */</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">double</span> variance<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">double</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> population<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">long</span> n = <span style="color: #cc66cc;">0</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">double</span> mean = <span style="color: #cc66cc;">0</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">double</span> s = <span style="color: #cc66cc;">0.0</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">double</span> x : population<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n++;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">double</span> delta = x &#8211; mean;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mean += delta / n;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s += delta * <span style="color: #66cc66;">&#40;</span>x &#8211; mean<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// if you want to calculate std deviation</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// of a sample change this to (s/(n-1))</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>s / n<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">/**</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * @param population an array, the population</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> * @return the standard deviation</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;"> */</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">double</span> standard_deviation<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">double</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> population<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?q=allinurl%3AMath+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">Math</span></a>.<span style="color: #006600;">sqrt</span><span style="color: #66cc66;">&#40;</span>variance<span style="color: #66cc66;">&#40;</span>population<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
<p>example usage:</p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4c85773961a8b">
<div class="synthi_header" style="font-weight:bold;"> Java <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4c85773961a8b').style.display='block';document.getElementById('plain_synthi_4c85773961a8b').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
double[] arr = { 1, 3, 24, 17, 12, 6, 14};
System.out.printf(&#034;%f&#034;, standard_deviation(arr));
  // prints 7.596992
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4c85773961a8b">
<div class="synthi_header" style="font-weight:bold;"> Java <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4c85773961a8b').style.display='block';document.getElementById('styled_synthi_4c85773961a8b').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="java" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #993333;">double</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> arr = <span style="color: #66cc66;">&#123;</span> <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">24</span>, <span style="color: #cc66cc;">17</span>, <span style="color: #cc66cc;">12</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">14</span><span style="color: #66cc66;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><a href="http://www.google.com/search?q=allinurl%3ASystem+java.sun.com&amp;bntl=1"><span style="color: #aaaadd; font-weight: bold;">System</span></a>.<span style="color: #006600;">out</span>.<span style="color: #006600;">printf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;%f&quot;</span>, standard_deviation<span style="color: #66cc66;">&#40;</span>arr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #808080; font-style: italic;">// prints 7.596992 </span></div>
</li>
</ol>
</div>
</div>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4c85773966839">
<div class="synthi_header" style="font-weight:bold;"> Ruby <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4c85773966839').style.display='block';document.getElementById('plain_synthi_4c85773966839').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
  def variance(population)
    n = 0
    mean = 0.0
    s = 0.0
    population.each { |x|
      n = n + 1
      delta = x - mean
      mean = mean + (delta / n)
      s = s + delta * (x - mean)
    }
    # if you want to calculate std deviation
    # of a sample change this to &#034;s / (n-1)&#034;
    return s / n
  end

  # calculate the standard deviation of a population
  # accepts: an array, the population
  # returns: the standard deviation
  def standard_deviation(population)
    Math.sqrt(variance(population))
  end
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4c85773966839">
<div class="synthi_header" style="font-weight:bold;"> Ruby <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4c85773966839').style.display='block';document.getElementById('styled_synthi_4c85773966839').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="ruby" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#9966CC; font-weight:bold;">def</span> variance<span style="color:#006600; font-weight:bold;">&#40;</span>population<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; n = <span style="color:#006666;">0</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mean = <span style="color:#006666;">0.0</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; s = <span style="color:#006666;">0.0</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; population.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> |x|</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; n = n + <span style="color:#006666;">1</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; delta = x &#8211; mean</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; mean = mean + <span style="color:#006600; font-weight:bold;">&#40;</span>delta / n<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; s = s + delta * <span style="color:#006600; font-weight:bold;">&#40;</span>x &#8211; mean<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># if you want to calculate std deviation</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># of a sample change this to &quot;s / (n-1)&quot;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#0000FF; font-weight:bold;">return</span> s / n</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#008000; font-style:italic;"># calculate the standard deviation of a population</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#008000; font-style:italic;"># accepts: an array, the population</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#008000; font-style:italic;"># returns: the standard deviation</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> standard_deviation<span style="color:#006600; font-weight:bold;">&#40;</span>population<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Math.<span style="color:#9900CC;">sqrt</span><span style="color:#006600; font-weight:bold;">&#40;</span>variance<span style="color:#006600; font-weight:bold;">&#40;</span>population<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#9966CC; font-weight:bold;">end</span> </div>
</li>
</ol>
</div>
</div>
<p>example usage:</p>
<div class="synthi_code" style="display:none;" id ="plain_synthi_4c85773968f3e">
<div class="synthi_header" style="font-weight:bold;"> Ruby <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('styled_synthi_4c85773968f3e').style.display='block';document.getElementById('plain_synthi_4c85773968f3e').style.display='none';return false">Show Styled Code</a>]:</span></div>
<pre style="width:100%;overflow:auto;">
 puts standard_deviation([1, 3, 24, 17, 12, 6, 14])
 # prints 7.59699188589047
</pre>
</div>
<div class="synthi_code" style="display:block;" id ="styled_synthi_4c85773968f3e">
<div class="synthi_header" style="font-weight:bold;"> Ruby <span  class="synthi_button"style="font-weight:lighter;font-size:smaller;">[<a href="#" onClick="javascript:document.getElementById('plain_synthi_4c85773968f3e').style.display='block';document.getElementById('styled_synthi_4c85773968f3e').style.display='none';return false">Show Plain Code</a>]:</span></div>
<div class="ruby" style="font-family: monospace;">
<ol>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#CC0066; font-weight:bold;">puts</span> standard_deviation<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">1</span>, <span style="color:#006666;">3</span>, <span style="color:#006666;">24</span>, <span style="color:#006666;">17</span>, <span style="color:#006666;">12</span>, <span style="color:#006666;">6</span>, <span style="color:#006666;">14</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#008000; font-style:italic;"># prints 7.59699188589047 </span></div>
</li>
</ol>
</div>
</div>
<p>If you found this at all useful, (or have spotted a bug), please leave a comment to that effect&#8230;
</p>
<p><!--d94bd1e207fd7b2827a6a2b9f29b6da5--></p>
]]></content:encoded>
			<wfw:commentRss>http://warrenseen.com/blog/2006/03/13/how-to-calculate-standard-deviation/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
