sketch_correlation.nx
buildroot/runtime/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 · 1 importers
imports: syscalls.nxsketch_types.nx
imported by: sketch_correlation_test.nx
structs
| 43 | struct Correlation { |
consts
| 41 | const NX_CORR_VALUE_LIMIT: i64 = 32768 // |x|, |y| < 2^15 for safe sum_xx |
functions
| 55 | func nx_corr_alloc() -> *Correlation { |
| 70 | func nx_corr_isqrt(x: i64) -> i64 {
called by 1: nx_corr_r_ppm |
| 89 | func nx_corr_safe_p(x: i64, y: i64) -> i64 { |
| 101 | func nx_corr_add(c: *Correlation, x: i64, y: i64) -> i64 { |
| 119 | func nx_corr_r_ppm(c: *Correlation) -> i64 { |
| 149 | func nx_corr_slope_ppm(c: *Correlation) -> i64 { |
| 157 | func nx_corr_intercept(c: *Correlation) -> i64 { |
| 168 | func nx_corr_query_r(c: *Correlation) -> *ApproxI64 { |
| 176 | func nx_corr_query_slope(c: *Correlation) -> *ApproxI64 { |
| 186 | func nx_corr_merge(a: *Correlation, b: *Correlation) -> *Correlation { |
| 199 | func nx_corr_memory_bytes(c: *Correlation) -> i64 { |
| 203 | func nx_corr_count(c: *Correlation) -> i64 {
called by 1: main |