function jstat(){}
j = jstat;
/* Simple JavaScript Inheritance
* By John Resig http://ejohn.org/
* MIT Licensed.
*/
// Inspired by base2 and Prototype
(function(){
var initializing = false, fnTest = /xyz/.test(function(){
xyz;
}) ? /\b_super\b/ : /.*/;
// The base Class implementation (does nothing)
this.Class = function(){};
// Create a new Class that inherits from this class
Class.extend = function(prop) {
var _super = this.prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor)
initializing = true;
var prototype = new this();
initializing = false;
// Copy the properties over onto the new prototype
for (var name in prop) {
// Check if we're overwriting an existing function
prototype[name] = typeof prop[name] == "function" &&
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
(function(name, fn){
return function() {
var tmp = this._super;
// Add a new ._super() method that is the same method
// but on the super-class
this._super = _super[name];
// The method only need to be bound temporarily, so we
// remove it when we're done executing
var ret = fn.apply(this, arguments);
this._super = tmp;
return ret;
};
})(name, prop[name]) :
prop[name];
}
// The dummy class constructor
function Class() {
// All construction is actually done in the init method
if ( !initializing && this.init )
this.init.apply(this, arguments);
}
// Populate our constructed prototype object
Class.prototype = prototype;
// Enforce the constructor to be what we expect
Class.constructor = Class;
// And make this class extendable
Class.extend = arguments.callee;
return Class;
};
})();
/******************************************************************************/
/* Constants */
/******************************************************************************/
jstat.ONE_SQRT_2PI = 0.3989422804014327;
jstat.LN_SQRT_2PI = 0.9189385332046727417803297;
jstat.LN_SQRT_PId2 = 0.225791352644727432363097614947;
jstat.DBL_MIN = 2.22507e-308;
jstat.DBL_EPSILON = 2.220446049250313e-16;
jstat.SQRT_32 = 5.656854249492380195206754896838;
jstat.TWO_PI = 6.283185307179586;
jstat.DBL_MIN_EXP = -999;
jstat.SQRT_2dPI = 0.79788456080287;
jstat.LN_SQRT_PI = 0.5723649429247;
/******************************************************************************/
/* jstat Functions */
/******************************************************************************/
jstat.seq = function(min, max, length) {
var r = new Range(min, max, length);
return r.getPoints();
}
jstat.dnorm = function(x, mean, sd, log) {
if(mean == null) mean = 0;
if(sd == null) sd = 1;
if(log == null) log = false;
var n = new NormalDistribution(mean, sd);
if(!isNaN(x)) {
// is a number
return n._pdf(x, log);
} else if(x.length) {
var res = [];
for(var i = 0; i < x.length; i++) {
res.push(n._pdf(x[i], log));
}
return res;
} else {
throw "Illegal argument: x";
}
}
jstat.pnorm = function(q, mean, sd, lower_tail, log) {
if(mean == null) mean = 0;
if(sd == null) sd = 1;
if(lower_tail == null) lower_tail = true;
if(log == null) log = false;
var n = new NormalDistribution(mean, sd);
if(!isNaN(q)) {
// is a number
return n._cdf(q, lower_tail, log);
} else if(q.length) {
var res = [];
for(var i = 0; i < q.length; i++) {
res.push(n._cdf(q[i], lower_tail, log));
}
return res;
} else {
throw "Illegal argument: x";
}
}
jstat.dlnorm = function(x, meanlog, sdlog, log) {
if(meanlog == null) meanlog = 0;
if(sdlog == null) sdlog = 1;
if(log == null) log = false;
var n = new LogNormalDistribution(meanlog, sdlog);
if(!isNaN(x)) {
// is a number
return n._pdf(x, log);
} else if(x.length) {
var res = [];
for(var i = 0; i < x.length; i++) {
res.push(n._pdf(x[i], log));
}
return res;
} else {
throw "Illegal argument: x";
}
}
jstat.plnorm = function(q, meanlog, sdlog, lower_tail, log) {
if(meanlog == null) meanlog = 0;
if(sdlog == null) sdlog = 1;
if(lower_tail == null) lower_tail = true;
if(log == null) log = false;
var n = new LogNormalDistribution(meanlog, sdlog);
if(!isNaN(q)) {
// is a number
return n._cdf(q, lower_tail, log);
}
else if(q.length) {
var res = [];
for(var i = 0; i < q.length; i++) {
res.push(n._cdf(q[i], lower_tail, log));
}
return res;
} else {
throw "Illegal argument: x";
}
}
jstat.dbeta = function(x, alpha, beta, ncp, log) {
if(ncp == null) ncp = 0;
if(log == null) log = false;
var b = new BetaDistribution(alpha, beta);
if(!isNaN(x)) {
// is a number
return b._pdf(x, log);
}
else if(x.length) {
var res = [];
for(var i = 0; i < x.length; i++) {
res.push(b._pdf(x[i], log));
}
return res;
} else {
throw "Illegal argument: x";
}
}
jstat.pbeta = function(q, alpha, beta, ncp, lower_tail, log) {
if(ncp == null) ncp = 0;
if(log == null) log = false;
if(lower_tail == null) lower_tail = true;
var b = new BetaDistribution(alpha, beta);
if(!isNaN(q)) {
// is a number
return b._cdf(q, lower_tail, log);
} else if(q.length) {
var res = [];
for(var i = 0; i < q.length; i++) {
res.push(b._cdf(q[i], lower_tail, log));
}
return res;
}
else {
throw "Illegal argument: x";
}
}
jstat.dgamma = function(x, shape, rate, scale, log) {
if(rate == null) rate = 1;
if(scale == null) scale = 1/rate;
if(log == null) log = false;
var g = new GammaDistribution(shape, scale);
if(!isNaN(x)) {
// is a number
return g._pdf(x, log);
} else if(x.length) {
var res = [];
for(var i = 0; i < x.length; i++) {
res.push(g._pdf(x[i], log));
}
return res;
} else {
throw "Illegal argument: x";
}
}
jstat.pgamma = function(q, shape, rate, scale, lower_tail, log) {
if(rate == null) rate = 1;
if(scale == null) scale = 1/rate;
if(lower_tail == null) lower_tail = true;
if(log == null) log = false;
var g = new GammaDistribution(shape, scale);
if(!isNaN(q)) {
// is a number
return g._cdf(q, lower_tail, log);
} else if(q.length) {
var res = [];
for(var i = 0; i < q.length; i++) {
res.push(g._cdf(q[i], lower_tail, log));
}
return res;
} else {
throw "Illegal argument: x";
}
}
jstat.dt = function(x, df, ncp, log) {
if(log == null) log = false;
var t = new StudentTDistribution(df, ncp);
if(!isNaN(x)) {
// is a number
return t._pdf(x, log);
} else if(x.length) {
var res = [];
for(var i = 0; i < x.length; i++) {
res.push(t._pdf(x[i], log));
}
return res;
} else {
throw "Illegal argument: x";
}
}
jstat.pt = function(q, df, ncp, lower_tail, log) {
if(lower_tail == null) lower_tail = true;
if(log == null) log = false;
var t = new S