Class: Bullshit::TDistribution

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

Overview

This class is used to compute the T-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) ⇒ TDistribution

Returns a TDistribution instance for the degrees of freedom df.


846
847
848
# File 'lib/bullshit.rb', line 846

def initialize(df)
  @df = df
end

Instance Attribute Details

#dfObject (readonly)

Degrees of freedom.


851
852
853
# File 'lib/bullshit.rb', line 851

def df
  @df
end

Instance Method Details

#inverse_probability(p) ⇒ Object

Returns the inverse cumulative probability (t-value) of the TDistribution for the probability p.


870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
# File 'lib/bullshit.rb', line 870

def inverse_probability(p)
  case
  when p <= 0
    -1 / 0.0
  when p >= 1
    1 / 0.0
  else 
    begin
      bisect = NewtonBisection.new { |x| probability(x) - p }
      range = bisect.bracket(-10..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 TDistribution for the t-value x.


855
856
857
858
859
860
861
862
863
864
865
866
# File 'lib/bullshit.rb', line 855

def probability(x)
  if x == 0
    0.5
  else
    t = beta_regularized(@df / (@df + x ** 2.0), 0.5 * @df, 0.5)
    if x < 0.0
      0.5 * t
    else
      1 - 0.5 * t
    end
  end
end