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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// "Whatever you do, work at it with all your heart, as working for the Lord,
// not for human masters, since you know that you will receive an inheritance
// from the Lord as a reward. It is the Lord Christ you are serving."
// (Col 3:23-24)
use r2rs_base::func::partial_sort;
pub fn lowess(x: &[f64], y: &[f64]) -> (Vec<f64>, Vec<f64>) {
let mut x_ordered = vec![0.0; x.len()];
let mut y_ordered = vec![0.0; x.len()];
let mut indices = (0..x.len()).collect::<Vec<_>>();
indices.sort_by(|&i1, &i2| x[i1].partial_cmp(&x[i2]).unwrap());
for (old_index, new_index) in indices.iter().cloned().enumerate() {
x_ordered[old_index] = x[new_index];
y_ordered[old_index] = y[new_index];
}
let x = x_ordered;
let y = y_ordered;
let mut ys = vec![0.0; y.len()];
let mut res = vec![0.0; y.len()];
let mut rw = vec![0.0; y.len()];
let n = x.len();
let f = 2.0 / 3.0;
let iter = 3;
let delta = 0.01 * (x[n - 1] - x[0]);
let nsteps = iter;
let ns = 2.max(n.min((f * n as f64 + 1e-7) as usize));
// robustness iterations
let mut d1 = 0.0;
let mut d2 = 0.0;
let mut iter = 1;
while iter < nsteps + 1 {
let mut nleft = 1;
let mut nright = ns;
let mut last = 1; // index of prev estimated point
let mut i = 1; // index of current point
loop {
if nright < n {
// move nleft, nright to right if radius decreases
d1 = x[i - 1] - x[nleft - 1];
d2 = x[nright + 1 - 1] - x[i - 1];
// if d1 <= d2 with x[nright+1] == x[nright], lowest fixes
if d1 > d2 {
// radius will not decrease by move right
nleft += 1;
nright += 1;
continue;
}
}
// fitted value at x[i]
let ok = lowest(
&x,
&y,
n,
&x,
&mut ys,
i,
nleft,
nright,
&mut res,
iter > 0,
&rw,
);
if !ok {
ys[i - 1] = y[i - 1];
}
// all weights zero
// copy over value (all rw==0)
if last < i {
// Maybe last < i - 1
let denom = x[i - 1] - x[last - 1];
// skipped points -- interpolate
// non-zero - proof?
for j in last + 1..i {
let alpha = (x[j - 1] - x[last - 1]) / denom;
ys[j - 1] = alpha * ys[i - 1] + (1.0 - alpha) * ys[last - 1];
}
}
// last point actually estimated
last = i;
// x coord of close points
let cut = x[last - 1] + delta;
for i2 in last + 1..=n {
if x[i2 - 1] > cut {
i = i2;
break;
}
if x[i2 - 1] == x[last - 1] {
ys[i2 - 1] = ys[last - 1];
last = i2;
}
}
i = (last + 1).max(i - 1);
if last >= n {
break;
}
}
// residuals
for i in 0..n {
res[i] = y[i + 1 - 1] - ys[i + 1 - 1];
}
// overall scale estimate
let mut sc = 0.0;
for i in 0..n {
sc += res[i].abs();
}
sc /= n as f64;
// compute robustness weights
// except last time
if iter > nsteps {
break;
}
// Note: The following code, biweight_{6 MAD|Ri|}
// is also used in stl(), loess and several other places.
// --> should provide API here (MM)
for i in 0..n {
rw[i] = res[i].abs();
}
// Compute cmad := 6 * median(rw[], n) ----
// FIXME: We need C API in R for Median !
let m1 = n / 2;
// partial sort, for m1 & m2
partial_sort(&mut rw, &[n - 1, m1 - 1]).expect("Error in partial sort");
// rPsort(rw, n, m1);
let cmad = if n % 2 == 0 {
let m2 = n - m1 - 1;
partial_sort(&mut rw, &[n - 1, m2 - 1]).expect("Error in partial sort");
// rPsort(rw, n, m2);
3.0 * (rw[m1] + rw[m2])
} else {
// n odd
6.0 * rw[m1]
};
if cmad < 1e-7 * sc {
// effectively zero
break;
}
let c9 = 0.999 * cmad;
let c1 = 0.001 * cmad;
for i in 0..n {
let r = res[i].abs();
if r <= c1 {
rw[i] = 1.0;
} else if r <= c9 {
rw[i] = fsquare(1.0 - fsquare(r / cmad));
} else {
rw[i] = 0.0;
}
}
iter += 1;
}
(x, ys)
}
fn fcube(x: f64) -> f64 {
x * x * x
}
fn fsquare(x: f64) -> f64 {
x * x
}
fn lowest(
x: &[f64],
y: &[f64],
n: usize,
xs: &[f64],
ys: &mut [f64],
i: usize,
nleft: usize,
nright: usize,
w: &mut [f64],
userw: bool,
rw: &[f64],
) -> bool {
let range = x[n - 1] - x[0];
let h = (xs[i - 1] - x[nleft - 1]).max(x[nright - 1] - xs[i - 1]);
let h9 = 0.999 * h;
let h1 = 0.001 * h;
// sum of weights
let mut a = 0.;
let mut j = nleft;
while j <= n {
// compute weights (pick up all ties on right)
w[j - 1] = 0.0;
let r = (x[j - 1] - xs[i - 1]).abs();
if r <= h9 {
if r <= h1 {
w[j - 1] = 1.0;
} else {
w[j - 1] = fcube(1.0 - fcube(r / h));
}
if userw {
w[j - 1] *= rw[j - 1];
}
a += w[j - 1];
} else if x[j - 1] > xs[i - 1] {
break;
}
j = j + 1;
}
// rightmost pt (may be greater than nright because of ties)
let nrt = j - 1;
if a <= 0.0 {
false
} else {
// weighted least squares
// make sum of w[j] == 1
for j in nleft..=nrt {
w[j - 1] /= a;
}
if h > 0.0 {
a = 0.0;
// use linear fit weighted center of x values
for j in nleft..=nrt {
a += w[j - 1] * x[j - 1];
}
let mut b = xs[i - 1] - a;
let mut c = 0.0;
for j in nleft..=nrt {
c += w[j - 1] * fsquare(x[j - 1] - a);
}
if c.sqrt() > 0.001 * range {
b /= c;
// points are spread out enough to compute slope
for j in nleft..=nrt {
w[j - 1] *= b * (x[j - 1] - a) + 1.0;
}
}
}
ys[i - 1] = 0.0;
for j in nleft..=nrt {
ys[i - 1] += w[j - 1] * y[j - 1];
}
true
}
}