stat/distuv: fix call parameters for SuffStat
Also clean up residual prints.
MODIFY
distuv/exponential.go
distuv/exponential.go
@@ -71,9 +71,9 @@ func (Exponential) ExKurtosis() float64 {
// If weights is nil, then all the weights are 1.
// If weights is not nil, then the len(weights) must equal len(samples).
func (e *Exponential) Fit(samples, weights []float64) {
- suffStat := make([]float64, 1)
+ suffStat := make([]float64, e.NumSuffStat())
nSamples := e.SuffStat(samples, weights, suffStat)
- e.ConjugateUpdate(suffStat, nSamples, []float64{0})
+ e.ConjugateUpdate(suffStat, nSamples, make([]float64, e.NumSuffStat()))
}
// LogProb computes the natural logarithm of the value of the probability density function at x.
@@ -104,6 +104,7 @@ func (Exponential) NumParameters() int {
return 1
}
+// NumSuffStat returns the number of sufficient statistics for the distribution.
func (Exponential) NumSuffStat() int {
return 1
}
@@ -199,13 +200,13 @@ func (e Exponential) StdDev() float64 {
// of the samples.
//
// If weights is nil, the weights are assumed to be 1, otherwise panics if
-// len(samples) != len(weights). Panics if len(suffStat) != 1.
+// len(samples) != len(weights). Panics if len(suffStat) != NumSuffStat().
func (Exponential) SuffStat(samples, weights, suffStat []float64) (nSamples float64) {
if len(weights) != 0 && len(samples) != len(weights) {
panic(badLength)
}
- if len(suffStat) != 1 {
+ if len(suffStat) != (Exponential{}).NumSuffStat() {
panic(badSuffStat)
}

MODIFY
distuv/exponential_test.go
distuv/exponential_test.go
@@ -58,3 +58,14 @@ func TestExponentialScore(t *testing.T) {
testDerivParam(t, test)
}
}
+
+func TestExponentialFitPanic(t *testing.T) {
+ e := Exponential{Rate: 2}
+ defer func() {
+ r := recover()
+ if r != nil {
+ t.Errorf("unexpected panic for Fit call: %v", r)
+ }
+ }()
+ e.Fit(make([]float64, 10), nil)
+}

MODIFY
distuv/norm.go
distuv/norm.go
@@ -80,9 +80,9 @@ func (Normal) ExKurtosis() float64 {
// data samples x with relative weights w. If weights is nil, then all the weights
// are 1. If weights is not nil, then the len(weights) must equal len(samples).
func (n *Normal) Fit(samples, weights []float64) {
- suffStat := make([]float64, 1)
+ suffStat := make([]float64, n.NumSuffStat())
nSamples := n.SuffStat(samples, weights, suffStat)
- n.ConjugateUpdate(suffStat, nSamples, []float64{0, 0})
+ n.ConjugateUpdate(suffStat, nSamples, make([]float64, n.NumSuffStat()))
}
// LogProb computes the natural logarithm of the value of the probability density function at x.
@@ -110,6 +110,7 @@ func (Normal) NumParameters() int {
return 2
}
+// NumSuffStat returns the number of sufficient statistics for the distribution.
func (Normal) NumSuffStat() int {
return 2
}
@@ -187,13 +188,13 @@ func (n Normal) StdDev() float64 {
// and the standard deviation of the samples.
//
// If weights is nil, the weights are assumed to be 1, otherwise panics if
-// len(samples) != len(weights). Panics if len(suffStat) != 2.
+// len(samples) != len(weights). Panics if len(suffStat) != NumSuffStat().
func (Normal) SuffStat(samples, weights, suffStat []float64) (nSamples float64) {
lenSamp := len(samples)
if len(weights) != 0 && len(samples) != len(weights) {
panic(badLength)
}
- if len(suffStat) != 2 {
+ if len(suffStat) != (Normal{}).NumSuffStat() {
panic(badSuffStat)
}

MODIFY
distuv/norm_test.go
distuv/norm_test.go
package distuv
import (
- "fmt"
"math"
"testing"
@@ -142,13 +141,23 @@ func TestNormalQuantile(t *testing.T) {
}
for i, v := range p {
got := UnitNormal.Quantile(v)
- fmt.Println(math.Abs(got - ans[i]))
if !floats.EqualWithinAbsOrRel(got, ans[i], 1e-10, 1e-10) {
t.Errorf("Quantile mismatch. Case %d, want: %v, got: %v", i, ans[i], got)
}
}
}
+func TestNormFitPanic(t *testing.T) {
+ n := Normal{Mu: 0, Sigma: 1}
+ defer func() {
+ r := recover()
+ if r != nil {
+ t.Errorf("unexpected panic for Fit call: %v", r)
+ }
+ }()
+ n.Fit(make([]float64, 10), nil)
+}
+
func BenchmarkNormalQuantile(b *testing.B) {
n := Normal{Mu: 2, Sigma: 3.1}
ps := make([]float64, 1000) // ensure there are small values

stat/distuv: fix call parameters for SuffStat

Also clean up residual prints.