stat/distuv: full test coverage for Triangle distribution
MODIFY
stat/distuv/triangle_test.go
stat/distuv/triangle_test.go
@@ -14,16 +14,16 @@ import (
func TestTriangleConstraint(t *testing.T) {
t.Parallel()
- defer func() {
- if r := recover(); r == nil {
- t.Errorf("The constraints were violated, but not caught")
+ for _, test := range []struct{ a, b, c float64 }{
+ {a: 1, b: 1, c: 1},
+ {a: 1, b: 1, c: 0},
+ {a: 1, b: 2, c: 3},
+ {a: 1, b: 2, c: 0},
+ } {
+ if !panics(func() { NewTriangle(test.a, test.b, test.c, nil) }) {
+ t.Errorf("expected panic for NewTriangle(%f, %f, %f, nil)", test.a, test.b, test.c)
+ }
}
- }()
-
- // test b < a
- NewTriangle(3, 1, 2, nil)
- // test c > b
- NewTriangle(1, 2, 3, nil)
}
func TestTriangle(t *testing.T) {
@@ -110,6 +110,12 @@ func TestTriangleProb(t *testing.T) {
cumProb: 1,
logProb: math.Inf(-1),
},
+ {
+ loc: 3.5,
+ prob: 0,
+ cumProb: 1,
+ logProb: math.Inf(-1),
+ },
}
testDistributionProbs(t, NewTriangle(1, 3, 2, nil), "Standard 1,2,3 Triangle", pts)
}
@@ -141,7 +147,7 @@ func TestTriangleScore(t *testing.T) {
f = Triangle{a: 0, b: 1, c: 1}
score = f.Score(nil, x)
if !math.IsNaN(score[1]) {
- t.Errorf("Expected score over B to be NaN for B == C, got %v", score[0])
+ t.Errorf("Expected score over B to be NaN for B == C, got %v", score[1])
}
if !math.IsNaN(score[2]) {
t.Errorf("Expected score over C to be NaN for B == C, got %v", score[2])
@@ -150,6 +156,42 @@ func TestTriangleScore(t *testing.T) {
if math.Abs(expectedScore-score[0]) > tol {
t.Errorf("Mismatch in score over A for B == C: want %g, got %v", expectedScore, score[0])
}
+
+ f = Triangle{a: 0, b: 1, c: 0.5}
+ score = f.Score(nil, f.a-0.01)
+ if !math.IsNaN(score[0]) {
+ t.Errorf("Expected score over B to be NaN for x < A, got %v", score[0])
+ }
+ if !math.IsNaN(score[1]) {
+ t.Errorf("Expected score over B to be NaN for x < A, got %v", score[1])
+ }
+ if !math.IsNaN(score[2]) {
+ t.Errorf("Expected score over C to be NaN for x < A, got %v", score[2])
+ }
+
+ score = f.Score(nil, f.b+0.01)
+ if !math.IsNaN(score[0]) {
+ t.Errorf("Expected score over B to be NaN for x > B, got %v", score[0])
+ }
+ if !math.IsNaN(score[1]) {
+ t.Errorf("Expected score over B to be NaN for x > B, got %v", score[1])
+ }
+ if !math.IsNaN(score[2]) {
+ t.Errorf("Expected score over C to be NaN for x > B, got %v", score[2])
+ }
+
+ score = f.Score(nil, f.a)
+ if !math.IsNaN(score[0]) {
+ t.Errorf("Expected score over C to be NaN for x == A, got %v", score[0])
+ }
+ score = f.Score(nil, f.b)
+ if !math.IsNaN(score[1]) {
+ t.Errorf("Expected score over C to be NaN for x == B, got %v", score[1])
+ }
+ score = f.Score(nil, f.c)
+ if !math.IsNaN(score[2]) {
+ t.Errorf("Expected score over C to be NaN for x == C, got %v", score[2])
+ }
}
func logProbDerivative(t Triangle, x float64, i int, h float64) float64 {

stat/distuv: full test coverage for Triangle distribution