Class: Bullshit::ChiSquareDistribution

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

Overview

This class is used to compute the Chi-Square 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(df) ⇒ ChiSquareDistribution

Creates a ChiSquareDistribution for df degrees of freedom.


934
935
936
937
# File 'lib/bullshit.rb', line 934

def initialize(df)
  @df = df
  @df_half = @df / 2.0
end

Instance Attribute Details

#dfObject (readonly)

Returns the value of attribute df


939
940
941
# File 'lib/bullshit.rb', line 939

def df
  @df
end

Instance Method Details

#inverse_probability(p) ⇒ Object

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


953
954
955
956
957
958
959
960
961
962
963
964
965
966
# File 'lib/bullshit.rb', line 953

def inverse_probability(p)
  case
  when p <= 0, p >= 1
    0.0
  else
    begin
      bisect = NewtonBisection.new { |x| probability(x) - p }
      range = bisect.bracket 0.5..10
      bisect.solve(range, 1_000_000)
    rescue
      0 / 0.0
    end
  end
end

#probability(x) ⇒ Object

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


943
944
945
946
947
948
949
# File 'lib/bullshit.rb', line 943

def probability(x)
  if x < 0
    0.0
  else
    gammaP_regularized(x / 2, @df_half)
  end
end