Comparing version
{ | ||
"name": "gaussian", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"homepage": "https://github.com/errcw/gaussian", | ||
@@ -5,0 +5,0 @@ "authors": [ |
{ | ||
"name": "gaussian", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"main": "lib/gaussian.js", | ||
@@ -5,0 +5,0 @@ "scripts": [ |
@@ -9,5 +9,23 @@ /** | ||
const _2PI = Math.PI * 2; | ||
function generateGaussian(mean,std){ | ||
var u1 = Math.random(); | ||
var u2 = Math.random(); | ||
/** | ||
* | ||
* @param {number} mean | ||
* @param {number} std | ||
* @param randFn - an optional function that returns a float between 0 (inclusive) and 1 | ||
* (exclusive). Use this if you want to pass in a random number generator other than | ||
* Math.random(). | ||
* @returns {number} | ||
*/ | ||
function generateGaussian(mean,std, randFn = null){ | ||
var u1; | ||
var u2; | ||
if (randFn) { | ||
u1 = randFn(); | ||
u2 = randFn(); | ||
} | ||
else { | ||
u1 = Math.random(); | ||
u2 = Math.random(); | ||
} | ||
@@ -24,3 +42,4 @@ var z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(_2PI * u2); | ||
? function(e) { module.exports = e; } | ||
// istanbul ignore next | ||
: function(e) { this["boxmuller"] = e; }); | ||
@@ -103,8 +103,16 @@ (function(exports) { | ||
// Generate [num] random samples | ||
Gaussian.prototype.random = function(num){ | ||
/** | ||
* Generate [num] random samples | ||
* @param {number} num | ||
* @param randFn - an optional function that returns a float between 0 (inclusive) and 1 | ||
* (exclusive). Use this if you want to pass in a random number generator other than | ||
* Math.random(). | ||
* @returns {number[]} | ||
*/ | ||
Gaussian.prototype.random = function(num, randFn = null){ | ||
let mean = this.mean; | ||
let std = this.standardDeviation; | ||
return Array(num).fill(0).map(() => { | ||
return generateGaussian(mean,std) | ||
return generateGaussian(mean,std, randFn) | ||
}) | ||
@@ -125,2 +133,4 @@ }; | ||
? function(e) { module.exports = e; } | ||
// istanbul ignore next | ||
: function(e) { this["gaussian"] = e; }); | ||
{ | ||
"name": "gaussian", | ||
"description": "A JavaScript model of a Gaussian distribution", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"author": "Eric Woroshow <eric@ericw.ca>", | ||
@@ -11,6 +11,3 @@ "homepage": "https://github.com/errcw/gaussian", | ||
}, | ||
"licenses": { | ||
"type": "MIT", | ||
"url": "http://github.com/errcw/gaussian/blob/master/LICENSE" | ||
}, | ||
"license": "MIT", | ||
"main": "lib/gaussian", | ||
@@ -17,0 +14,0 @@ "scripts": { |
@@ -0,1 +1,6 @@ | ||
[](https://www.npmjs.com/package/gaussian) | ||
[](https://github.com/errcw/gaussian/actions/workflows/tests.yml) | ||
[](https://coveralls.io/github/errcw/gaussian?branch=master) | ||
[](https://www.npmjs.com/package/gaussian) | ||
# gaussian | ||
@@ -9,2 +14,3 @@ | ||
### Creating a Distribution | ||
```javascript | ||
@@ -18,2 +24,3 @@ var gaussian = require('gaussian'); | ||
### Properties | ||
- `mean`: the mean (μ) of the distribution | ||
@@ -24,2 +31,3 @@ - `variance`: the variance (σ^2) of the distribution | ||
### Probability Functions | ||
- `pdf(x)`: the probability density function, which describes the probability | ||
@@ -32,2 +40,3 @@ of a random variable taking on the value _x_ | ||
### Combination Functions | ||
- `mul(d)`: returns the product distribution of this and the given distribution; equivalent to `scale(d)` when d is a constant | ||
@@ -40,2 +49,3 @@ - `div(d)`: returns the quotient distribution of this and the given distribution; equivalent to `scale(1/d)` when d is a constant | ||
### Generation Function | ||
- `random(n)`: returns an array of generated `n` random samples correspoding to the Gaussian parameters. | ||
- `random(n)`: returns an array of generated `n` random samples correspoding to the Gaussian parameters. |
@@ -102,2 +102,8 @@ // Tests based on values from Wolfram Alpha. | ||
it('test custom RNG', () => { | ||
var sillyFn = () => .5; | ||
const outcomes = gaussian(0, 0.3).random(2, sillyFn); | ||
outcomes.forEach((outcome) => expect(outcome).toBeCloseTo(-0.644894028, 8)); | ||
}); | ||
/** | ||
@@ -116,1 +122,24 @@ * Just a naive and simple | ||
}); | ||
/** | ||
* Coverage for gaussian.js:20 | ||
*/ | ||
it('ceils ppf >= 1', () => { | ||
expect.assertions(3); | ||
const normal = gaussian(0, 1); | ||
expect(normal.ppf(1)).toBe(141.4213562373095); | ||
expect(normal.ppf(1.1)).toBe(141.4213562373095); | ||
expect(normal.ppf(100)).toBe(141.4213562373095); | ||
}); | ||
/** | ||
* Coverage for gaussian.js:21 | ||
*/ | ||
it('ceils ppf <= 0', () => { | ||
expect.assertions(4); | ||
const normal = gaussian(0, 1); | ||
expect(normal.ppf(0)).toBe(-141.4213562373095); | ||
expect(normal.ppf(-0)).toBe(-141.4213562373095); | ||
expect(normal.ppf(-0.1)).toBe(-141.4213562373095); | ||
expect(normal.ppf(-1)).toBe(-141.4213562373095); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14270
19.83%10
11.11%304
20.63%47
30.56%0
-100%