test_randomBinomial.praat 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # test_randomBinomial.praat
  2. # djmw 20121211
  3. printline randomBinomial test
  4. p =1/6
  5. n = 100
  6. np = n * p
  7. numberOfRuns = 100000
  8. printline 'tab$''numberOfRuns' runs with randomBinomial (p, n), where p = 'p' and n = 'n'
  9. printline 'tab$'We expect values between 0 and 'n' with a mean near n * p = 'np'
  10. printline 'tab$'Bin 1 counts number of times randomBinomial return 0, bin 2 number of times randomBinomial return 1, etc.
  11. printline 'tab$'The probability q of the values in bin i equals binomialP (p, i - 1 , n) - binomialP (p, i - 2 , n)
  12. printline 'tab$' In bin i we expect numberOfRuns * q (='numberOfRuns' * q) values'newline$'
  13. Create TableOfReal... t 3 n+1
  14. for i to numberOfRuns
  15. nb = randomBinomial (p, n)
  16. val = Get value... 1 nb+1
  17. val += 1
  18. Set value... 1 nb+1 val
  19. endfor
  20. for i from 2 to n+1
  21. val = Get value... 1 i
  22. q = binomialP (p, i - 1 , n) - binomialP (p, i - 2 , n)
  23. expect = round (numberOfRuns * q)
  24. diff = val - expect
  25. Set value... 2 i expect
  26. Set value... 3 i diff
  27. printline 'tab$'bin = 'i': diff = 'diff' (expected = 'expect', q = 'q:3')
  28. endfor
  29. Remove
  30. printline randomBinomial test OK