File: normal_spec.rb

package info (click to toggle)
ruby-distribution 0.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 624 kB
  • ctags: 379
  • sloc: ruby: 4,283; makefile: 7
file content (109 lines) | stat: -rw-r--r-- 2,867 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
require File.expand_path(File.dirname(__FILE__)+"/spec_helper.rb")
  
describe Distribution::Normal do
  shared_examples_for "gaussian engine(with rng)" do
  it "should return correct rng" do
      samples=100
      sum=0
      ss=0
      exp_mean=rand(10)-5
      exp_sd=1
      rng=@engine.rng(exp_mean,exp_sd)
      
      samples.times do 
        v=rng.call
        sum+=v
        ss+=(v-exp_mean)**2
      end
      
      
      mean=sum.to_f/samples
      sd=Math::sqrt(ss.to_f/samples)
      mean.should be_within(0.5).of(exp_mean)
      sd.should be_within(0.3).of(exp_sd)
  end
end

shared_examples_for "gaussian engine(with pdf)" do
  it "should return correct pdf" do
    if @engine.respond_to? :pdf
      10.times do |i|
        x=(i-5)/2.0
        pdf=(1.0 / Distribution::SQ2PI)*Math::exp(-(x**2/2.0))
        @engine.pdf(x).should be_within(1e-10).of(pdf)
      end
    else
      pending("No #{@engine}.pdf")
    end
  end
end

shared_examples_for "gaussian engine" do
  it "should return correct cdf" do
    if @engine.respond_to? :cdf
      @engine.cdf(1.96).should be_within(1e-10).of(0.97500210485178)
      @engine.cdf(0).should be_within(1e-10).of(0.5)
    else
      pending("No #{@engine}.cdf")
    end
  end
  it "should return correct p_value" do
    if @engine.respond_to? :p_value
      @engine.p_value(0.5).should be_within(1e-3).of(0)
      10.times do |i|
        x=(i-5) / 2.0
        cdf=@engine.cdf(x)
        @engine.p_value(cdf).should be_within(1e-6).of(x)
      end
    else
      pending("No #{@engine}.p_value")
    end
  end
end

  describe "singleton" do
    before do
      @engine=Distribution::Normal
    end
    it_should_behave_like "gaussian engine"    
    it_should_behave_like "gaussian engine(with rng)"    
    it_should_behave_like "gaussian engine(with pdf)"    
  end
  
  describe Distribution::Normal::Ruby_ do
    before do
      @engine=Distribution::Normal::Ruby_
    end
    it_should_behave_like "gaussian engine"    
    it_should_behave_like "gaussian engine(with rng)"    
    it_should_behave_like "gaussian engine(with pdf)"    
  end
  if Distribution.has_gsl?
    describe Distribution::Normal::GSL_ do
      before do
        @engine=Distribution::Normal::GSL_
      end
    it_should_behave_like "gaussian engine"    
    it_should_behave_like "gaussian engine(with rng)"    
    it_should_behave_like "gaussian engine(with pdf)"    
    end
  end  
  if Distribution.has_statistics2?
    describe Distribution::Normal::Statistics2_ do
      before do
        @engine=Distribution::Normal::Statistics2_
      end
    it_should_behave_like "gaussian engine"    
    end  
  end
  
  if Distribution.has_java?
    describe Distribution::Normal::Java_ do
      before do
        @engine=Distribution::Normal::Java_
      end
    it_should_behave_like "gaussian engine"    
    end  
  end
  
end