Skip to content

Instantly share code, notes, and snippets.

@sredman
Created February 15, 2019 07:26
Show Gist options
  • Save sredman/76252faeca87fcf12ad9bb271c689d63 to your computer and use it in GitHub Desktop.
Save sredman/76252faeca87fcf12ad9bb271c689d63 to your computer and use it in GitHub Desktop.
A quick statistics calculator over the number of items which fill a certain condition
#!/usr/bin/env python3
#
# Filename: calculator.py
#
# Author: Simon Redman <simon@ergotech.com>
# File Created: 14.02.2019
# Last Modified: Fri 15 Feb 2019 12:20:49 AM MST
# Description: Return some statistics about the number of items filling
# a specified condition
#
import argparse
from itertools import combinations
def probability(num_satisfying: int, num_total: int, ind_prob: float) -> float:
items = range(1, num_total + 1)
choices: int = len(list(combinations(items, num_satisfying)))
return choices * ind_prob ** num_satisfying * (1 - ind_prob) ** (num_total - num_satisfying)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Statistics calculator about the number of items filling a certain condition")
parser.add_argument("--probability", type=float, default=0.1,
help="Independent probability of the condition applying to each item (Default = 0.1)")
parser.add_argument("--num-items", type=int, required=True,
help="Total number of items being calculated over")
args = parser.parse_args()
items = range(1, args.num_items + 1)
expectation = 0
total_prob = 0
for i in items:
prob = probability(i, len(items), args.probability)
expectation += prob * i
total_prob += prob
print("Probability of exactly {k} items of {n} satisfying is {prob:.5}".format(
k = i,
n = args.num_items,
prob = prob,))
print("It is expected that {exp:.4} items of {n} will satisfy the condition".format(
exp = expectation,
n = args.num_items,))
print("The probability that at least one item satisfies the condition is {prb:.4}".format(
prb = total_prob,))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment