quantile.d.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * This is the internal implementation of quantiles: when you know
  3. * that the order is sorted, you don't need to re-sort it, and the computations
  4. * are faster.
  5. *
  6. * @param {Array<number>} x sample of one or more data points
  7. * @param {number} p desired quantile: a number between 0 to 1, inclusive
  8. * @returns {number} quantile value
  9. * @throws {Error} if p ix outside of the range from 0 to 1
  10. * @throws {Error} if x is empty
  11. * @example
  12. * quantileSorted([3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20], 0.5); // => 9
  13. */
  14. export declare function quantileSorted(x: number[], p: number): number;
  15. /**
  16. * 交换数组位置
  17. * @param arr T[]
  18. * @param i number
  19. * @param j number
  20. */
  21. export declare function swap<T = any>(arr: T[], i: number, j: number): void;
  22. /**
  23. * Rearrange items in `arr` so that all items in `[left, k]` range are the smallest.
  24. * The `k`-th element will have the `(k - left + 1)`-th smallest value in `[left, right]`.
  25. *
  26. * Implements Floyd-Rivest selection algorithm https://en.wikipedia.org/wiki/Floyd-Rivest_algorithm
  27. *
  28. * @param {Array<number>} arr input array
  29. * @param {number} k pivot index
  30. * @param {number} [left] left index
  31. * @param {number} [right] right index
  32. * @returns {void} mutates input array
  33. * @example
  34. * var arr = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39];
  35. * quickselect(arr, 8);
  36. * // = [39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]
  37. */
  38. export declare function quickselect(arr: number[], k: any, left?: number, right?: number): void;
  39. /**
  40. * The [quantile](https://en.wikipedia.org/wiki/Quantile):
  41. * this is a population quantile, since we assume to know the entire
  42. * dataset in this library. This is an implementation of the
  43. * [Quantiles of a Population](http://en.wikipedia.org/wiki/Quantile#Quantiles_of_a_population)
  44. * algorithm from wikipedia.
  45. *
  46. * Sample is a one-dimensional array of numbers,
  47. * and p is either a decimal number from 0 to 1 or an array of decimal
  48. * numbers from 0 to 1.
  49. * In terms of a k/q quantile, p = k/q - it's just dealing with fractions or dealing
  50. * with decimal values.
  51. * When p is an array, the result of the function is also an array containing the appropriate
  52. * quantiles in input order
  53. *
  54. * @param {Array<number>} x sample of one or more numbers
  55. * @param {Array<number> | number} p the desired quantile, as a number between 0 and 1
  56. * @returns {number} quantile
  57. * @example
  58. * quantile([3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20], 0.5); // => 9
  59. */
  60. declare function quantile(x: number[], p: number): number;
  61. declare function quantile(x: number[], p: number[]): number[];
  62. export { quantile };