?? lungbayesdemo.html
字號:
P(B|X=t) = 0.18585P(L|X=t) = 0.67227P(X|X=t) = 1</pre><p>Given the observed abnormal x-ray results, the probability of lung cancer has increased significantly because of the direct dependency of node <tt>X</tt> (x-rays) on node <tt>L</tt> (lung cancer). </p> <p>Finally, suppose the patient has both been diagnosed with bronchitis and received positive results for his/her chest x-ray. We update the previous state of the network (<tt>X</tt> = <tt>t</tt>) with the new evidence(<tt>B</tt> = <tt>t</tt>), as shown below: </p><pre class="codeinput">evNode = B;evValue = t;[n3, e3, A3, a3] = bnMsgPassUpdate(n2, e2, A2, a2, evNode, evValue);<span class="keyword">for</span> i = 1:n disp([<span class="string">'P('</span> nodeNames{i}, <span class="string">'|B=t,X=t) = '</span> num2str(n3(i).P(1))]);<span class="keyword">end</span></pre><pre class="codeoutput">P(S|B=t,X=t) = 0.91372P(B|B=t,X=t) = 1P(L|B=t,X=t) = 0.85908P(X|B=t,X=t) = 1</pre><p>We can compare the three situations by plotting the probabilities as bar charts. Evidence of bronchitis and evidence of abnormal x-rays increase the probability of lung cancer and smoking history, one indirectly and the other directly. </p><pre class="codeinput">figure(); subplot(3,1,1);bar(x1, <span class="string">'stacked'</span>); set(gca, <span class="string">'xticklabel'</span>, nodeNames);ylabel(<span class="string">'Probability'</span>); title(<span class="string">'Bronchitis diagnosis'</span>);legend({<span class="string">'true'</span>, <span class="string">'false'</span>}, <span class="string">'location'</span>, <span class="string">'SouthEastOutside'</span>)hold <span class="string">on</span>; subplot(3,1,2);x2 = cat(1,n2.P);bar(x2,<span class="string">'stacked'</span>); set(gca, <span class="string">'xticklabel'</span>, nodeNames);ylabel(<span class="string">'Probability'</span>); title(<span class="string">'Abnormal x-rays'</span>);legend({<span class="string">'true'</span>, <span class="string">'false'</span>}, <span class="string">'location'</span>, <span class="string">'SouthEastOutside'</span>)hold <span class="string">on</span>; subplot(3,1,3);x3 = cat(1,n3.P);bar(x3, <span class="string">'stacked'</span>); set(gca, <span class="string">'xticklabel'</span>, nodeNames);ylabel(<span class="string">'Probability'</span>); title(<span class="string">'Bronchitis and abnormal x-ray'</span>);legend({<span class="string">'true'</span>, <span class="string">'false'</span>}, <span class="string">'location'</span>, <span class="string">'SouthEastOutside'</span>)</pre><img vspace="5" hspace="5" src="lungbayesdemo_05.png"> <p>We can now compare the effect of a positive versus negative bronchitis diagnosis in presence of abnormal x-ray results. We instantiate <tt>B = f</tt> (false) and compare with previous estimates <tt>B = t</tt> (true). </p><pre class="codeinput">evNode = B;evValue = f;[n4, e4, A4, a4] = bnMsgPassUpdate(n2, e2, [], [], evNode, evValue);<span class="keyword">for</span> i = 1:n disp([<span class="string">'P('</span> nodeNames{i}, <span class="string">'|B=f,X=t) = '</span> num2str(n4(i).P(1))]);<span class="keyword">end</span>figure();bar3([n3(S).P(:,t) n4(S).P(:,t); n3(L).P(:,t) n4(L).P(:,t)]);colormap(summer); zlabel(<span class="string">'Probability'</span>);set(gca,<span class="string">'xticklabel'</span>,{<span class="string">'Smoking'</span>,<span class="string">'Lung Cancer'</span>},<span class="string">'yticklabel'</span>, {<span class="string">'With Bronchitis'</span>, <span class="string">'Without Bronchitis'</span>});set(gca,<span class="string">'xticklabel'</span>,{<span class="string">'With Bronchitis'</span>,<span class="string">'Without Bronchitis'</span>},<span class="string">'yticklabel'</span>, {<span class="string">'Smoking'</span>,<span class="string">'Lung Cancer'</span>});title(<span class="string">'Conditional probabilities with evidence of abnormal x-ray results'</span>)view(50,35);</pre><pre class="codeoutput">P(S|B=f,X=t) = 0.62575P(B|B=f,X=t) = 0P(L|B=f,X=t) = 0.62962P(X|B=f,X=t) = 1</pre><img vspace="5" hspace="5" src="lungbayesdemo_06.png"> <p>When bronchitis is ruled out (<tt>B = f</tt>), the probability of smoking history decreases with respect to the case in which the bronchitis is confirmed (<tt>B = t</tt>). The effect is propagated across the network and affects the probability of lung cancer in a similar manner. </p> <h2>Expanding the Network<a name="16"></a></h2> <p>Among various symptoms related to lung cancer and bronchitis is shortness of breath (dyspnea). We want to model the relationship of this condition within the considered Bayesian Network. We introduce a node <tt>D</tt> and modify the adjacency matrix accordingly. </p><pre class="codeinput"><span class="comment">%=== add node D to the network</span>D = 5;CPT{D}(:,:,t) = [.75 .1; .5 .05];CPT{D}(:,:,f) = 1 - CPT{D}(:,:,t);values{D} = [1 2];adj(end+1,:) = [0 0 0 0];adj(:,end+1) = [0 1 1 0 0];<span class="comment">%=== draw the updated network</span>nodeLabels = {<span class="string">'Smoking'</span>, <span class="string">'Bronchitis'</span>, <span class="string">'Lung Cancer'</span>, <span class="string">'Xrays'</span>, <span class="string">'Dyspnea'</span>};nodeSymbols = {<span class="string">'S'</span>, <span class="string">'B'</span>, <span class="string">'L'</span>, <span class="string">'X'</span>, <span class="string">'D'</span>};bg = biograph(adj, nodeLabels, <span class="string">'arrowsize'</span>, 4);nodeHandles= bg.Nodes;set(nodeHandles, <span class="string">'shape'</span>, <span class="string">'ellipse'</span>);view(bg)</pre><img vspace="5" hspace="5" src="lungbayesdemo_07.png"> <p>With the introduction of node <tt>D</tt>, the network is not singly-connected anymore. In fact, there are more than one chain between any two nodes (i.e., <tt>S</tt> and <tt>D</tt>). We can check this property by considering the undirected graph associated with the network and veryfying that it is not acyclic. </p><pre class="codeinput">isAcyclic = graphisdag(sparse(adj | adj'))</pre><pre class="codeoutput">isAcyclic = 0</pre><p>In order to use the algorithm for exact inference described above, we must transform the new, multiply-connected network into a singly-connected network. Several approaches can be used, including clustering of parent nodes (in our case <tt>B</tt> and <tt>L</tt>) into a single node as follows. </p> <p>First, we combine the adjacency matrix entries corresponding to the nodes <tt>B</tt> and <tt>L</tt> into one entry associated to node <tt>BL</tt>. The node <tt>BL</tt> can take up to four values, corresponding to all possible combinations of values of the original nodes <tt>B</tt> and <tt>L</tt>. Then, we update the conditional probability distribution considering that <tt>B</tt> and <tt>L</tt> are conditionally independent given the node <tt>S</tt>, that is P(BL|S) = P(B,L|S) = P(B|S) * P(L|S). </p><pre class="codeinput"><span class="comment">%=== combine B and L</span>adj(B,:) = adj(B,:) | adj(L,:);adj(:,B) = adj(:,B) | adj(:,L);adj(L,:) = []; adj(:,L) = [];<span class="comment">%=== update the probability distribution accordingly</span>b1 = kron(CPT{B}(1,:), CPT{L}(1,:));b2 = kron(CPT{B}(2,:), CPT{L}(2,:));x = [CPT{X}(1,:) CPT{X}(1,:)];d = reshape((CPT{D}(:,:,1))', 1, 4);<span class="comment">%=== update the node values</span>S = 1; BL = 2; X = 3; D = 4;nodeNames = {<span class="string">'S'</span>, <span class="string">'BL'</span>, <span class="string">'X'</span>, <span class="string">'D'</span>};tt = 1; tf = 2; ft = 3; ff = 4;values{BL} = 1:4;values(L) = [];<span class="comment">%=== create a clustered Conditional Probability Table</span>cCPT{S} = CPT{S};cCPT{BL}(t,:) = b1; cCPT{BL}(f,:) = b2;cCPT{D}(:,t) = d; cCPT{D}(:,f) = 1 - d;cCPT{X}(:,t) = x; cCPT{X}(:,f) = 1 - x;<span class="comment">%=== create and intiate the net</span>root = find(sum(adj,1)==0); <span class="comment">% root (node with no parent)</span>[cNodes, cEdges] = bnMsgPassCreate(adj, values, cCPT);[cNodes, cEdges] = bnMsgPassInitiate(cNodes, cEdges, root);<span class="keyword">for</span> i = 1:n disp([<span class="string">'P('</span> nodeNames{i}, <span class="string">'|[]) = '</span> num2str(cNodes(i).P(1))]);<span class="keyword">end</span></pre><pre class="codeoutput">P(S|[]) = 0.2P(BL|[]) = 0.0152P(X|[]) = 0.4128P(D|[]) = 0.08634</pre><h2>Drawing the Expanded Network<a name="19"></a></h2><pre class="codeinput"><span class="comment">%=== draw the network</span>nodeLabels = {<span class="string">'Smoking'</span>, <span class="string">'Bronchitis or Lung Cancer'</span>, <span class="string">'Abnormal X-rays'</span>, <span class="string">'Dyspnea'</span>};cbg = biograph(adj, nodeNames, <span class="string">'arrowsize'</span>, 4);set(cbg.Nodes, <span class="string">'shape'</span>, <span class="string">'ellipse'</span>);cbgInViewer = view(cbg);<span class="comment">%=== assign relevant info to each node handle</span>cnodeHandles = cbgInViewer.Nodes;<span class="keyword">for</span> i = 1:n cnodeHandles(i).UserData.Distribution = [cNodes(i).P];<span class="keyword">end</span><span class="comment">%=== draw customized nodes</span>set(cnodeHandles, <span class="string">'shape'</span>,<span class="string">'circle'</span>)colormap(summer)cbgInViewer.ShowTextInNodes = <span class="string">'none'</span>;cbgInViewer.CustomNodeDrawFcn = @(node) customnodedraw(node);cbgInViewer.Scale = .7cbgInViewer.dolayout</pre><pre class="codeoutput">Biograph object with 4 nodes and 3 edges.</pre><img vspace="5" hspace="5" src="lungbayesdemo_08.png"> <h2>Performing Exact Inference on Clustered Trees<a name="20"></a></h2> <p>Suppose a patient complains of dyspnea (<tt>D=t</tt>). We would like to evaluate the likelihood that this symptom is related to either lung cancer or bronchitis. </p><pre class="codeinput">[n5, e5, A5, a5] = bnMsgPassUpdate(cNodes, cEdges, [], [], D, t);</pre><p>Because node <tt>B</tt> and node <tt>L</tt> are clustered into the node <tt>BL</tt>, we have to calculate their individual conditional probabilities by considering the appropriate value combinations. The conditional probabilities in <tt>BL</tt> correspond to the following <tt>B</tt> and <tt>L</tt> value combinations: <tt>BL = tt</tt> if <tt>B = t</tt> and <tt>L = t</tt>; <tt>BL = tf</tt> if <tt>B = t</tt> and <tt>L = f</tt>; <tt>BL = ft</tt> if <tt>B = f</tt> and <tt>L = t</tt>; <tt>BL = ff</tt> if <tt>B = f</tt> and <tt>L = f</tt>. Therefore P(B|evidence) is equal to the sum of the first two elements of P(BL|evidence), and similarly, P(L|evidence) is equal to the sum of the first and third elements in P(BL|evidence). </p><pre class="codeinput">p(1,:) = n5(S).P;p(2,:) = [sum(n5(BL).P([tt,tf])), 1-sum(n5(BL).P([tt,tf]))]; <span class="comment">% P(B|evidence)</span>p(3,:) = [sum(n5(BL).P([tt,ft])), 1-sum(n5(BL).P([tt,ft]))]; <span class="comment">% P(L|evidence)</span>p(4,:) = n5(X).P;p(5,:) = n5(D).P;<span class="keyword">for</span> i = 1:5 disp([<span class="string">'P('</span> nodeSymbols{i}, <span class="string">'|D=t) = '</span> num2str(p(i,1))]);<span class="keyword">end</span></pre><pre class="codeoutput">P(S|D=t) = 0.49224P(B|D=t) = 0.21867P(L|D=t) = 0.41464P(X|D=t) = 0.48293P(D|D=t) = 1</pre><p>When dyspnea is present, both the likelihood of bronchitis and lung cancer increase. This makes sense, since both illnesses have dyspnea as symptom and the patient is indeed exhibiting this symptom. </p> <h2>Explaining Away the Lung Cancer<a name="23"></a></h2> <p>As we can see in the graph, the dyspnea symptom has dependency both on bronchitis and lung cancer. Consider the effect of a bronchitis diagnosis on the likelihood of lung cancer. </p><pre class="codeinput"><span class="comment">%=== adjust the CPT to reflect B = 1 before clustering into BL node</span>B = 2; L = 3;CPT{B}(:,1) = [1 1] ; CPT{B}(:,2) = 1 - CPT{B}(:,1);b1 = kron(CPT{B}(1,:), CPT{L}(1,:));b2 = kron(CPT{B}(2,:), CPT{L}(2,:));<span class="comment">%=== create a clustered Conditional Probability Table</span>BL = 2;cCPT{BL}(1,:) = b1; cCPT{BL}(2,:) = b2;<span class="comment">%=== create and intiate the net</span>root = find(sum(adj,1)==0); <span class="comment">% root (node with no parent)</span>[cNodes, cEdges] = bnMsgPassCreate(adj, values, cCPT);[cNodes, cEdges] = bnMsgPassInitiate(cNodes, cEdges, root);<span class="comment">%=== instantiate for F = 1</span>[n7, e7, A7, a7] = bnMsgPassUpdate(cNodes, cEdges, [], [], D, t);w(1,:) = n7(S).P;w(2,:) = [sum(n7(BL).P([tt,tf])), 1-sum(n7(BL).P([tt,tf]))]; <span class="comment">% P(B|evidence)</span>w(3,:) = [sum(n7(BL).P([tt,ft])), 1-sum(n7(BL).P([tt,ft]))]; <span class="comment">% P(L|evidence)</span>w(4,:) = n7(X).P;w(5,:) = n7(D).P;<span class="keyword">for</span> i = 1:5 disp([<span class="string">'P('</span> nodeSymbols{i}, <span class="string">'|B=t,D=t) = '</span> num2str(w(i,1))]);<span class="keyword">end</span></pre><pre class="codeoutput">P(S|B=t,D=t) = 0.41667P(B|B=t,D=t) = 1P(L|B=t,D=t) = 0.33898P(X|B=t,D=t) = 0.4678P(D|B=t,D=t) = 1</pre><p>When a patient complains of dyspnea and is diagnosed with bronchitis, the conditional probability of lung cancer is lower.</p> <p>Consider now the effect of a lung cancer diagnosis on the likellihood of bronchitis.</p><pre class="codeinput"><span class="comment">%=== adjust the CPT to reflect L = 1 before clustering into BL node</span>B = 2; L = 3;CPT{B}(:,t) = [.25 .05] ; CPT{B}(:,f) = 1 - CPT{B}(:,t);CPT{L}(:,t) = [1 1]; CPT{L}(:,f) = 1 - CPT{L}(:,t);b1 = kron(CPT{B}(t,:), CPT{L}(t,:));b2 = kron(CPT{B}(f,:), CPT{L}(f,:));BL = 2;cCPT{BL}(t,:) = b1; cCPT{BL}(f,:) = b2;<span class="comment">%=== create and intiate the net</span>root = find(sum(adj,1)==0); <span class="comment">% root (node with no parent)</span>[cNodes, cEdges] = bnMsgPassCreate(adj, values, cCPT);[cNodes, cEdges] = bnMsgPassInitiate(cNodes, cEdges, root);<span class="comment">%=== instantiate for D = 1</span>[n8, e8, A8, a8] = bnMsgPassUpdate(cNodes, cEdges, [], [], D, t);v(1,:) = n8(S).P;v(2,:) = [sum(n8(BL).P([tt,tf])), 1-sum(n8(BL).P([tt,tf]))]; <span class="comment">% P(B|evidence)</span>v(3,:) = [sum(n8(BL).P([tt,ft])), 1-sum(n8(BL).P([tt,ft]))]; <span class="comment">% P(L|evidence)</span>v(4,:) = n8(X).P;v(5,:) = n8(D).P;<span class="keyword">for</span> i = 1:5 disp([<span class="string">'P('</span> nodeSymbols{i}, <span class="string">'|L=t,D=t) = '</span> num2str(v(i,1))]);<span class="keyword">end</span></pre><pre class="codeoutput">P(S|L=t,D=t) = 0.21531P(B|L=t,D=t) = 0.12919P(L|L=t,D=t) = 1P(X|L=t,D=t) = 0.6P(D|L=t,D=t) = 1</pre><p>If a patient is diagnosed with lung cancer in presence of dyspnea, the likelihood of bronchitis decreases significantly. This phenomenon is called "explaining away" and refers to the situations in which the chances of one cause decrease significantly when the chances of the competing cause increase. </p> <p>We can observe the "explaining away" phenomenon in the two situations described above by comparing the conditional probabilities of node <tt>L</tt> and <tt>B</tt> in the two cases. When the evidence for <tt>B</tt> is high, the likelihood of <tt>L</tt> is relatively low, and viceversa, when the evidence for <tt>L</tt> is high, the likelihood of <tt>B</tt> is low. </p><pre class="codeinput">y = [w(2:3,1) v(2:3,1)];figure();bar(y);set(gca, <span class="string">'xticklabel'</span>, {<span class="string">'Bronchitis'</span>, <span class="string">'Lung Cancer'</span>});ylabel(<span class="string">'Probability'</span>); title(<span class="string">'Explaining away with evidence of dyspnea'</span>)legend(<span class="string">'B = t'</span>, <span class="string">'L = t'</span>, <span class="string">'location'</span>, <span class="string">'SouthEastOutside'</span>);colormap(summer)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -