vega-statistics
Advanced tools
Comparing version 1.2.1 to 1.2.2
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-array')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'd3-array'], factory) : | ||
(factory((global.vega = global.vega || {}),global.d3)); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-array')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'd3-array'], factory) : | ||
(factory((global.vega = {}),global.d3)); | ||
}(this, (function (exports,d3Array) { 'use strict'; | ||
var bin = function(_) { | ||
// determine range | ||
var maxb = _.maxbins || 20, | ||
base = _.base || 10, | ||
logb = Math.log(base), | ||
div = _.divide || [5, 2], | ||
min = _.extent[0], | ||
max = _.extent[1], | ||
span = max - min, | ||
step, level, minstep, precision, v, i, n, eps; | ||
function bin(_) { | ||
// determine range | ||
var maxb = _.maxbins || 20, | ||
base = _.base || 10, | ||
logb = Math.log(base), | ||
div = _.divide || [5, 2], | ||
min = _.extent[0], | ||
max = _.extent[1], | ||
span = max - min, | ||
step, level, minstep, precision, v, i, n, eps; | ||
if (_.step) { | ||
// if step size is explicitly given, use that | ||
step = _.step; | ||
} else if (_.steps) { | ||
// if provided, limit choice to acceptable step sizes | ||
v = span / maxb; | ||
for (i=0, n=_.steps.length; i < n && _.steps[i] < v; ++i); | ||
step = _.steps[Math.max(0, i-1)]; | ||
} else { | ||
// else use span to determine step size | ||
level = Math.ceil(Math.log(maxb) / logb); | ||
minstep = _.minstep || 0; | ||
step = Math.max( | ||
minstep, | ||
Math.pow(base, Math.round(Math.log(span) / logb) - level) | ||
); | ||
if (_.step) { | ||
// if step size is explicitly given, use that | ||
step = _.step; | ||
} else if (_.steps) { | ||
// if provided, limit choice to acceptable step sizes | ||
v = span / maxb; | ||
for (i=0, n=_.steps.length; i < n && _.steps[i] < v; ++i); | ||
step = _.steps[Math.max(0, i-1)]; | ||
} else { | ||
// else use span to determine step size | ||
level = Math.ceil(Math.log(maxb) / logb); | ||
minstep = _.minstep || 0; | ||
step = Math.max( | ||
minstep, | ||
Math.pow(base, Math.round(Math.log(span) / logb) - level) | ||
); | ||
// increase step size if too many bins | ||
while (Math.ceil(span/step) > maxb) { step *= base; } | ||
// increase step size if too many bins | ||
while (Math.ceil(span/step) > maxb) { step *= base; } | ||
// decrease step size if allowed | ||
for (i=0, n=div.length; i<n; ++i) { | ||
v = step / div[i]; | ||
if (v >= minstep && span / v <= maxb) step = v; | ||
// decrease step size if allowed | ||
for (i=0, n=div.length; i<n; ++i) { | ||
v = step / div[i]; | ||
if (v >= minstep && span / v <= maxb) step = v; | ||
} | ||
} | ||
} | ||
// update precision, min and max | ||
v = Math.log(step); | ||
precision = v >= 0 ? 0 : ~~(-v / logb) + 1; | ||
eps = Math.pow(base, -precision - 1); | ||
if (_.nice || _.nice === undefined) { | ||
v = Math.floor(min / step + eps) * step; | ||
min = min < v ? v - step : v; | ||
max = Math.ceil(max / step) * step; | ||
// update precision, min and max | ||
v = Math.log(step); | ||
precision = v >= 0 ? 0 : ~~(-v / logb) + 1; | ||
eps = Math.pow(base, -precision - 1); | ||
if (_.nice || _.nice === undefined) { | ||
v = Math.floor(min / step + eps) * step; | ||
min = min < v ? v - step : v; | ||
max = Math.ceil(max / step) * step; | ||
} | ||
return { | ||
start: min, | ||
stop: max, | ||
step: step | ||
}; | ||
} | ||
return { | ||
start: min, | ||
stop: max, | ||
step: step | ||
}; | ||
}; | ||
function numbers(array, f) { | ||
var numbers = [], | ||
n = array.length, | ||
i = -1, a; | ||
var numbers = function(array, f) { | ||
var numbers = [], | ||
n = array.length, | ||
i = -1, a; | ||
if (f == null) { | ||
while (++i < n) if (!isNaN(a = number(array[i]))) numbers.push(a); | ||
} else { | ||
while (++i < n) if (!isNaN(a = number(f(array[i], i, array)))) numbers.push(a); | ||
} | ||
return numbers; | ||
} | ||
if (f == null) { | ||
while (++i < n) if (!isNaN(a = number(array[i]))) numbers.push(a); | ||
} else { | ||
while (++i < n) if (!isNaN(a = number(f(array[i], i, array)))) numbers.push(a); | ||
function number(x) { | ||
return x === null ? NaN : +x; | ||
} | ||
return numbers; | ||
}; | ||
function number(x) { | ||
return x === null ? NaN : +x; | ||
} | ||
exports.random = Math.random; | ||
exports.random = Math.random; | ||
function setRandom(r) { | ||
exports.random = r; | ||
} | ||
function setRandom(r) { | ||
exports.random = r; | ||
} | ||
function bootstrapCI(array, samples, alpha, f) { | ||
if (!array.length) return [undefined, undefined]; | ||
var bootstrapCI = function(array, samples, alpha, f) { | ||
var values = numbers(array, f), | ||
n = values.length, | ||
m = samples, | ||
a, i, j, mu; | ||
var values = numbers(array, f), | ||
n = values.length, | ||
m = samples, | ||
a, i, j, mu; | ||
for (j=0, mu=Array(m); j<m; ++j) { | ||
for (a=0, i=0; i<n; ++i) { | ||
a += values[~~(exports.random() * n)]; | ||
for (j=0, mu=Array(m); j<m; ++j) { | ||
for (a=0, i=0; i<n; ++i) { | ||
a += values[~~(exports.random() * n)]; | ||
} | ||
mu[j] = a / n; | ||
} | ||
mu[j] = a / n; | ||
return [ | ||
d3Array.quantile(mu.sort(d3Array.ascending), alpha/2), | ||
d3Array.quantile(mu, 1-(alpha/2)) | ||
]; | ||
} | ||
return [ | ||
d3Array.quantile(mu.sort(d3Array.ascending), alpha/2), | ||
d3Array.quantile(mu, 1-(alpha/2)) | ||
]; | ||
}; | ||
function quartiles(array, f) { | ||
var values = numbers(array, f); | ||
var quartiles = function(array, f) { | ||
var values = numbers(array, f); | ||
return [ | ||
d3Array.quantile(values.sort(d3Array.ascending), 0.25), | ||
d3Array.quantile(values, 0.50), | ||
d3Array.quantile(values, 0.75) | ||
]; | ||
}; | ||
var integer = function(min, max) { | ||
if (max == null) { | ||
max = min; | ||
min = 0; | ||
return [ | ||
d3Array.quantile(values.sort(d3Array.ascending), 0.25), | ||
d3Array.quantile(values, 0.50), | ||
d3Array.quantile(values, 0.75) | ||
]; | ||
} | ||
var dist = {}, | ||
a, b, d; | ||
dist.min = function(_) { | ||
if (arguments.length) { | ||
a = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return a; | ||
function integer(min, max) { | ||
if (max == null) { | ||
max = min; | ||
min = 0; | ||
} | ||
}; | ||
dist.max = function(_) { | ||
if (arguments.length) { | ||
b = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return b; | ||
} | ||
}; | ||
var dist = {}, | ||
a, b, d; | ||
dist.sample = function() { | ||
return a + Math.floor(d * exports.random()); | ||
}; | ||
dist.min = function(_) { | ||
if (arguments.length) { | ||
a = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return a; | ||
} | ||
}; | ||
dist.pdf = function(x) { | ||
return (x === Math.floor(x) && x >= a && x < b) ? 1 / d : 0; | ||
}; | ||
dist.max = function(_) { | ||
if (arguments.length) { | ||
b = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return b; | ||
} | ||
}; | ||
dist.cdf = function(x) { | ||
var v = Math.floor(x); | ||
return v < a ? 0 : v >= b ? 1 : (v - a + 1) / d; | ||
}; | ||
dist.sample = function() { | ||
return a + Math.floor(d * exports.random()); | ||
}; | ||
dist.icdf = function(p) { | ||
return (p >= 0 && p <= 1) ? a - 1 + Math.floor(p * d) : NaN; | ||
}; | ||
dist.pdf = function(x) { | ||
return (x === Math.floor(x) && x >= a && x < b) ? 1 / d : 0; | ||
}; | ||
return dist.min(min).max(max); | ||
}; | ||
dist.cdf = function(x) { | ||
var v = Math.floor(x); | ||
return v < a ? 0 : v >= b ? 1 : (v - a + 1) / d; | ||
}; | ||
var gaussian = function(mean, stdev) { | ||
var mu, | ||
sigma, | ||
next = NaN, | ||
dist = {}; | ||
dist.icdf = function(p) { | ||
return (p >= 0 && p <= 1) ? a - 1 + Math.floor(p * d) : NaN; | ||
}; | ||
dist.mean = function(_) { | ||
if (arguments.length) { | ||
mu = _ || 0; | ||
next = NaN; | ||
return dist; | ||
} else { | ||
return mu; | ||
} | ||
}; | ||
return dist.min(min).max(max); | ||
} | ||
dist.stdev = function(_) { | ||
if (arguments.length) { | ||
sigma = _ == null ? 1 : _; | ||
next = NaN; | ||
return dist; | ||
} else { | ||
return sigma; | ||
} | ||
}; | ||
function gaussian(mean, stdev) { | ||
var mu, | ||
sigma, | ||
next = NaN, | ||
dist = {}; | ||
dist.sample = function() { | ||
var x = 0, y = 0, rds, c; | ||
if (next === next) { | ||
x = next; | ||
next = NaN; | ||
return x; | ||
} | ||
do { | ||
x = exports.random() * 2 - 1; | ||
y = exports.random() * 2 - 1; | ||
rds = x * x + y * y; | ||
} while (rds === 0 || rds > 1); | ||
c = Math.sqrt(-2 * Math.log(rds) / rds); // Box-Muller transform | ||
next = mu + y * c * sigma; | ||
return mu + x * c * sigma; | ||
}; | ||
dist.mean = function(_) { | ||
if (arguments.length) { | ||
mu = _ || 0; | ||
next = NaN; | ||
return dist; | ||
} else { | ||
return mu; | ||
} | ||
}; | ||
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; | ||
}; | ||
// 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; | ||
} 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; | ||
dist.stdev = function(_) { | ||
if (arguments.length) { | ||
sigma = _ == null ? 1 : _; | ||
next = NaN; | ||
return dist; | ||
} 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; | ||
return sigma; | ||
} | ||
} | ||
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; | ||
}; | ||
dist.sample = function() { | ||
var x = 0, y = 0, rds, c; | ||
if (next === next) { | ||
x = next; | ||
next = NaN; | ||
return x; | ||
} | ||
do { | ||
x = exports.random() * 2 - 1; | ||
y = exports.random() * 2 - 1; | ||
rds = x * x + y * y; | ||
} while (rds === 0 || rds > 1); | ||
c = Math.sqrt(-2 * Math.log(rds) / rds); // Box-Muller transform | ||
next = mu + y * c * sigma; | ||
return mu + x * c * sigma; | ||
}; | ||
return dist.mean(mean).stdev(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; | ||
}; | ||
// TODO: support for additional kernels? | ||
var kde = function(support, bandwidth) { | ||
var kernel = gaussian(), | ||
dist = {}, | ||
n = 0; | ||
// 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; | ||
} 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; | ||
} | ||
} | ||
return z > 0 ? 1 - cd : cd; | ||
}; | ||
dist.data = function(_) { | ||
if (arguments.length) { | ||
support = _; | ||
n = _ ? _.length : 0; | ||
return dist.bandwidth(bandwidth); | ||
} else { | ||
return support; | ||
} | ||
}; | ||
// 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; | ||
}; | ||
dist.bandwidth = function(_) { | ||
if (!arguments.length) return bandwidth; | ||
bandwidth = _; | ||
if (!bandwidth && support) bandwidth = estimateBandwidth(support); | ||
return dist; | ||
}; | ||
return dist.mean(mean).stdev(stdev); | ||
} | ||
dist.sample = function() { | ||
return support[~~(exports.random() * n)] + bandwidth * kernel.sample(); | ||
}; | ||
// TODO: support for additional kernels? | ||
function kde(support, bandwidth) { | ||
var kernel = gaussian(), | ||
dist = {}, | ||
n = 0; | ||
dist.pdf = function(x) { | ||
for (var y=0, i=0; i<n; ++i) { | ||
y += kernel.pdf((x - support[i]) / bandwidth); | ||
} | ||
return y / bandwidth / n; | ||
}; | ||
dist.data = function(_) { | ||
if (arguments.length) { | ||
support = _; | ||
n = _ ? _.length : 0; | ||
return dist.bandwidth(bandwidth); | ||
} else { | ||
return support; | ||
} | ||
}; | ||
dist.cdf = function(x) { | ||
for (var y=0, i=0; i<n; ++i) { | ||
y += kernel.cdf((x - support[i]) / bandwidth); | ||
} | ||
return y / n; | ||
}; | ||
dist.bandwidth = function(_) { | ||
if (!arguments.length) return bandwidth; | ||
bandwidth = _; | ||
if (!bandwidth && support) bandwidth = estimateBandwidth(support); | ||
return dist; | ||
}; | ||
dist.icdf = function() { | ||
throw Error('KDE icdf not supported.'); | ||
}; | ||
dist.sample = function() { | ||
return support[~~(exports.random() * n)] + bandwidth * kernel.sample(); | ||
}; | ||
return dist.data(support); | ||
}; | ||
dist.pdf = function(x) { | ||
for (var y=0, i=0; i<n; ++i) { | ||
y += kernel.pdf((x - support[i]) / bandwidth); | ||
} | ||
return y / bandwidth / n; | ||
}; | ||
// Scott, D. W. (1992) Multivariate Density Estimation: | ||
// Theory, Practice, and Visualization. Wiley. | ||
function estimateBandwidth(array) { | ||
var n = array.length, | ||
q = quartiles(array), | ||
h = (q[2] - q[0]) / 1.34; | ||
return 1.06 * Math.min(Math.sqrt(d3Array.variance(array)), h) * Math.pow(n, -0.2); | ||
} | ||
dist.cdf = function(x) { | ||
for (var y=0, i=0; i<n; ++i) { | ||
y += kernel.cdf((x - support[i]) / bandwidth); | ||
} | ||
return y / n; | ||
}; | ||
var mixture = function(dists, weights) { | ||
var dist = {}, m = 0, w; | ||
dist.icdf = function() { | ||
throw Error('KDE icdf not supported.'); | ||
}; | ||
function normalize(x) { | ||
var w = [], sum = 0, i; | ||
for (i=0; i<m; ++i) { sum += (w[i] = (x[i]==null ? 1 : +x[i])); } | ||
for (i=0; i<m; ++i) { w[i] /= sum; } | ||
return w; | ||
return dist.data(support); | ||
} | ||
dist.weights = function(_) { | ||
if (arguments.length) { | ||
w = normalize(weights = (_ || [])); | ||
return dist; | ||
// Scott, D. W. (1992) Multivariate Density Estimation: | ||
// Theory, Practice, and Visualization. Wiley. | ||
function estimateBandwidth(array) { | ||
var n = array.length, | ||
q = quartiles(array), | ||
h = (q[2] - q[0]) / 1.34; | ||
return 1.06 * Math.min(Math.sqrt(d3Array.variance(array)), h) * Math.pow(n, -0.2); | ||
} | ||
function mixture(dists, weights) { | ||
var dist = {}, m = 0, w; | ||
function normalize(x) { | ||
var w = [], sum = 0, i; | ||
for (i=0; i<m; ++i) { sum += (w[i] = (x[i]==null ? 1 : +x[i])); } | ||
for (i=0; i<m; ++i) { w[i] /= sum; } | ||
return w; | ||
} | ||
return weights; | ||
}; | ||
dist.distributions = function(_) { | ||
if (arguments.length) { | ||
if (_) { | ||
m = _.length; | ||
dists = _; | ||
} else { | ||
m = 0; | ||
dists = []; | ||
dist.weights = function(_) { | ||
if (arguments.length) { | ||
w = normalize(weights = (_ || [])); | ||
return dist; | ||
} | ||
return dist.weights(weights); | ||
} | ||
return dists; | ||
}; | ||
return weights; | ||
}; | ||
dist.sample = function() { | ||
var r = exports.random(), | ||
d = dists[m-1], | ||
v = w[0], | ||
i = 0; | ||
dist.distributions = function(_) { | ||
if (arguments.length) { | ||
if (_) { | ||
m = _.length; | ||
dists = _; | ||
} else { | ||
m = 0; | ||
dists = []; | ||
} | ||
return dist.weights(weights); | ||
} | ||
return dists; | ||
}; | ||
// first select distribution | ||
for (; i<m-1; v += w[++i]) { | ||
if (r < v) { d = dists[i]; break; } | ||
} | ||
// then sample from it | ||
return d.sample(); | ||
}; | ||
dist.sample = function() { | ||
var r = exports.random(), | ||
d = dists[m-1], | ||
v = w[0], | ||
i = 0; | ||
dist.pdf = function(x) { | ||
for (var p=0, i=0; i<m; ++i) { | ||
p += w[i] * dists[i].pdf(x); | ||
} | ||
return p; | ||
}; | ||
// first select distribution | ||
for (; i<m-1; v += w[++i]) { | ||
if (r < v) { d = dists[i]; break; } | ||
} | ||
// then sample from it | ||
return d.sample(); | ||
}; | ||
dist.cdf = function(x) { | ||
for (var p=0, i=0; i<m; ++i) { | ||
p += w[i] * dists[i].cdf(x); | ||
} | ||
return p; | ||
}; | ||
dist.pdf = function(x) { | ||
for (var p=0, i=0; i<m; ++i) { | ||
p += w[i] * dists[i].pdf(x); | ||
} | ||
return p; | ||
}; | ||
dist.icdf = function() { | ||
throw Error('Mixture icdf not supported.'); | ||
}; | ||
dist.cdf = function(x) { | ||
for (var p=0, i=0; i<m; ++i) { | ||
p += w[i] * dists[i].cdf(x); | ||
} | ||
return p; | ||
}; | ||
return dist.distributions(dists).weights(weights); | ||
}; | ||
dist.icdf = function() { | ||
throw Error('Mixture icdf not supported.'); | ||
}; | ||
var uniform = function(min, max) { | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
return dist.distributions(dists).weights(weights); | ||
} | ||
var dist = {}, | ||
a, b, d; | ||
dist.min = function(_) { | ||
if (arguments.length) { | ||
a = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return a; | ||
function uniform(min, max) { | ||
if (max == null) { | ||
max = (min == null ? 1 : min); | ||
min = 0; | ||
} | ||
}; | ||
dist.max = function(_) { | ||
if (arguments.length) { | ||
b = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return b; | ||
} | ||
}; | ||
var dist = {}, | ||
a, b, d; | ||
dist.sample = function() { | ||
return a + d * exports.random(); | ||
}; | ||
dist.min = function(_) { | ||
if (arguments.length) { | ||
a = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return a; | ||
} | ||
}; | ||
dist.pdf = function(x) { | ||
return (x >= a && x <= b) ? 1 / d : 0; | ||
}; | ||
dist.max = function(_) { | ||
if (arguments.length) { | ||
b = _ || 0; | ||
d = b - a; | ||
return dist; | ||
} else { | ||
return b; | ||
} | ||
}; | ||
dist.cdf = function(x) { | ||
return x < a ? 0 : x > b ? 1 : (x - a) / d; | ||
}; | ||
dist.sample = function() { | ||
return a + d * exports.random(); | ||
}; | ||
dist.icdf = function(p) { | ||
return (p >= 0 && p <= 1) ? a + p * d : NaN; | ||
}; | ||
dist.pdf = function(x) { | ||
return (x >= a && x <= b) ? 1 / d : 0; | ||
}; | ||
return dist.min(min).max(max); | ||
}; | ||
dist.cdf = function(x) { | ||
return x < a ? 0 : x > b ? 1 : (x - a) / d; | ||
}; | ||
exports.bin = bin; | ||
exports.bootstrapCI = bootstrapCI; | ||
exports.quartiles = quartiles; | ||
exports.setRandom = setRandom; | ||
exports.randomInteger = integer; | ||
exports.randomKDE = kde; | ||
exports.randomMixture = mixture; | ||
exports.randomNormal = gaussian; | ||
exports.randomUniform = uniform; | ||
dist.icdf = function(p) { | ||
return (p >= 0 && p <= 1) ? a + p * d : NaN; | ||
}; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
return dist.min(min).max(max); | ||
} | ||
exports.bin = bin; | ||
exports.bootstrapCI = bootstrapCI; | ||
exports.quartiles = quartiles; | ||
exports.setRandom = setRandom; | ||
exports.randomInteger = integer; | ||
exports.randomKDE = kde; | ||
exports.randomMixture = mixture; | ||
exports.randomNormal = gaussian; | ||
exports.randomUniform = uniform; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
@@ -1,1 +0,1 @@ | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-array")):"function"==typeof define&&define.amd?define(["exports","d3-array"],t):t(n.vega=n.vega||{},n.d3)}(this,function(n,t){"use strict";function r(n){return null===n?NaN:+n}function e(n){var r=n.length,e=o(n),a=(e[2]-e[0])/1.34;return 1.06*Math.min(Math.sqrt(t.variance(n)),a)*Math.pow(r,-.2)}var a=function(n,t){var e,a=[],o=n.length,u=-1;if(null==t)for(;++u<o;)isNaN(e=r(n[u]))||a.push(e);else for(;++u<o;)isNaN(e=r(t(n[u],u,n)))||a.push(e);return a};n.random=Math.random;var o=function(n,r){var e=a(n,r);return[t.quantile(e.sort(t.ascending),.25),t.quantile(e,.5),t.quantile(e,.75)]},u=function(t,r){var e,a,o=NaN,u={};return u.mean=function(n){return arguments.length?(e=n||0,o=NaN,u):e},u.stdev=function(n){return arguments.length?(a=null==n?1:n,o=NaN,u):a},u.sample=function(){var t,r,u=0,i=0;if(o===o)return u=o,o=NaN,u;do{t=(u=2*n.random()-1)*u+(i=2*n.random()-1)*i}while(0===t||t>1);return r=Math.sqrt(-2*Math.log(t)/t),o=e+i*r*a,e+u*r*a},u.pdf=function(n){var t=Math.exp(Math.pow(n-e,2)/(-2*Math.pow(a,2)));return 1/(a*Math.sqrt(2*Math.PI))*t},u.cdf=function(n){var t,r=(n-e)/a,o=Math.abs(r);if(o>37)t=0;else{var u=Math.exp(-o*o/2);o<7.07106781186547?(t=u*((((((.0352624965998911*o+.700383064443688)*o+6.37396220353165)*o+33.912866078383)*o+112.079291497871)*o+221.213596169931)*o+220.206867912376),t/=((((((.0883883476483184*o+1.75566716318264)*o+16.064177579207)*o+86.7807322029461)*o+296.564248779674)*o+637.333633378831)*o+793.826512519948)*o+440.413735824752):t=u/(o+1/(o+2/(o+3/(o+4/(o+.65)))))/2.506628274631}return r>0?1-t:t},u.icdf=function(n){if(n<=0||n>=1)return NaN;var t=2*n-1,r=8*(Math.PI-3)/(3*Math.PI*(4-Math.PI)),o=2/(Math.PI*r)+Math.log(1-Math.pow(t,2))/2,u=Math.log(1-t*t)/r,i=(t>0?1:-1)*Math.sqrt(Math.sqrt(o*o-u)-o);return e+a*Math.SQRT2*i},u.mean(t).stdev(r)};n.bin=function(n){var t,r,e,a,o,u,i,f,c=n.maxbins||20,d=n.base||10,h=Math.log(d),l=n.divide||[5,2],s=n.extent[0],p=n.extent[1],M=p-s;if(n.step)t=n.step;else if(n.steps){for(o=M/c,u=0,i=n.steps.length;u<i&&n.steps[u]<o;++u);t=n.steps[Math.max(0,u-1)]}else{for(r=Math.ceil(Math.log(c)/h),e=n.minstep||0,t=Math.max(e,Math.pow(d,Math.round(Math.log(M)/h)-r));Math.ceil(M/t)>c;)t*=d;for(u=0,i=l.length;u<i;++u)(o=t/l[u])>=e&&M/o<=c&&(t=o)}return o=Math.log(t),a=o>=0?0:1+~~(-o/h),f=Math.pow(d,-a-1),(n.nice||void 0===n.nice)&&(s=s<(o=Math.floor(s/t+f)*t)?o-t:o,p=Math.ceil(p/t)*t),{start:s,stop:p,step:t}},n.bootstrapCI=function(r,e,o,u){var i,f,c,d,h=a(r,u),l=h.length,s=e;for(c=0,d=Array(s);c<s;++c){for(i=0,f=0;f<l;++f)i+=h[~~(n.random()*l)];d[c]=i/l}return[t.quantile(d.sort(t.ascending),o/2),t.quantile(d,1-o/2)]},n.quartiles=o,n.setRandom=function(t){n.random=t},n.randomInteger=function(t,r){null==r&&(r=t,t=0);var e,a,o,u={};return u.min=function(n){return arguments.length?(e=n||0,o=a-e,u):e},u.max=function(n){return arguments.length?(a=n||0,o=a-e,u):a},u.sample=function(){return e+Math.floor(o*n.random())},u.pdf=function(n){return n===Math.floor(n)&&n>=e&&n<a?1/o:0},u.cdf=function(n){var t=Math.floor(n);return t<e?0:t>=a?1:(t-e+1)/o},u.icdf=function(n){return n>=0&&n<=1?e-1+Math.floor(n*o):NaN},u.min(t).max(r)},n.randomKDE=function(t,r){var a=u(),o={},i=0;return o.data=function(n){return arguments.length?(t=n,i=n?n.length:0,o.bandwidth(r)):t},o.bandwidth=function(n){return arguments.length?(!(r=n)&&t&&(r=e(t)),o):r},o.sample=function(){return t[~~(n.random()*i)]+r*a.sample()},o.pdf=function(n){for(var e=0,o=0;o<i;++o)e+=a.pdf((n-t[o])/r);return e/r/i},o.cdf=function(n){for(var e=0,o=0;o<i;++o)e+=a.cdf((n-t[o])/r);return e/i},o.icdf=function(){throw Error("KDE icdf not supported.")},o.data(t)},n.randomMixture=function(t,r){function e(n){var t,r=[],e=0;for(t=0;t<u;++t)e+=r[t]=null==n[t]?1:+n[t];for(t=0;t<u;++t)r[t]/=e;return r}var a,o={},u=0;return o.weights=function(n){return arguments.length?(a=e(r=n||[]),o):r},o.distributions=function(n){return arguments.length?(n?(u=n.length,t=n):(u=0,t=[]),o.weights(r)):t},o.sample=function(){for(var r=n.random(),e=t[u-1],o=a[0],i=0;i<u-1;o+=a[++i])if(r<o){e=t[i];break}return e.sample()},o.pdf=function(n){for(var r=0,e=0;e<u;++e)r+=a[e]*t[e].pdf(n);return r},o.cdf=function(n){for(var r=0,e=0;e<u;++e)r+=a[e]*t[e].cdf(n);return r},o.icdf=function(){throw Error("Mixture icdf not supported.")},o.distributions(t).weights(r)},n.randomNormal=u,n.randomUniform=function(t,r){null==r&&(r=null==t?1:t,t=0);var e,a,o,u={};return u.min=function(n){return arguments.length?(e=n||0,o=a-e,u):e},u.max=function(n){return arguments.length?(a=n||0,o=a-e,u):a},u.sample=function(){return e+o*n.random()},u.pdf=function(n){return n>=e&&n<=a?1/o:0},u.cdf=function(n){return n<e?0:n>a?1:(n-e)/o},u.icdf=function(n){return n>=0&&n<=1?e+n*o:NaN},u.min(t).max(r)},Object.defineProperty(n,"__esModule",{value:!0})}); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("d3-array")):"function"==typeof define&&define.amd?define(["exports","d3-array"],t):t(n.vega={},n.d3)}(this,function(h,l){"use strict";function s(n,t){var r,e=[],a=n.length,o=-1;if(null==t)for(;++o<a;)isNaN(r=u(n[o]))||e.push(r);else for(;++o<a;)isNaN(r=u(t(n[o],o,n)))||e.push(r);return e}function u(n){return null===n?NaN:+n}function f(n,t){var r=s(n,t);return[l.quantile(r.sort(l.ascending),.25),l.quantile(r,.5),l.quantile(r,.75)]}function n(n,t){var u,i,a=NaN,r={mean:function(n){return arguments.length?(u=n||0,a=NaN,r):u},stdev:function(n){return arguments.length?(i=null==n?1:n,a=NaN,r):i},sample:function(){var n,t,r=0,e=0;if(a==a)return r=a,a=NaN,r;for(;0===(n=(r=2*h.random()-1)*r+(e=2*h.random()-1)*e)||1<n;);return t=Math.sqrt(-2*Math.log(n)/n),a=u+e*t*i,u+r*t*i},pdf:function(n){var t=Math.exp(Math.pow(n-u,2)/(-2*Math.pow(i,2)));return 1/(i*Math.sqrt(2*Math.PI))*t},cdf:function(n){var t,r=(n-u)/i,e=Math.abs(r);if(37<e)t=0;else{var a=Math.exp(-e*e/2);e<7.07106781186547?(t=a*((((((.0352624965998911*e+.700383064443688)*e+6.37396220353165)*e+33.912866078383)*e+112.079291497871)*e+221.213596169931)*e+220.206867912376),t/=((((((.0883883476483184*e+1.75566716318264)*e+16.064177579207)*e+86.7807322029461)*e+296.564248779674)*e+637.333633378831)*e+793.826512519948)*e+440.413735824752):t=a/(e+1/(e+2/(e+3/(e+4/(e+.65)))))/2.506628274631}return 0<r?1-t:t},icdf:function(n){if(n<=0||1<=n)return NaN;var t=2*n-1,r=8*(Math.PI-3)/(3*Math.PI*(4-Math.PI)),e=2/(Math.PI*r)+Math.log(1-Math.pow(t,2))/2,a=Math.log(1-t*t)/r,o=(0<t?1:-1)*Math.sqrt(Math.sqrt(e*e-a)-e);return u+i*Math.SQRT2*o}};return r.mean(n).stdev(t)}h.random=Math.random,h.bin=function(n){var t,r,e,a,o,u,i,f,c=n.maxbins||20,d=n.base||10,h=Math.log(d),l=n.divide||[5,2],s=n.extent[0],p=n.extent[1],M=p-s;if(n.step)t=n.step;else if(n.steps){for(o=M/c,u=0,i=n.steps.length;u<i&&n.steps[u]<o;++u);t=n.steps[Math.max(0,u-1)]}else{for(r=Math.ceil(Math.log(c)/h),e=n.minstep||0,t=Math.max(e,Math.pow(d,Math.round(Math.log(M)/h)-r));Math.ceil(M/t)>c;)t*=d;for(u=0,i=l.length;u<i;++u)e<=(o=t/l[u])&&M/o<=c&&(t=o)}return a=0<=(o=Math.log(t))?0:1+~~(-o/h),f=Math.pow(d,-a-1),(n.nice||void 0===n.nice)&&(s=s<(o=Math.floor(s/t+f)*t)?o-t:o,p=Math.ceil(p/t)*t),{start:s,stop:p,step:t}},h.bootstrapCI=function(n,t,r,e){if(!n.length)return[void 0,void 0];var a,o,u,i,f=s(n,e),c=f.length,d=t;for(u=0,i=Array(d);u<d;++u){for(o=a=0;o<c;++o)a+=f[~~(h.random()*c)];i[u]=a/c}return[l.quantile(i.sort(l.ascending),r/2),l.quantile(i,1-r/2)]},h.quartiles=f,h.setRandom=function(n){h.random=n},h.randomInteger=function(n,t){null==t&&(t=n,n=0);var r,e,a,o={};return o.min=function(n){return arguments.length?(a=e-(r=n||0),o):r},o.max=function(n){return arguments.length?(a=(e=n||0)-r,o):e},o.sample=function(){return r+Math.floor(a*h.random())},o.pdf=function(n){return n===Math.floor(n)&&r<=n&&n<e?1/a:0},o.cdf=function(n){var t=Math.floor(n);return t<r?0:e<=t?1:(t-r+1)/a},o.icdf=function(n){return 0<=n&&n<=1?r-1+Math.floor(n*a):NaN},o.min(n).max(t)},h.randomKDE=function(o,u){var e=n(),i={},a=0;return i.data=function(n){return arguments.length?(a=(o=n)?n.length:0,i.bandwidth(u)):o},i.bandwidth=function(n){return arguments.length?(!(u=n)&&o&&(r=(t=o).length,e=f(t),a=(e[2]-e[0])/1.34,u=1.06*Math.min(Math.sqrt(l.variance(t)),a)*Math.pow(r,-.2)),i):u;var t,r,e,a},i.sample=function(){return o[~~(h.random()*a)]+u*e.sample()},i.pdf=function(n){for(var t=0,r=0;r<a;++r)t+=e.pdf((n-o[r])/u);return t/u/a},i.cdf=function(n){for(var t=0,r=0;r<a;++r)t+=e.cdf((n-o[r])/u);return t/a},i.icdf=function(){throw Error("KDE icdf not supported.")},i.data(o)},h.randomMixture=function(a,t){var o,r={},u=0;return r.weights=function(n){return arguments.length?(o=function(n){var t,r=[],e=0;for(t=0;t<u;++t)e+=r[t]=null==n[t]?1:+n[t];for(t=0;t<u;++t)r[t]/=e;return r}(t=n||[]),r):t},r.distributions=function(n){return arguments.length?(a=n?(u=n.length,n):(u=0,[]),r.weights(t)):a},r.sample=function(){for(var n=h.random(),t=a[u-1],r=o[0],e=0;e<u-1;r+=o[++e])if(n<r){t=a[e];break}return t.sample()},r.pdf=function(n){for(var t=0,r=0;r<u;++r)t+=o[r]*a[r].pdf(n);return t},r.cdf=function(n){for(var t=0,r=0;r<u;++r)t+=o[r]*a[r].cdf(n);return t},r.icdf=function(){throw Error("Mixture icdf not supported.")},r.distributions(a).weights(t)},h.randomNormal=n,h.randomUniform=function(n,t){null==t&&(t=null==n?1:n,n=0);var r,e,a,o={};return o.min=function(n){return arguments.length?(a=e-(r=n||0),o):r},o.max=function(n){return arguments.length?(a=(e=n||0)-r,o):e},o.sample=function(){return r+a*h.random()},o.pdf=function(n){return r<=n&&n<=e?1/a:0},o.cdf=function(n){return n<r?0:e<n?1:(n-r)/a},o.icdf=function(n){return 0<=n&&n<=1?r+n*a:NaN},o.min(n).max(t)},Object.defineProperty(h,"__esModule",{value:!0})}); |
{ | ||
"name": "vega-statistics", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Statistical routines and probability distributions.", | ||
@@ -27,11 +27,11 @@ "keywords": [ | ||
"test": "tape 'test/**/*-test.js' && eslint index.js src test", | ||
"prepublish": "npm run build", | ||
"prepublishOnly": "npm run build", | ||
"postpublish": "git push && git push --tags && zip -j build/vega-statistics.zip -- LICENSE README.md build/vega-statistics.js build/vega-statistics.min.js" | ||
}, | ||
"dependencies": { | ||
"d3-array": "1" | ||
"d3-array": "^1.2.4" | ||
}, | ||
"devDependencies": { | ||
"eslint": "4", | ||
"rollup": "0.43", | ||
"eslint": "5", | ||
"rollup": "0.66.6", | ||
"tape": "4", | ||
@@ -38,0 +38,0 @@ "uglify-js": "3" |
@@ -6,2 +6,4 @@ import numbers from './numbers'; | ||
export default function(array, samples, alpha, f) { | ||
if (!array.length) return [undefined, undefined]; | ||
var values = numbers(array, f), | ||
@@ -8,0 +10,0 @@ n = values.length, |
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
38135
774
Updatedd3-array@^1.2.4