Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2602ac1

Browse files
authored
Add geometric distribution functions (#75)
1 parent 228f4fe commit 2602ac1

4 files changed

Lines changed: 154 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ func Trimean(input Float64Data) (float64, error) {}
129129
func VarP(input Float64Data) (sdev float64, err error) {}
130130
func VarS(input Float64Data) (sdev float64, err error) {}
131131
func Variance(input Float64Data) (sdev float64, err error) {}
132+
func ProbGeom(a int, b int, p float64) (prob float64, err error) {}
133+
func ExpGeom(p float64) (exp float64, err error) {}
134+
func VarGeom(p float64) (exp float64, err error) {}
132135

133136
type Coordinate struct {
134137
X, Y float64

examples/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,21 @@ func main() {
169169
e, _ = stats.Entropy([]float64{1.1, 2.2, 3.3})
170170
fmt.Println(e)
171171
// Output: 1.0114042647073518
172+
173+
p := 0.5
174+
begin := 1
175+
end := 2
176+
chance, _ := stats.ProbGeom(begin, end, p)
177+
fmt.Println(chance)
178+
// Output: 0.25
179+
180+
prob1 := 0.5
181+
exp, _ := stats.ExpGeom(prob1)
182+
fmt.Println(exp)
183+
// Output:
184+
185+
prob2:= 0.5
186+
vari, _ := stats.VarGeom(prob2)
187+
fmt.Println(vari)
188+
// Output: 2
172189
}

geometric_distribution.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package stats
2+
3+
import (
4+
"math"
5+
)
6+
7+
// ProbGeom generates the probability for a geometric random variable
8+
// with parameter p to achieve success in the interval of [a, b] trials
9+
// See https://en.wikipedia.org/wiki/Geometric_distribution for more information
10+
func ProbGeom(a int, b int, p float64) (prob float64, err error) {
11+
if (a > b) || (a < 1) {
12+
return math.NaN(), ErrBounds
13+
}
14+
15+
prob = 0
16+
q := 1 - p // probability of failure
17+
18+
for k := a + 1; k <= b; k++ {
19+
prob = prob + p*math.Pow(q, float64(k-1))
20+
}
21+
22+
return prob, nil
23+
}
24+
25+
// ProbGeom generates the expectation or average number of trials
26+
// for a geometric random variable with parameter p
27+
func ExpGeom(p float64) (exp float64, err error) {
28+
if (p > 1) || (p < 0) {
29+
return math.NaN(), ErrNegative
30+
}
31+
32+
return 1 / p, nil
33+
}
34+
35+
// ProbGeom generates the variance for number for a
36+
// geometric random variable with parameter p
37+
func VarGeom(p float64) (exp float64, err error) {
38+
if (p > 1) || (p < 0) {
39+
return math.NaN(), ErrNegative
40+
}
41+
return (1 - p) / math.Pow(p, 2), nil
42+
}

geometric_distribution_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package stats_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/montanaflynn/stats"
8+
)
9+
10+
func ExampleProbGeom() {
11+
p := 0.5
12+
a := 1
13+
b := 2
14+
chance, _ := stats.ProbGeom(a, b, p)
15+
fmt.Println(chance)
16+
// Output: 0.25
17+
}
18+
19+
func TestProbGeomLarge(t *testing.T) {
20+
p := 0.5
21+
a := 1
22+
b := 10000
23+
chance, _ := stats.ProbGeom(a, b, p)
24+
fmt.Println(chance)
25+
// Output: 1
26+
}
27+
28+
func TestErrBoundsProbGeom(t *testing.T) {
29+
p := 0.5
30+
a := -1
31+
b := 4
32+
chance, err := stats.ProbGeom(a, b, p)
33+
if err == nil {
34+
t.Errorf("Did not return an error when expected")
35+
}
36+
fmt.Println(chance)
37+
// Output: NaN
38+
}
39+
40+
func ExampleExpGeom() {
41+
p := 0.5
42+
exp, _ := stats.ExpGeom(p)
43+
fmt.Println(exp)
44+
// Output: 2
45+
}
46+
47+
func TestExpGeom(t *testing.T) {
48+
p := 0.5
49+
exp, err := stats.ExpGeom(p)
50+
if err != nil {
51+
t.Errorf("Returned an error when not expected")
52+
}
53+
fmt.Println(exp)
54+
// Output: 2
55+
}
56+
57+
func TestErrExpGeom(t *testing.T) {
58+
p := -1.0
59+
exp, err := stats.ExpGeom(p)
60+
if err == nil {
61+
t.Errorf("Expected Error")
62+
}
63+
fmt.Println(exp)
64+
// Output: NaN
65+
}
66+
67+
func ExampleVarGeom() {
68+
p := 0.5
69+
vari, _ := stats.VarGeom(p)
70+
fmt.Println(vari)
71+
// Output: 2
72+
}
73+
74+
func TestVarGeom(t *testing.T) {
75+
p := 0.25
76+
vari, err := stats.VarGeom(p)
77+
if err != nil {
78+
t.Errorf("Returned an error when not expected")
79+
}
80+
fmt.Println(vari)
81+
// Output: 12.0
82+
}
83+
84+
func TestErrVarGeom(t *testing.T) {
85+
p := -1.0
86+
vari, err := stats.VarGeom(p)
87+
if err == nil {
88+
t.Errorf("Expected Erorr")
89+
}
90+
fmt.Println(vari)
91+
// Output: NaN
92+
}

0 commit comments

Comments
 (0)