Robust
The CSharpNumerics.Statistics.Robust namespace provides outlier-resistant statistical methods.
βοΈ Sigma Clippingβ
Iterative sigma-clipping: removes data points outside [mean β Ο_low Γ Ο, mean + Ο_high Γ Ο] and recomputes until convergence.
using CSharpNumerics.Statistics.Robust;
var data = new[] { 1.0, 2.0, 2.5, 2.3, 100.0, 2.1, 1.9, -50.0, 2.4 };
// Full result with mask, mean, std, iteration count
ClipResult result = SigmaClipping.Clip(data, sigmaLow: 3.0, sigmaHigh: 3.0, maxIter: 10);
result.Mean // mean of retained points
result.Std // std of retained points
result.RetainedCount // number of non-clipped points
result.ClippedCount // number of removed outliers
result.Iterations // convergence iterations
result.Mask // bool[] β true = retained
// Symmetric shorthand
ClipResult sym = SigmaClipping.Clip(data, sigma: 2.5, maxIter: 5);
// Just get the cleaned array
double[] clean = SigmaClipping.Apply(data, sigmaLow: 3.0, sigmaHigh: 3.0);
// clean β { 1.0, 2.0, 2.5, 2.3, 2.1, 1.9, 2.4 }
π Median Absolute Deviation (MAD)β
A robust measure of spread: MAD = median(|xα΅’ β median(x)|). The scaled MAD (k = 1.4826) estimates Ο for normal data.
using CSharpNumerics.Statistics.Robust;
var data = new[] { 1.0, 2.0, 3.0, 4.0, 5.0, 100.0 };
MadResult result = MedianAbsoluteDeviation.Compute(data);
result.Median // median of the data
result.Mad // median absolute deviation
result.ScaledMad // 1.4826 Γ MAD (robust Ο estimate)
// Custom scale constant
MadResult custom = MedianAbsoluteDeviation.Compute(data, scale: 1.0);
βοΈ Trimmed Meanβ
Arithmetic mean after discarding a proportion of the smallest and largest values. Ξ± = 0 gives the ordinary mean; Ξ± β 0.5 converges on the median.
using CSharpNumerics.Statistics.Robust;
var data = new[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 100.0 };
double trimmed = TrimmedMean.Compute(data, trimRatio: 0.1);
// Trims lowest 10 % and highest 10 %, resistant to the 100.0 outlier
π Winsorized Meanβ
Like trimmed mean, but replaces extreme values with boundary values instead of discarding them β preserving the original sample size.
using CSharpNumerics.Statistics.Robust;
var data = new[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 100.0 };
double winsorized = WinsorizedMean.Compute(data, trimRatio: 0.1);
// Lowest 10 % replaced with next-lowest, highest 10 % with next-highest
π Huber Lossβ
Robust loss function that is quadratic for small residuals and linear for large ones β a smooth blend between MSE and MAE.
using CSharpNumerics.Statistics.Robust;
double loss = HuberLoss.Loss(residual: 0.5, delta: 1.35); // quadratic region
double loss2 = HuberLoss.Loss(residual: 5.0, delta: 1.35); // linear region
double grad = HuberLoss.Gradient(residual: 5.0, delta: 1.35);
double w = HuberLoss.Weight(residual: 5.0, delta: 1.35); // for IRLS
double[] residuals = { 0.5, -0.3, 4.0, -2.0 };
double meanLoss = HuberLoss.MeanLoss(residuals, delta: 1.35);
π― RANSACβ
RANdom SAmple Consensus β iterative robust model fitting that finds inlier consensus despite heavy outlier contamination.
using CSharpNumerics.Statistics.Robust;
var x = new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var y = new double[] { 2, 4, 6, 8, 10, 12, 14, 100, -50, 20 };
RansacResult result = Ransac.FitLine(x, y,
residualThreshold: 1.0, maxIterations: 2000, seed: 42);
result.BestModel // double[] { intercept, slope }
result.InlierMask // bool[] β true = inlier
result.InlierCount // number of inliers
result.Iterations // iterations performed
π Outlier Detectionβ
Three complementary methods for identifying outliers in univariate data.
using CSharpNumerics.Statistics.Robust;
var data = new[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 100.0 };
// IQR fence method (k = 1.5 mild, k = 3.0 extreme)
OutlierResult iqr = OutlierDetection.Iqr(data, k: 1.5);
// Standard Z-score
OutlierResult zs = OutlierDetection.ZScore(data, threshold: 3.0);
// Modified Z-score (MAD-based, more robust)
OutlierResult mz = OutlierDetection.ModifiedZScore(data, threshold: 3.5);
iqr.OutlierMask // bool[] β true = outlier
iqr.OutlierIndices // int[] indices of outliers
iqr.OutlierCount // number of outliers
iqr.Scores // deviation scores