联合,边际和条件概率计算

时间:2017-04-16 19:50:17

标签: python python-2.7 probability

我需要编写一个Python程序来解决联合,边缘或条件概率。我知道关于贝叶斯定理的所有事情。我知道在Python 3中有包可以做到这一点,但我使用的是Python 2.7。 Python 2.7中是否有任何用于概率计算的包?我自己写了一个程序,但它很慢,因为我有很多变量!你知道如何有效地编写我的程序吗? :(

示例输入文件:

P(P = + | X = +)
******
A
0.0 
***
B | A
0.8 -
0.4 +
***
C | B
0.5 -
0.1 +
***
D | C
1.0 -
0.1 +
***
E | D
0.8 -
0.4 +
***
F | E
0.5 -
0.9 +
***
G | F
0.5 -
0.7 +
***
H | G
0.3 -
0.5 +
***
I | H
0.7 -
0.6 +
***
J | I
1.0 -
0.6 +
***
K | J
0.6 -
0.8 +
***
L | K
0.8 -
0.2 +
***
M | L
0.9 -
0.0 +
***
N | M
0.5 -
0.9 +
***
O | N
0.4 -
0.4 +
***
P | O
0.1 -
0.4 +
***
Q | P
0.5 -
0.6 +
***
R | Q
1.0 -
0.0 +
***
S | R
0.7 -
0.9 +
***
T | S
0.5 -
0.1 +
***
U | T
0.3 -
0.2 +
***
V | U
0.1 -
0.8 +
***
W | V
0.4 -
0.9 +
*** 
X | W
0.2 -
0.5 +
***
Y | X
0.1 -
0.5 +
***
Z | Y
0.3 -
0.7 +
***
AA | Z
0.3 -
0.4 +
***
AB | AA
0.6 -
0.2 +
***
AC | AB
0.7 -
0.1 +
***
AD | AC
0.9 -
1.0 +
***
AE | AD
0.7 -
0.4 +
***
AF | AE
0.7 -
0.6 +

我的基本代码:

def solve_all_asigned(self,joint_s):
    mult_val=1
    for tab in self.tables:
        if tab['decision']:
            continue
        name=tab['name']
        nr_spd=name.replace('.', ' ', 1).split()
        val=''
        check_list=[x in joint_s.keys() for x in nr_spd]
        if False in check_list:
            continue
        val=''.join(map(joint_s.get,nr_spd))
        mult_val=mult_val*tab[val]
    return mult_val

def solve_joint_probability(self,joint_s):
    if 'nan' not in joint_s.values():
        ss=self.solve_all_asigned(joint_s)
    else:
        s=[]
        j_set=[x for x in joint_s.keys() if joint_s[x]!='nan']
        accum_set=set()
        total_set=set()
        decision_vars=[]
        for tab in self.tables:
            if tab['decision']:
                decision_vars.append(tab['name'])
        var=True
        while var:
            var=False
            for x in j_set:
                if x in self.dependencies.keys() and x not in total_set:
                    accum_set.update(self.dependencies[x])
                    var=True
                elif x in decision_vars:
                    for dep in self.dependencies.keys():
                        if x in self.dependencies[dep] and x not in total_set:
                            accum_set.add(dep)
                            accum_set.update(self.dependencies[dep])
                            var=True                   
            total_set.update(j_set)
            j_set=accum_set
            accum_set=set()
        nan_set=[x for x in total_set if joint_s[x]=='nan']
        n=len(nan_set)
        if n>13:
            return self.eliminate_variables(joint_s,nan_set)
        joint_s2={}
        if n>20:
            print('please wait .it may take some minutes ...')
        for binlist in itertools.product([0,1],repeat=n):
            a=['+-'[u] for u in binlist]
            joint_s2=dict(zip(nan_set,a))
            for key in joint_s.keys():
                if joint_s[key] in '-+':
                    joint_s2[key]=joint_s[key]
            s.append(self.solve_all_asigned(joint_s2))
        if n>20:
            print('finished ...')
        ss=sum(s)
    return ss

0 个答案:

没有答案