jStat: JavaScript Statistical Library
jStat is a comprehensive JavaScript library providing native implementations of statistical functions, suitable for both browser and Node.js environments. It stands out for offering a wide array of functions for various probability distributions, including Weibull, Cauchy, Poisson, Hypergeometric, and Beta, which are often not found in other similar libraries. For most distributions, jStat includes functions for PDF, CDF, inverse CDF, mean, mode, variance, and sampling. The current stable version is 1.9.6, last published four years ago, indicating a slower release cadence and suggesting potential abandonment. A key point of confusion is its module export structure, exposing `jStat` and `j$` properties within an object rather than as direct exports, which requires specific handling for module loaders like CommonJS and for ESM interoperability. The library also emphasizes browser usage, making the `jStat` object globally available.
Common errors
-
TypeError: require(...) is not a function
cause Attempting to call `require('jstat')` directly as a function in CommonJS.fixThe `jstat` module exports an object. You need to access its `jStat` property: `const { jStat } = require('jstat');` -
TypeError: Cannot read properties of undefined (reading 'normal')
cause `jStat` was not correctly imported or accessed, leading to `jStat` being `undefined` or the wrong object when trying to call a method like `normal()`.fixEnsure `jStat` is correctly destructured from the module import: `const { jStat } = require('jstat');` for CJS, or `import * as jStatModule from 'jstat'; const jStat = jStatModule.jStat;` for ESM. -
npm ERR! 404 Not Found - GET https://registry.npmjs.org/jStat - Not found
cause Using the incorrect, case-sensitive package name `jStat` instead of `jstat` during installation.fixInstall using the correct, all-lowercase package name: `npm install jstat`.
Warnings
- breaking The npm package name changed from the case-sensitive `jStat` to the all-lowercase `jstat`. Installing `jStat` will lead to an outdated or non-existent package.
- gotcha The `jstat` module does not export its main functionality directly. Instead, it exports an object containing `jStat` and `j$` properties. Direct `require('jstat')` or ESM `import jStat from 'jstat'` will result in an object, not the callable function.
- gotcha The library appears to be largely unmaintained. The last release was over four years ago, and there has been no significant activity on its GitHub repository. This suggests potential risks for security patches, bug fixes, or compatibility with newer JavaScript environments.
- gotcha When used in a browser via a script tag, `jStat` pollutes the global `window` object by adding `window.jStat` and `window.j$`. This can lead to naming conflicts in environments with many global scripts.
Install
-
npm install jstat -
yarn add jstat -
pnpm add jstat
Imports
- jStat
const jStat = require('jstat');const { jStat } = require('jstat'); - jStat
import { jStat } from 'jstat';// <script src='path/to/jstat.min.js'></script> const jstat = window.jStat;
- jStat
import jStat from 'jstat';
import * as jStatModule from 'jstat'; const jStat = jStatModule.jStat;
- j$
const { j$ } = require('jstat');
Quickstart
import * as jStatModule from 'jstat';
const jStat = jStatModule.jStat;
// Define a dataset
const dataset = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Calculate mean and standard deviation
const mean = jStat(dataset).mean();
const stdev = jStat(dataset).stdev();
console.log(`Dataset: [${dataset.join(', ')}]`);
console.log(`Mean: ${mean}`);
console.log(`Standard Deviation: ${stdev}`);
// Example of using a distribution function: Normal CDF
const normalDist = jStat.normal(mean, stdev);
const cdfAt7 = normalDist.cdf(7);
console.log(`CDF at x=7 for Normal(${mean.toFixed(2)}, ${stdev.toFixed(2)}): ${cdfAt7.toFixed(4)}`);
// Example of generating a sample
const sample = normalDist.sample(5);
console.log(`Random sample of 5 from Normal(${mean.toFixed(2)}, ${stdev.toFixed(2)}): [${sample.map(s => s.toFixed(2)).join(', ')}]`);