Javascript Standard Deviation, Variance, Average functions.


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

Hey everyone,
I just installed WP-Syntax for the WordPress and I want to test it out by posting javascript code for finding Standard Deviation, Variance, Average among numbers.

Without WP-Syntax

var str = 'This is a string.';
console.log( str ); //Boring!

With WP-Syntax

var str = 'hello world. Here I am!';
console.log( str ); //Nice colors!!

Without more delay, here are the Statistics functions.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Programmer: Larry Battle 
// Date: Mar 06, 2011
// Purpose: Calculate standard deviation, variance, and average among an array of numbers.
var isArray = function (obj) {
	return Object.prototype.toString.call(obj) === "[object Array]";
},
getNumWithSetDec = function( num, numOfDec ){
	var pow10s = Math.pow( 10, numOfDec || 0 );
	return ( numOfDec ) ? Math.round( pow10s * num ) / pow10s : num;
},
getAverageFromNumArr = function( numArr, numOfDec ){
	if( !isArray( numArr ) ){ return false;	}
	var i = numArr.length, 
		sum = 0;
	while( i-- ){
		sum += numArr[ i ];
	}
	return getNumWithSetDec( (sum / numArr.length ), numOfDec );
},
getVariance = function( numArr, numOfDec ){
	if( !isArray(numArr) ){ return false; }
	var avg = getAverageFromNumArr( numArr, numOfDec ), 
		i = numArr.length,
		v = 0;
 
	while( i-- ){
		v += Math.pow( (numArr[ i ] - avg), 2 );
	}
	v /= numArr.length;
	return getNumWithSetDec( v, numOfDec );
},
getStandardDeviation = function( numArr, numOfDec ){
	if( !isArray(numArr) ){ return false; }
	var stdDev = Math.sqrt( getVariance( numArr, numOfDec ) );
	return getNumWithSetDec( stdDev, numOfDec );
};

Usage

Testing with numbers from wiki

1
2
3
4
5
6
var arrOfNums = [ 2,4,4,4,5,5,7,9 ],
	precision = 4;
 
getAverageFromNumArr( arrOfNums, precision );	//returns 5
getVariance( arrOfNums, precision );	// returns 4
getStandardDeviation( arrOfNums, precision ); //returns 2

Enjoy! πŸ™‚
Update
For those wanting more statistical functions, use jStat : a JavaScript statistical library. by John Resig.

jStat is a statistical library written in JavaScript that allows you to perform advanced statistical operations without the need of a dedicated statistical language (i.e. MATLAB or R).

Update 2
Please read this for more information “http://easycalculation.com/statistics/learn-standard-deviation.php”

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Find data faster.

Stop wasting time and automate repetitive task. Automation increases your productivity and lessens your work load. Here are some few methods to do so.

Photo by: Nathan Eal Photography
Photo by: Nathan Eal Photography

Launching Programs :

The first improvement you can make is to use the keyboard over the mouse. Operating a mouse requires more concentration and time than hitting a keyboard shortcut and typing in the name of the program.
The easiest way is to use “Launchy”, a open source keystroke launcher. With Launchy, to open up a program like Firefox, you hit “alt” + “space” to bring up Launchy, then you type in “fox” and it auto completes for your and enter.
– Jump Directory ( Make a folder within side start menu for quick access to essential tools)
Shortcuts

Finding Files

Everything, (super fast searching).
Spacemonger or WinDirStat (data storage visualization)
Q-dir or xplorer2 (dual file manager)

Automation

Autoit (Windows OS Task automation)
Chickenfoot (firefox plugin for web automation)

More time saving tips

Regular Expression Tools: (Searching Through Text)
Poweroff ( schedule your computer to power off after events )
In conclusion, there are dozens of programs and other techniques that you can learn to save time. To take this a step forward, I suggest reading The Productive Programmer by Neal Ford.

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

What are Sig Figs? Tutorial

By the end of this tutorial you will understand:

  • What sig figs are.
  • The fastest way to find to the sig figs.
  • How to place your answer in sig figs after arithmetic.

What are sig figs?

Simply put, sig figs is the degree of reliability for a measurement. Calculations done on a measurement are as accurate as the least reliable measurement. You will encounter sig figs in chemistry, when dealing with atomic structures, and molecular. It’s worth noting that Sig Figs are commonly called Significant Figures, or Significant Digits. But I prefer Sig Figs.

The fastest way to finding sig figs.

The traditional way of teaching sig figs contains too many conditions. List of rules here. So, I found a way to simplify the approach into the following statement.

Sig figs are the number of digits from the “first nonzero” to the “last position”.
The “Last Position” is the last number’s position if the measurement has a decimal. Otherwise, the “Last Position” is the last non-zero’s position.
The “First NonZero” is the position of the first non-zero.

Another way of writing this is below.
Number of sig figs = ( “Last Position” – “First NonZero” ) + 1
NOTE: If the measurement is defined, then all the numbers are sig figs.

Now let’s do a few examples.
Note that “method 1” is done by counting. While method 2 is done by using the algorithm.

Question 1: 1200 has how many sig figs?
Answer: 2 sig figs.
Method 1: The first is 1, and the last is 2. There are two digts from digit 1 to digit 2. Therefore 2 sig figs.
Method 2: ( “Last Position” – “First NonZero” ) + 1 ) = (2 – 1) + 1 = 1 + 1 = 2 sig figs

Question 2: 1200.25 has how many sig figs?
Answer: 6 sig figs
Method 1: The first is digit 1 and the last is digit 5 because there is a decimal. There are 6 digits from 1 to 5. Therefore 6 sig figs.
Method 2: ( “Last Position” – “First NonZero” ) + 1 ) = (6 – 1) + 1 = 5 + 1 = 6 sig figs

Question 3: 0.0025 has how many sig figs?
Answer: 2 sig figs
Method 1: The first is digit 2 and the last is digit 5. There are 2 digits from 2-5. Therefore 2 sig figs.
Method 2: ( “Last Position” – “First NonZero” ) + 1 ) = (5 – 4) + 1 = 1 + 1 = 2 sig figs

Question 4: 1.0010 has how many sig figs?
Answer: 5 sig figs.

Operations then sig figs:

When multiplying and dividing measurements, the result must have the same sig figs as the term with the least. However, you should ignore sig figs for unitless numbers.
When adding and subtracting, the result must have the same number of decimal places as the term with the least.

Example:
Question 5: What is the average of 130 in and 122.14 in
Answer: 76 in
Average is sum of the terms divided by 2.
130 in + 122.14 in = 152.14, only worry about sig figs after your calculation are done.
152 in / 2 = 76 in. 2 is unit-less, so you should consider sig figs in this operation.

Question 6: What is the 8,000 ft / 20 ft
Answer: 4 x 103
8,000 ft / 20 ft = 4 x 103. Not 400 because you must use the sig figs from the term with the least.

That concludes this tutorial.
Like always I appreciate feedback.

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Javascript Tip: Convert string to unicode values

The following script will convert a string to the unicode values using javascript.
toUnicode is a extension to String.charCodeAt. The benefit of this function is that it returns the whole string a html unicode format.

Code

1
2
3
4
5
6
7
8
String.prototype.toUnicode = function () {
    var uni = [],
        i = this.length;
    while (i--) {
        uni[i] = this.charCodeAt(i);
    }
    return "&#" + uni.join(';&#') + ";";
};

Here’s another method that I thought of.
This one is using a regexp instead of a while loop and an array to form the unicode text.

1
2
3
4
5
String.prototype.toUnicode = function () {
    return this.replace(/./g, function (char) {
        return "&#" + String.charCodeAt(char) + ";";
    });
};

Usage:
Call toUnicode() on any string object.

1
obj.toUnicode();

Example:

1
β€œ<li/>”.toUnicode() //outputs: &#60;&#108;&#105;&#47;&#62;

However, if possible use document.createTextNode to insert text instead of converting it to unicode. Document.createTextNode is a native DOM method meaning it’s a lot faster, and easier to understand .
More infor here: document.createTextNode

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube