⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 construct_bigcpts.m

📁 The BNL toolbox is a set of Matlab functions for defining and estimating the parameters of a Bayesi
💻 M
字号:
function CPT=construct_bigCPTs(eCPT,equiv_class,...
    evidence_nodes,partial_evidence_nodes,terminal_merged_nodes,hid_nodes,...
    gausskwadnodes,N)
%incorporates the (categorical) data and constructs 'big' CPTS (one for
%each person) on the basis of equiv_class_CPTs
%enters hard and soft evidence
%not yet defined for evidence nodes
if ~isempty(evidence_nodes.nodenrs)
    error('not yet defined for evidence nodes; treat them as partial_evidence nodes');
end

nr_nodes=length(equiv_class);
CPT=cell(nr_nodes,1);

nr_eq_cl=length(eCPT);
%determine for which nodes we already have probs for each separate case
%(ungrouped data)
grouped=zeros(nr_eq_cl,1);
n=ones(nr_eq_cl,1)*N;
for i=1:nr_eq_cl
    nd=ndims(eCPT{i});
    siz=size(eCPT{i},nd);
    if siz(end)~=N 
        n(i)=1;
        grouped(i)=1;
    end
end
%construct "big" cpts (one for each case) and enter evidence for
%partial_evidence nodes and all hidden nodes
nodes=[hid_nodes.nodenrs' gausskwadnodes.nodenrs' evidence_nodes.nodenrs' partial_evidence_nodes.nodenrs'];
for i=1:length(nodes)
    e=equiv_class(nodes(i));
    nd=ndims(eCPT{e});
    %if isscalar(eCPT{e}) nd=1;, end;
    if grouped(e)
        CPT{nodes(i)}=squeeze(repmat(full(eCPT{e}),[ones(1,nd) N]));
    else
        CPT{nodes(i)}=squeeze(full(eCPT{e}));
    end
    %enter evidence
    f=find(partial_evidence_nodes.nodenrs==nodes(i));
    if ~isempty(f)
        data=partial_evidence_nodes.data{nodes(i)};
        siz=size(CPT{nodes(i)});
        shift=prod(siz(1:end-2));
        a=reshape(CPT{nodes(i)},[shift siz(end-1:end)]);
        for k=0:siz(end-1)-1
            y= find(data==k);
            a(:,mysetdiff(1:siz(end-1),k+1),y)=0;
        end
        CPT{nodes(i)}=reshape(a,siz);
    end
end

%construct big CPTs and enter soft evidence for terminal_merged_nodes
nodes=terminal_merged_nodes.nodenrs;
data=terminal_merged_nodes.data;
for i=1:length(nodes);
    S_item=terminal_merged_nodes.respcat{i}(1);
    f=find(terminal_merged_nodes.respcat{i}(:)~=S_item);
    if ~isempty(f) error('all variables merged into same node should have same nr of response categories'), end
    M=terminal_merged_nodes.nrvars(i);
    m=nodes(i);
    e=equiv_class(m);
    siz=size(eCPT{e});
    nd=ndims(eCPT{e});
    if ~grouped(e)
        siz=siz(1:end-1);%for grouped data, second last dimension is persons
        nd=nd-1;
    end
    a=ones([siz(1:end-1)  N]);
    shift=prod(siz(1:end-2));
    a=reshape(a,[shift M N]);
    probs=full(eCPT{e});
    if grouped(e) b=repmat(probs,[ones(1,nd) N]);
    else b=probs;
    end
    b=reshape(b,[shift   M S_item N]);
    for k=0:S_item-1
        y= find(data{i}==k);
        bb=b(:,:,k+1,:);
        a(:,y)=bb(:,y);
    end
    a=reshape(a,[siz(1:end-1)  N]);
    a=squeeze(prod(a,nd-1));
    CPT{m}=reshape(a,[siz(1:end-2) 1 N]);
end

⌨️ 快捷键说明

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