UNPKG

2.7 kBMarkdownView Raw
1## Overview
2
3### Description
4
5jStat is a statistical library written in JavaScript that allows you to perform advanced statistical operations without the need of a dedicated statistical language (e.g. MATLAB or R). It is available for download on [Github](http://github.com/jstat/jstat).
6
7### Architecture
8
9Calculations are done by *static methods*, while working with groups of numbers is handled by the *instance methods*.
10Here is a pseudo example of what is happening in `core.js`:
11
12 jStat.min = function( arr ) {
13 return Math.min.apply( null, arr );
14 }
15
16 jStat.prototype.min = function() {
17 var i = 0,
18 newval = [];
19 while( newval.push( jStat.min( this[i] )), ++i < this.length );
20 return newval;
21 }
22
23`jStat.min` does the actual calculation on the array, while `jStat.prototype.min` is a wrapper to help work with the jStat object.
24The reason for this approach is to allow for maxium flexibility to other developers who want to extend jStat, while allowing for easy creation of wrappers.
25This way extending jStat requires minimal performance overhead and allows for more unique wrappers to be created.
26
27**Remember: Static methods almost always return native JavaScript types. Instance methods always return a jStat object.**
28
29Here is a simple example on the difference in usage between the static and instance methods:
30
31 var myVect = [2,6,4,7,2,7,4],
32 jObj = jStat( myVect );
33
34 // calculate the sum of the the vector
35 jStat.sum( myVect ) === 32;
36 jObj.sum() === 32;
37
38Now say we want to do several operations on the vector (e.g. sum, min, max, and standard deviation).
39This can be accomplished using the static methods, but each will need to be called separately.
40By using the jStat object we can pass callback functions and chain the execution of each operation:
41
42 jObj.sum( function( val ) {
43 // val === sum
44 }).min( function( val ) {
45 // val === min
46 }).max( function( val ) {
47 // val === max
48 }).stdev( function( val ) {
49 // val === st. dev.
50 });
51
52This method sets each calculation to be executed in an asynchronous queue.
53Very useful method of preventing blocking when working with large data sets.
54
55Let's look at a few chaining and shorthand examples:
56
57 jStat( 0, 1, 11 ) === jStat( jStat.seq( 0, 1, 11 ));
58 jStat().rand( 4, 4 ) === jStat( jStat.rand( 4, 4 ));
59
60 jStat().create( 5, function( x, y ) {
61 return ( x + Math.random()) / ( y + Math.random());
62 }).min( true, function( x ) {
63 // do something with the min value
64 }).beta( 0.5, 0.5 ).pdf(); // generate and return the pdf
65 // of the beta function for all values