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

     1  // Copyright ©2021 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  
    11  // Logistic implements the Logistic distribution, a two-parameter distribution with support on the real axis.
    12  // Its cumulative distribution function is the logistic function.
    13  //
    14  // General form of probability density function for Logistic distribution is
    15  //
    16  //	E(x) / (s * (1 + E(x))^2)
    17  //	where E(x) = exp(-(x-μ)/s)
    18  //
    19  // For more information, see https://en.wikipedia.org/wiki/Logistic_distribution.
    20  type Logistic struct {
    21  	Mu float64 // Mean value
    22  	S  float64 // Scale parameter proportional to standard deviation
    23  }
    24  
    25  // CDF computes the value of the cumulative density function at x.
    26  func (l Logistic) CDF(x float64) float64 {
    27  	return 1 / (1 + math.Exp(-(x-l.Mu)/l.S))
    28  }
    29  
    30  // ExKurtosis returns the excess kurtosis of the distribution.
    31  func (l Logistic) ExKurtosis() float64 {
    32  	return 6.0 / 5.0
    33  }
    34  
    35  // LogProb computes the natural logarithm of the value of the probability
    36  // density function at x.
    37  func (l Logistic) LogProb(x float64) float64 {
    38  	return x - 2*math.Log(math.Exp(x)+1)
    39  }
    40  
    41  // Mean returns the mean of the probability distribution.
    42  func (l Logistic) Mean() float64 {
    43  	return l.Mu
    44  }
    45  
    46  // Mode returns the mode of the distribution.
    47  //
    48  // It is same as Mean for Logistic distribution.
    49  func (l Logistic) Mode() float64 {
    50  	return l.Mu
    51  }
    52  
    53  // Median returns the median of the distribution.
    54  //
    55  // It is same as Mean for Logistic distribution.
    56  func (l Logistic) Median() float64 {
    57  	return l.Mu
    58  }
    59  
    60  // NumParameters returns the number of parameters in the distribution.
    61  //
    62  // Always returns 2.
    63  func (l Logistic) NumParameters() int {
    64  	return 2
    65  }
    66  
    67  // Prob computes the value of the probability density function at x.
    68  func (l Logistic) Prob(x float64) float64 {
    69  	E := math.Exp(-(x - l.Mu) / l.S)
    70  	return E / (l.S * math.Pow(1+E, 2))
    71  }
    72  
    73  // Quantile returns the inverse of the cumulative distribution function.
    74  func (l Logistic) Quantile(p float64) float64 {
    75  	return l.Mu + l.S*math.Log(p/(1-p))
    76  }
    77  
    78  // Skewness returns the skewness of the distribution.
    79  //
    80  // Always 0 for Logistic distribution.
    81  func (l Logistic) Skewness() float64 {
    82  	return 0
    83  }
    84  
    85  // StdDev returns the standard deviation of the probability distribution.
    86  func (l Logistic) StdDev() float64 {
    87  	return l.S * math.Pi / sqrt3
    88  }
    89  
    90  // Survival returns the survival function (complementary CDF) at x.
    91  func (l Logistic) Survival(x float64) float64 {
    92  	return 1 - l.CDF(x)
    93  }
    94  
    95  // Variance returns the variance of the probability distribution.
    96  func (l Logistic) Variance() float64 {
    97  	return l.S * l.S * math.Pi * math.Pi / 3
    98  }