Class: TM::ProbDist

Inherits:
Object
  • Object
show all
Defined in:
lib/nysol/dictionary.rb

Instance Method Summary collapse

Constructor Details

#initialize(ub = 200) ⇒ ProbDist



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/nysol/dictionary.rb', line 18

def initialize(ub=200)
  @ub=ub
  @pascal=Array.new(ub+1,0)
  (0..ub).each{|i|
    @pascal[i]=Array.new(ub+1,0)
  }

  @pascal[0][0]=1
  (1..ub).each{|i|
    @pascal[i][0]=1
    @pascal[i][1]=i
    (2..i).each{|j|
      @pascal[i][j] = @pascal[i-1][j-1] + @pascal[i-1][j]
    }
  }
end

Instance Method Details

#combi(n, x) ⇒ Object



35
36
37
# File 'lib/nysol/dictionary.rb', line 35

def combi(n,x)
  return @pascal[n][x]
end

#normDist(x, mean, sd) ⇒ Object

正規分布の分布関数



40
41
42
# File 'lib/nysol/dictionary.rb', line 40

def normDist(x, mean, sd)
  return 1.0 - (1 + Math::erf((x - mean) / (sd * Math::sqrt(2)))) * 0.5
end

#prob(x, n, p) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nysol/dictionary.rb', line 44

def prob(x, n, p)
  q = 1 - p
accum=0.0
if n>@ub then
  accum=normDist(x.to_f-0.5, n.to_f*p, Math::sqrt(n.to_f*p*q))
else
  (x..n).each{|i|
    accum += combi(n,i) * (p ** i) * (q ** (n - i))
  }
end
  return accum
end