module Lowess

A LOWESS smoother

Constants

VERSION

Public Class Methods

ext_lowess(p1, p2, p3, p4) click to toggle source
static VALUE
lowess_ext_lowess(VALUE self, VALUE in_points, VALUE in_f, VALUE in_iter, VALUE in_delta) {
  const int len = RARRAY_LEN(in_points);

  double* xs = ALLOC_N(double, len);
  double* ys = ALLOC_N(double, len);

  const double f = NUM2DBL(in_f);
  const int iter = NUM2INT(in_iter);
  const double delta = NUM2DBL(in_delta);

  double* out_ys = ALLOC_N(double, len);
  double* rw = ALLOC_N(double, len);
  double* res = ALLOC_N(double, len);
  VALUE point_args[2];
  VALUE point;
  VALUE ret = rb_ary_new_capa(len);
  int i;

  VALUE e;
  for (i = 0; i < len; i++) {
    e = rb_ary_entry(in_points, i);
    xs[i] = NUM2DBL(rb_ivar_get(e, X));
    ys[i] = NUM2DBL(rb_ivar_get(e, Y));
  }

  clowess(xs, ys, len, f, iter, delta, out_ys, rw, res);

  rb_ary_resize(ret, len); /* fill with Qnil */
  for (i = 0; i < len; i++) {
    point_args[0] = DBL2NUM(xs[i]);
    point_args[1] = DBL2NUM(out_ys[i]);
    point = rb_class_new_instance(2, point_args, Lowess_Point);
    RARRAY_ASET(ret, i, point);
  }

  xfree(res);
  xfree(rw);
  xfree(out_ys);
  xfree(ys);
  xfree(xs);

  return ret;
}
lowess(points, options={}) click to toggle source

Smooths the input points

Arguments:

  • points: Array of Lowess::Point objects, in any order.

  • options: any of:

    • f: the smoother span. Larger values give more smoothness.

    • iter: the number of 'robustifying' iterations. Smaller is faster.

    • delta: minimum x distance between input points. Larger is faster.

See stat.ethz.ch/R-manual/R-devel/library/stats/html/lowess.html for more complete documentation.

# File lib/lowess.rb, line 15
def self.lowess(points, options={})
  raise ArgumentError.new("Must pass one or more points") if points.empty?

  sorted_points = points.sort

  f = (options[:f] || 2.0 / 3).to_f
  iter = (options[:iter] || 3).to_i
  delta = (options[:delta] || (sorted_points.last.x - sorted_points.first.x).to_f / 100).to_f || 1.0

  ext_lowess(sorted_points, f, iter, delta)
end