Bayesian network - MATLAB study notes (1)

1. The principle of Bayesian network

  • The principle of Bayesian network is Bayes theorem. The core of using Bayesian network to solve reasoning problems is to use known "forward probability" to solve "reverse probability".

2. Build a Bayesian network

1. Add Bayesian network construction tool FullBNT to matlab

FULLBNT is a Bayesian network toolbox that comes with matlab, download address: http://www.cs.ubc.ca/~murphyk/Software/BNT/FullBNT-1.0.4.zip

  • a. Unzip FullBNT-1.0.4.zip, and copy the entire directory FullBNT-1.0.4 to the TOOLBOX directory of the MATLAB installation directory. My directory is 'C:\Program Files\MATLAB\R2016a\toolbox';
  • b. Open Matlab, enter the above directory in the MATLAB command window, and enter the following command:
>> cd ('C:\Program Files\MATLAB\R2016a\toolbox\FullBNT-1.0.4') %添加括号是为了防止出现参数过多的错误。
  • c. Add the BNT toolbox under TOOLBOX to the MATLAB path, the code is as follows:
>> addpath(genpathKPM(pwd))
警告: 函数 assert 与某个 MATLAB 内置函数同名。建议您重命名该函数以避免潜在的名称冲突。 
> In path at 109
  In addpath at 86 
警告: 函数 isscalar 与某个 MATLAB 内置函数同名。建议您重命名该函数以避免潜在的名称冲突。 
> In path at 109
  In addpath at 86 
警告: 函数 isvector 与某个 MATLAB 内置函数同名。建议您重命名该函数以避免潜在的名称冲突。 
> In path at 109
  In addpath at 86 
  • d. Permanently save the above path, so as not to add it again when MATLAB is restarted next time, command:
>>savepath
  • e. Check whether it is successful, enter in the command line window:
>> which test_BNT.m
C:\Program Files\MATLAB\R2016a\toolbox\FullBNT-1.0.4\BNT\test_BNT.m
>> 

The display is correct and the toolbox has been added successfully.

2. Example analysis

【Example 1】

As shown in the figure below, the graph of the Bayesian network and the corresponding conditional probability are given:
insert image description here
to establish a Bayesian network for the above information, the code is as follows:

  • (1) Create a matlab program for a Bayesian network:
N=8; %8个节点,也就是8个特征
A=1; %visit to Asia到过亚洲
S=2; %somking抽烟
T=3; %tuberculosis肺结核
L=4; %lung cancer肺癌
B=5; %bronchitis支气管炎
E=6; %either tub. or lung cancer肺结核或者肺癌
X=7; %positive X-rayX光片呈阳性
D=8; %dyspnoea呼吸困难

%定义网络结构
dag=zeros(N,N);%创建无环图对应的矩阵
dag(A,T)=1;%按拓扑结构关系赋值
dag(S,[L,B])=1;
dag([T,L],E)=1;
dag(B,D)=1;
dag(E,[X,D])=1;
discrete_nodes=1:N;%从1取到N,也就是1-8,赋予各个节点类型,用1:N表示各个节点种类不同
node_sizes=2*ones(1,N);%定义节点状态,元素值为2的1*N行向量,赋予节点的大小,节点独立地有几种可能
bnet=mk_bnet(dag,node_sizes,'names',{
    
    'A','S','T','L','B','E','X','D'},'discrete',discrete_nodes);

%设置各个节点的边缘概率;
%对于A和S,定义顺序是False True;
%对于T、L和B这类,顺序是FF  FT TF TT;
%对于D这类,顺序是FFF FFT FTF FTT TFF TFT TTF TTT
bnet.CPD{
    
    A}=tabular_CPD(bnet,A,[0.99,0.01]);
bnet.CPD{
    
    S}=tabular_CPD(bnet,S,[0.5,0.5]);  
bnet.CPD{
    
    T}=tabular_CPD(bnet,T,[0.99,0.95,0.01,0.05]);  
bnet.CPD{
    
    L}=tabular_CPD(bnet,L,[0.99,0.9,0.01,0.1]);  
bnet.CPD{
    
    B}=tabular_CPD(bnet,B,[0.7,0.4,0.3,0.6]);  
bnet.CPD{
    
    E}=tabular_CPD(bnet,E,[1,0,0,0,0,1,1,1]);  
bnet.CPD{
    
    X}=tabular_CPD(bnet,X,[0.95,0.02,0.05,0.98]);  
bnet.CPD{
    
    D}=tabular_CPD(bnet,D,[0.9,0.2,0.3,0.1,0.1,0.8,0.7,0.9]); 

draw_graph(dag);%画出网络

  • (2) The completed Bayesian network:
    insert image description here
    So far, a simple Bayesian network has been constructed.
  • (3) Simply check the probability of A
engine=jtree_inf_engine(bnet);%结树,贝叶斯网络,创建一个"引擎"
evidence=cell(1,N);
[engine,loglik]=enter_evidence(engine,evidence);
m=marginal_nodes(engine,A);
m.T()

The result is as shown in the figure below:
insert image description here
Now any condition can be given, and then the probability can be calculated.

  • (4) Use the joint tree inference engine to infer the Bayesian network
%网络推断,使用联合树引擎,计算P(T=True|A=False,S=True,X=True,D=False)的概率 
%即病人在没有到过亚洲并且抽烟,在做检查之后发现X-ray光片呈阳性,但日常生活不伴有呼吸困难的前提下,患有肺结核的概率有多大?

engine=jtree_inf_engine(bnet);
evidence=cell(1,N);%1*N的空cell类型向量,元素可以是任意类型数据
evidence{
    
    A}=1;  
evidence{
    
    S}=2;  
evidence{
    
    X}=2;  
evidence{
    
    D}=1; 
[engine,loglik]=enter_evidence(engine,evidence);
m=marginal_nodes(engine,T);
m.T

Drawn:
insert image description here
It can be seen that T is TRUE, that is, the probability of the patient suffering from pulmonary tuberculosis is relatively high.

【Example 2】

insert image description here

  • (1) Create a matlab program for a Bayesian network:
N=4;
dag=zeros(N,N);%无向环用矩阵表示
C=1;S=2;R=3;W=4;%按拓扑矩阵给各个节点编号,方便下一步赋值
dag(C,[R S])=1;%按逻辑关系赋值
dag([S R],W)=1;
discrete_nodes = 1:N;%定义类型
node_sizes = 2*ones(1,N);%分配大小
bnet=mk_bnet(dag,node_sizes,'names',{
    
    'C','S','R','W'},'discrete',discrete_nodes);%创建网络
bnet.CPD{
    
    C}=tabular_CPD(bnet,C,[0.5 0.5]);%赋上各个事件的概率值
bnet.CPD{
    
    S}=tabular_CPD(bnet,S,[0.5 0.9 0.5 0.1]);
bnet.CPD{
    
    R}=tabular_CPD(bnet,R,[0.8 0.2 0.2 0.8]);
bnet.CPD{
    
    W}=tabular_CPD(bnet,W,[1.0 0.1 0.1 0.01 0 0.9 0.9 0.99]);
draw_graph(dag) %绘制出贝叶斯网络图

  • (2) Build the completed Bayesian network:
    insert image description here
  • (3) Simply check the probability of S occurring when C is True;
engine=jtree_inf_engine(bnet);
evidence=cell(1,N);%1*N的空cell类型向量,元素可以是任意类型数据
evidence{
    
    C}=2;  
[engine,loglik]=enter_evidence(engine,evidence);
m=marginal_nodes(engine,S);
m.T

The result is as shown in the figure below:
insert image description here
Now any condition can be given, and then the probability can be calculated.

  • (4) Use the joint tree inference engine to infer the Bayesian network. After the Bayesian network is established
    , the cause can be inferred based on the observed phenomenon; the following is the calculation of the possibility of rain when the glass is observed to be wet How big is this fragment of a matlab program:
engine=jtree_inf_engine(bnet); %结树,贝叶斯网络,创建一个"引擎"
evidence=cell(1,N);
evidence{
    
    W}=2;%计算当观察到玻璃是湿的时候,下雨的可能性有多大
[engine,loglik]=enter_evidence(engine,evidence);
m=marginal_nodes(engine,C);
m.T

The calculation results are shown in the figure below, indicating that when the glass is observed to be wet, the probability of no rain is 42.42%, and the probability of rain is 57.58%.
insert image description here
It can also be represented by a histogram:

bar(m.T)

insert image description here

3. Matters needing attention

Point 1:
insert image description here
When expressing this logical relationship, dag(A,T)=1, not dag(T,A)=1.

Point 2:
The size of the node only indicates how many possibilities the node has in an independent situation . For example, if node A in the above figure has been to Asia, there are two possible results, whether it has been or not, then the size of the node is 2 .

Point 3:
The probability values ​​here are in a certain order,
(1) When there are only two probability values: FT (actually 0 1)
(2) When there are four probability values, according to the following example, PC: FF FT TF TT (actually 00 01 10 11)
insert image description here

insert image description here

bnet.CPD{
    
    S}=tabular_CPD(bnet,S,[0.5 0.9 0.5 0.1]);

(3) When there are eight probability values, according to the following example, WRS: FFF FFT FTF FTT TFF TFT TTF TTT (actually 000 001 010 011 100 101 110 111)
insert image description here

bnet.CPD{
    
    W}=tabular_CPD(bnet,W,[1.0 0.1 0.1 0.01 0 0.9 0.9 0.99]);

Summary:
By the way, the rules followed by PC and WRS here are:
when they are at different levels, the ones below are ranked first;
when they are at the same level, the ones on the right are ranked first.

4. Problems encountered and solutions

MATLAB reported an error when generating the Bayesian network:

1. Problem 1 (Bayesian network without arrows)

Undefined operator '*' corresponding to input arguments of type 'matlab.graphics.axis.Axes'

Description of the problem:
When using matlab to generate a Bayesian network, it can be generated, but the generated Bayesian network has no edges, as shown in the following figure:
insert image description here
insert image description here

solution:

Replace the original arrow.m under C:\Program Files\MATLAB\R2016a\toolbox\FullBNT-1.0.4\GraphViz with the following file

New version of arrow.m file link: https://pan.baidu.com/s/1lcaY_58oNNgCSGK375Hv0g
Extraction code: tikt

2. Question 2 (draw_graph function call error)

Problem description : The error is as follows

>> BN_weather
错误使用 set
Patch 类中没有 Color 属性。

出错 draw_graph (line 103)
set(h_edge,'Color',color.edge)

出错 BN_weather (line 14)
draw_graph(dag) %绘制出贝叶斯网络图

Solution:
There is a problem when the draw_graph function is called, there is no way to identify set(h_edge,'Color',color.edge),

Because h_edge is an edge, and 'Color' is the color of the graph, in the draw_graph function, change the color of line 103 where the error is reported to EdgeColor.
insert image description here

For example, you can also set the color of the arrow: color.edge='green'

insert image description here
The result of the operation is as follows:
insert image description here

Reference:
[1] http://t.csdn.cn/dB5Bt
[2] http://t.csdn.cn/StObM
[3] http://t.csdn.cn/HEOpI
[4]https:/ /blog.csdn.net/corinne0623/article/details/124435366

Guess you like

Origin blog.csdn.net/weixin_45075135/article/details/129108204