Class: Bullshit::NormalDistribution

Inherits:
Object
  • Object
show all
Includes:
Functions
Defined in:
lib/bullshit.rb

Overview

This class is used to compute the Normal Distribution.

Constant Summary

Constants included from Functions

Functions::A, Functions::HALF_LOG_2_PI, Functions::LANCZOS_COEFFICIENTS, Functions::ROOT2

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Functions

beta_regularized, #erf, gammaP_regularized, gammaQ_regularized, log_beta, #log_gamma

Constructor Details

#initialize(mu = 0.0, sigma = 1.0) ⇒ NormalDistribution

Creates a NormalDistribution instance for the values mu and sigma.



893
894
895
# File 'lib/bullshit.rb', line 893

def initialize(mu = 0.0, sigma = 1.0)
  @mu, @sigma = mu.to_f, sigma.to_f
end

Instance Attribute Details

#muObject (readonly)

Returns the value of attribute mu.



897
898
899
# File 'lib/bullshit.rb', line 897

def mu
  @mu
end

#sigmaObject (readonly)

Returns the value of attribute sigma.



899
900
901
# File 'lib/bullshit.rb', line 899

def sigma
  @sigma
end

Instance Method Details

#inverse_probability(p) ⇒ Object

Returns the inverse cumulative probability value of the NormalDistribution for the probability p.



909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
# File 'lib/bullshit.rb', line 909

def inverse_probability(p)
  case
  when p <= 0
    -1 / 0.0
  when p >= 1
    1 / 0.0
  when p == 0.5 # This is a bit sloppy, maybe improve this later.
    @mu
  else
    begin
      NewtonBisection.new { |x| probability(x) - p }.solve(nil, 1_000_000)
    rescue
      0 / 0.0
    end
  end
end

#probability(x) ⇒ Object

Returns the cumulative probability (p-value) of the NormalDistribution for the value x.



903
904
905
# File 'lib/bullshit.rb', line 903

def probability(x)
  0.5 * (1 + erf((x - @mu) / (@sigma * ROOT2)))
end