Class: ForChrisLib::PChiSquared

Inherits:
Object
  • Object
show all
Defined in:
lib/chris_lib/for_chris_lib.rb

Overview

Wrapper around chi-squared tail probability helpers.

Instance Method Summary collapse

Constructor Details

#initialize(calculator: nil) ⇒ PChiSquared

Returns a new instance of PChiSquared.

Parameters:

  • calculator (#call) (defaults to: nil)

    dependency used to evaluate the tail probability



74
75
76
# File 'lib/chris_lib/for_chris_lib.rb', line 74

def initialize(calculator: nil)
  @calculator = calculator
end

Instance Method Details

#call(dof, nu) ⇒ Float

Returns upper-tail probability.

Parameters:

  • dof (Integer)

    degrees of freedom

  • nu (Numeric)

    chi-squared statistic

Returns:

  • (Float)

    upper-tail probability

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/chris_lib/for_chris_lib.rb', line 81

def call(dof, nu)
  raise ArgumentError, 'degrees of freedom must be positive' unless dof.is_a?(Numeric) && dof.positive?
  raise ArgumentError, 'chi-squared statistic must be non-negative' unless nu.is_a?(Numeric) && nu >= 0
  if calculator
    return calculator.call(dof, nu)
  end

  # Use the complemented incomplete gamma to evaluate the survival function.
  s = dof.to_f / 2.0
  x = nu.to_f / 2.0
  regularized_gamma_q(s, x)
end