standard_deviation 0.0.1
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/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +60 -0
- data/Rakefile +19 -0
- data/ext/standard_deviation/extconf.rb +2 -0
- data/ext/standard_deviation/standard_deviation.c +28 -0
- data/lib/standard_deviation/version.rb +3 -0
- data/lib/standard_deviation.rb +5 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/standard_deviation/standard_deviation_spec.rb +22 -0
- data/standard_deviation.gemspec +22 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Rodrigo Navarro
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Native Standard Deviation
|
2
|
+
|
3
|
+
An implementation of the standard deviation calculation in C, with much better performance (50x-100x) than using pure ruby.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'standard_deviation'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install standard_deviation
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Just call `standard_deviation` or `stdev` on a array with numbers.
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
[1, 2, 3, 4, 5].stdev => 1.5811388300841898
|
25
|
+
|
26
|
+
[521.0, BigDecimal("1234.45"), 1_120].standard_deviation => 383.168958598336
|
27
|
+
```
|
28
|
+
|
29
|
+
## Benchmarks
|
30
|
+
|
31
|
+
``` ruby
|
32
|
+
# Basic ruby implementation
|
33
|
+
class Array
|
34
|
+
def stdev_ruby
|
35
|
+
total = inject :+
|
36
|
+
mean = total.to_f / size
|
37
|
+
variance = inject(0) { |variance, value| variance + (value - mean) ** 2 }
|
38
|
+
|
39
|
+
Math.sqrt(variance / (size - 1))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Running on a 2.4 GHz MacBook Pro
|
44
|
+
n = 10000
|
45
|
+
values = (1..10_000).map(&:to_f)
|
46
|
+
|
47
|
+
Benchmark.bm do |b|
|
48
|
+
b.report do
|
49
|
+
n.times { values.stdev }
|
50
|
+
end
|
51
|
+
|
52
|
+
b.report do
|
53
|
+
n.times { values.stdev_ruby }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# user system total real
|
58
|
+
# 1.070000 0.000000 1.070000 ( 1.065246)
|
59
|
+
# 88.180000 0.550000 88.730000 ( 93.835144)
|
60
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
require "rake/extensiontask"
|
4
|
+
require "rspec"
|
5
|
+
require "rspec/core/rake_task"
|
6
|
+
|
7
|
+
Rake::ExtensionTask.new("standard_deviation") do |ext|
|
8
|
+
ext.lib_dir = File.join "lib", "standard_deviation"
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
RSpec::Core::RakeTask.new("spec") do |t|
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
Rake::Task[:spec].prerequisites << :compile
|
17
|
+
|
18
|
+
task :test => :spec
|
19
|
+
task :default => :test
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <math.h>
|
3
|
+
|
4
|
+
static VALUE stdev(VALUE self) {
|
5
|
+
int i, size;
|
6
|
+
double total, mean, variance;
|
7
|
+
|
8
|
+
size = RARRAY_LEN(self);
|
9
|
+
total = variance = 0;
|
10
|
+
VALUE *array = RARRAY_PTR(self);
|
11
|
+
|
12
|
+
for (i = 0; i < size; i++) {
|
13
|
+
total += NUM2DBL(array[i]);
|
14
|
+
}
|
15
|
+
|
16
|
+
mean = total / size;
|
17
|
+
|
18
|
+
for (i = 0; i < size; i++) {
|
19
|
+
variance += pow((NUM2DBL(array[i]) - mean), 2);
|
20
|
+
}
|
21
|
+
|
22
|
+
return rb_float_new(sqrt(variance / (size - 1)));
|
23
|
+
}
|
24
|
+
|
25
|
+
void Init_standard_deviation() {
|
26
|
+
rb_define_method(rb_cArray, "stdev", stdev, 0);
|
27
|
+
rb_define_alias(rb_cArray, "standard_deviation", "stdev");
|
28
|
+
}
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
describe "#stdev" do
|
5
|
+
subject { values.stdev }
|
6
|
+
|
7
|
+
context "with integer values" do
|
8
|
+
let(:values) { [1, 2, 6, 3, -4, 23] }
|
9
|
+
it { should == 9.32559202767667 }
|
10
|
+
end
|
11
|
+
|
12
|
+
context "with float values" do
|
13
|
+
let(:values) { [1.0, 2.0, 6.0, 3.0, -4.0, 23.0] }
|
14
|
+
it { should == 9.32559202767667 }
|
15
|
+
end
|
16
|
+
|
17
|
+
context "with bigdecimal values" do
|
18
|
+
let(:values) { [BigDecimal("1.0"), BigDecimal("2.0"), BigDecimal("6.0"), BigDecimal("3.0"), BigDecimal("-4.0"), BigDecimal("23.0")] }
|
19
|
+
it { should == 9.32559202767667 }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/standard_deviation/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Rodrigo Navarro"]
|
6
|
+
gem.email = ["rnavarro1@gmail.com"]
|
7
|
+
gem.description = %q{An implementation of the standard deviation calculation in C,
|
8
|
+
with much better performance (50x-100x) than using pure ruby.}
|
9
|
+
gem.summary = %q{Native extension for Ruby to calculate standard deviation.}
|
10
|
+
gem.homepage = "https://github.com/reu/standard_deviation"
|
11
|
+
|
12
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
13
|
+
gem.files = `git ls-files`.split("\n")
|
14
|
+
gem.extensions = ["ext/standard_deviation/extconf.rb"]
|
15
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
gem.name = "standard_deviation"
|
17
|
+
gem.require_paths = ["lib", "ext"]
|
18
|
+
gem.version = StandardDeviation::VERSION
|
19
|
+
|
20
|
+
gem.add_development_dependency "rake-compiler"
|
21
|
+
gem.add_development_dependency "rspec"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: standard_deviation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rodrigo Navarro
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-13 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake-compiler
|
16
|
+
requirement: &2156579940 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2156579940
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &2156579420 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2156579420
|
36
|
+
description: ! "An implementation of the standard deviation calculation in C,\n with
|
37
|
+
much better performance (50x-100x) than using pure ruby."
|
38
|
+
email:
|
39
|
+
- rnavarro1@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions:
|
42
|
+
- ext/standard_deviation/extconf.rb
|
43
|
+
extra_rdoc_files: []
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- LICENSE
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- ext/standard_deviation/extconf.rb
|
51
|
+
- ext/standard_deviation/standard_deviation.c
|
52
|
+
- lib/standard_deviation.rb
|
53
|
+
- lib/standard_deviation/version.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
- spec/standard_deviation/standard_deviation_spec.rb
|
56
|
+
- standard_deviation.gemspec
|
57
|
+
homepage: https://github.com/reu/standard_deviation
|
58
|
+
licenses: []
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
- ext
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
hash: -2904390813022944632
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
hash: -2904390813022944632
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.10
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Native extension for Ruby to calculate standard deviation.
|
88
|
+
test_files:
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
- spec/standard_deviation/standard_deviation_spec.rb
|