Spaces:
Running
Running
changes
Browse files
probability/11_expectation.py
CHANGED
@@ -90,7 +90,7 @@ def _(np, plt):
|
|
90 |
xy=(exp_num_rolls, exp_running_avg[-1]), xytext=(exp_num_rolls/3, 4),
|
91 |
arrowprops=dict(facecolor='black', shrink=0.05, width=1.5))
|
92 |
|
93 |
-
plt.gca()
|
94 |
return exp_die_rolls, exp_num_rolls, exp_running_avg
|
95 |
|
96 |
|
@@ -506,16 +506,12 @@ def _(
|
|
506 |
# Plot the expected value function
|
507 |
dist_ax.plot(param_values, expected_values, 'b-', linewidth=2, label="Expected Value Function")
|
508 |
|
509 |
-
# Mark the current parameter value
|
510 |
dist_ax.plot(current_param, current_expected, 'ro', markersize=10, label=f"Current Value: E[X] = {current_expected}")
|
511 |
|
512 |
-
# Add a horizontal line from y-axis to the current point
|
513 |
dist_ax.hlines(current_expected, param_values[0], current_param, colors='r', linestyles='dashed')
|
514 |
|
515 |
-
# Add a vertical line from x-axis to the current point
|
516 |
dist_ax.vlines(current_param, 0, current_expected, colors='r', linestyles='dashed')
|
517 |
|
518 |
-
# Add shaded area under the curve
|
519 |
dist_ax.fill_between(param_values, 0, expected_values, alpha=0.2, color='blue')
|
520 |
|
521 |
dist_ax.set_xlabel(x_label, fontsize=12)
|
@@ -531,7 +527,6 @@ def _(
|
|
531 |
dist_ax.text(0.02, 0.95, formula, transform=dist_ax.transAxes, fontsize=12,
|
532 |
verticalalignment='top', bbox=dist_props)
|
533 |
|
534 |
-
# Set reasonable y-axis limits based on the distribution
|
535 |
if dist_selection.value == "geometric":
|
536 |
max_y = min(50, 2/max(0.01, param_values[0]))
|
537 |
dist_ax.set_ylim(0, max_y)
|
@@ -540,7 +535,6 @@ def _(
|
|
540 |
else:
|
541 |
dist_ax.set_ylim(0, max(expected_values) * 1.1)
|
542 |
|
543 |
-
# Add annotations for current value
|
544 |
annotation_x = current_param + (param_values[-1] - param_values[0]) * 0.05
|
545 |
annotation_y = current_expected
|
546 |
|
@@ -609,7 +603,6 @@ def _(np, plt, stats):
|
|
609 |
skew_expected = skew_n * skew_p
|
610 |
skew_expected_rounded = round(skew_expected, 2)
|
611 |
|
612 |
-
# Create the plot
|
613 |
skew_fig, skew_ax = plt.subplots(figsize=(10, 5))
|
614 |
skew_ax.bar(skew_x_values, skew_pmf_values, alpha=0.7, width=0.4)
|
615 |
|
@@ -619,15 +612,12 @@ def _(np, plt, stats):
|
|
619 |
skew_ax.axvline(x=skew_expected, color='r', linestyle='--', linewidth=2,
|
620 |
label=f'Expected Value = {skew_expected_rounded} (Mean)')
|
621 |
|
622 |
-
# Add annotations to highlight the difference
|
623 |
skew_ax.annotate('Mode', xy=(skew_mode, 0.05), xytext=(skew_mode-2.0, 0.1),
|
624 |
arrowprops=dict(facecolor='green', shrink=0.05, width=1.5), color='green')
|
625 |
skew_ax.annotate('Expected Value', xy=(skew_expected, 0.05), xytext=(skew_expected+1, 0.15),
|
626 |
arrowprops=dict(facecolor='red', shrink=0.05, width=1.5), color='red')
|
627 |
|
628 |
-
# Highlight the difference between mode and expected value
|
629 |
if skew_mode != int(skew_expected):
|
630 |
-
# Add a shaded region between mode and expected value
|
631 |
min_x = min(skew_mode, skew_expected)
|
632 |
max_x = max(skew_mode, skew_expected)
|
633 |
skew_ax.axvspan(min_x, max_x, alpha=0.2, color='purple')
|
@@ -685,9 +675,8 @@ def _(mo):
|
|
685 |
|
686 |
1. **Finance**: Expected return on investments, option pricing
|
687 |
2. **Insurance**: Setting premiums based on expected claims
|
688 |
-
3. **
|
689 |
-
4. **
|
690 |
-
5. **Operations Research**: Decision making under uncertainty
|
691 |
"""
|
692 |
)
|
693 |
return
|
|
|
90 |
xy=(exp_num_rolls, exp_running_avg[-1]), xytext=(exp_num_rolls/3, 4),
|
91 |
arrowprops=dict(facecolor='black', shrink=0.05, width=1.5))
|
92 |
|
93 |
+
plt.gca()
|
94 |
return exp_die_rolls, exp_num_rolls, exp_running_avg
|
95 |
|
96 |
|
|
|
506 |
# Plot the expected value function
|
507 |
dist_ax.plot(param_values, expected_values, 'b-', linewidth=2, label="Expected Value Function")
|
508 |
|
|
|
509 |
dist_ax.plot(current_param, current_expected, 'ro', markersize=10, label=f"Current Value: E[X] = {current_expected}")
|
510 |
|
|
|
511 |
dist_ax.hlines(current_expected, param_values[0], current_param, colors='r', linestyles='dashed')
|
512 |
|
|
|
513 |
dist_ax.vlines(current_param, 0, current_expected, colors='r', linestyles='dashed')
|
514 |
|
|
|
515 |
dist_ax.fill_between(param_values, 0, expected_values, alpha=0.2, color='blue')
|
516 |
|
517 |
dist_ax.set_xlabel(x_label, fontsize=12)
|
|
|
527 |
dist_ax.text(0.02, 0.95, formula, transform=dist_ax.transAxes, fontsize=12,
|
528 |
verticalalignment='top', bbox=dist_props)
|
529 |
|
|
|
530 |
if dist_selection.value == "geometric":
|
531 |
max_y = min(50, 2/max(0.01, param_values[0]))
|
532 |
dist_ax.set_ylim(0, max_y)
|
|
|
535 |
else:
|
536 |
dist_ax.set_ylim(0, max(expected_values) * 1.1)
|
537 |
|
|
|
538 |
annotation_x = current_param + (param_values[-1] - param_values[0]) * 0.05
|
539 |
annotation_y = current_expected
|
540 |
|
|
|
603 |
skew_expected = skew_n * skew_p
|
604 |
skew_expected_rounded = round(skew_expected, 2)
|
605 |
|
|
|
606 |
skew_fig, skew_ax = plt.subplots(figsize=(10, 5))
|
607 |
skew_ax.bar(skew_x_values, skew_pmf_values, alpha=0.7, width=0.4)
|
608 |
|
|
|
612 |
skew_ax.axvline(x=skew_expected, color='r', linestyle='--', linewidth=2,
|
613 |
label=f'Expected Value = {skew_expected_rounded} (Mean)')
|
614 |
|
|
|
615 |
skew_ax.annotate('Mode', xy=(skew_mode, 0.05), xytext=(skew_mode-2.0, 0.1),
|
616 |
arrowprops=dict(facecolor='green', shrink=0.05, width=1.5), color='green')
|
617 |
skew_ax.annotate('Expected Value', xy=(skew_expected, 0.05), xytext=(skew_expected+1, 0.15),
|
618 |
arrowprops=dict(facecolor='red', shrink=0.05, width=1.5), color='red')
|
619 |
|
|
|
620 |
if skew_mode != int(skew_expected):
|
|
|
621 |
min_x = min(skew_mode, skew_expected)
|
622 |
max_x = max(skew_mode, skew_expected)
|
623 |
skew_ax.axvspan(min_x, max_x, alpha=0.2, color='purple')
|
|
|
675 |
|
676 |
1. **Finance**: Expected return on investments, option pricing
|
677 |
2. **Insurance**: Setting premiums based on expected claims
|
678 |
+
3. **Machine Learning**: Loss function minimization
|
679 |
+
4. **Operations Research**: Decision making under uncertainty
|
|
|
680 |
"""
|
681 |
)
|
682 |
return
|