File: hypergeometric_spec.rb

package info (click to toggle)
ruby-distribution 0.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 572 kB
  • ctags: 370
  • sloc: ruby: 4,270; makefile: 5
file content (113 lines) | stat: -rw-r--r-- 3,220 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
include ExampleWithGSL
#require 'ruby-prof'

# Need to test:
# * that Fixnum fast_choose returns same as choose
# * that pdf and exact_pdf return the same value in Ruby_
# * that cdf in Ruby_ returns the same value as cdf in GSL_

describe Distribution::Hypergeometric do
  describe Distribution::Hypergeometric::GSL_ do
     before do
      @ruby=Distribution::Hypergeometric::Ruby_
      @engine=Distribution::Hypergeometric::GSL_
    end
    it_only_with_gsl "cdf should return values near to exact Ruby calculations" do
     #RubyProf.start
      
      if @engine.respond_to? :cdf
        #[2].each do |k|
         [0,1,2,4,8,16].each do |k|
          @engine.cdf(k, 80, 100, 10000).should be_within(1e-10).of(@ruby.cdf(k, 80, 100, 10000))
      end
      #result = RubyProf.stop
      
      # Print a flat profile to text
      #printer = RubyProf::FlatPrinter.new(result)
      #printer.print(STDOUT)
        
      else
        pending("No #{@engine}.pdf")
      end
    end
    
  end
  describe Distribution::Hypergeometric::Ruby_ do
    
    before do
      @engine=Distribution::Hypergeometric::Ruby_
    end
    it_only_with_gsl "pdf_fast should return same as pdf" do
      pending("Aprox. factorial doesn't work right")
      if @engine.respond_to? :pdf
        [0,1,2,4,8,16].each do |k|
          @engine.pdf_aprox(k, 80, 100, 1000).should be_within(1e-8).of(GSL::Ran::hypergeometric_pdf(k, 80, 920, 100))
        end
      else
        pending("No #{@engine}.pdf")
      end
    end
  
    it_only_with_gsl "should return correct pdf" do
      #RubyProf.start
      
      if @engine.respond_to? :pdf
        #[2].each do |k|
        [0,1,2,4,8,16].each do |k|
          #puts "k:#{k}->#{@engine.pdf(k, 80, 100, 10000).to_f}"
          @engine.pdf(k, 80, 100, 10000).to_f.should be_within(1e-8).of(GSL::Ran::hypergeometric_pdf(k, 80, 9920, 100))
      end
      #result = RubyProf.stop
      
      # Print a flat profile to text
      #printer = RubyProf::FlatPrinter.new(result)
      #printer.print(STDOUT)
        
      else
        pending("No #{@engine}.pdf")
      end
    end
    
    
    
    
    it "should return correct cdf" do
      total=rand(5)+3000
      n=rand(10)+15
      m=rand(10)+5
      ac=0
      0.upto(m) do |i| 
        ac+=@engine.pdf(i,m,n,total)
        @engine.cdf(i,m,n,total).should eq(ac)
      end
    end
    
    it "should return correct p_value" do
      #0.upto(10) do |i|
      #  puts "#{i}:#{@engine.pdf(i,5,7,10)}"
      #end
      total=rand(5)+3000
      n=rand(10)+15
      m=rand(10)+5
      ac=0
      0.upto(m) do |k|
        ac+=@engine.pdf(k,m,n,total)
        @engine.p_value(ac, m, n, total).should eq(k)
      end
    end    
  end
  describe Distribution::Hypergeometric do
    before do
      @engine=Distribution::Hypergeometric
    end
    it {@engine.should respond_to(:exact_pdf) }
    it {@engine.should respond_to(:exact_cdf) }
    it {@engine.should respond_to(:exact_p_value) }
    it "exact pdf should return a Rational" do
      @engine.exact_pdf(1,1,1,1).should_not be_a(Float)
      @engine.exact_pdf(16, 80, 100, 10000).should_not be_a(Float)
    end

  end
end