vega-statistics
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -108,3 +108,67 @@ (function (global, factory) { | ||
function quartiles(array, f) { | ||
// Dot density binning for dot plot construction. | ||
// Based on Leland Wilkinson, Dot Plots, The American Statistician, 1999. | ||
// https://www.cs.uic.edu/~wilkinson/Publications/dotplots.pdf | ||
function dotbin(array, step, smooth, f) { | ||
f = f || (_ => _); | ||
let i = 0, j = 1, | ||
n = array.length, | ||
v = new Float64Array(n), | ||
a = f(array[0]), | ||
b = a, | ||
w = a + step, | ||
x; | ||
for (; j<n; ++j) { | ||
x = f(array[j]); | ||
if (x >= w) { | ||
b = (a + b) / 2; | ||
for (; i<j; ++i) v[i] = b; | ||
w = x + step; | ||
a = x; | ||
} | ||
b = x; | ||
} | ||
b = (a + b) / 2; | ||
for (; i<j; ++i) v[i] = b; | ||
return smooth ? smoothing(v, step + step / 4) : v; | ||
} | ||
// perform smoothing to reduce variance | ||
// swap points between "adjacent" stacks | ||
// Wilkinson defines adjacent as within step/4 units | ||
function smoothing(v, thresh) { | ||
let n = v.length, | ||
a = 0, | ||
b = 1, | ||
c, d; | ||
// get left stack | ||
while (v[a] === v[b]) ++b; | ||
while (b < n) { | ||
// get right stack | ||
c = b + 1; | ||
while (v[b] === v[c]) ++c; | ||
// are stacks adjacent? | ||
// if so, compare sizes and swap as needed | ||
if (v[b] - v[b-1] < thresh) { | ||
d = b + ((a + c - b - b) >> 1); | ||
while (d < b) v[d++] = v[b]; | ||
while (d > b) v[d--] = v[a]; | ||
} | ||
// update left stack indices | ||
a = b; | ||
b = c; | ||
} | ||
return v; | ||
} | ||
function quantiles(array, p, f) { | ||
var values = Float64Array.from(numbers(array, f)); | ||
@@ -116,9 +180,9 @@ | ||
return [ | ||
d3Array.quantile(values, 0.25), | ||
d3Array.quantile(values, 0.50), | ||
d3Array.quantile(values, 0.75) | ||
]; | ||
return p.map(_ => d3Array.quantileSorted(values, _)); | ||
} | ||
function quartiles(array, f) { | ||
return quantiles(array, [0.25, 0.50, 0.75], f); | ||
} | ||
function lcg(seed) { | ||
@@ -182,35 +246,16 @@ // Random numbers using a Linear Congruential Generator with seed value | ||
function gaussian(mean, stdev) { | ||
var mu, | ||
sigma, | ||
next = NaN, | ||
dist = {}; | ||
const SQRT2PI = Math.sqrt(2 * Math.PI); | ||
const SQRT2 = Math.SQRT2; | ||
dist.mean = function(_) { | ||
if (arguments.length) { | ||
mu = _ || 0; | ||
next = NaN; | ||
return dist; | ||
} else { | ||
return mu; | ||
} | ||
}; | ||
let nextSample = NaN; | ||
dist.stdev = function(_) { | ||
if (arguments.length) { | ||
sigma = _ == null ? 1 : _; | ||
next = NaN; | ||
return dist; | ||
} else { | ||
return sigma; | ||
} | ||
}; | ||
function sampleNormal(mean, stdev) { | ||
mean = mean || 0; | ||
stdev = stdev == null ? 1 : stdev; | ||
dist.sample = function() { | ||
var x = 0, y = 0, rds, c; | ||
if (next === next) { | ||
x = next; | ||
next = NaN; | ||
return x; | ||
} | ||
let x = 0, y = 0, rds, c; | ||
if (nextSample === nextSample) { | ||
x = nextSample; | ||
nextSample = NaN; | ||
} else { | ||
do { | ||
@@ -222,60 +267,170 @@ x = exports.random() * 2 - 1; | ||
c = Math.sqrt(-2 * Math.log(rds) / rds); // Box-Muller transform | ||
next = mu + y * c * sigma; | ||
return mu + x * c * sigma; | ||
}; | ||
x *= c; | ||
nextSample = y * c; | ||
} | ||
return mean + x * stdev; | ||
} | ||
dist.pdf = function(x) { | ||
var exp = Math.exp(Math.pow(x-mu, 2) / (-2 * Math.pow(sigma, 2))); | ||
return (1 / (sigma * Math.sqrt(2*Math.PI))) * exp; | ||
}; | ||
function densityNormal(value, mean, stdev) { | ||
stdev = stdev == null ? 1 : stdev; | ||
const z = (value - (mean || 0)) / stdev; | ||
return Math.exp(-0.5 * z * z) / (stdev * SQRT2PI); | ||
} | ||
// Approximation from West (2009) | ||
// Better Approximations to Cumulative Normal Functions | ||
dist.cdf = function(x) { | ||
var cd, | ||
z = (x - mu) / sigma, | ||
Z = Math.abs(z); | ||
if (Z > 37) { | ||
cd = 0; | ||
// Approximation from West (2009) | ||
// Better Approximations to Cumulative Normal Functions | ||
function cumulativeNormal(value, mean, stdev) { | ||
mean = mean || 0; | ||
stdev = stdev == null ? 1 : stdev; | ||
let cd, | ||
z = (value - mean) / stdev, | ||
Z = Math.abs(z); | ||
if (Z > 37) { | ||
cd = 0; | ||
} else { | ||
let sum, exp = Math.exp(-Z * Z / 2); | ||
if (Z < 7.07106781186547) { | ||
sum = 3.52624965998911e-02 * Z + 0.700383064443688; | ||
sum = sum * Z + 6.37396220353165; | ||
sum = sum * Z + 33.912866078383; | ||
sum = sum * Z + 112.079291497871; | ||
sum = sum * Z + 221.213596169931; | ||
sum = sum * Z + 220.206867912376; | ||
cd = exp * sum; | ||
sum = 8.83883476483184e-02 * Z + 1.75566716318264; | ||
sum = sum * Z + 16.064177579207; | ||
sum = sum * Z + 86.7807322029461; | ||
sum = sum * Z + 296.564248779674; | ||
sum = sum * Z + 637.333633378831; | ||
sum = sum * Z + 793.826512519948; | ||
sum = sum * Z + 440.413735824752; | ||
cd = cd / sum; | ||
} else { | ||
var sum, exp = Math.exp(-Z*Z/2); | ||
if (Z < 7.07106781186547) { | ||
sum = 3.52624965998911e-02 * Z + 0.700383064443688; | ||
sum = sum * Z + 6.37396220353165; | ||
sum = sum * Z + 33.912866078383; | ||
sum = sum * Z + 112.079291497871; | ||
sum = sum * Z + 221.213596169931; | ||
sum = sum * Z + 220.206867912376; | ||
cd = exp * sum; | ||
sum = 8.83883476483184e-02 * Z + 1.75566716318264; | ||
sum = sum * Z + 16.064177579207; | ||
sum = sum * Z + 86.7807322029461; | ||
sum = sum * Z + 296.564248779674; | ||
sum = sum * Z + 637.333633378831; | ||
sum = sum * Z + 793.826512519948; | ||
sum = sum * Z + 440.413735824752; | ||
cd = cd / sum; | ||
} else { | ||
sum = Z + 0.65; | ||
sum = Z + 4 / sum; | ||
sum = Z + 3 / sum; | ||
sum = Z + 2 / sum; | ||
sum = Z + 1 / sum; | ||
cd = exp / sum / 2.506628274631; | ||
} | ||
sum = Z + 0.65; | ||
sum = Z + 4 / sum; | ||
sum = Z + 3 / sum; | ||
sum = Z + 2 / sum; | ||
sum = Z + 1 / sum; | ||
cd = exp / sum / 2.506628274631; | ||
} | ||
return z > 0 ? 1 - cd : cd; | ||
}; | ||
} | ||
return z > 0 ? 1 - cd : cd; | ||
} | ||
// Approximation of Probit function using inverse error function. | ||
dist.icdf = function(p) { | ||
if (p <= 0 || p >= 1) return NaN; | ||
var x = 2*p - 1, | ||
v = (8 * (Math.PI - 3)) / (3 * Math.PI * (4-Math.PI)), | ||
a = (2 / (Math.PI*v)) + (Math.log(1 - Math.pow(x,2)) / 2), | ||
b = Math.log(1 - (x*x)) / v, | ||
s = (x > 0 ? 1 : -1) * Math.sqrt(Math.sqrt((a*a) - b) - a); | ||
return mu + sigma * Math.SQRT2 * s; | ||
}; | ||
// Approximation of Probit function using inverse error function. | ||
function quantileNormal(p, mean, stdev) { | ||
if (p < 0 || p > 1) return NaN; | ||
return (mean || 0) + (stdev == null ? 1 : stdev) * SQRT2 * erfinv(2 * p - 1); | ||
} | ||
// Approximate inverse error function. Implementation from "Approximating | ||
// the erfinv function" by Mike Giles, GPU Computing Gems, volume 2, 2010. | ||
// Ported from Apache Commons Math, http://www.apache.org/licenses/LICENSE-2.0 | ||
function erfinv(x) { | ||
// beware that the logarithm argument must be | ||
// commputed as (1.0 - x) * (1.0 + x), | ||
// it must NOT be simplified as 1.0 - x * x as this | ||
// would induce rounding errors near the boundaries +/-1 | ||
let w = - Math.log((1 - x) * (1 + x)), p; | ||
if (w < 6.25) { | ||
w -= 3.125; | ||
p = -3.6444120640178196996e-21; | ||
p = -1.685059138182016589e-19 + p * w; | ||
p = 1.2858480715256400167e-18 + p * w; | ||
p = 1.115787767802518096e-17 + p * w; | ||
p = -1.333171662854620906e-16 + p * w; | ||
p = 2.0972767875968561637e-17 + p * w; | ||
p = 6.6376381343583238325e-15 + p * w; | ||
p = -4.0545662729752068639e-14 + p * w; | ||
p = -8.1519341976054721522e-14 + p * w; | ||
p = 2.6335093153082322977e-12 + p * w; | ||
p = -1.2975133253453532498e-11 + p * w; | ||
p = -5.4154120542946279317e-11 + p * w; | ||
p = 1.051212273321532285e-09 + p * w; | ||
p = -4.1126339803469836976e-09 + p * w; | ||
p = -2.9070369957882005086e-08 + p * w; | ||
p = 4.2347877827932403518e-07 + p * w; | ||
p = -1.3654692000834678645e-06 + p * w; | ||
p = -1.3882523362786468719e-05 + p * w; | ||
p = 0.0001867342080340571352 + p * w; | ||
p = -0.00074070253416626697512 + p * w; | ||
p = -0.0060336708714301490533 + p * w; | ||
p = 0.24015818242558961693 + p * w; | ||
p = 1.6536545626831027356 + p * w; | ||
} else if (w < 16.0) { | ||
w = Math.sqrt(w) - 3.25; | ||
p = 2.2137376921775787049e-09; | ||
p = 9.0756561938885390979e-08 + p * w; | ||
p = -2.7517406297064545428e-07 + p * w; | ||
p = 1.8239629214389227755e-08 + p * w; | ||
p = 1.5027403968909827627e-06 + p * w; | ||
p = -4.013867526981545969e-06 + p * w; | ||
p = 2.9234449089955446044e-06 + p * w; | ||
p = 1.2475304481671778723e-05 + p * w; | ||
p = -4.7318229009055733981e-05 + p * w; | ||
p = 6.8284851459573175448e-05 + p * w; | ||
p = 2.4031110387097893999e-05 + p * w; | ||
p = -0.0003550375203628474796 + p * w; | ||
p = 0.00095328937973738049703 + p * w; | ||
p = -0.0016882755560235047313 + p * w; | ||
p = 0.0024914420961078508066 + p * w; | ||
p = -0.0037512085075692412107 + p * w; | ||
p = 0.005370914553590063617 + p * w; | ||
p = 1.0052589676941592334 + p * w; | ||
p = 3.0838856104922207635 + p * w; | ||
} else if (Number.isFinite(w)) { | ||
w = Math.sqrt(w) - 5.0; | ||
p = -2.7109920616438573243e-11; | ||
p = -2.5556418169965252055e-10 + p * w; | ||
p = 1.5076572693500548083e-09 + p * w; | ||
p = -3.7894654401267369937e-09 + p * w; | ||
p = 7.6157012080783393804e-09 + p * w; | ||
p = -1.4960026627149240478e-08 + p * w; | ||
p = 2.9147953450901080826e-08 + p * w; | ||
p = -6.7711997758452339498e-08 + p * w; | ||
p = 2.2900482228026654717e-07 + p * w; | ||
p = -9.9298272942317002539e-07 + p * w; | ||
p = 4.5260625972231537039e-06 + p * w; | ||
p = -1.9681778105531670567e-05 + p * w; | ||
p = 7.5995277030017761139e-05 + p * w; | ||
p = -0.00021503011930044477347 + p * w; | ||
p = -0.00013871931833623122026 + p * w; | ||
p = 1.0103004648645343977 + p * w; | ||
p = 4.8499064014085844221 + p * w; | ||
} else { | ||
p = Infinity; | ||
} | ||
return p * x; | ||
} | ||
function gaussian(mean, stdev) { | ||
var mu, | ||
sigma, | ||
dist = { | ||
mean: function(_) { | ||
if (arguments.length) { | ||
mu = _ || 0; | ||
return dist; | ||
} else { | ||
return mu; | ||
} | ||
}, | ||
stdev: function(_) { | ||
if (arguments.length) { | ||
sigma = _ == null ? 1 : _; | ||
return dist; | ||
} else { | ||
return sigma; | ||
} | ||
}, | ||
sample: () => sampleNormal(mu, sigma), | ||
pdf: value => densityNormal(value, mu, sigma), | ||
cdf: value => cumulativeNormal(value, mu, sigma), | ||
icdf: p => quantileNormal(p, mu, sigma) | ||
}; | ||
return dist.mean(mean).stdev(stdev); | ||
@@ -341,2 +496,53 @@ } | ||
function sampleLogNormal(mean, stdev) { | ||
mean = mean || 0; | ||
stdev = stdev == null ? 1 : stdev; | ||
return Math.exp(mean + sampleNormal() * stdev); | ||
} | ||
function densityLogNormal(value, mean, stdev) { | ||
if (value <= 0) return 0; | ||
mean = mean || 0; | ||
stdev = stdev == null ? 1 : stdev; | ||
const z = (Math.log(value) - mean) / stdev; | ||
return Math.exp(-0.5 * z * z) / (stdev * SQRT2PI * value); | ||
} | ||
function cumulativeLogNormal(value, mean, stdev) { | ||
return cumulativeNormal(Math.log(value), mean, stdev); | ||
} | ||
function quantileLogNormal(p, mean, stdev) { | ||
return Math.exp(quantileNormal(p, mean, stdev)); | ||
} | ||
function lognormal(mean, stdev) { | ||
var mu, | ||
sigma, | ||
dist = { | ||
mean: function(_) { | ||
if (arguments.length) { | ||
mu = _ || 0; | ||
return dist; | ||
} else { | ||
return mu; | ||
} | ||
}, | ||
stdev: function(_) { | ||
if (arguments.length) { | ||
sigma = _ == null ? 1 : _; | ||
return dist; | ||
} else { | ||
return sigma; | ||
} | ||
}, | ||
sample: () => sampleLogNormal(mu, sigma), | ||
pdf: value => densityLogNormal(value, mu, sigma), | ||
cdf: value => cumulativeLogNormal(value, mu, sigma), | ||
icdf: p => quantileLogNormal(p, mu, sigma) | ||
}; | ||
return dist.mean(mean).stdev(stdev); | ||
} | ||
function mixture(dists, weights) { | ||
@@ -409,3 +615,3 @@ var dist = {}, m = 0, w; | ||
function uniform(min, max) { | ||
function sampleUniform(min, max) { | ||
if (max == null) { | ||
@@ -415,42 +621,58 @@ max = (min == null ? 1 : min); | ||
} | ||
return min + (max - min) * exports.random(); | ||
} | ||
var dist = {}, | ||
a, b, d; | ||
function densityUniform(value, min, max) { | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
} | ||
return (value >= min && value <= max) ? 1 / (max - min) : 0; | ||
} | ||
dist.min = function(_) { | ||
if (arguments.length) { | ||
a = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return a; | ||
} | ||
}; | ||
function cumulativeUniform(value, min, max) { | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
} | ||
return value < min ? 0 : value > max ? 1 : (value - min) / (max - min); | ||
} | ||
dist.max = function(_) { | ||
if (arguments.length) { | ||
b = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return b; | ||
} | ||
}; | ||
function quantileUniform(p, min, max) { | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
} | ||
return (p >= 0 && p <= 1) ? min + p * (max - min) : NaN; | ||
} | ||
dist.sample = function() { | ||
return a + d * exports.random(); | ||
}; | ||
function uniform(min, max) { | ||
var a, b, | ||
dist = { | ||
min: function(_) { | ||
if (arguments.length) { | ||
a = _ || 0; | ||
return dist; | ||
} else { | ||
return a; | ||
} | ||
}, | ||
max: function(_) { | ||
if (arguments.length) { | ||
b = _ == null ? 1 : _; | ||
return dist; | ||
} else { | ||
return b; | ||
} | ||
}, | ||
sample: () => sampleUniform(a, b), | ||
pdf: value => densityUniform(value, a, b), | ||
cdf: value => cumulativeUniform(value, a, b), | ||
icdf: p => quantileUniform(p, a, b) | ||
}; | ||
dist.pdf = function(x) { | ||
return (x >= a && x <= b) ? 1 / d : 0; | ||
}; | ||
dist.cdf = function(x) { | ||
return x < a ? 0 : x > b ? 1 : (x - a) / d; | ||
}; | ||
dist.icdf = function(p) { | ||
return (p >= 0 && p <= 1) ? a + p * d : NaN; | ||
}; | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
} | ||
return dist.min(min).max(max); | ||
@@ -921,2 +1143,13 @@ } | ||
exports.bootstrapCI = bootstrapCI; | ||
exports.cumulativeLogNormal = cumulativeLogNormal; | ||
exports.cumulativeNormal = cumulativeNormal; | ||
exports.cumulativeUniform = cumulativeUniform; | ||
exports.densityLogNormal = densityLogNormal; | ||
exports.densityNormal = densityNormal; | ||
exports.densityUniform = densityUniform; | ||
exports.dotbin = dotbin; | ||
exports.quantileLogNormal = quantileLogNormal; | ||
exports.quantileNormal = quantileNormal; | ||
exports.quantileUniform = quantileUniform; | ||
exports.quantiles = quantiles; | ||
exports.quartiles = quartiles; | ||
@@ -926,2 +1159,3 @@ exports.randomInteger = integer; | ||
exports.randomLCG = lcg; | ||
exports.randomLogNormal = lognormal; | ||
exports.randomMixture = mixture; | ||
@@ -938,2 +1172,5 @@ exports.randomNormal = gaussian; | ||
exports.sampleCurve = sampleCurve; | ||
exports.sampleLogNormal = sampleLogNormal; | ||
exports.sampleNormal = sampleNormal; | ||
exports.sampleUniform = sampleUniform; | ||
exports.setRandom = setRandom; | ||
@@ -940,0 +1177,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("d3-array")):"function"==typeof define&&define.amd?define(["exports","d3-array"],n):n((t=t||self).vega={},t.d3)}(this,(function(t,n){"use strict";function*r(t,n){if(void 0===n)for(let n of t)null!=n&&(n=+n)>=n&&(yield n);else{let r=-1;for(let e of t)null!=(e=n(e,++r,t))&&(e=+e)>=e&&(yield e)}}function e(t,e){var o=Float64Array.from(r(t,e));return o.sort(n.ascending),[n.quantile(o,.25),n.quantile(o,.5),n.quantile(o,.75)]}function o(n,r){var e,o,a=NaN,u={mean:function(t){return arguments.length?(e=t||0,a=NaN,u):e},stdev:function(t){return arguments.length?(o=null==t?1:t,a=NaN,u):o},sample:function(){var n,r,u=0,f=0;if(a==a)return u=a,a=NaN,u;do{n=(u=2*t.random()-1)*u+(f=2*t.random()-1)*f}while(0===n||n>1);return r=Math.sqrt(-2*Math.log(n)/n),a=e+f*r*o,e+u*r*o},pdf:function(t){var n=Math.exp(Math.pow(t-e,2)/(-2*Math.pow(o,2)));return 1/(o*Math.sqrt(2*Math.PI))*n},cdf:function(t){var n,r=(t-e)/o,a=Math.abs(r);if(a>37)n=0;else{var u=Math.exp(-a*a/2);a<7.07106781186547?(n=u*((((((.0352624965998911*a+.700383064443688)*a+6.37396220353165)*a+33.912866078383)*a+112.079291497871)*a+221.213596169931)*a+220.206867912376),n/=((((((.0883883476483184*a+1.75566716318264)*a+16.064177579207)*a+86.7807322029461)*a+296.564248779674)*a+637.333633378831)*a+793.826512519948)*a+440.413735824752):n=u/(a+1/(a+2/(a+3/(a+4/(a+.65)))))/2.506628274631}return r>0?1-n:n},icdf:function(t){if(t<=0||t>=1)return NaN;var n=2*t-1,r=8*(Math.PI-3)/(3*Math.PI*(4-Math.PI)),a=2/(Math.PI*r)+Math.log(1-Math.pow(n,2))/2,u=Math.log(1-n*n)/r,f=(n>0?1:-1)*Math.sqrt(Math.sqrt(a*a-u)-a);return e+o*Math.SQRT2*f}};return u.mean(n).stdev(r)}function a(t){var r=t.length,o=e(t),a=(o[2]-o[0])/1.34;return 1.06*Math.min(Math.sqrt(n.variance(t)),a)*Math.pow(r,-.2)}function u(t,n,r,e){const o=e-t*t,a=Math.abs(o)<1e-24?0:(r-t*n)/o;return[n-a*t,a]}function f(t,n,r,e){t=t.filter(t=>{let e=n(t),o=r(t);return null!=e&&(e=+e)>=e&&null!=o&&(o=+o)>=o}),e&&t.sort((t,r)=>n(t)-n(r));const o=new Float64Array(t.length),a=new Float64Array(t.length);let u=0;for(let e of t)o[u]=n(e),a[u]=r(e),++u;return[o,a]}function i(t,n,r,e){let o,a,u=-1,f=-1;for(let i of t)o=n(i,++u,t),a=r(i,u,t),null!=o&&(o=+o)>=o&&null!=a&&(a=+a)>=a&&e(o,a,++f)}function l(t,n,r,e,o){let a=0,u=0;return i(t,n,r,(t,n)=>{const r=n-o(t),f=n-e;a+=r*r,u+=f*f}),1-a/u}function c(t,n,r){let e=0,o=0,a=0,f=0,c=0;i(t,n,r,(t,n)=>{e+=t,o+=n,a+=t*n,f+=t*t,++c});const s=u(e/c,o/c,a/c,f/c),h=t=>s[0]+s[1]*t;return{coef:s,predict:h,rSquared:l(t,n,r,o/c,h)}}function s(t,n,r){let e=0,o=0,a=0,u=0,f=0,c=0,s=0,h=0;i(t,n,r,(t,n)=>{const r=t*t;e+=t,o+=n,a+=r,u+=r*t,f+=r*r,c+=t*n,s+=r*n,++h});const d=a-e*e/h,p=u-a*e/h,M=f-a*a/h,g=d*M-p*p,m=((s-=a*(o/=h))*d-(c-=e*o)*p)/g,v=(c*M-s*p)/g,w=o-v*(e/h)-m*(a/h),b=t=>m*t*t+v*t+w;return{coef:[w,v,m],predict:b,rSquared:l(t,n,r,o,b)}}t.random=Math.random;const h=2,d=1e-12;function p(t){return(t=1-t*t*t)*t*t}function M(t,n,r){let e=t[n],o=r[0],a=r[1]+1;if(!(a>=t.length))for(;n>o&&t[a]-e<=e-t[o];)r[0]=++o,r[1]=a,++a}const g=.1*Math.PI/180;function m(t,n,r){const e=Math.atan2(r[1]-t[1],r[0]-t[0]),o=Math.atan2(n[1]-t[1],n[0]-t[0]);return Math.abs(e-o)}t.bin=function(t){var n,r,e,o,a,u,f,i,l=t.maxbins||20,c=t.base||10,s=Math.log(c),h=t.divide||[5,2],d=t.extent[0],p=t.extent[1],M=t.span||p-d||Math.abs(d)||1;if(t.step)n=t.step;else if(t.steps){for(a=M/l,u=0,f=t.steps.length;u<f&&t.steps[u]<a;++u);n=t.steps[Math.max(0,u-1)]}else{for(r=Math.ceil(Math.log(l)/s),e=t.minstep||0,n=Math.max(e,Math.pow(c,Math.round(Math.log(M)/s)-r));Math.ceil(M/n)>l;)n*=c;for(u=0,f=h.length;u<f;++u)(a=n/h[u])>=e&&M/a<=l&&(n=a)}return o=(a=Math.log(n))>=0?0:1+~~(-a/s),i=Math.pow(c,-o-1),(t.nice||void 0===t.nice)&&(d=d<(a=Math.floor(d/n+i)*n)?a-n:a,p=Math.ceil(p/n)*n),{start:d,stop:p===d?d+n:p,step:n}},t.bootstrapCI=function(e,o,a,u){if(!e.length)return[void 0,void 0];var f,i,l,c,s=Float64Array.from(r(e,u)),h=s.length,d=o;for(l=0,c=Array(d);l<d;++l){for(f=0,i=0;i<h;++i)f+=s[~~(t.random()*h)];c[l]=f/h}return c.sort(n.ascending),[n.quantile(c,a/2),n.quantile(c,1-a/2)]},t.quartiles=e,t.randomInteger=function(n,r){null==r&&(r=n,n=0);var e,o,a,u={};return u.min=function(t){return arguments.length?(a=o-(e=t||0),u):e},u.max=function(t){return arguments.length?(a=(o=t||0)-e,u):o},u.sample=function(){return e+Math.floor(a*t.random())},u.pdf=function(t){return t===Math.floor(t)&&t>=e&&t<o?1/a:0},u.cdf=function(t){var n=Math.floor(t);return n<e?0:n>=o?1:(n-e+1)/a},u.icdf=function(t){return t>=0&&t<=1?e-1+Math.floor(t*a):NaN},u.min(n).max(r)},t.randomKDE=function(n,r){var e=o(),u={},f=0;return u.data=function(t){return arguments.length?(n=t,f=t?t.length:0,u.bandwidth(r)):n},u.bandwidth=function(t){return arguments.length?(!(r=t)&&n&&(r=a(n)),u):r},u.sample=function(){return n[~~(t.random()*f)]+r*e.sample()},u.pdf=function(t){for(var o=0,a=0;a<f;++a)o+=e.pdf((t-n[a])/r);return o/r/f},u.cdf=function(t){for(var o=0,a=0;a<f;++a)o+=e.cdf((t-n[a])/r);return o/f},u.icdf=function(){throw Error("KDE icdf not supported.")},u.data(n)},t.randomLCG=function(t){return function(){return(t=(1103515245*t+12345)%2147483647)/2147483647}},t.randomMixture=function(n,r){var e,o={},a=0;function u(t){var n,r=[],e=0;for(n=0;n<a;++n)e+=r[n]=null==t[n]?1:+t[n];for(n=0;n<a;++n)r[n]/=e;return r}return o.weights=function(t){return arguments.length?(e=u(r=t||[]),o):r},o.distributions=function(t){return arguments.length?(t?(a=t.length,n=t):(a=0,n=[]),o.weights(r)):n},o.sample=function(){for(var r=t.random(),o=n[a-1],u=e[0],f=0;f<a-1;u+=e[++f])if(r<u){o=n[f];break}return o.sample()},o.pdf=function(t){for(var r=0,o=0;o<a;++o)r+=e[o]*n[o].pdf(t);return r},o.cdf=function(t){for(var r=0,o=0;o<a;++o)r+=e[o]*n[o].cdf(t);return r},o.icdf=function(){throw Error("Mixture icdf not supported.")},o.distributions(n).weights(r)},t.randomNormal=o,t.randomUniform=function(n,r){null==r&&(r=null==n?1:n,n=0);var e,o,a,u={};return u.min=function(t){return arguments.length?(a=o-(e=t||0),u):e},u.max=function(t){return arguments.length?(a=(o=t||0)-e,u):o},u.sample=function(){return e+a*t.random()},u.pdf=function(t){return t>=e&&t<=o?1/a:0},u.cdf=function(t){return t<e?0:t>o?1:(t-e)/a},u.icdf=function(t){return t>=0&&t<=1?e+t*a:NaN},u.min(n).max(r)},t.regressionExp=function(t,n,r){let e=0,o=0,a=0,f=0,c=0,s=0;i(t,n,r,(t,n)=>{const r=Math.log(n),u=t*n;e+=n,a+=u,c+=t*u,o+=n*r,f+=u*r,++s});const h=u(a/e,o/e,f/e,c/e),d=t=>h[0]*Math.exp(h[1]*t);return h[0]=Math.exp(h[0]),{coef:h,predict:d,rSquared:l(t,n,r,e/s,d)}},t.regressionLinear=c,t.regressionLoess=function(t,r,e,o){const[a,i]=f(t,r,e,!0),l=a.length,c=Math.max(2,~~(o*l)),s=new Float64Array(l),g=new Float64Array(l),m=new Float64Array(l).fill(1);for(let t=-1;++t<=h;){const r=[0,c-1];for(let t=0;t<l;++t){const n=a[t],e=r[0],o=r[1],f=n-a[e]>a[o]-n?e:o;let l=0,c=0,h=0,d=0,v=0,w=1/Math.abs(a[f]-n||1);for(let t=e;t<=o;++t){const r=a[t],e=i[t],o=p(Math.abs(n-r)*w)*m[t],u=r*o;l+=o,c+=u,h+=e*o,d+=e*u,v+=r*u}const[b,x]=u(c/l,h/l,d/l,v/l);s[t]=b+x*n,g[t]=Math.abs(i[t]-s[t]),M(a,t+1,r)}if(t===h)break;const e=n.median(g);if(Math.abs(e)<d)break;for(let t,n,r=0;r<l;++r)t=g[r]/(6*e),m[r]=t>=1?d:(n=1-t*t)*n}return function(t,n){const r=t.length,e=[];for(let o,a=0,u=0,f=[];a<r;++a)o=t[a],f[0]===o?f[1]+=(n[a]-f[1])/++u:(u=0,f=[o,n[a]],e.push(f));return e}(a,s)},t.regressionLog=function(t,n,r){let e=0,o=0,a=0,f=0,c=0;i(t,n,r,(t,n)=>{t=Math.log(t),e+=t,o+=n,a+=t*n,f+=t*t,++c});const s=u(e/c,o/c,a/c,f/c),h=t=>s[0]+s[1]*Math.log(t);return{coef:s,predict:h,rSquared:l(t,n,r,o/c,h)}},t.regressionPoly=function(t,n,r,e){if(1===e)return c(t,n,r);if(2===e)return s(t,n,r);const[o,a]=f(t,n,r),u=o.length,i=[],h=[],d=e+1;let p,M,g,m,v,w=0;for(p=0;p<u;++p)w+=a[p];for(p=0;p<d;++p){for(g=0,m=0;g<u;++g)m+=Math.pow(o[g],p)*a[g];for(i.push(m),v=new Float64Array(d),M=0;M<d;++M){for(g=0,m=0;g<u;++g)m+=Math.pow(o[g],p+M);v[M]=m}h.push(v)}h.push(i);const b=function(t){const n=t.length-1,r=[];let e,o,a,u,f;for(e=0;e<n;++e){for(u=e,o=e+1;o<n;++o)Math.abs(t[e][o])>Math.abs(t[e][u])&&(u=o);for(a=e;a<n+1;++a)f=t[a][e],t[a][e]=t[a][u],t[a][u]=f;for(o=e+1;o<n;++o)for(a=n;a>=e;a--)t[a][o]-=t[a][e]*t[e][o]/t[e][e]}for(o=n-1;o>=0;--o){for(f=0,a=o+1;a<n;++a)f+=t[a][o]*r[a];r[o]=(t[n][o]-f)/t[o][o]}return r}(h),x=t=>{let n=0,r=0,e=b.length;for(;r<e;++r)n+=b[r]*Math.pow(t,r);return n};return{coef:b,predict:x,rSquared:l(t,n,r,w/u,x)}},t.regressionPow=function(t,n,r){let e=0,o=0,a=0,f=0,c=0,s=0;i(t,n,r,(t,n)=>{const r=Math.log(t),u=Math.log(n);e+=r,o+=u,a+=r*u,f+=r*r,c+=n,++s});const h=u(e/s,o/s,a/s,f/s),d=t=>h[0]*Math.pow(t,h[1]);return h[0]=Math.exp(h[0]),{coef:h,predict:d,rSquared:l(t,n,r,c/s,d)}},t.regressionQuad=s,t.sampleCurve=function(t,n,r,e){r=r||25,e=Math.max(r,e||200);const o=n=>[n,t(n)],a=n[0],u=n[1],f=u-a,i=f/e,l=[o(a)],c=[];if(r===e){for(let t=1;t<e;++t)l.push(o(a+t/r*f));return l.push(o(u)),l}c.push(o(u));for(let t=r;--t>0;)c.push(o(a+t/r*f));let s=l[0],h=c[c.length-1];for(;h;){const t=o((s[0]+h[0])/2);t[0]-s[0]>=i&&m(s,t,h)>g?c.push(t):(s=h,l.push(h),c.pop()),h=c[c.length-1]}return l},t.setRandom=function(n){t.random=n},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("d3-array")):"function"==typeof define&&define.amd?define(["exports","d3-array"],n):n((t=t||self).vega={},t.d3)}(this,(function(t,n){"use strict";function*r(t,n){if(void 0===n)for(let n of t)null!=n&&(n=+n)>=n&&(yield n);else{let r=-1;for(let e of t)null!=(e=n(e,++r,t))&&(e=+e)>=e&&(yield e)}}function e(t,e,o){var u=Float64Array.from(r(t,o));return u.sort(n.ascending),e.map(t=>n.quantileSorted(u,t))}function o(t,n){return e(t,[.25,.5,.75],n)}t.random=Math.random;const u=Math.sqrt(2*Math.PI),a=Math.SQRT2;let l=NaN;function f(n,r){n=n||0,r=null==r?1:r;let e,o,u=0,a=0;if(l==l)u=l,l=NaN;else{do{e=(u=2*t.random()-1)*u+(a=2*t.random()-1)*a}while(0===e||e>1);u*=o=Math.sqrt(-2*Math.log(e)/e),l=a*o}return n+u*r}function i(t,n,r){const e=(t-(n||0))/(r=null==r?1:r);return Math.exp(-.5*e*e)/(r*u)}function c(t,n,r){let e,o=(t-(n=n||0))/(r=null==r?1:r),u=Math.abs(o);if(u>37)e=0;else{let t,n=Math.exp(-u*u/2);u<7.07106781186547?(e=n*(t=(t=(t=(t=(t=(t=.0352624965998911*u+.700383064443688)*u+6.37396220353165)*u+33.912866078383)*u+112.079291497871)*u+221.213596169931)*u+220.206867912376),e/=t=(t=(t=(t=(t=(t=(t=.0883883476483184*u+1.75566716318264)*u+16.064177579207)*u+86.7807322029461)*u+296.564248779674)*u+637.333633378831)*u+793.826512519948)*u+440.413735824752):e=n/(t=u+1/(t=u+2/(t=u+3/(t=u+4/(t=u+.65)))))/2.506628274631}return o>0?1-e:e}function s(t,n,r){return t<0||t>1?NaN:(n||0)+(null==r?1:r)*a*function(t){let n,r=-Math.log((1-t)*(1+t));r<6.25?n=1.6536545626831027+(n=.24015818242558962+(n=(n=(n=.00018673420803405714+(n=(n=(n=4.2347877827932404e-7+(n=(n=(n=1.0512122733215323e-9+(n=(n=(n=26335093153082323e-28+(n=(n=(n=6637638134358324e-30+(n=20972767875968562e-33+(n=(n=11157877678025181e-33+(n=128584807152564e-32+(n=(n=-364441206401782e-35)*(r-=3.125)-16850591381820166e-35)*r)*r)*r-1333171662854621e-31)*r)*r)*r-4054566272975207e-29)*r-8151934197605472e-29)*r)*r-12975133253453532e-27)*r-5415412054294628e-26)*r)*r-4.112633980346984e-9)*r-2.9070369957882005e-8)*r)*r-13654692000834679e-22)*r-13882523362786469e-21)*r)*r-.000740702534166267)*r-.006033670871430149)*r)*r:r<16?(r=Math.sqrt(r)-3.25,n=3.0838856104922208+(n=1.0052589676941592+(n=.005370914553590064+(n=(n=.002491442096107851+(n=(n=.0009532893797373805+(n=(n=24031110387097894e-21+(n=6828485145957318e-20+(n=(n=12475304481671779e-21+(n=29234449089955446e-22+(n=(n=15027403968909828e-22+(n=1.8239629214389228e-8+(n=(n=9.075656193888539e-8+(n=2.2137376921775787e-9)*r)*r-2.7517406297064545e-7)*r)*r)*r-4013867526981546e-21)*r)*r)*r-47318229009055734e-21)*r)*r)*r-.0003550375203628475)*r)*r-.0016882755560235047)*r)*r-.003751208507569241)*r)*r)*r):Number.isFinite(r)?(r=Math.sqrt(r)-5,n=4.849906401408584+(n=1.0103004648645344+(n=(n=(n=7599527703001776e-20+(n=(n=4526062597223154e-21+(n=(n=2.2900482228026655e-7+(n=(n=2.914795345090108e-8+(n=(n=7.61570120807834e-9+(n=(n=1.5076572693500548e-9+(n=(n=-27109920616438573e-27)*r-2.555641816996525e-10)*r)*r-3.789465440126737e-9)*r)*r-1.496002662714924e-8)*r)*r-6.771199775845234e-8)*r)*r-9.9298272942317e-7)*r)*r-1968177810553167e-20)*r)*r-.00021503011930044477)*r-.00013871931833623122)*r)*r):n=1/0;return n*t}(2*t-1)}function h(t,n){var r,e,o={mean:function(t){return arguments.length?(r=t||0,o):r},stdev:function(t){return arguments.length?(e=null==t?1:t,o):e},sample:()=>f(r,e),pdf:t=>i(t,r,e),cdf:t=>c(t,r,e),icdf:t=>s(t,r,e)};return o.mean(t).stdev(n)}function d(t){var r=t.length,e=o(t),u=(e[2]-e[0])/1.34;return 1.06*Math.min(Math.sqrt(n.variance(t)),u)*Math.pow(r,-.2)}function p(t,n){return t=t||0,n=null==n?1:n,Math.exp(t+f()*n)}function m(t,n,r){if(t<=0)return 0;n=n||0,r=null==r?1:r;const e=(Math.log(t)-n)/r;return Math.exp(-.5*e*e)/(r*u*t)}function M(t,n,r){return c(Math.log(t),n,r)}function g(t,n,r){return Math.exp(s(t,n,r))}function v(n,r){return null==r&&(r=null==n?1:n,n=0),n+(r-n)*t.random()}function b(t,n,r){return null==r&&(r=null==n?1:n,n=0),t>=n&&t<=r?1/(r-n):0}function x(t,n,r){return null==r&&(r=null==n?1:n,n=0),t<n?0:t>r?1:(t-n)/(r-n)}function w(t,n,r){return null==r&&(r=null==n?1:n,n=0),t>=0&&t<=1?n+t*(r-n):NaN}function y(t,n,r,e){const o=e-t*t,u=Math.abs(o)<1e-24?0:(r-t*n)/o;return[n-u*t,u]}function N(t,n,r,e){t=t.filter(t=>{let e=n(t),o=r(t);return null!=e&&(e=+e)>=e&&null!=o&&(o=+o)>=o}),e&&t.sort((t,r)=>n(t)-n(r));const o=new Float64Array(t.length),u=new Float64Array(t.length);let a=0;for(let e of t)o[a]=n(e),u[a]=r(e),++a;return[o,u]}function q(t,n,r,e){let o,u,a=-1,l=-1;for(let f of t)o=n(f,++a,t),u=r(f,a,t),null!=o&&(o=+o)>=o&&null!=u&&(u=+u)>=u&&e(o,u,++l)}function A(t,n,r,e,o){let u=0,a=0;return q(t,n,r,(t,n)=>{const r=n-o(t),l=n-e;u+=r*r,a+=l*l}),1-u/a}function F(t,n,r){let e=0,o=0,u=0,a=0,l=0;q(t,n,r,(t,n)=>{e+=t,o+=n,u+=t*n,a+=t*t,++l});const f=y(e/l,o/l,u/l,a/l),i=t=>f[0]+f[1]*t;return{coef:f,predict:i,rSquared:A(t,n,r,o/l,i)}}function L(t,n,r){let e=0,o=0,u=0,a=0,l=0,f=0,i=0,c=0;q(t,n,r,(t,n)=>{const r=t*t;e+=t,o+=n,u+=r,a+=r*t,l+=r*r,f+=t*n,i+=r*n,++c});const s=u-e*e/c,h=a-u*e/c,d=l-u*u/c,p=s*d-h*h,m=((i-=u*(o/=c))*s-(f-=e*o)*h)/p,M=(f*d-i*h)/p,g=o-M*(e/c)-m*(u/c),v=t=>m*t*t+M*t+g;return{coef:[g,M,m],predict:v,rSquared:A(t,n,r,o,v)}}const S=2,E=1e-12;function P(t){return(t=1-t*t*t)*t*t}function U(t,n,r){let e=t[n],o=r[0],u=r[1]+1;if(!(u>=t.length))for(;n>o&&t[u]-e<=e-t[o];)r[0]=++o,r[1]=u,++u}const I=.1*Math.PI/180;function k(t,n,r){const e=Math.atan2(r[1]-t[1],r[0]-t[0]),o=Math.atan2(n[1]-t[1],n[0]-t[0]);return Math.abs(e-o)}t.bin=function(t){var n,r,e,o,u,a,l,f,i=t.maxbins||20,c=t.base||10,s=Math.log(c),h=t.divide||[5,2],d=t.extent[0],p=t.extent[1],m=t.span||p-d||Math.abs(d)||1;if(t.step)n=t.step;else if(t.steps){for(u=m/i,a=0,l=t.steps.length;a<l&&t.steps[a]<u;++a);n=t.steps[Math.max(0,a-1)]}else{for(r=Math.ceil(Math.log(i)/s),e=t.minstep||0,n=Math.max(e,Math.pow(c,Math.round(Math.log(m)/s)-r));Math.ceil(m/n)>i;)n*=c;for(a=0,l=h.length;a<l;++a)(u=n/h[a])>=e&&m/u<=i&&(n=u)}return o=(u=Math.log(n))>=0?0:1+~~(-u/s),f=Math.pow(c,-o-1),(t.nice||void 0===t.nice)&&(d=d<(u=Math.floor(d/n+f)*n)?u-n:u,p=Math.ceil(p/n)*n),{start:d,stop:p===d?d+n:p,step:n}},t.bootstrapCI=function(e,o,u,a){if(!e.length)return[void 0,void 0];var l,f,i,c,s=Float64Array.from(r(e,a)),h=s.length,d=o;for(i=0,c=Array(d);i<d;++i){for(l=0,f=0;f<h;++f)l+=s[~~(t.random()*h)];c[i]=l/h}return c.sort(n.ascending),[n.quantile(c,u/2),n.quantile(c,1-u/2)]},t.cumulativeLogNormal=M,t.cumulativeNormal=c,t.cumulativeUniform=x,t.densityLogNormal=m,t.densityNormal=i,t.densityUniform=b,t.dotbin=function(t,n,r,e){e=e||(t=>t);let o,u=0,a=1,l=t.length,f=new Float64Array(l),i=e(t[0]),c=i,s=i+n;for(;a<l;++a){if((o=e(t[a]))>=s){for(c=(i+c)/2;u<a;++u)f[u]=c;s=o+n,i=o}c=o}for(c=(i+c)/2;u<a;++u)f[u]=c;return r?function(t,n){let r,e,o=t.length,u=0,a=1;for(;t[u]===t[a];)++a;for(;a<o;){for(r=a+1;t[a]===t[r];)++r;if(t[a]-t[a-1]<n){for(e=a+(u+r-a-a>>1);e<a;)t[e++]=t[a];for(;e>a;)t[e--]=t[u]}u=a,a=r}return t}(f,n+n/4):f},t.quantileLogNormal=g,t.quantileNormal=s,t.quantileUniform=w,t.quantiles=e,t.quartiles=o,t.randomInteger=function(n,r){null==r&&(r=n,n=0);var e,o,u,a={};return a.min=function(t){return arguments.length?(u=o-(e=t||0),a):e},a.max=function(t){return arguments.length?(u=(o=t||0)-e,a):o},a.sample=function(){return e+Math.floor(u*t.random())},a.pdf=function(t){return t===Math.floor(t)&&t>=e&&t<o?1/u:0},a.cdf=function(t){var n=Math.floor(t);return n<e?0:n>=o?1:(n-e+1)/u},a.icdf=function(t){return t>=0&&t<=1?e-1+Math.floor(t*u):NaN},a.min(n).max(r)},t.randomKDE=function(n,r){var e=h(),o={},u=0;return o.data=function(t){return arguments.length?(n=t,u=t?t.length:0,o.bandwidth(r)):n},o.bandwidth=function(t){return arguments.length?(!(r=t)&&n&&(r=d(n)),o):r},o.sample=function(){return n[~~(t.random()*u)]+r*e.sample()},o.pdf=function(t){for(var o=0,a=0;a<u;++a)o+=e.pdf((t-n[a])/r);return o/r/u},o.cdf=function(t){for(var o=0,a=0;a<u;++a)o+=e.cdf((t-n[a])/r);return o/u},o.icdf=function(){throw Error("KDE icdf not supported.")},o.data(n)},t.randomLCG=function(t){return function(){return(t=(1103515245*t+12345)%2147483647)/2147483647}},t.randomLogNormal=function(t,n){var r,e,o={mean:function(t){return arguments.length?(r=t||0,o):r},stdev:function(t){return arguments.length?(e=null==t?1:t,o):e},sample:()=>p(r,e),pdf:t=>m(t,r,e),cdf:t=>M(t,r,e),icdf:t=>g(t,r,e)};return o.mean(t).stdev(n)},t.randomMixture=function(n,r){var e,o={},u=0;function a(t){var n,r=[],e=0;for(n=0;n<u;++n)e+=r[n]=null==t[n]?1:+t[n];for(n=0;n<u;++n)r[n]/=e;return r}return o.weights=function(t){return arguments.length?(e=a(r=t||[]),o):r},o.distributions=function(t){return arguments.length?(t?(u=t.length,n=t):(u=0,n=[]),o.weights(r)):n},o.sample=function(){for(var r=t.random(),o=n[u-1],a=e[0],l=0;l<u-1;a+=e[++l])if(r<a){o=n[l];break}return o.sample()},o.pdf=function(t){for(var r=0,o=0;o<u;++o)r+=e[o]*n[o].pdf(t);return r},o.cdf=function(t){for(var r=0,o=0;o<u;++o)r+=e[o]*n[o].cdf(t);return r},o.icdf=function(){throw Error("Mixture icdf not supported.")},o.distributions(n).weights(r)},t.randomNormal=h,t.randomUniform=function(t,n){var r,e,o={min:function(t){return arguments.length?(r=t||0,o):r},max:function(t){return arguments.length?(e=null==t?1:t,o):e},sample:()=>v(r,e),pdf:t=>b(t,r,e),cdf:t=>x(t,r,e),icdf:t=>w(t,r,e)};return null==n&&(n=null==t?1:t,t=0),o.min(t).max(n)},t.regressionExp=function(t,n,r){let e=0,o=0,u=0,a=0,l=0,f=0;q(t,n,r,(t,n)=>{const r=Math.log(n),i=t*n;e+=n,u+=i,l+=t*i,o+=n*r,a+=i*r,++f});const i=y(u/e,o/e,a/e,l/e),c=t=>i[0]*Math.exp(i[1]*t);return i[0]=Math.exp(i[0]),{coef:i,predict:c,rSquared:A(t,n,r,e/f,c)}},t.regressionLinear=F,t.regressionLoess=function(t,r,e,o){const[u,a]=N(t,r,e,!0),l=u.length,f=Math.max(2,~~(o*l)),i=new Float64Array(l),c=new Float64Array(l),s=new Float64Array(l).fill(1);for(let t=-1;++t<=S;){const r=[0,f-1];for(let t=0;t<l;++t){const n=u[t],e=r[0],o=r[1],l=n-u[e]>u[o]-n?e:o;let f=0,h=0,d=0,p=0,m=0,M=1/Math.abs(u[l]-n||1);for(let t=e;t<=o;++t){const r=u[t],e=a[t],o=P(Math.abs(n-r)*M)*s[t],l=r*o;f+=o,h+=l,d+=e*o,p+=e*l,m+=r*l}const[g,v]=y(h/f,d/f,p/f,m/f);i[t]=g+v*n,c[t]=Math.abs(a[t]-i[t]),U(u,t+1,r)}if(t===S)break;const e=n.median(c);if(Math.abs(e)<E)break;for(let t,n,r=0;r<l;++r)t=c[r]/(6*e),s[r]=t>=1?E:(n=1-t*t)*n}return function(t,n){const r=t.length,e=[];for(let o,u=0,a=0,l=[];u<r;++u)o=t[u],l[0]===o?l[1]+=(n[u]-l[1])/++a:(a=0,l=[o,n[u]],e.push(l));return e}(u,i)},t.regressionLog=function(t,n,r){let e=0,o=0,u=0,a=0,l=0;q(t,n,r,(t,n)=>{t=Math.log(t),e+=t,o+=n,u+=t*n,a+=t*t,++l});const f=y(e/l,o/l,u/l,a/l),i=t=>f[0]+f[1]*Math.log(t);return{coef:f,predict:i,rSquared:A(t,n,r,o/l,i)}},t.regressionPoly=function(t,n,r,e){if(1===e)return F(t,n,r);if(2===e)return L(t,n,r);const[o,u]=N(t,n,r),a=o.length,l=[],f=[],i=e+1;let c,s,h,d,p,m=0;for(c=0;c<a;++c)m+=u[c];for(c=0;c<i;++c){for(h=0,d=0;h<a;++h)d+=Math.pow(o[h],c)*u[h];for(l.push(d),p=new Float64Array(i),s=0;s<i;++s){for(h=0,d=0;h<a;++h)d+=Math.pow(o[h],c+s);p[s]=d}f.push(p)}f.push(l);const M=function(t){const n=t.length-1,r=[];let e,o,u,a,l;for(e=0;e<n;++e){for(a=e,o=e+1;o<n;++o)Math.abs(t[e][o])>Math.abs(t[e][a])&&(a=o);for(u=e;u<n+1;++u)l=t[u][e],t[u][e]=t[u][a],t[u][a]=l;for(o=e+1;o<n;++o)for(u=n;u>=e;u--)t[u][o]-=t[u][e]*t[e][o]/t[e][e]}for(o=n-1;o>=0;--o){for(l=0,u=o+1;u<n;++u)l+=t[u][o]*r[u];r[o]=(t[n][o]-l)/t[o][o]}return r}(f),g=t=>{let n=0,r=0,e=M.length;for(;r<e;++r)n+=M[r]*Math.pow(t,r);return n};return{coef:M,predict:g,rSquared:A(t,n,r,m/a,g)}},t.regressionPow=function(t,n,r){let e=0,o=0,u=0,a=0,l=0,f=0;q(t,n,r,(t,n)=>{const r=Math.log(t),i=Math.log(n);e+=r,o+=i,u+=r*i,a+=r*r,l+=n,++f});const i=y(e/f,o/f,u/f,a/f),c=t=>i[0]*Math.pow(t,i[1]);return i[0]=Math.exp(i[0]),{coef:i,predict:c,rSquared:A(t,n,r,l/f,c)}},t.regressionQuad=L,t.sampleCurve=function(t,n,r,e){r=r||25,e=Math.max(r,e||200);const o=n=>[n,t(n)],u=n[0],a=n[1],l=a-u,f=l/e,i=[o(u)],c=[];if(r===e){for(let t=1;t<e;++t)i.push(o(u+t/r*l));return i.push(o(a)),i}c.push(o(a));for(let t=r;--t>0;)c.push(o(u+t/r*l));let s=i[0],h=c[c.length-1];for(;h;){const t=o((s[0]+h[0])/2);t[0]-s[0]>=f&&k(s,t,h)>I?c.push(t):(s=h,i.push(h),c.pop()),h=c[c.length-1]}return i},t.sampleLogNormal=p,t.sampleNormal=f,t.sampleUniform=v,t.setRandom=function(n){t.random=n},Object.defineProperty(t,"__esModule",{value:!0})})); |
25
index.js
export {default as bin} from './src/bin'; | ||
export {default as bootstrapCI} from './src/bootstrapCI'; | ||
export {default as dotbin} from './src/dotbin'; | ||
export {default as quantiles} from './src/quantiles'; | ||
export {default as quartiles} from './src/quartiles'; | ||
@@ -8,5 +10,24 @@ export {random, setRandom} from './src/random'; | ||
export {default as randomKDE} from './src/kde'; | ||
export { | ||
default as randomLogNormal, | ||
sampleLogNormal, | ||
densityLogNormal, | ||
cumulativeLogNormal, | ||
quantileLogNormal | ||
} from './src/lognormal'; | ||
export {default as randomMixture} from './src/mixture'; | ||
export {default as randomNormal} from './src/normal'; | ||
export {default as randomUniform} from './src/uniform'; | ||
export { | ||
default as randomNormal, | ||
sampleNormal, | ||
densityNormal, | ||
cumulativeNormal, | ||
quantileNormal | ||
} from './src/normal'; | ||
export { | ||
default as randomUniform, | ||
sampleUniform, | ||
densityUniform, | ||
cumulativeUniform, | ||
quantileUniform | ||
} from './src/uniform'; | ||
export {default as regressionLinear} from './src/regression/linear'; | ||
@@ -13,0 +34,0 @@ export {default as regressionLog} from './src/regression/log'; |
{ | ||
"name": "vega-statistics", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Statistical routines and probability distributions.", | ||
@@ -27,5 +27,5 @@ "keywords": [ | ||
"dependencies": { | ||
"d3-array": "^2.0.3" | ||
"d3-array": "^2.3.1" | ||
}, | ||
"gitHead": "9badf6d2d1490057f4010e3796189ca366878101" | ||
"gitHead": "016e41e94f617acc1fe6d5eb3242b2646b5780c6" | ||
} |
103
README.md
@@ -8,3 +8,4 @@ # vega-statistics | ||
- [Random Number Generation](#random-number-generation) | ||
- [Distributions](#distributions) | ||
- [Distribution Methods](#distribution-methods) | ||
- [Distribution Objects](#distribution-objects) | ||
- [Regression](#regression) | ||
@@ -33,6 +34,82 @@ - [Statistics](#statistics) | ||
### Distributions | ||
### Distribution Methods | ||
Methods for sampling and calculating probability distributions. Each method takes a set of distributional parameters and returns a distribution object representing a random variable. | ||
Methods for sampling and calculating values for probability distributions. | ||
<a name="sampleNormal" href="#sampleNormal">#</a> | ||
vega.<b>sampleNormal</b>([<i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/normal.js "Source") | ||
Returns a sample from a univariate [normal (Gaussian) probability distribution](https://en.wikipedia.org/wiki/Normal_distribution) with specified *mean* and standard deviation *stdev*. If unspecified, the mean defaults to `0` and the standard deviation defaults to `1`. | ||
<a name="cumulativeNormal" href="#cumulativeNormal">#</a> | ||
vega.<b>cumulativeNormal</b>(value[, <i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/normal.js "Source") | ||
Returns the value of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) at the given input domain *value* for a normal distribution with specified *mean* and standard deviation *stdev*. If unspecified, the mean defaults to `0` and the standard deviation defaults to `1`. | ||
<a name="densityNormal" href="#densityNormal">#</a> | ||
vega.<b>densityNormal</b>(value[, <i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/normal.js "Source") | ||
Returns the value of the [probability density function](https://en.wikipedia.org/wiki/Probability_density_function) at the given input domain *value*, for a normal distribution with specified *mean* and standard deviation *stdev*. If unspecified, the mean defaults to `0` and the standard deviation defaults to `1`. | ||
<a name="quantileNormal" href="#quantileNormal">#</a> | ||
vega.<b>quantileNormal</b>(probability[, <i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/normal.js "Source") | ||
Returns the quantile value (the inverse of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function)) for the given input *probability*, for a normal distribution with specified *mean* and standard deviation *stdev*. If unspecified, the mean defaults to `0` and the standard deviation defaults to `1`. | ||
<a name="sampleLogNormal" href="#sampleLogNormal">#</a> | ||
vega.<b>sampleLogNormal</b>([<i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source") | ||
Returns a sample from a univariate [log-normal probability distribution](https://en.wikipedia.org/wiki/Log-normal_distribution) with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`. | ||
<a name="cumulativeLogNormal" href="#cumulativeNormal">#</a> | ||
vega.<b>cumulativeLogNormal</b>(value[, <i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source") | ||
Returns the value of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) at the given input domain *value* for a log-normal distribution with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`. | ||
<a name="densityLogNormal" href="#densityLogNormal">#</a> | ||
vega.<b>densityLogNormal</b>(value[, <i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source") | ||
Returns the value of the [probability density function](https://en.wikipedia.org/wiki/Probability_density_function) at the given input domain *value*, for a log-normal distribution with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`. | ||
<a name="quantileLogNormal" href="#quantileLogNormal">#</a> | ||
vega.<b>quantileLogNormal</b>(probability[, <i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source") | ||
Returns the quantile value (the inverse of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function)) for the given input *probability*, for a log-normal distribution with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`. | ||
<a name="sampleUniform" href="#sampleUniform">#</a> | ||
vega.<b>sampleUniform</b>([<i>min</i>, <i>max</i>]) | ||
[<>](https://github.com/vega/vega-statistics/blob/master/src/uniform.js "Source") | ||
Returns a sample from a univariate [continuous uniform probability distribution](https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)) over the interval [*min*, *max*). If unspecified, *min* defaults to `0` and *max* defaults to `1`. If only one argument is provided, it is interpreted as the *max* value. | ||
<a name="cumulativeUniform" href="#cumulativeUniform">#</a> | ||
vega.<b>cumulativeUniform</b>(value[, <i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/uniform.js "Source") | ||
Returns the value of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function) at the given input domain *value* for a uniform distribution over the interval [*min*, *max*). If unspecified, *min* defaults to `0` and *max* defaults to `1`. If only one argument is provided, it is interpreted as the *max* value. | ||
<a name="densityUniform" href="#densityUniform">#</a> | ||
vega.<b>densityUniform</b>(value[, <i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/uniform.js "Source") | ||
Returns the value of the [probability density function](https://en.wikipedia.org/wiki/Probability_density_function) at the given input domain *value*, for a uniform distribution over the interval [*min*, *max*). If unspecified, *min* defaults to `0` and *max* defaults to `1`. If only one argument is provided, it is interpreted as the *max* value. | ||
<a name="quantileUniform" href="#quantileUniform">#</a> | ||
vega.<b>quantileUniform</b>(probability[, <i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/uniform.js "Source") | ||
Returns the quantile value (the inverse of the [cumulative distribution function](https://en.wikipedia.org/wiki/Cumulative_distribution_function)) for the given input *probability*, for a uniform distribution over the interval [*min*, *max*). If unspecified, *min* defaults to `0` and *max* defaults to `1`. If only one argument is provided, it is interpreted as the *max* value. | ||
### Distribution Objects | ||
Objects representing probability distributions, with methods for sampling and calculating values. Each method takes a set of distributional parameters and returns a distribution object representing a random variable. | ||
Distribution objects expose the following methods: | ||
@@ -53,2 +130,10 @@ | ||
<a name="randomLogNormal" href="#randomLogNormal">#</a> | ||
vega.<b>randomLogNormal</b>([<i>mean</i>, <i>stdev</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/lognormal.js "Source") | ||
Creates a distribution object representing a [log-normal probability distribution](https://en.wikipedia.org/wiki/Log-normal_distribution) with specified log *mean* and log standard deviation *stdev*. If unspecified, the log mean defaults to `0` and the log standard deviation defaults to `1`. | ||
Once created, *mean* and *stdev* values can be accessed or modified using the `mean` and `stdev` getter/setter methods. | ||
<a name="randomUniform" href="#randomUniform">#</a> | ||
@@ -198,2 +283,14 @@ vega.<b>randomUniform</b>([<i>min</i>, <i>max</i>]) | ||
<a name="dotbin" href="#dotbin">#</a> | ||
vega.<b>dotbin</b>(<i>sortedArray</i>, <i>step</i>[, <i>smooth</i>, <i>accessor</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/dotbin.js "Source") | ||
Calculates [dot plot](https://en.wikipedia.org/wiki/Dot_plot_%28statistics%29) bin locations for an input *sortedArray* of numerical values, and returns an array of bin locations with indices matching the input *sortedArray*. This method implements the ["dot density" algorithm of Wilkinson, 1999](https://www.cs.uic.edu/~wilkinson/Publications/dotplots.pdf). The *step* parameter determines the bin width: points within *step* values of an anchor point will be assigned the same bin location. The optional *smooth* parameter is a boolean value indicating if the bin locations should additionally be smoothed to reduce variance. An optional *accessor* function can be used to first extract numerical values from an array of input objects, and is equivalent to first calling `array.map(accessor)`. Any null, undefined, or NaN values should be removed prior to calling this method. | ||
<a name="quantiles" href="#quartiles">#</a> | ||
vega.<b>quantiles</b>(<i>array</i>, <i>p</i>[, <i>accessor</i>]) | ||
[<>](https://github.com/vega/vega/blob/master/packages/vega-statistics/src/quartiles.js "Source") | ||
Given an *array* of numeric values and array *p* of probability thresholds in the range [0, 1], returns an array of p-[quantiles](https://en.wikipedia.org/wiki/Quantile). The return value is a array the same length as the input *p*. An optional *accessor* function can be used to first extract numerical values from an array of input objects, and is equivalent to first calling `array.map(accessor)`. This method ignores null, undefined and NaN values. | ||
<a name="quartiles" href="#quartiles">#</a> | ||
@@ -200,0 +297,0 @@ vega.<b>quartiles</b>(<i>array</i>[, <i>accessor</i>]) |
@@ -0,36 +1,15 @@ | ||
import {SQRT2, SQRT2PI} from './constants'; | ||
import {random} from './random'; | ||
export default function(mean, stdev) { | ||
var mu, | ||
sigma, | ||
next = NaN, | ||
dist = {}; | ||
let nextSample = NaN; | ||
dist.mean = function(_) { | ||
if (arguments.length) { | ||
mu = _ || 0; | ||
next = NaN; | ||
return dist; | ||
} else { | ||
return mu; | ||
} | ||
}; | ||
export function sampleNormal(mean, stdev) { | ||
mean = mean || 0; | ||
stdev = stdev == null ? 1 : stdev; | ||
dist.stdev = function(_) { | ||
if (arguments.length) { | ||
sigma = _ == null ? 1 : _; | ||
next = NaN; | ||
return dist; | ||
} else { | ||
return sigma; | ||
} | ||
}; | ||
dist.sample = function() { | ||
var x = 0, y = 0, rds, c; | ||
if (next === next) { | ||
x = next; | ||
next = NaN; | ||
return x; | ||
} | ||
let x = 0, y = 0, rds, c; | ||
if (nextSample === nextSample) { | ||
x = nextSample; | ||
nextSample = NaN; | ||
} else { | ||
do { | ||
@@ -42,61 +21,171 @@ x = random() * 2 - 1; | ||
c = Math.sqrt(-2 * Math.log(rds) / rds); // Box-Muller transform | ||
next = mu + y * c * sigma; | ||
return mu + x * c * sigma; | ||
}; | ||
x *= c; | ||
nextSample = y * c; | ||
} | ||
return mean + x * stdev; | ||
} | ||
dist.pdf = function(x) { | ||
var exp = Math.exp(Math.pow(x-mu, 2) / (-2 * Math.pow(sigma, 2))); | ||
return (1 / (sigma * Math.sqrt(2*Math.PI))) * exp; | ||
}; | ||
export function densityNormal(value, mean, stdev) { | ||
stdev = stdev == null ? 1 : stdev; | ||
const z = (value - (mean || 0)) / stdev; | ||
return Math.exp(-0.5 * z * z) / (stdev * SQRT2PI); | ||
} | ||
// Approximation from West (2009) | ||
// Better Approximations to Cumulative Normal Functions | ||
dist.cdf = function(x) { | ||
var cd, | ||
z = (x - mu) / sigma, | ||
Z = Math.abs(z); | ||
if (Z > 37) { | ||
cd = 0; | ||
// Approximation from West (2009) | ||
// Better Approximations to Cumulative Normal Functions | ||
export function cumulativeNormal(value, mean, stdev) { | ||
mean = mean || 0; | ||
stdev = stdev == null ? 1 : stdev; | ||
let cd, | ||
z = (value - mean) / stdev, | ||
Z = Math.abs(z); | ||
if (Z > 37) { | ||
cd = 0; | ||
} else { | ||
let sum, exp = Math.exp(-Z * Z / 2); | ||
if (Z < 7.07106781186547) { | ||
sum = 3.52624965998911e-02 * Z + 0.700383064443688; | ||
sum = sum * Z + 6.37396220353165; | ||
sum = sum * Z + 33.912866078383; | ||
sum = sum * Z + 112.079291497871; | ||
sum = sum * Z + 221.213596169931; | ||
sum = sum * Z + 220.206867912376; | ||
cd = exp * sum; | ||
sum = 8.83883476483184e-02 * Z + 1.75566716318264; | ||
sum = sum * Z + 16.064177579207; | ||
sum = sum * Z + 86.7807322029461; | ||
sum = sum * Z + 296.564248779674; | ||
sum = sum * Z + 637.333633378831; | ||
sum = sum * Z + 793.826512519948; | ||
sum = sum * Z + 440.413735824752; | ||
cd = cd / sum; | ||
} else { | ||
var sum, exp = Math.exp(-Z*Z/2); | ||
if (Z < 7.07106781186547) { | ||
sum = 3.52624965998911e-02 * Z + 0.700383064443688; | ||
sum = sum * Z + 6.37396220353165; | ||
sum = sum * Z + 33.912866078383; | ||
sum = sum * Z + 112.079291497871; | ||
sum = sum * Z + 221.213596169931; | ||
sum = sum * Z + 220.206867912376; | ||
cd = exp * sum; | ||
sum = 8.83883476483184e-02 * Z + 1.75566716318264; | ||
sum = sum * Z + 16.064177579207; | ||
sum = sum * Z + 86.7807322029461; | ||
sum = sum * Z + 296.564248779674; | ||
sum = sum * Z + 637.333633378831; | ||
sum = sum * Z + 793.826512519948; | ||
sum = sum * Z + 440.413735824752; | ||
cd = cd / sum; | ||
} else { | ||
sum = Z + 0.65; | ||
sum = Z + 4 / sum; | ||
sum = Z + 3 / sum; | ||
sum = Z + 2 / sum; | ||
sum = Z + 1 / sum; | ||
cd = exp / sum / 2.506628274631; | ||
} | ||
sum = Z + 0.65; | ||
sum = Z + 4 / sum; | ||
sum = Z + 3 / sum; | ||
sum = Z + 2 / sum; | ||
sum = Z + 1 / sum; | ||
cd = exp / sum / 2.506628274631; | ||
} | ||
return z > 0 ? 1 - cd : cd; | ||
}; | ||
} | ||
return z > 0 ? 1 - cd : cd; | ||
} | ||
// Approximation of Probit function using inverse error function. | ||
dist.icdf = function(p) { | ||
if (p <= 0 || p >= 1) return NaN; | ||
var x = 2*p - 1, | ||
v = (8 * (Math.PI - 3)) / (3 * Math.PI * (4-Math.PI)), | ||
a = (2 / (Math.PI*v)) + (Math.log(1 - Math.pow(x,2)) / 2), | ||
b = Math.log(1 - (x*x)) / v, | ||
s = (x > 0 ? 1 : -1) * Math.sqrt(Math.sqrt((a*a) - b) - a); | ||
return mu + sigma * Math.SQRT2 * s; | ||
}; | ||
// Approximation of Probit function using inverse error function. | ||
export function quantileNormal(p, mean, stdev) { | ||
if (p < 0 || p > 1) return NaN; | ||
return (mean || 0) + (stdev == null ? 1 : stdev) * SQRT2 * erfinv(2 * p - 1); | ||
} | ||
// Approximate inverse error function. Implementation from "Approximating | ||
// the erfinv function" by Mike Giles, GPU Computing Gems, volume 2, 2010. | ||
// Ported from Apache Commons Math, http://www.apache.org/licenses/LICENSE-2.0 | ||
function erfinv(x) { | ||
// beware that the logarithm argument must be | ||
// commputed as (1.0 - x) * (1.0 + x), | ||
// it must NOT be simplified as 1.0 - x * x as this | ||
// would induce rounding errors near the boundaries +/-1 | ||
let w = - Math.log((1 - x) * (1 + x)), p; | ||
if (w < 6.25) { | ||
w -= 3.125; | ||
p = -3.6444120640178196996e-21; | ||
p = -1.685059138182016589e-19 + p * w; | ||
p = 1.2858480715256400167e-18 + p * w; | ||
p = 1.115787767802518096e-17 + p * w; | ||
p = -1.333171662854620906e-16 + p * w; | ||
p = 2.0972767875968561637e-17 + p * w; | ||
p = 6.6376381343583238325e-15 + p * w; | ||
p = -4.0545662729752068639e-14 + p * w; | ||
p = -8.1519341976054721522e-14 + p * w; | ||
p = 2.6335093153082322977e-12 + p * w; | ||
p = -1.2975133253453532498e-11 + p * w; | ||
p = -5.4154120542946279317e-11 + p * w; | ||
p = 1.051212273321532285e-09 + p * w; | ||
p = -4.1126339803469836976e-09 + p * w; | ||
p = -2.9070369957882005086e-08 + p * w; | ||
p = 4.2347877827932403518e-07 + p * w; | ||
p = -1.3654692000834678645e-06 + p * w; | ||
p = -1.3882523362786468719e-05 + p * w; | ||
p = 0.0001867342080340571352 + p * w; | ||
p = -0.00074070253416626697512 + p * w; | ||
p = -0.0060336708714301490533 + p * w; | ||
p = 0.24015818242558961693 + p * w; | ||
p = 1.6536545626831027356 + p * w; | ||
} else if (w < 16.0) { | ||
w = Math.sqrt(w) - 3.25; | ||
p = 2.2137376921775787049e-09; | ||
p = 9.0756561938885390979e-08 + p * w; | ||
p = -2.7517406297064545428e-07 + p * w; | ||
p = 1.8239629214389227755e-08 + p * w; | ||
p = 1.5027403968909827627e-06 + p * w; | ||
p = -4.013867526981545969e-06 + p * w; | ||
p = 2.9234449089955446044e-06 + p * w; | ||
p = 1.2475304481671778723e-05 + p * w; | ||
p = -4.7318229009055733981e-05 + p * w; | ||
p = 6.8284851459573175448e-05 + p * w; | ||
p = 2.4031110387097893999e-05 + p * w; | ||
p = -0.0003550375203628474796 + p * w; | ||
p = 0.00095328937973738049703 + p * w; | ||
p = -0.0016882755560235047313 + p * w; | ||
p = 0.0024914420961078508066 + p * w; | ||
p = -0.0037512085075692412107 + p * w; | ||
p = 0.005370914553590063617 + p * w; | ||
p = 1.0052589676941592334 + p * w; | ||
p = 3.0838856104922207635 + p * w; | ||
} else if (Number.isFinite(w)) { | ||
w = Math.sqrt(w) - 5.0; | ||
p = -2.7109920616438573243e-11; | ||
p = -2.5556418169965252055e-10 + p * w; | ||
p = 1.5076572693500548083e-09 + p * w; | ||
p = -3.7894654401267369937e-09 + p * w; | ||
p = 7.6157012080783393804e-09 + p * w; | ||
p = -1.4960026627149240478e-08 + p * w; | ||
p = 2.9147953450901080826e-08 + p * w; | ||
p = -6.7711997758452339498e-08 + p * w; | ||
p = 2.2900482228026654717e-07 + p * w; | ||
p = -9.9298272942317002539e-07 + p * w; | ||
p = 4.5260625972231537039e-06 + p * w; | ||
p = -1.9681778105531670567e-05 + p * w; | ||
p = 7.5995277030017761139e-05 + p * w; | ||
p = -0.00021503011930044477347 + p * w; | ||
p = -0.00013871931833623122026 + p * w; | ||
p = 1.0103004648645343977 + p * w; | ||
p = 4.8499064014085844221 + p * w; | ||
} else { | ||
p = Infinity; | ||
} | ||
return p * x; | ||
} | ||
export default function(mean, stdev) { | ||
var mu, | ||
sigma, | ||
dist = { | ||
mean: function(_) { | ||
if (arguments.length) { | ||
mu = _ || 0; | ||
return dist; | ||
} else { | ||
return mu; | ||
} | ||
}, | ||
stdev: function(_) { | ||
if (arguments.length) { | ||
sigma = _ == null ? 1 : _; | ||
return dist; | ||
} else { | ||
return sigma; | ||
} | ||
}, | ||
sample: () => sampleNormal(mu, sigma), | ||
pdf: value => densityNormal(value, mu, sigma), | ||
cdf: value => cumulativeNormal(value, mu, sigma), | ||
icdf: p => quantileNormal(p, mu, sigma) | ||
}; | ||
return dist.mean(mean).stdev(stdev); | ||
} |
@@ -1,16 +0,5 @@ | ||
import numbers from './numbers'; | ||
import {quantile, ascending} from 'd3-array'; | ||
import quantiles from './quantiles'; | ||
export default function(array, f) { | ||
var values = Float64Array.from(numbers(array, f)); | ||
// don't depend on return value from typed array sort call | ||
// protects against undefined sort results in Safari (vega/vega-lite#4964) | ||
values.sort(ascending); | ||
return [ | ||
quantile(values, 0.25), | ||
quantile(values, 0.50), | ||
quantile(values, 0.75) | ||
]; | ||
return quantiles(array, [0.25, 0.50, 0.75], f); | ||
} |
import {random} from './random'; | ||
export default function(min, max) { | ||
export function sampleUniform(min, max) { | ||
if (max == null) { | ||
@@ -8,43 +8,59 @@ max = (min == null ? 1 : min); | ||
} | ||
return min + (max - min) * random(); | ||
} | ||
var dist = {}, | ||
a, b, d; | ||
export function densityUniform(value, min, max) { | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
} | ||
return (value >= min && value <= max) ? 1 / (max - min) : 0; | ||
} | ||
dist.min = function(_) { | ||
if (arguments.length) { | ||
a = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return a; | ||
} | ||
}; | ||
export function cumulativeUniform(value, min, max) { | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
} | ||
return value < min ? 0 : value > max ? 1 : (value - min) / (max - min); | ||
} | ||
dist.max = function(_) { | ||
if (arguments.length) { | ||
b = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return b; | ||
} | ||
}; | ||
export function quantileUniform(p, min, max) { | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
} | ||
return (p >= 0 && p <= 1) ? min + p * (max - min) : NaN; | ||
} | ||
dist.sample = function() { | ||
return a + d * random(); | ||
}; | ||
export default function(min, max) { | ||
var a, b, | ||
dist = { | ||
min: function(_) { | ||
if (arguments.length) { | ||
a = _ || 0; | ||
return dist; | ||
} else { | ||
return a; | ||
} | ||
}, | ||
max: function(_) { | ||
if (arguments.length) { | ||
b = _ == null ? 1 : _; | ||
return dist; | ||
} else { | ||
return b; | ||
} | ||
}, | ||
sample: () => sampleUniform(a, b), | ||
pdf: value => densityUniform(value, a, b), | ||
cdf: value => cumulativeUniform(value, a, b), | ||
icdf: p => quantileUniform(p, a, b) | ||
}; | ||
dist.pdf = function(x) { | ||
return (x >= a && x <= b) ? 1 / d : 0; | ||
}; | ||
dist.cdf = function(x) { | ||
return x < a ? 0 : x > b ? 1 : (x - a) / d; | ||
}; | ||
dist.icdf = function(p) { | ||
return (p >= 0 && p <= 1) ? a + p * d : NaN; | ||
}; | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
} | ||
return dist.min(min).max(max); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104326
32
2043
297
Updatedd3-array@^2.3.1