Class: Croupier::Distributions::Geometric

Inherits:
Croupier::Distribution show all
Defined in:
lib/croupier/distributions/geometric.rb

Overview

Geometric Distribution Discrete probability distribution that expresses the number of X Bernoulli trials needed to get one success, supported on the set { 1, 2, 3, …}

Wikipedia – en.wikipedia.org/wiki/Geometric_distribution I made this choice because it’s Knuth choice. (The Art of Computer Programming, Volume 2, 3.4.1.F )

Instance Attribute Summary

Attributes inherited from Croupier::Distribution

#description, #name, #parameters

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Croupier::Distribution

#configure, #generate_number, #generate_sample, #params

Constructor Details

#initialize(options = {}) ⇒ Geometric

Returns a new instance of Geometric.



15
16
17
18
19
20
# File 'lib/croupier/distributions/geometric.rb', line 15

def initialize(options={})
  @name = "Geometric distribution"
  @description = "Discrete probability distribution that expresses the number of X Bernoulli trials needed to get one success, supported on the set { 1, 2, 3, ...}"
  configure(options)
  raise Croupier::InputParamsError, "Probability of success must be in the interval [0,1]" if params[:success] > 1 || params[:success] < 0
end

Class Method Details

.cli_nameObject



32
33
34
# File 'lib/croupier/distributions/geometric.rb', line 32

def self.cli_name
  "geometric"
end

.cli_optionsObject



36
37
38
39
40
41
42
# File 'lib/croupier/distributions/geometric.rb', line 36

def self.cli_options
  {:options => [
     [:success, 'success probability of each trial', {:type=>:float, :short => "-p", :default => 0.5}]
   ],
   :banner => "Geometric distribution. Discrete probability distribution that expresses the number of X Bernoulli trials needed to get one success, supported on the set { 1, 2, 3, ...} }"
  }
end

Instance Method Details

#default_parametersObject



28
29
30
# File 'lib/croupier/distributions/geometric.rb', line 28

def default_parameters
  {:success => 0.5}
end

#inv_cdf(n) ⇒ Object

Fair point: it is not the inverse of the cdf, but it generates the distribution from an uniform.



24
25
26
# File 'lib/croupier/distributions/geometric.rb', line 24

def inv_cdf n
  (Math.log(1-n) / Math.log(1-params[:success])).ceil
end