-
Notifications
You must be signed in to change notification settings - Fork 0
/
lottery.jl
2242 lines (1843 loc) · 70.4 KB
/
lottery.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### A Pluto.jl notebook ###
# v0.19.27
#> [frontmatter]
#> title = "Hotspot Analysis"
#> date = "2023-08-24"
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
end
# ╔═╡ bd481c9c-352b-11ee-2340-1d554ffed22a
using XLSX
# ╔═╡ ea4f446e-2a32-4b33-b202-0ee906a1fc26
using DataFrames
# ╔═╡ a487c1a4-19ba-4ac0-b593-0bf3fc6fcf77
using Plots
# ╔═╡ 8cf48b45-64fd-477d-a569-0c5fd182c89b
using Pipe
# ╔═╡ a784788c-6b14-4cba-be69-ff386c6ea702
using Statistics
# ╔═╡ 20568aff-1720-4dd0-ba88-e75ee2d2b711
using Distributions
# ╔═╡ 0c539612-0de0-4f06-87a8-7fbe9d3136ea
using StatsPlots
# ╔═╡ 9fd349a9-4133-479c-ae75-d8335c038b37
using HypothesisTests
# ╔═╡ 2abd4d89-6c36-431f-95cc-b83889e2ef0b
using Glob
# ╔═╡ cb4b7e67-d3cd-4407-b310-94ab9b2cf527
using PlutoUI
# ╔═╡ d9f5fae6-867c-4c35-9b4a-feed1840bd24
using Dates
# ╔═╡ 47f7ffef-9f69-4a08-afb3-d541b429e1b1
using SHA
# ╔═╡ ca3e1383-a461-4c14-86c4-ab1ab9694aeb
using Formatting
# ╔═╡ 42a7a1cf-e636-4e34-8ca8-c3456a5bf54e
using StatsBase
# ╔═╡ 663877a2-f975-4bfa-a88f-977a0c3c9204
md"""
# Exhibit: Analysis of Randomness of Hot Spot Results
Neal Fultz, <[email protected]>
"""
# ╔═╡ 63de8a85-fcdd-4823-996b-1acaf9082ac9
md"""
## Preamble
"""
# ╔═╡ 19ac3edc-455b-48b4-aad5-3abc189de685
HTML("""
<style>
@media print {
.noprint {
display:none !important
}
body :has(#noprintempty:checked) pluto-cell.code_folded:has(pluto-output>div:empty) {
display:none !important
}
body :has(#noprintcode:checked) pluto-input {
display:none !important
}
body :has(#noprintmd:checked) pluto-cell :has(div.markdown) + pluto-input {
display:none !important
}
div.cm-editor div[aria-live="polite"] {
display:none !important
}
}
</style>
<div class=noprint>
<header> Extra print options</header>
<input type="checkbox" id="noprintempty" name="noprintempty" checked />
<label for="noprintempty">Hide empty output</label>
<input type="checkbox" id="noprintmd" name="noprintmd" checked />
<label for="noprintmd">Hide markdown code</label>
<input type="checkbox" id="noprintcode" name="noprintcode" />
<label for="noprintcode">Hide *all* code</label>
</div>
""")
# ╔═╡ 142a4519-3228-42b8-84bb-43b4c759c74b
md"""
Select a data file:
"""
# ╔═╡ 90a7d7d7-f973-462b-a46c-e7801bdf277d
@bind xlsx Select(glob("*.xlsx"))
# ╔═╡ 4cff819e-677b-4340-b40e-65c1172fe1c6
begin
checksum = open(xlsx) do f
bytes2hex(sha2_256(f))
end
md""
end
# ╔═╡ ca48fd62-8e6b-4d02-ba48-dd9ade8c1638
begin
df = DataFrame(XLSX.readtable(xlsx, 1))
rename!((x) -> replace(x, r"\s" => "_"), df)
N = nrow(df)
first, last = df.Draw_Date[1], df.Draw_Date[N]
# Fix types for specific draws
for j in 1:20
if eltype(df[!, 4+j]) == Any
df[!, 4+j] = parse.(Int, df[!, 4+j])
end
end
md""
end
# ╔═╡ 20b19473-c99e-4077-aa53-d100717da9d2
md"""
* using $xlsx
* SHA2-256: $(checksum)
* Analyzing $N draws from $first - $last
* Executed on $(Dates.today())
"""
# ╔═╡ f77d8762-ebe5-4552-adb6-86a92dc805e8
md"""
## Exploratory Data Analysis
"""
# ╔═╡ 60af1b15-d99e-43ff-8ad7-d16a5b376132
md"""
### Distribution of Winning Numbers over Spots
"""
# ╔═╡ 35d0869d-7eb0-492a-8ff8-6974a2ce2cb9
begin
m = zeros(Int, 20, 80)
for j in 1:20
for i in 1:nrow(df)
m[j,df[i,4+j]] += 1
end
end
md""
end
# ╔═╡ 16661133-822a-4241-967e-eaeae2de5850
heatmap(m, xaxis=("Win Number"), yaxis=("Spot"), c=[:yellow, :blue, :red])
# ╔═╡ 889951a9-a704-4f90-9a28-0b2a8f736291
begin
cov11 = 1 - mean( round(N*1/80 - 1.96*sqrt(1/80*79/80*N), RoundDown)
.<= m .<
round(N*1/80 + 1.96*sqrt(1/80*79/80*N), RoundUp)
)
cov11 = sprintf1("%6.3f%%", cov11*100)
md"RR of $cov11"
end
# ╔═╡ 4ea47b75-8ad9-463c-8548-f59a23e7d0cb
md"""
### Distribution of Winning Numbers
"""
# ╔═╡ 4e17b54b-1443-4934-99cb-a3dfe1846e40
begin
h = sum(m, dims=1)
bar(vec(h), legend=false, xaxis="Win Number", yaxis="Frequency", yformatter=:plain)
hline!([N*.25 - 1.96*sqrt(.25*.75*N)], linestyle=:dash, color=:red)
hline!([N*.25 + 1.96*sqrt(.25*.75*N)], linestyle=:dash, color=:red)
end
# ╔═╡ b8691296-659c-4920-bcd6-a05f8453f14c
begin
cov12 = 1-mean(
round(N*.25 - 1.96*sqrt(.25*.75*N), RoundDown)
.< h .<=
round(N*.25 + 1.96*sqrt(.25*.75*N), RoundUp)
)
cov12 = sprintf1("%6.3f%%", cov12*100)
md"RR of $cov12"
end
# ╔═╡ dbaeae8b-75b5-4f43-84a8-04dacaaebc13
md"""
### Statistical Summaries
"""
# ╔═╡ a7372374-d23f-479f-ab2f-e6d5b2a48a7b
summary_stat = @pipe df |>
select(_, :Draw_Date, r"Win_Num_") |>
stack(_, Not(:Draw_Date)) |>
transform(_, :variable => (x -> replace.(x, r"Win_Num_" => "Spot ")) => :Spot) |>
groupby(_, :Spot) |>
combine(_,
:value => length => :N,
:value => mean => :Mean,
:value => std => :SD,
:value => minimum => :Min,
:value => (x -> quantile(x, .25)) => :Q1,
:value => median => :Median,
:value => (x -> quantile(x, .75)) => :Q3,
:value => maximum => :Max,
)
# ╔═╡ 16e67dc3-ba02-4388-8613-e7c1bd1bc48c
md"""
#### Spot Set Mean QC
"""
# ╔═╡ 4d32ad01-a98c-4cab-b2ab-a73529ec3470
begin
ssmqc = @pipe df |>
transform(_, :Draw_Date => ((x) -> (eachindex(df[!, :Draw_Date]) .- 1) .÷ 100 ) => :idx) |>
groupby(_, :idx) |>
combine(_, 5:24 .=> mean) |>
rename((x) -> replace(x, r"Win_Num_|_mean" => ""), _) |>
stack(_, Not(:idx)) |>
transform(_, :variable => ((x) -> parse.(Int, x) .+ rand(length(x))./2 .- 1/2) => :spot )
cov21 = 1 - mean((40.5-4.526) .<= ssmqc[!,:value] .<= (40.5+4.526))
cov21 = sprintf1("%6.3f%%", cov21*100)
md"RR of $cov21"
end
# ╔═╡ 43866a08-d4b7-46e2-b3f8-6a3e9e38fd90
begin
scatter(ssmqc[!,:spot], ssmqc[!, :value], legend=false, xaxis="Spot", yaxis="Sample Mean")
hline!([40.5-4.526], linestyle=:dash, color=:red)
hline!([40.5+4.526], linestyle=:dash, color=:red)
end
# ╔═╡ eb9c437f-32a5-4ed2-a031-385e4c682cd1
md"""
#### Day Average QC
"""
# ╔═╡ 36bee5a7-b2ef-4189-986b-c0681a8c1948
begin
daqc = @pipe df |>
groupby(_, :Draw_Date) |>
combine(_, 5:24 .=> mean) |>
rename((x) -> replace(x, r"Win_Num_|_mean" => ""), _) |>
stack(_, Not(:Draw_Date)) |>
transform(_, :variable => ((x) -> parse.(Int, x)) => :spot )
md""
end
# ╔═╡ 3455fc6c-bbdb-4a53-ac87-65a0a737aee6
begin
daqc2 = @pipe df |>
select(_, :Draw_Date, :Draw_Nbr, r"Win_Num_") |>
stack(_, Not(:Draw_Date, :Draw_Nbr)) |>
groupby(_, [:Draw_Date, :Draw_Nbr]) |>
combine(_, :value => mean) |>
groupby(_, :Draw_Date) |>
combine(_, :value_mean => mean => :m,
:value_mean => length => :n,
:value_mean => (x -> std(x)/sqrt(length(x) - 1)) => :se) |>
transform(_, [:se, :n] => ( (se,n) -> se .* quantile.(TDist.(n .- 1), 1-.025) ) => :w) |>
transform(_, [:m, :w] => ((m, w) -> (@. m-w < 40.5 < m+w )) => :out)
cov22 = 1 - mean(daqc2.out)
cov22 = sprintf1("%6.3f%%", cov22*100)
md"RR of $cov22"
end
# ╔═╡ c037e2e5-11fb-4701-aea1-a6c8879bc4aa
begin
theme(:ggplot2)
@df filter( :out => identity, daqc2) sticks(:Draw_Date, :m .- :w,
fillrange=(:m .+ :w),
linecolor = 1,
xaxis="Date", yaxis="Day Average", plot_title="95% CI for Mean Winning Number"
)
@df filter( :out => !, daqc2) sticks!(:Draw_Date, :m .- :w,
fillrange=(:m .+ :w),
linecolor = 2
)
hline!([40.5], linecolor=:grey, linestyle=:dash)
@df daqc2 scatter!(:Draw_Date, :m, legend=false, #yerror=:w, legend
markercolor = 2 .- :out, markerstrokecolor=:auto,
markersize=1.7
)
end
# ╔═╡ 9da09e5f-0fc6-4676-af90-6159e1f47856
md"""
Points not covered:
"""
# ╔═╡ 9bb035f5-9d9c-4e10-b190-551ecee1bb39
filter( :out => x -> !x, daqc2)
# ╔═╡ 70752728-d0ac-4634-b5de-81e187eabbd9
md"""
### Trace Plots
"""
# ╔═╡ 510ac63c-ed6e-42a7-9e59-5b1190e3b45f
begin
s1 = df[1:50, r"Win_Num"] |> Matrix
s2 = df[500:549, r"Win_Num"] |> Matrix
raw_mat = df[!, Between(:Win_Num_1, :Win_Num_20)] |> Matrix
md""
end
# ╔═╡ 2bd18cf5-36fd-4b49-a352-4f234541a1a6
plot(s1 |> transpose |> vec, legend=false, xaxis="", yaxis="Draw", plot_title="Seq 1 Trace: Rounds 1-50")
# ╔═╡ b1f29364-87f2-4ecc-9ced-24fa20157e1b
plot(s2 |> transpose |> vec, legend=false, xaxis="", yaxis="Draw", plot_title="Seq 2 Trace: Rounds 500-549")
# ╔═╡ 6e79b91f-4060-4041-92b5-54c5a44a4529
begin
draw_mean = @pipe df |>
select(_, :Draw_Nbr, r"Win_Num_") |>
stack(_, Not(:Draw_Nbr)) |>
groupby(_, :Draw_Nbr) |>
combine(_, :value => mean => :m)
plot(draw_mean[!, :Draw_Nbr], draw_mean[!,:m], legend=false, xaxis="Draw ID Number", yaxis="Round Average",xformatter=:plain)
end
# ╔═╡ 0974071a-f86c-425e-ab07-77e4f22dd34b
begin
draw_day = @pipe df |>
select(_, :Draw_Date, r"Win_Num_") |>
stack(_, Not(:Draw_Date)) |>
groupby(_, :Draw_Date) |>
combine(_, :value => mean => :m)
md""
end
# ╔═╡ 9b6a89b7-2b64-4c16-8195-128bb1796c54
plot(draw_day[!, :Draw_Date], draw_day[!,:m], legend=false, xaxis="Day", yaxis="Daily Average")
# ╔═╡ 0d8c0915-e550-4782-b4a2-250918236e4f
md"""
### Lag Plot
"""
# ╔═╡ 5c673bc0-a3bc-4d2d-a4c1-e1d2f882321c
begin
s1lag = s1 |> Matrix |> transpose |> vec
scatter(s1lag[1:end-1], s1lag[2:end], legend=false, markeralpha=.6,
xaxis="Seq 1 Lag 1", yaxis="Seq 1", plottitle="Within-Round")
plot!(1:80, 1:80, color=:red, linestyle=:dash)
end
# ╔═╡ 2557b636-886d-4176-a193-2787084ff6a3
begin
s2lag = s2 |> Matrix |> transpose |> vec
scatter(s2lag[1:end-1], s2lag[2:end], legend=false, markeralpha=.6,
xaxis="Seq 2 Lag 1", yaxis="Seq 2", plottitle="Within-Round")
plot!(1:80, 1:80, color=:red, linestyle=:dash)
end
# ╔═╡ d9f5495c-8c9f-4c23-9a9d-20af77242a9f
begin
s1lagT = s1 |> Matrix |> vec
#s1lagT = df[1:1000, :Win_Num_1]
scatter(s1lagT[1:end-1], s1lagT[2:end], legend=false, markeralpha=.6,
xaxis="Seq 1 Lag 1", yaxis="Seq 1", plottitle="Between-Round")
plot!(1:80, 1:80, color=:red, linestyle=:dash)
end
# ╔═╡ 16abd8b9-30e4-4fb0-a39a-fe9b0821d984
begin
s2lagT = s2 |> Matrix |> vec
scatter(s2lagT[1:end-1], s2lagT[2:end], legend=false, markeralpha=.6,
xaxis="Seq 2 Lag 1", yaxis="Seq 2", plottitle="Between-Round")
plot!(1:80, 1:80, color=:red, linestyle=:dash)
end
# ╔═╡ d2d8edad-187d-434e-9c49-bb30cfa460ea
md"""
### ACF Plot
"""
# ╔═╡ a13cbb76-d2fd-47c7-a3d3-d17a40658c56
begin
plot(s1 |> transpose |> vec |> autocor,
line=:stem, ylimits=(-.1, .1), yaxis="ACF", legend=false, plot_title="Seq 1 (By Round)")
hline!([1.96/sqrt(1000)], color=:red, linestyle=:dash)
hline!([-1.96/sqrt(1000)], color=:red, linestyle=:dash)
end
# ╔═╡ 14083b10-a7e1-4ac3-941e-92eb1339629a
begin
plot(df[!,r"Win_Num"] |> Matrix |> transpose |> vec |> autocor,
line=:stem, ylimits=(-.01, .01), yaxis="ACF", legend=false, plot_title="All (By Round)")
hline!([1.96/sqrt(N*20)], color=:red, linestyle=:dash)
hline!([-1.96/sqrt(N*20)], color=:red, linestyle=:dash)
end
# ╔═╡ 0bc09e9d-7366-43f0-95f6-f639ab415ea9
begin
plot(s1 |> vec |> autocor,
line=:stem, ylimits=(-.1, .1), yaxis="ACF", legend=false, plot_title="Seq 1 (By Spot)")
hline!([1.96/sqrt(1000)], color=:red, linestyle=:dash)
hline!([-1.96/sqrt(1000)], color=:red, linestyle=:dash)
end
# ╔═╡ 309e6156-99a2-4591-802e-ff6c3f8eb621
begin
plot(df[!,r"Win_Num"] |> Matrix |> vec |> autocor,
line=:stem, ylimits=(-.01, .01), yaxis="ACF", legend=false, plot_title="All (By Spot)")
hline!([1.96/sqrt(N*20)], color=:red, linestyle=:dash)
hline!([-1.96/sqrt(N*20)], color=:red, linestyle=:dash)
end
# ╔═╡ b116f2e5-80a2-4495-882e-2f1e06074f63
md"""
## Formal Tests
"""
# ╔═╡ 5a1b8f71-9bbb-4169-b751-261d8727e9d4
md"""
### $\chi^2$ Goodness of Fit
"""
# ╔═╡ e925c3e9-9abe-4a22-84b6-4dab4780da6c
begin
dist = Binomial(N, 1.0/80)
p_spot_win = 0.0*m
for ix in CartesianIndices(m)
p_spot_win[ix] = pvalue(ChisqTest([m[ix], nrow(df) - m[ix]], [1/80, 79/80]))
end
md""
end
# ╔═╡ 1b6c6766-a3a0-4b39-9b2f-1d22be8f19d4
begin
long_p = stack(hcat(DataFrame(spot=1:20), DataFrame(p_spot_win, :auto)), Not(:spot))
long_p.Win_Num = parse.(Int, replace.(long_p.variable, "x" => ""))
md""
end
# ╔═╡ 6b7f00cb-1bb8-4345-a2aa-59a9a30b577a
begin
@df long_p scatter(
:Win_Num, :value, legend=false,
markercolor=[(p > .05 ? 1 : 2) for p in :value],
markeralpha=.6,
plot_title="1600 tests, Spot X Win Num",
xaxis="Win Number",
yaxis="p"
)
hline!([.05], linestyle=:dash, linecolor=:red)
end
# ╔═╡ 0531f93e-b28a-40ab-afa2-19d1f7f00b90
begin
cov311 = mean(long_p[!,:value] .<= .05)
cov311 = sprintf1("%6.3f%%", cov311*100)
md"""
RR of $cov311
"""
end
# ╔═╡ e6ebcbc1-4d91-4261-a9f6-4c94b939d6a3
begin
dist2 = Binomial(nrow(df), 1.0/4)
#p_num = cdf.(dist2, sum(m, dims=1))[1,:] |> two_side
p_round_win = zeros(80)
for i in enumerate(sum(m, dims=1))
i, s = i
p_round_win[i] = pvalue(ChisqTest([s, nrow(df) - s], [1/4, 3/4]))
end
scatter(1:80, p_round_win, legend=false,
markercolor=[(p > .05 ? 1 : 2) for p in p_round_win],
markeralpha=.6,
plot_title="80 tests, Win Num in Round",
xaxis="Win Number",
yaxis="p"
)
hline!([.05], linestyle=:dash, linecolor=:red)
end
# ╔═╡ 51663667-f34c-4195-aa61-0bef1267bfd1
begin
cov312 = mean(p_round_win .<= .05)
cov312 = sprintf1("%6.3f%%", cov312*100)
md"""
RR of $cov312
"""
end
# ╔═╡ f5526ada-f62f-4af8-ada6-81769eb755c8
begin
dist3 = DiscreteUniform(1,80)
gdf = @pipe df |>
transform(_, :Draw_Nbr => (x -> repeat(1:10, inner=length(x) ÷ 10)) => :i) |>
groupby(_, :i)
spot = []
p3 = []
for i in enumerate(gdf)
i, x = i
for j in 1:20
table = @pipe x |> groupby(_, names(x)[j+4]) |> combine(_, nrow)
p_val = pvalue(ChisqTest(table.nrow))
push!(spot, j)
push!(p3, p_val)
end
end
# p3 = DataFrame(spot=spot, value=p3)
scatter(spot, p3, legend=false,
markercolor=[(p > .05 ? 1 : 2) for p in p3],
markeralpha=.6,
plot_title="200 tests, Spots (10 chunks)",
xaxis="Spot",
yaxis="p"
)
hline!([.05], linestyle=:dash, linecolor=:red)
end
# ╔═╡ b236037e-b052-4189-89ec-d2c3f4cfa60e
begin
cov313 = mean(p3 .<= .05)
cov313 = sprintf1("%6.3f%%", cov313*100)
md"""
RR of $cov313
"""
end
# ╔═╡ ded1d93d-1a90-457d-88c5-bf46f91be1b1
md"""
#### KS Tests
"""
# ╔═╡ 1a49d4ef-dd82-4fd8-a52c-26062a8981b3
@doc ExactOneSampleKSTest
# ╔═╡ 2b5d4bf4-c762-4612-b3df-9326093019df
md"Spot-by-number"
# ╔═╡ 63980b7e-0e22-4a06-ac67-c2d1b2679960
ApproximateOneSampleKSTest(p_spot_win |> vec, Uniform())
# ╔═╡ a505a05c-64d0-4ecc-8271-6ebb14d3bba1
md"Win Number in Round"
# ╔═╡ c17713de-7798-44a5-8698-772c92db2c5c
ApproximateOneSampleKSTest(p_round_win |> vec, Uniform())
# ╔═╡ 58169563-26ef-4460-bf5b-546844ad6a6e
md"Spot (10 chunks)"
# ╔═╡ dab99337-f3dc-41f1-8e45-66450cd6363f
ApproximateOneSampleKSTest(Float64.(p3) |> vec, Uniform())
# ╔═╡ a2d38a3e-a7f1-4c24-920c-42afb4d6d929
md"""
### Wald Wolfowitz R Test
"""
# ╔═╡ 2e12980b-ab4b-4fc1-aea8-b34f23a69aec
function wwr_test2(raw_mat, K, T=true, threshold=40.5)
"""
Goes across draws. Eg K = 10 => Spot1[1:200], Spot1[201:400], ..., Spot2[1:200]
"""
T = T ? identity : transpose
wwr = []
slice = nothing
raw_mat = T(raw_mat) |> vec
for i in 1:(K*20):size(raw_mat)[1]
e = min(i + K*20-1, size(raw_mat)[1])
slice = raw_mat[i:e] .> threshold
push!(wwr, WaldWolfowitzTest(slice ) |> pvalue)
end
K, prod(size(slice)), length(wwr), mean(wwr .<= .05)
end
# ╔═╡ b9fb424b-462e-4fb5-9a91-8761c01cb3f9
function wwr_test(raw_mat, K, T=false, threshold=40.5)
"""
Split into groups of K draws. Walk either by row or by column.
"""
T = T ? identity : transpose
wwr = []
slice = nothing
for i in 1:K:size(raw_mat)[1]
e = min(i + K-1, size(raw_mat)[1])
slice = raw_mat[i:e,:] .> threshold
push!(wwr, WaldWolfowitzTest(slice |> Matrix |> T |> vec) |> pvalue)
end
K, prod(size(slice)), length(wwr), mean(wwr .<= .05)
end
# ╔═╡ d2cff83f-11f9-453d-9885-4328571204d6
md"""
#### Rejection Rates for WWR / Round First
"""
# ╔═╡ 4f14cc60-b424-4807-adcc-6ac47e1f9b5b
DataFrame([
wwr_test(raw_mat, 10, false)
wwr_test(raw_mat, 20, false)
wwr_test(raw_mat, 50, false)
wwr_test(raw_mat, 100, false)
wwr_test(raw_mat, 200, false)
], ["Rounds", "Draws", "Tests", "RR"])
# ╔═╡ 0b6c3b88-1497-49c7-8322-ff32b308b1e0
md"""
#### Rejection Rates for WWR / Spot First
"""
# ╔═╡ 83d784e5-4f9c-47c0-9125-968591a7cb0a
DataFrame([
wwr_test2(raw_mat, 10, true)
wwr_test2(raw_mat, 20, true)
wwr_test2(raw_mat, 50, true)
wwr_test2(raw_mat, 100, true)
wwr_test2(raw_mat, 200, true)
], ["Rounds", "Draws", "Tests", "RR"])
# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Formatting = "59287772-0a20-5a39-b81b-1366585eb4c0"
Glob = "c27321d9-0574-5035-807b-f59d2c89b15c"
HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5"
Pipe = "b98c9c47-44ae-5843-9183-064241ee97a0"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PlutoUI = "7f904dfe-b85e-4ff6-b463-dae2292396a8"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
XLSX = "fdbf4ff8-1666-58a4-91e7-1b58723a45e0"
[compat]
DataFrames = "~1.6.1"
Distributions = "~0.25.99"
Formatting = "~0.4.2"
Glob = "~1.3.1"
HypothesisTests = "~0.11.0"
Pipe = "~1.3.0"
Plots = "~1.38.17"
PlutoUI = "~0.7.52"
StatsBase = "~0.34.0"
StatsPlots = "~0.15.6"
XLSX = "~0.9.0"
"""
# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised
julia_version = "1.9.2"
manifest_format = "2.0"
project_hash = "d5cf012d06cc6cd95d5c8376c0f691ae8959046a"
[[deps.AbstractFFTs]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "d92ad398961a3ed262d8bf04a1a2b8340f915fef"
uuid = "621f4979-c628-5d54-868e-fcf4e3e8185c"
version = "1.5.0"
weakdeps = ["ChainRulesCore", "Test"]
[deps.AbstractFFTs.extensions]
AbstractFFTsChainRulesCoreExt = "ChainRulesCore"
AbstractFFTsTestExt = "Test"
[[deps.AbstractPlutoDingetjes]]
deps = ["Pkg"]
git-tree-sha1 = "91bd53c39b9cbfb5ef4b015e8b582d344532bd0a"
uuid = "6e696c72-6542-2067-7265-42206c756150"
version = "1.2.0"
[[deps.Adapt]]
deps = ["LinearAlgebra", "Requires"]
git-tree-sha1 = "76289dc51920fdc6e0013c872ba9551d54961c24"
uuid = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
version = "3.6.2"
weakdeps = ["StaticArrays"]
[deps.Adapt.extensions]
AdaptStaticArraysExt = "StaticArrays"
[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
version = "1.1.1"
[[deps.Arpack]]
deps = ["Arpack_jll", "Libdl", "LinearAlgebra", "Logging"]
git-tree-sha1 = "9b9b347613394885fd1c8c7729bfc60528faa436"
uuid = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97"
version = "0.5.4"
[[deps.Arpack_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "JLLWrappers", "Libdl", "OpenBLAS_jll", "Pkg"]
git-tree-sha1 = "5ba6c757e8feccf03a1554dfaf3e26b3cfc7fd5e"
uuid = "68821587-b530-5797-8361-c406ea357684"
version = "3.5.1+1"
[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
[[deps.AxisAlgorithms]]
deps = ["LinearAlgebra", "Random", "SparseArrays", "WoodburyMatrices"]
git-tree-sha1 = "66771c8d21c8ff5e3a93379480a2307ac36863f7"
uuid = "13072b0f-2c55-5437-9ae7-d433b7a33950"
version = "1.0.1"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
[[deps.BitFlags]]
git-tree-sha1 = "43b1a4a8f797c1cddadf60499a8a077d4af2cd2d"
uuid = "d1d4a3ce-64b1-5f1a-9ba4-7e7e69966f35"
version = "0.1.7"
[[deps.Bzip2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "19a35467a82e236ff51bc17a3a44b69ef35185a2"
uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0"
version = "1.0.8+0"
[[deps.Cairo_jll]]
deps = ["Artifacts", "Bzip2_jll", "CompilerSupportLibraries_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Pkg", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"]
git-tree-sha1 = "4b859a208b2397a7a623a03449e4636bdb17bcf2"
uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a"
version = "1.16.1+1"
[[deps.Calculus]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "f641eb0a4f00c343bbc32346e1217b86f3ce9dad"
uuid = "49dc2e85-a5d0-5ad3-a950-438e2897f1b9"
version = "0.5.1"
[[deps.ChainRulesCore]]
deps = ["Compat", "LinearAlgebra", "SparseArrays"]
git-tree-sha1 = "e30f2f4e20f7f186dc36529910beaedc60cfa644"
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
version = "1.16.0"
[[deps.Clustering]]
deps = ["Distances", "LinearAlgebra", "NearestNeighbors", "Printf", "Random", "SparseArrays", "Statistics", "StatsBase"]
git-tree-sha1 = "b86ac2c5543660d238957dbde5ac04520ae977a7"
uuid = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5"
version = "0.15.4"
[[deps.CodecZlib]]
deps = ["TranscodingStreams", "Zlib_jll"]
git-tree-sha1 = "02aa26a4cf76381be7f66e020a3eddeb27b0a092"
uuid = "944b1d66-785c-5afd-91f1-9de20f533193"
version = "0.7.2"
[[deps.ColorSchemes]]
deps = ["ColorTypes", "ColorVectorSpace", "Colors", "FixedPointNumbers", "PrecompileTools", "Random"]
git-tree-sha1 = "dd3000d954d483c1aad05fe1eb9e6a715c97013e"
uuid = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
version = "3.22.0"
[[deps.ColorTypes]]
deps = ["FixedPointNumbers", "Random"]
git-tree-sha1 = "eb7f0f8307f71fac7c606984ea5fb2817275d6e4"
uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
version = "0.11.4"
[[deps.ColorVectorSpace]]
deps = ["ColorTypes", "FixedPointNumbers", "LinearAlgebra", "Requires", "Statistics", "TensorCore"]
git-tree-sha1 = "a1f44953f2382ebb937d60dafbe2deea4bd23249"
uuid = "c3611d14-8923-5661-9e6a-0046d554d3a4"
version = "0.10.0"
weakdeps = ["SpecialFunctions"]
[deps.ColorVectorSpace.extensions]
SpecialFunctionsExt = "SpecialFunctions"
[[deps.Colors]]
deps = ["ColorTypes", "FixedPointNumbers", "Reexport"]
git-tree-sha1 = "fc08e5930ee9a4e03f84bfb5211cb54e7769758a"
uuid = "5ae59095-9a9b-59fe-a467-6f913c188581"
version = "0.12.10"
[[deps.Combinatorics]]
git-tree-sha1 = "08c8b6831dc00bfea825826be0bc8336fc369860"
uuid = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
version = "1.0.2"
[[deps.CommonSolve]]
git-tree-sha1 = "0eee5eb66b1cf62cd6ad1b460238e60e4b09400c"
uuid = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
version = "0.2.4"
[[deps.Compat]]
deps = ["UUIDs"]
git-tree-sha1 = "e460f044ca8b99be31d35fe54fc33a5c33dd8ed7"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.9.0"
weakdeps = ["Dates", "LinearAlgebra"]
[deps.Compat.extensions]
CompatLinearAlgebraExt = "LinearAlgebra"
[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.0.5+0"
[[deps.ConcurrentUtilities]]
deps = ["Serialization", "Sockets"]
git-tree-sha1 = "5372dbbf8f0bdb8c700db5367132925c0771ef7e"
uuid = "f0e56b4a-5159-44fe-b623-3e5288b988bb"
version = "2.2.1"
[[deps.ConstructionBase]]
deps = ["LinearAlgebra"]
git-tree-sha1 = "fe2838a593b5f776e1597e086dcd47560d94e816"
uuid = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
version = "1.5.3"
[deps.ConstructionBase.extensions]
ConstructionBaseIntervalSetsExt = "IntervalSets"
ConstructionBaseStaticArraysExt = "StaticArrays"
[deps.ConstructionBase.weakdeps]
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
[[deps.Contour]]
git-tree-sha1 = "d05d9e7b7aedff4e5b51a029dced05cfb6125781"
uuid = "d38c429a-6771-53c6-b99e-75d170b6e991"
version = "0.6.2"
[[deps.Crayons]]
git-tree-sha1 = "249fe38abf76d48563e2f4556bebd215aa317e15"
uuid = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
version = "4.1.1"
[[deps.DataAPI]]
git-tree-sha1 = "8da84edb865b0b5b0100c0666a9bc9a0b71c553c"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.15.0"
[[deps.DataFrames]]
deps = ["Compat", "DataAPI", "DataStructures", "Future", "InlineStrings", "InvertedIndices", "IteratorInterfaceExtensions", "LinearAlgebra", "Markdown", "Missings", "PooledArrays", "PrecompileTools", "PrettyTables", "Printf", "REPL", "Random", "Reexport", "SentinelArrays", "SortingAlgorithms", "Statistics", "TableTraits", "Tables", "Unicode"]
git-tree-sha1 = "04c738083f29f86e62c8afc341f0967d8717bdb8"
uuid = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
version = "1.6.1"
[[deps.DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "3dbd312d370723b6bb43ba9d02fc36abade4518d"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.15"
[[deps.DataValueInterfaces]]
git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6"
uuid = "e2d170a0-9d28-54be-80f0-106bbe20a464"
version = "1.0.0"
[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
[[deps.DelimitedFiles]]
deps = ["Mmap"]
git-tree-sha1 = "9e2f36d3c96a820c678f2f1f1782582fcf685bae"
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
version = "1.9.1"
[[deps.Distances]]
deps = ["LinearAlgebra", "Statistics", "StatsAPI"]
git-tree-sha1 = "b6def76ffad15143924a2199f72a5cd883a2e8a9"
uuid = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
version = "0.10.9"
weakdeps = ["SparseArrays"]
[deps.Distances.extensions]
DistancesSparseArraysExt = "SparseArrays"
[[deps.Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
[[deps.Distributions]]
deps = ["FillArrays", "LinearAlgebra", "PDMats", "Printf", "QuadGK", "Random", "SpecialFunctions", "Statistics", "StatsAPI", "StatsBase", "StatsFuns", "Test"]
git-tree-sha1 = "27a18994a5991b1d2e2af7833c4f8ecf9af6b9ea"
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
version = "0.25.99"
[deps.Distributions.extensions]
DistributionsChainRulesCoreExt = "ChainRulesCore"
DistributionsDensityInterfaceExt = "DensityInterface"
[deps.Distributions.weakdeps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
DensityInterface = "b429d917-457f-4dbc-8f4c-0cc954292b1d"
[[deps.DocStringExtensions]]
deps = ["LibGit2"]
git-tree-sha1 = "2fb1e02f2b635d0845df5d7c167fec4dd739b00d"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.9.3"
[[deps.Downloads]]
deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
version = "1.6.0"
[[deps.DualNumbers]]
deps = ["Calculus", "NaNMath", "SpecialFunctions"]
git-tree-sha1 = "5837a837389fccf076445fce071c8ddaea35a566"
uuid = "fa6b7ba4-c1ee-5f82-b5fc-ecf0adba8f74"
version = "0.6.8"
[[deps.ExceptionUnwrapping]]
deps = ["Test"]
git-tree-sha1 = "e90caa41f5a86296e014e148ee061bd6c3edec96"
uuid = "460bff9d-24e4-43bc-9d9f-a8973cb893f4"
version = "0.1.9"
[[deps.Expat_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "4558ab818dcceaab612d1bb8c19cee87eda2b83c"
uuid = "2e619515-83b5-522b-bb60-26c02a35a201"
version = "2.5.0+0"
[[deps.EzXML]]
deps = ["Printf", "XML2_jll"]
git-tree-sha1 = "0fa3b52a04a4e210aeb1626def9c90df3ae65268"
uuid = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
version = "1.1.0"
[[deps.FFMPEG]]
deps = ["FFMPEG_jll"]
git-tree-sha1 = "b57e3acbe22f8484b4b5ff66a7499717fe1a9cc8"
uuid = "c87230d0-a227-11e9-1b43-d7ebe4e7570a"
version = "0.4.1"
[[deps.FFMPEG_jll]]
deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "JLLWrappers", "LAME_jll", "Libdl", "Ogg_jll", "OpenSSL_jll", "Opus_jll", "PCRE2_jll", "Pkg", "Zlib_jll", "libaom_jll", "libass_jll", "libfdk_aac_jll", "libvorbis_jll", "x264_jll", "x265_jll"]