darkext 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/History.txt +140 -0
- data/LICENSE +21 -0
- data/README.md +45 -0
- data/Rakefile +45 -0
- data/TODO +4 -0
- data/VERSION.yml +4 -0
- data/app_generators/sinatra_app_generator.rb +57 -0
- data/app_generators/templates/app.rb +16 -0
- data/app_generators/templates/config.ru +2 -0
- data/app_generators/templates/error.rb +8 -0
- data/app_generators/templates/gitignore +0 -0
- data/app_generators/templates/helpers.rb +12 -0
- data/app_generators/templates/http_method.rb +8 -0
- data/app_generators/templates/options.rb +9 -0
- data/bin/sinatra-app +14 -0
- data/darkext.gemspec +105 -0
- data/lib/darkext.rb +13 -0
- data/lib/darkext/array.rb +52 -0
- data/lib/darkext/beagle.rb +88 -0
- data/lib/darkext/boolean.rb +17 -0
- data/lib/darkext/fiber.rb +48 -0
- data/lib/darkext/float.rb +6 -0
- data/lib/darkext/hash.rb +38 -0
- data/lib/darkext/integer.rb +10 -0
- data/lib/darkext/io.rb +37 -0
- data/lib/darkext/net.rb +28 -0
- data/lib/darkext/numeric.rb +35 -0
- data/lib/darkext/object.rb +11 -0
- data/lib/darkext/sinatra.rb +70 -0
- data/lib/darkext/sitemap_generator.rb +90 -0
- data/lib/darkext/statistics.rb +197 -0
- data/lib/darkext/string.rb +63 -0
- data/lib/darkext/symbol.rb +7 -0
- data/spec/array_spec.rb +112 -0
- data/spec/beagle_spec.rb +42 -0
- data/spec/boolean_spec.rb +53 -0
- data/spec/fiber_spec.rb +35 -0
- data/spec/float_spec.rb +18 -0
- data/spec/hash_spec.rb +26 -0
- data/spec/integer_spec.rb +22 -0
- data/spec/io_spec.rb +44 -0
- data/spec/net_spec.rb +23 -0
- data/spec/numeric_spec.rb +52 -0
- data/spec/object_spec.rb +28 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/statistics_spec.rb +162 -0
- data/spec/string_spec.rb +54 -0
- data/spec/symbol_spec.rb +16 -0
- metadata +119 -0
data/spec/beagle_spec.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Beagle do
|
4
|
+
it 'should respond to all the methods' do
|
5
|
+
Beagle.should respond_to(*%w(home home= start stop status query running?))
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should not work if Beagle.home is nil' do
|
9
|
+
Beagle.home = nil
|
10
|
+
lambda { Beagle.start }.should raise_error
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should return a true or false value from running' do
|
14
|
+
# TODO: use temp dir
|
15
|
+
Beagle.home = '/balls'
|
16
|
+
Beagle.running?.is_boolean?.should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should probably not be running for a random directory' do
|
20
|
+
# TODO: use temp dir
|
21
|
+
Beagle.home = '/balls'
|
22
|
+
Beagle.running?.should be_false
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should return a String for the status' do
|
26
|
+
# TODO: use temp dir
|
27
|
+
Beagle.home = '/balls'
|
28
|
+
Beagle.status.should be_a_kind_of(String)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should return a true or false value from start' do
|
32
|
+
# TODO: use temp dir
|
33
|
+
Beagle.home = '/usr'
|
34
|
+
Beagle.start.is_boolean?.should be_true
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should return a true or false value from stop' do
|
38
|
+
# TODO: use temp dir
|
39
|
+
Beagle.home = '/usr'
|
40
|
+
Beagle.stop.is_boolean?.should be_true
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Object do
|
4
|
+
it 'should respond to the new method' do
|
5
|
+
Object.new.should respond_to(*%w(is_boolean?))
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should return a boolean from is_boolean' do
|
9
|
+
true.is_boolean?.is_boolean?.should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should be randomly booolean' do
|
13
|
+
1.is_boolean?.should be_false
|
14
|
+
'true'.is_boolean?.should be_false
|
15
|
+
:true.is_boolean?.should be_false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe TrueClass do
|
20
|
+
it 'should respond to the new method' do
|
21
|
+
true.should respond_to(*%w(intern))
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should return a symbol from intern' do
|
25
|
+
true.intern.should be_a_kind_of(Symbol)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should be boolean' do
|
29
|
+
true.is_boolean?.should be_true
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should intern properly' do
|
33
|
+
true.intern.should == :true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe FalseClass do
|
38
|
+
it 'should respond to the new method' do
|
39
|
+
false.should respond_to(*%w(intern))
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should return a symbol from intern' do
|
43
|
+
false.intern.should be_a_kind_of(Symbol)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should be boolean' do
|
47
|
+
false.is_boolean?.should be_true
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should intern properly' do
|
51
|
+
false.intern.should == :false
|
52
|
+
end
|
53
|
+
end
|
data/spec/fiber_spec.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Fiber do
|
4
|
+
before(:each) do
|
5
|
+
@fiber = Fiber.new do
|
6
|
+
(1..10).each { |i| Fiber.yield(i) }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should respond to all the class methods' do
|
11
|
+
Fiber.should respond_to(*%w(yield current))
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should respond to all the instance methods' do
|
15
|
+
@fiber.should respond_to(*%w(resume yield))
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should raise an error when it is done' do
|
19
|
+
lambda { 100.times { @fiber.resume } }.should raise_error
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should return a string from inspect' do
|
23
|
+
@fiber.inspect.should be_a_kind_of(String)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should inspect properly' do
|
27
|
+
@fiber.inspect.should match(/.*Fiber:0x/)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should, um, work' do
|
31
|
+
a = Array.new
|
32
|
+
10.times { a << @fiber.resume }
|
33
|
+
a.should == (1..10).to_a
|
34
|
+
end
|
35
|
+
end
|
data/spec/float_spec.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Float do
|
4
|
+
it 'should respond to the new methods' do
|
5
|
+
1.0.should respond_to(*%w(equals?))
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should return a boolean from equals?' do
|
9
|
+
1.0.equals?(1.0000001).is_boolean?.should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should properly equals' do
|
13
|
+
1.00000000000000001.equals?(1.00000000000000002).should be_true
|
14
|
+
1.1.equals?(1.2).should be_false
|
15
|
+
1.1.equals?(1.2,0.1).should be_true
|
16
|
+
1.2.equals?(1.1,0.1).should be_true
|
17
|
+
end
|
18
|
+
end
|
data/spec/hash_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
before(:each) do
|
5
|
+
@h = { :foo => 1, :bar => 2, :baz => 3 }
|
6
|
+
@nh = { :foo => { :bar => 2 }, :baz => { :biz => 4 } }
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should respond to all the new methods' do
|
10
|
+
Hash.new.should respond_to(*%w(nested_find deep_merge!))
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should deep merge' do
|
14
|
+
a = { :foo => 1, :bar => { :baz => 10, :biz => { :hello => :world }}}
|
15
|
+
b = { :pickles => true, :sandwich => { :ham => 2, :bread => { :grains => :whole }}}
|
16
|
+
result = { :foo => 1, :pickles => true, :sandwich => { :ham => 2, :bread => { :grains => :whole }}, :bar => { :baz => 10, :biz => { :hello => :world }}}
|
17
|
+
a.deep_merge!(b)
|
18
|
+
a.should == result
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should find nested things' do
|
22
|
+
@nh.nested_find(:baz,:biz).should == 4
|
23
|
+
@nh.nested_find(:foo,:bar).should == 2
|
24
|
+
@nh.nested_find(:foo,:biz).should be_nil
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Integer do
|
4
|
+
it 'should respond to the new methods' do
|
5
|
+
10.should respond_to(*%w(fact))
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should not calculate the factorial of negative numbers' do
|
9
|
+
lambda { -10.fact }.should raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should return an Integer from factorial' do
|
13
|
+
10.fact.should be_a_kind_of(Integer)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should factorial properly' do
|
17
|
+
5.fact.should == 120
|
18
|
+
1.fact.should == 1
|
19
|
+
0.fact.should == 1
|
20
|
+
1000.fact.should == 402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
21
|
+
end
|
22
|
+
end
|
data/spec/io_spec.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe DarkIO do
|
4
|
+
it 'should return an array of size 2 when capturing both stdout and stderr' do
|
5
|
+
out = DarkIO::capture_output(:stderr => true, :stdout => true) do
|
6
|
+
STDOUT.print('Hello, World!')
|
7
|
+
STDERR.print('Hello, World!')
|
8
|
+
end
|
9
|
+
out.should be_a_kind_of(Array)
|
10
|
+
out.size.should == 2
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should return a string when capturing either stdout or stderr' do
|
14
|
+
out = DarkIO::capture_output(:stderr => true, :stdout => false) do
|
15
|
+
STDERR.print('Hello, World!')
|
16
|
+
end
|
17
|
+
out.should be_a_kind_of(String)
|
18
|
+
out = DarkIO::capture_output(:stdout => true, :stderr => false) do
|
19
|
+
STDOUT.print('Hello, World!')
|
20
|
+
end
|
21
|
+
out.should be_a_kind_of(String)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should return nil if not capturing anything' do
|
25
|
+
DarkIO::capture_output(:stderr => false, :stdout => false) do
|
26
|
+
STDOUT.print('Hello, World!')
|
27
|
+
STDERR.print('Hello, World!')
|
28
|
+
end.nil?.should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should capture output' do
|
32
|
+
HW = 'Hello, World!'
|
33
|
+
out = DarkIO::capture_output { HW.print }
|
34
|
+
out.should == HW
|
35
|
+
out = DarkIO::capture_output(:stderr => true) do
|
36
|
+
(HW + 'STDOUT').print
|
37
|
+
STDERR.print(HW + 'STDERR')
|
38
|
+
end
|
39
|
+
out.shift.should == HW + 'STDOUT'
|
40
|
+
out.shift.should == HW + 'STDERR'
|
41
|
+
out = DarkIO::capture_output(:stderr => true, :stdout => false) { STDERR.print(HW) }
|
42
|
+
out.should == HW
|
43
|
+
end
|
44
|
+
end
|
data/spec/net_spec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Net do
|
4
|
+
it 'should respond to the new methods' do
|
5
|
+
Net.should respond_to(*%w(download download_and_save))
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should raise an exception on 404' do
|
9
|
+
lambda { Net::download('http://www.google.com/pickles.html') }.should raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should raise an exception when trying to save an empty path' do
|
13
|
+
lambda { Net::download_and_save('http://www.google.com/','') }.should raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should not write anything to the disk if not success' do
|
17
|
+
begin
|
18
|
+
Net::download_and_save('http://www.google.com/pickles.html')
|
19
|
+
rescue
|
20
|
+
end
|
21
|
+
File.exists?('pickles.html').should be_false
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Numeric do
|
4
|
+
it 'should respond to the new methods' do
|
5
|
+
[10,10.5].each do |num|
|
6
|
+
num.should respond_to(*%w(square cube sqrt root ln log))
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should return Numeric from most new methods' do
|
11
|
+
%w(square cube sqrt root ln log).each do |method|
|
12
|
+
[10,10.5].each do |num|
|
13
|
+
num.send(method.intern).should be_a_kind_of(Numeric)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should do squares' do
|
19
|
+
4.square.should == 16
|
20
|
+
-4.square.should == 16
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should do cubes' do
|
24
|
+
3.cube.should == 27
|
25
|
+
-3.cube.should == -27
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should do square roots' do
|
29
|
+
9.sqrt.should == 3
|
30
|
+
81.sqrt.should == 9
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should do roots' do
|
34
|
+
9.sqrt.should == 9.root
|
35
|
+
27.root(3).should == 3
|
36
|
+
81.root(4).should == 3
|
37
|
+
5.root(1).should == 5
|
38
|
+
10.root(-1).should == 0.1
|
39
|
+
100.root(-2).should == 0.1
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should do ln' do
|
43
|
+
Math::E.ln.should == 1
|
44
|
+
Math::PI.ln.should be_close(1.14473, 0.001)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should do log' do
|
48
|
+
10.log.should == 1
|
49
|
+
100.log.should == 2
|
50
|
+
Math::PI.log.should be_close(0.4971, 0.0001)
|
51
|
+
end
|
52
|
+
end
|
data/spec/object_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Object do
|
4
|
+
it 'should respond to the new methods' do
|
5
|
+
Object.new.should respond_to(*%w(try tap))
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should return itself from tap' do
|
9
|
+
a = Object.new
|
10
|
+
a.tap { |s| }.should equal(a)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should return nil from try if called on nil' do
|
14
|
+
nil.try(1) { |a| }.should be_nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should try...like really try' do
|
18
|
+
nil.try { |me| me.foo }.should be_nil
|
19
|
+
true.try { |me| me }.should be_true
|
20
|
+
true.try(:nil?).should_not be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should tap dance' do
|
24
|
+
10.tap { |m| m.should == 10 }.should == 10
|
25
|
+
o = Object.new
|
26
|
+
o.tap { |m| m.should == o }.should == o
|
27
|
+
end
|
28
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
|
4
|
+
# gem install redgreen for colored test output
|
5
|
+
begin
|
6
|
+
require 'redgreen' unless ENV['TM_CURRENT_LINE'];
|
7
|
+
rescue LoadError
|
8
|
+
end
|
9
|
+
|
10
|
+
path = File.expand_path(File.dirname(__FILE__) + '/../lib/')
|
11
|
+
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
|
12
|
+
|
13
|
+
require 'lib/darkext'
|
@@ -0,0 +1,162 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
before(:each) do
|
5
|
+
@a = [1,2,3,4,5,6,7,8,9,10]
|
6
|
+
@hist = [1,2,1,1,3,2,1,1,3,3,3,3,3,2,2,1]
|
7
|
+
@b = [5,1,9,12,6,7,8,8,8,1,2,3,4]
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should respond to all the new methods' do
|
11
|
+
Array.new.should respond_to(*%w(mean average ave harmonic_mean h_mean geometric_mean g_mean median histogram mode standard_deviation stddev standard_deviation sample ci standardize standardize! sum_of_squares))
|
12
|
+
Darkext::Statistics.should respond_to(*%w(prob zscore))
|
13
|
+
Darkext::Statistics::Regression.should respond_to(*%w(least_squares))
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should return a Numeric from mean, harmonic_mean, geometric_mean, median, variance, standard_deviation, and sum_of_squares' do
|
17
|
+
%w(mean harmonic_mean geometric_mean median variance standard_deviation sum_of_squares).each do |method|
|
18
|
+
@a.send(method.intern).should be_a_kind_of(Numeric)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should return a hash from histogram' do
|
23
|
+
@a.histogram.should be_a_kind_of(Hash)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should return an array from mode, sample, ci, and standardize' do
|
27
|
+
%w(mode sample ci standardize).each do |method|
|
28
|
+
@a.send(method.intern).should be_a_kind_of(Array)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should raise and erro from mean, harmonic_mean, geometric_mean, median, mode, population_variance, sample_variance, standard_deviation, ci, and sum_of_squares if size is zero' do
|
33
|
+
%w(mean harmonic_mean geometric_mean median mode population_variance sample_variance population_deviation sample_deviation geometric_deviation ci sum_of_squares).each do |method|
|
34
|
+
lambda { Array.new.send(method.intern) }.should raise_error
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should return a Numeric from prob and zscore' do
|
39
|
+
Darkext::Statistics::prob(1).should be_a_kind_of(Numeric)
|
40
|
+
Darkext::Statistics::zscore(0.9).should be_a_kind_of(Numeric)
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should return a Hash from least_squares' do
|
44
|
+
Darkext::Statistics::Regression::least_squares([1,2,4,5],[2,3,6,10]).should be_a_kind_of(Hash)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should raise an error if the arguments to least_squares have different sizes' do
|
48
|
+
lambda { Darkext::Statistics::Regression::least_squares(@a,[1,2,3]) }.should raise_error
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should raise an error if either argument has size.zero?' do
|
52
|
+
lambda { Darkext::Statistics::Regression::least_squares(@a,Array.new) }.should raise_error
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'should handle mean' do
|
56
|
+
(1..6).to_a.mean.should == 3.5
|
57
|
+
[1].mean.should == 1
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should handle harmonic mean' do
|
61
|
+
[60,40].harmonic_mean.should be_close(48, 0.0001)
|
62
|
+
[9].harmonic_mean.should be_close(9, 0.0001)
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should handle geometric mean' do
|
66
|
+
[2,8].geometric_mean.should == 4
|
67
|
+
[9].geometric_mean.should be_close(9, 0.0001)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should handle median' do
|
71
|
+
(1..3).to_a.median.should == 2
|
72
|
+
[1].median.should == 1
|
73
|
+
[2,4,6,8].median.should == 5
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should make histograms' do
|
77
|
+
@hist.histogram.should == { 1 => 6, 2 => 4, 3 => 6 }
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should handle mode' do
|
81
|
+
@hist.mode.should == [1,3]
|
82
|
+
(@hist + [1]).mode.should == [1]
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should handle variance' do
|
86
|
+
(1..6).to_a.variance.should be_close(35/12, 0.0001)
|
87
|
+
[1].variance.should be_close(0, 0.0001)
|
88
|
+
[1,1].variance.should be_close(0, 0.0001)
|
89
|
+
@b.variance.should be_close(10.5, 0.05)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should handle std dev' do
|
93
|
+
[2,4,4,4,5,5,7,9].stddev.should be_close(2, 0.0001)
|
94
|
+
@b.stddev.should be_close(3.24, 0.01)
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'should sample' do
|
98
|
+
@hist.sample(5).size.should == 5
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'should handle confidence intervals' do
|
102
|
+
ci = @b.ci
|
103
|
+
ci.shift.should be_close(5.15, 0.01)
|
104
|
+
ci.shift.should be_close(6.24, 0.01)
|
105
|
+
@b.ci(:type => :upper).shift.should be_close(6.15, 0.01)
|
106
|
+
@b.ci(:type => :lower).shift.should be_close(5.24, 0.01)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should standardize' do
|
110
|
+
[2,2,4,4].standardize.should == [-1,-1,1,1].map(&:to_f)
|
111
|
+
@b.standardize.size.should == 13
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should do sum of squares' do
|
115
|
+
(1..3).to_a.sum_of_squares.should == 2.0
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should do probability' do
|
119
|
+
Darkext::Statistics::prob(2).should be_close(0.96, 0.02)
|
120
|
+
Darkext::Statistics::prob(0.5).should be_close(0.69, 0.02)
|
121
|
+
Darkext::Statistics::prob(Darkext::Statistics::zscore(1)).should be_close(1, 0.0001)
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should do zscore' do
|
125
|
+
Darkext::Statistics::zscore(0.5).should be_close(0, 0.1)
|
126
|
+
Darkext::Statistics::prob(0.75).should be_close(0.77, 0.02)
|
127
|
+
Darkext::Statistics::zscore(Darkext::Statistics::prob(2.5)).should be_close(2.5, 0.0001)
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should handle regressions' do
|
131
|
+
xs = [0,1.2,2,2.9,4,5,6]
|
132
|
+
ys = [0.1,1,2.1,3,4.3,4.9,6]
|
133
|
+
|
134
|
+
results = Darkext::Statistics::Regression::least_squares(xs,ys)
|
135
|
+
|
136
|
+
results[:n].should == xs.size
|
137
|
+
results[:b_1].should be_close(1, 0.1)
|
138
|
+
results[:b_0].should be_close(0, 0.1)
|
139
|
+
results[:predicted].size.should == xs.size
|
140
|
+
|
141
|
+
results[:predicted].each_with_index do |pred,index|
|
142
|
+
pred.should be_close(index, 0.3)
|
143
|
+
end
|
144
|
+
|
145
|
+
results[:residuals].size.should == xs.size
|
146
|
+
|
147
|
+
results[:residuals].each_with_index do |pred,index|
|
148
|
+
pred.should be_close(0, 0.3)
|
149
|
+
end
|
150
|
+
|
151
|
+
results[:ss_e].should be_close(0.15, 0.01)
|
152
|
+
results[:ss_t].should be_close(27.49, 0.01)
|
153
|
+
results[:estimator].should be_close(0.03, 0.01)
|
154
|
+
results[:r_2].should be_close(1, 0.01)
|
155
|
+
results[:r].should be_close(1, 0.01)
|
156
|
+
|
157
|
+
eqn = results[:equation]
|
158
|
+
(0..100).each do |i|
|
159
|
+
eqn.call(i).should be_close(i, 0.25)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|