Class: MoreMath::TDistribution
- Includes:
- Functions
- Defined in:
- lib/more_math/distributions.rb
Overview
This class is used to compute the T-Distribution.
The t-distribution (Student’s t-distribution) is a probability distribution that is used when estimating the mean of a normally distributed population in situations where the sample size is small and the population standard deviation is unknown.
Instance Attribute Summary collapse
-
#df ⇒ Integer
readonly
Degrees of freedom.
Instance Method Summary collapse
-
#initialize(df) ⇒ TDistribution
constructor
Returns a TDistribution instance for the degrees of freedom
df. -
#inverse_probability(p) ⇒ Float
Returns the inverse cumulative probability (t-value) of the TDistribution for the probability
p. -
#probability(x) ⇒ Float
Returns the cumulative probability (p-value) of the TDistribution for the t-value
x.
Methods included from Functions
#beta, beta, #beta_regularized, beta_regularized, #cantor_pairing, cantor_pairing, #cantor_pairing_inv, cantor_pairing_inv, #erf, #erfc, #gamma, gamma, #gammaP_regularized, gammaP_regularized, #gammaQ_regularized, gammaQ_regularized, #log_beta, log_beta, #log_ceil, log_ceil, #log_floor, log_floor, #log_gamma, #logb, logb, #numberify_string, numberify_string, #stringify_number, stringify_number
Constructor Details
#initialize(df) ⇒ TDistribution
Returns a TDistribution instance for the degrees of freedom df.
169 170 171 |
# File 'lib/more_math/distributions.rb', line 169 def initialize(df) @df = df end |
Instance Attribute Details
#df ⇒ Integer (readonly)
Degrees of freedom.
176 177 178 |
# File 'lib/more_math/distributions.rb', line 176 def df @df end |
Instance Method Details
#inverse_probability(p) ⇒ Float
Returns the inverse cumulative probability (t-value) of the TDistribution for the probability p.
This finds the value x such that P(X <= x) = p where X ~ t(df).
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/more_math/distributions.rb', line 205 def inverse_probability(p) case when p <= 0 -1 / 0.0 # Negative infinity when p >= 1 1 / 0.0 # Positive infinity else begin bisect = NewtonBisection.new { |x| probability(x) - p } range = bisect.bracket(-10..10) bisect.solve(range, 1_000_000) rescue 0 / 0.0 # NaN on error end end end |
#probability(x) ⇒ Float
Returns the cumulative probability (p-value) of the TDistribution for the t-value x.
This calculates P(X <= x) where X ~ t(df).
185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/more_math/distributions.rb', line 185 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 |