tmath.nim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. discard """
  2. action: run
  3. output: '''[Suite] random int
  4. [Suite] random float
  5. [Suite] cumsum
  6. [Suite] random sample
  7. [Suite] ^
  8. '''
  9. """
  10. import math, random, os
  11. import unittest
  12. import sets, tables
  13. suite "random int":
  14. test "there might be some randomness":
  15. var set = initHashSet[int](128)
  16. for i in 1..1000:
  17. incl(set, rand(high(int)))
  18. check len(set) == 1000
  19. test "single number bounds work":
  20. var rand: int
  21. for i in 1..1000:
  22. rand = rand(1000)
  23. check rand < 1000
  24. check rand > -1
  25. test "slice bounds work":
  26. var rand: int
  27. for i in 1..1000:
  28. rand = rand(100..1000)
  29. check rand < 1000
  30. check rand >= 100
  31. test " again gives new numbers":
  32. var rand1 = rand(1000000)
  33. os.sleep(200)
  34. var rand2 = rand(1000000)
  35. check rand1 != rand2
  36. suite "random float":
  37. test "there might be some randomness":
  38. var set = initHashSet[float](128)
  39. for i in 1..100:
  40. incl(set, rand(1.0))
  41. check len(set) == 100
  42. test "single number bounds work":
  43. var rand: float
  44. for i in 1..1000:
  45. rand = rand(1000.0)
  46. check rand < 1000.0
  47. check rand > -1.0
  48. test "slice bounds work":
  49. var rand: float
  50. for i in 1..1000:
  51. rand = rand(100.0..1000.0)
  52. check rand < 1000.0
  53. check rand >= 100.0
  54. test " again gives new numbers":
  55. var rand1:float = rand(1000000.0)
  56. os.sleep(200)
  57. var rand2:float = rand(1000000.0)
  58. check rand1 != rand2
  59. suite "cumsum":
  60. test "cumsum int seq return":
  61. let counts = [ 1, 2, 3, 4 ]
  62. check counts.cumsummed == [ 1, 3, 6, 10 ]
  63. test "cumsum float seq return":
  64. let counts = [ 1.0, 2.0, 3.0, 4.0 ]
  65. check counts.cumsummed == [ 1.0, 3.0, 6.0, 10.0 ]
  66. test "cumsum int in-place":
  67. var counts = [ 1, 2, 3, 4 ]
  68. counts.cumsum
  69. check counts == [ 1, 3, 6, 10 ]
  70. test "cumsum float in-place":
  71. var counts = [ 1.0, 2.0, 3.0, 4.0 ]
  72. counts.cumsum
  73. check counts == [ 1.0, 3.0, 6.0, 10.0 ]
  74. suite "random sample":
  75. test "non-uniform array sample unnormalized int CDF":
  76. let values = [ 10, 20, 30, 40, 50 ] # values
  77. let counts = [ 4, 3, 2, 1, 0 ] # weights aka unnormalized probabilities
  78. var histo = initCountTable[int]()
  79. let cdf = counts.cumsummed # unnormalized CDF
  80. for i in 0 ..< 5000:
  81. histo.inc(sample(values, cdf))
  82. check histo.len == 4 # number of non-zero in `counts`
  83. # Any one bin is a binomial random var for n samples, each with prob p of
  84. # adding a count to k; E[k]=p*n, Var k=p*(1-p)*n, approximately Normal for
  85. # big n. So, P(abs(k - p*n)/sqrt(p*(1-p)*n))>3.0) =~ 0.0027, while
  86. # P(wholeTestFails) =~ 1 - P(binPasses)^4 =~ 1 - (1-0.0027)^4 =~ 0.01.
  87. for i, c in counts:
  88. if c == 0:
  89. check values[i] notin histo
  90. continue
  91. let p = float(c) / float(cdf[^1])
  92. let n = 5000.0
  93. let expected = p * n
  94. let stdDev = sqrt(n * p * (1.0 - p))
  95. check abs(float(histo[values[i]]) - expected) <= 3.0 * stdDev
  96. test "non-uniform array sample normalized float CDF":
  97. let values = [ 10, 20, 30, 40, 50 ] # values
  98. let counts = [ 0.4, 0.3, 0.2, 0.1, 0 ] # probabilities
  99. var histo = initCountTable[int]()
  100. let cdf = counts.cumsummed # normalized CDF
  101. for i in 0 ..< 5000:
  102. histo.inc(sample(values, cdf))
  103. check histo.len == 4 # number of non-zero in ``counts``
  104. for i, c in counts:
  105. if c == 0:
  106. check values[i] notin histo
  107. continue
  108. let p = float(c) / float(cdf[^1])
  109. let n = 5000.0
  110. let expected = p * n
  111. let stdDev = sqrt(n * p * (1.0 - p))
  112. # NOTE: like unnormalized int CDF test, P(wholeTestFails) =~ 0.01.
  113. check abs(float(histo[values[i]]) - expected) <= 3.0 * stdDev
  114. suite "^":
  115. test "compiles for valid types":
  116. check: compiles(5 ^ 2)
  117. check: compiles(5.5 ^ 2)
  118. check: compiles(5.5 ^ 2.int8)
  119. check: compiles(5.5 ^ 2.uint)
  120. check: compiles(5.5 ^ 2.uint8)
  121. check: not compiles(5.5 ^ 2.2)