Module: Lowess

Defined in:
lib/lowess.rb,
lib/lowess/version.rb,
ext/ext_lowess/lowess.c

Overview

A LOWESS smoother

Defined Under Namespace

Classes: Point

Constant Summary collapse

VERSION =
'1.0.0'

Class Method Summary collapse

Class Method Details

.ext_lowess(in_points, in_f, in_iter, in_delta) ⇒ Object



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
# File 'ext/ext_lowess/lowess.c', line 14

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* points = ALLOC_N(VALUE, len);
  VALUE point_args[2];
  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);

  for (i = 0; i < len; i++) {
    point_args[0] = DBL2NUM(xs[i]);
    point_args[1] = DBL2NUM(out_ys[i]);
    points[i] = rb_class_new_instance(2, point_args, Lowess_Point);
  }

  rb_ary_cat(ret, points, len);

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

  return ret;
}

.lowess(points, options = {}) ⇒ Object

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.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
# 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