Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: reu/standard_deviation
base: 5a2210d444
...
head fork: reu/standard_deviation
compare: ce52437740
  • 6 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
4 README.md
@@ -39,9 +39,9 @@ class Array
def stdev_ruby
total = inject :+
mean = total.to_f / size
- variance = inject(0) { |variance, value| variance + (value - mean) ** 2 }
+ variance = inject(0) { |variance, value| variance + (value - mean) ** 2 } / (size - 1)
- Math.sqrt(variance / (size - 1))
+ Math.sqrt(variance)
end
end
View
56 ext/standard_deviation/standard_deviation.c
@@ -1,50 +1,58 @@
#include <ruby.h>
#include <math.h>
-static VALUE stdev(VALUE self) {
- int i, size;
- double total, mean, variance;
-
- size = RARRAY_LEN(self);
- total = variance = 0;
- VALUE *array = RARRAY_PTR(self);
+static double sum(VALUE *array, int size) {
+ int i;
+ double total = 0;
for (i = 0; i < size; i++) {
total += NUM2DBL(array[i]);
}
- mean = total / size;
+ return total;
+}
- for (i = 0; i < size; i++) {
- variance += pow((NUM2DBL(array[i]) - mean), 2);
+static double distance_from_mean(VALUE *array, int size) {
+ int i;
+ double mean, distance;
+
+ mean = sum(array, size) / size;
+
+ for (i = 0, distance = 0; i < size; i++) {
+ distance += pow((NUM2DBL(array[i]) - mean), 2);
}
- return rb_float_new(sqrt(variance / (size - 1)));
+ return distance;
}
-static VALUE stdevp(VALUE self) {
- int i, size;
- double total, mean, variance;
+static double sample_variance(VALUE *array, int size) {
+ return distance_from_mean(array, size) / (size - 1);
+}
+
+static double population_variance(VALUE *array, int size) {
+ return distance_from_mean(array, size) / size;
+}
+
+static VALUE stdev(VALUE self) {
+ int size;
size = RARRAY_LEN(self);
- total = variance = 0;
VALUE *array = RARRAY_PTR(self);
- for (i = 0; i < size; i++) {
- total += NUM2DBL(array[i]);
- }
+ return rb_float_new(sqrt(sample_variance(array, size)));
+}
- mean = total / size;
+static VALUE stdevp(VALUE self) {
+ int size;
- for (i = 0; i < size; i++) {
- variance += pow((NUM2DBL(array[i]) - mean), 2);
- }
+ size = RARRAY_LEN(self);
+ VALUE *array = RARRAY_PTR(self);
- return rb_float_new(sqrt(variance / size));
+ return rb_float_new(sqrt(population_variance(array, size)));
}
void Init_standard_deviation() {
rb_define_method(rb_cArray, "stdev", stdev, 0);
rb_define_alias(rb_cArray, "standard_deviation", "stdev");
- rb_define_method(rb_cArray, "stdevp", stdev, 0);
+ rb_define_method(rb_cArray, "stdevp", stdevp, 0);
}
View
10 spec/standard_deviation/standard_deviation_spec.rb
@@ -19,23 +19,23 @@
it { should == 9.32559202767667 }
end
end
-
- describe "#stdevp" do
+
+ describe "#stdevp" do
subject { values.stdevp }
context "with integer values" do
let(:values) { [1, 2, 6, 3, -4, 23] }
- it { should == 8.51306185941476 }
+ it { should == 8.513061859414755 }
end
context "with float values" do
let(:values) { [1.0, 2.0, 6.0, 3.0, -4.0, 23.0] }
- it { should == 8.51306185941476 }
+ it { should == 8.513061859414755 }
end
context "with bigdecimal values" do
let(:values) { [BigDecimal("1.0"), BigDecimal("2.0"), BigDecimal("6.0"), BigDecimal("3.0"), BigDecimal("-4.0"), BigDecimal("23.0")] }
- it { should == 8.51306185941476 }
+ it { should == 8.513061859414755 }
end
end
end

No commit comments for this range

Something went wrong with that request. Please try again.