概率图模型 Assignment 2 Issue 3

PGM Assignment 2

Question 3

Now we have four binary variables A, B, C, D, and two cases of their joint distribution.

Notice that in TABLE 2, we only give you an unnormalized probability distribution. But you can transfrom these to a normalized distribution by dividing the sum of all unnormalized Ps.

Check all independent and conditionally independent relations among A, B, C, D. (Hint: do you need a normalized distribution for this problem?)

Table 1: Joint distribution of A, B, C, D.

A B C D P
0 0 0 0 0.0096
0 0 0 1 0.0144
0 0 1 0 0.0224
0 0 1 1 0.0336
0 1 0 0 0.004
0 1 0 1 0.004
0 1 1 0 0.006
0 1 1 1 0.006
1 0 0 0 0.216
1 0 0 1 0.144
1 0 1 0 0.216
1 0 1 1 0.144
1 1 0 0 0.0756
1 1 0 1 0.0324
1 1 1 0 0.0504
1 1 1 1 0.0216

Table 2: Joint distribution of A, B, C, D.

A B C D Unnormalized P
0 0 0 0 360000
0 0 0 1 600
0 0 1 0 36000
0 0 1 1 3600
0 1 0 0 600
0 1 0 1 100000
0 1 1 0 60
0 1 1 1 600000
1 0 0 0 6000
1 0 0 1 10
1 0 1 0 900
1 0 1 1 90
1 1 0 0 600
1 1 0 1 100000
1 1 1 0 90
1 1 1 1 900000

Solution

关于记号的注解:

\(x0\) 代表 \(x=0\)

\(x0y0\) 代表事件 \((x=0, y=0)\)

Table 1

涉及浮点数的数值比较精确到小数点后4位

1
2
import pandas as pd
df = pd.read_csv('./data_1.txt',sep='\t')

单个事件概率

1
prob_dict={}
1
2
3
4
for i in ['a','b','c','d']:
for j in [0, 1]:
print("P(%s=%d)=%.1f" % (i,j, sum(df[(df[i]==j)]['p'])))
prob_dict[i+str(j)]=round(sum(df[(df[i]==j)]['p']),2)
1
2
3
4
5
6
7
8
P(a=0)=0.1
P(a=1)=0.9
P(b=0)=0.8
P(b=1)=0.2
P(c=0)=0.5
P(c=1)=0.5
P(d=0)=0.6
P(d=1)=0.4

所有组合

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
for a in [0.5, 0, 1]:
for b in [0.5, 0, 1]:
for c in [0.5, 0, 1]:
for d in [0.5, 0, 1]:
p_sum = round(sum(df[(df['a']!=a) & (df['b']!=b) & \
(df['c']!=c) & (df['d']!=d)]['p']), 4)
item = ''
#### a
if a == 0.5:
pass
else:
if a == 0:
a_value = '1'
elif a == 1:
a_value = '0'
item += 'a' + a_value

#### b
if b == 0.5:
pass
else:
if b == 0:
b_value = '1'
elif b == 1:
b_value = '0'
item += 'b' + b_value

#### c
if c == 0.5:
pass
else:
if c == 0:
c_value = '1'
elif c == 1:
c_value = '0'
item += 'c' + c_value

#### d
if d == 0.5:
pass
else:
if d == 0:
d_value = '1'
elif d == 1:
d_value = '0'
item += 'd' + d_value

if item == "":
continue
else:
prob_dict[item] = p_sum
1
2
for ele in prob_dict.keys():
print("P(%s) = %.4f" % (ele, prob_dict[ele]))
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
P(a0) = 0.1000
P(a1) = 0.9000
P(b0) = 0.8000
P(b1) = 0.2000
P(c0) = 0.5000
P(c1) = 0.5000
P(d0) = 0.6000
P(d1) = 0.4000
P(c1d1) = 0.2052
P(c1d0) = 0.2948
P(c0d1) = 0.1948
P(c0d0) = 0.3052
P(b1d1) = 0.0640
P(b1d0) = 0.1360
P(b1c1) = 0.0840
P(b1c1d1) = 0.0276
P(b1c1d0) = 0.0564
P(b1c0) = 0.1160
P(b1c0d1) = 0.0364
P(b1c0d0) = 0.0796
P(b0d1) = 0.3360
P(b0d0) = 0.4640
P(b0c1) = 0.4160
P(b0c1d1) = 0.1776
P(b0c1d0) = 0.2384
P(b0c0) = 0.3840
P(b0c0d1) = 0.1584
P(b0c0d0) = 0.2256
P(a1d1) = 0.3420
P(a1d0) = 0.5580
P(a1c1) = 0.4320
P(a1c1d1) = 0.1656
P(a1c1d0) = 0.2664
P(a1c0) = 0.4680
P(a1c0d1) = 0.1764
P(a1c0d0) = 0.2916
P(a1b1) = 0.1800
P(a1b1d1) = 0.0540
P(a1b1d0) = 0.1260
P(a1b1c1) = 0.0720
P(a1b1c1d1) = 0.0216
P(a1b1c1d0) = 0.0504
P(a1b1c0) = 0.1080
P(a1b1c0d1) = 0.0324
P(a1b1c0d0) = 0.0756
P(a1b0) = 0.7200
P(a1b0d1) = 0.2880
P(a1b0d0) = 0.4320
P(a1b0c1) = 0.3600
P(a1b0c1d1) = 0.1440
P(a1b0c1d0) = 0.2160
P(a1b0c0) = 0.3600
P(a1b0c0d1) = 0.1440
P(a1b0c0d0) = 0.2160
P(a0d1) = 0.0580
P(a0d0) = 0.0420
P(a0c1) = 0.0680
P(a0c1d1) = 0.0396
P(a0c1d0) = 0.0284
P(a0c0) = 0.0320
P(a0c0d1) = 0.0184
P(a0c0d0) = 0.0136
P(a0b1) = 0.0200
P(a0b1d1) = 0.0100
P(a0b1d0) = 0.0100
P(a0b1c1) = 0.0120
P(a0b1c1d1) = 0.0060
P(a0b1c1d0) = 0.0060
P(a0b1c0) = 0.0080
P(a0b1c0d1) = 0.0040
P(a0b1c0d0) = 0.0040
P(a0b0) = 0.0800
P(a0b0d1) = 0.0480
P(a0b0d0) = 0.0320
P(a0b0c1) = 0.0560
P(a0b0c1d1) = 0.0336
P(a0b0c1d0) = 0.0224
P(a0b0c0) = 0.0240
P(a0b0c0d1) = 0.0144
P(a0b0c0d0) = 0.0096

2个元素独立性

1
2
3
4
5
6
7
for ele in prob_dict.keys():
if len(ele) == 4:
ele1 = ele[0:2]
ele2 = ele[2:4]
#print(ele1, ele2)
if prob_dict[ele] == round(prob_dict[ele1] * prob_dict[ele2], 4):
print("P(%s) = P(%s) * P(%s)" % (ele, ele1, ele2))
1
2
3
4
P(a1b1) = P(a1) * P(b1)
P(a1b0) = P(a1) * P(b0)
P(a0b1) = P(a0) * P(b1)
P(a0b0) = P(a0) * P(b0)

\[ \therefore \ A \ \bot \ B \]

3个元素独立性

1
2
3
4
5
6
7
8
9
10
11
12
13
def sort_var_name(_list):
tmp_list = _list
string = ""
for ele in tmp_list:
string += ele
tmp_list = []
for i in range(int(len(string)/2)):
tmp_list.append(string[2*i:2*i+2])
tmp_list.sort()
string = ""
for ele in tmp_list:
string += ele
return string
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
for ele in prob_dict.keys():
if len(ele) == 6:
ele1 = ele[0:2]
ele2 = ele[2:4]
ele3 = ele[4:6]
#print(ele1, ele2, ele3)

# 检查独立性
if prob_dict[ele] == round(prob_dict[ele1] * prob_dict[ele2] \
* prob_dict[ele3], 6):
print("P(%s) = P(%s) * P(%s) * P(%s)" % (ele, ele1, ele2, ele3))

# 检查条件独立性
# P(AB|C) 形式,条件元素个数为1
for i in range(3):
ele_list = [ele1, ele2, ele3]
cond = ele_list[i]
ele_list.pop(i)
test1 = ele_list[0]
test2 = ele_list[1]

prob_condition = round(prob_dict[ele], 4)
prob_test1 = prob_dict[sort_var_name([test1, cond])]
prob_test2 = prob_dict[sort_var_name([test2, cond])]

div = round(prob_test1 * prob_test2 / prob_dict[cond], 4)
if div == prob_condition:
print("P(%s, %s|%s) = P(%s|%s)*P(%s|%s)" % \
(test1, test2, cond, test1, cond, test2, cond))

结果

1
2
3
4
P(a1b0d1) = P(a1) * P(b0) * P(d1)
P(a1b0d0) = P(a1) * P(b0) * P(d0)
P(a1b0c1) = P(a1) * P(b0) * P(c1)
P(a1b0c0) = P(a1) * P(b0) * P(c0)

4个元素独立性

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
for ele in prob_dict.keys():
if len(ele) == 8:
ele1 = ele[0:2]
ele2 = ele[2:4]
ele3 = ele[4:6]
ele4 = ele[6:8]
#print(ele1, ele2, ele3, ele4)

# 检查条件独立性
# P(ABC|D) 形式,条件元素个数为1, 不考虑两个联合分布和另一个的条件独立性,
for i in range(4):
ele_list = [ele1, ele2, ele3, ele4]
cond = ele_list[i]
ele_list.pop(i)
test1 = ele_list[0]
test2 = ele_list[1]
test3 = ele_list[2]

prob_condition = round(prob_dict[ele], 4)
prob_test1 = prob_dict[sort_var_name([test1, cond])]
prob_test2 = prob_dict[sort_var_name([test2, cond])]
prob_test3 = prob_dict[sort_var_name([test3, cond])]

div = round(prob_test1 * prob_test2 * prob_test3 / pow(prob_dict[cond], 2), 4)
if div == prob_condition:
print("P(%s, %s, %s |%s) = P(%s|%s)*P(%s|%s)*P(%s|%s)" % \
(test1, test2, test3, cond, test1, cond, test2, cond, test3, cond))

# P(ABC|D) 形式,条件元素个数为1, 只考虑两个联合分布和另一个的条件独立性
for i in range(4):
ele_list = [ele1, ele2, ele3, ele4]
cond = ele_list[i]
ele_list.pop(i)

## 选出一个作为单独的那个元素
for j in range(3):
ele1_list = [_ele for _ele in ele_list]
test_single = ele1_list[j]
ele1_list.pop(j)
test_couple = sort_var_name(ele1_list)

prob_condition = round(prob_dict[ele], 4)
prob_test_single = prob_dict[sort_var_name([test_single, cond])]
prob_test_couple = prob_dict[sort_var_name([test_couple, cond])]

div = round(prob_test_single * prob_test_couple / prob_dict[cond], 4)

if div == prob_condition:
print("P(%s, %s|%s) = P(%s|%s)*P(%s|%s)" % \
(test_single, test_couple, cond,
test_single, cond, test_couple, cond))

# P(AB|CD) 形式,条件元素个数为2
for i in range(4):
for j in range(i+1, 4):
ele_list = [ele1, ele2, ele3, ele4]
cond = sort_var_name([ele_list[i], ele_list[j]])
a = [0, 1, 2, 3]
#ele_list.pop(i)
#ele_list.pop(j-1)

a.remove(i)
a.remove(j)
test1 = ele_list[a[0]]
test2 = ele_list[a[1]]

prob_condition = round(prob_dict[ele], 4)
prob_test1 = prob_dict[sort_var_name([test1, cond])]
prob_test2 = prob_dict[sort_var_name([test2, cond])]

div = round(prob_test1 * prob_test2 / prob_dict[cond], 4)
if div == prob_condition:
print("P(%s, %s|%s) = P(%s|%s)*P(%s|%s)" % \
(test1, test2, cond, test1, cond, test2, cond))

结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
P(c1, d1|a1b1) = P(c1|a1b1)*P(d1|a1b1)
P(c1, d0|a1b1) = P(c1|a1b1)*P(d0|a1b1)
P(c0, d1|a1b1) = P(c0|a1b1)*P(d1|a1b1)
P(c0, d0|a1b1) = P(c0|a1b1)*P(d0|a1b1)
P(c1, d1|a1b0) = P(c1|a1b0)*P(d1|a1b0)
P(c1, d0|a1b0) = P(c1|a1b0)*P(d0|a1b0)
P(c0, d1|a1b0) = P(c0|a1b0)*P(d1|a1b0)
P(c0, d0|a1b0) = P(c0|a1b0)*P(d0|a1b0)
P(c1, d1|a0b1) = P(c1|a0b1)*P(d1|a0b1)
P(c1, d0|a0b1) = P(c1|a0b1)*P(d0|a0b1)
P(c0, d1|a0b1) = P(c0|a0b1)*P(d1|a0b1)
P(c0, d0|a0b1) = P(c0|a0b1)*P(d0|a0b1)
P(c0, d0|a0b0) = P(c0|a0b0)*P(d0|a0b0)
P(c1, d0|a0b0) = P(c1|a0b0)*P(d0|a0b0)
P(c0, d1|a0b0) = P(c0|a0b0)*P(d1|a0b0)
P(c1, d1|a0b0) = P(c1|a0b0)*P(d1|a0b0)

\[ \therefore \ (C\ \bot\ D \ | \ A,\ B)\]

Table 2

1
2
import pandas as pd
df = pd.read_csv('./data_2.txt',sep='\t')

单个事件概率

原始数据经过标准化,涉及浮点数的数值比较精确到小数点后3位

1
prob_dict={}
1
2
3
4
for i in ['a','b','c','d']:
for j in [0, 1]:
print("P(%s=%d)=%.1f" % (i,j, sum(df[(df[i]==j)]['p'])))
prob_dict[i+str(j)]=round(sum(df[(df[i]==j)]['p']),2)
1
2
3
4
5
6
7
8
P(a=0)=0.5
P(a=1)=0.5
P(b=0)=0.2
P(b=1)=0.8
P(c=0)=0.3
P(c=1)=0.7
P(d=0)=0.2
P(d=1)=0.8

所有组合

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
for a in [0.5, 0, 1]:
for b in [0.5, 0, 1]:
for c in [0.5, 0, 1]:
for d in [0.5, 0, 1]:
p_sum = round(sum(df[(df['a']!=a) & (df['b']!=b) & \
(df['c']!=c) & (df['d']!=d)]['p']), 4)
item = ''
#### a
if a == 0.5:
pass
else:
if a == 0:
a_value = '1'
elif a == 1:
a_value = '0'
item += 'a' + a_value

#### b
if b == 0.5:
pass
else:
if b == 0:
b_value = '1'
elif b == 1:
b_value = '0'
item += 'b' + b_value

#### c
if c == 0.5:
pass
else:
if c == 0:
c_value = '1'
elif c == 1:
c_value = '0'
item += 'c' + c_value

#### d
if d == 0.5:
pass
else:
if d == 0:
d_value = '1'
elif d == 1:
d_value = '0'
item += 'd' + d_value

if item == "":
continue
else:
prob_dict[item] = p_sum
1
2
for ele in prob_dict.keys():
print("P(%s) = %.4f" % (ele, prob_dict[ele]))
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
P(a0) = 0.5221
P(a1) = 0.4779
P(b0) = 0.1931
P(b1) = 0.8069
P(c0) = 0.2693
P(c1) = 0.7307
P(d0) = 0.1917
P(d1) = 0.8083
P(c1d1) = 0.7131
P(c1d0) = 0.0176
P(c0d1) = 0.0951
P(c0d0) = 0.1741
P(b1d1) = 0.8062
P(b1d0) = 0.0006
P(b1c1) = 0.7115
P(b1c1d1) = 0.7114
P(b1c1d0) = 0.0001
P(b1c0) = 0.0954
P(b1c0d1) = 0.0949
P(b1c0d0) = 0.0006
P(b0d1) = 0.0020
P(b0d0) = 0.1911
P(b0c1) = 0.0193
P(b0c1d1) = 0.0018
P(b0c1d0) = 0.0175
P(b0c0) = 0.1739
P(b0c0d1) = 0.0003
P(b0c0d0) = 0.1736
P(a1d1) = 0.4743
P(a1d0) = 0.0036
P(a1c1) = 0.4273
P(a1c1d1) = 0.4269
P(a1c1d0) = 0.0005
P(a1c0) = 0.0506
P(a1c0d1) = 0.0474
P(a1c0d0) = 0.0031
P(a1b1) = 0.4746
P(a1b1d1) = 0.4743
P(a1b1d0) = 0.0003
P(a1b1c1) = 0.4269
P(a1b1c1d1) = 0.4268
P(a1b1c1d0) = 0.0000
P(a1b1c0) = 0.0477
P(a1b1c0d1) = 0.0474
P(a1b1c0d0) = 0.0003
P(a1b0) = 0.0033
P(a1b0d1) = 0.0000
P(a1b0d0) = 0.0033
P(a1b0c1) = 0.0005
P(a1b0c1d1) = 0.0000
P(a1b0c1d0) = 0.0004
P(a1b0c0) = 0.0029
P(a1b0c0d1) = 0.0000
P(a1b0c0d0) = 0.0028
P(a0d1) = 0.3340
P(a0d0) = 0.1881
P(a0c1) = 0.3034
P(a0c1d1) = 0.2863
P(a0c1d0) = 0.0171
P(a0c0) = 0.2187
P(a0c0d1) = 0.0477
P(a0c0d0) = 0.1710
P(a0b1) = 0.3323
P(a0b1d1) = 0.3320
P(a0b1d0) = 0.0003
P(a0b1c1) = 0.2846
P(a0b1c1d1) = 0.2846
P(a0b1c1d0) = 0.0000
P(a0b1c0) = 0.0477
P(a0b1c0d1) = 0.0474
P(a0b1c0d0) = 0.0003
P(a0b0) = 0.1898
P(a0b0d1) = 0.0020
P(a0b0d0) = 0.1878
P(a0b0c1) = 0.0188
P(a0b0c1d1) = 0.0017
P(a0b0c1d0) = 0.0171
P(a0b0c0) = 0.1710
P(a0b0c0d1) = 0.0003
P(a0b0c0d0) = 0.1707

2个元素独立性

1
2
3
4
5
6
7
for ele in prob_dict.keys():
if len(ele) == 4:
ele1 = ele[0:2]
ele2 = ele[2:4]
#print(ele1, ele2)
if prob_dict[ele] == round(prob_dict[ele1] * prob_dict[ele2], 3):
print("P(%s) = P(%s) * P(%s)" % (ele, ele1, ele2))

3个元素独立性

1
2
3
4
5
6
7
8
9
10
11
12
13
def sort_var_name(_list):
tmp_list = _list
string = ""
for ele in tmp_list:
string += ele
tmp_list = []
for i in range(int(len(string)/2)):
tmp_list.append(string[2*i:2*i+2])
tmp_list.sort()
string = ""
for ele in tmp_list:
string += ele
return string
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
for ele in prob_dict.keys():
if len(ele) == 6:
ele1 = ele[0:2]
ele2 = ele[2:4]
ele3 = ele[4:6]
#print(ele1, ele2, ele3)

# 检查独立性
if prob_dict[ele] == round(prob_dict[ele1] * prob_dict[ele2] \
* prob_dict[ele3], 6):
print("P(%s) = P(%s) * P(%s) * P(%s)" % (ele, ele1, ele2, ele3))

# 检查条件独立性
# P(AB|C) 形式,条件元素个数为1
for i in range(3):
ele_list = [ele1, ele2, ele3]
cond = ele_list[i]
ele_list.pop(i)
test1 = ele_list[0]
test2 = ele_list[1]

prob_condition = round(prob_dict[ele], 3)
prob_test1 = prob_dict[sort_var_name([test1, cond])]
prob_test2 = prob_dict[sort_var_name([test2, cond])]

div = round(prob_test1 * prob_test2 / prob_dict[cond], 3)
if div == prob_condition:
print("P(%s, %s|%s) = P(%s|%s)*P(%s|%s)" % \
(test1, test2, cond, test1, cond, test2, cond))

结果

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
P(b0,c0|d0) = P(b0|d0)*P(c0|d0)
P(b0,c1|d0) = P(b0|d0)*P(c1|d0)
P(b1,c0|d0) = P(b1|d0)*P(c0|d0)
P(b1,c1|d0) = P(b1|d0)*P(c1|d0)
P(b0,c0|d1) = P(b0|d1)*P(c0|d1)
P(b0,c1|d1) = P(b0|d1)*P(c1|d1)
P(b1,c0|d1) = P(b1|d1)*P(c0|d1)
P(b1,c1|d1) = P(b1|d1)*P(c1|d1)
-------------------------------
P(a0,d0|b0) = P(a0|b0)*P(d0|b0)
P(a0,d1|b0) = P(a0|b0)*P(d1|b0)
P(a1,d0|b0) = P(a1|b0)*P(d0|b0)
P(a1,d1|b0) = P(a1|b0)*P(d1|b0)
P(a0,d0|b1) = P(a0|b1)*P(d0|b1)
P(a0,d1|b1) = P(a0|b1)*P(d1|b1)
P(a1,d0|b1) = P(a1|b1)*P(d0|b1)
P(a1,d1|b1) = P(a1|b1)*P(d1|b1)
-------------------------------


P(a0,c0|b0) = P(a0|b0)*P(c0|b0)
P(a0,c1|b0) = P(a0|b0)*P(c1|b0)
P(a1,c0|b0) = P(a1|b0)*P(c0|b0)
P(c0,d1|b1) = P(c0|b1)*P(d1|b1)
P(c1,d1|b1) = P(c1|b1)*P(d1|b1)
P(a0,b0|d0) = P(a0|d0)*P(b0|d0)
P(a1,b1|d0) = P(a1|d0)*P(b1|d0)
P(a0,c0|d0) = P(a0|d0)*P(c0|d0)
P(a0,c1|d0) = P(a0|d0)*P(c1|d0)
P(a1,c0|d0) = P(a1|d0)*P(c0|d0)

\[ \therefore (B \ \bot \ C \ | \ D ), \quad (\ A \ \bot \ D \ | \ B) \]

4个元素独立性

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
for ele in prob_dict.keys():
if len(ele) == 8:
ele1 = ele[0:2]
ele2 = ele[2:4]
ele3 = ele[4:6]
ele4 = ele[6:8]
#print(ele1, ele2, ele3, ele4)

# 检查条件独立性
# P(ABC|D) 形式,条件元素个数为1, 不考虑两个联合分布和另一个的条件独立性,
for i in range(4):
ele_list = [ele1, ele2, ele3, ele4]
cond = ele_list[i]
ele_list.pop(i)
test1 = ele_list[0]
test2 = ele_list[1]
test3 = ele_list[2]

prob_condition = round(prob_dict[ele], 3)
prob_test1 = prob_dict[sort_var_name([test1, cond])]
prob_test2 = prob_dict[sort_var_name([test2, cond])]
prob_test3 = prob_dict[sort_var_name([test3, cond])]

div = round(prob_test1 * prob_test2 * prob_test3 / pow(prob_dict[cond], 2), 3)
if div == prob_condition:
print("P(%s, %s, %s |%s) = P(%s|%s)*P(%s|%s)*P(%s|%s)" % \
(test1, test2, test3, cond, test1, cond, test2, cond, test3, cond))

# P(ABC|D) 形式,条件元素个数为1, 只考虑两个联合分布和另一个的条件独立性
for i in range(4):
ele_list = [ele1, ele2, ele3, ele4]
cond = ele_list[i]
ele_list.pop(i)

## 选出一个作为单独的那个元素
for j in range(3):
ele1_list = [_ele for _ele in ele_list]
test_single = ele1_list[j]
ele1_list.pop(j)
test_couple = sort_var_name(ele1_list)

prob_condition = round(prob_dict[ele], 3)
prob_test_single = prob_dict[sort_var_name([test_single, cond])]
prob_test_couple = prob_dict[sort_var_name([test_couple, cond])]

div = round(prob_test_single * prob_test_couple / prob_dict[cond], 3)

if div == prob_condition:
print("P(%s, %s|%s) = P(%s|%s)*P(%s|%s)" % \
(test_single, test_couple, cond,
test_single, cond, test_couple, cond))

# P(AB|CD) 形式,条件元素个数为2
for i in range(4):
for j in range(i+1, 4):
ele_list = [ele1, ele2, ele3, ele4]
cond = sort_var_name([ele_list[i], ele_list[j]])
a = [0, 1, 2, 3]
#ele_list.pop(i)
#ele_list.pop(j-1)

a.remove(i)
a.remove(j)
test1 = ele_list[a[0]]
test2 = ele_list[a[1]]

prob_condition = round(prob_dict[ele], 3)
prob_test1 = prob_dict[sort_var_name([test1, cond])]
prob_test2 = prob_dict[sort_var_name([test2, cond])]

div = round(prob_test1 * prob_test2 / prob_dict[cond], 3)
if div == prob_condition:
print("P(%s, %s|%s) = P(%s|%s)*P(%s|%s)" % \
(test1, test2, cond, test1, cond, test2, cond))

结果

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
P(b0,c0|a0d0)=P(b0|a0d0)*P(c0|a0d0)
P(b0,c1|a0d0)=P(b0|a0d0)*P(c1|a0d0)
P(b1,c0|a0d0)=P(b1|a0d0)*P(c0|a0d0)
P(b1,c1|a0d0)=P(b1|a0d0)*P(c1|a0d0)
P(b0,c0|a0d1)=P(b0|a0d1)*P(c0|a0d1)
P(b0,c1|a0d1)=P(b0|a0d1)*P(c1|a0d1)
P(b1,c0|a0d1)=P(b1|a0d1)*P(c0|a0d1)
P(b1,c1|a0d1)=P(b1|a0d1)*P(c1|a0d1)
P(b0,c0|a1d0)=P(b0|a1d0)*P(c0|a1d0)
P(b0,c1|a1d0)=P(b0|a1d0)*P(c1|a1d0)
P(b1,c0|a1d0)=P(b1|a1d0)*P(c0|a1d0)
P(b1,c1|a1d0)=P(b1|a1d0)*P(c1|a1d0)
P(b0,c0|a1d1)=P(b0|a1d1)*P(c0|a1d1)
P(b0,c1|a1d1)=P(b0|a1d1)*P(c1|a1d1)
P(b1,c0|a1d1)=P(b1|a1d1)*P(c0|a1d1)
P(b1,c1|a1d1)=P(b1|a1d1)*P(c1|a1d1)
-----------------------------------
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