Created
July 25, 2022 16:10
-
-
Save patrickbucher/b44723fd475e5fdf9e2219c2dadd7807 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
#!/usr/bin/env python3 | |
from functools import reduce | |
from operator import mul | |
# the probability that it will rain in the next hour | |
rain_probs = { | |
1200: 0.05, | |
1300: 0.05, | |
1400: 0.05, | |
1500: 0.05, | |
1600: 0.06, | |
1700: 0.09, | |
1800: 0.13, | |
1900: 0.18, | |
2000: 0.22, | |
2100: 0.26, | |
2200: 0.31, | |
2300: 0.35, | |
} | |
# inverse: probability that it won't rain in the next hour | |
no_rain_probs = {h: 1.0 - p for h, p in rain_probs.items()} | |
print(no_rain_probs) | |
# accumulated: probability that it never rains until the next hour | |
acc_p = 1.0 | |
no_rain_acc_probs = {} | |
for hour, prob in no_rain_probs.items(): | |
acc_p *= prob | |
no_rain_acc_probs[hour] = acc_p | |
print(no_rain_acc_probs) | |
# same as above, but calculated in a function | |
def calc_acc_probs(prob_dict): | |
acc_p = 1.0 | |
acc_probs = {} | |
for k, p in prob_dict.items(): | |
acc_p *= p | |
acc_probs[k] = acc_p | |
return acc_probs | |
print(calc_acc_probs(no_rain_probs)) | |
# same as above, but using a functional style | |
probs = list(reduce(lambda l, r: l + [l[-1]*r], no_rain_probs.values(), [1.0])[1:]) | |
no_rain_acc_probs = dict(zip(no_rain_probs.keys(), probs)) | |
print(no_rain_acc_probs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment