gonum.org/v1/gonum@v0.14.0/stat/distuv/chisquared.go (about)

     1  // Copyright ©2016 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package distuv
     6  
     7  import (
     8  	"math"
     9  
    10  	"golang.org/x/exp/rand"
    11  
    12  	"gonum.org/v1/gonum/mathext"
    13  )
    14  
    15  // ChiSquared implements the χ² distribution, a one parameter distribution
    16  // with support on the positive numbers.
    17  //
    18  // The density function is given by
    19  //
    20  //	1/(2^{k/2} * Γ(k/2)) * x^{k/2 - 1} * e^{-x/2}
    21  //
    22  // It is a special case of the Gamma distribution, Γ(k/2, 1/2).
    23  //
    24  // For more information, see https://en.wikipedia.org/wiki/Chi-squared_distribution.
    25  type ChiSquared struct {
    26  	// K is the shape parameter, corresponding to the degrees of freedom. Must
    27  	// be greater than 0.
    28  	K float64
    29  
    30  	Src rand.Source
    31  }
    32  
    33  // CDF computes the value of the cumulative density function at x.
    34  func (c ChiSquared) CDF(x float64) float64 {
    35  	return mathext.GammaIncReg(c.K/2, x/2)
    36  }
    37  
    38  // ExKurtosis returns the excess kurtosis of the distribution.
    39  func (c ChiSquared) ExKurtosis() float64 {
    40  	return 12 / c.K
    41  }
    42  
    43  // LogProb computes the natural logarithm of the value of the probability
    44  // density function at x.
    45  func (c ChiSquared) LogProb(x float64) float64 {
    46  	if x < 0 {
    47  		return math.Inf(-1)
    48  	}
    49  	lg, _ := math.Lgamma(c.K / 2)
    50  	return (c.K/2-1)*math.Log(x) - x/2 - (c.K/2)*math.Ln2 - lg
    51  }
    52  
    53  // Mean returns the mean of the probability distribution.
    54  func (c ChiSquared) Mean() float64 {
    55  	return c.K
    56  }
    57  
    58  // Mode returns the mode of the distribution.
    59  func (c ChiSquared) Mode() float64 {
    60  	return math.Max(c.K-2, 0)
    61  }
    62  
    63  // NumParameters returns the number of parameters in the distribution.
    64  func (c ChiSquared) NumParameters() int {
    65  	return 1
    66  }
    67  
    68  // Prob computes the value of the probability density function at x.
    69  func (c ChiSquared) Prob(x float64) float64 {
    70  	return math.Exp(c.LogProb(x))
    71  }
    72  
    73  // Rand returns a random sample drawn from the distribution.
    74  func (c ChiSquared) Rand() float64 {
    75  	return Gamma{c.K / 2, 0.5, c.Src}.Rand()
    76  }
    77  
    78  // Quantile returns the inverse of the cumulative distribution function.
    79  func (c ChiSquared) Quantile(p float64) float64 {
    80  	if p < 0 || p > 1 {
    81  		panic(badPercentile)
    82  	}
    83  	return mathext.GammaIncRegInv(0.5*c.K, p) * 2
    84  }
    85  
    86  // StdDev returns the standard deviation of the probability distribution.
    87  func (c ChiSquared) StdDev() float64 {
    88  	return math.Sqrt(c.Variance())
    89  }
    90  
    91  // Survival returns the survival function (complementary CDF) at x.
    92  func (c ChiSquared) Survival(x float64) float64 {
    93  	if x < 0 {
    94  		return 1
    95  	}
    96  	return mathext.GammaIncRegComp(0.5*c.K, 0.5*x)
    97  }
    98  
    99  // Variance returns the variance of the probability distribution.
   100  func (c ChiSquared) Variance() float64 {
   101  	return 2 * c.K
   102  }