Class: Statistical::Distribution::TwoPoint

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

Overview

Note:

The states used to represent success & failure must be Numeric. Using it on generic state lables can cause strange outcomes!

Note:

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

Author:

  • Vaibhav Yenamandra

Direct Known Subclasses

Bernoulli

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prob_success = 0.5, state_failure = 0, state_success = 1) ⇒ TwoPoint

Note:

The states used to represent success & failure must be Numeric. Using it on generic state lables can cause strange outcomes!

Note:

state_failure < state_sucesss, required to have a sane CDF.

Returns a new instance of the TwoPoint distribution

Parameters:

  • prob_success (Float) (defaults to: 0.5)

    The probability of success

  • state_success (Numeric) (defaults to: 1)

    An object to describe the 1-state of success

  • state_failure (Numeric) (defaults to: 0)

    An object to describe the 0-state of failure



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

#pObject (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

#qObject (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

#statesObject (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

#supportObject (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

Parameters:

  • x (Numeric)

    The state the the random variable takes. Can be 0, 1

Returns:

  • (Float)

    The cumulative probability over all of the random variates states.



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

#meanObject

Returns the expected mean value for the calling instance.

Returns:

  • Mean of the distribution



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)?”

Parameters:

  • x (Numeric)

    The state the the random variable takes. Can be 0, 1

Returns:

  • (Float)
    • p if state (x) is 1.

Raises:

  • (ArgumentError)

    if x is not of the states this instance was initialized with



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

Parameters:

  • p (Numeric)

    a value within [0, 1]

Returns:

  • Inverse CDF for valid p

Raises:

  • (RangeError)

    if p > 1 or p < 0

See Also:



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

#varianceObject

Returns the expected value of variance for the calling instance.

Returns:

  • Variance of the distribution



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