Last active
June 19, 2017 11:11
-
-
Save florianherrengt/49029989298bf068b1514f85e1275c7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import random | |
from statistics import mean | |
variables = [3, 5, 7, 10, 12, 13] | |
weight = [.1, .05, .05, .2, .4, .2] | |
if sum(weight) != 1: | |
raise 'sum(weight) != 1' | |
range_of_weight = [sum(weight[0:index + 1]) for index, w in enumerate(weight)] | |
def get_random_variable(): | |
random_number = random() | |
selected = None | |
for index, w in enumerate(range_of_weight): | |
if random_number <= w: | |
return variables[index] | |
exact_expected_value = sum([w * v for w, v in zip(weight, variables)]) | |
print('exact_expected_value', exact_expected_value) | |
average_expected_value = mean([get_random_variable() for _ in range(1000000)]) | |
print('average_expected_value', average_expected_value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment