code wiki / (root) / sketch_correlation.nx

sketch_correlation.nx

buildroot/runtime/sketch_correlation.nx

6617 B205 linesdepth 4pulls 4 transitivereach 1 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 · 1 importers

syscalls.nx sketch_types.nx sketch_correlation.nx sketch_correlation_test.nx

imports: syscalls.nxsketch_types.nx

imported by: sketch_correlation_test.nx

structs

43struct Correlation {

consts

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

functions

55func nx_corr_alloc() -> *Correlation {
called by 2: nx_corr_mergemain
70func nx_corr_isqrt(x: i64) -> i64 {
called by 1: nx_corr_r_ppm
89func nx_corr_safe_p(x: i64, y: i64) -> i64 {
called by 2: nx_corr_addmain
101func nx_corr_add(c: *Correlation, x: i64, y: i64) -> i64 {
called by 1: main calls 1: nx_corr_safe_p
119func nx_corr_r_ppm(c: *Correlation) -> i64 {
called by 2: nx_corr_query_rmain calls 1: nx_corr_isqrt
149func nx_corr_slope_ppm(c: *Correlation) -> i64 {
157func nx_corr_intercept(c: *Correlation) -> i64 {
called by 1: main calls 1: nx_corr_slope_ppm
168func nx_corr_query_r(c: *Correlation) -> *ApproxI64 {
called by 1: main calls 2: nx_corr_r_ppmnx_approx_new
176func nx_corr_query_slope(c: *Correlation) -> *ApproxI64 {
186func nx_corr_merge(a: *Correlation, b: *Correlation) -> *Correlation {
called by 1: main calls 1: nx_corr_alloc
199func nx_corr_memory_bytes(c: *Correlation) -> i64 {
203func nx_corr_count(c: *Correlation) -> i64 {
called by 1: main