code wiki / (root) / nx_sketch_correlation.nx

nx_sketch_correlation.nx

buildroot/runtime/nx_sketch_correlation.nx

6669 B211 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind sketch/demotopic sketch
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_sketch_types.nx nx_sketch_correlation.nx

imports: nx_syscalls.nxnx_sketch_types.nx

imported by: nobody (leaf or entry point)

structs

49struct Correlation

consts

47const NX_CORR_VALUE_LIMIT: i64 = 32768 // |x|, |y| < 2^15 for safe sum_xx

functions

61func nx_corr_alloc() -> *Correlation
called by 1: nx_corr_merge calls 1: sys_mmap
76func nx_corr_isqrt(x: i64) -> i64
called by 1: nx_corr_r_ppm
95func nx_corr_safe_p(x: i64, y: i64) -> i64
called by 1: nx_corr_add
107func nx_corr_add(c: *Correlation, x: i64, y: i64) -> i64
calls 1: nx_corr_safe_p
125func nx_corr_r_ppm(c: *Correlation) -> i64
called by 1: nx_corr_query_r calls 1: nx_corr_isqrt
155func nx_corr_slope_ppm(c: *Correlation) -> i64
163func nx_corr_intercept(c: *Correlation) -> i64
174func nx_corr_query_r(c: *Correlation) -> *ApproxI64
182func nx_corr_query_slope(c: *Correlation) -> *ApproxI64
192func nx_corr_merge(a: *Correlation, b: *Correlation) -> *Correlation
calls 1: nx_corr_alloc
205func nx_corr_memory_bytes(c: *Correlation) -> i64
209func nx_corr_count(c: *Correlation) -> i64