Class: Statistical::Distribution::TwoPoint
- Inherits:
-
Object
- Object
- Statistical::Distribution::TwoPoint
- Defined in:
- lib/statistical/distribution/two_point.rb
Overview
The states used to represent success & failure must be Numeric. Using it on generic state lables can cause strange outcomes!
state_failure < state_sucesss, for the sake of sanity.
Two-Point distribution implementation that uses generic labels for states that it’s random variables can take. The assumptions made would be that the states are comparable and failure < success in whatever scheme of comparison that the state objects implement. This defaults to behaving as the bernoulli distribution
Direct Known Subclasses
Instance Attribute Summary collapse
-
#p ⇒ Object
readonly
This is probably the best but the least descriptive variable name.
-
#q ⇒ Object
readonly
This is probably the best but the least descriptive variable name.
-
#states ⇒ Object
readonly
This is probably the best but the least descriptive variable name.
-
#support ⇒ Object
readonly
This is probably the best but the least descriptive variable name.
Instance Method Summary collapse
-
#cdf(x) ⇒ Float
Returns value of cumulative density function at a point.
-
#initialize(prob_success = 0.5, state_failure = 0, state_success = 1) ⇒ TwoPoint
constructor
Returns a new instance of the TwoPoint distribution.
-
#mean ⇒ Object
Returns the expected mean value for the calling instance.
-
#pdf(x) ⇒ Float
Returns value of probability density function at a given state of the random variate X.
-
#quantile(p) ⇒ Object
(also: #p_value)
Returns value of inverse CDF for a given probability.
-
#variance ⇒ Object
Returns the expected value of variance for the calling instance.
Constructor Details
#initialize(prob_success = 0.5, state_failure = 0, state_success = 1) ⇒ TwoPoint
The states used to represent success & failure must be Numeric. Using it on generic state lables can cause strange outcomes!
state_failure < state_sucesss, required to have a sane CDF.
Returns a new instance of the TwoPoint distribution
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/statistical/distribution/two_point.rb', line 37 def initialize(prob_success = 0.5, state_failure = 0, state_success = 1) if state_failure == state_success raise ArgumentError, 'Success & failure must be two distinct states' end if state_failure > state_success raise ArgumentError, 'Failure state must be smaller that the success state!' end unless (state_failure + state_success).is_a?(Numeric) raise ArgumentError, "States must be Numeric! Found #{state_failure.class} and #{state_success.class}" end if prob_success > 1 || prob_success < 0 raise ArgumentError, "Probabilty of success must be within [0, 1]. Found #{prob_success}" end @p = prob_success @q = 1 - prob_success @states = { failure: state_failure, success: state_success } @support = @states.values.sort self end |
Instance Attribute Details
#p ⇒ Object (readonly)
This is probably the best but the least descriptive variable name
21 22 23 |
# File 'lib/statistical/distribution/two_point.rb', line 21 def p @p end |
#q ⇒ Object (readonly)
This is probably the best but the least descriptive variable name
21 22 23 |
# File 'lib/statistical/distribution/two_point.rb', line 21 def q @q end |
#states ⇒ Object (readonly)
This is probably the best but the least descriptive variable name
21 22 23 |
# File 'lib/statistical/distribution/two_point.rb', line 21 def states @states end |
#support ⇒ Object (readonly)
This is probably the best but the least descriptive variable name
23 24 25 |
# File 'lib/statistical/distribution/two_point.rb', line 23 def support @support end |
Instance Method Details
#cdf(x) ⇒ Float
Returns value of cumulative density function at a point. Calculated
using some technique that you might want to name
87 88 89 90 91 |
# File 'lib/statistical/distribution/two_point.rb', line 87 def cdf(x) return 0 if x < @states[:failure] return @q if x.between?(@states[:failure], @states[:success]) return 1 if x >= @states[:success] end |
#mean ⇒ Object
Returns the expected mean value for the calling instance.
109 110 111 |
# File 'lib/statistical/distribution/two_point.rb', line 109 def mean return @p * @states[:success] + @q * @states[:failure] end |
#pdf(x) ⇒ Float
Returns value of probability density function at a given state of the random variate X. Essentially: “what’s P(X=x)?”
75 76 77 78 79 |
# File 'lib/statistical/distribution/two_point.rb', line 75 def pdf(x) return @p if @states[:success] == x return @q if @states[:failure] == x return 0 end |
#quantile(p) ⇒ Object Also known as: p_value
Returns value of inverse CDF for a given probability
100 101 102 103 104 |
# File 'lib/statistical/distribution/two_point.rb', line 100 def quantile(p) raise RangeError, "`p` must be in [0, 1], found: #{p}" if p < 0 || p > 1 return @states[:failure] if p <= @q return @states[:success] if p > @q end |
#variance ⇒ Object
Returns the expected value of variance for the calling instance.
116 117 118 119 |
# File 'lib/statistical/distribution/two_point.rb', line 116 def variance return @p * (@states[:success]**2) + @q * (@states[:failure]**2) - (mean**2) end |