Debian Codegraph @

dwtest/test_randomFunctions.praat

raw
1 # test_randomFunctions.praat
2 # djmw 20121211, 20191227
3
4
5 appendInfoLine: "test_randomFunctions.praat"
6
7 Erase all
8 Font size: 10
9 @test_randomBiniomial: 1/6, 100, 1, 1
10 @test_randomBiniomial: 1/10, 50, 2, 0
11 @test_randomGamma: 3, 0.02, 3
12 @test_randomGamma: 5, 0.02, 4
13 sleep: 2 ; some time to look at the drawing
14
15 appendInfoLine: "test_randomFunctions.praat OK"
16
17 procedure test_randomBiniomial: .p, .n, .ipicture, .info
18 appendInfoLine: tab$, "randomBinomial (", .p, ",", .n, ")"
19 .np = .n * .p
20 .numberOfRuns = 100000
21 if .info <> 0
22 appendInfoLine: tab$, tab$, .numberOfRuns, " runs with randomBinomial (", .p, ",", .n, ")"
23 appendInfoLine: tab$, tab$, "We expect values between 0 and ", .n, " with a mean near n * p = ", .np
24 appendInfoLine: tab$, tab$, "Bin 1 counts number of times randomBinomial return 0, bin 2 number of times randomBinomial returns 1, etc."
25 appendInfoLine: tab$, tab$, "The probability q of the values in bin i equals binomialP (p, i - 1 , n) - binomialP (p, i - 2 , n)"
26 appendInfoLine: tab$, tab$, "In bin i we expect numberOfRuns * q (=", .numberOfRuns, " * q values"
27 endif
28 .mat = Create simple Matrix: "m", .numberOfRuns, 1, "randomBinomial (.p, .n)"
29 Select outer viewport: 0, 6, (.ipicture -1) * 3, .ipicture * 3
30 .numberOfBins = 40
31 Draw distribution: 0, 0, 0, 0, 0, .numberOfBins, .numberOfBins, 0, 0, "yes"
32 One mark bottom: .np, "no", "yes", "yes", fixed$ (.np, 2)
33 Text top: "no", "randomBinomial (1/6, 100)"
34 .info$ = Picture info
35 .ymax = extractNumber (.info$, "Axis top:")
36 Text: 0.65 * .numberOfBins, "left", 0.75 * .ymax, "Half", "Top expected at x = " + fixed$ (.np, 2)
37 .table = Create TableOfReal: "t", 3, .n+1
38 for .i to .numberOfRuns
39 .nb = randomBinomial (.p, .n)
40 .val = Get value: 1, .nb+1
41 .val += 1
42 Set value: 1, .nb+1, .val
43 endfor
44
45 for .i from 2 to .numberOfBins
46 .val = Get value: 1, .i
47 .q = binomialP (.p, .i - 1 , .n) - binomialP (.p, .i - 2 , .n)
48 .expect = round (.numberOfRuns * .q)
49 .diff = .val - .expect
50 Set value: 2, .i, .expect
51 Set value: 3, .i, .diff
52 if .info <> 0
53 appendInfoLine: tab$, tab$, "bin = ", .i, ", diff = ", .diff, " (expected = ", .expect, " q = ", .q
54 endif
55 endfor
56
57 removeObject: .mat, .table
58 endproc
59
60 procedure test_randomGamma: .alpha, .beta, .ipicture
61 appendInfoLine: tab$, "randomGamma (", .alpha, ",", .beta, ")"
62 .nrow = 1000
63 .ncol = 100
64 .gamma = Create simple Matrix: "gamma", .nrow, .ncol, "randomGamma (.alpha, .beta)"
65 Select outer viewport: 0, 6, (.ipicture -1) * 3, .ipicture * 3
66 .numberOfBins = 100
67 .maximumValue = 750
68 Draw distribution: 0, 0, 0, 0, 0, .maximumValue, .numberOfBins, 0, 0, "yes"
69 .xOfMaximum = (.alpha - 1) / .beta
70 One mark bottom: .xOfMaximum, "yes", "yes", "yes", ""
71 .numberOfCells = .nrow * .ncol
72 Draw function: 0, .maximumValue, 1000, "(.maximumValue/.numberOfBins)*
73 ... .numberOfCells*exp(-lnGamma(.alpha))*.beta^.alpha *x^(.alpha-1)*exp(-.beta*x)"
74 .topText$ = "randomGamma (" + string$( .alpha) + ", " + string$ (.beta) + ")"
75 Text top: "no", .topText$
76 .info$ = Picture info
77 .ymax = extractNumber (.info$, "Axis top:")
78 Text: .maximumValue / 2, "left", 0.75 * .ymax, "Half", "Top expected at x = " + string$ (.xOfMaximum)
79 removeObject: .gamma
80 endproc
81