Class: Croupier::Distribution
- Inherits:
-
Object
- Object
- Croupier::Distribution
- Defined in:
- lib/croupier/distribution.rb
Overview
Distribution represents the probability distribution used to generate the sample of random numbers.
Direct Known Subclasses
Croupier::Distributions::Bernoulli, Croupier::Distributions::Binomial, Croupier::Distributions::Cauchy, Croupier::Distributions::Exponential, Croupier::Distributions::Geometric, Croupier::Distributions::Nbinomial, Croupier::Distributions::Normal, Croupier::Distributions::Poisson, Croupier::Distributions::Triangular, Croupier::Distributions::Uniform
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
Class Method Summary collapse
-
.cli_name ⇒ Object
Defines the name of this Distribution to be used from the CLI.
-
.cli_options ⇒ Object
Defines a hash with banner and all available CLI options.
Instance Method Summary collapse
-
#configure(options = {}) ⇒ Object
Merge the hash of options into the default distribution parameters.
-
#default_parameters ⇒ Object
Default hash of distribution parameters.
-
#generate_number ⇒ Object
Generates one random number using the current probability distribution.
-
#generate_sample(n = 1) ⇒ Object
Main method to generate n random numbers using the current probability distribution.
-
#initialize(options = {}) ⇒ Distribution
constructor
Initializes Distribution object and adds received options to the distribution parameters.
-
#params ⇒ Object
convenience method for lazy programmers.
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(={}) @name = nil @description = nil configure() end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
9 10 11 |
# File 'lib/croupier/distribution.rb', line 9 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/croupier/distribution.rb', line 9 def name @name end |
#parameters ⇒ Object
Returns the value of attribute parameters.
8 9 10 |
# File 'lib/croupier/distribution.rb', line 8 def parameters @parameters end |
Class Method Details
.cli_name ⇒ Object
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_options ⇒ Object
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. {: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(={}) @parameters = default_parameters.merge end |
#default_parameters ⇒ Object
Default hash of distribution parameters
24 25 26 |
# File 'lib/croupier/distribution.rb', line 24 def default_parameters {} end |
#generate_number ⇒ Object
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 |
#params ⇒ Object
convenience method for lazy programmers
47 48 49 |
# File 'lib/croupier/distribution.rb', line 47 def params @parameters end |