MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / bigPopulationVariance

Function bigPopulationVariance

src/compute-engine/numerics/statistics.ts:83–98  ·  view source on GitHub ↗
(
  values: Iterable<BigDecimal>
)

Source from the content-addressed store, hash-verified

81 */
82function welford(values: Iterable<number>): {
83 n: number;
84 m2: number;
85 scale: number;
86} {
87 let n = 0;
88 let mean = 0;
89 let m2 = 0;
90 // 0 while the update runs on the data as it is; otherwise the unit that
91 // `mean` and `m2` are held in (`mean / scale`, `m2 / scale²`).
92 let scale = 0;
93 for (const x of values) {
94 n += 1;
95 if (scale === 0) {
96 const before = x - mean;
97 const nextMean = mean + before / n;
98 const nextM2 = m2 + before * (x - nextMean);
99 if (Number.isFinite(nextM2) || !Number.isFinite(x)) {
100 mean = nextMean;
101 m2 = nextM2;

Callers 2

statistics.tsFile · 0.90

Calls 4

addMethod · 0.65
mulMethod · 0.65
divMethod · 0.65
subMethod · 0.65

Tested by

no test coverage detected