File: poisson_spec.rb

package info (click to toggle)
ruby-distribution 0.8.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 624 kB
  • sloc: ruby: 4,535; makefile: 10
file content (114 lines) | stat: -rw-r--r-- 2,918 bytes parent folder | download
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
114
require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')
include ExampleWithGSL
describe Distribution::Poisson do
  shared_examples_for 'poisson engine(with rng)' do
    it 'should return correct rng' do
      # The expected rng output when 1 is set as the seed value
      exp_means = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9]

      lambda_val = rand(10) + 1
      exp_mean = exp_means[lambda_val - 1]
      seed = 1

      rng = @engine.rng(lambda_val, seed)

      samples = 100
      sum = 0
      samples.times do
        v = rng.call
        sum += v
      end
      mean = sum / samples
      expect(mean.to_i).to be_within(1e-10).of(exp_mean)
    end
  end

  shared_examples_for 'poisson engine(with rng)' do
    it 'rng with a specified seed should be reproducible' do
      seed = 1
      rng1 = @engine.rng(3, seed)
      rng2 = @engine.rng(3, seed)
      expect((rng1.call)).to eq(rng2.call)
    end
  end


  shared_examples_for 'poisson engine' do
    it 'should return correct pdf' do
      if @engine.respond_to? :pdf
        [0.5, 1, 1.5].each {|l|
          1.upto(5) {|k|
            expect(@engine.pdf(k, l)).to be_within(1e-10).of((l**k * Math.exp(-l)).quo(Math.factorial(k)))
          }
        }
      else
        pending("No #{@engine}.pdf")
      end
    end

    it_only_with_gsl 'should return correct cdf' do
      if @engine.respond_to? :cdf
        [0.5, 1, 1.5, 4, 10].each {|l|
          1.upto(5) {|k|
            expect(@engine.cdf(k, l)).to be_within(1e-10).of(GSL::Cdf.poisson_P(k, l))
          }
        }

      else
        pending("No #{@engine}.cdf")
      end
    end

    it 'should return correct p_value' do
      pending('No exact p_value')
      if @engine.respond_to? :p_value
        [0.1, 1, 5, 10].each {|l|
          1.upto(20) {|k|
            pr = @engine.cdf(k, l)
            expect(@engine.p_value(pr, l)).to eq(k)
          }
        }
      else
        skip("No #{@engine}.p_value")
      end
    end
  end

  describe 'singleton' do
    before do
      @engine = Distribution::Poisson
    end
    it_should_behave_like 'poisson engine'
  end

  describe Distribution::Poisson::Ruby_ do
    before do
      @engine = Distribution::Poisson::Ruby_
    end
    it_should_behave_like 'poisson engine'
  end
  if Distribution.has_gsl?
    describe Distribution::Poisson::GSL_ do
      before do
        @engine = Distribution::Poisson::GSL_
      end
      it_should_behave_like 'poisson engine'
    end
  end
  if Distribution.has_java?
    describe Distribution::Poisson::Java_ do
      before do
        @engine = Distribution::Poisson::Java_
      end
      it_should_behave_like 'poisson engine'

      it 'should return correct cdf' do
        [0.5, 1, 1.5, 4, 10].each {|l|
          1.upto(5) {|k|
            expect(@engine.cdf(k, l)).to be_within(1e-10).of(Distribution::Poisson::Ruby_.cdf(k, l))
          }
        }
      end
    end
  end
end