<?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>Ad hoc Geek &#187; statistics</title>
	<atom:link href="http://www.adhocgeek.com/tag/statistics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adhocgeek.com</link>
	<description>Approaching geekery in an ad hoc and improvisational manner.</description>
	<lastBuildDate>Fri, 30 Sep 2011 09:43:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>VAR, COVAR and COREL in Actionscript</title>
		<link>http://www.adhocgeek.com/2009/08/var-covar-and-corel-in-actionscript/</link>
		<comments>http://www.adhocgeek.com/2009/08/var-covar-and-corel-in-actionscript/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 11:07:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[covariance]]></category>
		<category><![CDATA[mathematics]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://www.adhocgeek.com/?p=119</guid>
		<description><![CDATA[Someone was asking whether or not it&#8217;s possible to call Excel functions from a Flex project because they needed to use VAR, COVAR and COREL. The short answer is, of course, no (at least not to my knowledge, and, even if it were possible, I don&#8217;t think it&#8217;s something you really want to encourage). A [...]]]></description>
			<content:encoded><![CDATA[<p>Someone was asking whether or not it&#8217;s possible to call Excel functions from a Flex project because they needed to use VAR, COVAR and COREL. The short answer is, of course, no (at least not to my knowledge, and, even if it were possible, I don&#8217;t think it&#8217;s something you really want to encourage). A better answer would explain that these functions aren&#8217;t especially complex and that an Actionscript implementation is fairly straightforward. The wikipedia page on <a href="http://en.wikipedia.org/wiki/Covariance">covariance</a> is a little daunting if you&#8217;ve never really thought about what these functions entail (or have forgotten), but essentially it boils down to this :</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> corel<span style="color: #66cc66;">&#40;</span>X:<span style="color: #0066CC;">Array</span>, Y:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">//correlation coeff between two random variables X and Y is defined as :</span>
    <span style="color: #808080; font-style: italic;">//correlation(X,Y) = covar(X,Y)/(sqrt(Var(X)) * sqrt(Var(Y)))</span>
    <span style="color: #808080; font-style: italic;">//</span>
    <span style="color: #808080; font-style: italic;">//var(X) = covar(X,X);</span>
    <span style="color: #808080; font-style: italic;">//covar(X,Y) = E((X-xm)(Y-ym)); where xm, ym are the population means.</span>
    <span style="color: #b1b100;">return</span> covar<span style="color: #66cc66;">&#40;</span>X, Y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">/</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">sqrt</span><span style="color: #66cc66;">&#40;</span>covar<span style="color: #66cc66;">&#40;</span>X, X<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> covar<span style="color: #66cc66;">&#40;</span>Y, Y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> covar<span style="color: #66cc66;">&#40;</span>X:<span style="color: #0066CC;">Array</span>, Y:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">//Sample covariance is Sum((X-xm)(Y-ym))/n-1</span>
    <span style="color: #808080; font-style: italic;">//where n is the sample size and xm &amp; ym are sample means.</span>
    <span style="color: #808080; font-style: italic;">//I'll assume that X and Y are the same size...</span>
    <span style="color: #000000; font-weight: bold;">var</span> total:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
    <span style="color: #000000; font-weight: bold;">var</span> xm:<span style="color: #0066CC;">Number</span> = average<span style="color: #66cc66;">&#40;</span>X<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">var</span> ym:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span>X == Y<span style="color: #66cc66;">&#41;</span>? xm : average<span style="color: #66cc66;">&#40;</span>Y<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">0</span>; i<span style="color: #66cc66;">&lt;</span>X.<span style="color: #0066CC;">length</span>; i++<span style="color: #66cc66;">&#41;</span>
        total += <span style="color: #66cc66;">&#40;</span>X<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>-xm<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#40;</span>Y<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>-ym<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span> total<span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#40;</span>X.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> average<span style="color: #66cc66;">&#40;</span>X:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">//Sample mean or average = Sum(X)/(sample size)</span>
    <span style="color: #000000; font-weight: bold;">var</span> total:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> x:<span style="color: #0066CC;">Number</span> <span style="color: #b1b100;">in</span> X<span style="color: #66cc66;">&#41;</span>
        total += x;
    <span style="color: #b1b100;">return</span> total<span style="color: #66cc66;">/</span>X.<span style="color: #0066CC;">length</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Of course, it&#8217;s not quite that simple &#8211; my covar won&#8217;t return the same value as COVAR in Excel, because Excel uses the biased estimator (i.e. it divides by n rather than (n-1)), but this cancels out when calculating the correlation coefficient, and correcting covar to the biased estimator is trivial (multply by (n-1)/n). Personally, I think Excel is wrong for defaulting to the biased estimator for COVAR, especially since VAR uses the unbiased one (and, as I&#8217;ve written in the comments, VAR(X)=COVAR(X,X)).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adhocgeek.com/2009/08/var-covar-and-corel-in-actionscript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

