Class: Croupier::Distributions::Nbinomial
- Inherits:
-
Croupier::Distribution
- Object
- Croupier::Distribution
- Croupier::Distributions::Nbinomial
- Defined in:
- lib/croupier/distributions/nbinomial.rb
Overview
Negative Binomial Distribution Discrete probability distribution of the number of successes in a sequence of Bernoulli trials before a specified (non-random) number of failures (denoted size) occur.
Wikipedia – en.wikipedia.org/wiki/Negative_binomial_distribution
Instance Attribute Summary
Attributes inherited from Croupier::Distribution
#description, #name, #parameters
Class Method Summary collapse
Instance Method Summary collapse
- #default_parameters ⇒ Object
-
#generate_sample(n = 1) ⇒ Object
Fair point: it is not the inverse of the cdf, but it generates the distribution from an uniform.
-
#initialize(options = {}) ⇒ Nbinomial
constructor
A new instance of Nbinomial.
Methods inherited from Croupier::Distribution
#configure, #generate_number, #params
Constructor Details
#initialize(options = {}) ⇒ Nbinomial
Returns a new instance of Nbinomial.
13 14 15 16 17 18 |
# File 'lib/croupier/distributions/nbinomial.rb', line 13 def initialize(={}) @name = "Negative binomial distribution" @description = "Discrete probability distribution of the number of successes in a sequence of Bernoulli trials before a specified (non-random) number of failures (denoted size) occur." configure() 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_name ⇒ Object
34 35 36 |
# File 'lib/croupier/distributions/nbinomial.rb', line 34 def self.cli_name "nbinomial" end |
.cli_options ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/croupier/distributions/nbinomial.rb', line 38 def self. {:options => [ [:size, 'number of errors', {:type => :integer, :default => 1}], [:success, 'success probability of each trial', {:type=>:float, :short => "-p", :default => 0.5}] ], :banner => "Negative binomial distribution. Discrete probability distribution of the number of successes in a sequence of Bernoulli trials before a specified (non-random) number of failures (denoted size) occur." } end |
Instance Method Details
#default_parameters ⇒ Object
30 31 32 |
# File 'lib/croupier/distributions/nbinomial.rb', line 30 def default_parameters {:success => 0.5, :size => 1} end |
#generate_sample(n = 1) ⇒ Object
Fair point: it is not the inverse of the cdf, but it generates the distribution from an uniform.
22 23 24 25 26 27 28 |
# File 'lib/croupier/distributions/nbinomial.rb', line 22 def generate_sample n=1 generate_geometrics(n).each_slice(params[:size]).map do |sample| sample.inject(-params[:size], &:+) # Inject starts on -size because # this way it is equivalent to: # sample.map{|x| x - 1}.inject(&:+) end end |