File: poisson.rb

package info (click to toggle)
ruby-statistics 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 224 kB
  • sloc: ruby: 989; sh: 4; makefile: 4
file content (38 lines) | stat: -rw-r--r-- 1,105 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
module Statistics
  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_f
      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_f)
      end
    end
  end
end