tstats.nim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. include stats
  2. proc clean(x: float): float =
  3. result = round(1.0e8*x).float * 1.0e-8
  4. var rs: RunningStat
  5. rs.push(@[1.0, 2.0, 1.0, 4.0, 1.0, 4.0, 1.0, 2.0])
  6. doAssert(rs.n == 8)
  7. doAssert(clean(rs.mean) == 2.0)
  8. doAssert(clean(rs.variance()) == 1.5)
  9. doAssert(clean(rs.varianceS()) == 1.71428571)
  10. doAssert(clean(rs.skewness()) == 0.81649658)
  11. doAssert(clean(rs.skewnessS()) == 1.01835015)
  12. doAssert(clean(rs.kurtosis()) == -1.0)
  13. doAssert(clean(rs.kurtosisS()) == -0.7000000000000001)
  14. var rs1, rs2: RunningStat
  15. rs1.push(@[1.0, 2.0, 1.0, 4.0])
  16. rs2.push(@[1.0, 4.0, 1.0, 2.0])
  17. let rs3 = rs1 + rs2
  18. doAssert(clean(rs3.mom2) == clean(rs.mom2))
  19. doAssert(clean(rs3.mom3) == clean(rs.mom3))
  20. doAssert(clean(rs3.mom4) == clean(rs.mom4))
  21. rs1 += rs2
  22. doAssert(clean(rs1.mom2) == clean(rs.mom2))
  23. doAssert(clean(rs1.mom3) == clean(rs.mom3))
  24. doAssert(clean(rs1.mom4) == clean(rs.mom4))
  25. rs1.clear()
  26. rs1.push(@[1.0, 2.2, 1.4, 4.9])
  27. doAssert(rs1.sum == 9.5)
  28. doAssert(rs1.mean() == 2.375)
  29. when not defined(cpu32):
  30. # XXX For some reason on 32bit CPUs these results differ
  31. var rr: RunningRegress
  32. rr.push(@[0.0, 1.0, 2.8, 3.0, 4.0], @[0.0, 1.0, 2.3, 3.0, 4.0])
  33. doAssert(rr.slope() == 0.9695585996955861)
  34. doAssert(rr.intercept() == -0.03424657534246611)
  35. doAssert(rr.correlation() == 0.9905100362239381)
  36. var rr1, rr2: RunningRegress
  37. rr1.push(@[0.0, 1.0], @[0.0, 1.0])
  38. rr2.push(@[2.8, 3.0, 4.0], @[2.3, 3.0, 4.0])
  39. let rr3 = rr1 + rr2
  40. doAssert(rr3.correlation() == rr.correlation())
  41. doAssert(clean(rr3.slope()) == clean(rr.slope()))
  42. doAssert(clean(rr3.intercept()) == clean(rr.intercept()))