probabilistic graphic models Exercise 3.1
起因
学完了一章,看题,怎么都不会?
经过
想了好久
结果
从最简单的开始,令X,Y,Z的取值为{0,1},则只需构造8个项的概率,记作
v
0
,
v
1
,
v
2
,
⋯
,
v
7
v_0,v_1,v_2,\cdots,v_7
v0,v1,v2,⋯,v7,按照X,Y,Z的取值进行编号,X的值对应v下标的最高二进制位,X取0的概率记作
P
x
P_x
Px.
那么
v
0
+
v
1
+
v
2
+
v
3
=
P
x
v
0
+
v
1
+
v
4
+
v
5
=
P
y
v
0
+
v
3
+
v
5
+
v
7
=
P
z
v_0+v_1+v_2+v_3=P_x\\ v_0+v_1+v_4+v_5=P_y\\ v_0+v_3+v_5+v_7=P_z
v0+v1+v2+v3=Pxv0+v1+v4+v5=Pyv0+v3+v5+v7=Pz
再由已知条件
(
X
⊥
Y
)
,
(
X
⊥
Z
)
,
(
Y
⊥
Z
)
(X\perp Y),(X\perp Z),(Y\perp Z)
(X⊥Y),(X⊥Z),(Y⊥Z)有,
v
0
+
v
1
=
P
a
P
b
v
0
+
v
2
=
P
a
P
c
v
0
+
v
4
=
P
b
P
c
v_0+v_1=P_aP_b\\ v_0+v_2=P_aP_c\\ v_0+v_4=P_bP_c
v0+v1=PaPbv0+v2=PaPcv0+v4=PbPc
由概率的定义有
∑
i
=
0
7
v
i
=
1
\sum\limits_{i=0}^7v_i=1
i=0∑7vi=1
8个变量,7个方程,于是再加上一个条件,给定
v
0
v_0
v0的值,则可以相应的解出其它的值.
#include <iostream>
#include <matrix.hpp>
#include <mat_algos.hpp>
#include <string>
using namespace std;
int main() {
double pa,pb,pc,v0;
cout<<"find three-variable binary joint distribution."<<endl;
cout<<"please input P(a0) P(b0) P(c0) P(a0,b0,c0):";
cin>>pa>>pb>>pc>>v0;
mine::Matrix m(8,9,{
1, 0, 0, 0, 0, 0, 0, 0, v0,
1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0, 0, 0, 0, pa,
1, 1, 0, 0, 1, 1, 0, 0, pb,
1, 0, 1, 0, 1, 0, 1, 0, pc,
1, 1, 0, 0, 0, 0, 0, 0, pa*pb,
1, 0, 1, 0, 0, 0, 0, 0, pa*pc,
1, 0, 0, 0, 1, 0, 0, 0, pb*pc
});
mine::gaussCR(m);//自己写的矩阵库,
mine::solveUpper(m);//用来求解方程,最后一列即为最终的结果.
for(int i=0;i<8;++i){
printf("%f",m[i][8]);
if((i+1)%4==0)cout<<endl<<"---------------------"<<endl;
else if((i+1)%2==0)cout<<endl;
else cout<<' ';
}
return 0;
}
求解结果可能会出现负概率,对输入进行调整,试几次,就会有结果了.
find three-variable binary joint distribution.
please input P(a0) P(b0) P(c0) P(a0,b0,c0):0.1 0.2 0.3 0
0.000000 0.020000
0.030000 0.050000
---------------------
0.060000 0.120000
0.210000 0.510000
---------------------