Sha256: 9ffa863e3d3bba85a94bf4b79acfec86edca96e488f88d4cd3c13f37f48bd56b

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

module RubyStatistics
  module Distribution
    class Poisson
      attr_accessor :expected_number_of_occurrences

      alias_method :mean, :expected_number_of_occurrences
      alias_method :variance, :expected_number_of_occurrences

      def initialize(l)
        self.expected_number_of_occurrences = l
      end

      def probability_mass_function(k)
        return if k < 0 || expected_number_of_occurrences < 0

        k = k.to_i

        upper = (expected_number_of_occurrences ** k) * Math.exp(-expected_number_of_occurrences)
        lower = Math.factorial(k)

        upper/lower.to_r
      end

      def cumulative_function(k)
        return if k < 0 || expected_number_of_occurrences < 0

        k = k.to_i

        upper = Math.lower_incomplete_gamma_function((k + 1).floor, expected_number_of_occurrences)
        lower = Math.factorial(k.floor)

        # We need the right tail, i.e.: The upper incomplete gamma function. This can be
        # achieved by doing a substraction between 1 and the lower incomplete gamma function.
        1 - (upper/lower.to_r)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ruby-statistics-4.1.0 lib/ruby-statistics/distribution/poisson.rb
ruby-statistics-4.0.1 lib/ruby-statistics/distribution/poisson.rb
ruby-statistics-4.0.0 lib/ruby-statistics/distribution/poisson.rb