Skip to content

Chapter 19: Columnar DataFrames, Statistics & Evaluation Metrics

"Pandas convenience belongs in C23 memory arenas—without Python GILs or object boxing."


19.1 Arrow-Compatible Columnar DataFrames (rcre_df)

Row-oriented struct arrays poison CPU caches when analytics scan a single column across millions of rows. RCRE's DataFrame engine (include/rcre_df.h, src/core/df.c) adopts a Structure-of-Arrays (SoA) layout paired with LSB-first Apache Arrow validity bitmaps (1 = valid, 0 = null):

\[\text{is\_valid}(i) = \left(\text{bitmap}[\lfloor i / 8 \rfloor] \gg (i \bmod 8)\right) \mathbin{\&} 1\]

Core zero-copy and parallel operations include: - Linear-Time \(\mathcal{O}(N)\) IEEE-754 Float LSD Radix Sort: flips all bits on negative floats and flips the sign bit on non-negative floats so unsigned 32-bit byte-radix passes sort arbitrary signed floating-point values strictly linearly in 4 cache passes (256-bucket histograms). - Multi-Threaded Hash GroupBy: partitions rows into thread-local hash accumulators and computes Sum, Mean, Min, Max, Count, Var, and Std inside arena slabs.


19.2 Welford Moments, Quantiles & Canonical PCA (rcre_stat, rcre_prep)

Single-pass variance (E[X^2] - E[X]^2) suffers catastrophic cancellation when data has large offsets. rcre_stat_describe() executes Welford/Pebay's numerically stable recurrence tracking mean, variance, skewness, and kurtosis in one sequential scan, paired with quickselect linear-interpolation quantiles (lo = floor(q*(n-1))).

For PCA (rcre_pca_fit), power iteration with Hotelling deflation computes orthonormal eigenvectors up to sign \(\pm \mathbf{v}_k\). To guarantee deterministic downstream projections matching Scikit-Learn svd_flip, src/ml/stat.c canonicalizes each principal component so its largest-magnitude coordinate is strictly positive.


19.3 Evaluation Protocols & Decision Ensembles (rcre_metric, forest.c)

To validate models purely in C23 without calling Python: - ROC-AUC & PR-AUC (rcre_metric.h): exact trapezoidal integration over sorted probability thresholds, paired with Confusion Matrix, F1, and Matthews Correlation Coefficient (MCC). - K-Fold & Stratified K-Fold Splits: deterministic arena-backed train/validation index permutation without data copying. - Random Forest & L1 Sparse Linear: rcre_ml_rf_fit() constructs bootstrapped CART ensembles sampling \(m = \lceil \sqrt{D} \rceil\) features per node via Gini impurity, while rcre_ml_sparse_linear_fit() drives noisy weights cleanly to zero via soft-thresholding coordinate descent.


19.4 Chapter Project: Columnar Bitmap Filter, Float Radix Sort & Welford Moment Check

Companion code in examples/ch19/main.c (make ch19) demonstrates Arrow-compatible null bitmaps, IEEE-754 order-preserving radix sort, and Welford single-pass variance verified under ASan and UBSan.