github.com/jgbaldwinbrown/perf@v0.1.1/pkg/stats/tdist.go (about)

     1  // Copyright 2015 The Go 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 stats
     6  
     7  import "math"
     8  
     9  // A TDist is a Student's t-distribution with V degrees of freedom.
    10  type TDist struct {
    11  	V float64
    12  }
    13  
    14  func (t TDist) PDF(x float64) float64 {
    15  	return math.Exp(lgamma((t.V+1)/2)-lgamma(t.V/2)) /
    16  		math.Sqrt(t.V*math.Pi) * math.Pow(1+(x*x)/t.V, -(t.V+1)/2)
    17  }
    18  
    19  func (t TDist) CDF(x float64) float64 {
    20  	if x == 0 {
    21  		return 0.5
    22  	} else if x > 0 {
    23  		return 1 - 0.5*mathBetaInc(t.V/(t.V+x*x), t.V/2, 0.5)
    24  	} else if x < 0 {
    25  		return 1 - t.CDF(-x)
    26  	} else {
    27  		return math.NaN()
    28  	}
    29  }
    30  
    31  func (t TDist) Bounds() (float64, float64) {
    32  	return -4, 4
    33  }