-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_dist.py
More file actions
1329 lines (1188 loc) · 61.9 KB
/
Copy pathutils_dist.py
File metadata and controls
1329 lines (1188 loc) · 61.9 KB
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
"""
Utility functions and visualization class for distribution models exploration.
Used by distribution_models_week_2.ipynb
"""
import numpy as np
import ipywidgets as widgets
import plotly.graph_objects as go
from IPython.display import display, clear_output
from scipy import stats
import time
def sample_distribution(dist_type, dist_category, n_samples, **params):
"""Sample from the specified distribution"""
if dist_category == "Continuous":
if dist_type == "Uniform":
low = params.get('low', 0)
high = params.get('high', 1)
return np.random.uniform(low, high, n_samples)
elif dist_type == "Exponential":
scale = params.get('scale', 1)
return np.random.exponential(scale, n_samples)
elif dist_type == "Pareto":
shape = params.get('shape', 2.0)
scale = params.get('scale', 1.0)
# Pareto distribution: P(X > x) = (scale/x)^shape for x >= scale
# Using scipy's parameterization: pareto(b, loc=0, scale=scale) gives support [scale, inf)
return stats.pareto.rvs(shape, loc=0, scale=scale, size=n_samples)
elif dist_type == "Beta":
alpha = params.get('alpha', 2)
beta = params.get('beta', 2)
return np.random.beta(alpha, beta, n_samples)
elif dist_type == "Gamma":
shape = params.get('shape', 2)
scale = params.get('scale', 1)
return np.random.gamma(shape, scale, n_samples)
elif dist_type == "Normal":
mean = params.get('mean', 0)
std = params.get('std', 1)
return np.random.normal(mean, std, n_samples)
else:
return np.random.normal(0, 1, n_samples)
else: # Discrete
if dist_type == "Bernoulli":
p = params.get('p', 0.5)
return np.random.binomial(1, p, n_samples)
elif dist_type == "Geometric":
p = params.get('p', 0.5)
return np.random.geometric(p, n_samples)
elif dist_type == "Binomial":
n = params.get('n', 10)
p = params.get('p', 0.5)
return np.random.binomial(n, p, n_samples)
elif dist_type == "Poisson":
lam = params.get('lam', 5)
return np.random.poisson(lam, n_samples)
elif dist_type == "Hypergeometric":
ngood = params.get('ngood', 10)
nbad = params.get('nbad', 10)
nsample = params.get('nsample', 10)
return np.random.hypergeometric(ngood, nbad, nsample, n_samples)
else:
return np.random.binomial(1, 0.5, n_samples)
def compute_pdf_pmf(x_values, dist_type, dist_category, **params):
"""Compute PDF (continuous) or PMF (discrete) for the distribution"""
if dist_category == "Continuous":
if dist_type == "Uniform":
low = params.get('low', 0)
high = params.get('high', 1)
return stats.uniform.pdf(x_values, loc=low, scale=high-low)
elif dist_type == "Exponential":
scale = params.get('scale', 1)
return stats.expon.pdf(x_values, scale=scale)
elif dist_type == "Pareto":
shape = params.get('shape', 2.0)
scale = params.get('scale', 1.0)
return stats.pareto.pdf(x_values, shape, loc=0, scale=scale)
elif dist_type == "Beta":
alpha = params.get('alpha', 2)
beta = params.get('beta', 2)
return stats.beta.pdf(x_values, alpha, beta)
elif dist_type == "Gamma":
shape = params.get('shape', 2)
scale = params.get('scale', 1)
return stats.gamma.pdf(x_values, shape, scale=scale)
elif dist_type == "Normal":
mean = params.get('mean', 0)
std = params.get('std', 1)
return stats.norm.pdf(x_values, mean, std)
else:
return stats.norm.pdf(x_values, 0, 1)
else: # Discrete
if dist_type == "Bernoulli":
p = params.get('p', 0.5)
return stats.bernoulli.pmf(np.round(x_values).astype(int), p)
elif dist_type == "Geometric":
p = params.get('p', 0.5)
return stats.geom.pmf(np.round(x_values).astype(int), p)
elif dist_type == "Binomial":
n = params.get('n', 10)
p = params.get('p', 0.5)
return stats.binom.pmf(np.round(x_values).astype(int), n, p)
elif dist_type == "Poisson":
lam = params.get('lam', 5)
# For discrete, return PMF at integer values
return stats.poisson.pmf(np.round(x_values).astype(int), lam)
elif dist_type == "Hypergeometric":
ngood = params.get('ngood', 10)
nbad = params.get('nbad', 10)
nsample = params.get('nsample', 10)
return stats.hypergeom.pmf(np.round(x_values).astype(int), ngood + nbad, ngood, nsample)
else:
return stats.bernoulli.pmf(np.round(x_values).astype(int), 0.5)
def compute_true_probability(dist_type, dist_category, prob_type, bound1, bound2, **params):
"""Compute true probability using CDF/PMF"""
if dist_category == "Continuous":
if dist_type == "Uniform":
low = params.get('low', 0)
high = params.get('high', 1)
dist = stats.uniform(loc=low, scale=high-low)
elif dist_type == "Exponential":
scale = params.get('scale', 1)
dist = stats.expon(scale=scale)
elif dist_type == "Pareto":
shape = params.get('shape', 2.0)
scale = params.get('scale', 1.0)
dist = stats.pareto(shape, loc=0, scale=scale)
elif dist_type == "Beta":
alpha = params.get('alpha', 2)
beta = params.get('beta', 2)
dist = stats.beta(alpha, beta)
elif dist_type == "Gamma":
shape = params.get('shape', 2)
scale = params.get('scale', 1)
dist = stats.gamma(shape, scale=scale)
elif dist_type == "Normal":
mean = params.get('mean', 0)
std = params.get('std', 1)
dist = stats.norm(mean, std)
else:
dist = stats.norm(0, 1)
if prob_type == "of outcome":
# For continuous, P(X = x) = 0, so return 0
return 0.0
elif prob_type == "under upper bound":
# P(X <= bound2) - inclusive upper bound
prob = dist.cdf(bound2)
# Round to 1.0 if very close (within floating point precision)
return 1.0 if abs(prob - 1.0) < 1e-10 else prob
elif prob_type == "above lower bound":
# P(X >= bound1) - inclusive lower bound
prob = 1 - dist.cdf(bound1)
# Round to 1.0 if very close (within floating point precision)
return 1.0 if abs(prob - 1.0) < 1e-10 else prob
elif prob_type == "in interval":
# For continuous distributions, P(bound1 <= X <= bound2) - inclusive bounds
# Since P(X = x) = 0 for continuous, this equals CDF(bound2) - CDF(bound1)
prob = dist.cdf(bound2) - dist.cdf(bound1)
# Ensure result is in [0, 1]
prob = max(0.0, min(1.0, prob))
return 1.0 if abs(prob - 1.0) < 1e-6 else prob
else: # Discrete
if dist_type == "Bernoulli":
p = params.get('p', 0.5)
dist = stats.bernoulli(p)
elif dist_type == "Geometric":
p = params.get('p', 0.5)
dist = stats.geom(p)
elif dist_type == "Binomial":
n = params.get('n', 10)
p = params.get('p', 0.5)
dist = stats.binom(n, p)
elif dist_type == "Poisson":
lam = params.get('lam', 5)
dist = stats.poisson(lam)
elif dist_type == "Hypergeometric":
ngood = params.get('ngood', 10)
nbad = params.get('nbad', 10)
nsample = params.get('nsample', 10)
dist = stats.hypergeom(ngood + nbad, ngood, nsample)
else:
dist = stats.bernoulli(0.5)
if prob_type == "of outcome":
return dist.pmf(int(np.round(bound1)))
elif prob_type == "under upper bound":
# P(X <= bound2) - inclusive upper bound
return dist.cdf(int(np.round(bound2)))
elif prob_type == "above lower bound":
# P(X >= bound1) - inclusive lower bound: 1 - CDF(bound1 - 1)
return 1 - dist.cdf(int(np.round(bound1)) - 1)
elif prob_type == "in interval":
# P(bound1 <= X <= bound2) - inclusive both bounds
# = CDF(bound2) - CDF(bound1 - 1)
return dist.cdf(int(np.round(bound2))) - dist.cdf(int(np.round(bound1)) - 1)
return 0.0
def compute_estimated_probability(samples, prob_type, bound1, bound2):
"""Compute estimated probability from samples using inclusive bounds"""
if prob_type == "of outcome":
# Count samples exactly equal to bound1 (for discrete, use integer comparison)
if samples.dtype in [np.int32, np.int64] or np.all(samples == np.round(samples)):
count = np.sum(samples == int(np.round(bound1)))
else:
count = np.sum(np.abs(samples - bound1) < 1e-6)
elif prob_type == "under upper bound":
count = np.sum(samples <= bound2) # Inclusive upper bound
elif prob_type == "above lower bound":
count = np.sum(samples >= bound1) # Inclusive lower bound
elif prob_type == "in interval":
count = np.sum((samples >= bound1) & (samples <= bound2)) # Inclusive both bounds
else:
return 0.0
return count / len(samples) if len(samples) > 0 else 0.0
def determine_batch_size(sample_index):
"""
Determine how many samples to add in this batch for animation.
Larger batches = fewer plot updates = faster animation.
- Samples 1-50: 5 at a time (for smooth start)
- Samples 50-200: 20 at a time
- Samples 200-500: 50 at a time
- Samples 500+: 100 at a time
"""
if sample_index < 50:
return 5
elif sample_index < 200:
return 20
elif sample_index < 500:
return 50
else:
return 100
class DistributionProbabilityVisualization:
"""Interactive visualization for distribution sampling and probability calculation"""
def __init__(self):
self.samples = np.array([])
self.plot_output = widgets.Output()
self.show_pdf_flag = False # Track whether PDF should be shown
self.show_shaded_region_flag = False # Track whether shaded region should be shown
self.bounds_interacted = False # Track whether user has interacted with bounds
# Distribution options
self.continuous_dists = ["Uniform", "Exponential", "Pareto", "Beta", "Gamma", "Normal"]
self.discrete_dists = ["Bernoulli", "Geometric", "Binomial", "Poisson", "Hypergeometric"]
self._create_widgets()
self._setup_callbacks()
def _create_widgets(self):
"""Create all widgets"""
# Category dropdown
self.category_dropdown = widgets.Dropdown(
options=["Discrete", "Continuous"],
value="Discrete",
description="Type:",
style={'description_width': 'initial'}
)
# Distribution dropdown (will be updated based on category)
self.dist_dropdown = widgets.Dropdown(
options=self.discrete_dists,
value="Bernoulli",
description="Distribution:",
style={'description_width': 'initial'}
)
# Parameter widgets (will be shown/hidden based on distribution)
self.param_widgets = {}
# Normal parameters
self.param_widgets['Normal'] = [
widgets.FloatSlider(value=0, min=-5, max=5, step=0.1, description='Mean:', style={'description_width': 'initial'}),
widgets.FloatSlider(value=1, min=0.1, max=3, step=0.1, description='Std:', style={'description_width': 'initial'})
]
# Exponential parameters
self.param_widgets['Exponential'] = [
widgets.FloatSlider(value=1, min=0.1, max=5, step=0.1, description='Scale:', style={'description_width': 'initial'})
]
# Beta parameters
self.param_widgets['Beta'] = [
widgets.FloatSlider(value=2, min=0.5, max=10, step=0.1, description='Alpha:', style={'description_width': 'initial'}),
widgets.FloatSlider(value=2, min=0.5, max=10, step=0.1, description='Beta:', style={'description_width': 'initial'})
]
# Gamma parameters
self.param_widgets['Gamma'] = [
widgets.FloatSlider(value=2, min=0.5, max=10, step=0.1, description='Shape:', style={'description_width': 'initial'}),
widgets.FloatSlider(value=1, min=0.1, max=5, step=0.1, description='Scale:', style={'description_width': 'initial'})
]
# Uniform parameters
self.param_widgets['Uniform'] = [
widgets.FloatSlider(value=0, min=-5, max=5, step=0.1, description='Low:', style={'description_width': 'initial'}),
widgets.FloatSlider(value=1, min=-5, max=5, step=0.1, description='High:', style={'description_width': 'initial'})
]
# Pareto parameters
self.param_widgets['Pareto'] = [
widgets.FloatSlider(value=2.0, min=0.1, max=5, step=0.1, description='Shape (α):', style={'description_width': 'initial'}),
widgets.FloatSlider(value=1.0, min=0.1, max=5, step=0.1, description='Scale (xₘ):', style={'description_width': 'initial'})
]
# Poisson parameters
self.param_widgets['Poisson'] = [
widgets.FloatSlider(value=2, min=0.5, max=20, step=0.1, description='Lambda:', style={'description_width': 'initial'})
]
# Binomial parameters
self.param_widgets['Binomial'] = [
widgets.IntSlider(value=10, min=1, max=50, step=1, description='n:', style={'description_width': 'initial'}),
widgets.FloatSlider(value=0.5, min=0.1, max=0.9, step=0.05, description='p:', style={'description_width': 'initial'})
]
# Bernoulli parameters
self.param_widgets['Bernoulli'] = [
widgets.FloatSlider(value=0.5, min=0.1, max=0.9, step=0.05, description='p:', style={'description_width': 'initial'})
]
# Geometric parameters
self.param_widgets['Geometric'] = [
widgets.FloatSlider(value=0.5, min=0.1, max=0.9, step=0.05, description='p:', style={'description_width': 'initial'})
]
# Hypergeometric parameters
self.param_widgets['Hypergeometric'] = [
widgets.IntSlider(value=10, min=1, max=50, step=1, description='ngood:', style={'description_width': 'initial'}),
widgets.IntSlider(value=10, min=1, max=50, step=1, description='nbad:', style={'description_width': 'initial'}),
widgets.IntSlider(value=10, min=1, max=50, step=1, description='nsample:', style={'description_width': 'initial'})
]
# Sample size (allow small draws like 5–10 as well as large Monte Carlo samples)
self.n_samples_slider = widgets.IntSlider(
value=1000, min=5, max=10000, step=5,
description="Samples:",
style={'description_width': 'initial'}
)
# Draw samples button
self.draw_button = widgets.Button(
description="Draw Samples",
button_style='success'
)
# Reset all button
self.reset_button = widgets.Button(
description="Reset all",
button_style='warning'
)
# Show PDF/PMF button (available before samples are drawn)
self.show_pdf_button = widgets.Button(
description="Show PDF/PMF",
button_style='info',
disabled=False
)
# Show Shaded Region button (shaded region follows probability type selection)
self.show_shaded_region_button = widgets.Button(
description="Display Shaded Region",
button_style='primary',
disabled=False
)
# Probability calculation dropdown
self.prob_type_dropdown = widgets.Dropdown(
options=["", "of outcome", "under upper bound", "above lower bound", "in interval"],
value="",
description="Find Probability:",
style={'description_width': 'initial'}
)
# Bound sliders (will be shown/hidden based on prob_type)
self.bound1_slider = widgets.FloatSlider(
value=0, min=-10, max=10, step=0.1,
description="Lower bound:",
style={'description_width': 'initial'}
)
self.bound2_slider = widgets.FloatSlider(
value=1, min=-10, max=10, step=0.1,
description="Upper bound:",
style={'description_width': 'initial'}
)
# Probability display
self.prob_label = widgets.HTML(
value='<div style="font-size: 18px; padding: 10px; background-color: #f0f0f0; border: 2px solid #333; border-radius: 5px;"><b>Estimated Probability:</b> <span style="color: #0066cc; font-size: 20px; font-weight: bold;">N/A</span><br><b>True Probability:</b> <span style="color: #cc6600; font-size: 20px; font-weight: bold;">N/A</span></div>'
)
# Status display for animation progress
self.status_html = widgets.HTML(
value="Ready to draw samples."
)
# Parameter container (will be updated)
self.param_container = widgets.VBox([])
# Slider container (will be dynamically updated based on prob_type)
self.slider_container = widgets.VBox([self.bound1_slider, self.bound2_slider])
# Probability controls (always visible so PDF/PMF can be shown before sampling)
self.prob_controls_container = widgets.VBox([
widgets.HTML("<hr>"),
self.prob_type_dropdown,
self.slider_container, # Dynamic slider container
widgets.HBox([self.show_pdf_button]), # Only PDF button (shaded region is automatic)
widgets.HTML("<hr>"),
self.prob_label
])
# Initialize slider visibility based on default prob_type
self._update_slider_visibility()
self._update_bound_sliders()
def _setup_callbacks(self):
"""Setup widget callbacks"""
self.category_dropdown.observe(self._on_category_change, names='value')
self.dist_dropdown.observe(self._on_dist_change, names='value')
self.prob_type_dropdown.observe(self._on_prob_type_change, names='value')
self.draw_button.on_click(self._on_draw_clicked)
self.reset_button.on_click(self._on_reset_clicked)
self.show_pdf_button.on_click(self._on_show_pdf_clicked)
self.show_shaded_region_button.on_click(self._on_show_shaded_region_clicked)
# Update plot when sliders change (but only if samples exist)
for widgets_list in self.param_widgets.values():
for w in widgets_list:
w.observe(self._on_param_change, names='value')
self.n_samples_slider.observe(self._on_param_change, names='value')
self.bound1_slider.observe(self._on_bound_change, names='value')
self.bound2_slider.observe(self._on_bound_change, names='value')
def _on_bound_change(self, change):
"""Handle bound slider changes"""
self.bounds_interacted = True # User has interacted with bounds
if len(self.samples) > 0 or self.show_pdf_flag:
self._update_plot()
def _on_param_change(self, change):
"""Handle parameter changes"""
# Keep bound/slider ranges in sync when theoretical support changes
if self.dist_dropdown.value in ["Poisson", "Binomial"] or self.show_pdf_flag:
self._update_bound_sliders()
if len(self.samples) > 0 or self.show_pdf_flag:
self._update_plot()
def _on_category_change(self, change):
"""Handle category change"""
if change['new'] == "Continuous":
self.dist_dropdown.options = self.continuous_dists
self.dist_dropdown.value = "Uniform"
else:
self.dist_dropdown.options = self.discrete_dists
self.dist_dropdown.value = "Bernoulli"
self._update_param_widgets()
# Reset probability type dropdown to empty
self.prob_type_dropdown.value = ""
# Clear samples; keep PDF/PMF controls available
self.samples = np.array([])
self.show_pdf_flag = False
self.show_pdf_button.disabled = False
self.show_pdf_button.description = "Show PDF/PMF"
self.show_shaded_region_flag = False
self.show_shaded_region_button.disabled = False
self.show_shaded_region_button.description = "Display Shaded Region"
# Reset bounds interaction flag - histogram will be all blue until user interacts
self.bounds_interacted = False
# Reset status
self.status_html.value = "Ready to draw samples."
self._update_bound_sliders()
self._show_blank_plot()
def _on_dist_change(self, change):
"""Handle distribution change"""
self._update_param_widgets()
# Reset probability type dropdown to empty
self.prob_type_dropdown.value = ""
# Clear samples; keep PDF/PMF controls available
self.samples = np.array([])
self.show_pdf_flag = False
self.show_pdf_button.disabled = False
self.show_pdf_button.description = "Show PDF/PMF"
self.show_shaded_region_flag = False
self.show_shaded_region_button.disabled = False
self.show_shaded_region_button.description = "Display Shaded Region"
# Reset bounds interaction flag - histogram will be all blue until user interacts
self.bounds_interacted = False
# Reset status
self.status_html.value = "Ready to draw samples."
self._update_bound_sliders()
self._show_blank_plot()
def _on_reset_clicked(self, button):
"""Handle Reset all button click - reset everything to initial state"""
# Clear samples
self.samples = np.array([])
# Reset PDF flag and button (still available without samples)
self.show_pdf_flag = False
self.show_pdf_button.disabled = False
self.show_pdf_button.description = "Show PDF/PMF"
# Reset shaded region flag and button
self.show_shaded_region_flag = False
self.show_shaded_region_button.disabled = False
self.show_shaded_region_button.description = "Display Shaded Region"
# Reset bounds interaction flag
self.bounds_interacted = False
# Reset probability type
self.prob_type_dropdown.value = ""
self._update_slider_visibility()
self._update_bound_sliders()
# Reset probability label
self.prob_label.value = '<div style="font-size: 18px; padding: 10px; background-color: #f0f0f0; border: 2px solid #333; border-radius: 5px;"><b>Estimated Probability:</b> <span style="color: #0066cc; font-size: 20px; font-weight: bold;">N/A</span><br><b>True Probability:</b> <span style="color: #cc6600; font-size: 20px; font-weight: bold;">N/A</span></div>'
# Reset status
self.status_html.value = "Ready to draw samples."
# Show blank plot
self._show_blank_plot()
def _on_show_pdf_clicked(self, button):
"""Handle Show PDF/PMF button click (works with or without samples)"""
self.show_pdf_flag = not self.show_pdf_flag
if self.show_pdf_flag:
self.show_pdf_button.description = "Hide PDF/PMF"
self._update_bound_sliders()
self._update_plot()
else:
self.show_pdf_button.description = "Show PDF/PMF"
if len(self.samples) > 0:
self._update_plot()
else:
self._show_blank_plot()
def _on_show_shaded_region_clicked(self, button):
"""Handle Show Shaded Region button click"""
if len(self.samples) > 0 or self.show_pdf_flag:
self.show_shaded_region_flag = not self.show_shaded_region_flag
if self.show_shaded_region_flag:
self.show_shaded_region_button.description = "Hide Shaded Region"
else:
self.show_shaded_region_button.description = "Display Shaded Region"
self._update_plot()
def _update_param_widgets(self):
"""Update parameter widgets based on current distribution"""
dist = self.dist_dropdown.value
if dist in self.param_widgets:
self.param_container.children = tuple(self.param_widgets[dist])
# Don't update plot here to avoid double updates
def _update_slider_visibility(self):
"""Update which sliders are visible based on probability type"""
prob_type = self.prob_type_dropdown.value
if prob_type == "":
# Empty selection - hide sliders
self.slider_container.children = ()
elif prob_type == "of outcome":
# Only show bound1 (the outcome value)
self.slider_container.children = (self.bound1_slider,)
self.bound1_slider.description = "Outcome:"
elif prob_type == "under upper bound":
# Only show bound2 (upper bound)
self.slider_container.children = (self.bound2_slider,)
self.bound2_slider.description = "Upper bound:"
elif prob_type == "above lower bound":
# Only show bound1 (lower bound)
self.slider_container.children = (self.bound1_slider,)
self.bound1_slider.description = "Lower bound:"
elif prob_type == "in interval":
# Show both bounds
self.slider_container.children = (self.bound1_slider, self.bound2_slider)
self.bound1_slider.description = "Lower bound:"
self.bound2_slider.description = "Upper bound:"
def _on_prob_type_change(self, change):
"""Handle probability type change"""
self._update_slider_visibility() # Update which sliders are shown
# If empty, set bounds off-screen and hide shaded region
if self.prob_type_dropdown.value == "":
# Set bounds to very negative and very positive values to keep them off-screen
self.bound1_slider.value = -1e10
self.bound2_slider.value = 1e10
# Hide shaded region when no prob_type is selected
self.show_shaded_region_flag = False
else:
# When changing to a non-empty type, automatically show shaded region
self.show_shaded_region_flag = True
# When changing to a non-empty type, reset bounds to default range
# Check if previous value was empty (changing from empty to non-empty)
if change.get('old') == "":
# Reset bounds_interacted so bounds get set to default range
self.bounds_interacted = False
# Update bound sliders to set proper ranges and default values
# This will set bounds to full range (x_min to x_max) for the distribution
self._update_bound_sliders(reset_to_full_range=True)
if len(self.samples) > 0 or self.show_pdf_flag:
self._update_plot()
def _on_draw_clicked(self, button):
"""Handle draw samples button with progressive animation"""
dist_type = self.dist_dropdown.value
dist_category = self.category_dropdown.value
n_total = self.n_samples_slider.value
# Get parameters
params = self._get_params_dict()
# Generate all samples at once
all_samples = sample_distribution(dist_type, dist_category, n_total, **params)
# Progressive visualization
self.status_html.value = "Generating samples..."
# Preserve PDF visibility if the student already turned it on
keep_pdf = self.show_pdf_flag
sample_index = 0
batch_count = 0
while sample_index < n_total:
batch_size = determine_batch_size(sample_index)
end_index = min(sample_index + batch_size, n_total)
# Get samples up to current index
self.samples = all_samples[:end_index]
# Update bound sliders on first batch
if sample_index == 0:
# If prob_type is not empty, reset bounds to full range for the distribution
if self.prob_type_dropdown.value != "":
self.bounds_interacted = False
self._update_bound_sliders(reset_to_full_range=True)
else:
self._update_bound_sliders()
self.show_pdf_button.disabled = False
self.show_pdf_flag = keep_pdf
self.show_pdf_button.description = "Hide PDF/PMF" if keep_pdf else "Show PDF/PMF"
# Automatically show shaded region if prob_type is selected
if self.prob_type_dropdown.value != "":
self.show_shaded_region_flag = True
else:
self.show_shaded_region_flag = False
# Update slider visibility based on current prob_type
self._update_slider_visibility()
# Update plot less frequently to speed up animation
# Update every batch for first 100 samples, then every 2 batches
should_update_plot = (sample_index < 100) or (batch_count % 2 == 0)
if should_update_plot:
self._update_plot()
# Always update status
self.status_html.value = f"Generated {end_index} / {n_total} samples"
# Constant speed: every 1000 samples takes 2 seconds
# Delay is proportional to batch size
delay = batch_size / 500.0
time.sleep(delay)
sample_index = end_index
batch_count += 1
# Final update with all samples
self.samples = all_samples
self._update_bound_sliders() # Update sliders to match full sample range
self._update_plot()
self.status_html.value = f"Complete! Generated {n_total} samples."
def _get_theoretical_x_range(self, params=None):
"""Return a reasonable x-range covering most of the current distribution's mass."""
dist_type = self.dist_dropdown.value
if params is None:
params = self._get_params_dict()
if dist_type == "Uniform":
low = params.get('low', 0)
high = params.get('high', 1)
return float(min(low, high)), float(max(low, high))
if dist_type == "Exponential":
scale = params.get('scale', 1)
return 0.0, float(max(scale * 5, 1))
if dist_type == "Pareto":
scale = params.get('scale', 1.0)
return float(scale), float(scale + 5.0)
if dist_type == "Beta":
return 0.0, 1.0
if dist_type == "Gamma":
shape = params.get('shape', 2)
scale = params.get('scale', 1)
return 0.0, float(max(shape * scale * 5, 1))
if dist_type == "Normal":
mean = params.get('mean', 0)
std = max(params.get('std', 1), 1e-6)
return float(mean - 4 * std), float(mean + 4 * std)
if dist_type == "Bernoulli":
return 0, 1
if dist_type == "Geometric":
p = max(params.get('p', 0.5), 0.01)
return 1, int(max(15, stats.geom.ppf(0.99, p)))
if dist_type == "Binomial":
return 0, int(params.get('n', 10))
if dist_type == "Poisson":
lam = params.get('lam', 5)
return 0, int(max(lam * 3, lam + 4 * np.sqrt(lam) + 1, 5))
if dist_type == "Hypergeometric":
ngood = int(params.get('ngood', 10))
nsample = int(params.get('nsample', 10))
return 0, min(nsample, ngood)
return -5.0, 5.0
def _get_x_axis_range(self):
"""Get the x-axis range for the plot (used by both plot and sliders).
When PDF/PMF is shown, the range is the union of the sample extent and the
current theoretical support so neither leaves the window after parameter changes.
"""
dist_type = self.dist_dropdown.value
dist_category = self.category_dropdown.value
params = self._get_params_dict()
theo_min, theo_max = self._get_theoretical_x_range(params)
if len(self.samples) > 0:
sample_min = float(np.min(self.samples))
sample_max = float(np.max(self.samples))
if self.show_pdf_flag:
# Fit both samples and the updated theoretical PDF/PMF
pad = 1.0 if dist_category == "Discrete" else 0.5
x_min = min(sample_min, theo_min) - pad
x_max = max(sample_max, theo_max) + pad
elif dist_type == "Pareto":
x_min = theo_min
x_max = theo_max
else:
pad = 1.0 if dist_category == "Discrete" else 1.0
x_min = sample_min - pad
x_max = sample_max + pad
else:
x_min, x_max = theo_min, theo_max
if dist_category == "Discrete":
x_min = max(int(np.floor(x_min)), 0) if dist_type != "Bernoulli" else 0
if dist_type == "Bernoulli":
x_min, x_max = 0, 1
elif dist_type == "Binomial":
x_min = 0
x_max = int(max(x_max, params.get('n', 10)))
else:
x_max = int(np.ceil(x_max))
if not self.show_pdf_flag and dist_type not in ["Poisson", "Bernoulli", "Binomial"]:
range_padding = max(int((x_max - x_min) * 0.2), 3)
x_max = x_max + range_padding
if x_min >= x_max:
x_max = x_min + 1
return x_min, x_max
def _update_bound_sliders(self, reset_to_full_range=False):
"""Update bound slider ranges to match the plot's x-axis range"""
# If prob_type is empty, set bounds off-screen and return early
if self.prob_type_dropdown.value == "":
# Set bounds to very negative and very positive values to keep them off-screen
# Expand range first to avoid conflicts
wide_min = min(self.bound1_slider.min, self.bound2_slider.min, -1e10)
wide_max = max(self.bound1_slider.max, self.bound2_slider.max, 1e10)
self.bound1_slider.max = wide_max
self.bound2_slider.max = wide_max
self.bound1_slider.min = wide_min
self.bound2_slider.min = wide_min
self.bound1_slider.value = -1e10
self.bound2_slider.value = 1e10
return
dist_type = self.dist_dropdown.value
dist_category = self.category_dropdown.value
x_min, x_max = self._get_x_axis_range()
# Ensure valid range
if x_min >= x_max:
x_max = x_min + 1
# Determine step size
if dist_category == "Discrete":
step = 1
else:
step = 0.1
# If bounds haven't been interacted with or reset requested, set to full range
# To avoid TraitError when updating slider min/max, we need to handle the order carefully
# First, expand the range to accommodate all possible values, then set the final range
# Temporarily expand the range to a very wide interval to avoid conflicts
wide_min = min(x_min, self.bound1_slider.min, self.bound2_slider.min, -1e10)
wide_max = max(x_max, self.bound1_slider.max, self.bound2_slider.max, 1e10)
# Set to wide range first (order: max first, then min to avoid min > max)
self.bound1_slider.max = wide_max
self.bound2_slider.max = wide_max
self.bound1_slider.min = wide_min
self.bound2_slider.min = wide_min
if reset_to_full_range or not self.bounds_interacted:
# Set to full range so entire histogram is blue (all selected)
self.bound1_slider.value = x_min
self.bound2_slider.value = x_max
else:
# Clamp slider values to new range
self.bound1_slider.value = max(x_min, min(x_max, self.bound1_slider.value))
self.bound2_slider.value = max(x_min, min(x_max, self.bound2_slider.value))
# Now set the actual min, max, and step (order: min first since we're shrinking from wide range)
self.bound1_slider.min = x_min
self.bound1_slider.max = x_max
self.bound1_slider.step = step
self.bound2_slider.min = x_min
self.bound2_slider.max = x_max
self.bound2_slider.step = step
def _draw_samples(self):
"""Draw new samples"""
dist_type = self.dist_dropdown.value
dist_category = self.category_dropdown.value
n_samples = self.n_samples_slider.value
# Get parameters
params = {}
if dist_type in self.param_widgets:
widgets_list = self.param_widgets[dist_type]
if dist_type == "Uniform":
params['low'] = widgets_list[0].value
params['high'] = widgets_list[1].value
elif dist_type == "Exponential":
params['scale'] = widgets_list[0].value
elif dist_type == "Pareto":
params['shape'] = widgets_list[0].value
params['scale'] = widgets_list[1].value
elif dist_type == "Beta":
params['alpha'] = widgets_list[0].value
params['beta'] = widgets_list[1].value
elif dist_type == "Gamma":
params['shape'] = widgets_list[0].value
params['scale'] = widgets_list[1].value
elif dist_type == "Normal":
params['mean'] = widgets_list[0].value
params['std'] = widgets_list[1].value
elif dist_type == "Bernoulli":
params['p'] = widgets_list[0].value
elif dist_type == "Geometric":
params['p'] = widgets_list[0].value
elif dist_type == "Binomial":
params['n'] = widgets_list[0].value
params['p'] = widgets_list[1].value
elif dist_type == "Poisson":
params['lam'] = widgets_list[0].value
elif dist_type == "Hypergeometric":
params['ngood'] = widgets_list[0].value
params['nbad'] = widgets_list[1].value
params['nsample'] = widgets_list[2].value
self.samples = sample_distribution(dist_type, dist_category, n_samples, **params)
def _get_params_dict(self):
"""Get current parameters as dictionary"""
dist_type = self.dist_dropdown.value
params = {}
if dist_type in self.param_widgets:
widgets_list = self.param_widgets[dist_type]
if dist_type == "Uniform":
params['low'] = widgets_list[0].value
params['high'] = widgets_list[1].value
elif dist_type == "Exponential":
params['scale'] = widgets_list[0].value
elif dist_type == "Pareto":
params['shape'] = widgets_list[0].value
params['scale'] = widgets_list[1].value
elif dist_type == "Beta":
params['alpha'] = widgets_list[0].value
params['beta'] = widgets_list[1].value
elif dist_type == "Gamma":
params['shape'] = widgets_list[0].value
params['scale'] = widgets_list[1].value
elif dist_type == "Normal":
params['mean'] = widgets_list[0].value
params['std'] = widgets_list[1].value
elif dist_type == "Bernoulli":
params['p'] = widgets_list[0].value
elif dist_type == "Geometric":
params['p'] = widgets_list[0].value
elif dist_type == "Binomial":
params['n'] = widgets_list[0].value
params['p'] = widgets_list[1].value
elif dist_type == "Poisson":
params['lam'] = widgets_list[0].value
elif dist_type == "Hypergeometric":
params['ngood'] = widgets_list[0].value
params['nbad'] = widgets_list[1].value
params['nsample'] = widgets_list[2].value
return params
def _show_blank_plot(self):
"""Show blank plot when distribution changes"""
with self.plot_output:
clear_output(wait=True)
# Create empty figure (single plot)
fig = go.Figure()
# Set default axis ranges for blank plot from current distribution support
y_title = "P(X = x)" if self.category_dropdown.value == "Discrete" else "Density"
x_min, x_max = self._get_theoretical_x_range()
if x_min >= x_max:
x_max = x_min + 1
fig.update_xaxes(title_text="x", range=[x_min, x_max])
fig.update_yaxes(title_text=y_title, range=[0, 1])
fig.update_layout(height=600, showlegend=True, title="Select Show PDF/PMF or Draw Samples")
fig.show()
# Reset probability label
self.prob_label.value = '<div style="font-size: 18px; padding: 10px; background-color: #f0f0f0; border: 2px solid #333; border-radius: 5px;"><b>Estimated Probability:</b> <span style="color: #0066cc; font-size: 20px; font-weight: bold;">N/A</span><br><b>True Probability:</b> <span style="color: #cc6600; font-size: 20px; font-weight: bold;">N/A</span></div>'
def _update_plot(self, change=None):
"""Update the plot with histogram and/or overlaid PDF/PMF"""
# Allow PDF/PMF-only view before any samples are drawn
if len(self.samples) == 0 and not self.show_pdf_flag:
self._show_blank_plot()
return
# Use instance flags for display options
show_pdf = self.show_pdf_flag
show_shaded_region = self.show_shaded_region_flag
with self.plot_output:
clear_output(wait=True)
dist_type = self.dist_dropdown.value
dist_category = self.category_dropdown.value
prob_type = self.prob_type_dropdown.value
params = self._get_params_dict()
# Determine bounds based on probability type
bound1 = self.bound1_slider.value
bound2 = self.bound2_slider.value
# Create single figure (no subplots)
fig = go.Figure()
# Determine x range (use same method as sliders to ensure they match)
x_min, x_max = self._get_x_axis_range()
# For discrete, ensure we cover integer values
if dist_category == "Discrete":
x_range = np.arange(int(x_min), int(x_max) + 1)
else:
x_range = np.linspace(x_min, x_max, 500)
# Compute PDF/PMF if needed (will overlay on histogram)
pdf_pmf_values = None
if show_pdf:
pdf_pmf_values = compute_pdf_pmf(x_range, dist_type, dist_category, **params)
# Create histogram first (as base layer)
if len(self.samples) > 0:
if dist_category == "Discrete":
# For discrete, use integer bins
unique_vals, counts = np.unique(self.samples, return_counts=True)
counts = counts / len(self.samples) # Normalize to probability
# Only show red highlighting when shaded region is shown and prob_type is not empty
if show_shaded_region and prob_type != "":
# Determine which values are in the selected region (inclusive bounds)
if prob_type == "of outcome":
selected_mask = unique_vals == int(np.round(bound1))
elif prob_type == "under upper bound":
selected_mask = unique_vals <= bound2 # Inclusive upper bound
elif prob_type == "above lower bound":
selected_mask = unique_vals >= bound1 # Inclusive lower bound
elif prob_type == "in interval":