{"id":292,"date":"2011-03-06T13:40:01","date_gmt":"2011-03-06T19:40:01","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=292"},"modified":"2012-11-22T02:50:55","modified_gmt":"2012-11-22T08:50:55","slug":"javascript-standard-deviation-variance-average-functions","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2011\/03\/javascript-standard-deviation-variance-average-functions\/","title":{"rendered":"Javascript Standard Deviation, Variance, Average functions."},"content":{"rendered":"<p>Hey everyone,<br \/>\nI just installed <a href=\"http:\/\/wordpress.org\/extend\/plugins\/wp-syntax\/\">WP-Syntax<\/a> for the WordPress and I want to test it out by posting javascript code for finding Standard Deviation, Variance, Average among numbers.<\/p>\n<p><b>Without WP-Syntax<\/b><br \/>\n<code><br \/>\nvar str = 'This is a string.';<br \/>\nconsole.log( str ); \/\/Boring!<br \/>\n<\/code><\/p>\n<p><b>With WP-Syntax<\/b><\/p>\n<pre lang=\"javascript\">\r\nvar str = 'hello world. Here I am!';\r\nconsole.log( str ); \/\/Nice colors!!\r\n<\/pre>\n<p>Without more delay, here are the Statistics functions.<\/p>\n<h2>Code<\/h2>\n<pre lang=\"javascript\" line=\"1\">\r\n\/\/ Programmer: Larry Battle \r\n\/\/ Date: Mar 06, 2011\r\n\/\/ Purpose: Calculate standard deviation, variance, and average among an array of numbers.\r\nvar isArray = function (obj) {\r\n\treturn Object.prototype.toString.call(obj) === \"[object Array]\";\r\n},\r\ngetNumWithSetDec = function( num, numOfDec ){\r\n\tvar pow10s = Math.pow( 10, numOfDec || 0 );\r\n\treturn ( numOfDec ) ? Math.round( pow10s * num ) \/ pow10s : num;\r\n},\r\ngetAverageFromNumArr = function( numArr, numOfDec ){\r\n\tif( !isArray( numArr ) ){ return false;\t}\r\n\tvar i = numArr.length, \r\n\t\tsum = 0;\r\n\twhile( i-- ){\r\n\t\tsum += numArr[ i ];\r\n\t}\r\n\treturn getNumWithSetDec( (sum \/ numArr.length ), numOfDec );\r\n},\r\ngetVariance = function( numArr, numOfDec ){\r\n\tif( !isArray(numArr) ){ return false; }\r\n\tvar avg = getAverageFromNumArr( numArr, numOfDec ), \r\n\t\ti = numArr.length,\r\n\t\tv = 0;\r\n\t\t\r\n\twhile( i-- ){\r\n\t\tv += Math.pow( (numArr[ i ] - avg), 2 );\r\n\t}\r\n\tv \/= numArr.length;\r\n\treturn getNumWithSetDec( v, numOfDec );\r\n},\r\ngetStandardDeviation = function( numArr, numOfDec ){\r\n\tif( !isArray(numArr) ){ return false; }\r\n\tvar stdDev = Math.sqrt( getVariance( numArr, numOfDec ) );\r\n\treturn getNumWithSetDec( stdDev, numOfDec );\r\n};\r\n<\/pre>\n<h2>Usage<\/h2>\n<p>Testing with numbers from <a href=\"http:\/\/en.wikipedia.org\/wiki\/Standard_deviation\">wiki<\/a><\/p>\n<pre lang=\"javascript\" line=\"1\">\r\nvar arrOfNums = [ 2,4,4,4,5,5,7,9 ],\r\n\tprecision = 4;\r\n\t\r\ngetAverageFromNumArr( arrOfNums, precision );\t\/\/returns 5\r\ngetVariance( arrOfNums, precision );\t\/\/ returns 4\r\ngetStandardDeviation( arrOfNums, precision ); \/\/returns 2\r\n<\/pre>\n<p>Enjoy! \ud83d\ude42<br \/>\n<b>Update<\/b><br \/>\nFor those wanting more statistical functions, use <a href=\"http:\/\/www.jstat.org\/\">jStat : a JavaScript statistical library.<\/a> by <a href=\"http:\/\/ejohn.org\/\" alt=\"John Resig Homepage\">John Resig<\/a>.<\/p>\n<blockquote>\n<p><a href=\"http:\/\/www.jstat.org\/\">jStat<\/a> 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).\n<\/p><\/blockquote>\n<p><b>Update 2<\/b><br \/>\nPlease read this for more information <a href=\"http:\/\/easycalculation.com\/statistics\/learn-standard-deviation.php\">&#8220;http:\/\/easycalculation.com\/statistics\/learn-standard-deviation.php&#8221;<\/a><\/p>\n<p><iframe src=\"http:\/\/rcm.amazon.com\/e\/cm?lt1=_blank&#038;bc1=FFFFFF&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=baterucom-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;ref=tf_til&#038;asins=0062731025\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 = &#8216;This is a string.&#8217;; console.log( str ); \/\/Boring! With WP-Syntax var str = &#8216;hello world. Here I am!&#8217;; console.log( str ); \/\/Nice &hellip; <a href=\"https:\/\/bateru.com\/news\/2011\/03\/javascript-standard-deviation-variance-average-functions\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Javascript Standard Deviation, Variance, Average functions.<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12,10],"tags":[25,164,166,26,24,27,16,22],"class_list":["post-292","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-science","category-tutorials","tag-average","tag-javascript","tag-math","tag-mean","tag-standard-deviation","tag-testing","tag-tutorial","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":19,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"predecessor-version":[{"id":302,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/292\/revisions\/302"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}