Skip to content

Instantly share code, notes, and snippets.

@alvaromuir
Created June 28, 2016 00:53
Show Gist options
  • Save alvaromuir/74914ae607a7d4765bdd96699a48db07 to your computer and use it in GitHub Desktop.
Save alvaromuir/74914ae607a7d4765bdd96699a48db07 to your computer and use it in GitHub Desktop.
#!/bin/python3
"""
returns possible combinatations in a list
"""
def combo(n,k):
import itertools
return list(itertools.combinations(list(range((n-(n-1)),(n+1))),k))
"""
returns a tuple representing entire sample space of a given DICT
"""
def sample_space(inputs):
rslt = []
for k in inputs:
rslt += k * inputs[k]
return(tuple(rslt))
"""
retuns number of outcomes of an event. requires sample space(ss) and
number times sample is taken
"""
def outcomes(ss, taken):
import math
n = len(ss)
return math.factorial(n) / (math.factorial(taken) * math.factorial(n - taken))
"""
retuns relative frequency (Na) for a given event. Requires event and population
"""
def relative_freq(pop, event):
rslts = 1
for k in event[1]:
rslts *= len(combo(pop[k],event[1][k]))
return(round(rslts, 3))
"""
retuns boolean value if first index of tuple matches total of second
"""
def validate_event(input):
ttl = 0
for k in input[1]:
ttl += input[1][k]
return(input[0] == ttl)
"""
return probability of an event
requires a population description DICT {desceet value: qty}
and event criteria TUPLE <num of event, {criteria:dict}
"""
def prob(pop, event):
if (validate_event(event)):
Na = relative_freq(pop,event)
N = outcomes(sample_space(pop), event[0])
return(round(Na/N, 3))
else:
print("Error: Please check event input")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment