distributions-cauchy-quantile
Version:
Cauchy distribution quantile function.
122 lines (108 loc) • 3.74 kB
JavaScript
// MODULES //
var isNumber = require( 'validate.io-number-primitive' ),
isArrayLike = require( 'validate.io-array-like' ),
isTypedArrayLike = require( 'validate.io-typed-array-like' ),
isMatrixLike = require( 'validate.io-matrix-like' ),
ctors = require( 'compute-array-constructors' ),
matrix = require( 'dstructs-matrix' ),
validate = require( './validate.js' );
// FUNCTIONS //
var quantile1 = require( './number.js' ),
quantile2 = require( './array.js' ),
quantile3 = require( './accessor.js' ),
quantile4 = require( './deepset.js' ),
quantile5 = require( './matrix.js' ),
quantile6 = require( './typedarray.js' );
// PDF //
/**
* FUNCTION: quantile( p[, opts] )
* Evaluates the quantile function for a Cauchy distribution.
*
* @param {Number|Number[]|Array|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|Matrix} p - input value
* @param {Object} [opts] - function options
* @param {Number} [opts.x0=0] - location parameter
* @param {Number} [opts.gamma=1] - scale parameter
* @param {Boolean} [opts.copy=true] - boolean indicating if the function should return a new data structure
* @param {Function} [opts.accessor] - accessor function for accessing array values
* @param {String} [opts.path] - deep get/set key path
* @param {String} [opts.sep="."] - deep get/set key path separator
* @param {String} [opts.dtype="float64"] - output data type
* @returns {Number|Number[]|Array|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|Matrix} quantile function value(s)
*/
function quantile( p, options ) {
/* jshint newcap:false */
var opts = {},
ctor,
err,
out,
dt,
d;
if ( arguments.length > 1 ) {
err = validate( opts, options );
if ( err ) {
throw err;
}
}
opts.x0 = typeof opts.x0 !== 'undefined' ? opts.x0 : 0;
opts.gamma = typeof opts.gamma !== 'undefined' ? opts.gamma : 1;
if ( isNumber( p ) ) {
return quantile1( p, opts.x0, opts.gamma );
}
if ( isMatrixLike( p ) ) {
if ( opts.copy !== false ) {
dt = opts.dtype || 'float64';
ctor = ctors( dt );
if ( ctor === null ) {
throw new Error( 'quantile()::invalid option. Data type option does not have a corresponding array constructor. Option: `' + dt + '`.' );
}
// Create an output matrix:
d = new ctor( p.length );
out = matrix( d, p.shape, dt );
} else {
out = p;
}
return quantile5( out, p, opts.x0, opts.gamma );
}
if ( isTypedArrayLike( p ) ) {
if ( opts.copy === false ) {
out = p;
} else {
dt = opts.dtype || 'float64';
ctor = ctors( dt );
if ( ctor === null ) {
throw new Error( 'quantile()::invalid option. Data type option does not have a corresponding array constructor. Option: `' + dt + '`.' );
}
out = new ctor( p.length );
}
return quantile6( out, p, opts.x0, opts.gamma );
}
if ( isArrayLike( p ) ) {
// Handle deepset first...
if ( opts.path ) {
opts.sep = opts.sep || '.';
return quantile4( p, opts.x0, opts.gamma, opts.path, opts.sep );
}
// Handle regular and accessor arrays next...
if ( opts.copy === false ) {
out = p;
}
else if ( opts.dtype ) {
ctor = ctors( opts.dtype );
if ( ctor === null ) {
throw new TypeError( 'quantile()::invalid option. Data type option does not have a corresponding array constructor. Option: `' + opts.dtype + '`.' );
}
out = new ctor( p.length );
}
else {
out = new Array( p.length );
}
if ( opts.accessor ) {
return quantile3( out, p, opts.x0, opts.gamma, opts.accessor );
}
return quantile2( out, p, opts.x0, opts.gamma );
}
return NaN;
} // end FUNCTION quantile()
// EXPORTS //
module.exports = quantile;
;