Skip to content

Instantly share code, notes, and snippets.

@jMyles
Created February 15, 2021 14:49
Show Gist options
  • Save jMyles/e1bde7a737512a6ebe437b0b97e3712d to your computer and use it in GitHub Desktop.
Save jMyles/e1bde7a737512a6ebe437b0b97e3712d to your computer and use it in GitHub Desktop.
A simple loop to calculate the combine hypergeometric distribution of repeated node selection to show a probability of selecting a malicious node
import math
def calculate_combined_probability_with_hypergeometric_distribution(total_population,
sample_size,
malicious_population,
failure_threshold):
failure_modes = {}
sample_coefficient = math.comb(total_population, sample_size)
for failure_sample in range(failure_threshold, sample_size + 1):
# We'll iterate through the possible samples which represent a failure and calculate the probably of that
# sample occuring in a hypergeometric distribution.
# The number of ways to choose among the malicious population for a malicious cohort of failure_sample size.
malicious_node_coefficient = math.comb(malicious_population, failure_sample)
# The number of ways to choose the benign population among the sample size that's not among a failure cohort-sized sample
selection_coefficient = math.comb(total_population - malicious_population, sample_size - failure_sample)
failure_modes[failure_sample] = (malicious_node_coefficient * selection_coefficient) / sample_coefficient
return failure_modes
failure_modes = calculate_combined_probability_with_hypergeometric_distribution(total_population=1000,
sample_size=100,
malicious_population=333,
failure_threshold=51
)
print(failure_modes)
combined_probability = sum(failure_modes.values())
print(combined_probability)
print("-----------------------------------------")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment