standard_deviation 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md
CHANGED
@@ -18,13 +18,24 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
Just call `standard_deviation` or `stdev` on
|
21
|
+
Just call `standard_deviation` or `stdev` on an array with numbers:
|
22
22
|
|
23
23
|
``` ruby
|
24
24
|
[1, 2, 3, 4, 5].stdev => 1.5811388300841898
|
25
25
|
|
26
26
|
[521.0, BigDecimal("1234.45"), 1_120].standard_deviation => 383.168958598336
|
27
27
|
```
|
28
|
+
For population standard deviation, call `stdevp`:
|
29
|
+
|
30
|
+
``` ruby
|
31
|
+
[1, 2, 3, 4, 5].stdevp => 1.4142135623730951
|
32
|
+
```
|
33
|
+
|
34
|
+
Also, the API exposes the `sample_variance` and `population_variance` calculations:
|
35
|
+
``` ruby
|
36
|
+
[1, 2, 3, 4, 5].sample_variance => 2.5
|
37
|
+
[1, 2, 3, 4, 5].population_variance => 2.0
|
38
|
+
```
|
28
39
|
|
29
40
|
## Benchmarks
|
30
41
|
|
@@ -34,9 +45,9 @@ class Array
|
|
34
45
|
def stdev_ruby
|
35
46
|
total = inject :+
|
36
47
|
mean = total.to_f / size
|
37
|
-
variance = inject(0) { |variance, value| variance + (value - mean) ** 2 }
|
48
|
+
variance = inject(0) { |variance, value| variance + (value - mean) ** 2 } / (size - 1)
|
38
49
|
|
39
|
-
Math.sqrt(variance
|
50
|
+
Math.sqrt(variance)
|
40
51
|
end
|
41
52
|
end
|
42
53
|
|
@@ -1,28 +1,72 @@
|
|
1
1
|
#include <ruby.h>
|
2
2
|
#include <math.h>
|
3
3
|
|
4
|
-
static
|
5
|
-
int i
|
6
|
-
double total
|
7
|
-
|
8
|
-
size = RARRAY_LEN(self);
|
9
|
-
total = variance = 0;
|
10
|
-
VALUE *array = RARRAY_PTR(self);
|
4
|
+
static double sum(VALUE *array, int size) {
|
5
|
+
int i;
|
6
|
+
double total = 0;
|
11
7
|
|
12
8
|
for (i = 0; i < size; i++) {
|
13
9
|
total += NUM2DBL(array[i]);
|
14
10
|
}
|
15
11
|
|
16
|
-
|
12
|
+
return total;
|
13
|
+
}
|
14
|
+
|
15
|
+
static double distance_from_mean(VALUE *array, int size) {
|
16
|
+
int i;
|
17
|
+
double mean, distance;
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
mean = sum(array, size) / size;
|
20
|
+
|
21
|
+
for (i = 0, distance = 0; i < size; i++) {
|
22
|
+
distance += pow((NUM2DBL(array[i]) - mean), 2);
|
20
23
|
}
|
21
24
|
|
22
|
-
return
|
25
|
+
return distance;
|
26
|
+
}
|
27
|
+
|
28
|
+
static double compute_sample_variance(VALUE *array, int size) {
|
29
|
+
return distance_from_mean(array, size) / (size - 1);
|
30
|
+
}
|
31
|
+
|
32
|
+
static double compute_population_variance(VALUE *array, int size) {
|
33
|
+
return distance_from_mean(array, size) / size;
|
34
|
+
}
|
35
|
+
|
36
|
+
static VALUE sample_variance(VALUE self) {
|
37
|
+
int size = RARRAY_LEN(self);
|
38
|
+
VALUE *array = RARRAY_PTR(self);
|
39
|
+
|
40
|
+
return rb_float_new(compute_sample_variance(array, size));
|
41
|
+
}
|
42
|
+
|
43
|
+
static VALUE population_variance(VALUE self) {
|
44
|
+
int size = RARRAY_LEN(self);
|
45
|
+
VALUE *array = RARRAY_PTR(self);
|
46
|
+
|
47
|
+
return rb_float_new(compute_population_variance(array, size));
|
48
|
+
}
|
49
|
+
|
50
|
+
static VALUE stdev(VALUE self) {
|
51
|
+
int size = RARRAY_LEN(self);
|
52
|
+
VALUE *array = RARRAY_PTR(self);
|
53
|
+
|
54
|
+
return rb_float_new(sqrt(compute_sample_variance(array, size)));
|
55
|
+
}
|
56
|
+
|
57
|
+
static VALUE stdevp(VALUE self) {
|
58
|
+
int size = RARRAY_LEN(self);
|
59
|
+
VALUE *array = RARRAY_PTR(self);
|
60
|
+
|
61
|
+
return rb_float_new(sqrt(compute_population_variance(array, size)));
|
23
62
|
}
|
24
63
|
|
25
64
|
void Init_standard_deviation() {
|
26
65
|
rb_define_method(rb_cArray, "stdev", stdev, 0);
|
27
66
|
rb_define_alias(rb_cArray, "standard_deviation", "stdev");
|
67
|
+
rb_define_method(rb_cArray, "stdevp", stdevp, 0);
|
68
|
+
|
69
|
+
rb_define_method(rb_cArray, "sample_variance", sample_variance, 0);
|
70
|
+
rb_define_alias(rb_cArray, "variance", "sample_variance");
|
71
|
+
rb_define_method(rb_cArray, "population_variance", population_variance, 0);
|
28
72
|
}
|
@@ -19,4 +19,61 @@ describe Array do
|
|
19
19
|
it { should == 9.32559202767667 }
|
20
20
|
end
|
21
21
|
end
|
22
|
+
|
23
|
+
describe "#stdevp" do
|
24
|
+
subject { values.stdevp }
|
25
|
+
|
26
|
+
context "with integer values" do
|
27
|
+
let(:values) { [1, 2, 6, 3, -4, 23] }
|
28
|
+
it { should == 8.513061859414755 }
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with float values" do
|
32
|
+
let(:values) { [1.0, 2.0, 6.0, 3.0, -4.0, 23.0] }
|
33
|
+
it { should == 8.513061859414755 }
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with bigdecimal values" do
|
37
|
+
let(:values) { [BigDecimal("1.0"), BigDecimal("2.0"), BigDecimal("6.0"), BigDecimal("3.0"), BigDecimal("-4.0"), BigDecimal("23.0")] }
|
38
|
+
it { should == 8.513061859414755 }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#sample_variance" do
|
43
|
+
subject { values.sample_variance }
|
44
|
+
|
45
|
+
context "with integer values" do
|
46
|
+
let(:values) { [1, 2, 6, 3, -4, 23] }
|
47
|
+
it { should == 86.96666666666665 }
|
48
|
+
end
|
49
|
+
|
50
|
+
context "with float values" do
|
51
|
+
let(:values) { [1.0, 2.0, 6.0, 3.0, -4.0, 23.0] }
|
52
|
+
it { should == 86.96666666666665 }
|
53
|
+
end
|
54
|
+
|
55
|
+
context "with bigdecimal values" do
|
56
|
+
let(:values) { [BigDecimal("1.0"), BigDecimal("2.0"), BigDecimal("6.0"), BigDecimal("3.0"), BigDecimal("-4.0"), BigDecimal("23.0")] }
|
57
|
+
it { should == 86.96666666666665 }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#population_variance" do
|
62
|
+
subject { values.population_variance }
|
63
|
+
|
64
|
+
context "with integer values" do
|
65
|
+
let(:values) { [1, 2, 6, 3, -4, 23] }
|
66
|
+
it { should == 72.47222222222221 }
|
67
|
+
end
|
68
|
+
|
69
|
+
context "with float values" do
|
70
|
+
let(:values) { [1.0, 2.0, 6.0, 3.0, -4.0, 23.0] }
|
71
|
+
it { should == 72.47222222222221 }
|
72
|
+
end
|
73
|
+
|
74
|
+
context "with bigdecimal values" do
|
75
|
+
let(:values) { [BigDecimal("1.0"), BigDecimal("2.0"), BigDecimal("6.0"), BigDecimal("3.0"), BigDecimal("-4.0"), BigDecimal("23.0")] }
|
76
|
+
it { should == 72.47222222222221 }
|
77
|
+
end
|
78
|
+
end
|
22
79
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: standard_deviation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-04-13 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake-compiler
|
16
|
-
requirement: &
|
16
|
+
requirement: &2161598240 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2161598240
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &2161597660 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2161597660
|
36
36
|
description: ! "An implementation of the standard deviation calculation in C,\n with
|
37
37
|
much better performance (50x-100x) than using pure ruby."
|
38
38
|
email:
|
@@ -69,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
69
|
version: '0'
|
70
70
|
segments:
|
71
71
|
- 0
|
72
|
-
hash:
|
72
|
+
hash: 2357727006440476187
|
73
73
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
74
|
none: false
|
75
75
|
requirements:
|
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
78
|
version: '0'
|
79
79
|
segments:
|
80
80
|
- 0
|
81
|
-
hash:
|
81
|
+
hash: 2357727006440476187
|
82
82
|
requirements: []
|
83
83
|
rubyforge_project:
|
84
84
|
rubygems_version: 1.8.10
|