dag_to_cpdag.m.svn-base

来自「bayesian network structrue learning mat」· SVN-BASE 代码 · 共 110 行

SVN-BASE
110
字号
function  [cpdag] = dag_to_cpdag(dags)% (also works with a cell array of dags, returning a cell array of cpdags)% DAG_TO_CPDAG produce a N*N matrix which values respect :%% 	If the edge is compelled then 1 on the edge.%	If the edge is reversible then 1 on the edge and 1 in the reverse edge.%% Make sure that the entry is a DAG.%% See D.M. Chickering: "Learning Equivalence Classes of Bayesian-Network Structures".%% % philippe.leray@insa-rouen.fr,  olivier.francois@insa-rouen.frif ~iscell(dags)    dag=cell(1,1);    dag{1}=dags;else    dag=dags;endfor da=1:length(dag)    cpdags{da} = abs(label_edges(dag{da}));endif ~iscell(dags)    cpdag=cpdags{1};else    cpdag=cpdags;end%%==============================================================================function [label] = label_edges(dag)% LABEL-EDGES produce a N*N matrix which values are% 	+1 if the edge is compelled or%	-1 if the edge is reversible.% Make sure that the entry is a DAG.%% olivier.francois@insa-rouen.frN=length(dag);[order xedge yedge] = order_edges(dag);label = 2*dag;NbEdges = length(xedge) ;for Edge=1:NbEdges,    xlow=xedge(Edge);    ylow=yedge(Edge);    if label(xlow,ylow)==2        fin = 0;        wcompelled = find(label(:,xlow)==1);        parenty = find(label(:,ylow)~=0);        for w = wcompelled            if ~ismember(w,parenty)                label(parenty,ylow)=1;                fin = 1;            elseif fin == 0                label(w,ylow)=1;            end        end        if fin == 0            parentx = [xlow ; find(label(:,xlow)~=0)];            if ~isempty(mysetdiff(parenty,parentx))                label(find(label(:,ylow)==2),ylow)=1;            else	                label(xlow,ylow)=-1;                label(ylow,xlow)=-1;                ttp=find(label(:,ylow)==2);                label(ttp,ylow)=-1;                label(ylow,ttp)=-1;            end        end    endend%%%========================================================================================function [order, x, y] = order_edges(dag)% ORDER_EDGES produce a total (natural) ordering over the edges in a DAG.% Make sure that the entry is a DAG.%% philippe.leray@insa-rouen.fr, olivier.francois@insa-rouen.fr%% 2 mai 2003if acyclic(dag)==0    error('Requires an acyclic graph');endN=length(dag);order = zeros(N,N);node_order = topological_sort(dag);[tmp oo] = sort(node_order);dag=dag(node_order,node_order);[x y]=find(flipud(dag)==1);nb_edges=length(x);if nb_edges~=0  order(sub2ind([N N],N+1-x,y))=1:nb_edges ;endorder=order(oo,oo);x=node_order(N+1-x);y=node_order(y);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?