|
| 1 | +// Copyright (C) MongoDB, Inc. 2025-present. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 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 | + yData := []float64{} |
| 19 | + |
| 20 | + for i := start1; i < stop1; i += step1 { |
| 21 | + xData = append(xData, float64(i)) |
| 22 | + } |
| 23 | + for j := start2; j < stop2; j += step2 { |
| 24 | + yData = append(yData, float64(j)) |
| 25 | + } |
| 26 | + |
| 27 | + x := mat.NewDense(len(xData), 1, xData) |
| 28 | + y := mat.NewDense(len(yData), 1, yData) |
| 29 | + |
| 30 | + return x, y |
| 31 | +} |
| 32 | + |
| 33 | +func TestEnergyStatistics(t *testing.T) { |
| 34 | + t.Run("similar distributions should have small e,t,h values ", func(t *testing.T) { |
| 35 | + x, y := createTestVectors(1, 100, 1, 1, 105, 1) |
| 36 | + e, tstat, h, _ := getEnergyStatistics(x, y) |
| 37 | + |
| 38 | + del := 1e-3 |
| 39 | + // Limit precision of comparison to 3 digits after the decimal. |
| 40 | + assert.InDelta(t, 0.160, e, del) // |0.160 - e| < 0.001 |
| 41 | + assert.InDelta(t, 8.136, tstat, del) |
| 42 | + assert.InDelta(t, 0.002, h, del) |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("different distributions should have large e,t,h values", func(t *testing.T) { |
| 46 | + x, y := createTestVectors(1, 100, 1, 10000, 13000, 14) |
| 47 | + e, tstat, h, _ := getEnergyStatistics(x, y) |
| 48 | + del := 1e-3 |
| 49 | + |
| 50 | + assert.InDelta(t, 21859.691, e, del) |
| 51 | + assert.InDelta(t, 1481794.709, tstat, del) |
| 52 | + assert.InDelta(t, 0.954, h, del) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("uni-variate distributions", func(t *testing.T) { |
| 56 | + x, y := createTestVectors(1, 300, 1, 1000, 5000, 10) |
| 57 | + e, tstat, h, _ := getEnergyStatistics(x, y) |
| 58 | + del := 1e-3 |
| 59 | + |
| 60 | + assert.InDelta(t, 4257.009, e, del) |
| 61 | + assert.InDelta(t, 728381.015, tstat, del) |
| 62 | + assert.InDelta(t, 0.748, h, del) |
| 63 | + }) |
| 64 | + |
| 65 | + t.Run("equal distributions should have all 0 values", func(t *testing.T) { |
| 66 | + x := mat.NewDense(10, 1, []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) |
| 67 | + y := mat.NewDense(1, 1, []float64{1}) |
| 68 | + |
| 69 | + e, tstat, h, _ := getEnergyStatistics(x, y) |
| 70 | + |
| 71 | + assert.Equal(t, 0.0, e) |
| 72 | + assert.Equal(t, 0.0, tstat) |
| 73 | + assert.Equal(t, 0.0, h) |
| 74 | + }) |
| 75 | + |
| 76 | + t.Run("energy stats returns errors on malformed input", func(t *testing.T) { |
| 77 | + x := mat.NewDense(2, 2, make([]float64, 4)) |
| 78 | + y := mat.NewDense(2, 3, make([]float64, 6)) |
| 79 | + |
| 80 | + _, _, _, err := getEnergyStatistics(x, y) |
| 81 | + assert.NotEqual(t, nil, err) |
| 82 | + assert.ErrorContains(t, err, "both inputs must have the same number of columns") |
| 83 | + |
| 84 | + x.Reset() |
| 85 | + y = &mat.Dense{} |
| 86 | + |
| 87 | + _, _, _, err = getEnergyStatistics(x, y) |
| 88 | + assert.NotEqual(t, nil, err) |
| 89 | + assert.ErrorContains(t, err, "inputs cannot be empty") |
| 90 | + |
| 91 | + x = mat.NewDense(2, 2, make([]float64, 4)) |
| 92 | + y = mat.NewDense(3, 2, make([]float64, 6)) |
| 93 | + |
| 94 | + _, _, _, err = getEnergyStatistics(x, y) |
| 95 | + assert.NotEqual(t, nil, err) |
| 96 | + assert.ErrorContains(t, err, "both inputs must be column vectors") |
| 97 | + }) |
| 98 | +} |
0 commit comments