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

Random CSS generator with evolution.


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 Guys,
What if you could just write HTML and have the CSS generate itself?
Well, this question have been bugging me for while, so I decided to program it.

Method:
The method in which I did so what the following. I wanted to simulate evolution by having the user selecting which CSSJSON object would have offspring, in an attempt to find the perfect css for the html layout.

I’m think if I applied statistics to see which traces are favorable over all the selected offspring, then the resulting offspring would come out to being more to want the user wants. Also, I discovered that the structure of the html and the choice of tags are fundamental to clean and simple design. CSS can only do so much.

Issues:

  • Works with Chrome, Firefox and Opera
  • Layout needs some fixing up.

Demo:

Random CSS generator with evolution

Feedback:

So what do you think?
Do you have any suggestions for improvements?

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 release for Quine-McCluskeyJS


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

After having too much trouble with simplifying long Boolean expressions by hand, I decided to write a script for it. So with a day or two of coding and testing, Quine-McCluskeyJS was formed.
Quine-McCluskeyJS is a Javascript version of the Quine Mccluskey algorithm for Boolean algebra reduction.

Take a look here for more information.
Link: http://code.google.com/p/quine-mccluskeyjs/

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

Chickenfoot mixed with jQuery.


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

Chickenfoot is a Firefox extension design for website automation.

Chickenfoot is a Firefox extension that puts a programming environment in the browser’s sidebar so you can write scripts to manipulate web pages and automate web browsing. In Chickenfoot, scripts are written in a superset of Javascript that includes special functions specific to web tasks.

Basically, it let’s you take control of Firefox and simulate a user. The API is fill with basic operation, like go(url), append(content), onClick( function, window) and etc. You can get more power and functionality by including other JavaScript libraries and frameworks. That’s when the fun begins.

Since the goal in website automation is a lot of HTML manipulations then, jQuery is the best framework for the job. jQuery can be easily included with a simple fix here.
Here are some examples of Chickenfoot scripts. Once Chickenfoot is installed hit f8, press the “New” tab, then insert one of the following scripts.

Downloading a URL

include( "fileio.js" ); //Enables the write and read commands.
var url = "http://google.com",
content = read( url ),
filename = desktopDir().path + "\\google.html";

write( filename, content );

Clean up craigslist.org

include( "http://bateru.com/js/jquery-1.4.2-mod.js" );
$ = window.$;
var url = "http://houston.craigslist.org/sss/";
go( url, true ); //Refresh the page to the url
sleep(2); // So you can see the different.
var cleanHTML = $(document.body).html().toLowerCase();
cleanHTML = cleanHTML.replace( /[$\*!]{2}|[~\*]/gi, "");
$( document.body ).html( cleanHTML );

Overall, Chickenfoot is a great extension to have. These few examples shown here only scratch the surface of what it’s capable of.

Chickenfoot 1.0.7 link
jQuery link

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