JavaScript Math and Statistics For Fun!

Fredrick Galoso
April 19, 2012

Motivation

Why JavaScript?

Any application that can be written in JavaScript, will eventually be written in JavaScript.

Why yet another math/statistics library?

Why Node.js?

Data retrieval is underrated and overlooked.

Node.js

Hello Gauss!

Installation
npm install gauss

var Vector = require('gauss').Vector;

// List of numbers
var set = new Vector(5, 1, 3, 2, 21, 6, 5);

// From an Array
var numbers = new Vector([8, 6, 7, 5, 3, 0, 9]);

// Convert an Array to a Vector with helper method toVector()
var vanilla = [4, 1, 2, 5, 6];
var chocolate = vanilla.toVector();

// After instantiation, Gauss objects can be conveniently used like any Array
numbers[0] = 2;
set[1] = 7;

// Sum
> set.sum();
49
        

Vector methods


// Standard deviation
> set.stdev();
5.9281411203561225

// Relative frequency distribution
> set.distribution('relative');
{ '2': 0.14285714285714285,
  '3': 0.14285714285714285,
  '5': 0.2857142857142857,
  '6': 0.14285714285714285,
  '7': 0.14285714285714285,
  '21': 0.14285714285714285 }

// Apply Math on Vector and pass to callback
> set.pow(2, function(squares) {
    return Math.sqrt(squares.sum() / squares.length);
});
9.172941575244941
        

Demo: Real-time analysis of tweets

var twitter = require('ntwitter'),
    gauss = require('gauss');

var firehose = new twitter({
  consumer_key: 'your_consumer_key',
  consumer_secret: 'your_consumer_secret',
  access_token_key: 'your_access_token_key',
  access_token_secret: 'your_access_token_secret'
});

var data = new gauss.Vector();

firehose.stream('statuses/sample', function(stream) {
  stream.on('data', function (tweet) {
    // Build an initial sampling of tweet lengths
    if (data.length < 1000 && tweet.text.length <= 140) {
      data.push(tweet.text.length);
      console.log(data.length);
    } else if (data.density(.50).indexOf(tweet.text.length) !== -1) {
      data.push(tweet.text.length);
      console.log(data.distribution());
      console.log({
        "Median tweet length": data.median(),
        "Mean tweet length": data.mean(),
        "Standard deviation": data.stdev(),
        "Total tweets sampled": data.length
      });
    }
  });
});

Thank you!

General math/statistics resources