random_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2020 gorse Project Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package base
  15. import (
  16. "testing"
  17. "github.com/chewxy/math32"
  18. mapset "github.com/deckarep/golang-set/v2"
  19. "github.com/stretchr/testify/assert"
  20. "github.com/thoas/go-funk"
  21. )
  22. const randomEpsilon = 0.1
  23. func TestRandomGenerator_MakeNormalMatrix(t *testing.T) {
  24. rng := NewRandomGenerator(0)
  25. vec := rng.NormalMatrix(1, 1000, 1, 2)[0]
  26. assert.False(t, math32.Abs(mean(vec)-1) > randomEpsilon)
  27. assert.False(t, math32.Abs(stdDev(vec)-2) > randomEpsilon)
  28. }
  29. func TestRandomGenerator_MakeUniformMatrix(t *testing.T) {
  30. rng := NewRandomGenerator(0)
  31. vec := rng.UniformMatrix(1, 1000, 1, 2)[0]
  32. assert.False(t, funk.MinFloat32(vec) < 1)
  33. assert.False(t, funk.MaxFloat32(vec) > 2)
  34. }
  35. func TestRandomGenerator_Sample(t *testing.T) {
  36. excludeSet := mapset.NewSet(0, 1, 2, 3, 4)
  37. rng := NewRandomGenerator(0)
  38. for i := 1; i <= 10; i++ {
  39. sampled := rng.Sample(0, 10, i, excludeSet)
  40. for j := range sampled {
  41. assert.False(t, excludeSet.Contains(sampled[j]))
  42. }
  43. }
  44. }
  45. func TestRandomGenerator_SampleInt32(t *testing.T) {
  46. excludeSet := mapset.NewSet[int32](0, 1, 2, 3, 4)
  47. rng := NewRandomGenerator(0)
  48. for i := 1; i <= 10; i++ {
  49. sampled := rng.SampleInt32(0, 10, i, excludeSet)
  50. for j := range sampled {
  51. assert.False(t, excludeSet.Contains(sampled[j]))
  52. }
  53. }
  54. }
  55. // mean of a slice of 32-bit floats.
  56. func mean(x []float32) float32 {
  57. return funk.SumFloat32(x) / float32(len(x))
  58. }
  59. // stdDev returns the sample standard deviation.
  60. func stdDev(x []float32) float32 {
  61. _, variance := meanVariance(x)
  62. return math32.Sqrt(variance)
  63. }
  64. // meanVariance computes the sample mean and unbiased variance, where the mean and variance are
  65. //
  66. // \sum_i w_i * x_i / (sum_i w_i)
  67. // \sum_i w_i (x_i - mean)^2 / (sum_i w_i - 1)
  68. //
  69. // respectively.
  70. // If weights is nil then all of the weights are 1. If weights is not nil, then
  71. // len(x) must equal len(weights).
  72. // When weights sum to 1 or less, a biased variance estimator should be used.
  73. func meanVariance(x []float32) (m, variance float32) {
  74. // This uses the corrected two-pass algorithm (1.7), from "Algorithms for computing
  75. // the sample variance: Analysis and recommendations" by Chan, Tony F., Gene H. Golub,
  76. // and Randall J. LeVeque.
  77. // note that this will panic if the slice lengths do not match
  78. m = mean(x)
  79. var (
  80. ss float32
  81. compensation float32
  82. )
  83. for _, v := range x {
  84. d := v - m
  85. ss += d * d
  86. compensation += d
  87. }
  88. variance = (ss - compensation*compensation/float32(len(x))) / float32(len(x)-1)
  89. return
  90. }