class Digiproc::Probability::TheoreticalGaussianDistribution

Class to calculate probabilities based off of a gaussian distributed random variable.

Attributes

mean[R]
stddev[R]
variance[R]

Public Class Methods

new(mean: , stddev: , generator_strategy: Digiproc::Strategies::GaussianGeneratorBoxMullerStrategy) click to toggle source
# File lib/probability/theoretical_gaussian_distribution.rb, line 7
def initialize(mean: , stddev: , generator_strategy: Digiproc::Strategies::GaussianGeneratorBoxMullerStrategy)
    @mean, @stddev = mean, stddev
    @variance = stddev ** 2
    @generator_strategy = generator_strategy.new(mean, stddev)
end

Public Instance Methods

cdf(x) click to toggle source

Return the cdf [Float] of an input x [Float]

gd = Digiproc::Probability::TheoreticalGaussianDistribution.new(mean: 0, stddev: 1)
gd.cdf(0.3) # => 0.6179114221889526
# File lib/probability/theoretical_gaussian_distribution.rb, line 17
def cdf(x)
    1 - q(x)
end
p_between(lower, upper) click to toggle source

Return the probability [Float] of the random variable being between an upper [Float] and lower [Float] value

gd = Digiproc::Probability::TheoreticalGaussianDistribution.new(mean: 0, stddev: 1)
gd.p_between(-1, 1) # => 0.6826894921370859
# File lib/probability/theoretical_gaussian_distribution.rb, line 34
def p_between(lower, upper)
    cdf(upper) - cdf(lower)
end
p_outside(lower, upper) click to toggle source

Return the probability [Float] of the random variable being outside an upper [Float] and lower [Float] value

gd = Digiproc::Probability::TheoreticalGaussianDistribution.new(mean: 0, stddev: 1)
gd.p_outside(-1, 1) # => 0.31731050786291415
# File lib/probability/theoretical_gaussian_distribution.rb, line 42
def p_outside(lower, upper)
    cdf(lower) + q(upper)
end
q(x) click to toggle source

Return the q value [Float] of an input x [Float]

gd = Digiproc::Probability::TheoreticalGaussianDistribution.new(mean: 0, stddev: 1)
gd.q(0.3) # => 0.3820885778110474
# File lib/probability/theoretical_gaussian_distribution.rb, line 25
def q(x)
    xform_x = xform_x_to_standard(x)
    0.5 * Math.erfc(xform_x  / (2 ** 0.5))
end
rand() click to toggle source

Outputs a single random number from the gaussian distribution

gd.rand # => 1.4173209026395848
# File lib/probability/theoretical_gaussian_distribution.rb, line 49
def rand
    @generator_strategy.rand
end

Private Instance Methods

xform_x_to_standard(x) click to toggle source
# File lib/probability/theoretical_gaussian_distribution.rb, line 56
def xform_x_to_standard(x)
    (x - self.mean) / self.stddev.to_f
end