|
| 1 | +import type { Aggregator } from "../Aggregator.ts"; |
| 2 | +import type { Record } from "../Record.ts"; |
| 3 | +import type { JsonValue } from "../types/json.ts"; |
| 4 | +import { findKey } from "../KeySpec.ts"; |
| 5 | +import { aggregatorRegistry } from "../Aggregator.ts"; |
| 6 | + |
| 7 | +// [sum1, sumX, sumY, sumXY, sumX2, sumY2] |
| 8 | +type Ord2BivState = [number, number, number, number, number, number]; |
| 9 | + |
| 10 | +/** |
| 11 | + * Second-order bivariate statistics aggregator. |
| 12 | + * Computes covariance, correlation, and linear regression parameters |
| 13 | + * between two fields using a single pass. |
| 14 | + * |
| 15 | + * Analogous to App::RecordStream::Aggregator::Ord2Bivariate in Perl. |
| 16 | + */ |
| 17 | +export class Ord2BivariateAggregator implements Aggregator<Ord2BivState | null> { |
| 18 | + fieldX: string; |
| 19 | + fieldY: string; |
| 20 | + |
| 21 | + constructor(fieldX: string, fieldY: string) { |
| 22 | + this.fieldX = fieldX; |
| 23 | + this.fieldY = fieldY; |
| 24 | + } |
| 25 | + |
| 26 | + initial(): Ord2BivState | null { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + combine(state: Ord2BivState | null, record: Record): Ord2BivState | null { |
| 31 | + const vx = findKey(record.dataRef(), this.fieldX, true); |
| 32 | + const vy = findKey(record.dataRef(), this.fieldY, true); |
| 33 | + if (vx === undefined || vx === null || vy === undefined || vy === null) return state; |
| 34 | + const x = Number(vx); |
| 35 | + const y = Number(vy); |
| 36 | + const mapped: Ord2BivState = [1, x, y, x * y, x * x, y * y]; |
| 37 | + if (state === null) return mapped; |
| 38 | + return [ |
| 39 | + state[0] + mapped[0], |
| 40 | + state[1] + mapped[1], |
| 41 | + state[2] + mapped[2], |
| 42 | + state[3] + mapped[3], |
| 43 | + state[4] + mapped[4], |
| 44 | + state[5] + mapped[5], |
| 45 | + ]; |
| 46 | + } |
| 47 | + |
| 48 | + squish(state: Ord2BivState | null): JsonValue { |
| 49 | + if (state === null) return null; |
| 50 | + const [n, sumX, sumY, sumXY, sumX2, sumY2] = state; |
| 51 | + |
| 52 | + const meanX = sumX / n; |
| 53 | + const meanY = sumY / n; |
| 54 | + |
| 55 | + // Covariance: E[XY] - E[X]*E[Y] |
| 56 | + const covariance = sumXY / n - meanX * meanY; |
| 57 | + |
| 58 | + // Variances |
| 59 | + const varX = sumX2 / n - meanX * meanX; |
| 60 | + const varY = sumY2 / n - meanY * meanY; |
| 61 | + |
| 62 | + // Correlation: cov / (stdX * stdY) |
| 63 | + const denominator = Math.sqrt(varX * varY); |
| 64 | + const correlation = denominator > 0 |
| 65 | + ? (sumXY * n - sumX * sumY) / Math.sqrt((sumX2 * n - sumX ** 2) * (sumY2 * n - sumY ** 2)) |
| 66 | + : null; |
| 67 | + |
| 68 | + // Linear regression: y = alpha + beta * x |
| 69 | + const betaDenom = sumX2 * n - sumX ** 2; |
| 70 | + const beta = betaDenom !== 0 ? (sumXY * n - sumX * sumY) / betaDenom : null; |
| 71 | + const alpha = beta !== null ? (sumY - beta * sumX) / n : null; |
| 72 | + |
| 73 | + const result: { [key: string]: JsonValue } = { |
| 74 | + count: n, |
| 75 | + covariance, |
| 76 | + correlation, |
| 77 | + }; |
| 78 | + |
| 79 | + if (alpha !== null && beta !== null) { |
| 80 | + result["alpha"] = alpha; |
| 81 | + result["beta"] = beta; |
| 82 | + } |
| 83 | + |
| 84 | + return result; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +aggregatorRegistry.register("ord2biv", { |
| 89 | + create: (fieldX: string, fieldY: string) => new Ord2BivariateAggregator(fieldX, fieldY), |
| 90 | + argCounts: [2], |
| 91 | + shortUsage: "compute second-order bivariate statistics for two fields", |
| 92 | + longUsage: |
| 93 | + "Usage: ord2biv,<field1>,<field2>\n" + |
| 94 | + " Compute covariance, correlation, and linear regression parameters\n" + |
| 95 | + " between two fields.", |
| 96 | + aliases: ["ord2bivariate"], |
| 97 | +}); |
0 commit comments