File: lognormal_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 (54 lines) | stat: -rw-r--r-- 1,459 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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper.rb')

describe Distribution::LogNormal do
  shared_examples_for 'log-normal engine' do
    it 'should return correct pdf' do
      if @engine.respond_to? :pdf
        1.upto(10) {
          u = rand
          s = rand * 100
          x = rand * 50
          exp = (1.0 / (x * s * Math.sqrt(2 * Math::PI))) * Math.exp(-((Math.log(x) - u)**2 / (2 * s**2)))
          expect(@engine.pdf(x, u, s)).to be_within(1e-10).of(exp)
        }
      else
        pending("No #{@engine}.pdf")
       end
    end
    it 'should return correct cdf' do
      if @engine.respond_to? :cdf
        1.upto(10) {
          u = rand
          s = rand * 100
          x = rand * 50
          exp = Distribution::Normal.cdf((Math.log(x) - u) / s)
          expect(@engine.cdf(x, u, s)).to be_within(1e-10).of(exp)
        }
      else
        pending("No #{@engine}.cdf")
       end
    end
  end

  describe 'singleton' do
    before do
      @engine = Distribution::LogNormal
    end
    it_should_behave_like 'log-normal engine'
  end

  describe Distribution::LogNormal::Ruby_ do
    before do
      @engine = Distribution::LogNormal::Ruby_
    end
    it_should_behave_like 'log-normal engine'
  end
  if Distribution.has_gsl?
    describe Distribution::LogNormal::GSL_ do
      before do
        @engine = Distribution::LogNormal::GSL_
      end
      it_should_behave_like 'log-normal engine'
    end
  end
end