UNPKG

3.54 kBJavaScriptView Raw
1import { factory } from '../../utils/factory'
2
3const name = 'std'
4const dependencies = ['typed', 'sqrt', 'variance']
5
6export const createStd = /* #__PURE__ */ factory(name, dependencies, ({ typed, sqrt, variance }) => {
7 /**
8 * Compute the standard deviation of a matrix or a list with values.
9 * The standard deviations is defined as the square root of the variance:
10 * `std(A) = sqrt(variance(A))`.
11 * In case of a (multi dimensional) array or matrix, the standard deviation
12 * over all elements will be calculated by default, unless an axis is specified
13 * in which case the standard deviation will be computed along that axis.
14 *
15 * Additionally, it is possible to compute the standard deviation along the rows
16 * or columns of a matrix by specifying the dimension as the second argument.
17 *
18 * Optionally, the type of normalization can be specified as the final
19 * parameter. The parameter `normalization` can be one of the following values:
20 *
21 * - 'unbiased' (default) The sum of squared errors is divided by (n - 1)
22 * - 'uncorrected' The sum of squared errors is divided by n
23 * - 'biased' The sum of squared errors is divided by (n + 1)
24 *
25 *
26 * Syntax:
27 *
28 * math.std(a, b, c, ...)
29 * math.std(A)
30 * math.std(A, normalization)
31 * math.std(A, dimension)
32 * math.std(A, dimension, normalization)
33 *
34 * Examples:
35 *
36 * math.std(2, 4, 6) // returns 2
37 * math.std([2, 4, 6, 8]) // returns 2.581988897471611
38 * math.std([2, 4, 6, 8], 'uncorrected') // returns 2.23606797749979
39 * math.std([2, 4, 6, 8], 'biased') // returns 2
40 *
41 * math.std([[1, 2, 3], [4, 5, 6]]) // returns 1.8708286933869707
42 * math.std([[1, 2, 3], [4, 6, 8]], 0) // returns [2.1213203435596424, 2.8284271247461903, 3.5355339059327378]
43 * math.std([[1, 2, 3], [4, 6, 8]], 1) // returns [1, 2]
44 * math.std([[1, 2, 3], [4, 6, 8]], 1, 'biased') // returns [0.7071067811865476, 1.4142135623730951]
45 *
46 * See also:
47 *
48 * mean, median, max, min, prod, sum, variance
49 *
50 * @param {Array | Matrix} array
51 * A single matrix or or multiple scalar values
52 * @param {string} [normalization='unbiased']
53 * Determines how to normalize the variance.
54 * Choose 'unbiased' (default), 'uncorrected', or 'biased'.
55 * @param dimension {number | BigNumber}
56 * Determines the axis to compute the standard deviation for a matrix
57 * @return {*} The standard deviation
58 */
59 return typed(name, {
60 // std([a, b, c, d, ...])
61 'Array | Matrix': _std,
62
63 // std([a, b, c, d, ...], normalization)
64 'Array | Matrix, string': _std,
65
66 // std([a, b, c, c, ...], dim)
67 'Array | Matrix, number | BigNumber': _std,
68
69 // std([a, b, c, c, ...], dim, normalization)
70 'Array | Matrix, number | BigNumber, string': _std,
71
72 // std(a, b, c, d, ...)
73 '...': function (args) {
74 return _std(args)
75 }
76 })
77
78 function _std (array, normalization) {
79 if (array.length === 0) {
80 throw new SyntaxError('Function std requires one or more parameters (0 provided)')
81 }
82
83 try {
84 return sqrt(variance.apply(null, arguments))
85 } catch (err) {
86 if (err instanceof TypeError && err.message.indexOf(' variance') !== -1) {
87 throw new TypeError(err.message.replace(' variance', ' std'))
88 } else {
89 throw err
90 }
91 }
92 }
93})