Class: ViralSeq::Math::PoissonDist

Inherits:
Object
  • Object
show all
Defined in:
lib/viral_seq/math.rb

Overview

class for poisson distribution.

An event can occur 0, 1, 2, … times in an interval.
The average number of events in an interval is designated λ (lambda).
λ is the event rate, also called the rate parameter.
The probability of observing k events in an interval is given by the equation

P(k events in interval) = e^(-λ) * λ^k / k!

λ is the average number of events per interval
e is the number 2.71828... (Euler's number) the base of the natural logarithms
k takes values 0, 1, 2, …
k! = k × (k − 1) × (k − 2) × … × 2 × 1 is the factorial of k.

calculate the probablity of 3 mutations on one sequence

new_poisson_dist = ViralSeq::Math::PoissonDist.new(0.01)
prob_hash = new_poisson_dist.poisson_hash
1000 * prob_hash[3].round(5)
=> 0.00017

Examples:

given the mutation rate at 0.01 and sequence length of 1000 bp,

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rate, k = 5) ⇒ PoissonDist

initialize with given event rate λ, default events upper limit set to 5



65
66
67
68
69
70
71
72
73
# File 'lib/viral_seq/math.rb', line 65

def initialize(rate,k = 5)
  @rate = rate
  @k = k
  @poisson_hash = {}
  (0..k).each do |n|
    p = (rate**n * ::Math::E**(-rate))/n.factorial
    @poisson_hash[n] = p
  end
end

Instance Attribute Details

#kInteger

Returns maxinum events number shows in @poisson_hash.

Returns:

  • (Integer)

    maxinum events number shows in @poisson_hash



78
79
80
# File 'lib/viral_seq/math.rb', line 78

def k
  @k
end

#poisson_hashHash (readonly)

Returns probablity hash of :event_number => :probablity.

Returns:

  • (Hash)

    probablity hash of :event_number => :probablity



80
81
82
# File 'lib/viral_seq/math.rb', line 80

def poisson_hash
  @poisson_hash
end

#rateFloat

Returns event rate λ.

Returns:

  • (Float)

    event rate λ



76
77
78
# File 'lib/viral_seq/math.rb', line 76

def rate
  @rate
end