tstats.nim 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import std/[stats, assertions]
  2. import std/math
  3. func `~=`(x, y: float32): bool =
  4. math.almostEqual(x, y)
  5. template main() =
  6. var rs: RunningStat
  7. rs.push(@[1.0, 2.0, 1.0, 4.0, 1.0, 4.0, 1.0, 2.0])
  8. doAssert(rs.n == 8)
  9. doAssert rs.mean ~= 2.0
  10. doAssert rs.variance() ~= 1.5
  11. doAssert rs.varianceS() ~= 1.71428571
  12. doAssert rs.skewness() ~= 0.81649658
  13. doAssert rs.skewnessS() ~= 1.01835015
  14. doAssert rs.kurtosis() ~= -1.0
  15. doAssert rs.kurtosisS() ~= -0.7000000000000001
  16. var rs1, rs2: RunningStat
  17. rs1.push(@[1.0, 2.0, 1.0, 4.0])
  18. rs2.push(@[1.0, 4.0, 1.0, 2.0])
  19. let rs3 = rs1 + rs2
  20. doAssert rs3.variance ~= rs.variance
  21. doAssert rs3.skewness ~= rs.skewness
  22. doAssert rs3.kurtosis ~= rs.kurtosis
  23. rs1 += rs2
  24. doAssert rs1.variance ~= rs.variance
  25. doAssert rs1.skewness ~= rs.skewness
  26. doAssert rs1.kurtosis ~= rs.kurtosis
  27. rs1.clear()
  28. rs1.push(@[1.0, 2.2, 1.4, 4.9])
  29. doAssert rs1.sum ~= 9.5
  30. doAssert rs1.mean() ~= 2.375
  31. when not defined(cpu32):
  32. # XXX For some reason on 32bit CPUs these results differ
  33. var rr: RunningRegress
  34. rr.push(@[0.0, 1.0, 2.8, 3.0, 4.0], @[0.0, 1.0, 2.3, 3.0, 4.0])
  35. doAssert rr.slope() ~= 0.9695585996955861
  36. doAssert rr.intercept() ~= -0.03424657534246611
  37. doAssert rr.correlation() ~= 0.9905100362239381
  38. var rr1, rr2: RunningRegress
  39. rr1.push(@[0.0, 1.0], @[0.0, 1.0])
  40. rr2.push(@[2.8, 3.0, 4.0], @[2.3, 3.0, 4.0])
  41. let rr3 = rr1 + rr2
  42. doAssert rr3.correlation() ~= rr.correlation()
  43. doAssert rr3.slope() ~= rr.slope()
  44. doAssert rr3.intercept() ~= rr.intercept()
  45. block: # bug #18718
  46. var rs: RunningStat
  47. rs.push(-1.0)
  48. doAssert rs.max == -1.0
  49. static: main()
  50. main()