-
Notifications
You must be signed in to change notification settings - Fork 16
/
student_t.go
37 lines (32 loc) · 1.2 KB
/
student_t.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package rng
import (
"fmt"
"math"
)
// StudentTGenerator is a random number generator for student-t distribution.
// The zero value is invalid, use NewStudentTGenerator to create a generator
type StudentTGenerator struct {
gaussian *GaussianGenerator
chisquared *ChiSquaredGenerator
}
// NewStudentTGenerator returns a student-t distribution generator
// it is recommended using time.Now().UnixNano() as the seed, for example:
// stng := rng.NewStudentTGenerator(time.Now().UnixNano())
func NewStudentTGenerator(seed int64) *StudentTGenerator {
// make sure they have different random seeds
grng := NewGaussianGenerator(seed)
crng := NewChiSquaredGenerator(2*seed + 1)
return &StudentTGenerator{grng, crng}
}
// Student returns a random number of student-t distribution (freedom > 0.0)
func (stng StudentTGenerator) Student(freedom int64) float64 {
if !(freedom > 0) {
panic(fmt.Sprintf("Invalid parameter freedom: %d (freedom > 0)", freedom))
}
return stng.student(freedom)
}
// inspired by http://www.xycoon.com/stt_random.htm
func (stng StudentTGenerator) student(freedom int64) float64 {
X := stng.gaussian.StdGaussian() / math.Sqrt(stng.chisquared.ChiSquared(freedom)/float64(freedom))
return X
}