gonum.org/v1/gonum@v0.14.0/stat/distuv/beta.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  // Beta implements the Beta distribution, a two-parameter continuous distribution
    16  // with support between 0 and 1.
    17  //
    18  // The beta distribution has density function
    19  //
    20  //	x^(α-1) * (1-x)^(β-1) * Γ(α+β) / (Γ(α)*Γ(β))
    21  //
    22  // For more information, see https://en.wikipedia.org/wiki/Beta_distribution
    23  type Beta struct {
    24  	// Alpha is the left shape parameter of the distribution. Alpha must be greater
    25  	// than 0.
    26  	Alpha float64
    27  	// Beta is the right shape parameter of the distribution. Beta must be greater
    28  	// than 0.
    29  	Beta float64
    30  
    31  	Src rand.Source
    32  }
    33  
    34  // CDF computes the value of the cumulative distribution function at x.
    35  func (b Beta) CDF(x float64) float64 {
    36  	if x <= 0 {
    37  		return 0
    38  	}
    39  	if x >= 1 {
    40  		return 1
    41  	}
    42  	return mathext.RegIncBeta(b.Alpha, b.Beta, x)
    43  }
    44  
    45  // Entropy returns the differential entropy of the distribution.
    46  func (b Beta) Entropy() float64 {
    47  	if b.Alpha <= 0 || b.Beta <= 0 {
    48  		panic("beta: negative parameters")
    49  	}
    50  	return mathext.Lbeta(b.Alpha, b.Beta) - (b.Alpha-1)*mathext.Digamma(b.Alpha) -
    51  		(b.Beta-1)*mathext.Digamma(b.Beta) + (b.Alpha+b.Beta-2)*mathext.Digamma(b.Alpha+b.Beta)
    52  }
    53  
    54  // ExKurtosis returns the excess kurtosis of the distribution.
    55  func (b Beta) ExKurtosis() float64 {
    56  	num := 6 * ((b.Alpha-b.Beta)*(b.Alpha-b.Beta)*(b.Alpha+b.Beta+1) - b.Alpha*b.Beta*(b.Alpha+b.Beta+2))
    57  	den := b.Alpha * b.Beta * (b.Alpha + b.Beta + 2) * (b.Alpha + b.Beta + 3)
    58  	return num / den
    59  }
    60  
    61  // LogProb computes the natural logarithm of the value of the probability
    62  // density function at x.
    63  func (b Beta) LogProb(x float64) float64 {
    64  	if x < 0 || x > 1 {
    65  		return math.Inf(-1)
    66  	}
    67  
    68  	if b.Alpha <= 0 || b.Beta <= 0 {
    69  		panic("beta: negative parameters")
    70  	}
    71  
    72  	lab, _ := math.Lgamma(b.Alpha + b.Beta)
    73  	la, _ := math.Lgamma(b.Alpha)
    74  	lb, _ := math.Lgamma(b.Beta)
    75  	var lx float64
    76  	if b.Alpha != 1 {
    77  		lx = (b.Alpha - 1) * math.Log(x)
    78  	}
    79  	var l1mx float64
    80  	if b.Beta != 1 {
    81  		l1mx = (b.Beta - 1) * math.Log(1-x)
    82  	}
    83  	return lab - la - lb + lx + l1mx
    84  }
    85  
    86  // Mean returns the mean of the probability distribution.
    87  func (b Beta) Mean() float64 {
    88  	return b.Alpha / (b.Alpha + b.Beta)
    89  }
    90  
    91  // Mode returns the mode of the distribution.
    92  //
    93  // Mode returns NaN if both parameters are less than or equal to 1 as a special case,
    94  // 0 if only Alpha <= 1 and 1 if only Beta <= 1.
    95  func (b Beta) Mode() float64 {
    96  	if b.Alpha <= 1 {
    97  		if b.Beta <= 1 {
    98  			return math.NaN()
    99  		}
   100  		return 0
   101  	}
   102  	if b.Beta <= 1 {
   103  		return 1
   104  	}
   105  	return (b.Alpha - 1) / (b.Alpha + b.Beta - 2)
   106  }
   107  
   108  // NumParameters returns the number of parameters in the distribution.
   109  func (b Beta) NumParameters() int {
   110  	return 2
   111  }
   112  
   113  // Prob computes the value of the probability density function at x.
   114  func (b Beta) Prob(x float64) float64 {
   115  	return math.Exp(b.LogProb(x))
   116  }
   117  
   118  // Quantile returns the inverse of the cumulative distribution function.
   119  func (b Beta) Quantile(p float64) float64 {
   120  	if p < 0 || p > 1 {
   121  		panic(badPercentile)
   122  	}
   123  	return mathext.InvRegIncBeta(b.Alpha, b.Beta, p)
   124  }
   125  
   126  // Rand returns a random sample drawn from the distribution.
   127  func (b Beta) Rand() float64 {
   128  	ga := Gamma{Alpha: b.Alpha, Beta: 1, Src: b.Src}.Rand()
   129  	gb := Gamma{Alpha: b.Beta, Beta: 1, Src: b.Src}.Rand()
   130  	return ga / (ga + gb)
   131  }
   132  
   133  // StdDev returns the standard deviation of the probability distribution.
   134  func (b Beta) StdDev() float64 {
   135  	return math.Sqrt(b.Variance())
   136  }
   137  
   138  // Survival returns the survival function (complementary CDF) at x.
   139  func (b Beta) Survival(x float64) float64 {
   140  	switch {
   141  	case x <= 0:
   142  		return 1
   143  	case x >= 1:
   144  		return 0
   145  	}
   146  	return mathext.RegIncBeta(b.Beta, b.Alpha, 1-x)
   147  }
   148  
   149  // Variance returns the variance of the probability distribution.
   150  func (b Beta) Variance() float64 {
   151  	return b.Alpha * b.Beta / ((b.Alpha + b.Beta) * (b.Alpha + b.Beta) * (b.Alpha + b.Beta + 1))
   152  }