(function(window,factory){if(typeof exports==="object"){module.exports=factory()}else if(typeof define==="function"&&define.amd){define(factory)}else{window.jStat=factory()}})(this,function(){var jStat=function(Math,undefined){var concat=Array.prototype.concat;var slice=Array.prototype.slice;var toString=Object.prototype.toString;function calcRdx(n,m){var val=n>m?n:m;return Math.pow(10,17-~~(Math.log(val>0?val:-val)*Math.LOG10E))}var isArray=Array.isArray||function isArray(arg){return toString.call(arg)==="[object Array]"};function isFunction(arg){return toString.call(arg)==="[object Function]"}function isNumber(num){return typeof num==="number"?num-num===0:false}function toVector(arr){return concat.apply([],arr)}function jStat(){return new jStat._init(arguments)}jStat.fn=jStat.prototype;jStat._init=function _init(args){if(isArray(args[0])){if(isArray(args[0][0])){if(isFunction(args[1]))args[0]=jStat.map(args[0],args[1]);for(var i=0;i=0;nrow--,i++)res[i]=[arr[i][nrow]];return res};jStat.transpose=function transpose(arr){var obj=[];var objArr,rows,cols,j,i;if(!isArray(arr[0]))arr=[arr];rows=arr.length;cols=arr[0].length;for(i=0;i0)res[row][0]=arr[row][0];for(col=1;colend&&step>0){return[]}if(step>0){for(i=start;iend;i+=step){rl.push(i)}}return rl};jStat.slice=function(){function _slice(list,start,end,step){var i;var rl=[];var length=list.length;if(start===undefined&&end===undefined&&step===undefined){return jStat.copy(list)}start=start||0;end=end||list.length;start=start>=0?start:length+start;end=end>=0?end:length+end;step=step||1;if(start===end||step===0){return[]}if(startend&&step>0){return[]}if(step>0){for(i=start;iend;i+=step){rl.push(list[i])}}return rl}function slice(list,rcSlice){var colSlice,rowSlice;rcSlice=rcSlice||{};if(isNumber(rcSlice.row)){if(isNumber(rcSlice.col))return list[rcSlice.row][rcSlice.col];var row=jStat.rowa(list,rcSlice.row);colSlice=rcSlice.col||{};return _slice(row,colSlice.start,colSlice.end,colSlice.step)}if(isNumber(rcSlice.col)){var col=jStat.cola(list,rcSlice.col);rowSlice=rcSlice.row||{};return _slice(col,rowSlice.start,rowSlice.end,rowSlice.step)}rowSlice=rcSlice.row||{};colSlice=rcSlice.col||{};var rows=_slice(list,rowSlice.start,rowSlice.end,rowSlice.step);return rows.map(function(row){return _slice(row,colSlice.start,colSlice.end,colSlice.step)})}return slice}();jStat.sliceAssign=function sliceAssign(A,rcSlice,B){var nl,ml;if(isNumber(rcSlice.row)){if(isNumber(rcSlice.col))return A[rcSlice.row][rcSlice.col]=B;rcSlice.col=rcSlice.col||{};rcSlice.col.start=rcSlice.col.start||0;rcSlice.col.end=rcSlice.col.end||A[0].length;rcSlice.col.step=rcSlice.col.step||1;nl=jStat.arange(rcSlice.col.start,Math.min(A.length,rcSlice.col.end),rcSlice.col.step);var m=rcSlice.row;nl.forEach(function(n,i){A[m][n]=B[i]});return A}if(isNumber(rcSlice.col)){rcSlice.row=rcSlice.row||{};rcSlice.row.start=rcSlice.row.start||0;rcSlice.row.end=rcSlice.row.end||A.length;rcSlice.row.step=rcSlice.row.step||1;ml=jStat.arange(rcSlice.row.start,Math.min(A[0].length,rcSlice.row.end),rcSlice.row.step);var n=rcSlice.col;ml.forEach(function(m,j){A[m][n]=B[j]});return A}if(B[0].length===undefined){B=[B]}rcSlice.row.start=rcSlice.row.start||0;rcSlice.row.end=rcSlice.row.end||A.length;rcSlice.row.step=rcSlice.row.step||1;rcSlice.col.start=rcSlice.col.start||0;rcSlice.col.end=rcSlice.col.end||A[0].length;rcSlice.col.step=rcSlice.col.step||1;ml=jStat.arange(rcSlice.row.start,Math.min(A.length,rcSlice.row.end),rcSlice.row.step);nl=jStat.arange(rcSlice.col.start,Math.min(A[0].length,rcSlice.col.end),rcSlice.col.step);ml.forEach(function(m,i){nl.forEach(function(n,j){A[m][n]=B[i][j]})});return A};jStat.diagonal=function diagonal(diagArray){var mat=jStat.zeros(diagArray.length,diagArray.length);diagArray.forEach(function(t,i){mat[i][i]=t});return mat};jStat.copy=function copy(A){return A.map(function(row){if(isNumber(row))return row;return row.map(function(t){return t})})};var jProto=jStat.prototype;jProto.length=0;jProto.push=Array.prototype.push;jProto.sort=Array.prototype.sort;jProto.splice=Array.prototype.splice;jProto.slice=Array.prototype.slice;jProto.toArray=function toArray(){return this.length>1?slice.call(this):slice.call(this)[0]};jProto.map=function map(func,toAlter){return jStat(jStat.map(this,func,toAlter))};jProto.cumreduce=function cumreduce(func,toAlter){return jStat(jStat.cumreduce(this,func,toAlter))};jProto.alter=function alter(func){jStat.alter(this,func);return this};(function(funcs){for(var i=0;i=0)sum+=arr[i];return sum};jStat.sumsqrd=function sumsqrd(arr){var sum=0;var i=arr.length;while(--i>=0)sum+=arr[i]*arr[i];return sum};jStat.sumsqerr=function sumsqerr(arr){var mean=jStat.mean(arr);var sum=0;var i=arr.length;var tmp;while(--i>=0){tmp=arr[i]-mean;sum+=tmp*tmp}return sum};jStat.sumrow=function sumrow(arr){var sum=0;var i=arr.length;while(--i>=0)sum+=arr[i];return sum};jStat.product=function product(arr){var prod=1;var i=arr.length;while(--i>=0)prod*=arr[i];return prod};jStat.min=function min(arr){var low=arr[0];var i=0;while(++ihigh)high=arr[i];return high};jStat.unique=function unique(arr){var hash={},_arr=[];for(var i=0;imaxCount){mode_arr=[_arr[i]];maxCount=count;numMaxCount=0}else if(count===maxCount){mode_arr.push(_arr[i]);numMaxCount++}count=1}}return numMaxCount===0?mode_arr[0]:mode_arr};jStat.range=function range(arr){return jStat.max(arr)-jStat.min(arr)};jStat.variance=function variance(arr,flag){return jStat.sumsqerr(arr)/(arr.length-(flag?1:0))};jStat.pooledvariance=function pooledvariance(arr){var sumsqerr=arr.reduce(function(a,samples){return a+jStat.sumsqerr(samples)},0);var count=arr.reduce(function(a,samples){return a+samples.length},0);return sumsqerr/(count-arr.length)};jStat.deviation=function(arr){var mean=jStat.mean(arr);var arrlen=arr.length;var dev=new Array(arrlen);for(var i=0;i=0;i--){a.push(Math.abs(arr[i]-mean))}return jStat.mean(a)};jStat.meddev=function meddev(arr){var median=jStat.median(arr);var a=[];for(var i=arr.length-1;i>=0;i--){a.push(Math.abs(arr[i]-median))}return jStat.median(a)};jStat.coeffvar=function coeffvar(arr){return jStat.stdev(arr)/jStat.mean(arr)};jStat.quartiles=function quartiles(arr){var arrlen=arr.length;var _arr=arr.slice().sort(ascNum);return[_arr[Math.round(arrlen/4)-1],_arr[Math.round(arrlen/2)-1],_arr[Math.round(arrlen*3/4)-1]]};jStat.quantiles=function quantiles(arr,quantilesArray,alphap,betap){var sortedArray=arr.slice().sort(ascNum);var quantileVals=[quantilesArray.length];var n=arr.length;var i,p,m,aleph,k,gamma;if(typeof alphap==="undefined")alphap=3/8;if(typeof betap==="undefined")betap=3/8;for(i=0;i1){tmpthis=fullbool===true?this:this.transpose();for(;i1){if(passfunc!=="sumrow")tmpthis=fullbool===true?this:this.transpose();for(;i1){tmpthis=tmpthis.transpose();for(;i=0;k--){gl0*=x2;gl0+=a[k]}gl=gl0/x0+.5*Math.log(xp)+(x0-.5)*Math.log(x0)-x0;if(x<=7){for(k=1;k<=n;k++){gl-=Math.log(x0-1);x0-=1}}return gl};jStat.gammafn=function gammafn(x){var p=[-1.716185138865495,24.76565080557592,-379.80425647094563,629.3311553128184,866.9662027904133,-31451.272968848367,-36144.413418691176,66456.14382024054];var q=[-30.8402300119739,315.35062697960416,-1015.1563674902192,-3107.771671572311,22538.11842098015,4755.846277527881,-134659.9598649693,-115132.2596755535];var fact=false;var n=0;var xden=0;var xnum=0;var y=x;var i,z,yi,res;if(x>171.6243769536076){return Infinity}if(y<=0){res=y%1+36e-17;if(res){fact=(!(y&1)?1:-1)*Math.PI/Math.sin(Math.PI*res);y=1-y}else{return Infinity}}yi=y;if(y<1){z=y++}else{z=(y-=n=(y|0)-1)-1}for(i=0;i<8;++i){xnum=(xnum+p[i])*z;xden=xden*z+q[i]}res=xnum/xden+1;if(yiy){for(i=0;i=1?a:1/a)*8.5+a*.4+17);var an;if(x<0||a<=0){return NaN}else if(x170||m>170?Math.exp(jStat.combinationln(n,m)):jStat.factorial(n)/jStat.factorial(m)/jStat.factorial(n-m)};jStat.combinationln=function combinationln(n,m){return jStat.factorialln(n)-jStat.factorialln(m)-jStat.factorialln(n-m)};jStat.permutation=function permutation(n,m){return jStat.factorial(n)/jStat.factorial(n-m)};jStat.betafn=function betafn(x,y){if(x<=0||y<=0)return undefined;return x+y>170?Math.exp(jStat.betaln(x,y)):jStat.gammafn(x)*jStat.gammafn(y)/jStat.gammafn(x+y)};jStat.betaln=function betaln(x,y){return jStat.gammaln(x)+jStat.gammaln(y)-jStat.gammaln(x+y)};jStat.betacf=function betacf(x,a,b){var fpmin=1e-30;var m=1;var qab=a+b;var qap=a+1;var qam=a-1;var c=1;var d=1-qab*x/qap;var m2,aa,del,h;if(Math.abs(d)=1)return Math.max(100,a+100*Math.sqrt(a));if(p<=0)return 0;if(a>1){lna1=Math.log(a1);afac=Math.exp(a1*(lna1-1)-gln);pp=p<.5?p:1-p;t=Math.sqrt(-2*Math.log(pp));x=(2.30753+t*.27061)/(1+t*(.99229+t*.04481))-t;if(p<.5)x=-x;x=Math.max(.001,a*Math.pow(1-1/(9*a)-x/(3*Math.sqrt(a)),3))}else{t=1-a*(.253+a*.12);if(p1)t=afac*Math.exp(-(x-a1)+a1*(Math.log(x)-lna1));else t=Math.exp(-x+a1*Math.log(x)-gln);u=err/t;x-=t=u/(1-.5*Math.min(1,u*((a-1)/x-1)));if(x<=0)x=.5*(x+t);if(Math.abs(t)0;j--){tmp=d;d=ty*d-dd+cof[j];dd=tmp}res=t*Math.exp(-x*x+.5*(cof[0]+ty*d)-dd);return isneg?res-1:1-res};jStat.erfc=function erfc(x){return 1-jStat.erf(x)};jStat.erfcinv=function erfcinv(p){var j=0;var x,err,t,pp;if(p>=2)return-100;if(p<=0)return 100;pp=p<1?p:2-p;t=Math.sqrt(-2*Math.log(pp/2));x=-.70711*((2.30753+t*.27061)/(1+t*(.99229+t*.04481))-t);for(;j<2;j++){err=jStat.erfc(x)-pp;x+=err/(1.1283791670955126*Math.exp(-x*x)-x*err)}return p<1?x:-x};jStat.ibetainv=function ibetainv(p,a,b){var EPS=1e-8;var a1=a-1;var b1=b-1;var j=0;var lna,lnb,pp,t,u,err,x,al,h,w,afac;if(p<=0)return 0;if(p>=1)return 1;if(a>=1&&b>=1){pp=p<.5?p:1-p;t=Math.sqrt(-2*Math.log(pp));x=(2.30753+t*.27061)/(1+t*(.99229+t*.04481))-t;if(p<.5)x=-x;al=(x*x-3)/6;h=2/(1/(2*a-1)+1/(2*b-1));w=x*Math.sqrt(al+h)/h-(1/(2*b-1)-1/(2*a-1))*(al+5/6-2/(3*h));x=a/(a+b*Math.exp(2*w))}else{lna=Math.log(a/(a+b));lnb=Math.log(b/(a+b));t=Math.exp(a*lna)/a;u=Math.exp(b*lnb)/b;w=t+u;if(p=1)x=.5*(x+t+1);if(Math.abs(t)0)break}return x};jStat.ibeta=function ibeta(x,a,b){var bt=x===0||x===1?0:Math.exp(jStat.gammaln(a+b)-jStat.gammaln(a)-jStat.gammaln(b)+a*Math.log(x)+b*Math.log(1-x));if(x<0||x>1)return false;if(x<(a+1)/(a+b+2))return bt*jStat.betacf(x,a,b)/a;return 1-bt*jStat.betacf(1-x,b,a)/b};jStat.randn=function randn(n,m){var u,v,x,y,q;if(!m)m=n;if(n)return jStat.create(n,m,function(){return jStat.randn()});do{u=jStat._random_fn();v=1.7156*(jStat._random_fn()-.5);x=u-.449871;y=Math.abs(v)+.386595;q=x*x+y*(.196*y-.25472*x)}while(q>.27597&&(q>.27846||v*v>-4*Math.log(u)*u*u));return v/u};jStat.randg=function randg(shape,n,m){var oalph=shape;var a1,a2,u,v,x,mat;if(!m)m=n;if(!shape)shape=1;if(n){mat=jStat.zeros(n,m);mat.alter(function(){return jStat.randg(shape)});return mat}if(shape<1)shape+=1;a1=shape-1/3;a2=1/Math.sqrt(9*a1);do{do{x=jStat.randn();v=1+a2*x}while(v<=0);v=v*v*v;u=jStat._random_fn()}while(u>1-.331*Math.pow(x,4)&&Math.log(u)>.5*x*x+a1*(1-v+Math.log(v)));if(shape==oalph)return a1*v;do{u=jStat._random_fn()}while(u===0);return Math.pow(u,1/oalph)*a1*v};(function(funcs){for(var i=0;i1||x<0)return 0;if(alpha==1&&beta==1)return 1;if(alpha<512&&beta<512){return Math.pow(x,alpha-1)*Math.pow(1-x,beta-1)/jStat.betafn(alpha,beta)}else{return Math.exp((alpha-1)*Math.log(x)+(beta-1)*Math.log(1-x)-jStat.betaln(alpha,beta))}},cdf:function cdf(x,alpha,beta){return x>1||x<0?(x>1)*1:jStat.ibeta(x,alpha,beta)},inv:function inv(x,alpha,beta){return jStat.ibetainv(x,alpha,beta)},mean:function mean(alpha,beta){return alpha/(alpha+beta)},median:function median(alpha,beta){return jStat.ibetainv(.5,alpha,beta)},mode:function mode(alpha,beta){return(alpha-1)/(alpha+beta-2)},sample:function sample(alpha,beta){var u=jStat.randg(alpha);return u/(u+jStat.randg(beta))},variance:function variance(alpha,beta){return alpha*beta/(Math.pow(alpha+beta,2)*(alpha+beta+1))}});jStat.extend(jStat.centralF,{pdf:function pdf(x,df1,df2){var p,q,f;if(x<0)return 0;if(df1<=2){if(x===0&&df1<2){return Infinity}if(x===0&&df1===2){return 1}return 1/jStat.betafn(df1/2,df2/2)*Math.pow(df1/df2,df1/2)*Math.pow(x,df1/2-1)*Math.pow(1+df1/df2*x,-(df1+df2)/2)}p=df1*x/(df2+x*df1);q=df2/(df2+x*df1);f=df1*q/2;return f*jStat.binomial.pdf((df1-2)/2,(df1+df2-2)/2,p)},cdf:function cdf(x,df1,df2){if(x<0)return 0;return jStat.ibeta(df1*x/(df1*x+df2),df1/2,df2/2)},inv:function inv(x,df1,df2){return df2/(df1*(1/jStat.ibetainv(x,df1/2,df2/2)-1))},mean:function mean(df1,df2){return df2>2?df2/(df2-2):undefined},mode:function mode(df1,df2){return df1>2?df2*(df1-2)/(df1*(df2+2)):undefined},sample:function sample(df1,df2){var x1=jStat.randg(df1/2)*2;var x2=jStat.randg(df2/2)*2;return x1/df1/(x2/df2)},variance:function variance(df1,df2){if(df2<=4)return undefined;return 2*df2*df2*(df1+df2-2)/(df1*(df2-2)*(df2-2)*(df2-4))}});jStat.extend(jStat.cauchy,{pdf:function pdf(x,local,scale){if(scale<0){return 0}return scale/(Math.pow(x-local,2)+Math.pow(scale,2))/Math.PI},cdf:function cdf(x,local,scale){return Math.atan((x-local)/scale)/Math.PI+.5},inv:function(p,local,scale){return local+scale*Math.tan(Math.PI*(p-.5))},median:function median(local){return local},mode:function mode(local){return local},sample:function sample(local,scale){return jStat.randn()*Math.sqrt(1/(2*jStat.randg(.5)))*scale+local}});jStat.extend(jStat.chisquare,{pdf:function pdf(x,dof){if(x<0)return 0;return x===0&&dof===2?.5:Math.exp((dof/2-1)*Math.log(x)-x/2-dof/2*Math.log(2)-jStat.gammaln(dof/2))},cdf:function cdf(x,dof){if(x<0)return 0;return jStat.lowRegGamma(dof/2,x/2)},inv:function(p,dof){return 2*jStat.gammapinv(p,.5*dof)},mean:function(dof){return dof},median:function median(dof){return dof*Math.pow(1-2/(9*dof),3)},mode:function mode(dof){return dof-2>0?dof-2:0},sample:function sample(dof){return jStat.randg(dof/2)*2},variance:function variance(dof){return 2*dof}});jStat.extend(jStat.exponential,{pdf:function pdf(x,rate){return x<0?0:rate*Math.exp(-rate*x)},cdf:function cdf(x,rate){return x<0?0:1-Math.exp(-rate*x)},inv:function(p,rate){return-Math.log(1-p)/rate},mean:function(rate){return 1/rate},median:function(rate){return 1/rate*Math.log(2)},mode:function mode(){return 0},sample:function sample(rate){return-1/rate*Math.log(jStat._random_fn())},variance:function(rate){return Math.pow(rate,-2)}});jStat.extend(jStat.gamma,{pdf:function pdf(x,shape,scale){if(x<0)return 0;return x===0&&shape===1?1/scale:Math.exp((shape-1)*Math.log(x)-x/scale-jStat.gammaln(shape)-shape*Math.log(scale))},cdf:function cdf(x,shape,scale){if(x<0)return 0;return jStat.lowRegGamma(shape,x/scale)},inv:function(p,shape,scale){return jStat.gammapinv(p,shape)*scale},mean:function(shape,scale){return shape*scale},mode:function mode(shape,scale){if(shape>1)return(shape-1)*scale;return undefined},sample:function sample(shape,scale){return jStat.randg(shape)*scale},variance:function variance(shape,scale){return shape*scale*scale}});jStat.extend(jStat.invgamma,{pdf:function pdf(x,shape,scale){if(x<=0)return 0;return Math.exp(-(shape+1)*Math.log(x)-scale/x-jStat.gammaln(shape)+shape*Math.log(scale))},cdf:function cdf(x,shape,scale){if(x<=0)return 0;return 1-jStat.lowRegGamma(shape,scale/x)},inv:function(p,shape,scale){return scale/jStat.gammapinv(1-p,shape)},mean:function(shape,scale){return shape>1?scale/(shape-1):undefined},mode:function mode(shape,scale){return scale/(shape+1)},sample:function sample(shape,scale){return scale/jStat.randg(shape)},variance:function variance(shape,scale){if(shape<=2)return undefined;return scale*scale/((shape-1)*(shape-1)*(shape-2))}});jStat.extend(jStat.kumaraswamy,{pdf:function pdf(x,alpha,beta){if(x===0&&alpha===1)return beta;else if(x===1&&beta===1)return alpha;return Math.exp(Math.log(alpha)+Math.log(beta)+(alpha-1)*Math.log(x)+(beta-1)*Math.log(1-Math.pow(x,alpha)))},cdf:function cdf(x,alpha,beta){if(x<0)return 0;else if(x>1)return 1;return 1-Math.pow(1-Math.pow(x,alpha),beta)},inv:function inv(p,alpha,beta){return Math.pow(1-Math.pow(1-p,1/beta),1/alpha)},mean:function(alpha,beta){return beta*jStat.gammafn(1+1/alpha)*jStat.gammafn(beta)/jStat.gammafn(1+1/alpha+beta)},median:function median(alpha,beta){return Math.pow(1-Math.pow(2,-1/beta),1/alpha)},mode:function mode(alpha,beta){if(!(alpha>=1&&beta>=1&&(alpha!==1&&beta!==1)))return undefined;return Math.pow((alpha-1)/(alpha*beta-1),1/alpha)},variance:function variance(){throw new Error("variance not yet implemented")}});jStat.extend(jStat.lognormal,{pdf:function pdf(x,mu,sigma){if(x<=0)return 0;return Math.exp(-Math.log(x)-.5*Math.log(2*Math.PI)-Math.log(sigma)-Math.pow(Math.log(x)-mu,2)/(2*sigma*sigma))},cdf:function cdf(x,mu,sigma){if(x<0)return 0;return.5+.5*jStat.erf((Math.log(x)-mu)/Math.sqrt(2*sigma*sigma))},inv:function(p,mu,sigma){return Math.exp(-1.4142135623730951*sigma*jStat.erfcinv(2*p)+mu)},mean:function mean(mu,sigma){return Math.exp(mu+sigma*sigma/2)},median:function median(mu){return Math.exp(mu)},mode:function mode(mu,sigma){return Math.exp(mu-sigma*sigma)},sample:function sample(mu,sigma){return Math.exp(jStat.randn()*sigma+mu)},variance:function variance(mu,sigma){return(Math.exp(sigma*sigma)-1)*Math.exp(2*mu+sigma*sigma)}});jStat.extend(jStat.noncentralt,{pdf:function pdf(x,dof,ncp){var tol=1e-14;if(Math.abs(ncp)tol||value>tol){lastvalue=value;if(j>0){p*=ncp*ncp/(2*j);q*=ncp*ncp/(2*(j+1/2))}value=p*jStat.beta.cdf(y,j+.5,dof/2)+q*jStat.beta.cdf(y,j+1,dof/2);prob+=.5*value;j++}return flip?1-prob:prob}});jStat.extend(jStat.normal,{pdf:function pdf(x,mean,std){return Math.exp(-.5*Math.log(2*Math.PI)-Math.log(std)-Math.pow(x-mean,2)/(2*std*std))},cdf:function cdf(x,mean,std){return.5*(1+jStat.erf((x-mean)/Math.sqrt(2*std*std)))},inv:function(p,mean,std){return-1.4142135623730951*std*jStat.erfcinv(2*p)+mean},mean:function(mean){return mean},median:function median(mean){return mean},mode:function(mean){return mean},sample:function sample(mean,std){return jStat.randn()*std+mean},variance:function(mean,std){return std*std}});jStat.extend(jStat.pareto,{pdf:function pdf(x,scale,shape){if(x1e100?1e100:dof;return 1/(Math.sqrt(dof)*jStat.betafn(.5,dof/2))*Math.pow(1+x*x/dof,-((dof+1)/2))},cdf:function cdf(x,dof){var dof2=dof/2;return jStat.ibeta((x+Math.sqrt(x*x+dof))/(2*Math.sqrt(x*x+dof)),dof2,dof2)},inv:function(p,dof){var x=jStat.ibetainv(2*Math.min(p,1-p),.5*dof,.5);x=Math.sqrt(dof*(1-x)/x);return p>.5?x:-x},mean:function mean(dof){return dof>1?0:undefined},median:function median(){return 0},mode:function mode(){return 0},sample:function sample(dof){return jStat.randn()*Math.sqrt(dof/(2*jStat.randg(dof/2)))},variance:function variance(dof){return dof>2?dof/(dof-2):dof>1?Infinity:undefined}});jStat.extend(jStat.weibull,{pdf:function pdf(x,scale,shape){if(x<0||scale<0||shape<0)return 0;return shape/scale*Math.pow(x/scale,shape-1)*Math.exp(-Math.pow(x/scale,shape))},cdf:function cdf(x,scale,shape){return x<0?0:1-Math.exp(-Math.pow(x/scale,shape))},inv:function(p,scale,shape){return scale*Math.pow(-Math.log(1-p),1/shape)},mean:function(scale,shape){return scale*jStat.gammafn(1+1/shape)},median:function median(scale,shape){return scale*Math.pow(Math.log(2),1/shape)},mode:function mode(scale,shape){if(shape<=1)return 0;return scale*Math.pow((shape-1)/shape,1/shape)},sample:function sample(scale,shape){return scale*Math.pow(-Math.log(jStat._random_fn()),1/shape)},variance:function variance(scale,shape){return scale*scale*jStat.gammafn(1+2/shape)-Math.pow(jStat.weibull.mean(scale,shape),2)}});jStat.extend(jStat.uniform,{pdf:function pdf(x,a,b){return xb?0:1/(b-a)},cdf:function cdf(x,a,b){if(xeps){a2=a1;c9=-(a+m9)*(a+b+m9)*x/(a+2*m9)/(a+2*m9+1);a0=a1+c9*a0;b0=b1+c9*b0;m9=m9+1;c9=m9*(b-m9)*x/(a+2*m9-1)/(a+2*m9);a1=a0+c9*a1;b1=b0+c9*b1;a0=a0/b1;b0=b0/b1;a1=a1/b1;b1=1}return a1/a}jStat.extend(jStat.binomial,{pdf:function pdf(k,n,p){return p===0||p===1?n*p===k?1:0:jStat.combination(n,k)*Math.pow(p,k)*Math.pow(1-p,n-k)},cdf:function cdf(x,n,p){var betacdf;var eps=1e-10;if(x<0)return 0;if(x>=n)return 1;if(p<0||p>1||n<=0)return NaN;x=Math.floor(x);var z=p;var a=x+1;var b=n-x;var s=a+b;var bt=Math.exp(jStat.gammaln(s)-jStat.gammaln(b)-jStat.gammaln(a)+a*Math.log(z)+b*Math.log(1-z));if(z<(a+1)/(s+2))betacdf=bt*betinc(z,a,b,eps);else betacdf=1-bt*betinc(1-z,b,a,eps);return Math.round((1-betacdf)*(1/eps))/(1/eps)}});jStat.extend(jStat.negbin,{pdf:function pdf(k,r,p){if(k!==k>>>0)return false;if(k<0)return 0;return jStat.combination(k+r-1,r-1)*Math.pow(1-p,k)*Math.pow(p,r)},cdf:function cdf(x,r,p){var sum=0,k=0;if(x<0)return 0;for(;k<=x;k++){sum+=jStat.negbin.pdf(k,r,p)}return sum}});jStat.extend(jStat.hypgeom,{pdf:function pdf(k,N,m,n){if(k!==k|0){return false}else if(k<0||kn||k>m){return 0}else if(m*2>N){if(n*2>N){return jStat.hypgeom.pdf(N-m-n+k,N,N-m,N-n)}else{return jStat.hypgeom.pdf(n-k,N,N-m,n)}}else if(n*2>N){return jStat.hypgeom.pdf(m-k,N,m,N-n)}else if(m1&&samplesDone=n||x>=m){return 1}else if(m*2>N){if(n*2>N){return jStat.hypgeom.cdf(N-m-n+x,N,N-m,N-n)}else{return 1-jStat.hypgeom.cdf(n-x-1,N,N-m,n)}}else if(n*2>N){return 1-jStat.hypgeom.cdf(m-x-1,N,m,N-n)}else if(m1&&samplesDoneL);return k-1},sampleLarge:function sampleLarge(l){var lam=l;var k;var U,V,slam,loglam,a,b,invalpha,vr,us;slam=Math.sqrt(lam);loglam=Math.log(lam);b=.931+2.53*slam;a=-.059+.02483*b;invalpha=1.1239+1.1328/(b-3.4);vr=.9277-3.6224/(b-2);while(1){U=Math.random()-.5;V=Math.random();us=.5-Math.abs(U);k=Math.floor((2*a/us+b)*U+lam+.43);if(us>=.07&&V<=vr){return k}if(k<0||us<.013&&V>us){continue}if(Math.log(V)+Math.log(invalpha)-Math.log(a/(us*us)+b)<=-lam+k*loglam-jStat.loggam(k+1)){return k}}},sample:function sample(l){if(l<10)return this.sampleSmall(l);else return this.sampleLarge(l)}});jStat.extend(jStat.triangular,{pdf:function pdf(x,a,b,c){if(b<=a||cb){return NaN}else{if(xb){return 0}else if(xb)return NaN;if(x<=a)return 0;else if(x>=b)return 1;if(x<=c)return Math.pow(x-a,2)/((b-a)*(c-a));else return 1-Math.pow(b-x,2)/((b-a)*(b-c))},inv:function inv(p,a,b,c){if(b<=a||cb){return NaN}else{if(p<=(c-a)/(b-a)){return a+(b-a)*Math.sqrt(p*((c-a)/(b-a)))}else{return a+(b-a)*(1-Math.sqrt((1-p)*(1-(c-a)/(b-a))))}}},mean:function mean(a,b,c){return(a+b+c)/3},median:function median(a,b,c){if(c<=(a+b)/2){return b-Math.sqrt((b-a)*(b-c))/Math.sqrt(2)}else if(c>(a+b)/2){return a+Math.sqrt((b-a)*(c-a))/Math.sqrt(2)}},mode:function mode(a,b,c){return c},sample:function sample(a,b,c){var u=jStat._random_fn();if(u<(c-a)/(b-a))return a+Math.sqrt(u*(b-a)*(c-a));return b-Math.sqrt((1-u)*(b-a)*(b-c))},variance:function variance(a,b,c){return(a*a+b*b+c*c-a*b-a*c-b*c)/18}});jStat.extend(jStat.arcsine,{pdf:function pdf(x,a,b){if(b<=a)return NaN;return x<=a||x>=b?0:2/Math.PI*Math.pow(Math.pow(b-a,2)-Math.pow(2*x-a-b,2),-.5)},cdf:function cdf(x,a,b){if(x=bb)return 1;var pr_w=2*jStat.normal.cdf(qsqz,0,1,1,0)-1;if(pr_w>=Math.exp(C2/cc))pr_w=Math.pow(pr_w,cc);else pr_w=0;var wincr;if(w>wlar)wincr=wincr1;else wincr=wincr2;var blb=qsqz;var binc=(bb-qsqz)/wincr;var bub=blb+binc;var einsum=0;var cc1=cc-1;for(var wi=1;wi<=wincr;wi++){var elsum=0;var a=.5*(bub+blb);var b=.5*(bub-blb);for(var jj=1;jj<=nleg;jj++){var j,xx;if(ihalfC3)break;var pplus=2*jStat.normal.cdf(ac,0,1,1,0);var pminus=2*jStat.normal.cdf(ac,w,1,1,0);var rinsum=pplus*.5-pminus*.5;if(rinsum>=Math.exp(C1/cc1)){rinsum=aleg[j-1]*Math.exp(-(.5*qexpo))*Math.pow(rinsum,cc1);elsum+=rinsum}}elsum*=2*b*cc/Math.sqrt(2*Math.PI);einsum+=elsum;blb=bub;bub+=binc}pr_w+=einsum;if(pr_w<=Math.exp(C1/rr))return 0;pr_w=Math.pow(pr_w,rr);if(pr_w>=1)return 1;return pr_w}function tukeyQinv(p,c,v){var p0=.322232421088;var q0=.099348462606;var p1=-1;var q1=.588581570495;var p2=-.342242088547;var q2=.531103462366;var p3=-.204231210125;var q3=.10353775285;var p4=-453642210148e-16;var q4=.0038560700634;var c1=.8832;var c2=.2368;var c3=1.214;var c4=1.208;var c5=1.4142;var vmax=120;var ps=.5-.5*p;var yi=Math.sqrt(Math.log(1/(ps*ps)));var t=yi+((((yi*p4+p3)*yi+p2)*yi+p1)*yi+p0)/((((yi*q4+q3)*yi+q2)*yi+q1)*yi+q0);if(vdlarg)return tukeyWprob(q,rr,cc);var f2=df*.5;var f2lf=f2*Math.log(df)-df*Math.log(2)-jStat.gammaln(f2);var f21=f2-1;var ff4=df*.25;var ulen;if(df<=dhaf)ulen=ulen1;else if(df<=dquar)ulen=ulen2;else if(df<=deigh)ulen=ulen3;else ulen=ulen4;f2lf+=Math.log(ulen);var ans=0;for(var i=1;i<=50;i++){var otsum=0;var twa1=(2*i-1)*ulen;for(var jj=1;jj<=nlegq;jj++){var j,t1;if(ihalfq=eps1){if(ihalfq=1&&otsum<=eps2)break;ans+=otsum}if(otsum>eps2){throw new Error("tukey.cdf failed to converge")}if(ans>1)ans=1;return ans},inv:function(p,nmeans,df){var rr=1;var cc=nmeans;var eps=1e-4;var maxiter=50;if(df<2||rr<1||cc<2)return NaN;if(p<0||p>1)return NaN;if(p===0)return 0;if(p===1)return Infinity;var x0=tukeyQinv(p,cc,df);var valx0=jStat.tukey.cdf(x0,nmeans,df)-p;var x1;if(valx0>0)x1=Math.max(0,x0-1);else x1=x0+1;var valx1=jStat.tukey.cdf(x1,nmeans,df)-p;var ans;for(var iter=1;iteri){submatrix[row-1][col-1]=a[row][col]}}}var sign=i%2?-1:1;determinant+=det(submatrix)*a[0][i]*sign}return determinant},gauss_elimination:function gauss_elimination(a,b){var i=0,j=0,n=a.length,m=a[0].length,factor=1,sum=0,x=[],maug,pivot,temp,k;a=jStat.aug(a,b);maug=a[0].length;for(i=0;i=0;i--){sum=0;for(j=i+1;j<=n-1;j++){sum=sum+x[j]*a[i][j]}x[i]=(a[i][maug-1]-sum)/a[i][i]}return x},gauss_jordan:function gauss_jordan(a,b){var m=jStat.aug(a,b);var h=m.length;var w=m[0].length;var c=0;var x,y,y2;for(y=0;yMath.abs(m[maxrow][y]))maxrow=y2}var tmp=m[y];m[y]=m[maxrow];m[maxrow]=tmp;for(y2=y+1;y2=0;y--){c=m[y][y];for(y2=0;y2y-1;x--){m[y2][x]-=m[y][x]*m[y2][y]/c}}m[y][y]/=c;for(x=h;xj){l[i][j]=a[i][j];u[i][j]=d[i][j]=0}else if(ir){xv=xk;xk=jStat.add(jStat.multiply(h,xv),c);i++}return xk},gauss_seidel:function gauss_seidel(a,b,x,r){var i=0;var n=a.length;var l=[];var u=[];var d=[];var j,xv,c,h,xk;for(;ij){l[i][j]=a[i][j];u[i][j]=d[i][j]=0}else if(ir){xv=xk;xk=jStat.add(jStat.multiply(h,xv),c);i=i+1}return xk},SOR:function SOR(a,b,x,r,w){var i=0;var n=a.length;var l=[];var u=[];var d=[];var j,xv,c,h,xk;for(;ij){l[i][j]=a[i][j];u[i][j]=d[i][j]=0}else if(ir){xv=xk;xk=jStat.add(jStat.multiply(h,xv),c);i++}return xk},householder:function householder(a){var m=a.length;var n=a[0].length;var i=0;var w=[];var p=[];var alpha,r,k,j,factor;for(;i0?-1:1;alpha=factor*Math.sqrt(alpha);r=Math.sqrt((alpha*alpha-a[i+1][i]*alpha)/2);w=jStat.zeros(m,1);w[i+1][0]=(a[i+1][i]-alpha)/(2*r);for(k=i+2;k0?Math.PI/4:-Math.PI/4;else theta=Math.atan(2*a[p][q]/(a[p][p]-a[q][q]))/2;s=jStat.identity(n,n);s[p][p]=Math.cos(theta);s[p][q]=-Math.sin(theta);s[q][p]=Math.sin(theta);s[q][q]=Math.cos(theta);e=jStat.multiply(e,s);b=jStat.multiply(jStat.multiply(jStat.inv(s),a),s);a=b;condition=0;for(i=1;i.001){condition=1}}}}for(i=0;i=h_min){y1=pos(X,x+h);y2=pos(X,x);g[i]=(f[y1]-2*f[y2]+f[2*y2-y1])/(h*h);h/=2;i++}a=g.length;m=1;while(a!=1){for(j=0;jvalue)break}j-=1;return F[j]+(value-X[j])*b[j]+jStat.sq(value-X[j])*c[j]+(value-X[j])*jStat.sq(value-X[j])*d[j]},gauss_quadrature:function gauss_quadrature(){throw new Error("gauss_quadrature not yet implemented")},PCA:function PCA(X){var m=X.length;var n=X[0].length;var i=0;var j,temp1;var u=[];var D=[];var result=[];var temp2=[];var Y=[];var Bt=[];var B=[];var C=[];var V=[];var Vt=[];for(i=0;i2){z=jStat.zscore(args[0],args[1],args[2]);return args[3]===1?jStat.normal.cdf(-Math.abs(z),0,1):jStat.normal.cdf(-Math.abs(z),0,1)*2}else{z=args[0];return args[1]===1?jStat.normal.cdf(-Math.abs(z),0,1):jStat.normal.cdf(-Math.abs(z),0,1)*2}}}});jStat.extend(jStat.fn,{zscore:function zscore(value,flag){return(value-this.mean())/this.stdev(flag)},ztest:function ztest(value,sides,flag){var zscore=Math.abs(this.zscore(value,flag));return sides===1?jStat.normal.cdf(-zscore,0,1):jStat.normal.cdf(-zscore,0,1)*2}});jStat.extend({tscore:function tscore(){var args=slice.call(arguments);return args.length===4?(args[0]-args[1])/(args[2]/Math.sqrt(args[3])):(args[0]-jStat.mean(args[1]))/(jStat.stdev(args[1],true)/Math.sqrt(args[1].length))},ttest:function ttest(){var args=slice.call(arguments);var tscore;if(args.length===5){tscore=Math.abs(jStat.tscore(args[0],args[1],args[2],args[3]));return args[4]===1?jStat.studentt.cdf(-tscore,args[3]-1):jStat.studentt.cdf(-tscore,args[3]-1)*2}if(isNumber(args[1])){tscore=Math.abs(args[0]);return args[2]==1?jStat.studentt.cdf(-tscore,args[1]-1):jStat.studentt.cdf(-tscore,args[1]-1)*2}tscore=Math.abs(jStat.tscore(args[0],args[1]));return args[2]==1?jStat.studentt.cdf(-tscore,args[1].length-1):jStat.studentt.cdf(-tscore,args[1].length-1)*2}});jStat.extend(jStat.fn,{tscore:function tscore(value){return(value-this.mean())/(this.stdev(true)/Math.sqrt(this.cols()))},ttest:function ttest(value,sides){return sides===1?1-jStat.studentt.cdf(Math.abs(this.tscore(value)),this.cols()-1):jStat.studentt.cdf(-Math.abs(this.tscore(value)),this.cols()-1)*2}});jStat.extend({anovafscore:function anovafscore(){var args=slice.call(arguments),expVar,sample,sampMean,sampSampMean,tmpargs,unexpVar,i,j;if(args.length===1){tmpargs=new Array(args[0].length);for(i=0;i1||p2>1||p1<=0||p2<=0){throw new Error("Proportions should be greater than 0 and less than 1")}var pooled=(p1*n1+p2*n2)/(n1+n2);var se=Math.sqrt(pooled*(1-pooled)*(1/n1+1/n2));return(p1-p2)/se}jStat.extend(jStat.fn,{oneSidedDifferenceOfProportions:function oneSidedDifferenceOfProportions(p1,n1,p2,n2){var z=differenceOfProportions(p1,n1,p2,n2);return jStat.ztest(z,1)},twoSidedDifferenceOfProportions:function twoSidedDifferenceOfProportions(p1,n1,p2,n2){var z=differenceOfProportions(p1,n1,p2,n2);return jStat.ztest(z,2)}})})(jStat,Math);jStat.models=function(){function sub_regress(exog){var var_count=exog[0].length;var modelList=jStat.arange(var_count).map(function(endog_index){var exog_index=jStat.arange(var_count).filter(function(i){return i!==endog_index});return ols(jStat.col(exog,endog_index).map(function(x){return x[0]}),jStat.col(exog,exog_index))});return modelList}function ols(endog,exog){var nobs=endog.length;var df_model=exog[0].length-1;var df_resid=nobs-df_model-1;var coef=jStat.lstsq(exog,endog);var predict=jStat.multiply(exog,coef.map(function(x){return[x]})).map(function(p){return p[0]});var resid=jStat.subtract(endog,predict);var ybar=jStat.mean(endog);var SSE=jStat.sum(predict.map(function(f){return Math.pow(f-ybar,2)}));var SSR=jStat.sum(endog.map(function(y,i){return Math.pow(y-predict[i],2)}));var SST=SSE+SSR;var R2=SSE/SST;return{exog:exog,endog:endog,nobs:nobs,df_model:df_model,df_resid:df_resid,coef:coef,predict:predict,resid:resid,ybar:ybar,SST:SST,SSE:SSE,SSR:SSR,R2:R2}}function t_test(model){var subModelList=sub_regress(model.exog);var sigmaHat=Math.sqrt(model.SSR/model.df_resid);var seBetaHat=subModelList.map(function(mod){var SST=mod.SST;var R2=mod.R2;return sigmaHat/Math.sqrt(SST*(1-R2))});var tStatistic=model.coef.map(function(coef,i){return(coef-0)/seBetaHat[i]});var pValue=tStatistic.map(function(t){var leftppf=jStat.studentt.cdf(t,model.df_resid);return(leftppf>.5?1-leftppf:leftppf)*2});var c=jStat.studentt.inv(.975,model.df_resid);var interval95=model.coef.map(function(coef,i){var d=c*seBetaHat[i];return[coef-d,coef+d]});return{se:seBetaHat,t:tStatistic,p:pValue,sigmaHat:sigmaHat,interval95:interval95}}function F_test(model){var F_statistic=model.R2/model.df_model/((1-model.R2)/model.df_resid);var fcdf=function(x,n1,n2){return jStat.beta.cdf(x/(n2/n1+x),n1/2,n2/2)};var pvalue=1-fcdf(F_statistic,model.df_model,model.df_resid);return{F_statistic:F_statistic,pvalue:pvalue}}function ols_wrap(endog,exog){var model=ols(endog,exog);var ttest=t_test(model);var ftest=F_test(model);var adjust_R2=1-(1-model.R2)*((model.nobs-1)/model.df_resid);model.t=ttest;model.f=ftest;model.adjust_R2=adjust_R2;return model}return{ols:ols_wrap}}();jStat.extend({buildxmatrix:function buildxmatrix(){var matrixRows=new Array(arguments.length);for(var i=0;i1){result=[];for(i=0;i