|
5 | 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
6 | 6 |
|
7 | 7 | package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "gonum.org/v1/gonum/mat" |
| 14 | +) |
| 15 | + |
| 16 | +func createTestVectors(start1 int, stop1 int, step1 int, start2 int, stop2 int, step2 int) (*mat.Dense, *mat.Dense) { |
| 17 | + xData := []float64{} |
| 18 | + i := start1 |
| 19 | + for i < stop1 { |
| 20 | + xData = append(xData, float64(i)) |
| 21 | + i += step1 |
| 22 | + } |
| 23 | + |
| 24 | + yData := []float64{} |
| 25 | + j := start2 |
| 26 | + for j < stop2 { |
| 27 | + yData = append(yData, float64(j)) |
| 28 | + j += step2 |
| 29 | + } |
| 30 | + |
| 31 | + x := mat.NewDense(len(xData), 1, xData) |
| 32 | + y := mat.NewDense(len(yData), 1, yData) |
| 33 | + |
| 34 | + return x, y |
| 35 | +} |
| 36 | + |
| 37 | +func TestEnergyStatistics(t *testing.T) { |
| 38 | + |
| 39 | + t.Run("similar distributions should have small e,t,h values ", func(t *testing.T) { |
| 40 | + x, y := createTestVectors(1, 100, 1, 1, 105, 1) |
| 41 | + e, tstat, h := getEnergyStatistics(x, y) |
| 42 | + |
| 43 | + del := 1e-3 |
| 44 | + |
| 45 | + assert.InDelta(t, 0.160, e, del) // |0.160 - e| < 1/100 |
| 46 | + assert.InDelta(t, 8.136, tstat, del) |
| 47 | + assert.InDelta(t, 0.002, h, del) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("different distributions should have large e,t,h values", func(t *testing.T) { |
| 51 | + x, y := createTestVectors(1, 100, 1, 10000, 13000, 14) |
| 52 | + e, tstat, h := getEnergyStatistics(x, y) |
| 53 | + del := 1e-3 |
| 54 | + |
| 55 | + assert.InDelta(t, 21859.691, e, del) |
| 56 | + assert.InDelta(t, 1481794.709, tstat, del) |
| 57 | + assert.InDelta(t, 0.954, h, del) |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("uni-variate distributions", func(t *testing.T) { |
| 61 | + x, y := createTestVectors(1, 300, 1, 1000, 5000, 10) |
| 62 | + e, tstat, h := getEnergyStatistics(x, y) |
| 63 | + del := 1e-3 |
| 64 | + |
| 65 | + assert.InDelta(t, 4257.009, e, del) |
| 66 | + assert.InDelta(t, 1481794.709, tstat, del) |
| 67 | + assert.InDelta(t, 0.954, h, del) |
| 68 | + }) |
| 69 | +} |
0 commit comments