Removed the ability to insert data as a series of scalar values. Consolidated sorted and normal cases into 1 file. Added ability to use a list of probabilities, or an integer as a number of would be evenly spaced probabilities.

This commit is contained in:
Favian Contreras 2015-05-14 08:06:38 -07:00
parent ae3267d5b8
commit fbfbd030d1
8 changed files with 414 additions and 448 deletions

View File

@ -1,129 +0,0 @@
'use strict';
module.exports = function(math) {
var util = require('../../util/index');
var collection = math.collection;
var BigNumber = math.type.BigNumber;
var Unit = require('../../type/Unit');
var isCollection = collection.isCollection;
var isNumber = util.number.isNumber;
var add = math.add;
var flatten = util.array.flatten;
var multiply = math.multiply;
/**
* Calculate the prob order quantile of an n-dimensional array.
* This function is meant for internal use: it is used by the public functions
*
* @param {Array} args
* @param {Boolean} sorted
* @return {Number, BigNumber, Unit} prob order quantile
* @private
*/
math._quantile_seq = function _quantile_seq(args, sorted) {
// TODO: this is a temporary function, to be removed as soon as the library is modularized (i.e. no dependencies on math from the individual functions)
var prob = args.pop();
if (isCollection(args[0])) {
if (args.length == 1) {
// quantile{_sorted}_seq([a, b, c, d, ...], prob)
return __quantile_seq(args[0].valueOf(), prob, sorted);
}
throw new SyntaxError('Wrong number of parameters');
}
// quantile{_sorted}_seq(a, b, c, d, ..., prob)
return __quantile_seq(args, prob, sorted);
};
/**
* Calculate the prob order quantile of an n-dimensional array.
* @param {Array} array
* @param {Number, BigNumber} prob
* @param {Boolean} sorted
* @return {Number, BigNumber, Unit} prob order quantile
* @private
*/
function __quantile_seq(array, prob, sorted) {
var error_file_str = (sorted) ? 'quantile_sorted_seq' : 'quantile_seq';
if (!isNumber(prob) && !(prob instanceof BigNumber)) {
throw new math.error.UnsupportedTypeError(error_file_str, math['typeof'](prob));
}
if ((isNumber(prob) && (prob < 0 || prob > 1)) ||
(prob instanceof BigNumber && (prob.isNegative() || prob.gt(prob.constructor.ONE)))) {
throw new Error('Probability must be between 0 and 1, inclusive');
}
var flat = flatten(array);
var len = flat.length;
if (len === 0) {
throw new Error('Cannot calculate quantile of an empty sequence');
}
if (!sorted) {
flat.sort(math.compare);
}
if (isNumber(prob)) {
var index = prob * (len-1);
var fracPart = index % 1;
if (fracPart === 0) {
var value = flat[index];
typecheck(error_file_str, value);
return value;
}
var integerPart = Math.floor(index);
var left = flat[integerPart];
var right = flat[integerPart+1];
typecheck(error_file_str, left, right);
return add(multiply(left, 1 - fracPart), multiply(right, fracPart));
}
// If prob is a BigNumber
var index = prob.times(len-1);
if (index.isInteger()) {
var value = flat[index.toNumber()];
typecheck(error_file_str, value);
return value;
}
var integerPart = index.floor();
var fracPart = index.minus(integerPart);
var integerPartNumber = integerPart.toNumber();
var left = flat[integerPartNumber];
var right = flat[integerPartNumber+1];
typecheck(error_file_str, left, right);
var one = fracPart.constructor.ONE;
return add(multiply(left, one.minus(fracPart)), multiply(right, fracPart));
}
/**
* Check if array value types are valid, throw error otherwise.
* @param {String} error_file_str
* @param {*} x
* @param {*} y
* @private
*/
function typecheck(error_file_str, x, y) {
if (!isNumber(x) && !(x instanceof BigNumber) && !(x instanceof Unit)) {
throw new math.error.UnsupportedTypeError(error_file_str, math['typeof'](x));
}
if (y !== undefined && !isNumber(y) && !(y instanceof BigNumber) && !(y instanceof Unit)) {
throw new math.error.UnsupportedTypeError(error_file_str, math['typeof'](y));
}
}
};

View File

@ -0,0 +1,227 @@
'use strict';
module.exports = function (math) {
var util = require('../../util/index');
var collection = math.collection;
var BigNumber = math.type.BigNumber;
var Unit = require('../../type/Unit');
var isArray = Array.isArray;
var isBoolean = util.boolean.isBoolean;
var isCollection = collection.isCollection;
var isInteger = util.number.isInteger;
var isNumber = util.number.isNumber;
var add = math.add;
var flatten = util.array.flatten;
var multiply = math.multiply;
/**
* Compute the prob order quantile of a matrix or a list with values.
* The sequence is sorted and the middle value is returned.
* Supported types of sequence values are: Number, BigNumber, Unit
* Supported types of probablity are: Number, BigNumber
*
* In case of a (multi dimensional) array or matrix, the prob order quantile
* of all elements will be calculated.
*
* Syntax:
*
* math.quantileSeq(A, prob[, sorted])
* math.quantileSeq(A, [prob1, prob2, ...][, sorted])
* math.quantileSeq(A, N[, sorted])
*
* Examples:
*
* math.quantileSeq([3, -1, 5, 7], 0.5); // returns 4
* math.quantileSeq([3, -1, 5, 7], [1/3, 2/3]); // returns [3, 5]
* math.quantileSeq([3, -1, 5, 7], 2); // returns [3, 5]
* math.quantileSeq([-1, 3, 5, 7], 0.5, true); // returns 4
*
* See also:
*
* median, mean, min, max, sum, prod, std, var
*
* @param {Array, Matrix} data A single matrix or Array
* @param {Number, BigNumber, Array} probOrN prob is the order of the quantile, while N is
* the amount of evenly distributed steps of
* probabilities; only one of these options can
* be provided
* @param {Boolean} sorted=False is data sorted in ascending order
* @return {Number, BigNumber, Unit, Array} Quantile(s)
*/
math.quantileSeq = function quantileSeq(data, probOrN, sorted) {
var probArr, dataArr, one;
if (arguments.length < 2 || arguments.length > 3) {
throw new SyntaxError('Function quantileSeq requires two or three parameters');
}
if (isCollection(data)) {
sorted = sorted || false;
if (isBoolean(sorted)) {
dataArr = data.valueOf();
if (isNumber(probOrN)) {
if (probOrN < 0) {
throw new Error('N/prob must be non-negative');
}
if (probOrN <= 1) {
// quantileSeq([a, b, c, d, ...], prob[,sorted])
return _quantileSeq(dataArr, probOrN, sorted);
}
if (probOrN > 1) {
// quantileSeq([a, b, c, d, ...], N[,sorted])
if (!isInteger(probOrN)) {
throw new Error('N must be a positive integer');
}
var nPlusOne = probOrN + 1;
probArr = new Array(probOrN);
for (var i = 0; i < probOrN;) {
probArr[i] = _quantileSeq(dataArr, (++i) / nPlusOne, sorted);
}
return probArr;
}
}
if (probOrN instanceof BigNumber) {
if (probOrN.isNegative()) {
throw new Error('N/prob must be non-negative');
}
one = probOrN.constructor.ONE;
if (probOrN.lte(one)) {
// quantileSeq([a, b, c, d, ...], prob[,sorted])
return _quantileSeq(dataArr, probOrN, sorted);
}
if (probOrN.gt(one)) {
// quantileSeq([a, b, c, d, ...], N[,sorted])
if (!probOrN.isInteger()) {
throw new Error('N must be a positive integer');
}
var intN = probOrN.toNumber();
var nPlusOne = new BigNumber(intN + 1);
probArr = new Array(intN);
for (var i = 0; i < intN;) {
probArr[i] = _quantileSeq(dataArr, new BigNumber(++i).div(nPlusOne), sorted);
}
return probArr;
}
}
if (isArray(probOrN)) {
// quantileSeq([a, b, c, d, ...], [prob][,sorted])
probArr = new Array(probOrN.length);
for (var i = 0; i < probArr.length; ++i) {
var currProb = probOrN[i];
if (isNumber(currProb)) {
if (currProb < 0 || currProb > 1) {
throw new Error('Probability must be between 0 and 1, inclusive');
}
} else if (currProb instanceof BigNumber) {
one = currProb.constructor.ONE;
if (currProb.isNegative() || currProb.gt(one)) {
throw new Error('Probability must be between 0 and 1, inclusive');
}
} else {
throw new math.error.UnsupportedTypeError('quantileSeq', math['typeof'](currProb));
}
probArr[i] = _quantileSeq(dataArr, currProb, sorted);
}
return probArr;
}
throw new math.error.UnsupportedTypeError('quantileSeq', math['typeof'](probOrN));
}
throw new math.error.UnsupportedTypeError('quantileSeq', math['typeof'](sorted));
}
throw new math.error.UnsupportedTypeError('quantileSeq', math['typeof'](data));
};
/**
* Calculate the prob order quantile of an n-dimensional array.
*
* @param {Array} array
* @param {Number, BigNumber} prob
* @param {Boolean} sorted
* @return {Number, BigNumber, Unit} prob order quantile
* @private
*/
function _quantileSeq(array, prob, sorted) {
var flat = flatten(array);
var len = flat.length;
if (len === 0) {
throw new Error('Cannot calculate quantile of an empty sequence');
}
if (!sorted) {
flat.sort(math.compare);
}
if (isNumber(prob)) {
var index = prob * (len-1);
var fracPart = index % 1;
if (fracPart === 0) {
var value = flat[index];
typecheck(value);
return value;
}
var integerPart = Math.floor(index);
var left = flat[integerPart];
var right = flat[integerPart+1];
typecheck(left, right);
return add(multiply(left, 1 - fracPart), multiply(right, fracPart));
}
// If prob is a BigNumber
var index = prob.times(len-1);
if (index.isInteger()) {
var value = flat[index.toNumber()];
typecheck(value);
return value;
}
var integerPart = index.floor();
var fracPart = index.minus(integerPart);
var integerPartNumber = integerPart.toNumber();
var left = flat[integerPartNumber];
var right = flat[integerPartNumber+1];
typecheck(left, right);
var one = fracPart.constructor.ONE;
return add(multiply(left, one.minus(fracPart)), multiply(right, fracPart));
}
/**
* Check if array value types are valid, throw error otherwise.
* @param {*} x
* @param {*} y
* @private
*/
function typecheck(x, y) {
if (!isNumber(x) && !(x instanceof BigNumber) && !(x instanceof Unit)) {
throw new math.error.UnsupportedTypeError('quantileSeq', math['typeof'](x));
}
if (y !== undefined && !isNumber(y) && !(y instanceof BigNumber) && !(y instanceof Unit)) {
throw new math.error.UnsupportedTypeError('quantileSeq', math['typeof'](y));
}
}
};

View File

@ -1,47 +0,0 @@
'use strict';
module.exports = function (math) {
var util = require('../../util/index');
var _quantile_seq = math._quantile_seq;
/**
* Compute the prob order quantile of a matrix or a list with values.
* The sequence is sorted and the middle value is returned.
* Supported types of sequence values are: Number, BigNumber, Unit
* Supported types of probablity are: Number, BigNumber
*
* In case of a (multi dimensional) array or matrix, the prob order quantile
* of all elements will be calculated.
*
* Syntax:
*
* math.quantile_seq(a, b, c, ..., prob)
* math.quantile_seq(A, prob)
*
* Examples:
*
* math.quantile_seq(5, 2, 7, 0.4); // returns 4.4
* math.quantile_seq([3, -1, 5, 7], 0.5); // returns 4
*
* See also:
*
* median, mean, min, max, sum, prod, std, var, multiply
*
* @param {... *} args A single matrix or or multiple scalar values
* @param {Number, BigNumber} prob
* @return {Number, BigNumber, Unit} prob order quantile
*/
math.quantile_seq = function quantile_seq(args, prob) {
if (arguments.length < 2) {
throw new SyntaxError('Function quantile_seq requires two or more parameters');
}
var arg_arr = new Array(arguments.length);
for (var i = 0; i < arg_arr.length; ++i) {
arg_arr[i] = arguments[i];
}
return _quantile_seq(arg_arr, false);
};
};

View File

@ -1,44 +0,0 @@
'use strict';
module.exports = function (math) {
var util = require('../../util/index');
var _quantile_seq = math._quantile_seq;
/**
* Compute the prob order quantile of a matrix or a list with values.
* NOTE: This function assumes the sequence is sorted in ascending order.
* See quantile_seq for more functional information.
*
* Syntax:
*
* math.quantile_sorted_seq(a, b, c, ..., prob)
* math.quantile_sorted_seq(A, prob)
*
* Examples:
*
* math.quantile_sorted_seq(2, 5, 7, 0.4); // returns 4.4
* math.quantile_sorted_seq([-1, 3, 5, 7], 0.5); // returns 4
* math.quantile_sorted_seq([[-1, 3], [5, 7]], 0.5); // returns 4
*
* See also:
*
* quantile_seq
*
* @param {... *} ascending_args A single matrix or multiple scalar values
* @param {Number, BigNumber} prob
* @return {Number, BigNumber, Unit} prob order quantile
*/
math.quantile_sorted_seq = function quantile_sorted_seq(ascending_args, prob) {
if (arguments.length < 2) {
throw new SyntaxError('Function quantile_sorted_seq requires two or more parameters');
}
var arg_arr = new Array(arguments.length);
for (var i = 0; i < arg_arr.length; ++i) {
arg_arr[i] = arguments[i];
}
return _quantile_seq(arg_arr, true);
};
};

View File

@ -329,9 +329,7 @@ function create (config) {
require('./function/statistics/mean')(math, _config);
require('./function/statistics/median')(math, _config);
require('./function/statistics/prod')(math, _config);
require('./function/statistics/_quantile_seq')(math, _config);
require('./function/statistics/quantile_seq')(math, _config);
require('./function/statistics/quantile_sorted_seq')(math, _config);
require('./function/statistics/quantileSeq')(math, _config);
require('./function/statistics/std')(math, _config);
require('./function/statistics/sum')(math, _config);
require('./function/statistics/var')(math, _config);

View File

@ -0,0 +1,186 @@
var assert = require('assert'),
approx = require('../../../tools/approx'),
math = require('../../../index'),
bignumber = math.bignumber,
quantileSeq = math.quantileSeq;
describe('quantileSeq', function() {
it('should return the quantileSeq from an array with number probability', function() {
var lst = [3.7, 2.7, 3.3, 1.3, 2.2, 3.1];
assert.equal(quantileSeq(lst, 0), 1.3);
assert.equal(quantileSeq(lst, 0.1), 1.75);
assert.equal(quantileSeq(lst, 0.2), 2.2);
assert.equal(quantileSeq(lst, 0.25), 2.325);
assert.equal(quantileSeq(lst, 0.25, false), 2.325);
assert.equal(quantileSeq(lst, 0.3), 2.45);
assert.equal(quantileSeq(lst, 0.4), 2.7);
approx.equal(quantileSeq(lst, 0.5), 2.9);
assert.equal(quantileSeq(lst, 0.6), 3.1);
assert.equal(quantileSeq(lst, 0.7), 3.2);
approx.equal(quantileSeq(lst, 0.75), 3.25);
assert.equal(quantileSeq(lst, 0.8), 3.3);
assert.equal(quantileSeq(lst, 0.9), 3.5);
assert.equal(quantileSeq(lst, 1), 3.7);
});
it('should return the quantileSeq from an ascending array with number probability', function() {
var lst = [1.3, 2.2, 2.7, 3.1, 3.3, 3.7];
assert.equal(quantileSeq(lst, 0, true), 1.3);
assert.equal(quantileSeq(lst, 0.1, true), 1.75);
assert.equal(quantileSeq(lst, 0.2, true), 2.2);
assert.equal(quantileSeq(lst, 0.25, true), 2.325);
assert.equal(quantileSeq(lst, 0.3, true), 2.45);
assert.equal(quantileSeq(lst, 0.4, true), 2.7);
approx.equal(quantileSeq(lst, 0.5, true), 2.9);
assert.equal(quantileSeq(lst, 0.6, true), 3.1);
assert.equal(quantileSeq(lst, 0.7, true), 3.2);
approx.equal(quantileSeq(lst, 0.75, true), 3.25);
assert.equal(quantileSeq(lst, 0.8, true), 3.3);
assert.equal(quantileSeq(lst, 0.9, true), 3.5);
assert.equal(quantileSeq(lst, 1, true), 3.7);
});
it('should return the quantileSeq from an array with BigNumber probability', function() {
var lst = [3.7, 2.7, 3.3, 1.3, 2.2, 3.1];
assert.equal(quantileSeq(lst, bignumber(0)), 1.3);
assert.equal(quantileSeq(lst, bignumber(0.1)), 1.75);
assert.equal(quantileSeq(lst, bignumber(0.2)), 2.2);
assert.equal(quantileSeq(lst, bignumber(0.25)), 2.325);
assert.equal(quantileSeq(lst, bignumber(0.3)), 2.45);
assert.equal(quantileSeq(lst, bignumber(0.4)), 2.7);
assert.equal(quantileSeq(lst, bignumber(0.5)), 2.9);
assert.equal(quantileSeq(lst, bignumber(0.6)), 3.1);
assert.equal(quantileSeq(lst, bignumber(0.7)), 3.2);
assert.equal(quantileSeq(lst, bignumber(0.75)), 3.25);
assert.equal(quantileSeq(lst, bignumber(0.8)), 3.3);
assert.equal(quantileSeq(lst, bignumber(0.9)), 3.5);
assert.equal(quantileSeq(lst, bignumber(1)), 3.7);
});
it('should return the quantileSeq of an array of bignumbers with number probability', function() {
approx.equal(quantileSeq([bignumber(0.5377),bignumber(1.8339),bignumber(-2.2588),bignumber(0.8622),
bignumber(0.3188),bignumber(-1.3077),bignumber(-0.4336),bignumber(0.3426),
bignumber(3.5784),bignumber(2.7694)],0.3),
0.09308);
});
it('should return the quantileSeq of an array of bignumbers with BigNumber probability', function() {
assert.deepEqual(quantileSeq([bignumber(0.5377),bignumber(1.8339),bignumber(-2.2588),bignumber(0.8622),
bignumber(0.3188),bignumber(-1.3077),bignumber(-0.4336),bignumber(0.3426),
bignumber(3.5784),bignumber(2.7694)],bignumber(0.3)),
bignumber(0.09308));
});
it('should return the quantileSeq of units', function() {
assert.deepEqual(quantileSeq([math.unit('5mm'), math.unit('15mm'), math.unit('10mm')], 0.5), math.unit('10mm'));
});
it('should return the quantileSeq from an 1d matrix', function() {
assert.equal(quantileSeq(math.matrix([2,4,6,8,10,12,14]), 0.25), 5);
});
it('should return the quantileSeq from a 2d array', function() {
approx.equal(quantileSeq([
[3.7, 2.7, 3.3],
[1.3, 2.2, 3.1]
], 0.75), 3.25);
});
it('should return the quantileSeq from an ascending 2d array', function() {
approx.equal(quantileSeq([
[1.3, 2.2, 2.7],
[3.1, 3.3, 3.7]
], 0.75, true), 3.25);
});
it('should return the quantileSeq from a 2d matrix', function() {
approx.equal(quantileSeq(math.matrix([
[3.7, 2.7, 3.3],
[1.3, 2.2, 3.1]
]), 0.75), 3.25);
});
it('should return the quantileSeq from an ascending 2d matrix', function() {
approx.equal(quantileSeq(math.matrix([
[1.3, 2.2, 2.7],
[3.1, 3.3, 3.7]
]), 0.75, true), 3.25);
});
it('should return list quantiles for list of number probabilities', function() {
var lst = [3.7, 2.7, 3.3, 1.3, 2.2, 3.1];
approx.deepEqual(quantileSeq(lst, [0.25, 0.5, 0.75]), [2.325, 2.9, 3.25]);
approx.deepEqual(quantileSeq(lst, [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]),
[1.75, 2.2, 2.45, 2.7, 2.9, 3.1, 3.2, 3.3, 3.5]);
});
it('should return list quantiles for list of BigNumber probabilities', function() {
var lst = [3.7, 2.7, 3.3, 1.3, 2.2, 3.1];
assert.deepEqual(quantileSeq(lst, [bignumber(0.25), bignumber(0.5), bignumber(0.75)]),
[bignumber(2.325), bignumber(2.9), bignumber(3.25)]);
assert.equal(quantileSeq(lst, [bignumber(0.1), bignumber(0.2), bignumber(0.3), bignumber(0.4),
bignumber(0.5), bignumber(0.6), bignumber(0.7), bignumber(0.8),
bignumber(0.9)]).toString(), '1.75,2.2,2.45,2.7,2.9,3.1,3.2,3.3,3.5');
});
it('should return list quantiles for list of number and BigNumber probabilities', function() {
var lst = [3.7, 2.7, 3.3, 1.3, 2.2, 3.1];
approx.deepEqual(quantileSeq(lst, [0.25, bignumber(0.5), 0.75]), [2.325, 2.9, 3.25]);
approx.deepEqual(quantileSeq(lst, [0.1, 0.2, bignumber(0.3), 0.4, 0.5, 0.6, 0.7, bignumber(0.8), 0.9]),
[1.75, 2.2, 2.45, 2.7, 2.9, 3.1, 3.2, 3.3, 3.5]);
});
it('should return the evenly number spaced quantiles of an array', function() {
var lst = [3.7, 2.7, 3.3, 1.3, 2.2, 3.1];
approx.deepEqual(quantileSeq(lst, 3), [2.325, 2.9, 3.25]);
approx.deepEqual(quantileSeq(lst, 9), [1.75, 2.2, 2.45, 2.7, 2.9, 3.1, 3.2, 3.3, 3.5]);
});
it('should return the evenly BigNumber spaced quantiles of an array', function() {
var lst = [3.7, 2.7, 3.3, 1.3, 2.2, 3.1];
assert.deepEqual(quantileSeq(lst, bignumber(3)), [bignumber(2.325), bignumber(2.9), bignumber(3.25)]);
assert.equal(quantileSeq(lst, bignumber(9)).toString(), "1.75,2.2,2.45,2.7,2.9,3.1,3.2,3.3,3.5");
});
it('should throw an error if called with invalid number of arguments', function() {
assert.throws(function() {quantileSeq()}, SyntaxError);
assert.throws(function() {quantileSeq(2)}, SyntaxError);
assert.throws(function() {quantileSeq([], 2, 3, 1)}, SyntaxError);
});
it('should throw an error if called with unsupported type of arguments', function() {
assert.throws(function () {quantileSeq([2,4,6,8,10,12,14], 0.25, 10)}, math.error.UnsupportedTypeError);
assert.throws(function () {quantileSeq([2,4,6,8,10,12,14], [0.25, 2])}, math.error.UnsuppoError);
assert.throws(function () {quantileSeq('A', 'C', 'B')}, math.error.UnsupportedTypeError);
assert.throws(function () {quantileSeq(true, false, true)}, math.error.UnsupportedTypeError);
assert.throws(function () {quantileSeq(0, 'B')}, math.error.UnsupportedTypeError);
assert.throws(function () {quantileSeq(math.complex(2,3), math.complex(-1,2))}, TypeError);
});
it('should throw error for bad probabilities and splits', function() {
assert.throws(function () {quantileSeq([2,4,6,8,10,12,14], [0.23, 2, 0.2])}, Error);
assert.throws(function () {quantileSeq([2,4,6,8,10,12,14], [0.23, bignumber(2), 0.2])}, Error);
assert.throws(function () {quantileSeq([2,4,6,8,10,12,14], -2)}, Error);
assert.throws(function () {quantileSeq([2,4,6,8,10,12,14], bignumber(-2))}, Error);
});
it('should throw an error if called with an empty array', function() {
assert.throws(function() {quantileSeq([])});
});
it('should not mutate the input', function () {
var a = [3,2,1];
quantileSeq(a, 0.2);
quantileSeq(a, 2);
quantileSeq(a, [0.1, 0.3]);
assert.deepEqual(a,[3,2,1]);
});
/*
it('should LaTeX quantileSeq', function () {
var expression = math.parse('quantileSeq(1,2,3,4,0.3)');
assert.equal(expression.toTex(), '\\mathrm{quantile}\\left(1,2,3,4,0.3\\right)');
});
*/
});

View File

@ -1,111 +0,0 @@
var assert = require('assert'),
approx = require('../../../tools/approx'),
math = require('../../../index'),
bignumber = math.bignumber,
quantile_seq = math.quantile_seq;
describe('quantile_seq', function() {
it('should return the quantile_seq of a sequence of numbers with number probability', function() {
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0), 1.3);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.1), 1.75);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.2), 2.2);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.25), 2.325);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.3), 2.45);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.4), 2.7);
approx.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.5), 2.9);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.6), 3.1);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.7), 3.2);
approx.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.75), 3.25);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.8), 3.3);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 0.9), 3.5);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, 1), 3.7);
});
it('should return the quantile_seq of a sequence of numbers with BigNumber probability', function() {
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0)), 1.3);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.1)), 1.75);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.2)), 2.2);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.25)), 2.325);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.3)), 2.45);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.4)), 2.7);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.5)), 2.9);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.6)), 3.1);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.7)), 3.2);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.75)), 3.25);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.8)), 3.3);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(0.9)), 3.5);
assert.equal(quantile_seq(3.7, 2.7, 3.3, 1.3, 2.2, 3.1, bignumber(1)), 3.7);
});
it('should return the quantile_seq of a sequence of bignumbers with number probability', function() {
approx.equal(quantile_seq(bignumber(0.5377),bignumber(1.8339),bignumber(-2.2588),bignumber(0.8622),
bignumber(0.3188),bignumber(-1.3077),bignumber(-0.4336),bignumber(0.3426),
bignumber(3.5784),bignumber(2.7694),0.3),
0.09308);
});
it('should return the quantile_seq of a sequence of bignumbers with BigNumber probability', function() {
assert.deepEqual(quantile_seq(bignumber(0.5377),bignumber(1.8339),bignumber(-2.2588),bignumber(0.8622),
bignumber(0.3188),bignumber(-1.3077),bignumber(-0.4336),bignumber(0.3426),
bignumber(3.5784),bignumber(2.7694),bignumber(0.3)),
bignumber(0.09308));
});
it('should return the quantile_seq from an array', function() {
assert.equal(quantile_seq([2,4,6,8,10,12,14], 0.25), 5);
});
it('should return the quantile_seq of units', function() {
assert.deepEqual(quantile_seq([math.unit('5mm'), math.unit('15mm'), math.unit('10mm')], 0.5), math.unit('10mm'));
});
it('should return the quantile_seq from an 1d matrix', function() {
assert.equal(quantile_seq(math.matrix([2,4,6,8,10,12,14]), 0.25), 5);
});
it('should return the quantile_seq from a 2d array', function() {
approx.equal(quantile_seq([
[3.7, 2.7, 3.3],
[1.3, 2.2, 3.1]
], 0.75), 3.25);
});
it('should return the quantile_seq from a 2d matrix', function() {
approx.equal(quantile_seq(math.matrix([
[3.7, 2.7, 3.3],
[1.3, 2.2, 3.1]
]), 0.75), 3.25);
});
it('should throw an error if called with invalid number of arguments', function() {
assert.throws(function() {quantile_seq()});
assert.throws(function() {quantile_seq(2)});
assert.throws(function() {quantile_seq([], 2, 3)});
});
it('should throw an error if called with unsupported type of arguments', function() {
assert.throws(function () {quantile_seq('A', 'C', 'D', 'B')}, math.error.UnsupportedTypeError);
assert.throws(function () {quantile_seq('A', 'C', 'B')}, math.error.UnsupportedTypeError);
assert.throws(function () {quantile_seq(true, false, true)}, math.error.UnsupportedTypeError);
assert.throws(function () {quantile_seq(0, 'B')}, math.error.UnsupportedTypeError);
assert.throws(function () {quantile_seq(math.complex(2,3), math.complex(-1,2))}, TypeError);
});
it('should throw an error if called with an empty array', function() {
assert.throws(function() {quantile_seq([])});
});
it('should not mutate the input', function () {
var a = [3,2,1];
var b = quantile_seq(a, 0.2);
assert.deepEqual(a,[3,2,1]);
});
/*
it('should LaTeX quantile_seq', function () {
var expression = math.parse('quantile_seq(1,2,3,4)');
assert.equal(expression.toTex(), '\\mathrm{quantile}\\left(1,2,3,4\\right)');
});
*/
});

View File

@ -1,114 +0,0 @@
var assert = require('assert'),
approx = require('../../../tools/approx'),
math = require('../../../index'),
bignumber = math.bignumber,
quantile_sorted_seq = math.quantile_sorted_seq;
describe('quantile_sorted_seq', function() {
it('should return the quantile_sorted_seq of a sequence of numbers with number probability', function() {
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0), 1.3);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.1), 1.75);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.2), 2.2);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.25), 2.325);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.3), 2.45);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.4), 2.7);
approx.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.5), 2.9);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.6), 3.1);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.7), 3.2);
approx.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.75), 3.25);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.8), 3.3);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 0.9), 3.5);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, 1), 3.7);
});
it('should return the quantile_sorted_seq of a sequence of numbers with BigNumber probability', function() {
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0)), 1.3);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.1)), 1.75);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.2)), 2.2);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.25)), 2.325);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.3)), 2.45);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.4)), 2.7);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.5)), 2.9);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.6)), 3.1);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.7)), 3.2);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.75)), 3.25);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.8)), 3.3);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(0.9)), 3.5);
assert.equal(quantile_sorted_seq(1.3, 2.2, 2.7, 3.1, 3.3, 3.7, bignumber(1)), 3.7);
});
it('should return the quantile_sorted_seq of a sequence of bignumbers with number probability', function() {
approx.equal(quantile_sorted_seq(bignumber(-2.2588), bignumber(-1.3077), bignumber(-0.4336),
bignumber(0.3188), bignumber(0.3426), bignumber(0.5377),
bignumber(0.8622), bignumber(1.8339), bignumber(2.7694),
bignumber(3.5784), 0.3),
0.09308);
});
it('should return the quantile_sorted_seq of a sequence of bignumbers with BigNumber probability', function() {
assert.deepEqual(quantile_sorted_seq(bignumber(-2.2588), bignumber(-1.3077), bignumber(-0.4336),
bignumber(0.3188), bignumber(0.3426), bignumber(0.5377),
bignumber(0.8622), bignumber(1.8339), bignumber(2.7694),
bignumber(3.5784), bignumber(0.3)),
bignumber(0.09308));
});
it('should return the quantile_sorted_seq from an array', function() {
assert.equal(quantile_sorted_seq([2,4,6,8,10,12,14], 0.25), 5);
});
it('should return the quantile_sorted_seq of units', function() {
assert.deepEqual(quantile_sorted_seq([math.unit('5mm'), math.unit('10mm'), math.unit('15mm')], 0.5), math.unit('10mm'));
assert.deepEqual(quantile_sorted_seq([math.unit('5mm'), math.unit('10mm'), math.unit('12mm'), math.unit('15mm')], 0.5), math.unit('11mm'));
});
it('should return the quantile_sorted_seq from an 1d matrix', function() {
assert.equal(quantile_sorted_seq(math.matrix([2,4,6,8,10,12,14]), 0.25), 5);
});
it('should return the quantile_sorted_seq from a 2d array', function() {
approx.equal(quantile_sorted_seq([
[1.3, 2.2, 2.7],
[3.1, 3.3, 3.7]
], 0.75), 3.25);
});
it('should return the quantile_sorted_seq from a 2d matrix', function() {
approx.equal(quantile_sorted_seq(math.matrix([
[1.3, 2.2, 2.7],
[3.1, 3.3, 3.7]
]), 0.75), 3.25);
});
it('should throw an error if called with invalid number of arguments', function() {
assert.throws(function() {quantile_sorted_seq()});
assert.throws(function() {quantile_sorted_seq(2)});
assert.throws(function() {quantile_sorted_seq([], 2, 3)});
});
it('should throw an error if called with unsupported type of arguments', function() {
assert.throws(function () {quantile_sorted_seq('A', 'C', 'D', 'B')}, math.error.UnsupportedTypeError);
assert.throws(function () {quantile_sorted_seq('A', 'C', 'B')}, math.error.UnsupportedTypeError);
assert.throws(function () {quantile_sorted_seq(true, false, true)}, math.error.UnsupportedTypeError);
assert.throws(function () {quantile_sorted_seq(0, 'B')}, math.error.UnsupportedTypeError);
assert.throws(function () {quantile_sorted_seq(math.complex(2,3), math.complex(-1,2))}, TypeError);
});
it('should throw an error if called with an empty array', function() {
assert.throws(function() {quantile_sorted_seq([])});
});
it('should not mutate the input', function () {
var a = [1,2,3];
var b = quantile_sorted_seq(a, 0.2);
assert.deepEqual(a,[1,2,3]);
});
/*
it('should LaTeX quantile_sorted_seq', function () {
var expression = math.parse('quantile_sorted_seq(1,2,3,4)');
assert.equal(expression.toTex(), '\\mathrm{quantile}\\left(1,2,3,4\\right)');
});
*/
});