All files / lib/evolution calibration.utils.ts

0% Statements 0/28
0% Branches 0/1
0% Functions 0/1
0% Lines 0/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33                                                                 
// lib/evolution/calibration.utils.ts
export function mean(xs: number[]) {
  if (!xs.length) return 0;
  return xs.reduce((a, b) => a + b, 0) / xs.length;
}
 
export function stdev(xs: number[]) {
  if (xs.length < 2) return 0;
  const m = mean(xs);
  const v = mean(xs.map((x) => (x - m) ** 2));
  return Math.sqrt(v);
}
 
export function minmax(xs: number[]) {
  if (!xs.length) return { min: 0, max: 0 };
  return { min: Math.min(...xs), max: Math.max(...xs) };
}
 
export function clamp(x: number, [lo, hi]: [number, number]) {
  return Math.max(lo, Math.min(hi, x));
}
 
export function movingAverage(xs: number[], k: number) {
  if (!xs.length || k <= 1) return xs.slice();
  const out: number[] = [];
  for (let i = 0; i < xs.length; i++) {
    const start = Math.max(0, i - k + 1);
    const slice = xs.slice(start, i + 1);
    out.push(mean(slice));
  }
  return out;
}