-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demonstrations_of_Single-Player_Simulations_for_Non-Stationary-Bandits.html
4509 lines (3923 loc) · 265 KB
/
Demonstrations_of_Single-Player_Simulations_for_Non-Stationary-Bandits.html
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
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-38514290-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Table of Contents — SMPyBandits 0.9.6 documentation</title>
<script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/language_data.js"></script>
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})</script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="A note on execution times, speed and profiling" href="../Profiling.html" />
<link rel="prev" title="Table of Contents" href="Experiments_of_statistical_tests_for_piecewise_stationary_bandit.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> SMPyBandits
<img src="../_static/logo.png" class="logo" alt="Logo"/>
</a>
<div class="version">
0.9
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../README.html"><em>SMPyBandits</em></a></li>
<li class="toctree-l1"><a class="reference internal" href="../docs/modules.html">SMPyBandits modules</a></li>
<li class="toctree-l1"><a class="reference internal" href="../How_to_run_the_code.html">How to run the code ?</a></li>
<li class="toctree-l1"><a class="reference internal" href="../PublicationsWithSMPyBandits.html">List of research publications using Lilian Besson’s SMPyBandits project</a></li>
<li class="toctree-l1"><a class="reference internal" href="../Aggregation.html"><strong>Policy aggregation algorithms</strong></a></li>
<li class="toctree-l1"><a class="reference internal" href="../MultiPlayers.html"><strong>Multi-players simulation environment</strong></a></li>
<li class="toctree-l1"><a class="reference internal" href="../DoublingTrick.html"><strong>Doubling Trick for Multi-Armed Bandits</strong></a></li>
<li class="toctree-l1"><a class="reference internal" href="../SparseBandits.html"><strong>Structure and Sparsity of Stochastic Multi-Armed Bandits</strong></a></li>
<li class="toctree-l1"><a class="reference internal" href="../NonStationaryBandits.html"><strong>Non-Stationary Stochastic Multi-Armed Bandits</strong></a></li>
<li class="toctree-l1"><a class="reference internal" href="../API.html">Short documentation of the API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../About_parallel_computations.html">About parallel computations</a></li>
<li class="toctree-l1"><a class="reference internal" href="../TODO.html">💥 TODO</a></li>
<li class="toctree-l1"><a class="reference internal" href="../plots/README.html">Some illustrations for this project</a></li>
<li class="toctree-l1"><a class="reference internal" href="README.html">Jupyter Notebooks 📓 by Naereen @ GitHub</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="list.html">List of notebooks for SMPyBandits</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="Easily_creating_MAB_problems.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="Easily_creating_MAB_problems.html#Easily-creating-MAB-problems">Easily creating MAB problems</a></li>
<li class="toctree-l2"><a class="reference internal" href="Do_we_even_need_UCB.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="Do_we_even_need_UCB.html#Do-we-even-need-a-smart-learning-algorithm?-Is-UCB-useless?"><em>Do we even need a smart learning algorithm? Is UCB useless?</em></a></li>
<li class="toctree-l2"><a class="reference internal" href="Example_of_a_small_Single-Player_Simulation.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="Example_of_a_small_Single-Player_Simulation.html#An-example-of-a-small-Single-Player-simulation">An example of a small Single-Player simulation</a></li>
<li class="toctree-l2"><a class="reference internal" href="Example_of_a_small_Multi-Player_Simulation__with_Centralized_Algorithms.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="Example_of_a_small_Multi-Player_Simulation__with_Centralized_Algorithms.html#An-example-of-a-small-Multi-Player-simulation,-with-Centralized-Algorithms">An example of a small Multi-Player simulation, with Centralized Algorithms</a></li>
<li class="toctree-l2"><a class="reference internal" href="Example_of_a_small_Multi-Player_Simulation__with_rhoRand_and_Selfish_Algorithms.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="Example_of_a_small_Multi-Player_Simulation__with_rhoRand_and_Selfish_Algorithms.html#An-example-of-a-small-Multi-Player-simulation,-with-rhoRand-and-Selfish,-for-different-algorithms">An example of a small Multi-Player simulation, with rhoRand and Selfish, for different algorithms</a></li>
<li class="toctree-l2"><a class="reference internal" href="Unsupervised_Learning_for_Bandit_problem.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="Unsupervised_Learning_for_Bandit_problem.html#Trying-to-use-Unsupervised-Learning-algorithms-for-a-Gaussian-bandit-problem">Trying to use Unsupervised Learning algorithms for a Gaussian bandit problem</a></li>
<li class="toctree-l2"><a class="reference internal" href="BlackBox_Bayesian_Optimization_for_Bandit_problems.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="BlackBox_Bayesian_Optimization_for_Bandit_problems.html#Trying-to-use-Black-Box-Bayesian-optimization-algorithms-for-a-Gaussian-bandit-problem">Trying to use Black-Box Bayesian optimization algorithms for a Gaussian bandit problem</a></li>
<li class="toctree-l2"><a class="reference internal" href="Lai_Robbins_Lower_Bound_for_Doubling_Trick_with_Restarting_Algorithms.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="Lai_Robbins_Lower_Bound_for_Doubling_Trick_with_Restarting_Algorithms.html#Lai-&-Robbins-lower-bound-for-stochastic-bandit-with-full-restart-points">Lai & Robbins lower-bound for stochastic bandit with full restart points</a></li>
<li class="toctree-l2"><a class="reference internal" href="Exploring_different_doubling_tricks_for_different_kinds_of_regret_bounds.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="Exploring_different_doubling_tricks_for_different_kinds_of_regret_bounds.html#Exploring-different-doubling-tricks-for-different-kinds-of-regret-bounds">Exploring different doubling tricks for different kinds of regret bounds</a></li>
<li class="toctree-l2"><a class="reference internal" href="Experiments_of_statistical_tests_for_piecewise_stationary_bandit.html">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="Experiments_of_statistical_tests_for_piecewise_stationary_bandit.html#Requirements-and-helper-functions">Requirements and helper functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="Experiments_of_statistical_tests_for_piecewise_stationary_bandit.html#Python-implementations-of-some-statistical-tests">Python implementations of some statistical tests</a></li>
<li class="toctree-l2"><a class="reference internal" href="Experiments_of_statistical_tests_for_piecewise_stationary_bandit.html#Comparing-the-different-implementations">Comparing the different implementations</a></li>
<li class="toctree-l2"><a class="reference internal" href="Experiments_of_statistical_tests_for_piecewise_stationary_bandit.html#More-simulations-and-some-plots">More simulations and some plots</a></li>
<li class="toctree-l2"><a class="reference internal" href="Experiments_of_statistical_tests_for_piecewise_stationary_bandit.html#Exploring-the-parameters-of-change-point-detection-algorithms:-how-to-tune-them?">Exploring the parameters of change point detection algorithms: how to tune them?</a></li>
<li class="toctree-l2"><a class="reference internal" href="Experiments_of_statistical_tests_for_piecewise_stationary_bandit.html#Conclusions">Conclusions</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Table of Contents</a></li>
<li class="toctree-l2"><a class="reference internal" href="#Demonstrations-of-Single-Player-Simulations-for-Non-Stationary-Bandits">Demonstrations of Single-Player Simulations for Non-Stationary-Bandits</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#Creating-the-problem">Creating the problem</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#Parameters-for-the-simulation">Parameters for the simulation</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Two-MAB-problems-with-Bernoulli-arms-and-piecewise-stationary-means">Two MAB problems with Bernoulli arms and piecewise stationary means</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Some-MAB-algorithms">Some MAB algorithms</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#Checking-if-the-problems-are-too-hard-or-not">Checking if the problems are too hard or not</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Creating-the-Evaluator-object">Creating the <code class="docutils literal notranslate"><span class="pre">Evaluator</span></code> object</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Solving-the-problem">Solving the problem</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#First-problem">First problem</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Second-problem">Second problem</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#Plotting-the-results">Plotting the results</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#First-problem-with-change-on-only-one-arm-(Local-Restart-should-be-better)">First problem with change on only one arm (Local Restart should be better)</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Second-problem-with-changes-on-all-arms-(Global-restart-should-be-better)">Second problem with changes on all arms (Global restart should be better)</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../Profiling.html">A note on execution times, speed and profiling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../uml_diagrams/README.html">UML diagrams</a></li>
<li class="toctree-l1"><a class="reference internal" href="../logs/README.html"><code class="docutils literal notranslate"><span class="pre">logs</span></code> files</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">SMPyBandits</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> »</li>
<li><a href="list.html">List of notebooks for SMPyBandits</a> »</li>
<li>Table of Contents</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/notebooks/Demonstrations_of_Single-Player_Simulations_for_Non-Stationary-Bandits.ipynb.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<style>
/* CSS for nbsphinx extension */
/* remove conflicting styling from Sphinx themes */
div.nbinput,
div.nbinput div.prompt,
div.nbinput div.input_area,
div.nbinput div[class*=highlight],
div.nbinput div[class*=highlight] pre,
div.nboutput,
div.nbinput div.prompt,
div.nbinput div.output_area,
div.nboutput div[class*=highlight],
div.nboutput div[class*=highlight] pre {
background: none;
border: none;
padding: 0 0;
margin: 0;
box-shadow: none;
}
/* avoid gaps between output lines */
div.nboutput div[class*=highlight] pre {
line-height: normal;
}
/* input/output containers */
div.nbinput,
div.nboutput {
display: -webkit-flex;
display: flex;
align-items: flex-start;
margin: 0;
width: 100%;
}
@media (max-width: 540px) {
div.nbinput,
div.nboutput {
flex-direction: column;
}
}
/* input container */
div.nbinput {
padding-top: 5px;
}
/* last container */
div.nblast {
padding-bottom: 5px;
}
/* input prompt */
div.nbinput div.prompt pre {
color: #307FC1;
}
/* output prompt */
div.nboutput div.prompt pre {
color: #BF5B3D;
}
/* all prompts */
div.nbinput div.prompt,
div.nboutput div.prompt {
min-width: 5ex;
padding-top: 0.4em;
padding-right: 0.4em;
text-align: right;
flex: 0;
}
@media (max-width: 540px) {
div.nbinput div.prompt,
div.nboutput div.prompt {
text-align: left;
padding: 0.4em;
}
div.nboutput div.prompt.empty {
padding: 0;
}
}
/* disable scrollbars on prompts */
div.nbinput div.prompt pre,
div.nboutput div.prompt pre {
overflow: hidden;
}
/* input/output area */
div.nbinput div.input_area,
div.nboutput div.output_area {
padding: 0.4em;
-webkit-flex: 1;
flex: 1;
overflow: auto;
}
@media (max-width: 540px) {
div.nbinput div.input_area,
div.nboutput div.output_area {
width: 100%;
}
}
/* input area */
div.nbinput div.input_area {
border: 1px solid #e0e0e0;
border-radius: 2px;
background: #f5f5f5;
}
/* override MathJax center alignment in output cells */
div.nboutput div[class*=MathJax] {
text-align: left !important;
}
/* override sphinx.ext.imgmath center alignment in output cells */
div.nboutput div.math p {
text-align: left;
}
/* standard error */
div.nboutput div.output_area.stderr {
background: #fdd;
}
/* ANSI colors */
.ansi-black-fg { color: #3E424D; }
.ansi-black-bg { background-color: #3E424D; }
.ansi-black-intense-fg { color: #282C36; }
.ansi-black-intense-bg { background-color: #282C36; }
.ansi-red-fg { color: #E75C58; }
.ansi-red-bg { background-color: #E75C58; }
.ansi-red-intense-fg { color: #B22B31; }
.ansi-red-intense-bg { background-color: #B22B31; }
.ansi-green-fg { color: #00A250; }
.ansi-green-bg { background-color: #00A250; }
.ansi-green-intense-fg { color: #007427; }
.ansi-green-intense-bg { background-color: #007427; }
.ansi-yellow-fg { color: #DDB62B; }
.ansi-yellow-bg { background-color: #DDB62B; }
.ansi-yellow-intense-fg { color: #B27D12; }
.ansi-yellow-intense-bg { background-color: #B27D12; }
.ansi-blue-fg { color: #208FFB; }
.ansi-blue-bg { background-color: #208FFB; }
.ansi-blue-intense-fg { color: #0065CA; }
.ansi-blue-intense-bg { background-color: #0065CA; }
.ansi-magenta-fg { color: #D160C4; }
.ansi-magenta-bg { background-color: #D160C4; }
.ansi-magenta-intense-fg { color: #A03196; }
.ansi-magenta-intense-bg { background-color: #A03196; }
.ansi-cyan-fg { color: #60C6C8; }
.ansi-cyan-bg { background-color: #60C6C8; }
.ansi-cyan-intense-fg { color: #258F8F; }
.ansi-cyan-intense-bg { background-color: #258F8F; }
.ansi-white-fg { color: #C5C1B4; }
.ansi-white-bg { background-color: #C5C1B4; }
.ansi-white-intense-fg { color: #A1A6B2; }
.ansi-white-intense-bg { background-color: #A1A6B2; }
.ansi-default-inverse-fg { color: #FFFFFF; }
.ansi-default-inverse-bg { background-color: #000000; }
.ansi-bold { font-weight: bold; }
.ansi-underline { text-decoration: underline; }
/* Some additional styling taken form the Jupyter notebook CSS */
div.rendered_html table {
border: none;
border-collapse: collapse;
border-spacing: 0;
color: black;
font-size: 12px;
table-layout: fixed;
}
div.rendered_html thead {
border-bottom: 1px solid black;
vertical-align: bottom;
}
div.rendered_html tr,
div.rendered_html th,
div.rendered_html td {
text-align: right;
vertical-align: middle;
padding: 0.5em 0.5em;
line-height: normal;
white-space: normal;
max-width: none;
border: none;
}
div.rendered_html th {
font-weight: bold;
}
div.rendered_html tbody tr:nth-child(odd) {
background: #f5f5f5;
}
div.rendered_html tbody tr:hover {
background: rgba(66, 165, 245, 0.2);
}
/* CSS overrides for sphinx_rtd_theme */
/* 24px margin */
.nbinput.nblast,
.nboutput.nblast {
margin-bottom: 19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast + .nbinput {
margin-top: -19px;
}
.admonition > p:before {
margin-right: 4px; /* make room for the exclamation icon */
}
/* Fix math alignment, see https://github.com/rtfd/sphinx_rtd_theme/pull/686 */
.math {
text-align: unset;
}
</style>
<div class="section" id="Table-of-Contents">
<h1>Table of Contents<a class="headerlink" href="#Table-of-Contents" title="Permalink to this headline">¶</a></h1>
<p><div class="lev1 toc-item"><p>1 Demonstrations of Single-Player Simulations for Non-Stationary-Bandits</p>
</div><div class="lev2 toc-item"><p>1.1 Creating the problem</p>
</div><div class="lev3 toc-item"><p>1.1.1 Parameters for the simulation</p>
</div><div class="lev3 toc-item"><p>1.1.2 Two MAB problems with Bernoulli arms and piecewise stationary means</p>
</div><div class="lev3 toc-item"><p>1.1.3 Some MAB algorithms</p>
</div><div class="lev4 toc-item"><p>1.1.3.1 Parameters of the algorithms</p>
</div><div class="lev4 toc-item"><p>1.1.3.2 Algorithms</p>
</div><div class="lev2 toc-item"><p>1.2 Checking if the problems are too hard or not</p>
</div><div class="lev2 toc-item"><p>1.3 Creating the Evaluator object</p>
</div><div class="lev2 toc-item"><p>1.4 Solving the problem</p>
</div><div class="lev3 toc-item"><p>1.4.1 First problem</p>
</div><div class="lev3 toc-item"><p>1.4.2 Second problem</p>
</div><div class="lev2 toc-item"><p>1.5 Plotting the results</p>
</div><div class="lev3 toc-item"><p>1.5.1 First problem with change on only one arm (Local Restart should be better)</p>
</div><div class="lev3 toc-item"><p>1.5.2 Second problem with changes on all arms (Global restart should be better)</p>
</div></div>
<hr class="docutils" />
<div class="section" id="Demonstrations-of-Single-Player-Simulations-for-Non-Stationary-Bandits">
<h1>Demonstrations of Single-Player Simulations for Non-Stationary-Bandits<a class="headerlink" href="#Demonstrations-of-Single-Player-Simulations-for-Non-Stationary-Bandits" title="Permalink to this headline">¶</a></h1>
<p>This notebook shows how to 1) <strong>define</strong>, 2) <strong>launch</strong>, and 3) <strong>plot the results</strong> of numerical simulations of piecewise stationary (multi-armed) bandits problems using my framework <a class="reference external" href="https://github.com/SMPyBandits/SMPyBandits">SMPyBandits</a>. For more details on the maths behind this problem, see this page in the documentation: <a class="reference external" href="https://smpybandits.github.io/NonStationaryBandits.html">SMPyBandits.GitHub.io/NonStationaryBandits.html</a>.</p>
<p>First, be sure to be in the main folder, or to have <a class="reference external" href="https://github.com/SMPyBandits/SMPyBandits">SMPyBandits</a> installed, and import <code class="docutils literal notranslate"><span class="pre">Evaluator</span></code> from <code class="docutils literal notranslate"><span class="pre">Environment</span></code> package.</p>
<p>WARNING If you are running this notebook locally, in the <code class="docutils literal notranslate"><span class="pre">`notebooks</span></code> <<a class="reference external" href="https://github.com/SMPyBandits/SMPyBandits/tree/master/notebooks">https://github.com/SMPyBandits/SMPyBandits/tree/master/notebooks</a>>`__ folder in the <code class="docutils literal notranslate"><span class="pre">`SMPyBandits</span></code> <<a class="reference external" href="https://github.com/SMPyBandits/SMPyBandits/">https://github.com/SMPyBandits/SMPyBandits/</a>>`__ source, you need to do:</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[1]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="s1">'..'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>If you are running this notebook elsewhere, <code class="docutils literal notranslate"><span class="pre">SMPyBandits</span></code> can be <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span></code>ed easily: (this is especially true if you run this notebook from Google Colab or MyBinder).</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[2]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="k">try</span><span class="p">:</span>
<span class="kn">import</span> <span class="nn">SMPyBandits</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
<span class="o">!</span>pip3 install SMPyBandits
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Info: Using the Jupyter notebook version of the tqdm() decorator, tqdm_notebook() ...
</pre></div></div>
</div>
<p>Let’s just check the versions of the installed modules:</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[3]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="o">!</span>pip3 install watermark > /dev/null
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[4]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="o">%</span><span class="k">load_ext</span> watermark
<span class="o">%</span><span class="k">watermark</span> -v -m -p SMPyBandits,numpy,matplotlib -a "Lilian Besson"
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Lilian Besson
CPython 3.6.7
IPython 7.2.0
SMPyBandits 0.9.4
numpy 1.15.4
matplotlib 3.0.2
compiler : GCC 8.2.0
system : Linux
release : 4.15.0-42-generic
machine : x86_64
processor : x86_64
CPU cores : 4
interpreter: 64bit
</pre></div></div>
</div>
<p>We can now import all the modules we need for this demonstration.</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[5]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[44]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">FIGSIZE</span> <span class="o">=</span> <span class="p">(</span><span class="mf">19.80</span><span class="p">,</span> <span class="mf">10.80</span><span class="p">)</span>
<span class="n">DPI</span> <span class="o">=</span> <span class="mi">160</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[45]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="c1"># Large figures for pretty notebooks</span>
<span class="kn">import</span> <span class="nn">matplotlib</span> <span class="k">as</span> <span class="nn">mpl</span>
<span class="n">mpl</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">'figure.figsize'</span><span class="p">]</span> <span class="o">=</span> <span class="n">FIGSIZE</span>
<span class="n">mpl</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">'figure.dpi'</span><span class="p">]</span> <span class="o">=</span> <span class="n">DPI</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[46]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="c1"># Local imports</span>
<span class="kn">from</span> <span class="nn">SMPyBandits.Environment</span> <span class="k">import</span> <span class="n">Evaluator</span><span class="p">,</span> <span class="n">tqdm</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[47]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="c1"># Large figures for pretty notebooks</span>
<span class="kn">import</span> <span class="nn">matplotlib</span> <span class="k">as</span> <span class="nn">mpl</span>
<span class="n">mpl</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">'figure.figsize'</span><span class="p">]</span> <span class="o">=</span> <span class="n">FIGSIZE</span>
<span class="n">mpl</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">'figure.dpi'</span><span class="p">]</span> <span class="o">=</span> <span class="n">DPI</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[48]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="c1"># Large figures for pretty notebooks</span>
<span class="kn">import</span> <span class="nn">matplotlib</span> <span class="k">as</span> <span class="nn">mpl</span>
<span class="n">mpl</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">'figure.figsize'</span><span class="p">]</span> <span class="o">=</span> <span class="n">FIGSIZE</span>
<span class="n">mpl</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">'figure.dpi'</span><span class="p">]</span> <span class="o">=</span> <span class="n">DPI</span>
</pre></div>
</div>
</div>
<p>We also need arms, for instance <code class="docutils literal notranslate"><span class="pre">Bernoulli</span></code>-distributed arm:</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[10]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="c1"># Import arms</span>
<span class="kn">from</span> <span class="nn">SMPyBandits.Arms</span> <span class="k">import</span> <span class="n">Bernoulli</span>
</pre></div>
</div>
</div>
<p>And finally we need some single-player Reinforcement Learning algorithms:</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[11]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="c1"># Import algorithms</span>
<span class="kn">from</span> <span class="nn">SMPyBandits.Policies</span> <span class="k">import</span> <span class="o">*</span>
</pre></div>
</div>
</div>
<hr class="docutils" />
<div class="section" id="Creating-the-problem">
<h2>Creating the problem<a class="headerlink" href="#Creating-the-problem" title="Permalink to this headline">¶</a></h2>
<div class="section" id="Parameters-for-the-simulation">
<h3>Parameters for the simulation<a class="headerlink" href="#Parameters-for-the-simulation" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li><p><span class="math notranslate nohighlight">\(T = 2000\)</span> is the time horizon,</p></li>
<li><p><span class="math notranslate nohighlight">\(N = 100\)</span> is the number of repetitions, or 1 to debug the simulations,</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">N_JOBS</span> <span class="pre">=</span> <span class="pre">4</span></code> is the number of cores used to parallelize the code,</p></li>
<li><p><span class="math notranslate nohighlight">\(5\)</span> piecewise stationary sequences will have length 400</p></li>
</ul>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[49]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="k">import</span> <span class="n">cpu_count</span>
<span class="n">CPU_COUNT</span> <span class="o">=</span> <span class="n">cpu_count</span><span class="p">()</span>
<span class="n">N_JOBS</span> <span class="o">=</span> <span class="n">CPU_COUNT</span> <span class="k">if</span> <span class="n">CPU_COUNT</span> <span class="o"><=</span> <span class="mi">4</span> <span class="k">else</span> <span class="n">CPU_COUNT</span> <span class="o">-</span> <span class="mi">4</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Using </span><span class="si">{}</span><span class="s2"> jobs in parallel..."</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">N_JOBS</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Using 4 jobs in parallel...
</pre></div></div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[51]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">HORIZON</span> <span class="o">=</span> <span class="mi">2000</span>
<span class="n">REPETITIONS</span> <span class="o">=</span> <span class="mi">50</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Using T = </span><span class="si">{}</span><span class="s2">, and N = </span><span class="si">{}</span><span class="s2"> repetitions"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">HORIZON</span><span class="p">,</span> <span class="n">REPETITIONS</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
Using T = 2000, and N = 50 repetitions
</pre></div></div>
</div>
</div>
<div class="section" id="Two-MAB-problems-with-Bernoulli-arms-and-piecewise-stationary-means">
<h3>Two MAB problems with Bernoulli arms and piecewise stationary means<a class="headerlink" href="#Two-MAB-problems-with-Bernoulli-arms-and-piecewise-stationary-means" title="Permalink to this headline">¶</a></h3>
<p>We consider in this example <span class="math notranslate nohighlight">\(2\)</span> problems, with <code class="docutils literal notranslate"><span class="pre">Bernoulli</span></code> arms, of different piecewise stationary means.</p>
<ol class="arabic simple">
<li><p>The first problem has changes on only one arm at every breakpoint times,</p></li>
<li><p>The second problem has changes on all arms at every breakpoint times.</p></li>
</ol>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[53]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">ENVIRONMENTS</span> <span class="o">=</span> <span class="p">[]</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[54]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">ENVIRONMENT_0</span> <span class="o">=</span> <span class="p">{</span> <span class="c1"># A simple piece-wise stationary problem</span>
<span class="s2">"arm_type"</span><span class="p">:</span> <span class="n">Bernoulli</span><span class="p">,</span>
<span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"listOfMeans"</span><span class="p">:</span> <span class="p">[</span>
<span class="p">[</span><span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">],</span> <span class="c1"># 0 to 399</span>
<span class="p">[</span><span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">],</span> <span class="c1"># 400 to 799</span>
<span class="p">[</span><span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">],</span> <span class="c1"># 800 to 1199</span>
<span class="p">[</span><span class="mf">0.7</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">],</span> <span class="c1"># 1200 to 1599</span>
<span class="p">[</span><span class="mf">0.7</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">],</span> <span class="c1"># 1600 to end</span>
<span class="p">],</span>
<span class="s2">"changePoints"</span><span class="p">:</span> <span class="p">[</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">0</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">400</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">800</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">1200</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">1600</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="p">],</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[55]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="c1"># Pb 2 changes are on all or almost arms at a time</span>
<span class="n">ENVIRONMENT_1</span> <span class="o">=</span> <span class="p">{</span> <span class="c1"># A simple piece-wise stationary problem</span>
<span class="s2">"arm_type"</span><span class="p">:</span> <span class="n">Bernoulli</span><span class="p">,</span>
<span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"listOfMeans"</span><span class="p">:</span> <span class="p">[</span>
<span class="p">[</span><span class="mf">0.4</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">],</span> <span class="c1"># 0 to 399</span>
<span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="mf">0.7</span><span class="p">],</span> <span class="c1"># 400 to 799</span>
<span class="p">[</span><span class="mf">0.6</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">],</span> <span class="c1"># 800 to 1199</span>
<span class="p">[</span><span class="mf">0.7</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.3</span><span class="p">],</span> <span class="c1"># 1200 to 1599</span>
<span class="p">[</span><span class="mf">0.8</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">],</span> <span class="c1"># 1600 to end</span>
<span class="p">],</span>
<span class="s2">"changePoints"</span><span class="p">:</span> <span class="p">[</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">0</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">400</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">800</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">1200</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="nb">int</span><span class="p">(</span><span class="mi">1600</span> <span class="o">*</span> <span class="n">HORIZON</span> <span class="o">/</span> <span class="mf">2000.0</span><span class="p">),</span>
<span class="p">],</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[56]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">ENVIRONMENTS</span> <span class="o">=</span> <span class="p">[</span>
<span class="n">ENVIRONMENT_0</span><span class="p">,</span>
<span class="n">ENVIRONMENT_1</span><span class="p">,</span>
<span class="p">]</span>
<span class="n">list_nb_arms</span> <span class="o">=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s2">"params"</span><span class="p">][</span><span class="s2">"listOfMeans"</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span> <span class="k">for</span> <span class="n">env</span> <span class="ow">in</span> <span class="n">ENVIRONMENTS</span><span class="p">]</span>
<span class="n">NB_ARMS</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">list_nb_arms</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">all</span><span class="p">(</span><span class="n">n</span> <span class="o">==</span> <span class="n">NB_ARMS</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">list_nb_arms</span><span class="p">),</span> <span class="s2">"Error: it is NOT supported to have successive problems with a different number of arms!"</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"==> Using K = </span><span class="si">{}</span><span class="s2"> arms"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">NB_ARMS</span><span class="p">))</span>
<span class="n">NB_BREAK_POINTS</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s2">"params"</span><span class="p">][</span><span class="s2">"changePoints"</span><span class="p">])</span> <span class="k">for</span> <span class="n">env</span> <span class="ow">in</span> <span class="n">ENVIRONMENTS</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"==> Using Upsilon_T = </span><span class="si">{}</span><span class="s2"> change points"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">NB_BREAK_POINTS</span><span class="p">))</span>
<span class="n">CHANGE_POINTS</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="o">.</span><span class="n">union</span><span class="p">(</span><span class="o">*</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">env</span><span class="p">[</span><span class="s2">"params"</span><span class="p">][</span><span class="s2">"changePoints"</span><span class="p">])</span> <span class="k">for</span> <span class="n">env</span> <span class="ow">in</span> <span class="n">ENVIRONMENTS</span><span class="p">)))))</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"==> Using the following </span><span class="si">{}</span><span class="s2"> change points"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">CHANGE_POINTS</span><span class="p">)))</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
==> Using K = 3 arms
==> Using Upsilon_T = 5 change points
==> Using the following [0, 400, 800, 1200, 1600] change points
</pre></div></div>
</div>
</div>
<div class="section" id="Some-MAB-algorithms">
<h3>Some MAB algorithms<a class="headerlink" href="#Some-MAB-algorithms" title="Permalink to this headline">¶</a></h3>
<p>We want compare some classical MAB algorithms (<span class="math notranslate nohighlight">\(\mathrm{UCB}_1\)</span>, Thompson Sampling and <span class="math notranslate nohighlight">\(\mathrm{kl}\)</span>-<span class="math notranslate nohighlight">\(\mathrm{UCB}\)</span>) that are designed to solve stationary problems against other algorithms designed to solve piecewise-stationary problems.</p>
<div class="section" id="Parameters-of-the-algorithms">
<h4>Parameters of the algorithms<a class="headerlink" href="#Parameters-of-the-algorithms" title="Permalink to this headline">¶</a></h4>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[15]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">klucb</span> <span class="o">=</span> <span class="n">klucb_mapping</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">ENVIRONMENTS</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="s1">'arm_type'</span><span class="p">]),</span> <span class="n">klucbBern</span><span class="p">)</span>
<span class="n">klucb</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[15]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span><function SMPyBandits.Policies.kullback.klucbBern>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[61]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">WINDOW_SIZE</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="mi">80</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">HORIZON</span> <span class="o">/</span> <span class="mi">10000</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"M-UCB will use a window of size </span><span class="si">{}</span><span class="s2">"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">WINDOW_SIZE</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
M-UCB will use a window of size 80
</pre></div></div>
</div>
</div>
<div class="section" id="Algorithms">
<h4>Algorithms<a class="headerlink" href="#Algorithms" title="Permalink to this headline">¶</a></h4>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[62]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">POLICIES</span> <span class="o">=</span> <span class="p">[</span> <span class="c1"># XXX Regular adversarial bandits algorithms!</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">Exp3PlusPlus</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{}</span> <span class="p">},</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># XXX Regular stochastic bandits algorithms!</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">UCBalpha</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span> <span class="s2">"alpha"</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="p">}</span> <span class="p">},</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">klUCB</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span> <span class="s2">"klucb"</span><span class="p">:</span> <span class="n">klucb</span><span class="p">,</span> <span class="p">}</span> <span class="p">},</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">Thompson</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span> <span class="s2">"posterior"</span><span class="p">:</span> <span class="n">Beta</span><span class="p">,</span> <span class="p">}</span> <span class="p">},</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># XXX This is still highly experimental!</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">DiscountedThompson</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"posterior"</span><span class="p">:</span> <span class="n">DiscountedBeta</span><span class="p">,</span> <span class="s2">"gamma"</span><span class="p">:</span> <span class="n">gamma</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">for</span> <span class="n">gamma</span> <span class="ow">in</span> <span class="p">[</span><span class="mf">0.99</span><span class="p">,</span> <span class="mf">0.9</span><span class="p">,</span> <span class="mf">0.7</span><span class="p">]</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># --- The Exp3R algorithm works reasonably well</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">Exp3R</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span> <span class="s2">"horizon"</span><span class="p">:</span> <span class="n">HORIZON</span><span class="p">,</span> <span class="p">}</span> <span class="p">}</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># --- XXX The Exp3RPlusPlus variant of Exp3R algorithm works also reasonably well</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">Exp3RPlusPlus</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span> <span class="s2">"horizon"</span><span class="p">:</span> <span class="n">HORIZON</span><span class="p">,</span> <span class="p">}</span> <span class="p">}</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># --- XXX Test a few CD-MAB algorithms that need to know NB_BREAK_POINTS</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">archtype</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"horizon"</span><span class="p">:</span> <span class="n">HORIZON</span><span class="p">,</span>
<span class="s2">"max_nb_random_events"</span><span class="p">:</span> <span class="n">NB_BREAK_POINTS</span><span class="p">,</span>
<span class="s2">"policy"</span><span class="p">:</span> <span class="n">policy</span><span class="p">,</span>
<span class="s2">"per_arm_restart"</span><span class="p">:</span> <span class="n">per_arm_restart</span><span class="p">,</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">for</span> <span class="n">archtype</span> <span class="ow">in</span> <span class="p">[</span>
<span class="n">CUSUM_IndexPolicy</span><span class="p">,</span>
<span class="n">PHT_IndexPolicy</span><span class="p">,</span> <span class="c1"># OK PHT_IndexPolicy is very much like CUSUM</span>
<span class="p">]</span>
<span class="k">for</span> <span class="n">policy</span> <span class="ow">in</span> <span class="p">[</span>
<span class="c1"># UCB, # XXX comment to only test klUCB</span>
<span class="n">klUCB</span><span class="p">,</span>
<span class="p">]</span>
<span class="k">for</span> <span class="n">per_arm_restart</span> <span class="ow">in</span> <span class="p">[</span>
<span class="kc">True</span><span class="p">,</span> <span class="c1"># Per-arm restart XXX comment to only test global arm</span>
<span class="kc">False</span><span class="p">,</span> <span class="c1"># Global restart XXX seems more efficient? (at least more memory efficient!)</span>
<span class="p">]</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># --- XXX Test a few CD-MAB algorithms</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">archtype</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"horizon"</span><span class="p">:</span> <span class="n">HORIZON</span><span class="p">,</span>
<span class="s2">"policy"</span><span class="p">:</span> <span class="n">policy</span><span class="p">,</span>
<span class="s2">"per_arm_restart"</span><span class="p">:</span> <span class="n">per_arm_restart</span><span class="p">,</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">for</span> <span class="n">archtype</span> <span class="ow">in</span> <span class="p">[</span>
<span class="n">BernoulliGLR_IndexPolicy</span><span class="p">,</span> <span class="c1"># OK BernoulliGLR_IndexPolicy is very much like CUSUM</span>
<span class="n">GaussianGLR_IndexPolicy</span><span class="p">,</span> <span class="c1"># OK GaussianGLR_IndexPolicy is very much like Bernoulli GLR</span>
<span class="n">SubGaussianGLR_IndexPolicy</span><span class="p">,</span> <span class="c1"># OK SubGaussianGLR_IndexPolicy is very much like Gaussian GLR</span>
<span class="p">]</span>
<span class="k">for</span> <span class="n">policy</span> <span class="ow">in</span> <span class="p">[</span>
<span class="c1"># UCB, # XXX comment to only test klUCB</span>
<span class="n">klUCB</span><span class="p">,</span>
<span class="p">]</span>
<span class="k">for</span> <span class="n">per_arm_restart</span> <span class="ow">in</span> <span class="p">[</span>
<span class="kc">True</span><span class="p">,</span> <span class="c1"># Per-arm restart XXX comment to only test global arm</span>
<span class="kc">False</span><span class="p">,</span> <span class="c1"># Global restart XXX seems more efficient? (at least more memory efficient!)</span>
<span class="p">]</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># --- XXX The Monitored_IndexPolicy with specific tuning of the input parameters</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">Monitored_IndexPolicy</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"horizon"</span><span class="p">:</span> <span class="n">HORIZON</span><span class="p">,</span>
<span class="s2">"w"</span><span class="p">:</span> <span class="n">WINDOW_SIZE</span><span class="p">,</span>
<span class="s2">"b"</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">WINDOW_SIZE</span><span class="o">/</span><span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">NB_ARMS</span> <span class="o">*</span> <span class="n">HORIZON</span><span class="o">**</span><span class="mi">2</span><span class="p">)),</span>
<span class="s2">"policy"</span><span class="p">:</span> <span class="n">policy</span><span class="p">,</span>
<span class="s2">"per_arm_restart"</span><span class="p">:</span> <span class="n">per_arm_restart</span><span class="p">,</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">for</span> <span class="n">policy</span> <span class="ow">in</span> <span class="p">[</span>
<span class="c1"># UCB,</span>
<span class="n">klUCB</span><span class="p">,</span> <span class="c1"># XXX comment to only test UCB</span>
<span class="p">]</span>
<span class="k">for</span> <span class="n">per_arm_restart</span> <span class="ow">in</span> <span class="p">[</span>
<span class="kc">True</span><span class="p">,</span> <span class="c1"># Per-arm restart XXX comment to only test global arm</span>
<span class="kc">False</span><span class="p">,</span> <span class="c1"># Global restart XXX seems more efficient? (at least more memory efficient!)</span>
<span class="p">]</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># --- DONE The SW_UCB_Hash algorithm works fine!</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">SWHash_IndexPolicy</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"alpha"</span><span class="p">:</span> <span class="n">alpha</span><span class="p">,</span> <span class="s2">"lmbda"</span><span class="p">:</span> <span class="n">lmbda</span><span class="p">,</span> <span class="s2">"policy"</span><span class="p">:</span> <span class="n">UCB</span><span class="p">,</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">for</span> <span class="n">alpha</span> <span class="ow">in</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">]</span>
<span class="k">for</span> <span class="n">lmbda</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># --- # XXX experimental other version of the sliding window algorithm, knowing the horizon</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">SWUCBPlus</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"horizon"</span><span class="p">:</span> <span class="n">HORIZON</span><span class="p">,</span> <span class="s2">"alpha"</span><span class="p">:</span> <span class="n">alpha</span><span class="p">,</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">for</span> <span class="n">alpha</span> <span class="ow">in</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">]</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># --- # XXX experimental discounted UCB algorithm, knowing the horizon</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">DiscountedUCBPlus</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"max_nb_random_events"</span><span class="p">:</span> <span class="n">max_nb_random_events</span><span class="p">,</span> <span class="s2">"alpha"</span><span class="p">:</span> <span class="n">alpha</span><span class="p">,</span> <span class="s2">"horizon"</span><span class="p">:</span> <span class="n">HORIZON</span><span class="p">,</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">for</span> <span class="n">alpha</span> <span class="ow">in</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">]</span>
<span class="k">for</span> <span class="n">max_nb_random_events</span> <span class="ow">in</span> <span class="p">[</span><span class="n">NB_BREAK_POINTS</span><span class="p">]</span>
<span class="p">]</span> <span class="o">+</span> <span class="p">[</span> <span class="c1"># --- DONE the OracleSequentiallyRestartPolicy with klUCB/UCB policy works quite well, but NOT optimally!</span>
<span class="p">{</span> <span class="s2">"archtype"</span><span class="p">:</span> <span class="n">OracleSequentiallyRestartPolicy</span><span class="p">,</span> <span class="s2">"params"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"changePoints"</span><span class="p">:</span> <span class="n">CHANGE_POINTS</span><span class="p">,</span> <span class="s2">"policy"</span><span class="p">:</span> <span class="n">policy</span><span class="p">,</span>
<span class="s2">"per_arm_restart"</span><span class="p">:</span> <span class="n">per_arm_restart</span><span class="p">,</span>
<span class="c1"># "full_restart_when_refresh": full_restart_when_refresh,</span>
<span class="p">}</span> <span class="p">}</span>
<span class="k">for</span> <span class="n">policy</span> <span class="ow">in</span> <span class="p">[</span>
<span class="n">UCB</span><span class="p">,</span>
<span class="n">klUCB</span><span class="p">,</span> <span class="c1"># XXX comment to only test UCB</span>
<span class="n">Exp3PlusPlus</span><span class="p">,</span> <span class="c1"># XXX comment to only test UCB</span>
<span class="p">]</span>
<span class="k">for</span> <span class="n">per_arm_restart</span> <span class="ow">in</span> <span class="p">[</span><span class="kc">True</span><span class="p">]</span> <span class="c1">#, False]</span>
<span class="c1"># for full_restart_when_refresh in [True, False]</span>
<span class="p">]</span>
</pre></div>
</div>
</div>
<p>The complete configuration for the problems and these algorithms is then a simple dictionary:</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[64]:
</pre></div>
</div>
<div class="input_area highlight-ipython3 notranslate"><div class="highlight"><pre>
<span></span><span class="n">configuration</span> <span class="o">=</span> <span class="p">{</span>
<span class="c1"># --- Duration of the experiment</span>
<span class="s2">"horizon"</span><span class="p">:</span> <span class="n">HORIZON</span><span class="p">,</span>
<span class="c1"># --- Number of repetition of the experiment (to have an average)</span>
<span class="s2">"repetitions"</span><span class="p">:</span> <span class="n">REPETITIONS</span><span class="p">,</span>
<span class="c1"># --- Parameters for the use of joblib.Parallel</span>
<span class="s2">"n_jobs"</span><span class="p">:</span> <span class="n">N_JOBS</span><span class="p">,</span> <span class="c1"># = nb of CPU cores</span>
<span class="s2">"verbosity"</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="c1"># Max joblib verbosity</span>
<span class="c1"># --- Arms</span>
<span class="s2">"environment"</span><span class="p">:</span> <span class="n">ENVIRONMENTS</span><span class="p">,</span>
<span class="c1"># --- Algorithms</span>
<span class="s2">"policies"</span><span class="p">:</span> <span class="n">POLICIES</span><span class="p">,</span>
<span class="c1"># --- Random events</span>
<span class="s2">"nb_break_points"</span><span class="p">:</span> <span class="n">NB_BREAK_POINTS</span><span class="p">,</span>
<span class="c1"># --- Should we plot the lower-bounds or not?</span>
<span class="s2">"plot_lowerbound"</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span> <span class="c1"># XXX Default</span>
<span class="p">}</span>
<span class="n">configuration</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre><span></span>[64]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span>{'horizon': 2000,
'repetitions': 50,
'n_jobs': 4,
'verbosity': 0,
'environment': [{'arm_type': SMPyBandits.Arms.Bernoulli.Bernoulli,
'params': {'listOfMeans': [[0.2, 0.5, 0.9],
[0.2, 0.2, 0.9],
[0.2, 0.2, 0.1],
[0.7, 0.2, 0.1],
[0.7, 0.5, 0.1]],