File: bernoulli.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 (35 lines) | stat: -rw-r--r-- 751 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
module Statistics
  module Distribution
    class Bernoulli
      def self.density_function(n, p)
        return if n != 0 && n != 1 # The support of the distribution is n = {0, 1}.

        case n
        when 0 then 1.0 - p
        when 1 then p
        end
      end

      def self.cumulative_function(n, p)
        return if n != 0 && n != 1 # The support of the distribution is n = {0, 1}.

        case n
        when 0 then 1.0 - p
        when 1 then 1.0
        end
      end

      def self.variance(p)
        p * (1.0 - p)
      end

      def self.skewness(p)
        (1.0 - 2.0*p).to_f / Math.sqrt(p * (1.0 - p))
      end

      def self.kurtosis(p)
        (6.0 * (p ** 2) - (6 * p) + 1) / (p * (1.0 - p))
      end
    end
  end
end