Class: Croupier::Distribution

Inherits:
Object
  • Object
show all
Defined in:
lib/croupier/distribution.rb

Overview

Distribution represents the probability distribution used to generate the sample of random numbers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Distribution

Initializes Distribution object and adds received options to the distribution parameters.



12
13
14
15
16
# File 'lib/croupier/distribution.rb', line 12

def initialize(options={})
  @name = nil
  @description = nil
  configure(options)
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/croupier/distribution.rb', line 9

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/croupier/distribution.rb', line 9

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



8
9
10
# File 'lib/croupier/distribution.rb', line 8

def parameters
  @parameters
end

Class Method Details

.cli_nameObject

Defines the name of this Distribution to be used from the CLI.



68
69
70
# File 'lib/croupier/distribution.rb', line 68

def self.cli_name
  ''
end

.cli_optionsObject

Defines a hash with banner and all available CLI options. It is a hash with two keys:

:banner =>  A string used as banner in the command line help
:options => An array of arrays each one representing a CLI option
           with the following format:[:option, 'description', {hash of option params}]
 Example:
 {:banner => "This distribution generates only number 33",
  :options => [
    [:mean, 'The mean of the distribution', {:default => 33}],
    [:median, 'Median of the distribution',{:default => 33.0, :type => :float}]
  ]
 }


63
64
65
# File 'lib/croupier/distribution.rb', line 63

def self.cli_options
  {:banner => nil, :options=>[]}
end

Instance Method Details

#configure(options = {}) ⇒ Object

Merge the hash of options into the default distribution parameters



19
20
21
# File 'lib/croupier/distribution.rb', line 19

def configure(options={})
  @parameters = default_parameters.merge options
end

#default_parametersObject

Default hash of distribution parameters



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

def default_parameters
  {}
end

#generate_numberObject

Generates one random number using the current probability distribution



38
39
40
41
42
43
44
# File 'lib/croupier/distribution.rb', line 38

def generate_number
  if self.respond_to? :inv_cdf
    inv_cdf(rand)
  else
    generate_sample 1
  end
end

#generate_sample(n = 1) ⇒ Object

Main method to generate n random numbers using the current probability distribution



29
30
31
32
33
34
35
# File 'lib/croupier/distribution.rb', line 29

def generate_sample(n=1)
  if self.respond_to? :inv_cdf
    (1..n).map{ inv_cdf(rand) }
  else
    (1..n).map{ generate_number }
  end
end

#paramsObject

convenience method for lazy programmers



47
48
49
# File 'lib/croupier/distribution.rb', line 47

def params
  @parameters
end