bnt_1 一个用于贝叶斯网络的matlab的工具

N = 4;
dag = zeros( N, N );
C = 1; 
S = 2;
R = 3;
W = 4;
dag( C, [R S] ) = 1;
dag( R, W ) = 1;
dag( S, W ) = 1;

node_sizes = 2*ones(1,N);
%node_sizes = [ 4 2 3 5];

%建立有向无环图
onodes = [];
bNet = mk_bnet( dag, node_sizes );

%建立条件概率表
bNet.CPD{C} = tabular_CPD( bNet, C, 'CPT', [0.5 0.5] );
bNet.CPD{R} = tabular_CPD( bNet, R,  'CPT',[0.8 0.2 0.2 0.8 ] );
bNet.CPD{S} = tabular_CPD( bNet, S,  'CPT', [0.5 0.9 0.5 0.1] );
bNet.CPD{W} = tabular_CPD( bNet, W, 'CPT', [1 0.1 0.1 0.01 0 0.9 0.9 0.99 ] );
    


%用于推断
engine = jtree_inf_engine( bNet);

evidence = cell( 1,N );
evidence{W} = 2;

%添加证据
[engine, loglik] = enter_evidence( engine, evidence );
marg = marginal_nodes( engine, S );
p = marg.T(2);

%添加下雨的证据
evidence{R} = 2;
[ engine, loglik ] = enter_evidence( engine, evidence );
marg = marginal_nodes( engine, S );
p = marg.T(2);

bar( marg.T );


% Joint distributions
evidence = cell(1,N);
[engine, ll] = enter_evidence(engine, evidence);
m = marginal_nodes(engine, [S R W]);
m.T

  

posted @ 2011-11-17 15:54  lxgeek  阅读(2920)  评论(0编辑  收藏  举报