std.js 3.8 KB

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