1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//! Robust fitting using biweight function
/// Compute biweight robustness weights
///
/// Computes robustness weights using the biweight function:
/// - w = (1 - (r/c)^2)^2 for |r| < c
/// - w = 1 for |r| <= c1
/// - w = 0 for |r| >= c9
/// where c = 6 * MAD (median absolute deviation)
///
/// Note: The caller should check if consistent_mad (6 * MAD) is too small relative to
/// the mean absolute residual before calling this function.
///
/// # Arguments
///
/// * `residuals` - Residuals from previous fit
///
/// # Returns
///
/// Vector of robustness weights
///
/// # Example
///
/// ```
/// use linreg_core::loess::robust::compute_biweight_weights;
///
/// // Residuals with one clear outlier
/// let residuals = vec![0.0, 0.1, 0.2, -0.1, 5.0];
///
/// let weights = compute_biweight_weights(&residuals);
///
/// // All weights should be in [0, 1]
/// assert!(weights.iter().all(|w| *w >= 0.0 && *w <= 1.0));
///
/// // Zero residual should give weight 1.0
/// assert_eq!(weights[0], 1.0);
///
/// // Large residual (5.0) should have smaller weight
/// assert!(weights[4] < weights[1]);
/// ```