skab 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  module Skab
2
+ require ROOT + '/skab/models/base'
2
3
  require ROOT + '/skab/models/poisson'
3
4
  require ROOT + '/skab/models/binomial'
4
5
 
@@ -0,0 +1,25 @@
1
+ module Skab
2
+ module Models
3
+ class Base
4
+
5
+ private
6
+
7
+ def factorial(n)
8
+ @_factorials ||= {
9
+ 0 => 1,
10
+ 1 => 1
11
+ }
12
+
13
+ raise "Illegal factorial, expecting n > 0" if n < 0
14
+
15
+ last = @_factorials.keys.last
16
+ (last..n).each do |i|
17
+ @_factorials[i] = i * @_factorials[i - 1]
18
+ end
19
+
20
+ return @_factorials[n]
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -27,7 +27,9 @@ module Skab
27
27
  i = 0.0
28
28
  while i <= 1000
29
29
  @distribution[i][1] /= sums[1]
30
+ @distribution[i][1] *= 1000
30
31
  @distribution[i][2] /= sums[2]
32
+ @distribution[i][2] *= 1000
31
33
  i += 1
32
34
  end
33
35
  @distribution
@@ -40,7 +42,7 @@ module Skab
40
42
  while i <= 1000
41
43
  j = 0.0
42
44
  while j <= 1000
43
- @differential[(j - i) / 1000] += distribution[j][2] * distribution[i][1]
45
+ @differential[(j - i) / 1000] += distribution[j][2] * distribution[i][1] / 1000
44
46
  j += 1
45
47
  end
46
48
  i += 1
@@ -48,6 +50,17 @@ module Skab
48
50
  @differential
49
51
  end
50
52
 
53
+ def percentile(p)
54
+ sum = 0.0
55
+ Hash[differential.sort].each do |k, v|
56
+ sum += v
57
+ if sum >= p * 1000
58
+ return k
59
+ end
60
+ end
61
+ percentile
62
+ end
63
+
51
64
  def self.help
52
65
  <<-HELP
53
66
  skab [output] binomial [trials_a] [successes_a] [trials_b] [successes_b]
@@ -70,11 +83,6 @@ skab [output] binomial [trials_a] [successes_a] [trials_b] [successes_b]
70
83
  def binomial_coef(n, k)
71
84
  factorial(n) / (factorial(k) * factorial(n - k))
72
85
  end
73
-
74
- def factorial(n)
75
- return @fact[n] if @fact[n]
76
- @fact[n] = (n > 1) ? n * factorial(n - 1) : 1
77
- end
78
86
  end
79
87
  end
80
88
  end
@@ -1,6 +1,6 @@
1
1
  module Skab
2
2
  module Models
3
- class Poisson
3
+ class Poisson < Base
4
4
 
5
5
  def initialize(args)
6
6
  @a = args.shift.to_i
@@ -31,6 +31,17 @@ module Skab
31
31
  @differential
32
32
  end
33
33
 
34
+ def percentile(p)
35
+ sum = 0.0
36
+ Hash[differential.sort].each do |k, v|
37
+ sum += v
38
+ if sum >= p
39
+ return k
40
+ end
41
+ end
42
+ percentile
43
+ end
44
+
34
45
  def self.help
35
46
  <<-USAGE
36
47
  skab [output] poisson [a] [b]
@@ -54,11 +65,6 @@ skab [output] poisson [a] [b]
54
65
  def poisson(k, delta)
55
66
  ((delta ** k) * Math.exp(-delta)) / factorial(k)
56
67
  end
57
-
58
- def factorial(n)
59
- return @fact[n] if @fact[n]
60
- @fact[n] = (n > 1) ? n * fact(n - 1) : 1
61
- end
62
68
  end
63
69
  end
64
70
  end
@@ -6,18 +6,8 @@ module Skab
6
6
  end
7
7
 
8
8
  def output(model)
9
- sum = 0.0
10
- min = 0
11
- max = 0
12
- Hash[model.differential.sort].each do |k, v|
13
- sum += v
14
- if min == 0 || sum <= 0.05
15
- min = k
16
- end
17
- if max == 0 && sum >= 0.95
18
- max = k
19
- end
20
- end
9
+ min = model.percentile(0.05)
10
+ max = model.percentile(0.95)
21
11
 
22
12
  @out.puts "The difference is located between #{min} and #{max} (90% confidence)"
23
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-30 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2013-05-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description:
15
31
  email: vivien@songkick.com
16
32
  executables:
@@ -30,6 +46,7 @@ files:
30
46
  - lib/skab/output.rb
31
47
  - lib/skab/models/binomial.rb
32
48
  - lib/skab/models/poisson.rb
49
+ - lib/skab/models/base.rb
33
50
  - lib/skab/models.rb
34
51
  homepage: http://github.com/songkick/skab
35
52
  licenses: []