-
Notifications
You must be signed in to change notification settings - Fork 0
/
P_atlaset_x_of_n.py
executable file
·61 lines (50 loc) · 1.71 KB
/
P_atlaset_x_of_n.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /usr/bin/env python
#coding: utf-8
help_info = '''
this program calculate probablity of at least x of n particular events happend. And n is the subset of N
x is the subset of M.
Usage:
./P_atleast_x_of_n -x int -n int -M int -N int
'''
from math import factorial
from scipy.stats import hypergeom
import argparse
parser = argparse.ArgumentParser(description="this program calculate " +\
"probability of at least x of n particular events happend. And n "+\
"is the subset of N, x is the subset of M.")
parser.add_argument('x',metavar='x',type=int,help="x is the particular events number of n ")
parser.add_argument('n',metavar='n',type=int,help="n is the picked put events out of N")
parser.add_argument('M',metavar='M',type=int,help="M is the particular events number of N ")
parser.add_argument('N',metavar='N',type=int,help="N is the total probably space event number ")
def verify(x,n,M,N):
try:
assert N>M
except:
sys.stderr.write("N (%d) is suppose bigger than M (%d)"%(N,M))
sys.exit()
try:
assert N>n
except:
sys.stderr.write("N (%d) is suppose bigger than n (%d)"%(N,n))
sys.exit()
try:
assert n>x
except:
sys.stderr.write("n (%d) is suppose bigger than x (%d)"%(n,x))
sys.exit()
try:
assert M>x
except:
sys.stderr.write("M (%d) is suppose bigger than x (%d)"%(M,x))
sys.exit()
def main():
args = parser.parse_args()
x,n,M,N = args.x,args.n,args.M,args.N
verify(x,n,M,N)
total_probablity = 0
for i in range(x,min(n,M)+1,1):
probablity = hypergeom.pmf(i,N,M,n)
total_probablity += probablity
print total_probablity
if __name__=="__main__":
main()