All files / detect attribution.ts

100% Statements 62/62
90.69% Branches 39/43
100% Functions 3/3
100% Lines 62/62

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                                                      1x                   1x 527x 527x 527x 527x 527x 527x 527x 527x   524x 527x 5x 5x 5x 5x 5x 5x     519x     519x 527x 1546x 1546x 1546x 1546x 1546x     519x 519x 510x 9x   527x 1546x 1546x 1546x 527x 527x         1x 519x 519x 519x 519x   7x 7x   519x 16x 11x 14x 25x 25x 25x 20x 20x 25x 11x   7x 5x 2x 519x 1x   1546x 1546x 1546x  
/**
 * DimensionAttribution — leave-one-out Shapley approximation.
 *
 * For multivariate anomaly detection, identifies which dimension(s)
 * contributed most to an elevated anomaly score by masking each
 * dimension individually and measuring the score delta.
 *
 * Algorithm:
 *   1. Run detection with all dimensions → baselineScore
 *   2. For each dimension i:
 *      a. Mask dimension i (replace with its historical mean)
 *      b. Re-run detection → maskedScore_i
 *      c. contribution_i = baselineScore - maskedScore_i
 *   3. Normalize contributions to [-1, 1]
 *
 * @module detect/attribution
 */
 
import type { DataPoint, DimensionAttribution } from '../types.js'
 
/** Score function type — executes detection and returns the score. */
type ScoreFn = (inputArray: number[], timestamp: number) => number
 
/**
 * DimensionAttributor computes per-dimension anomaly contributions
 * using the leave-one-out Shapley approximation.
 */
export class DimensionAttributor {
  /**
   * Compute dimension contributions.
   *
   * @param point - Current data point with dimensions
   * @param context - Historical context (used to compute dimension means)
   * @param inputArray - Current input values (ordered by dimensionNames)
   * @param baselineScore - Score with all dimensions present
   * @param scoreFn - Function that takes (inputArray, timestamp) and returns score
   */
  compute(
    point: DataPoint,
    context: DataPoint[],
    inputArray: number[],
    baselineScore: number,
    scoreFn: ScoreFn
  ): DimensionAttribution[] {
    const dims = point.dimensions
    if (!dims) return []
 
    const names = Object.keys(dims)
    if (names.length <= 1) {
      return names.map((name) => ({
        dimension: name,
        contribution: baselineScore > 0 ? 1 : 0,
        confidence: 1,
      }))
    }
 
    // Compute historical means for each dimension
    const means = this.computeMeans(names, context)
 
    // Leave-one-out: mask each dimension and compute score delta
    const contributions: number[] = []
    for (let i = 0; i < names.length; i++) {
      const masked = [...inputArray]
      masked[i] = means[i] ?? 0
      const maskedScore = scoreFn(masked, point.timestamp)
      contributions.push(baselineScore - maskedScore)
    }
 
    // Normalize
    const absSum = contributions.reduce((s, c) => s + Math.abs(c), 0)
    const normalized = absSum > 0
      ? contributions.map((c) => c / absSum)
      : contributions.map(() => 0)
 
    return names.map((name, i) => ({
      dimension: name,
      contribution: clamp(normalized[i] ?? 0, -1, 1),
      confidence: Math.min(baselineScore / (Math.abs(means[i] ?? 1) + 1), 1),
    }))
  }
 
  /**
   * Compute per-dimension historical means from context.
   */
  private computeMeans(
    names: string[],
    context: DataPoint[]
  ): number[] {
    if (context.length === 0) return names.map(() => 0)
 
    const sums = new Array<number>(names.length).fill(0)
    let count = 0
 
    for (const dp of context) {
      if (!dp.dimensions) continue
      count++
      for (let i = 0; i < names.length; i++) {
        const name = names[i]!
        const val = dp.dimensions[name]
        if (typeof val === 'number' && Number.isFinite(val)) {
          sums[i] = (sums[i] ?? 0) + val
        }
      }
    }
 
    return count > 0
      ? sums.map((s) => s / count)
      : names.map(() => 0)
  }
}
 
function clamp(v: number, min: number, max: number): number {
  return Math.max(min, Math.min(max, v))
}