Skip to content

Binomial Distribution

Esteban Zapata Rojas edited this page Oct 19, 2017 · 1 revision

Binomial Distribution

Instance Methods

Probability mass function

It returns the probability mass value for the specified K. If the specified K < 0 or K > number of specified trials at initialization time, then the probability mass function is not defined.

[25] pry(main)> Distribution::Binomial.new(3,0.5)
=> #<Statistics::Distribution::Binomial:0x00000000018b7648 @number_of_trials=3, @probability_per_trial=0.5>
[26] pry(main)> Distribution::Binomial.new(3,0.5).probability_mass_function(-1)
=> nil
[27] pry(main)> Distribution::Binomial.new(3,0.5).probability_mass_function(4)
=> nil
[28] pry(main)> Distribution::Binomial.new(3,0.5).probability_mass_function(2)
=> 0.375

Cumulative function

It returns the probability P(x <= K) for the specified K. If the specified K < 0 or K > number of specified trials at initialization time, then the cumulative function is not defined.

[29] pry(main)> Distribution::Binomial.new(3,0.5).cumulative_function(4)
=> nil
[30] pry(main)> Distribution::Binomial.new(3,0.5).cumulative_function(-1)
=> nil
[31] pry(main)> Distribution::Binomial.new(3,0.5).cumulative_function(2)
=> 0.875

Mean

It calculates the mean of the binomial distribution. It is number_of_trials * probability_per_trial.

[32] pry(main)> Distribution::Binomial.new(3,0.5).mean
=> 1.5
[33] pry(main)> 3 * 0.5
=> 1.5

Variance

It returns the expected variance for the binomial distribution

[34] pry(main)> Distribution::Binomial.new(3,0.5).variance
=> 0.75

Mode

The mode is defined depending of a number of conditions based on the test, which is defined as test = (number_of_trials + 1) * probability_per_trial.

  • If the test value is zero or is a rational number, it returns Floor(test).
  • If the test value is an integer and 1 <= test <= number of trials, it returns an array with [test, test - 1].
  • If the test value is the same as number of trials - 1, then the returned value is the number of trials.
[43] pry(main)> Distribution::Binomial.new(3, 0.351).mode
=> 1
[44] pry(main)> Distribution::Binomial.new(3, 0.5).mode
=> [2.0, 1.0]
[45] pry(main)> Distribution::Binomial.new(3, 1.0).mode
=> 3