利用matlab进行简单的贝叶斯网络构建

matlab的安装

      第一次装matlab,装好后发现没有在桌面生成图标,还以为是最后提示的编译器没有安装。结果发现,matlab的图标在其安装的bin目录下面,进行初始化就能开始使用了

关于贝叶斯网络

      1.简单的教程可以参照如下地址:

              http://cs.nyu.edu/faculty/davise/ai/bayesnet.html

               http://www.cnblogs.com/leoo2sk/archive/2010/09/18/bayes-network.html

      2.向matlab中添加FULLBNT,参考地址:

               http://hi.baidu.com/73290673/item/21db99f36d90bc49932af29d

              采用MATLAB语言编制的贝叶斯网络工具箱(Bayesian Networks Toolbox,BNT)可实现贝叶斯网络结构学习、参数学习、推理和构建贝叶斯分类器,此工具箱在贝叶斯学习编        程方面非常灵活。              

官方主页:http://www.cs.ubc.ca/~murphyk/Software/BNT/bnt.html              

官方下载:http://www.cs.ubc.ca/~murphyk/Software/BNT/FullBNT-1.0.4.zip              

原文链接:http://hi.baidu.com/zgyz/blog/item/2d3627f415c7fbe77709d763.html              

贝叶斯网络:http://www.cs.ubc.ca/~murphyk/Software/BNT/bnt.html              

语音工具箱:http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html            

1、解压FullBNT-1.0.4.zip,将整个目录FullBNT-1.0.4复制到MATLAB的安装目录的TOOLBOX目录下,如D:\MATLAB7\toolbox\              

2、打开Matlab,在MATLAB命令窗口中输入以下命令:

             >> cd D:\MATLAB7\toolbox\FullBNT-1.0.4

             >> addpath(genpathKPM(pwd))

             >>             将TOOLBOX下新加的BNT工具箱加到MATLAB的搜索路径中去。             添加BNT工具箱的MATLAB的搜索路径也可采用如下指令

             >> addpath(genpath('D:\MATLAB7\toolbox\FullBNT-1.0.4'))

            >>            

3、为了永久保存上面的路径,以免下次重启MATLAB时重新添加,在MATLAB命令窗口下使用下面的命令:

            >> savepath             >>            

4、检验是否成功设置的方法:           在命令窗口中输入以下命令:which test_BNT.m(可以为所加工具箱的任一个M文件名称),如果显示正确,就说明上面的设置成功。

            >> which test_BNT.m             D:\MATLAB7\toolbox\FullBNT-1.0.4\BNT\test_BNT.m

           >>

       3.关于FULLBNT使用简单教程

          http://www.cs.ubc.ca/~murphyk/Software/BNT/usage.html#examples

          http://blog.sina.com.cn/s/blog_6c7b434d01013ufz.html 

实例分析

            如下图所示,给出了贝叶斯网络的图和相应的条件概率

           

                        对上述信息建立贝叶斯网络,代码如下

                     

  1. N=8; 
  2. dag=zeros(N,N); 
  3. A=1;S=2;T=3;L=4;B=5;E=6;X=7;D=8; 
  4. dag(A,T)=1; 
  5. dag(S,[L B])=1; 
  6. dag([T L],E)=1; 
  7. dag(B,D)=1; 
  8. dag(E,[X D])=1; 
  9. discrete_nodes=1:N; 
  10. node_sizes=2*ones(1,N); 
  11. bnet=mk_bnet(dag,node_sizes,'names',{'A','S','T','L','B','E','X','D'},'discrete',discrete_nodes); 
  12. bnet.CPD{A}=tabular_CPD(bnet,A,[0.99,0.01]); 
  13. bnet.CPD{S}=tabular_CPD(bnet,S,[0.5,0.5]); 
  14. bnet.CPD{T}=tabular_CPD(bnet,T,[0.99,0.95,0.01,0.05]); 
  15. bnet.CPD{L}=tabular_CPD(bnet,L,[0.99,0.9,0.01,0.1]); 
  16. bnet.CPD{B}=tabular_CPD(bnet,B,[0.7,0.4,0.3,0.6]); 
  17. bnet.CPD{E}=tabular_CPD(bnet,E,[1,0,0,0,0,1,1,1]); 
  18. bnet.CPD{X}=tabular_CPD(bnet,X,[0.95,0.02,0.05,0.98]); 
  19. bnet.CPD{D}=tabular_CPD(bnet,D,[0.9,0.2,0.3,0.1,0.1,0.8,0.7,0.9]); 
  20. draw_graph(dag) 
N=8;
dag=zeros(N,N);
A=1;S=2;T=3;L=4;B=5;E=6;X=7;D=8;
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;
node_sizes=2*ones(1,N);
bnet=mk_bnet(dag,node_sizes,'names',{'A','S','T','L','B','E','X','D'},'discrete',discrete_nodes);
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)

                    说明:有N=8个节点,建立有向无环图dag,并且这些点的值是离散的,这里1=False 2=True,node_sizes给出了所有状态

 

                    mk_bnet中names后的{}里面给出了各个节点的别名

                    利用tabular_CPD设置各个变量的边缘概率,对于A和S,定义顺序是False True;对于T、L和B这类,顺序是FF  FT TF TT;对于D这类,顺序是FFF FFT FTF FTT TFF TFT TTF TTT

       简单检查下A的概率

  1. engine=jtree_inf_engine(bnet); 
  2. evidence=cell(1,N); 
  3. [engine,loglik]=enter_evidence(engine,evidence); 
  4. m=marginal_nodes(engine,A); 
  5. m.T() 
engine=jtree_inf_engine(bnet);
evidence=cell(1,N);
[engine,loglik]=enter_evidence(engine,evidence);
m=marginal_nodes(engine,A);
m.T()

现在可以给定任意条件,然后计算概率了。

  1. 例如要计算任意组合条件下,个体分别得Tub、lung cancer和bronchitis的概率。下面代码计算了P(T=True|A=False,S=True,X=True,D=False)的概率 
     例如要计算任意组合条件下,个体分别得Tub、lung cancer和bronchitis的概率。下面代码计算了P(T=True|A=False,S=True,X=True,D=False)的概率
  1. engine=jtree_inf_engine(bnet); 
  2. evidence=cell(1,N); 
  3. evidence{A}=1; 
  4. evidence{S}=2; 
  5. evidence{X}=2; 
  6. evidence{D}=1; 
  7. [engine,loglik]=enter_evidence(engine,evidence); 
  8. m=marginal_nodes(engine,T); 
  9. m.T(2) 

源地址:http://blog.csdn.net/moodytong/article/details/8122327#t4

转载于:https://www.cnblogs.com/xjx-user/archive/2013/04/14/3020301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值