(*^ ::[ Information = "This is a Mathematica Notebook file. It contains ASCII text, and can be transferred by email, ftp, or other text-file transfer utility. It should be read or edited using a copy of Mathematica or MathReader. If you received this as email, use your mail application or copy/paste to save everything from the line containing (*^ down to the line containing ^*) into a plain text file. On some systems you may have to give the file a name ending with ".ma" to allow Mathematica to recognize it as a Notebook. The line below identifies what version of Mathematica created this file, but it can be opened using any other version as well."; FrontEndVersion = "Macintosh Mathematica Notebook Front End Version 2.2"; MacintoshStandardFontEncoding; fontset = title, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e8, 24, "Times"; fontset = subtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, e6, 18, "Times"; fontset = subsubtitle, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, e6, 14, "Times"; fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, a20, 18, "Times"; fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, a15, 14, "Times"; fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, a12, 12, "Times"; fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times"; fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, L-5, 12, "Courier"; fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5, 12, "Courier"; fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, R65535, L-5, 12, "Courier"; fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5, 12, "Courier"; fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, B65535, L-5, 12, "Courier"; fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287, 12, "Courier"; fontset = name, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, 10, "Geneva"; fontset = header, inactive, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = leftheader, inactive, L2, 12, "Times"; fontset = footer, inactive, noKeepOnOnePage, preserveAspect, center, M7, 12, "Times"; fontset = leftfooter, inactive, L2, 12, "Times"; fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 10, "Times"; fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, 12, "Times"; paletteColors = 128; automaticGrouping; currentKernel; ] :[font = input; preserveAspect] (* This notebook explores some of the ideas presented in Cooper and Herskovits [1992]. The following code assumes familiarity with this paper. *) :[font = input; preserveAspect] Needs["Dynamics`Packages`CooperandHerskovits`"] :[font = input; preserveAspect] (* A database is a list of cases where a case is a list of assignments in which the ith element of the list is the assignment to the ith variables in that particular case. Here is the database in Table 1 of Cooper and Herskovits [1992]. *) :[font = input; preserveAspect] db1 = {{1, 0, 0}, {1, 1, 1}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}, {0, 1, 1}, {1, 1, 1}, {0, 0, 0}, {1, 1, 1}, {0, 0, 0}} ; :[font = input; preserveAspect; startGroup] TableForm[db1] :[font = output; output; inactive; preserveAspect; endGroup] TableForm[{{1, 0, 0}, {1, 1, 1}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}, {0, 1, 1}, {1, 1, 1}, {0, 0, 0}, {1, 1, 1}, {0, 0, 0}}] ;[o] 1 0 0 1 1 1 0 0 1 1 1 1 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 0 0 0 :[font = input; preserveAspect] (* A network structure is represented as a list of lists such that ith list is the list of the parents of the ith variable. For example, the network structure X_1 -> X_2 -> X_ 3 is represented by the list {{},{1},{2}} and the network structure X_3 <- X_1 -> X_2 is represented by the list {{},{1},{1}}. The following correspond to B_{S1} and B_{S2} in Cooper and Herskovits [1992], respectively. *) :[font = input; preserveAspect; startGroup] bs1 = {{},{1},{2}} :[font = output; output; inactive; preserveAspect; endGroup] {{}, {1}, {2}} ;[o] {{}, {1}, {2}} :[font = input; preserveAspect; startGroup] bs2 = {{},{1},{1}} :[font = output; output; inactive; preserveAspect; endGroup] {{}, {1}, {1}} ;[o] {{}, {1}, {1}} :[font = input; preserveAspect] (* Compare the two Bayesian network structures given in Figures 1 and 2 of Cooper and Herskovits [1992]. *) :[font = input; preserveAspect] (* First compute Pr(bs1,db1)/Pr(bs1). *) :[font = input; preserveAspect; startGroup] N[ComputeProbability[bs1,db1]] :[font = output; output; inactive; preserveAspect; endGroup] 2.226854078705930558*10^-9 ;[o] -9 2.22685 10 :[font = input; preserveAspect] (* Next compute Pr(bs2,db1)/Pr(bs2). *) :[font = input; preserveAspect; startGroup] N[ComputeProbability[bs2,db1]] :[font = output; output; inactive; preserveAspect; endGroup] 2.226854078705930558*10^-10 ;[o] -10 2.22685 10 :[font = input; preserveAspect] Needs["Dynamics`Packages`MarkovExamples`"] :[font = input; preserveAspect] Needs["Dynamics`Packages`BayesianNetworks`"] :[font = input; preserveAspect; startGroup] network1 = MarkovExample[1] :[font = output; output; inactive; preserveAspect; endGroup] BayesianNetwork[9] ;[o] BayesianNetwork[9] :[font = input; preserveAspect; startGroup] ShowGraph[DependencyGraph[network1], Directed] ; :[font = postscript; PostScript; formatAsPostScript; output; inactive; preserveAspect; pictureLeft = 34; pictureWidth = 282; pictureHeight = 282; endGroup] %! %%Creator: Mathematica %%AspectRatio: 1 MathPictureStart %% Graphics /Courier findfont 10 scalefont setfont % Scaling calculations -1.72222 2.59259 0.0522388 1.04478 [ [ 0 0 0 0 ] [ 1 1 0 0 ] ] MathScale % Start of Graphics 1 setlinecap 1 setlinejoin newpath [ ] 0 setdash 0 g p P 0 0 m 1 0 L 1 1 L 0 1 L closepath clip newpath p .025 w .12963 .05224 Mdot .12963 .35075 Mdot .12963 .64925 Mdot .12963 .94776 Mdot .87037 .05224 Mdot .87037 .35075 Mdot .87037 .64925 Mdot .87037 .94776 Mdot .004 w .12963 .05224 m .87037 .05224 L s .12963 .35075 m .87037 .35075 L s .12963 .35075 m .87037 .64925 L s .12963 .64925 m .87037 .64925 L s .12963 .94776 m .87037 .94776 L s .87037 .35075 m .87037 .05224 L s .87037 .05224 m .74074 .07836 L .74074 .02612 L F .87037 .35075 m .74074 .37687 L .74074 .32463 L F .87037 .64925 m .73288 .63078 L .82454 .59385 L F .87037 .64925 m .74074 .67537 L .74074 .62313 L F .87037 .94776 m .74074 .97388 L .74074 .92164 L F .87037 .05224 m .93519 .10448 L .80556 .10448 L F P % End of Graphics MathPictureEnd :[font = input; preserveAspect; startGroup] network2 = MarkovExample[2] :[font = output; output; inactive; preserveAspect; endGroup] BayesianNetwork[10] ;[o] BayesianNetwork[10] :[font = input; preserveAspect; startGroup] ShowGraph[DependencyGraph[network2], Directed] ; :[font = postscript; PostScript; formatAsPostScript; output; inactive; preserveAspect; pictureLeft = 34; pictureWidth = 282; pictureHeight = 282; endGroup] %! %%Creator: Mathematica %%AspectRatio: 1 MathPictureStart %% Graphics /Courier findfont 10 scalefont setfont % Scaling calculations -1.72222 2.59259 0.0522388 1.04478 [ [ 0 0 0 0 ] [ 1 1 0 0 ] ] MathScale % Start of Graphics 1 setlinecap 1 setlinejoin newpath [ ] 0 setdash 0 g p P 0 0 m 1 0 L 1 1 L 0 1 L closepath clip newpath p .025 w .12963 .05224 Mdot .12963 .35075 Mdot .12963 .64925 Mdot .12963 .94776 Mdot .87037 .05224 Mdot .87037 .35075 Mdot .87037 .64925 Mdot .87037 .94776 Mdot .004 w .12963 .05224 m .87037 .05224 L s .12963 .35075 m .87037 .35075 L s .12963 .35075 m .87037 .64925 L s .12963 .35075 m .87037 .94776 L s .12963 .64925 m .87037 .64925 L s .12963 .94776 m .87037 .35075 L s .12963 .94776 m .87037 .94776 L s .87037 .35075 m .87037 .05224 L s .87037 .05224 m .74074 .07836 L .74074 .02612 L F .87037 .35075 m .74074 .37687 L .74074 .32463 L F .87037 .64925 m .73288 .63078 L .82454 .59385 L F .87037 .94776 m .75443 .91272 L .87037 .88936 L F .87037 .64925 m .74074 .67537 L .74074 .62313 L F .87037 .35075 m .87037 .40915 L .75443 .38579 L F .87037 .94776 m .74074 .97388 L .74074 .92164 L F .87037 .05224 m .93519 .10448 L .80556 .10448 L F P % End of Graphics MathPictureEnd :[font = input; preserveAspect; startGroup] bs3 = Table[Parents[i,network1],{i,NumVars[network1]}] :[font = output; output; inactive; preserveAspect; endGroup] {{}, {}, {}, {}, {1, 6}, {2}, {2, 3}, {4}} ;[o] {{}, {}, {}, {}, {1, 6}, {2}, {2, 3}, {4}} :[font = input; preserveAspect; startGroup] bs4 = Table[Parents[i,network2],{i,NumVars[network2]}] :[font = output; output; inactive; preserveAspect; endGroup] {{}, {}, {}, {}, {1, 6}, {2, 4}, {2, 3}, {2, 4}} ;[o] {{}, {}, {}, {}, {1, 6}, {2, 4}, {2, 3}, {2, 4}} :[font = input; preserveAspect] (* The Cooper and Herskovits implementation requires that Boolean True be represented as 1 and Boolean False as 0. Here we generate random samples in {True, False} format from the Bayesian network and convert them to {1,0} format. *) :[font = input; preserveAspect] db2 = Table[Map[(If[# == True,1,0])&, RandomSample[network1]], {10}] ; :[font = input; preserveAspect; startGroup] TableForm[db2] :[font = output; output; inactive; preserveAspect; endGroup] TableForm[{{0, 1, 0, 0, 1, 1, 1, 1}, {1, 0, 0, 0, 1, 1, 1, 1}, {1, 1, 1, 0, 1, 1, 1, 1}, {1, 0, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 0, 0}, {0, 1, 0, 0, 1, 1, 1, 1}, {0, 0, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 1, 1, 1, 0, 1}, {0, 0, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 1, 1, 0, 1}}] ;[o] 0 1 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 0 0 0 1 0 0 1 1 1 1 0 0 1 1 1 1 1 1 0 0 0 1 1 1 0 1 0 0 0 1 1 1 0 1 1 0 1 1 1 1 0 1 :[font = input; preserveAspect] (* Compute Pr(bs3,db2)/Pr(bs3). *) :[font = input; preserveAspect; startGroup] N[ComputeProbability[bs3,db2]] :[font = output; output; inactive; preserveAspect; endGroup] 1.764232682734354533*10^-22 ;[o] -22 1.76423 10 :[font = input; preserveAspect] (* Compare with Pr(bs4,db2)/Pr(bs4). *) :[font = input; preserveAspect; startGroup] N[ComputeProbability[bs4,db2]] :[font = output; output; inactive; preserveAspect; endGroup] 9.801292681857525183*10^-23 ;[o] -23 9.80129 10 :[font = input; preserveAspect] Needs["Dynamics`Packages`TemporalBayesianNetworks`"] :[font = input; preserveAspect; startGroup] process1 = TemporalBayesianNetworkToMarkovProcess[network1] :[font = output; output; inactive; preserveAspect; endGroup] MarkovProcess[5] ;[o] MarkovProcess[5] :[font = input; preserveAspect] Needs["Dynamics`Packages`MarkovProcesses`"] :[font = input; preserveAspect; startGroup] ShowGraph[TransitionGraph[process1]] ; :[font = postscript; PostScript; formatAsPostScript; output; inactive; preserveAspect; pictureLeft = 34; pictureWidth = 282; pictureHeight = 282; endGroup] %! %%Creator: Mathematica %%AspectRatio: 1 MathPictureStart %% Graphics /Courier findfont 10 scalefont setfont % Scaling calculations 0.0454545 0.909091 0.0454545 0.909091 [ [ 0 0 0 0 ] [ 1 1 0 0 ] ] MathScale % Start of Graphics 1 setlinecap 1 setlinejoin newpath [ ] 0 setdash 0 g p P 0 0 m 1 0 L 1 1 L 0 1 L closepath clip newpath p .025 w .91995 .67395 Mdot .82141 .82141 Mdot .67395 .91995 Mdot .5 .95455 Mdot .32605 .91995 Mdot .17859 .82141 Mdot .08005 .67395 Mdot .04545 .5 Mdot .08005 .32605 Mdot .17859 .17859 Mdot .32605 .08005 Mdot .5 .04545 Mdot .67395 .08005 Mdot .82141 .17859 Mdot .91995 .32605 Mdot .95455 .5 Mdot .004 w .91995 .67395 m .91995 .67395 L s .91995 .67395 m .82141 .82141 L s .91995 .67395 m .67395 .91995 L s .91995 .67395 m .5 .95455 L s .91995 .67395 m .08005 .32605 L s .91995 .67395 m .17859 .17859 L s .91995 .67395 m .32605 .08005 L s .91995 .67395 m .5 .04545 L s .91995 .67395 m .67395 .08005 L s .91995 .67395 m .82141 .17859 L s .91995 .67395 m .91995 .32605 L s .91995 .67395 m .95455 .5 L s .82141 .82141 m .82141 .82141 L s .82141 .82141 m .5 .95455 L s .82141 .82141 m .17859 .17859 L s .82141 .82141 m .5 .04545 L s .82141 .82141 m .82141 .17859 L s .82141 .82141 m .95455 .5 L s .67395 .91995 m .91995 .67395 L s .67395 .91995 m .82141 .82141 L s .67395 .91995 m .67395 .91995 L s .67395 .91995 m .5 .95455 L s .67395 .91995 m .08005 .32605 L s .67395 .91995 m .17859 .17859 L s .67395 .91995 m .32605 .08005 L s .67395 .91995 m .5 .04545 L s .67395 .91995 m .67395 .08005 L s .67395 .91995 m .82141 .17859 L s .67395 .91995 m .91995 .32605 L s .67395 .91995 m .95455 .5 L s .5 .95455 m .82141 .82141 L s .5 .95455 m .5 .95455 L s .5 .95455 m .17859 .17859 L s .5 .95455 m .5 .04545 L s .5 .95455 m .82141 .17859 L s .5 .95455 m .95455 .5 L s .32605 .91995 m .91995 .32605 L s .32605 .91995 m .95455 .5 L s .17859 .82141 m .95455 .5 L s .08005 .67395 m .91995 .32605 L s .08005 .67395 m .95455 .5 L s .04545 .5 m .95455 .5 L s .08005 .32605 m .91995 .67395 L s .08005 .32605 m .82141 .82141 L s .08005 .32605 m .67395 .91995 L s .08005 .32605 m .5 .95455 L s .08005 .32605 m .08005 .32605 L s .08005 .32605 m .17859 .17859 L s .08005 .32605 m .32605 .08005 L s .08005 .32605 m .5 .04545 L s .08005 .32605 m .67395 .08005 L s .08005 .32605 m .82141 .17859 L s .08005 .32605 m .91995 .32605 L s .08005 .32605 m .95455 .5 L s .17859 .17859 m .82141 .82141 L s .17859 .17859 m .5 .95455 L s .17859 .17859 m .17859 .17859 L s .17859 .17859 m .5 .04545 L s .17859 .17859 m .82141 .17859 L s .17859 .17859 m .95455 .5 L s .32605 .08005 m .91995 .67395 L s .32605 .08005 m .82141 .82141 L s .32605 .08005 m .67395 .91995 L s .32605 .08005 m .5 .95455 L s .32605 .08005 m .08005 .32605 L s .32605 .08005 m .17859 .17859 L s .32605 .08005 m .32605 .08005 L s .32605 .08005 m .5 .04545 L s .32605 .08005 m .67395 .08005 L s .32605 .08005 m .82141 .17859 L s .32605 .08005 m .91995 .32605 L s .32605 .08005 m .95455 .5 L s .5 .04545 m .82141 .82141 L s .5 .04545 m .5 .95455 L s .5 .04545 m .17859 .17859 L s .5 .04545 m .5 .04545 L s .5 .04545 m .82141 .17859 L s .5 .04545 m .95455 .5 L s .67395 .08005 m .91995 .32605 L s .67395 .08005 m .95455 .5 L s .82141 .17859 m .95455 .5 L s .91995 .32605 m .91995 .32605 L s .91995 .32605 m .95455 .5 L s .95455 .5 m .95455 .5 L s P % End of Graphics MathPictureEnd :[font = input; preserveAspect] (* Generate n independent random samples using the Markov process representation. *) :[font = input; preserveAspect] Module[{u,v,s = {},n = 10}, Do[u = Random[Integer,{1,NumStates[process1]}] ; v = RandomNextState[u,process1] ; s = Append[s,Join[IntegerToStateVector[u,process1], IntegerToStateVector[v,process1]]], {n}] ; db3 = s] ; :[font = input; preserveAspect; startGroup] TableForm[db3] :[font = output; output; inactive; preserveAspect; endGroup] TableForm[{{1, 0, 0, 1, 1, 1, 0, 1}, {0, 0, 1, 0, 1, 1, 0, 0}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 1, 0, 1, 1, 0, 0}, {0, 0, 1, 0, 1, 1, 1, 0}, {1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 0, 0}, {0, 0, 1, 0, 1, 1, 1, 0}, {1, 1, 1, 0, 1, 1, 1, 0}, {0, 0, 1, 0, 1, 1, 1, 0}}] ;[o] 1 0 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 1 1 1 1 1 1 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 1 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 1 1 1 0 0 0 1 0 1 1 1 0 :[font = input; preserveAspect] (* Generate n = h k dependent random samples from h trajectories of length k using the Markov process representation. *) :[font = input; preserveAspect] Module[{u,v,s = {},h = 2,k = 5}, Do[u = Random[Integer,{1,NumStates[process1]}] ; Do[v = RandomNextState[u,process1] ; u = v ; s = Append[s,Join[IntegerToStateVector[u,process1], IntegerToStateVector[v,process1]]], {k}], {h}] ; db4 = s] ; :[font = input; preserveAspect; startGroup] TableForm[db4] :[font = output; output; inactive; preserveAspect; endGroup] TableForm[{{1, 0, 1, 0, 1, 0, 1, 0}, {1, 1, 0, 1, 1, 1, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1}}] ;[o] 1 0 1 0 1 0 1 0 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 :[font = input; preserveAspect] (* Compute Pr(bs3,db3)/Pr(bs3). *) :[font = input; preserveAspect; startGroup] N[ComputeProbability[bs3,db3]] :[font = output; output; inactive; preserveAspect; endGroup] 1.575864089004905221*10^-20 ;[o] -20 1.57586 10 :[font = input; preserveAspect] (* Compare with Pr(bs3,db4)/Pr(bs3). *) :[font = input; preserveAspect; startGroup] N[ComputeProbability[bs4,db3]] :[font = output; output; inactive; preserveAspect; endGroup] 2.286968292433422542*10^-21 ;[o] -21 2.28697 10 ^*)