std.js 3.7 KB

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