'use strict';var _Mathsign=Math.sign,_MathPI=Math.PI,_NumberisInteger=Number.isInteger,_Mathabs=Math.abs,_Mathexp=Math.exp,_Mathlog=Math.log,_Mathsqrt=Math.sqrt,_Mathpow=Math.pow,_Mathround=Math.round,_Mathfloor=Math.floor,Statistics=function(o,l,u={}){this.data=void 0,this.columns=void 0,this.valueMaps=void 0,this.storedResults=void 0,this.lastUpdated=void 0,this.validScales=['nominal','ordinal','interval','metric'],this.zTable=void 0,this.factorials=[1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600,6227020800,87178291200,1307674368000,20922789888000,355687428096000,6402373705728000,121645100408832000,2432902008176640000,5109094217170944000],this.defaultOptions={epsilon:1e-5,excludeColumns:['ID','id'],incompleteBetaIterations:40,incompleteGammaIterations:80,maxBarnardsN:200,spougeConstant:40,suppressWarnings:!1,zTableIterations:25},this.init=function(M,S,T={}){for(var C in'undefined'==typeof M&&this.errorMessage('No data was supplied.'),this.defaultOptions){var N='object'==typeof T&&this.has(T,C)&&this.has(this.defaultOptions,C)?T[C]:this.defaultOptions[C];Object.defineProperty(this,C,{value:N,writable:!1})}return'undefined'!=typeof M&&this.updateData(M,S),this},this.updateData=function(M,S){this.addData(M),'object'==typeof S&&this.assignValueMap(S),M.constructor===Array&&'object'!=typeof S&&(this.errorMessage('It is strongly encouraged to initalise statistics.js with a variable table that defines the scale of measurement of each variable (e.g. nominal, metric.). All variables will be assumed as nominal and subsequent analyses will likely be flawed.'),g.apply(this))},this.addData=function(M){try{let S=typeof M;if('string'!=S&&'object'!=S||null===M)throw'Input variable data is neither an object nor a JSON encoded string. The variable type is '+S+'. The data could not be properly imported.';return'string'==S&&(M=JSON.parse(M)),this.data=this.data?this.data.concat(M):M,this.lastUpdated=Date.now(),!0}catch(S){return this.errorMessage(S)}},this.addRow=function(M){if('undefined'==typeof M)return this.errorMessage('Add Row: No data was given.');let S='undefined'==typeof this.data?[]:this.data;return S.push(M),this.data=S,this.lastUpdated=Date.now(),!0},this.removeRow=function(M,S=!1){if('undefined'==typeof M)return this.errorMessage('Remove row: No index was given.');let T=this.data,C=this.has(this.columns,'id')||this.has(this.columns,'ID');if(S&&C){let N=-1,I=0;for(;-1==N&&IT.length-1)return this.errorMessage('Remove row: The stored data has only '+T.length+' rows and index '+M+' is too large. Indexes start at 0.');T.splice(M,1)}return this.data=T,this.lastUpdated=Date.now(),!0},this.reset=function(){try{return this.data=void 0,this.storedResults=void 0,this.lastUpdated=Date.now(),!0}catch(M){this.errorMessage(M.message)}},this.assignValueMap=function(M){let S={},T={};for(var C in M){if(!this.has(M,C)||-1{return D[C]}))),'undefined'!=typeof V&&(T[C]=V),-1{return T[C]})},this.checkLastUpdated=function(M,S){return''!==M&&this.has(this.columns,M)&&'undefined'!=typeof M&&'undefined'!=typeof S&&('undefined'==typeof this.storedResults||'undefined'==typeof this.storedResults[M]||'undefined'==typeof this.storedResults[M][S]||'undefined'==typeof this.storedResults[M][S].lastUpdated||this.storedResults[M][S].lastUpdated{return S[M]}):this.errorMessage('Get column: The column "'+M+'" was not found.')},this.sortColumn=function(M,S='asc'){return'undefined'==typeof M?this.errorMessage('Sort column: No column to sort was specified.'):this.has(this.columns,M)?this.sort(this.getColumn(M),S):this.errorMessage('Sort column: The column "'+M+'" was not found.')},this.sortDataByColumn=function(M,{data:S=this.data,order:T='asc',changeOriginal:C=!1}={}){return S!==this.data||'undefined'!=typeof M&&this.has(this.columns,M)?S===this.data||this.has(S[0],M)?function(N,I,V,D){return I.sort(function(F,P){return D.isNumeric(F[N])&&D.isNumeric(P[N])?('asc'===V?1:-1)*(F[N]-P[N]):0})}(M,C?S:this.deepCopy(S),T,this):this.errorMessage('Sort data by column: The column "'+M+'" does not exist.'):this.errorMessage('Sort data by column: No column was specified or this column does not exist.')},this.sort=function(M,S='asc'){return'undefined'==typeof M?this.errorMessage('Sort: No values given.'):M.constructor!==Array||0===M.length?this.errorMessage('Sort: No array or an empty array of values was given.'):function(T,C,N){return T.sort((I,V)=>{return C.isNumeric(I)&&C.isNumeric(V)?('asc'===N?1:-1)*(I-V):0})}(this.deepCopy(M),this,S)},this.getUniqueValues=function(M){if('undefined'==typeof M)return this.errorMessage('Get unique values: No values given.');let S=this.validateInput(M,'nominal','get unique values');return!1===S?void 0:this.sort(S.data.filter((T,C)=>{return S.data.indexOf(T)===C}))},this.reduceToPairs=function(M,S){if('undefined'==typeof S||'undefined'==typeof M)return this.errorMessage('This method requires two variables to be compared.');let T=this.validateInput(M,'nominal');if(!1!==T){let C=this.validateInput(S,'nominal');if(!1!==C){let N=T.length>=C.length?T.length:C.length,I=[],V=[],D=[],F='string'==typeof M?M:'first',P='string'==typeof S?S:'second';for(var B=0;B=this.validScales.indexOf(S)};return this.errorMessage=function(M){if(!this.suppressWarnings)try{throw new TypeError('string'==typeof M?M:M.message)}catch(S){console.error('statistics.js: '+S.message)}},this.has=function(M,S){var T=Object.prototype.hasOwnProperty;return T.call(M,S)},this.deepCopy=function(M){let S=Array.isArray(M)?[]:{};for(let T in M){let C=M[T];S[T]='object'==typeof C?this.deepCopy(C):C}return S},Statistics.prototype.assignRanks=function(M,{data:S=this.data,order:T='asc',handleTiedValues:C='mean',returnFrequencies:N=!1}={}){if('undefined'==typeof M)return this.errorMessage('Assign ranks: You need to specify a column to be ranked.');let I=this.deepCopy(this.sortDataByColumn(M,{data:S,order:T,changeOriginal:!1})),V={};for(var D=0;DA+D+1)),O=B[_Mathfloor(Math.random()*B.length)],B.splice(B.indexOf(O),1)),P==V[U]&&(P=0),I[D]['rank-'+M]=O}return N?{data:I,frequencies:V}:I},Statistics.prototype.contingencyTable=function(M,S){if('undefined'==typeof S)return this.errorMessage('A contingency table requires two columns to analyze.');if(!this.has(this.columns,M))return this.errorMessage('There is no variable "'+M+'" defined.');if(!this.has(this.columns,S))return this.errorMessage('There is no variable "'+S+'" defined.');let T=this.getScale(M),C=this.getScale(S);if('nominal'!==T&&'ordinal'!==T||'nominal'!==C&&'ordinal'!==C)return this.errorMessage('Both variables need to be nominal for. They are '+T+' and '+C+'.');let N=this.getValueMap(M),I=this.getValueMap(S);if('undefined'==typeof N||'undefined'==typeof I)return this.errorMessage('Contingency table: There are no valid values.');let V={total:{total:0}},D=this.data;for(var F=0;F=N.length&&2>=I.length&&(P.a=V[N[0]][I[0]]||0,P.b=V[N[0]][I[1]]||0,P.c=V[N[1]][I[0]]||0,P.d=V[N[1]][I[1]]||0),P},this.showData=function(M){if('string'==typeof M&&this.has(this.columns,M))'ordinal'===this.getScale(M)?console.log(this.applyValueMap(M)):console.log(this.getColumn(M));else if('undefined'==typeof M){let C=this.valueMaps,N=this.data;if('undefined'!=typeof C)for(var S in C)if(this.has(C,S)&&'undefined'!=typeof C[S]&&'ordinal'===this.getScale(S))for(var T=0;T=F||0>=P)return this.errorMessage('Scotter plot: The number of line to plot must be larger than 0.');let G=typeof M[0];if('object'==G&&(!T||!C))return this.errorMessage('Scatter plot: The variables for the x and y axes need to be supplied.');let A={},L=Infinity,E=-Infinity,W=Infinity,K=-Infinity,Y=0;for(var X=0;XE&&(E=me),meK&&(K=ce),ceL/E&&(L=0),0.1>W/K&&(W=0),null===S&&(S=document.createElement('canvas'));let Q=S.getContext('2d'),H=N?0.1*N:0.1*(E-L);for(var J in N=(N?N-2*H:E-L)+2*V,I=(I?I-2*H:K-W)+2*V,400>N&&(N=400),400>I&&(I=400),40>H&&(H=40),S.width=N,S.height=I,Q.fillStyle='transparent',Q.fillRect(0,0,N,I),Q.fillStyle=U,A)for(var Z=0;Zae-0.5*H||re<0.5*H||($(ee,re,te-0.4*H,re,te-0.6*H),D&&$(ee,re,te-0.6*H,re,0.5*H,O),ee.fillText(se,re,te-0.2*H));let de=(K-W)/P,le=1<=de?_Mathpow(10,parseInt(de).toString().length-1):0.01;if(de=_Mathround(de/le)*le,se=0,0te-0.5*H||oe<0.5*H||($(ee,0.4*H,oe,0.6*H,oe),D&&$(ee,0.6*H,oe,N+H-2*V,oe,O),ee.fillText(se,0.2*H,oe));return $(ee,0.5*H,te-0.5*H,ae-H,te-0.5*H),$(ee,0.5*H,te-0.5*H,0.5*H,0.5*H),ee.drawImage(S,0.5*H-0.5*V,1.5*H-1.5*V),Q.drawImage(_,0,0,N,I),S},this.init(o,l,u)};'undefined'==typeof exports?window.Statistics=Statistics:('undefined'!=typeof module&&module.exports&&(exports=module.exports=Statistics),exports.Statistics=Statistics),Statistics.prototype.mean=function(o){return'undefined'==typeof o?void 0:this.arithmeticMean(o)},Statistics.prototype.arithmeticMean=function(o){if('undefined'==typeof o)return this.errorMessage('Arithmetic mean: No data was supplied.');if('string'==typeof o&&!1===this.checkLastUpdated(o,'mean'))return this.getStatistics(o,'mean');let l=this.validateInput(o,'interval','arithmetic mean');if(!1!==l){let u=this.sumExact(l.data)/l.length;return'string'==typeof o&&this.updateStatistics(o,'mean',u),u}},Statistics.prototype.geometricMean=function(o){if('undefined'==typeof o)return this.errorMessage('Geometric mean: No data was supplied.');if('string'==typeof o&&!1===this.checkLastUpdated(o,'geometricMean'))return this.getStatistics(o,'geometricMean');let l=this.validateInput(o,'metric','geometric mean');if(!1===l)return;let u,g=!1;return u=l.data.reduce((h,M)=>{return 0{return 0>S?(g=!0,0):0===S?(u=!0,0):M+1/S},0);return h=u?0:l.length/h,h=g?void 0:h,'string'==typeof o&&this.updateStatistics(o,'harmonicMean',h),h},Statistics.prototype.rootMeanSquare=function(o){if('undefined'==typeof o)return this.errorMessage('Root mean square: No data was supplied.');if('string'==typeof o&&!1===this.checkLastUpdated(o,'rootMeanSquare'))return this.getStatistics(o,'rootMeanSquare');let l=this.validateInput(o,'interval','root mean square');if(!1!==l){let u=_Mathsqrt(l.data.reduce((g,h)=>{return g+h*h},0)/l.length);return'string'==typeof o&&this.updateStatistics(o,'rootMeanSquare',u),u}},Statistics.prototype.cubicMean=function(o){if('undefined'==typeof o)return this.errorMessage('Cubic mean: No data was supplied.');if('string'==typeof o&&!1===this.checkLastUpdated(o,'cubicMean'))return this.getStatistics(o,'cubicMean');let l=this.validateInput(o,'interval','cubic mean');if(!1===l)return;let u=l.data.reduce((g,h)=>{return g+h*h*h},0);return u=0<=u&&0l||0.5l||0.5u||0.5M?(h=[T],M=g[T]):g[T]===M&&h.push(T)}return 1===h.length&&(h=h[0]),'string'==typeof o&&this.updateStatistics(o,'mode',h),h},Statistics.prototype.minimum=function(o){if('undefined'==typeof o)return this.errorMessage('Minimum: No data was supplied.');if('string'==typeof o&&!1===this.checkLastUpdated(o,'minimum'))return this.getStatistics(o,'minimum');let l=this.validateInput(o,'ordinal','minimum');if(!1===l)return;let u=Infinity,g=l.length;for(;g--;)l.data[g]u&&(u=l.data[g]);return'string'==typeof o&&this.updateStatistics(o,'maximum',u),u},Statistics.prototype.range=function(o){if('undefined'==typeof o)return this.errorMessage('Range: No data was supplied.');if('string'==typeof o&&!1===this.checkLastUpdated(o,'range'))return this.getStatistics(o,'range');let l,u;if('string'==typeof o&&'nominal'===this.getScale(o)){if(l=this.validateInput(o,'nominal','range'),!1===l)return;u=this.getUniqueValues(l.data)}else{if(l=this.validateInput(o,'ordinal','range'),!1===l)return;let g=this.sort(l.data);u=g[l.length-1]-g[0]}return'string'==typeof o&&this.updateStatistics(o,'range',u),u},Statistics.prototype.variance=function(o,l=!0){if('undefined'==typeof o)return this.errorMessage('Variance: No data was supplied.');if('string'==typeof o&&!1===this.checkLastUpdated(o,'variance'))return this.getStatistics(o,'variance');let u=this.validateInput(o,'interval','variance');if(!1===u)return;if(2>u.length)return this.errorMessage('The data supplied to compute variance needs to contain at least two datasets.');let g=0;if(this.isNumeric(l)){for(var h=0;hu.length)return this.errorMessage('The data supplied to compute standardDeviation needs to contain at least two datasets.');let g=_Mathsqrt(this.variance(o,l));return'string'==typeof o&&this.updateStatistics(o,'standardDeviation',g),g}},Statistics.prototype.coefficientOfVariation=function(o,l){if('undefined'==typeof o)return this.errorMessage('Coefficient of variation: No data was supplied.');let u=this.validateInput(o,'interval','coefficient of variation');if(!1!==u){let g=this.standardDeviation(u.data),h='undefined'==typeof l?this.mean(u.data):l;return'undefined'!=typeof g&&'undefined'!=typeof h&&0!==h?g/h:void 0}},Statistics.prototype.indexOfDispersion=function(o,l){if('undefined'==typeof o)return this.errorMessage('Index of dispersion: No data was supplied.');let u=this.validateInput(o,'interval','coefficient of variation');if(!1!==u){let g=this.variance(u.data),h='undefined'==typeof l?this.mean(u.data):l;return'undefined'!=typeof g&&'undefined'!=typeof h&&0!==h?g/h:void 0}},Statistics.prototype.geometricStandardDeviation=function(o){if('undefined'==typeof o)return this.errorMessage('Geomtric standard deviation: No data was supplied.');if('string'==typeof o&&!1===this.checkLastUpdated(o,'geometricStandardDeviation'))return this.getStatistics(o,'geometricStandardDeviation');let l=this.validateInput(o,'interval','geometric standard deviation');if(!1===l)return;let u=l.length,g=this.geometricMean(o),h=0,M=!1;for(var S=0;S=l.data[S]?M=!0:h+=_Mathpow(_Mathlog(l.data[S]/g),2);return h=M?void 0:_Mathexp(_Mathsqrt(h/u)),'string'==typeof o&&this.updateStatistics(o,'geometricStandardDeviation',h),h},Statistics.prototype.medianAbsoluteDeviation=function(o){if('undefined'==typeof o)return this.errorMessage('Median absolute deviation: No data was supplied.');let l=this.validateInput(o,'interval','median absolute deviation');if(!1===l)return;let u=l.data,g=this.median(u);if('undefined'!=typeof g)return u=u.map((h)=>{return _Mathabs(h-g)}),this.median(u)},Statistics.prototype.cumulativeFrequency=function(o,l){if('undefined'==typeof o)return this.errorMessage('Cumulative frequency: No data was supplied.');if('undefined'==typeof l||!this.isNumeric(l))return void this.errorMessage('You need to specify a boundary for the cumulative frequency analysis that is either an integer or a floating point number.');let u=this.validateInput(o,'ordinal','cumulative frequency analysis');if(!1!==u){let g=this.sort(u.data),h=0;for(;l>=g[h];)h++;return h}},Statistics.prototype.frequencies=function(o){if('undefined'==typeof o)return this.errorMessage('Frequencies: No data was supplied.');let l=this.validateInput(o,'nominal','frequencies');if(!1===l)return;let u=l.data,g={},h=[];for(var M=0;M{return g[T]-g[S]}),h=h.map((S)=>{return{value:S,absolute:g[S],relative:g[S]/l.length}}),h},Statistics.prototype.quantile=function(o,l=0.5){if('undefined'==typeof o)return this.errorMessage('Quantile: No data was supplied.');if(!this.isNumeric(l)||0>l||1u.length)return;let g=this.mean(o),h=this.standardDeviation(o,!1),M=0;for(var S=0;Sl.length)return;let u=this.mean(o),g=this.standardDeviation(o),h=0;for(var M=0;M{return this.isNumeric(u)?l+u:l},0))},Statistics.prototype.sumExact=function(o){if('undefined'==typeof o)return this.errorMessage('Sum exact: No data given.');if('string'==typeof o&&this.has(this.columns,o)&&(o=this.getColumn(o)),0==o.length)return;let l=0,u=0;for(var g=0;g{return this.isNumeric(u)?l*u:l},1))},Statistics.prototype.factorial=function(o){return this.isNumeric(o)&&!_NumberisInteger(o)?this.gamma(o):'undefined'==typeof this.factorials[o]?this.computeFactorial(o):this.factorials[o]},Statistics.prototype.computeFactorial=function(o){if('undefined'==typeof o||!this.isNumeric(o)||0>o)return;if('undefined'!=typeof this.factorials[o])return this.factorials[o];if(!_NumberisInteger(o))return this.gamma(o);let l=1,u=1;for(;o>u;)u++,l*=u,'undefined'==typeof this.factorials[u]&&(this.factorials[u]=l);return l},Statistics.prototype.binomialCoefficient=function(o=1,l=1){if(ol)return this.errorMessage('The binomial coefficient is only defined for n and k with n \u2265 k \u2265 0. N is '+o+' and k is '+l+'.');if(!_NumberisInteger(o)||!_NumberisInteger(l))return this.errorMessage('The binomial coefficient is only defined for integers n and k.');let u=[];for(var g=1;g<=l;g++)u.push((o+1-g)/g);return this.product(u)},Statistics.prototype.gamma=function(o,l=!1){return'undefined'==typeof o||!this.isNumeric(o)||0>o?void 0:_NumberisInteger(o)&&'undefined'!=typeof this.factorials[o-1]?this.factorials[o-1]:l?this.gammaSpouge(o):this.gammaStirling(o)},Statistics.prototype.gammaSpouge=function(o){if('undefined'==typeof o||!this.isNumeric(o)||0>o)return;if(_NumberisInteger(o)&&'undefined'!=typeof this.factorials[o-1])return this.factorials[o-1];const l=this.spougeConstant;let u=_Mathpow(o+l,o+0.5),g=1,h=0,M=_Mathsqrt(2*_MathPI);u*=_Mathexp(-o-l),u/=o;for(var S=1;So)return;if(_NumberisInteger(o)&&'undefined'!=typeof this.factorials[o-1])return this.factorials[o-1];let g=1/(10*o);return g=1/(12*o-g),g=(g+o)*0.36787944117144233,g=_Mathpow(g,o),g*=_Mathsqrt(6.283185307179586/o),g},Statistics.prototype.incompleteGamma=function(o,l){if('undefined'==typeof l)return this.errorMessage('The incomplete lower gamma function is only defined for two numeric variables s and x.');if(!this.isNumeric(o)||!this.isNumeric(l))return this.errorMessage('The incomplete lower gamma function is only defined for numeric variables s and x.');if(0>l)return this.errorMessage('The incomplete lower gamma function is defined for x > 0.');let u=this.incompleteGammaIterations,g=1;for(var h=0;hl?this.errorMessage('The regularised lower gamma function is defined for x > 0.'):this.incompleteGamma(o,l)/this.gamma(o,!0):this.errorMessage('The regularised lower gamma function is only defined for numeric variables s and x.')},Statistics.prototype.beta=function(o,l){return'undefined'==typeof l?this.errorMessage('The beta function is only defined for two numeric variables a and b.'):this.isNumeric(o)&&this.isNumeric(l)?0>=o||0>=l?this.errorMessage('The beta function is defined for a and b with a > 0 and b > 0.'):_NumberisInteger(o)&&_NumberisInteger(l)&&0o||1=l||0>=u)return this.errorMessage('The incomplete beta function is defined for a and b with a > 0 and b > 0.');if(1==o)return this.beta(l,u);var g=function(N,I,V,D){if(0==N%2){let F=0.5*N;return F*(V-F)*D/((I+2*F-1)*(I+2*F))}let F=0.5*N-0.5;return-(I+F)*(I+V+F)*D/((I+2*F)*(I+2*F+1))};let h=_Mathpow(o,l)*_Mathpow(1-o,u)/l,M=this.incompleteBetaIterations,S=1;for(var T=0;To)return this.errorMessage('The regularised beta function is defined for x \u2265 0 and x \u2266 1.');if(0>=l||0>=u)return this.errorMessage('The regularised beta function is defined for a and b with a > 0 and b > 0.');if(!_NumberisInteger(l)||!_NumberisInteger(u))return this.incompleteBeta(o,l,u)/this.beta(l,u);let g=this.epsilon+1,h=l,M=0;for(;g>=this.epsilon;)g=this.binomialCoefficient(u+h-1,h)*_Mathpow(o,h),M+=g,h++;return M*_Mathpow(1-o,u)},Statistics.prototype.covariance=function(o,l,u=!0){if('undefined'==typeof l)return this.errorMessage('Covariance requires two variables to be compared.');let g=this.validateInput(o,'interval','covariance');if(!1===g)return;let h=this.validateInput(l,'interval','covariance');if(!1===h)return;let M=this.reduceToPairs(g.data,h.data);if(0===M.length)return;let S=this.mean(M.valuesFirst),T=this.mean(M.valuesSecond),C=0;for(var N=0;N=g.length?u.length:g.length,M=u.data,S=g.data,T=[],C=0,N=0;for(var I=0;I{return O+_Mathpow(R['rank-'+o]-R['rank-'+l],2)},0),V=6*I;if(u){let O=Object.values(C).reduce((L,E)=>{return L+_Mathpow(E,3)-E}),R=Object.values(N).reduce((L,E)=>{return L+_Mathpow(E,3)-E}),G=_Mathpow(S,3)-S-0.5*O-0.5*R-V,A=_Mathsqrt((_Mathpow(S,3)-S-O)*(_Mathpow(S,3)-S-R));V=G/A}else V=1-V/(_Mathpow(S,3)-S);let D=S-2,F=_Mathsqrt((D-1)/1.06)*this.fisherTransformation(V),P=1-this.normalCumulativeValue(_Mathabs(F)),B=V*_Mathsqrt(D/(1-V*V)),U=1-this.studentsTCumulativeValue(_Mathabs(B),D);return{rho:V,significanceNormal:{zScore:F,pOneTailed:P,pTwoTailed:2*P},significanceStudent:{degreesOfFreedom:D,tStatistic:B,pOneTailed:U,pTwoTailed:2*U},missings:M.missings}},Statistics.prototype.kendallsTau=function(o,l){if('undefined'==typeof l)return this.errorMessage('Kendall\'s Tau requires two columns to analyze.');let u=this.validateInput(o,'ordinal','Kendall\'s Tau');if(!1===u)return;let g=this.validateInput(l,'ordinal','Kendall\'s Tau');if(!1===g)return;let h=this.reduceToPairs(o,l),M=h.length,S=h.valuesCombined,T=0,C=0,N={},I={},V=0;for(var D=0;Do||1=u)return this.errorMessage('Normal probability density: variance must be larger than 0.');let g=-_Mathpow(o-l,2)/(2*u);return _Mathexp(g)/_Mathsqrt(2*_MathPI*u)},Statistics.prototype.normalDistribution=function(o=0,l=1){if(0>=l)return this.errorMessage('Normal distribution: variance must be larger than 0.');let u=0,g=1,h={};for(;g>=this.epsilon&&(g=this.normalProbabilityDensity(o+u,o,l),!(g=l;l+=0.01){let u=_Mathround(100*l)/100;o[u.toFixed(2)]=this.normalCumulativeValue(l)}this.zTable=o}return o},Statistics.prototype.binomialProbabilityMass=function(o,l=10,u=0.5){return'undefined'==typeof o?this.errorMessage('Binomial probability mass: the required argument k was not given.'):0>o||!_NumberisInteger(o)?this.errorMessage('Binomial probability mass: k must be a non-negative integer.'):0>l||!_NumberisInteger(l)?this.errorMessage('Binomial probability mass: n must be a non-negative integer.'):0>u||1o||!_NumberisInteger(o))return this.errorMessage('Binomial distribution: n must be a non-negative integer.');if(0>l||1o||!_NumberisInteger(o)?this.errorMessage('Binomial cumulative distribution value: k must be a non-negative integer.'):0>l||!_NumberisInteger(l)?this.errorMessage('Binomial cumulative distribution value: n must be a non-negative integer.'):0>u||1o||!_NumberisInteger(o))return this.errorMessage('Binomial cumulative distribution: n must be a non-negative integer.');if(0>l||1o||0>=l)return this.errorMessage('Poisson probability mass: Both k and lambda must be larger than 0.');if(10=o)return this.errorMessage('Poisson distribution: Lambda must be larger than 0.');let l=0,u=0,g=[];for(;u<1-this.epsilon;){let h=this.poissonProbabilityMass(l,o);g.push(h),u+=h,l++}return g},Statistics.prototype.poissonCumulativeValue=function(o,l=1){if('undefined'==typeof o||!_NumberisInteger(o))return this.errorMessage('Poisson cumulative distribution: The number of cumulative events k must be supplied.');if(0>o||0>=l)return this.errorMessage('Poisson distribution: Both k and lambda must be larger than 0.');let u=this.poissonDistribution(l);return o=o)return this.errorMessage('Poisson distribution: lambda must be larger than 0.');let l=this.poissonDistribution(o),u=0;return l.map(function(g){return u=this.sumExact([u,g]),u},this)},Statistics.prototype.studentsTProbabilityDensity=function(o,l){if('undefined'==typeof l)return this.errorMessage('Student\'s t-distribution probability density: no value for degrees of freedom (df) given.');if(0>=l)return this.errorMessage('Student\'s t-distribution probability density: degrees of freedom (df) must be larger than 0.');let u=_Mathpow(1+o*o/l,-0.5*(l+1));return u/=_Mathsqrt(l)*this.beta(0.5,0.5*l),u},Statistics.prototype.studentsTDistribution=function(o){if('undefined'==typeof o)return this.errorMessage('Student\'s t-distribution: no value for degrees of freedom (df) given.');if(0>=o)return this.errorMessage('Student\'s t-distribution: degrees of freedom (df) must be larger than 0.');let l=0,u=1,g={};for(;u>=this.epsilon&&(u=this.studentsTProbabilityDensity(l,o),!(u=l?this.errorMessage('Student\'s cumulative t-distribution value: degrees of freedom (df) must be larger than 0.'):0>=o?0.5*this.regularisedBeta(l/(o*o+l),0.5*l,0.5):0.5+0.5*this.regularisedBeta(o*o/(o*o+l),0.5,0.5*l)},Statistics.prototype.studentsTCumulativeDistribution=function(o){if('undefined'==typeof o)return this.errorMessage('Student\'s cumulative t-distribution: no value for degrees of freedom (df) given.');if(0>=o)return this.errorMessage('Student\'s cumulative t-distribution: degrees of freedom (df) must be larger than 0.');let l=0,u=0,g=-0.1,h={};for(;u<=1-this.epsilon&&(u=this.studentsTCumulativeValue(l,o),!(g>=u));)h[l.toFixed(2)]=u,h[(-l).toFixed(2)]=u,g=u,l+=0.01;return h},Statistics.prototype.chiSquaredProbabilityDensity=function(o,l){return'undefined'==typeof l?this.errorMessage('Chi squared distribution probability density: no value for degrees of freedom (df) given.'):0>=l?this.errorMessage('Chi squared distribution probability density: degrees of freedom (df) must be larger than 0.'):0>=o?0:_Mathpow(o,0.5*l-1)*_Mathexp(-0.5*o)/(_Mathpow(2,0.5*l)*this.gamma(0.5*l,!0))},Statistics.prototype.chiSquaredDistribution=function(o){if('undefined'==typeof o)return this.errorMessage('Chi squared distribution: no value for degrees of freedom (df) given.');if(0>=o)return this.errorMessage('Chi squared distribution: degrees of freedom (df) must be larger than 0.');let l=0.01,u=1,g={'0.00':0};for(;(2>=o&&u>=this.epsilon||2=this.epsilon)&&(u=this.chiSquaredProbabilityDensity(l,o),!(u=o-2&&2=l?this.errorMessage('Chi squared cumulative distribution value: degrees of freedom (df) must be larger than 0.'):0>=o?0:this.regularisedGamma(0.5*l,0.5*o)},Statistics.prototype.chiSquaredCumulativeDistribution=function(o){if('undefined'==typeof o)return this.errorMessage('Chi squared cumulative distribution: no value for degrees of freedom (df) given.');if(0>=o)return this.errorMessage('Chi squared cumulative distribution: degrees of freedom (df) must be larger than 0.');let l=0.01,u=0,g={'0.00':0};for(;u<=1-this.epsilon&&(u=this.chiSquaredCumulativeValue(l,o),!(u>=1-this.epsilon));)0this.maxBarnardsN)return this.errorMessage('Barnard\'s test is a resource-intensive method, relative to the total number of datasets to analyze. There are '+T+' datasets in the supplied data, exceeding the maxinum of '+this.maxBarnardsN+'. You can change this number by changing the "maxBarnardsN" option (be cautious).');var C=(g+h)/T;C=C*(1-C)*(1/(g+M)+1/(h+S)),C=(h/(h+S)-g/(g+M))/_Mathsqrt(C),isNaN(C)&&(C=0);let N=[];for(var I=0;1>I;I=this.sumExact([I,1e-3])){let F=0;for(var V=0;V<=g+M;V++)for(var D=0;D<=h+S;D++){let P=(V+D)/T;if(P=P*(1-P)*(1/(g+M)+1/(h+S)),P=(V/(g+M)-D/(h+S))/_Mathsqrt(P),!isNaN(P)&&_Mathabs(P)>=_Mathabs(C)){let B=this.binomialCoefficient(g+M,V)*this.binomialCoefficient(h+S,D);B*=_Mathpow(I,V+D)*_Mathpow(1-I,T-V-D),F+=isNaN(B)?0:B}}N.push({nuisance:I,significance:F})}return N=this.sortDataByColumn('significance',{data:N,order:'desc'}),{wald:C,nuisance:N[0].nuisance,pOneTailed:0.5*N[0].significance,pTwoTailed:N[0].significance}},Statistics.prototype.binomialTest=function(o,l,u=0.5){if('undefined'==typeof l)return this.errorMessage('Binomial test requires the data to test and a value which is hypotethised to be observed with a probability of alpha.');if(0>u||1M.indexOf(l))return this.errorMessage('The value "'+l+'" was not found in the supplied data.');let S=h.data.filter(function(I){return I===l}).length,T=this.binomialProbabilityMass(S,h.length,u),C=this.binomialCumulativeValue(S-1,h.length,u),N=1-C-T;return{pExactly:T,pFewer:C,pAtMost:C+T,pMore:N,pAtLeast:N+T}}},Statistics.prototype.signTest=function(o,l){if('undefined'==typeof l)return this.errorMessage('Sign test: No data was supplied.');let u=this.validateInput(o,'ordinal','sign test'),g=this.validateInput(l,'ordinal','sign test');if(!1===u||!1===g)return;if(0===u.length||0===g.length)return;let h=this.reduceToPairs(u.data,g.data),M=h.missings,S=h.length,T=h.valuesFirst,C=h.valuesSecond,N=0;for(var I=0;IC[I]&&(N+=1);let V=this.binomialProbabilityMass(N,S),D=this.binomialCumulativeValue(N-1,S),F=1-D-V;return{positives:N,pExactly:V,pFewer:D,pAtMost:D+V,pMore:F,pAtLeast:F+V}},Statistics.prototype.fishersExactTest=function(o,l){if('undefined'==typeof l)return this.errorMessage('Fisher\'s exact test requires two columns to analyze.');let u=this.contingencyTable(o,l,'Fisher\'s exact test');if('undefined'!=typeof u){let{a:g,b:h,c:M,d:S}=u,T=this.binomialCoefficient(g+h,g)*this.binomialCoefficient(M+S,M)/this.binomialCoefficient(g+h+M+S,g+M);return T}},Statistics.prototype.mannWhitneyU=function(o,l){if('undefined'==typeof l)return this.errorMessage('Mann-Whitney-U test requires two columns to analyze.');let u=this.validateInput(o,'nominal','Mann-Whitney-U test'),g=this.validateInput(l,'ordinal','Mann-Whitney-U test');if(!1===u||!1===g)return;if(0===u.length||0===g.length)return;let h=this.sort(g.data),M=this.getUniqueValues(u.data);if(2!==M.length)return this.errorMessage('The Mann-Whitney-U test requires the independent variable to have exactly two unique values. Variable "'+independentValue+'" has '+M.length+' different values: '+u.data.join(', '));let S=this.assignRanks(l),T=S.reduce((O,R)=>{return R[o]===M[0]?O+R['rank-'+l]:O},0),C=S.reduce((O,R)=>{return R[o]===M[0]?O+1:O},0),N=S.reduce((O,R)=>{return R[o]===M[1]?O+R['rank-'+l]:O},0),I=S.reduce((O,R)=>{return R[o]===M[1]?O+1:O},0),F=Math.min(C*(0.5*C+I+0.5)-T,I*(0.5*I+C+0.5)-N),P=(F-0.5*C*I)/_Mathsqrt(C*I*(C+I+1)/12),B=1-this.normalCumulativeValue(_Mathabs(P));return{MannWhitneyU:F,zScore:P,pOneTailed:B,pTwoTailed:2*B}},Statistics.prototype.chiSquaredTest=function(o,l){if('undefined'==typeof l)return this.errorMessage('Chi Squared Test: You need to specify two variables, either of nominal or ordinal scale.');let u=this.getScale(o),g=this.getScale(l);if('ordinal'!==u&&'nominal'!==u||'ordinal'!==g&&'nominal'!==g)return this.errorMessage('Chi Squared Test: Both variables need to be either of nominal or ordinal scale.');let h=this.contingencyTable(o,l);if('undefined'==typeof h)return this.errorMessage('Chi Squared Test: Failed to create a contingency table. Please make sure your data is prepared correctly.');h=h.detailled;let M=h.total.total,S=0,T=Object.keys(h).length-1;for(var C in T=(T-1)*(Object.keys(h.total).length-T-2),h)if(this.has(h,C)&&'total'!=C){let V=h[C].total;for(var N in h[C])if(this.has(h[C],N)&&'total'!=N){let D=h.total[N],F=V*D/M;S+=_Mathpow(h[C][N]-F,2)/F}}let I;return I=0>S||1>T?0:1-this.chiSquaredCumulativeValue(S,T),{PearsonChiSquared:S,degreesOfFreedom:T,significance:I}},Statistics.prototype.studentsTTestOneSample=function(o,l){if('undefined'==typeof l||!this.isNumeric(l))return this.errorMessage('Student\'s t-test (one sample) requires data and the mean for which the null hypothesis should hold true.');let u=this.validateInput(o,'interval','student\'s t-test (one sample)');if(!1===u)return;if(0===u.length)return;let g=this.mean(u.data),h=this.standardDeviation(u.data),M=_Mathsqrt(u.length)*(g-l)/h,S=u.length-1,T=this.studentsTCumulativeValue(_Mathabs(M),S);return 0.5o)return this.errorMessage('Inverse Gaussian Error Function: x can not be smaller than -1.');var u,l=-_Mathlog((1-o)*(1+o));return 5>l?(l-=2.5,u=+3.43273939e-7+ +2.81022636e-8*l,u=+-3.5233877E-6+u*l,u=+-4.39150654E-6+u*l,u=2.1858087e-4+u*l,u=-0.00125372503+u*l,u=-0.00417768164+u*l,u=0.246640727+u*l,u=1.50140941+u*l):(l=_Mathsqrt(l)-3,u=1.00950558e-4-2.00214257e-4*l,u=0.00134934322+u*l,u=-0.00367342844+u*l,u=0.00573950773+u*l,u=-0.0076224613+u*l,u=0.00943887047+u*l,u=1.00167406+u*l,u=2.83297682+u*l),(u*o).toFixed(7)},Statistics.prototype.probit=function(o){return'undefined'!=typeof o&&this.isNumeric(o)?0>o||1l)return this.errorMessage('Xorshift: startIndex must be a non-negative integer.');const u=4,g=64;let h=0,M=u,S=new Uint32Array(g);S[0]=o[0],S[1]=o[1],S[2]=o[2],S[3]=o[3];const T=function(I,V,D){for(var F=V;F>>0;I[0]=I[1],I[1]=I[2],I[2]=I[3],I[3]=((I[3]^I[3]>>>19)>>>0^(P^P>>>8)>>>0)>>>0,I[F]=I[3]}return I};this.next=function(I=!0){let V=S[M];return h+=1,M++>=g&&(M=u,T(S,u,g)),I?V/4294967296:V},S=T(S,u,g);for(var C=0;C=h||1<=h)&&50>S);do M=g(),T+=1;while((0>=M||1<=M)&&50>T);for(;0>=h||1<=h;)h=Math.random();for(;0>=M||1<=M;)M=Math.random();let C=_Mathsqrt(-2*_Mathlog(h))*Math.cos(2*_MathPI*M);return C*l+o},Statistics.prototype.ziggurat=function(o=0,l=1){let u=123456789,g=[128],h=[128],M=[128];const S=function(I,V){let P,B,D=3.442619855899,F=1/D;for(;;){if(P=I*g[V],0===V){for(P=-_Mathlog(C())*F,B=-_Mathlog(C());B+B>>17,V^=V<<5,u=V,0|I+V},C=function(){return 0.5*(1+T()/-2147483648)};(function(){u^=new Date().getTime();let I=2147483648,V=3.442619855899,D=V,F=0.00991256303526217,P=F/_Mathexp(-0.5*V*V);M[0]=_Mathfloor(V*I/P),M[1]=0,g[0]=P/I,g[127]=V/I,h[0]=1,h[127]=_Mathexp(-0.5*V*V);for(var B=126;1<=B;B--)V=_Mathsqrt(-2*_Mathlog(F/V+_Mathexp(-0.5*V*V))),M[B+1]=_Mathfloor(V*I/D),D=V,h[B]=_Mathexp(-0.5*V*V),g[B]=V/I})(),this.next=function(){let I=T(),V=127&I,D=_Mathabs(I)