-
-
Save binarymax/985971 to your computer and use it in GitHub Desktop.
Statistical variance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************* | |
* | |
* Statistical variance for numeric array 'a' | |
* | |
* Two pass algorithm O(2n), returns a float | |
* | |
*******************************************/ | |
function V( | |
a, //The only parameter is a non-empty numeric array | |
R, //distance of number to average | |
I, //iterator for all items in a | |
A, //average of all numbers in a | |
N, //sum of distances squared | |
C, //sum of distance | |
e //number of items | |
){ | |
for(I=A=N=C=0,e=a.length;I<e;I++) A+=a[I]; //sum of numbers in 'a' | |
A/=e; //average of numbers in 'a' | |
for(;I--;){ //iterate over a again (two pass) | |
R=a[I]-A; //distance from average | |
N+=R*R; //add square of distance to N | |
C+=R //add distance to C | |
} | |
return(N-(C*C)/e)/(e-1) //return variance | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Variance</title> | |
<meta charset="utf-8" /> | |
<script src="index.js" type="text/javascript"></script> | |
<script language="javascript" type="text/javascript"> | |
function page_load() { | |
/*Variance:*/ | |
for(var i=0,l=100,a=[];i<l;i++) a.push(parseInt(Math.random()*1000)); | |
document.body.innerHTML+='<div>Variance = ' + V(a) + '</div><br />[' + a.join(', ') + ']'; | |
} | |
</script> | |
</head> | |
<body onload="page_load();"> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function V(a,R,I,A,N,C,e){for(I=A=N=C=0,e=a.length;I<e;I++)A+=a[I];A/=e;for(;I--;){R=a[I]-A;N+=R*R;C+=R}return(N-(C*C)/e)/(e-1)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2011 Max Lovenheim Irwin, http://binarymax.com | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Variance", | |
"description": "Returns the floating point statistical variance for a non-empty numeric array.", | |
"keywords": [ | |
"variance", | |
"statistics" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
function V(a,R,I,A,N,C,e){for(I=A=N=C=0,e=a.length;I<e;I++)A+=a[I];for(A/=e;I--;){R=a[I]-A;N+=R_R;C+=R}return(N-(C_C)/e)/(e-1)}
...shaves a byte ;)