nx_sketch_correlation.nx
buildroot/runtime/nx_sketch_correlation.nx
about
sketch_correlation.nx -- streaming Pearson correlation + linear regression.
Bivariate streaming primitive. For paired observations (x, y) consumed
one at a time, computes:
r -- Pearson correlation coefficient, in PPM in [-1_000_000, 1_000_000]
slope -- least-squares regression slope m: y = m*x + b
intercept -- least-squares regression intercept b
SIX ACCUMULATORS (integer-exact under overflow budget):
n = count
sum_x = Σ x_i
sum_y = Σ y_i
sum_xy = Σ x_i * y_i
sum_xx = Σ x_i²
sum_yy = Σ y_i²
FORMULAS:
cov_xy = n * sum_xy - sum_x * sum_y
var_x = n * sum_xx - sum_x²
var_y = n * sum_yy - sum_y²
r = cov_xy / sqrt(var_x * var_y)
slope = cov_xy / var_x
intercept = (sum_y - slope * sum_x) / n
OVERFLOW BUDGET:
sum_xx grows as n * x_max². For x_max=2^15, n_max=2^32 -> sum_xx
max ~ 2^62. And the cross-term computations need extra margin.
nx_corr_safe_p flags unsafe inputs.
USE CASES:
- SRE: correlate metric series (CPU vs latency)
- finance: returns correlation
- science: regression on streamed measurements
LOSSLESS-LANGUAGE DISCIPLINE: r returned in PPM with NX_ENV_ABS,
param_a = 0 (exact under budget). Production tier.
dependencies 2 imports · 0 importers
imports: nx_syscalls.nxnx_sketch_types.nx
imported by: nobody (leaf or entry point)
structs
| 49 | struct Correlation |
consts
| 47 | const NX_CORR_VALUE_LIMIT: i64 = 32768 // |x|, |y| < 2^15 for safe sum_xx |
functions
| 61 | func nx_corr_alloc() -> *Correlation |
| 76 | func nx_corr_isqrt(x: i64) -> i64 called by 1: nx_corr_r_ppm |
| 95 | func nx_corr_safe_p(x: i64, y: i64) -> i64 called by 1: nx_corr_add |
| 107 | func nx_corr_add(c: *Correlation, x: i64, y: i64) -> i64 calls 1: nx_corr_safe_p |
| 125 | func nx_corr_r_ppm(c: *Correlation) -> i64 |
| 155 | func nx_corr_slope_ppm(c: *Correlation) -> i64 |
| 163 | func nx_corr_intercept(c: *Correlation) -> i64 calls 1: nx_corr_slope_ppm |
| 174 | func nx_corr_query_r(c: *Correlation) -> *ApproxI64 |
| 182 | func nx_corr_query_slope(c: *Correlation) -> *ApproxI64 |
| 192 | func nx_corr_merge(a: *Correlation, b: *Correlation) -> *Correlation calls 1: nx_corr_alloc |
| 205 | func nx_corr_memory_bytes(c: *Correlation) -> i64 |
| 209 | func nx_corr_count(c: *Correlation) -> i64 |