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