JavaScript – PseudoSavant https://pseudosavant.com/blog The Musings of Paul Ellis Wed, 29 Jan 2014 21:55:26 +0000 en-US hourly 1 https://wordpress.org/?v=5.6 4146239 psMathStats 2.0 https://pseudosavant.com/blog/2013/06/14/psmathstats-2-0/ https://pseudosavant.com/blog/2013/06/14/psmathstats-2-0/#respond Fri, 14 Jun 2013 17:58:09 +0000 http://pseudosavant.com/blog/?p=618 javascript-icon.pngI just pushed the latest version of psMathStats to GitHub. It had been sitting on the shelf 95% done for probably a year, but it is out at last. There are some new methods and while the syntax is almost exactly the same, it isn’t a drop-in replacement for 1.0 as it now uses my ps namespace.

New Features

A full breakdown of all the methods and functions of 2.0 are available on GitHub, but these are some of the new features.

  • Array.sample
  • Array.histogram
  • Array.countByType
  • Array.percentile
  • ps.math.even
  • ps.math.odd
  • ps.math.product
  • ps.math.randomBetween
  • ps.math.randomNormal

Array.sample

Probably the biggest new feature is the ability to do sampling for any of the Array methods using Array.sample. It returns a randomly sampled array of any length less than or equal to the source array length. You can use it to quickly do calculations over large datasets (1MM+ rows) very quickly while sacrificing only a small amount of accuracy.

Here is an example:

// This will take a long time (many seconds) to run.
// Makes the browser become unresponsive
tenMillionRowArray.stdDev();

// Takes only a few milliseconds to run, and the
// returned value is almost exactly the same.
tenMillionRowArray.sample(20000).stdDev();

Suggestions

As always, if you have any other useful math or statistics functions you’d like to see implemented just drop me a line at Twitter or GitHub with some details on how to perform the calculation. Even better, just send me a pull-request with your implementation. ;)

]]>
https://pseudosavant.com/blog/2013/06/14/psmathstats-2-0/feed/ 0 618
JavaScript Statistics and Math Library https://pseudosavant.com/blog/2010/12/22/javascript-statistics-and-math-library/ https://pseudosavant.com/blog/2010/12/22/javascript-statistics-and-math-library/#comments Wed, 22 Dec 2010 07:23:46 +0000 http://pseudosavant.com/blog/?p=429 altRecently I started my own Google Code project to share some of the reusable code I have developed. There are lots of good general purpose JavaScript libraries such as jQuery or Closure but sometimes there are things that are out of the scope for these types of libraries. One of those things is basic math and statistics operations.

Update: Version 2.0 is out now. Check it out here.

This deficiency became apparent to me while coding something for work where I wanted to use JavaScript to calculate some basic math and statistics: mean, variance, standard deviation, etc. There weren’t any good libraries or code snippets I could find to do these functions easily.

I ended up throwing something together that got the job done but later decided that I could make it a lot more simple and reusable. Turns out that JavaScript’s prototyping capability is perfect for these types of operations. It turns Math.max.apply(Math, myArray) into myArray.max().

These are the methods I have implemented so far:

  • Array.prototype.sum(): returns sum of all array values
  • Array.prototype.min(): returns the lowest numeric value of an array
  • Array.prototype.max(): returns the highest numeric value of an array
  • Array.prototype.mean(): returns the arithmetic mean of an array
  • Array.prototype.median(): returns the median of an array
  • Array.prototype.sortNumber(boolean decending): returns array sorted ascending, or descending if sortNumber(true)
  • Array.prototype.variance(): returns the variance of an array
  • Array.prototype.stdDev(): returns standard deviation of an array
  • normsinv(p): returns lower tail quantile for standard normal distribution function

These are just some basic Excel-type of functions. I am open to adding more functionality to the library, so if you would like to contribute some code or just have a suggestion for a useful function then please leave comment.

Download the script and examples here. You can also link against the minified version here if you would always like to use the latest version.

]]>
https://pseudosavant.com/blog/2010/12/22/javascript-statistics-and-math-library/feed/ 5 429