What is jstat?
The jStat npm package is a JavaScript statistical library that provides a wide range of statistical functions and utilities. It is designed to perform various statistical operations such as probability distributions, descriptive statistics, hypothesis testing, and linear regression.
What are jstat's main functionalities?
Descriptive Statistics
This feature allows you to calculate basic descriptive statistics such as mean, median, and variance for a given dataset.
const jStat = require('jstat');
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const mean = jStat.mean(data);
const median = jStat.median(data);
const variance = jStat.variance(data);
console.log(`Mean: ${mean}, Median: ${median}, Variance: ${variance}`);
Probability Distributions
This feature provides functions to work with various probability distributions, including normal, binomial, and Poisson distributions. You can calculate the probability density function (PDF) and cumulative distribution function (CDF) for these distributions.
const jStat = require('jstat');
const normalDist = jStat.normal(0, 1);
const pdf = normalDist.pdf(0);
const cdf = normalDist.cdf(0);
console.log(`PDF at 0: ${pdf}, CDF at 0: ${cdf}`);
Hypothesis Testing
This feature allows you to perform various hypothesis tests, such as t-tests, to determine if there are significant differences between datasets.
const jStat = require('jstat');
const sample1 = [1, 2, 3, 4, 5];
const sample2 = [2, 3, 4, 5, 6];
const tTest = jStat.ttest(sample1, sample2, 2);
console.log(`T-test result: ${tTest}`);
Linear Regression
This feature provides tools for performing linear regression analysis, allowing you to model the relationship between dependent and independent variables.
const jStat = require('jstat');
const x = [1, 2, 3, 4, 5];
const y = [2, 3, 5, 7, 11];
const slope = jStat.models.ols(y, x).beta[1];
const intercept = jStat.models.ols(y, x).beta[0];
console.log(`Slope: ${slope}, Intercept: ${intercept}`);
Other packages similar to jstat
simple-statistics
The simple-statistics package provides a wide range of statistical functions similar to jStat, including descriptive statistics, probability distributions, and regression analysis. It is known for its simplicity and ease of use.
mathjs
The mathjs package is a comprehensive mathematics library that includes statistical functions among many other mathematical utilities. It offers a broader range of mathematical operations compared to jStat, making it suitable for more general mathematical computations.
mljs
The mljs package is a machine learning library for JavaScript that includes statistical functions as part of its toolkit. It provides more advanced machine learning algorithms and data processing capabilities compared to jStat.
jStat - JavaScript Statistical Library
jStat provides native javascript implementations of statistical functions.
Full details are available in the docs.
jStat provides more functions than most libraries, including the weibull,
cauchy, poisson, hypergeometric, and beta distributions. For most
distributions, jStat provides the pdf, cdf, inverse, mean, mode, variance, and
a sample function, allowing for more complex calculations.
NOTICE: The previous case sensitive jStat
module will no longer be
updated. Instead use the all lowercase jstat
when doing an npm install
or
similar.
Using jStat in a Browser
jStat can be used in the browser. The jStat
object will be added to the
window. For example:
<script src="components/jstat.js"></script> <!-- include jStat, from the CDN or otherwise -->
<script>
...
var jstat = this.jStat(dataset); // jStat will be added to the window
...
data[i]['cum'] = jstat.normal(jstat.mean(), jstat.stdev()).cdf(data[i].x);
...
</script>
CDN
The library is hosted on jsDelivr using the following
url:
//cdn.jsdelivr.net/npm/jstat@latest/dist/jstat.min.js
Note that 'latest'
can be replaced with any released verion of jStat.
Module Loaders
Currently jStat is exposed as j$
and jStat
inside an object, rather than
exported directly. This may confuse some module loaders, however should be
easily remedied with the correct configuration.
NodeJS & NPM
To install via npm:
npm install --save jstat
When loading under Node be sure to reference the child object.
var { jStat } = require('jstat').
RequireJS Shim
For RequireJS not only exports
but also init
function must be specified.
requirejs.config({
paths: {
'jstat': 'path/to/jstat/dist/jstat.min'
},
shim: {
jstat: {
exports: ['j$', 'jStat'],
init: function () {
return {
j$: j$,
jStat: jStat
};
}
}
}
});
Build Prerequisites
In order to build jStat, you need to have GNU make 3.8 or later, Node.js 0.2 or
later, and git 1.7 or later. (Earlier versions might work OK, but are not
tested.)
Windows users have two options:
- Install msysgit (Full installer for official Git),
GNU make for Windows, and a
binary version of Node.js. Make sure all three packages are installed to the same
location (by default, this is C:\Program Files\Git).
- Install Cygwin (make sure you install the git, make, and which packages), then either follow
the Node.js build instructions or install
the binary version of Node.js.
Mac OS users should install Xcode (comes on your Mac OS install DVD, or downloadable from
Apple's Xcode site) and
http://mxcl.github.com/homebrew/. Once Homebrew is installed, run brew install git
to install git,
and brew install node
to install Node.js.
Linux/BSD users should use their appropriate package managers to install make, git, and node, or build from source
if you swing that way.
Building jStat
First, clone a copy of the jStat git repo by running git clone git://github.com/jstat/jstat.git
.
To download all necessary libraries run npm install
.
Then, to get a complete, minified version of jStat and all documentation,
simply cd
to the jstat
directory and type make
. If you don't have Node
installed and/or want to make a basic, uncompressed, unlinted version of jstat,
use make jstat
instead of make
.
The built version of jStat will be put in the dist/
subdirectory.
Generate just the documentation by running make doc
. Documentation will be
placed in dist/docs
by default.
To remove all built files, run make clean
.
Running Tests
Execute all tests by running make test
.
Or if you wish to run a specific test, cd
to test/<subdir>
and run node <some_test>-test.js
.
Get the Code
Both the minified and unminified source are located in the dist/
directory.
For those who don't want to build it themselves.
Contribute
jStat is now going to follow most of the v8
JavaScript
guidelines. There will be plenty of source that uses the old style, but we're
going to work away from that.
Also, we'll be going through and reimplementing a good portion of the code to
run faster. Hopefully it won't take too long to get the project on one basic
standard.
When submitting pull requests, no need to check in dist/*.js
. They'll be
recompiled for distribution anyway.
We always like discussion of how to improve jStat.
Join us at our mailing list
and let us know what you'd like to see. Also come ask questions in the #jstat
channel on irc.freenode.net.