File size: 505 Bytes
582228b
 
 
 
22f5471
582228b
 
 
22f5471
582228b
 
 
 
 
 
 
 
22f5471
 
582228b
 
 
22f5471
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
let stats = {
  mean: 0,
  std: 1,
};

export function trainModel(values) {
  const n = values.length;
  if (n === 0) return;

  const mean = values.reduce((a, b) => a + b, 0) / n;
  const variance =
    values.reduce((a, b) => a + (b - mean) ** 2, 0) / n;

  stats.mean = mean;
  stats.std = Math.sqrt(variance) || 1;

  console.log("📊 Anomaly model trained", stats);
}

export function isAnomaly(value) {
  const z = Math.abs((value - stats.mean) / stats.std);
  return z > 3; // Z-score threshold
}