-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuniform.go
executable file
·84 lines (71 loc) · 1.69 KB
/
uniform.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package prob
import (
"math"
"math/rand"
)
//The Uniform Distribution is a continuous probability distribution
// with parameters Min and Max, with Max < Min.
//
// See: https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)
type Uniform struct {
Min float64 `json:"min"`
Max float64 `json:"max"`
}
func NewUniform(min float64, max float64) (Uniform, error) {
dist := Uniform{min, max}
if err := dist.Validate(); err != nil {
return dist, err
}
return dist, nil
}
func (dist Uniform) Validate() error {
if dist.Max <= dist.Min {
return InvalidParamsError{ "Max must be greater than Min." }
}
return nil
}
func (dist Uniform) Mean() float64 {
result := (dist.Min + dist.Max) / 2
return result
}
func (dist Uniform) Variance() float64 {
diff := dist.Max - dist.Min
result := diff * diff / 12
return result
}
func (dist Uniform) Skewness() float64 {
return 0.0
}
func (dist Uniform) Kurtosis() float64 {
return -6.0 / 5.0
}
func (dist Uniform) StdDev() float64 {
diff := dist.Max - dist.Min
result := math.Sqrt(diff * diff / 12)
return result
}
func (dist Uniform) RelStdDev() float64 {
result := (dist.Max - dist.Min) / (math.Sqrt(3) * (dist.Max + dist.Min))
return result
}
func (dist Uniform) Pdf(x float64) float64 {
if x < dist.Min || x > dist.Max {
return 0.0
}
result := 1 / (dist.Max - dist.Min)
return result
}
func (dist Uniform) Cdf(x float64) float64 {
if (x < dist.Min) {
return 0.0
}
if (x >= dist.Max) {
return 1.0
}
result := (x - dist.Min) / (dist.Max - dist.Min);
return result
}
func (dist Uniform) Random() float64 {
value := dist.Min + (rand.Float64() * (dist.Max - dist.Min))
return value
}