vega-statistics
Advanced tools
Comparing version 1.7.2 to 1.7.3
@@ -796,24 +796,24 @@ (function (global, factory) { | ||
function exp(data, x, y) { | ||
let Y = 0, YL = 0, XY = 0, XYL = 0, X2Y = 0, n = 0; | ||
// eslint-disable-next-line no-unused-vars | ||
const [xv, yv, ux, uy] = points(data, x, y); | ||
let YL = 0, XY = 0, XYL = 0, X2Y = 0, n = 0, dx, ly, xy; | ||
visitPoints(data, x, y, (dx, dy) => { | ||
const ly = Math.log(dy), | ||
xy = dx * dy; | ||
++n; | ||
Y += (dy - Y) / n; | ||
visitPoints(data, x, y, (_, dy) => { | ||
dx = xv[n++]; | ||
ly = Math.log(dy); | ||
xy = dx * dy; | ||
YL += (dy * ly - YL) / n; | ||
XY += (xy - XY) / n; | ||
XYL += (xy * ly - XYL) / n; | ||
X2Y += (dx * xy - X2Y) / n; | ||
YL += (dy * ly - YL) / n; | ||
XYL += (xy * ly - XYL) / n; | ||
}); | ||
const coef = ols(XY / Y, YL / Y, XYL / Y, X2Y / Y), | ||
predict = x => coef[0] * Math.exp(coef[1] * x); | ||
const [c0, c1] = ols(XY / uy, YL / uy, XYL / uy, X2Y / uy), | ||
predict = x => Math.exp(c0 + c1 * (x - ux)); | ||
coef[0] = Math.exp(coef[0]); | ||
return { | ||
coef: coef, | ||
coef: [Math.exp(c0 - c1 * ux), c1], | ||
predict: predict, | ||
rSquared: rSquared(data, x, y, Y, predict) | ||
rSquared: rSquared(data, x, y, uy, predict) | ||
}; | ||
@@ -851,3 +851,2 @@ } | ||
function quad(data, x, y) { | ||
const [xv, yv, ux, uy] = points(data, x, y), | ||
@@ -854,0 +853,0 @@ n = xv.length; |
@@ -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,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)}function u(t,r){var e=t.length,u=n.deviation(t,r),a=o(t,r),f=(a[2]-a[0])/1.34;return 1.06*(u=Math.min(u,f)||u||Math.abs(a[0])||1)*Math.pow(e,-.2)}t.random=Math.random;const a=Math.sqrt(2*Math.PI),f=Math.SQRT2;let l=NaN;function i(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{u=2*t.random()-1,a=2*t.random()-1,e=u*u+a*a}while(0===e||e>1);o=Math.sqrt(-2*Math.log(e)/e),u*=o,l=a*o}return n+u*r}function c(t,n,r){const e=(t-(n||0))/(r=null==r?1:r);return Math.exp(-.5*e*e)/(r*a)}function s(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?(t=.0352624965998911*u+.700383064443688,t=t*u+6.37396220353165,t=t*u+33.912866078383,t=t*u+112.079291497871,t=t*u+221.213596169931,t=t*u+220.206867912376,e=n*t,t=.0883883476483184*u+1.75566716318264,t=t*u+16.064177579207,t=t*u+86.7807322029461,t=t*u+296.564248779674,t=t*u+637.333633378831,t=t*u+793.826512519948,t=t*u+440.413735824752,e/=t):(t=u+.65,t=u+4/t,t=u+3/t,t=u+2/t,t=u+1/t,e=n/t/2.506628274631)}return o>0?1-e:e}function h(t,n,r){return t<0||t>1?NaN:(n||0)+(null==r?1:r)*f*function(t){let n,r=-Math.log((1-t)*(1+t));r<6.25?(r-=3.125,n=-364441206401782e-35,n=n*r-16850591381820166e-35,n=128584807152564e-32+n*r,n=11157877678025181e-33+n*r,n=n*r-1333171662854621e-31,n=20972767875968562e-33+n*r,n=6637638134358324e-30+n*r,n=n*r-4054566272975207e-29,n=n*r-8151934197605472e-29,n=26335093153082323e-28+n*r,n=n*r-12975133253453532e-27,n=n*r-5415412054294628e-26,n=1.0512122733215323e-9+n*r,n=n*r-4.112633980346984e-9,n=n*r-2.9070369957882005e-8,n=4.2347877827932404e-7+n*r,n=n*r-13654692000834679e-22,n=n*r-13882523362786469e-21,n=.00018673420803405714+n*r,n=n*r-.000740702534166267,n=n*r-.006033670871430149,n=.24015818242558962+n*r,n=1.6536545626831027+n*r):r<16?(r=Math.sqrt(r)-3.25,n=2.2137376921775787e-9,n=9.075656193888539e-8+n*r,n=n*r-2.7517406297064545e-7,n=1.8239629214389228e-8+n*r,n=15027403968909828e-22+n*r,n=n*r-4013867526981546e-21,n=29234449089955446e-22+n*r,n=12475304481671779e-21+n*r,n=n*r-47318229009055734e-21,n=6828485145957318e-20+n*r,n=24031110387097894e-21+n*r,n=n*r-.0003550375203628475,n=.0009532893797373805+n*r,n=n*r-.0016882755560235047,n=.002491442096107851+n*r,n=n*r-.003751208507569241,n=.005370914553590064+n*r,n=1.0052589676941592+n*r,n=3.0838856104922208+n*r):Number.isFinite(r)?(r=Math.sqrt(r)-5,n=-27109920616438573e-27,n=n*r-2.555641816996525e-10,n=1.5076572693500548e-9+n*r,n=n*r-3.789465440126737e-9,n=7.61570120807834e-9+n*r,n=n*r-1.496002662714924e-8,n=2.914795345090108e-8+n*r,n=n*r-6.771199775845234e-8,n=2.2900482228026655e-7+n*r,n=n*r-9.9298272942317e-7,n=4526062597223154e-21+n*r,n=n*r-1968177810553167e-20,n=7599527703001776e-20+n*r,n=n*r-.00021503011930044477,n=n*r-.00013871931833623122,n=1.0103004648645344+n*r,n=4.849906401408584+n*r):n=1/0;return n*t}(2*t-1)}function d(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:()=>i(r,e),pdf:t=>c(t,r,e),cdf:t=>s(t,r,e),icdf:t=>h(t,r,e)};return o.mean(t).stdev(n)}function p(t,n){return t=t||0,n=null==n?1:n,Math.exp(t+i()*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*a*t)}function M(t,n,r){return s(Math.log(t),n,r)}function g(t,n,r){return Math.exp(h(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 w(t,n,r){return null==r&&(r=null==n?1:n,n=0),t<n?0:t>r?1:(t-n)/(r-n)}function x(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=t.length,u=new Float64Array(o),a=new Float64Array(o);let f,l,i,c=0,s=0,h=0;for(i of t)u[c]=f=+n(i),a[c]=l=+r(i),++c,s+=(f-s)/c,h+=(l-h)/c;for(c=0;c<o;++c)u[c]-=s,a[c]-=h;return[u,a,s,h]}function q(t,n,r,e){let o,u,a=-1;for(let f of t)o=n(f),u=r(f),null!=o&&(o=+o)>=o&&null!=u&&(u=+u)>=u&&e(o,u,++a)}function A(t,n,r,e,o){let u=0,a=0;return q(t,n,r,(t,n)=>{const r=n-o(t),f=n-e;u+=r*r,a+=f*f}),1-u/a}function F(t,n,r){let e=0,o=0,u=0,a=0,f=0;q(t,n,r,(t,n)=>{++f,e+=(t-e)/f,o+=(n-o)/f,u+=(t*n-u)/f,a+=(t*t-a)/f});const l=y(e,o,u,a),i=t=>l[0]+l[1]*t;return{coef:l,predict:i,rSquared:A(t,n,r,o,i)}}function L(t,n,r){const[e,o,u,a]=N(t,n,r),f=e.length;let l,i,c,s,h=0,d=0,p=0,m=0,M=0;for(l=0;l<f;)i=e[l],c=o[l++],s=i*i,h+=(s-h)/l,d+=(s*i-d)/l,p+=(s*s-p)/l,m+=(i*c-m)/l,M+=(s*c-M)/l;const g=p-h*h,v=h*g-d*d,b=(M*h-m*d)/v,w=(m*g-M*d)/v,x=-b*h,y=t=>b*(t-=u)*t+w*t+x+a;return{coef:[x-w*u+b*u*u+a,w-2*b*u,b],predict:y,rSquared:A(t,n,r,a,y)}}function S(t,n,r,e){const o=Array(t);let u,a,f,l;for(u=0;u<t;++u)o[u]=0;for(u=t-1;u>=0;--u)for(f=n[u],l=1,o[u]+=f,a=1;a<=u;++a)l*=(u+1-a)/a,o[u-a]+=f*Math.pow(r,a)*l;return o[0]+=e,o}function E(t){return(t=1-t*t*t)*t*t}function P(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 U=.1*Math.PI/180;function I(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.bandwidthNRD=u,t.bin=function(t){var n,r,e,o,u,a,f,l,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,f=t.steps.length;a<f&&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,f=h.length;a<f;++a)(u=n/h[a])>=e&&m/u<=i&&(n=u)}return o=(u=Math.log(n))>=0?0:1+~~(-u/s),l=Math.pow(c,-o-1),(t.nice||void 0===t.nice)&&(d=d<(u=Math.floor(d/n+l)*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 f,l,i,c,s=Float64Array.from(r(e,a)),h=s.length,d=o;for(i=0,c=Array(d);i<d;++i){for(f=0,l=0;l<h;++l)f+=s[~~(t.random()*h)];c[i]=f/h}return c.sort(n.ascending),[n.quantile(c,u/2),n.quantile(c,1-u/2)]},t.cumulativeLogNormal=M,t.cumulativeNormal=s,t.cumulativeUniform=w,t.densityLogNormal=m,t.densityNormal=c,t.densityUniform=b,t.dotbin=function(t,n,r,e){e=e||(t=>t);let o,u=0,a=1,f=t.length,l=new Float64Array(f),i=e(t[0]),c=i,s=i+n;for(;a<f;++a){if(o=e(t[a]),o>=s){for(c=(i+c)/2;u<a;++u)l[u]=c;s=o+n,i=o}c=o}for(c=(i+c)/2;u<a;++u)l[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}(l,n+n/4):l},t.quantileLogNormal=g,t.quantileNormal=h,t.quantileUniform=x,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=d(),o={},a=0;return o.data=function(t){return arguments.length?(n=t,a=t?t.length:0,o.bandwidth(r)):n},o.bandwidth=function(t){return arguments.length?(!(r=t)&&n&&(r=u(n)),o):r},o.sample=function(){return n[~~(t.random()*a)]+r*e.sample()},o.pdf=function(t){for(var o=0,u=0;u<a;++u)o+=e.pdf((t-n[u])/r);return o/r/a},o.cdf=function(t){for(var o=0,u=0;u<a;++u)o+=e.cdf((t-n[u])/r);return o/a},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],f=0;f<u-1;a+=e[++f])if(r<a){o=n[f];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=d,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=>w(t,r,e),icdf:t=>x(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,f=0,l=0;q(t,n,r,(t,n)=>{const r=Math.log(n),i=t*n;++l,e+=(n-e)/l,u+=(i-u)/l,f+=(t*i-f)/l,o+=(n*r-o)/l,a+=(i*r-a)/l});const i=y(u/e,o/e,a/e,f/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,c)}},t.regressionLinear=F,t.regressionLoess=function(t,r,e,o){const[u,a,f,l]=N(t,r,e,!0),i=u.length,c=Math.max(2,~~(o*i)),s=new Float64Array(i),h=new Float64Array(i),d=new Float64Array(i).fill(1);for(let t=-1;++t<=2;){const r=[0,c-1];for(let t=0;t<i;++t){const n=u[t],e=r[0],o=r[1],f=n-u[e]>u[o]-n?e:o;let l=0,i=0,c=0,p=0,m=0,M=1/Math.abs(u[f]-n||1);for(let t=e;t<=o;++t){const r=u[t],e=a[t],o=E(Math.abs(n-r)*M)*d[t],f=r*o;l+=o,i+=f,c+=e*o,p+=e*f,m+=r*f}const[g,v]=y(i/l,c/l,p/l,m/l);s[t]=g+v*n,h[t]=Math.abs(a[t]-s[t]),P(u,t+1,r)}if(2===t)break;const e=n.median(h);if(Math.abs(e)<1e-12)break;for(let t,n,r=0;r<i;++r)t=h[r]/(6*e),d[r]=t>=1?1e-12:(n=1-t*t)*n}return function(t,n,r,e){const o=t.length,u=[];let a,f=0,l=0,i=[];for(;f<o;++f)a=t[f]+r,i[0]===a?i[1]+=(n[f]-i[1])/++l:(l=0,i[1]+=e,i=[a,n[f]],u.push(i));return i[1]+=e,u}(u,s,f,l)},t.regressionLog=function(t,n,r){let e=0,o=0,u=0,a=0,f=0;q(t,n,r,(t,n)=>{++f,t=Math.log(t),e+=(t-e)/f,o+=(n-o)/f,u+=(t*n-u)/f,a+=(t*t-a)/f});const l=y(e,o,u,a),i=t=>l[0]+l[1]*Math.log(t);return{coef:l,predict:i,rSquared:A(t,n,r,o,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,a,f]=N(t,n,r),l=o.length,i=[],c=[],s=e+1;let h,d,p,m,M;for(h=0;h<s;++h){for(p=0,m=0;p<l;++p)m+=Math.pow(o[p],h)*u[p];for(i.push(m),M=new Float64Array(s),d=0;d<s;++d){for(p=0,m=0;p<l;++p)m+=Math.pow(o[p],h+d);M[d]=m}c.push(M)}c.push(i);const g=function(t){const n=t.length-1,r=[];let e,o,u,a,f;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)f=t[u][e],t[u][e]=t[u][a],t[u][a]=f;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(f=0,u=o+1;u<n;++u)f+=t[u][o]*r[u];r[o]=(t[n][o]-f)/t[o][o]}return r}(c),v=t=>{t-=a;let n=f+g[0]+g[1]*t+g[2]*t*t;for(h=3;h<s;++h)n+=g[h]*Math.pow(t,h);return n};return{coef:S(s,g,-a,f),predict:v,rSquared:A(t,n,r,f,v)}},t.regressionPow=function(t,n,r){let e=0,o=0,u=0,a=0,f=0,l=0;q(t,n,r,(t,n)=>{const r=Math.log(t),i=Math.log(n);++l,e+=(r-e)/l,o+=(i-o)/l,u+=(r*i-u)/l,a+=(r*r-a)/l,f+=(n-f)/l});const i=y(e,o,u,a),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,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],f=a-u,l=f/e,i=[o(u)],c=[];if(r===e){for(let t=1;t<e;++t)i.push(o(u+t/r*f));return i.push(o(a)),i}c.push(o(a));for(let t=r;--t>0;)c.push(o(u+t/r*f));let s=i[0],h=c[c.length-1];for(;h;){const t=o((s[0]+h[0])/2);t[0]-s[0]>=l&&I(s,t,h)>U?c.push(t):(s=h,i.push(h),c.pop()),h=c[c.length-1]}return i},t.sampleLogNormal=p,t.sampleNormal=i,t.sampleUniform=v,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)}function u(t,r){var e=t.length,u=n.deviation(t,r),a=o(t,r),f=(a[2]-a[0])/1.34;return 1.06*(u=Math.min(u,f)||u||Math.abs(a[0])||1)*Math.pow(e,-.2)}t.random=Math.random;const a=Math.sqrt(2*Math.PI),f=Math.SQRT2;let l=NaN;function i(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{u=2*t.random()-1,a=2*t.random()-1,e=u*u+a*a}while(0===e||e>1);o=Math.sqrt(-2*Math.log(e)/e),u*=o,l=a*o}return n+u*r}function c(t,n,r){const e=(t-(n||0))/(r=null==r?1:r);return Math.exp(-.5*e*e)/(r*a)}function s(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?(t=.0352624965998911*u+.700383064443688,t=t*u+6.37396220353165,t=t*u+33.912866078383,t=t*u+112.079291497871,t=t*u+221.213596169931,t=t*u+220.206867912376,e=n*t,t=.0883883476483184*u+1.75566716318264,t=t*u+16.064177579207,t=t*u+86.7807322029461,t=t*u+296.564248779674,t=t*u+637.333633378831,t=t*u+793.826512519948,t=t*u+440.413735824752,e/=t):(t=u+.65,t=u+4/t,t=u+3/t,t=u+2/t,t=u+1/t,e=n/t/2.506628274631)}return o>0?1-e:e}function h(t,n,r){return t<0||t>1?NaN:(n||0)+(null==r?1:r)*f*function(t){let n,r=-Math.log((1-t)*(1+t));r<6.25?(r-=3.125,n=-364441206401782e-35,n=n*r-16850591381820166e-35,n=128584807152564e-32+n*r,n=11157877678025181e-33+n*r,n=n*r-1333171662854621e-31,n=20972767875968562e-33+n*r,n=6637638134358324e-30+n*r,n=n*r-4054566272975207e-29,n=n*r-8151934197605472e-29,n=26335093153082323e-28+n*r,n=n*r-12975133253453532e-27,n=n*r-5415412054294628e-26,n=1.0512122733215323e-9+n*r,n=n*r-4.112633980346984e-9,n=n*r-2.9070369957882005e-8,n=4.2347877827932404e-7+n*r,n=n*r-13654692000834679e-22,n=n*r-13882523362786469e-21,n=.00018673420803405714+n*r,n=n*r-.000740702534166267,n=n*r-.006033670871430149,n=.24015818242558962+n*r,n=1.6536545626831027+n*r):r<16?(r=Math.sqrt(r)-3.25,n=2.2137376921775787e-9,n=9.075656193888539e-8+n*r,n=n*r-2.7517406297064545e-7,n=1.8239629214389228e-8+n*r,n=15027403968909828e-22+n*r,n=n*r-4013867526981546e-21,n=29234449089955446e-22+n*r,n=12475304481671779e-21+n*r,n=n*r-47318229009055734e-21,n=6828485145957318e-20+n*r,n=24031110387097894e-21+n*r,n=n*r-.0003550375203628475,n=.0009532893797373805+n*r,n=n*r-.0016882755560235047,n=.002491442096107851+n*r,n=n*r-.003751208507569241,n=.005370914553590064+n*r,n=1.0052589676941592+n*r,n=3.0838856104922208+n*r):Number.isFinite(r)?(r=Math.sqrt(r)-5,n=-27109920616438573e-27,n=n*r-2.555641816996525e-10,n=1.5076572693500548e-9+n*r,n=n*r-3.789465440126737e-9,n=7.61570120807834e-9+n*r,n=n*r-1.496002662714924e-8,n=2.914795345090108e-8+n*r,n=n*r-6.771199775845234e-8,n=2.2900482228026655e-7+n*r,n=n*r-9.9298272942317e-7,n=4526062597223154e-21+n*r,n=n*r-1968177810553167e-20,n=7599527703001776e-20+n*r,n=n*r-.00021503011930044477,n=n*r-.00013871931833623122,n=1.0103004648645344+n*r,n=4.849906401408584+n*r):n=1/0;return n*t}(2*t-1)}function d(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:()=>i(r,e),pdf:t=>c(t,r,e),cdf:t=>s(t,r,e),icdf:t=>h(t,r,e)};return o.mean(t).stdev(n)}function p(t,n){return t=t||0,n=null==n?1:n,Math.exp(t+i()*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*a*t)}function M(t,n,r){return s(Math.log(t),n,r)}function g(t,n,r){return Math.exp(h(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 w(t,n,r){return null==r&&(r=null==n?1:n,n=0),t<n?0:t>r?1:(t-n)/(r-n)}function x(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=t.length,u=new Float64Array(o),a=new Float64Array(o);let f,l,i,c=0,s=0,h=0;for(i of t)u[c]=f=+n(i),a[c]=l=+r(i),++c,s+=(f-s)/c,h+=(l-h)/c;for(c=0;c<o;++c)u[c]-=s,a[c]-=h;return[u,a,s,h]}function q(t,n,r,e){let o,u,a=-1;for(let f of t)o=n(f),u=r(f),null!=o&&(o=+o)>=o&&null!=u&&(u=+u)>=u&&e(o,u,++a)}function A(t,n,r,e,o){let u=0,a=0;return q(t,n,r,(t,n)=>{const r=n-o(t),f=n-e;u+=r*r,a+=f*f}),1-u/a}function F(t,n,r){let e=0,o=0,u=0,a=0,f=0;q(t,n,r,(t,n)=>{++f,e+=(t-e)/f,o+=(n-o)/f,u+=(t*n-u)/f,a+=(t*t-a)/f});const l=y(e,o,u,a),i=t=>l[0]+l[1]*t;return{coef:l,predict:i,rSquared:A(t,n,r,o,i)}}function L(t,n,r){const[e,o,u,a]=N(t,n,r),f=e.length;let l,i,c,s,h=0,d=0,p=0,m=0,M=0;for(l=0;l<f;)i=e[l],c=o[l++],s=i*i,h+=(s-h)/l,d+=(s*i-d)/l,p+=(s*s-p)/l,m+=(i*c-m)/l,M+=(s*c-M)/l;const g=p-h*h,v=h*g-d*d,b=(M*h-m*d)/v,w=(m*g-M*d)/v,x=-b*h,y=t=>b*(t-=u)*t+w*t+x+a;return{coef:[x-w*u+b*u*u+a,w-2*b*u,b],predict:y,rSquared:A(t,n,r,a,y)}}function S(t,n,r,e){const o=Array(t);let u,a,f,l;for(u=0;u<t;++u)o[u]=0;for(u=t-1;u>=0;--u)for(f=n[u],l=1,o[u]+=f,a=1;a<=u;++a)l*=(u+1-a)/a,o[u-a]+=f*Math.pow(r,a)*l;return o[0]+=e,o}function E(t){return(t=1-t*t*t)*t*t}function P(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 U=.1*Math.PI/180;function I(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.bandwidthNRD=u,t.bin=function(t){var n,r,e,o,u,a,f,l,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,f=t.steps.length;a<f&&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,f=h.length;a<f;++a)(u=n/h[a])>=e&&m/u<=i&&(n=u)}return o=(u=Math.log(n))>=0?0:1+~~(-u/s),l=Math.pow(c,-o-1),(t.nice||void 0===t.nice)&&(d=d<(u=Math.floor(d/n+l)*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 f,l,i,c,s=Float64Array.from(r(e,a)),h=s.length,d=o;for(i=0,c=Array(d);i<d;++i){for(f=0,l=0;l<h;++l)f+=s[~~(t.random()*h)];c[i]=f/h}return c.sort(n.ascending),[n.quantile(c,u/2),n.quantile(c,1-u/2)]},t.cumulativeLogNormal=M,t.cumulativeNormal=s,t.cumulativeUniform=w,t.densityLogNormal=m,t.densityNormal=c,t.densityUniform=b,t.dotbin=function(t,n,r,e){e=e||(t=>t);let o,u=0,a=1,f=t.length,l=new Float64Array(f),i=e(t[0]),c=i,s=i+n;for(;a<f;++a){if(o=e(t[a]),o>=s){for(c=(i+c)/2;u<a;++u)l[u]=c;s=o+n,i=o}c=o}for(c=(i+c)/2;u<a;++u)l[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}(l,n+n/4):l},t.quantileLogNormal=g,t.quantileNormal=h,t.quantileUniform=x,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=d(),o={},a=0;return o.data=function(t){return arguments.length?(n=t,a=t?t.length:0,o.bandwidth(r)):n},o.bandwidth=function(t){return arguments.length?(!(r=t)&&n&&(r=u(n)),o):r},o.sample=function(){return n[~~(t.random()*a)]+r*e.sample()},o.pdf=function(t){for(var o=0,u=0;u<a;++u)o+=e.pdf((t-n[u])/r);return o/r/a},o.cdf=function(t){for(var o=0,u=0;u<a;++u)o+=e.cdf((t-n[u])/r);return o/a},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],f=0;f<u-1;a+=e[++f])if(r<a){o=n[f];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=d,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=>w(t,r,e),icdf:t=>x(t,r,e)};return null==n&&(n=null==t?1:t,t=0),o.min(t).max(n)},t.regressionExp=function(t,n,r){const[e,o,u,a]=N(t,n,r);let f,l,i,c=0,s=0,h=0,d=0,p=0;q(t,n,r,(t,n)=>{f=e[p++],l=Math.log(n),i=f*n,c+=(n*l-c)/p,s+=(i-s)/p,h+=(i*l-h)/p,d+=(f*i-d)/p});const[m,M]=y(s/a,c/a,h/a,d/a),g=t=>Math.exp(m+M*(t-u));return{coef:[Math.exp(m-M*u),M],predict:g,rSquared:A(t,n,r,a,g)}},t.regressionLinear=F,t.regressionLoess=function(t,r,e,o){const[u,a,f,l]=N(t,r,e,!0),i=u.length,c=Math.max(2,~~(o*i)),s=new Float64Array(i),h=new Float64Array(i),d=new Float64Array(i).fill(1);for(let t=-1;++t<=2;){const r=[0,c-1];for(let t=0;t<i;++t){const n=u[t],e=r[0],o=r[1],f=n-u[e]>u[o]-n?e:o;let l=0,i=0,c=0,p=0,m=0,M=1/Math.abs(u[f]-n||1);for(let t=e;t<=o;++t){const r=u[t],e=a[t],o=E(Math.abs(n-r)*M)*d[t],f=r*o;l+=o,i+=f,c+=e*o,p+=e*f,m+=r*f}const[g,v]=y(i/l,c/l,p/l,m/l);s[t]=g+v*n,h[t]=Math.abs(a[t]-s[t]),P(u,t+1,r)}if(2===t)break;const e=n.median(h);if(Math.abs(e)<1e-12)break;for(let t,n,r=0;r<i;++r)t=h[r]/(6*e),d[r]=t>=1?1e-12:(n=1-t*t)*n}return function(t,n,r,e){const o=t.length,u=[];let a,f=0,l=0,i=[];for(;f<o;++f)a=t[f]+r,i[0]===a?i[1]+=(n[f]-i[1])/++l:(l=0,i[1]+=e,i=[a,n[f]],u.push(i));return i[1]+=e,u}(u,s,f,l)},t.regressionLog=function(t,n,r){let e=0,o=0,u=0,a=0,f=0;q(t,n,r,(t,n)=>{++f,t=Math.log(t),e+=(t-e)/f,o+=(n-o)/f,u+=(t*n-u)/f,a+=(t*t-a)/f});const l=y(e,o,u,a),i=t=>l[0]+l[1]*Math.log(t);return{coef:l,predict:i,rSquared:A(t,n,r,o,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,a,f]=N(t,n,r),l=o.length,i=[],c=[],s=e+1;let h,d,p,m,M;for(h=0;h<s;++h){for(p=0,m=0;p<l;++p)m+=Math.pow(o[p],h)*u[p];for(i.push(m),M=new Float64Array(s),d=0;d<s;++d){for(p=0,m=0;p<l;++p)m+=Math.pow(o[p],h+d);M[d]=m}c.push(M)}c.push(i);const g=function(t){const n=t.length-1,r=[];let e,o,u,a,f;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)f=t[u][e],t[u][e]=t[u][a],t[u][a]=f;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(f=0,u=o+1;u<n;++u)f+=t[u][o]*r[u];r[o]=(t[n][o]-f)/t[o][o]}return r}(c),v=t=>{t-=a;let n=f+g[0]+g[1]*t+g[2]*t*t;for(h=3;h<s;++h)n+=g[h]*Math.pow(t,h);return n};return{coef:S(s,g,-a,f),predict:v,rSquared:A(t,n,r,f,v)}},t.regressionPow=function(t,n,r){let e=0,o=0,u=0,a=0,f=0,l=0;q(t,n,r,(t,n)=>{const r=Math.log(t),i=Math.log(n);++l,e+=(r-e)/l,o+=(i-o)/l,u+=(r*i-u)/l,a+=(r*r-a)/l,f+=(n-f)/l});const i=y(e,o,u,a),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,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],f=a-u,l=f/e,i=[o(u)],c=[];if(r===e){for(let t=1;t<e;++t)i.push(o(u+t/r*f));return i.push(o(a)),i}c.push(o(a));for(let t=r;--t>0;)c.push(o(u+t/r*f));let s=i[0],h=c[c.length-1];for(;h;){const t=o((s[0]+h[0])/2);t[0]-s[0]>=l&&I(s,t,h)>U?c.push(t):(s=h,i.push(h),c.pop()),h=c[c.length-1]}return i},t.sampleLogNormal=p,t.sampleNormal=i,t.sampleUniform=v,t.setRandom=function(n){t.random=n},Object.defineProperty(t,"__esModule",{value:!0})})); |
{ | ||
"name": "vega-statistics", | ||
"version": "1.7.2", | ||
"version": "1.7.3", | ||
"description": "Statistical routines and probability distributions.", | ||
@@ -29,3 +29,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "178cab5429841aaf02d164753a0621e89d1dce9a" | ||
"gitHead": "49a57c10b309addc513a52a38e1959ee169d4758" | ||
} |
import ols from './ols'; | ||
import {visitPoints} from './points'; | ||
import {points, visitPoints} from './points'; | ||
import rSquared from './r-squared'; | ||
export default function(data, x, y) { | ||
let Y = 0, YL = 0, XY = 0, XYL = 0, X2Y = 0, n = 0; | ||
// eslint-disable-next-line no-unused-vars | ||
const [xv, yv, ux, uy] = points(data, x, y); | ||
let YL = 0, XY = 0, XYL = 0, X2Y = 0, n = 0, dx, ly, xy; | ||
visitPoints(data, x, y, (dx, dy) => { | ||
const ly = Math.log(dy), | ||
xy = dx * dy; | ||
++n; | ||
Y += (dy - Y) / n; | ||
visitPoints(data, x, y, (_, dy) => { | ||
dx = xv[n++]; | ||
ly = Math.log(dy); | ||
xy = dx * dy; | ||
YL += (dy * ly - YL) / n; | ||
XY += (xy - XY) / n; | ||
XYL += (xy * ly - XYL) / n; | ||
X2Y += (dx * xy - X2Y) / n; | ||
YL += (dy * ly - YL) / n; | ||
XYL += (xy * ly - XYL) / n; | ||
}); | ||
const coef = ols(XY / Y, YL / Y, XYL / Y, X2Y / Y), | ||
predict = x => coef[0] * Math.exp(coef[1] * x); | ||
const [c0, c1] = ols(XY / uy, YL / uy, XYL / uy, X2Y / uy), | ||
predict = x => Math.exp(c0 + c1 * (x - ux)); | ||
coef[0] = Math.exp(coef[0]); | ||
return { | ||
coef: coef, | ||
coef: [Math.exp(c0 - c1 * ux), c1], | ||
predict: predict, | ||
rSquared: rSquared(data, x, y, Y, predict) | ||
rSquared: rSquared(data, x, y, uy, predict) | ||
}; | ||
} |
@@ -5,3 +5,2 @@ import {points} from './points'; | ||
export default function(data, x, y) { | ||
const [xv, yv, ux, uy] = points(data, x, y), | ||
@@ -8,0 +7,0 @@ n = xv.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
107824