Lowess regression smoother.
Based on R function clowess: Translated to C++ by C. Stratowa (R source file: lowess.c by R Development Core Team (C) 1999-2001)
152 constexpr
size_t two = 2;
154 std::max(two, std::min(
n,
static_cast<size_t>(span *
n + 1e-7)));
158 while (iiter <= iter + 1) {
167 const auto d1 = x[i] - x[nleft];
168 const auto d2 = x[nright + 1] - x[i];
180 const bool iterg1 = iiter > 1;
182 lowest(&x[1], &y[1],
n, x[i], ys[i], nleft, nright, res, iterg1, rw, ok);
188 const auto denom = x[i] - x[last];
191 for (
auto j = last + 1; j < i; j++) {
192 const auto alpha = (x[j] - x[last]) / denom;
193 ys[j] = alpha * ys[i] + (1. - alpha) * ys[last];
201 const auto cut = x[last] + delta;
202 for (i = last + 1; i <=
n; i++) {
205 if (x[i] == x[last]) {
210 i = std::max(last + 1, i - 1);
216 for (i = 0; i <
n; i++)
217 res[i] = y[i + 1] - ys[i + 1];
222 for (i = 0; i <
n; i++)
223 rw[i] = std::abs(res[i]);
226 const auto m1 =
n / 2;
232 const auto m2 =
n - m1 - 1;
234 cmad = 3. * (rw[m1] + rw[m2]);
239 const auto c9 = 0.999 * cmad;
240 const auto c1 = 0.001 * cmad;
241 for (i = 0; i <
n; i++) {
249 const auto r = std::abs(res[i]);
254 rw[i] = (1. - (r / cmad) * (r / cmad)) * (1. - (r / cmad) * (r / cmad));
void lowest(const T *x, const T *y, size_t n, T xs, T &ys, size_t nleft, size_t nright, T *w, bool userw, T *rw, bool &ok)
Fit value at x[i] Based on R function lowest: Translated to C++ by C.
void psort(T *x, size_t n, size_t k)
Partial sort.