Home
gPy User Manual - University of York
Contents
1. abnormal normal asso CER A MON false 0 05 0 95 true 0 98 0 02 gt gt gt foo asiac condition Dyspnea absent gt gt gt print asiac Bronchitis Dyspnea TbOrCa b an gt absent absent false 0 90 absent absent true 0 30 present absent false 0 20 present absent true 0 10 Bronchitis Smoking pie ELM Eo pei do absent nonsmoker 0 70 absent smoker 0 40 present nonsmoker 0 30 present smoker 0 60 Cancer Smoking llle esce REN absent nonsmoker 0 99 absent smoker 0 90 present nonsmoker 0 01 present smoker 0 10 Cancer TbOrCa Tuberculosis R l gt gt absent false absent 1 00 absent false present 0 00 absent true absent 0 00 absent true present 1 00 present false absent 0 00 present false present 0 00 present true absent 1 00 present true present 1 00 46 Smoking nonsmoker 0 50 smoker 0 50 TbOrCa XRay false abnormal 0 05 false normal 0 95 true abnormal 0 98 true normal 0 02 Tuberculosis VisitAsia absent no visit 0 99 absent visit 0 95 present no visit 0 01 present visit 0 05 VisitAsia Hi SPEM jit no visit 0 99 visit 0 01 Note that the conditioned asiac is no longer a Bayesian network The condition method returns the newly conditioned object so avoid having this printed out to the interpre
2. C D 7B set frozenset C B frozenset A B 7D set frozenset A D frozenset C D 23 Chapter 4 Join Forests To each decomposable hypergraph there is one or more associated join forests A join forest F for a decomposable hypergraph H is an undirected graph whose vertices are the hyperedges of Hwith the following two properties 1 The graph isa forest it is the disjoint union of one or more trees An undirected tree is a graph which contains no cycles 2 The join forest F obeys the join property If hyperedges h and ho are in the same tree in F then for any hyperedge ha on the unique path between h and ho we have hy N ha C ha Join forests are also often referred to as junction forests for example in 2 Also many presentations make the assumption that there is only one tree in the forest and so talk about join trees or junction trees In gPy join forests are constructed similarly to DecomposableHypergraph objects with one differences the JoinForest constructor does not accept a check argument since the construction of a join forest inevitably checks that the input hypergraph is decomposable The JoinForest class is a direct sub class of the DecomposableHypergraph class see Fig 4 1 for the full hierarchy of hypergraphs JoinForest objects have an additional _uforest attribute which contains the join forest itself The join fores
3. C present true 0 10000000000000001 0 90000000000000002 41 Chapter 7 Models 7 1 Altering models The two central ways of altering an existing model are by adding and removing factors When removing a factor the factor is specified by its variables and the remove method is used gt gt gt from gPy Examples import asia gt gt gt asiac asia copy gt gt gt asiac remove Bronchitis Smoking gt gt gt print asiac Bronchitis TbOrCa Dyspnea absent present absent false 0 90 0 10 absent true 0 30 0 70 present false 0 20 0 80 present true 0 10 0 90 Smoking Cancer absent present See tes Lis 2 nonsmoker 0 99 0 01 smoker 0 90 0 10 Cancer Tuberculosis TbOrCa false true HERMES pee eu ee ee absent absent 1 00 0 00 42 absent present 0 00 1 00 present absent 0 00 1 00 present present 0 00 1 00 Smoking nonsmoker smoker TbOrCa XRay abnormal normal false 0 05 0 95 true 0 98 0 02 VisitAsia Tuberculosis absent present no_visit 0 99 0 01 visit 0 95 0 05 VisitAsia no visit visit gt gt gt type asiac class gPy Models SFR In the example immediately above the factor with Bronchitis and Smoking as variables has been removed As a result the asiac object is no longer a BN Bayesian network object and has become a mere SFR a simple factored representation object Nonetheless since its f
4. CPT factor copy Bronchitis cpt_force True gt gt gt print cpt Dyspnea TbOrCa Bronchitis absent present absent false 0 82 0 18 absent true 0 75 0 25 present false 0 11 0 89 present true 0 44 0 56 Sometimes this normalisation can produce a division by zero which raises an exception It is sometimes convenient to permit a fake CPT to be constructed even in this situation To do so use the allow dummies flag gt gt gt factor asia TbOrCa 1 gt gt gt print factor Cancer TbOrCa Tuberculosis 595 9 pp a a EE absent false absent 1 00 absent false present 0 00 absent true absent 0 00 absent true present 1 00 present false absent 0 00 present false present 0 00 present true absent 1 00 38 present true present 1 00 gt gt gt cpt CPT factor copyO Tuberculosis cpt force True Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Parameters py line 911 in init self data indx prob sum ZeroDivisionError float division gt gt gt cpt CPT factor copyO Tuberculosis cpt force True allow dummies True gt gt gt print cpt Cancer TbOrCa Tuberculosis absent present absent false 1 00 0 00 absent true 0 00 1 00 present false 0 00 0 00 present true 0 50 0 50 Note that the row of the CPT where the zero division error has bee
5. Examples of adding redundant edges etc 3 6 Generating objects from hypergraphs 3 6 1 Generating hypergraphs from hypergraphs 3 6 2 Generating graphs from hypergraphs Each hypergraph H has an associated undirected graph Hj called its 2 section Hjo has the same vertices as H and two vertices are connected in Hjoj if and only if they are both elements of some hyperedge in H 2 sections are generated using the two section method of gPy Hypergraphs Hypergraph gt gt gt print hgl CHA B C C D B C TA D gt gt gt print hg1 two_section Vertices PA B C D Lines A B A C A D B C C D Fig 3 2 provides an example of a two section graph generated from a hypergraph To generate the clique hypergraph gPy provides the gPy Graphs UGraph method hypergraph Here we generate the hypergraph of the graph displayed in Fig 3 2 gt gt gt g1 hg1 two section gt gt gt print gl Vertices A 2B 20 D gt Lines A B 21 D Figure 3 2 The graph of the hypergraph A B C B C A DL C DM Q U gt gt Tawa gt gt gt print g1 hypergraph A B C 4 C D 3 It is interesting to examine the relationship between a hypergraph 4 gt gt gt print hgl TA B C 10 D B C TA D and C Hjo the clique hypergraph of its two section gt gt gt print hg1 two_section hypergraph A B C TA C D
6. It is not difficult to prove that for any hypergraph H every hyperedge in H is contained within some hyperedge in C 71j However the reverse does not always follow In the example above the hyperedge A C D of C Hiaj is not contained in any hyperedge of H This means that H is not graphical 3 7 Internal representation of hypergraphs Internally each hyperedge is stored as a frozenset of vertices Each hyper graph has a private attribute hyperedges which is just the set of these hy peredges which constitute the hypergraph For simple hypergraphs of which more later this attribute contains all the information required to define the hy pergraph However it is useful to also maintain a mapping from each vertex to the the set of hyperedges which contain it its star This is essentially the dual hypergraph l This mapping a Python dictionary is the private attribute star Here is the internal representation of the hg1 hypergraph given earlier with added fake line breaks to make the output readable 22 gt gt gt hg1 Hypergraph AB BC CD AD gt gt gt for att val in vars hg1 items print att print val print hyperedges set frozenset C B frozenset A D frozenset A B frozenset C D star A set frozenset A D frozenset A B C set frozenset C B frozenset
7. absent no_visit abnormal 0 01 absent absent absent smoker false absent no visit normal 0 15 absent absent absent smoker false absent visit abnormal 0 00 absent absent absent smoker false absent visit normal 0 00 absent absent absent smoker false present no_visit abnormal 0 00 absent absent absent smoker false present no_visit normal 0 00 absent absent absent smoker false present visit abnormal 0 00 absent absent absent smoker false present visit normal 0 00 absent absent absent smoker true absent no_visit abnormal 0 00 absent absent absent smoker true absent no_visit normal 0 00 absent absent absent smoker true absent visit abnormal 0 00 absent absent absent smoker true absent visit normal 0 00 absent absent absent smoker true present no_visit abnormal 0 00 absent absent absent smoker true present no_visit normal 0 00 absent absent absent smoker true present visit abnormal 0 00 absent absent absent smoker true present visit normal 0 00 absent absent present nonsmoker false absent no_visit abnormal 0 00 absent absent present nonsmoker false absent no_visit normal 0 03 absent absent present nonsmoker false absent visit abnormal 0 00 absent absent present nonsmoke
8. n C frozenset 1 2 31 7B frozenset x o These functions are rarely useful since variables can be added to the internal de fault domain using new_domain_variables when an object that needs these new variables is constructed However sometimes it is neater to declare all vari ables that your code ever needs with a call to Variables set_default_domain and then there is no need to use the new_domain_variables argument when constructing objects that need these variables 6 2 Copying factors todo also copy_rename 6 3 Accessing and modifying data in factors Data in a factor can be accessed using the usual Python indexing syntax There are a three permissible forms of index Dictionary lf a dictionary is supplied as an index the keys of the dictionary should be the variables in the factor Associated with each variable key should be a single value for that variable The single data value associated with the joint instantiation thus specified is returned Sequence If a sequence is supplied it should be a sequence of variable values ordered according to the lexicographical ordering of the variable names This specifies a joint instantiation corresponding to a row of the printed representation of a factor and the corresponding data value is returned Anything else Any index which is not a dictionary or a sequence is viewed as an index for the internal data attribute a list which holds the dat
9. 0 30 smoker 0 40 0 60 Smoking Cancer absent present nonsmoker 0 99 0 01 smoker 0 90 0 10 Bronchitis TbOrCa Dyspnea absent present absent false 0 90 0 10 absent true 0 30 0 70 present false 0 20 0 80 present true 0 10 0 90 Smoking nonsmoker smoker 0 50 0 50 Cancer Tuberculosis TbOrCa false true TE NDZ aa anaa NY aaa aaa absent absent 1 00 0 00 absent present 0 00 1 00 present absent 0 00 1 00 present present 0 00 1 00 VisitAsia Tuberculosis absent present SEL MANIKA shoe e ce no visit 0 99 0 01 visit 0 95 0 05 VisitAsia no visit visit TbOrCa XRay abnormal normal dist strat ee ee Le LIS false 0 05 0 95 true 0 98 0 02 So the textual representation of a BN is simply a list of the CPTs in the BN Each CPT in the BN can be accessed via the name of its child variable gt gt gt print asia Bronchitis Smoking Bronchitis absent present nonsmoker 0 70 0 30 smoker 0 40 0 60 gt gt gt print asia Smoking Smoking nonsmoker smoker A basic operation for graphical models is factor multiplication CPTs are a special sort of factor To multiply two CPTs we just use the operator gt gt gt example factor asia Bronchitis asia Smoking gt gt gt print example factor Bronchitis Smoking absent nonsmoker 0 35 absent smoker 0 20 present nonsmoker 0 15 present smoker 0 30 exa
10. 00 present present present smoker true present visit abnormal 0 00 present present present smoker true present visit normal 0 00 The default precision of two decimal places causes a lot of rounding errors in the presentation To change this pull the Parameters module into your namespace and alter the value of its precision variable gt gt gt import gPy Parameters gt gt gt gPy Parameters precision 4 If you were to print out joint again I won t since it takes up so much space you would get some more numbers after the decimal point 10 Chapter 3 Hypergraphs In gPy hypergraphs rather than graphs are the central structure although the latter still play an important role A hypergraph H is simply a collection of subsets of a finite set H These subsets h H are known as hyperedges H is called the base set The elements of H are known as vertices In gPy hypergraphs are restricted so that H Jj ep A every element of the base set is contained in at least one hyperedge Table 3 1 gives some example hypergraphs 3 1 Constructing hypergraphs In gPy hypergraphs are object of the class gPy Hypergraph Hypergraph or one of its subclasses To construct a hypergraph it suffices to send the desired hyperedges to the Hypergraph constructor method gt gt gt from gPy Hypergraphs import Hypergraph gt gt gt hg1 Hypergraph AB BC CD DA gt gt gt hg2 H
11. 6 on linux2 Type help copyright credits or license for more information gt gt gt from gPy Parameters import Factor gt gt gt f Factor ABC new_domain_variables A yn B ox C 1 2 3 gt gt gt print f BIC pi 00 00 00 00 00 00 00 00 00 00 00 00 wo Sse wd MB PH DB B BB M MOHO O O K K K O O O WNE W NR WDR W NV HH RARRRRRRERE ER HR 28 Refer to the API documentation for the full details of arguments that can be sent to the Factor constructor In the case above a factor with 3 variables A B and C was made The values the variables can take was specified by sending an appropriate dictionary as the value of new domain variables argument Note that no data values were supplied and a default value of 1 is thus given to every joint instantiation of the variables Here s an example which is a continuation of the session just above of supplying data values when constructing a factor gt gt gt g Factor ABC data range 2 2 3 gt gt gt print g A BICI nlolil 0 n o 2 1 n ol3 2 nix 1 3 n x 2 4 n x 3 5 ylol1 6 ylol2l 7 ylol3 8 ylx 1 9 ylx 2Il 10 ylxl3 11 Note that the data is expected to be a sequence of values of the right length for the factor and the data values in this sequence are assigned to the joint instantations according to the standard lexicographic ordering of variables and their value
12. A simple hypergraph has no repeated hyperedges A reduced hypergraph contains no hyperedge contained in another and so is always simple A graphical hypergraph is such that its reduction constitutes the cliques of some undirected graph A decomposable hypergraph is a graphical hypergraph whose reduction constitutes the cliques of some tri angulated graph Table 3 2 repeats the example hypergraphs of Table 3 1 but with their clas sifications added Fig 3 1 gives the hierarchical relationships between these classes To construct objects of any of Hypergraph s subclasses one option is to send a Hypergraph to the class constructor if this object meets the conditions of the subclass i e is reduced graphical decomposable etc then an object of the required class is returned if not an exception is raised The returned object will share its attributes with the inputted object Here s some examples of successful attempts to construct various objects gt gt gt from gPy Hypergraphs import gt gt gt sg SimpleHypergraph Hypergraph ABC BCD gt gt gt print sg 12 Hypergraph SimpleHypergraph GraphicalHypergraph ReducedHypergraph DecomposableHypergraph ReducedGraphicalHypergraph ReducedDecomposableHypergraph JoinForest Figure 3 1 Hypergraph hierarchy 13 B C D 4 B C gt gt gt dg DecomposableHypergraph Hypergraph ABC BCD gt gt gt g
13. Tuberculosis is not decomposable 7 4 2 Calibrating join forest models To calibrate a join forest model just use the calibrate method Note that currently gPy does not keep track of whether a JFR is calibrated or not In the following example the default precision two for printing numbers in factors makes some numbers appear to be zeroes when they are not This can be fixed by setting gPy Parameters precision to a larger value gt gt gt asia jfm JFR asia copy O modify True gt gt gt asia jfm calibrate gt gt gt print asia jfm Cligues Bronchitis Dyspnea TbOrCa purum najan nana ta I ia absent absent false 0 47 absent absent true 0 01 absent present false 0 05 absent present true 0 02 present absent false 0 08 present absent true 0 00 present present false 0 33 present present true 0 03 Bronchitis Smoking TbOrCa CE DES eue pe absent nonsmoker false 0 34 absent nonsmoker true 0 01 51 O O O O O O O O O 49 01 45 00 00 00 00 05 absent smoker false absent smoker true present nonsmoker false present nonsmoker true present smoker false present smoker true Cancer Smoking TbOrCa a Nn SSA absent nonsmoker false absent nonsmoker true absent smoker false absent smoker true present nonsmoker false present nonsmoker
14. absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit normal abnormal
15. at time of writing gt gt gt for i in xrange 30 print bias_coin sample hthhhtthhthhthhhththhhthhhttth 55 Bibliography 1 Claude Berge Graphs and hypergraphs North Holland Amsterdam 1973 2 Steffen L Lauritzen Graphical Models Oxford University Press Oxford 1996 3 Robert E Tarjan and Mihalis Yannakakis Simple linear time algorithms to test chordality of graphs test acyclicity of hypergraphs and selectively reduce acyclic hypergraphs SIAM Journal of Computing 13 3 566 579 August 1984 56
16. be used if it not used afterwards and so will be garbage collected at some point or it can even be explicitly deleted although this would rarely be necessary If you want to construct a new hypergraph from an existing one and wish the two to have independent existences just use a copy which will be a deep copy of the existing hypergraph to construct the new one gt gt gt hg Hypergraph C AB BC CD gt gt gt rhg ReducedHypergraph hg copy gt gt gt hg add hyperedge A gt gt gt print hg C B C A B A C D gt gt gt print rhg B C TA B C D 3 2 Getting information about hypergraphs 3 2 1 Basic information gt gt gt from gPy Hypergraphs import gt gt gt hg5 Hypergraph TE TAG AGS F00 gt gt gt print hg5 F 0 SUA G T A G S 1E T gt gt gt TE in hg5 True gt gt gt 2TA in hg5 False gt gt gt PT E in hg5 True gt gt gt T E in hg5 True gt gt gt set T E in hg5 True gt gt gt 3 2 2 Paths in hypergraphs Python 2 4 3 1 Jul 26 2006 20 13 39 GCC 3 4 6 on linux2 Type help copyright credits or license for more information gt gt gt from gPy Hypergraphs import gt gt gt hg4 Hypergraph AT TE TAG AGS 19 gt gt gt print hg4 44 G T A T TA G S E T D gt gt gt hg4 reacha
17. identical shared attributes with the CPT including a child attribute specifying the child variable The only difference is the class of the objects To avoid any possible confusion a safe bet is to use a copy of a factor to construct a CPT gt gt gt print factor Bronchitis Dyspnea TbOrCa pono seme 7 1 m absent absent false 0 90 absent absent true 0 30 absent present false 0 10 absent present true 0 70 present absent false 0 20 present absent true 0 10 present present false 0 80 present present true 0 90 gt gt gt cpt CPT factor copy Dyspnea 37 Options for constructing CPTs Firstly note that the default behaviour for constructing CPTs is not to check that the resulting object is a valid CPT This is because it is often useful to construct fake CP Ts To check that the factor used to construct a CPT is indeed a CPT use the cpt check flag gt gt gt cpt CPT factor copy Bronchitis cpt_check True Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Parameters py line 919 in __init__ raise CPTError errmsg gPy Parameters CPTError For child Bronchitis For row 0 4 Sum was 1 10 should be 1 0 In other cases it is useful to normalise data values in the input factor so that the factor is forced to become a CPT To do this use the cpt_force flag gt gt gt cpt
18. jc godot research gPy gPy Hypergraphs py line 689 in add_hyperedge raise RedundancyError s would make s no longer reduced hyperedge self gPy Hypergraphs RedundancyError Adding A would make B C A C TA B no longer reduced gt gt gt hg Hypergraph rg gt gt gt hg add hyperedge A gt gt gt print hg C B C TA C TA B A As a convenience it is also possible to construct any type of hypergraph directly from hyperedges exactly as described for the Hypergraph class in Sec tion 3 1 gt gt gt dg1 DecomposableHypergraph ABC BCD gt gt gt ggl GraphicalHypergraph AB BC CD AD gt gt gt rgl ReducedHypergraph AB BC CA When called in this way a temporary internal Hypergraph is constructed from the hyperedges and the object is constructed from this temporary Hypergraph exactly as it had been supplied as an argument 3 1 2 Constructing hypergraphs without checks In some cases you may know that a particular hypergraph has the properties required for a hypergraph class below it in the hierarchy Fig 3 1 and wish to avoid the cost of pointlessly checking that it meets the necessary conditions In this case an object of the relevant class can be constructed using the optional check argument set to False the default it True This facility should only be used if you are absolutely sure that no checking is required otherwise
19. true present smoker false present smoker true Cancer TbOrCa Tuberculosis absent false absent absent false present absent true absent absent true present present false absent present false present present true absent present true present TbOrCa XRay zac K ja false abnormal 0 05 false normal 0 89 true abnormal 0 06 true normal 0 00 Tuberculosis VisitAsia alad is OCZ kasa m absent no visit 0 98 absent visit 0 01 present no visit 0 01 present visit 0 00 52 18 02 15 00 27 03 94 00 00 01 00 00 05 00 Separators Bronchitis TbOrCa absent false 0 52 absent true 0 03 present false 0 41 present true 0 04 Cancer TbOrCa ZOE E Roz absent false 0 94 absent true 0 01 present false 0 00 present true 0 05 Smoking TbOrCa a ANAN NA nonsmoker false 0 49 nonsmoker true 0 01 smoker false 0 45 smoker true 0 05 TbOrCa A ss false 0 94 true 0 06 Tuberculosis KANEA DARA CE has absent 0 99 present 0 01 Join Forest Bronchitis Smoking TbOrCa tTbOrCa XRay Cancer Smoking TbOrCa Tuberculosis VisitAsia Bronchitis Dyspnea TbOrCa Cancer TbOrCa Tuberculosis Vertices Bronchitis Smoking TbOrCa TbOrCa XRay Cancer Smoking TbOrCa Tuberculosis Vis
20. used as indices to e g modify data values of the factor although this is a rather inefficient approach gt gt gt print factor Bronchitis Dyspnea TbOrCa SRSRTSERAL 2899999 S 4 sess absent absent false 0 90 absent absent true 0 30 absent present false 0 10 absent present true 0 70 34 present present present present absent absent present present false 0 20 true 0 10 false 0 80 true 0 90 gt gt gt for inst in factor insts print dict zip sorted variables inst TbOrCa false TbOrCa true TbOrCa false TbOrCa true TbOrCa false C TbOrCa true C TbOrCa false C TbOrCa true Dyspnea absent Bronchitis absent Dyspnea absent Bronchitis absent Dyspnea present Bronchitis absent Dyspnea present Bronchitis absent gt Dyspnea absent Bronchitis present Dyspnea absent Bronchitis present Dyspnea present Bronchitis present Dyspnea present Bronchitis present gt gt gt for inst in factor insts factor dict zip sorted_variables inst 0 4 gt gt gt print factor Bronchitis absent absent absent absent present present present present Dyspnea absent absent p
21. you can for example construct a ReducedHypergraph object that is not reduced gt gt gt hg Hypergraph BC AB A CD gt gt gt print hg C B CF TA B A C D gt gt gt r ReducedHypergraph hg copy O Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Hypergraphs py line 678 in __init_ 15 if hypergraph is None gPy Hypergraphs RedundancyError B Ch A B A C D is not reduced due to frozenset A gt gt gt r ReducedHypergraph hg copy O check False gt gt gt print r IB C A B A C D 3 1 3 Constructing hypergraphs by copying existing hy pergraphs New hypergraphs can be made by copying existing hypergraphs The copy is a deep copy it is completely independent from the original any alterations done to one will have no effect on the other gt gt gt hg Hypergraph AB BC CD DA gt gt gt hg cp hg copyO gt gt gt hg add hyperedge C EF gt gt gt hg cp remove hyperedge AB gt gt gt print hg E F3 TB C A D TA B C D gt gt gt print hg cp C C D B C A D D 3 1 4 Constructing hypergraphs by modifying existing hy pergraphs It is often useful to construct a hypergraph meeting certain conditions from a hypergraph which does not meet these conditions For example we can con struct a reduced hy
22. DA AE gt gt gt dg2a DecomposableHypergraph hg2 copy modify True elimination_order ABCDE gt gt gt dg2b DecomposableHypergraph hg2 copy modify True gt gt gt print dg2a B C D E A B D E gt gt gt print dg2b C B C D B D A E 3 1 5 Potential pitfalls in creating hypergraphs Recall that a new hypergraph constructed from an old one has identical at tributes to the original hypergraph This can lead a hypergraph to get into an illegal state gt gt gt rg ReducedHypergraph Hypergraph AB BC CA gt gt gt hg Hypergraph rg gt gt gt hg add_hyperedge A gt gt gt rg _hyperedges is hg _hyperedges True gt gt gt print rg I B C A C A B A gt gt gt type rg lt class gPy Hypergraphs ReducedHypergraph gt Because rg and hg share the same hyperedges attribute adding the redun dant hyperedge to hg also adds it to rg even though the latter is of class ReducedHypergraph This behaviour is allowed in gPy since it is sometimes 18 useful to have distinct hypergraph objects sharing the same attributes Also it is very easy to avoid the problem of illegal states construct hypergraphs using unnamed input hypergraphs in which case the input hypergraph will be immediately garbage collected This approach was taken in many of the ex amples given above Alternatively a named input hypergraph can
23. TbOrCa Tuberculosis Vertices Bronchitis Smoking TbOrCa TbOrCa XRay Cancer Smoking Tb rCa Tuberculosis VisitAsia Bronchitis Dyspnea TbOrCa Cancer TbOrCa Tuberculosis Lines Cancer Smoking TbOrCa Bronchitis Smoking TbOrCa TbOrCa XRay Bronchitis Smoking TbOrCa Bronchitis Dyspnea TbOrCa Bronchitis Smoking TbOrCa Cancer TbO0rCa Tuberculosis Cancer Smoking TbOrCa Cancer TbOrCa Tuberculosis Tuberculosis VisitAsia If the modify flag is not set and the input model is not decomposable then 50 an exception will be raised Here is an example of this behaviour gt gt gt from gPy Models import JFR gt gt gt from gPy Examples import asia gt gt gt asia jfm ex JFR asia copy True Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Models py line 1009 in init join forest ReducedJoinForest hm hypergraph modify True elimination order File home jc godot research gPy gPy Hypergraphs py line 2160 in init hypergraph uforest hypergraph join forest File home jc godot research gPy gPy Hypergraphs py line 814 in join forest raise DecomposabilityError s is not decomposable self gPy Hypergraphs DecomposabilityError TbO0rCa XRay Bronchitis Smoking Tuberculosis VisitAsia VisitAsia Smoking Bronchitis Dyspnea TbOrCa Cancer Smoking Cancer TbOrCa
24. a Here are some examples of using these various indexing options to yank out data values Python 2 4 3 1 Jul 26 2006 20 13 39 GCC 3 4 6 on linux2 Type help copyright credits or license for more information 30 gt gt gt from gPy Examples import asia gt gt gt factor asia Dyspnea 1 gt gt gt import gPy Parameters gt gt gt gPy Parameters precision 2 gt gt gt print factor Bronchitis Dyspnea TbOrCa A eeu absent absent false 0 90 absent absent true 0 30 absent present false 0 10 absent present true 0 70 present absent false 0 20 present absent true 0 10 present present false 0 80 present present true 0 90 gt gt gt factor 2 0 10000000000000001 gt gt gt factor absent absent true 0 29999999999999999 gt gt gt factor Dyspnea absent Bronchitis absent TbOrCa true 0 29999999999999999 gt gt gt factor 2 4 0 10000000000000001 0 69999999999999996 One can also set data values in the same way gt gt gt factor Bronchitis absent Tb0rCa false Dyspnea absent 0 7 gt gt gt print factor Bronchitis Dyspnea TbOrCa purum najan nana ni a raaa ex absent absent false 0 70 absent absent true 0 30 absent present false 0 10 absent present true 0 70 present absent false 0 20 p
25. actors all remain CPTs they are printed out as such 7 2 Copying models Some methods alter the model object upon which they are called Methods which perform conditioning see Section 7 3 are an important example If the original model object is still required such methods need to be called on a copy of the model not the model itself 43 gt gt gt from gPy Examples import asia gt gt gt asia cp asia copyO gt gt gt asia cp remove Cancer gt gt gt print asia Cancer Smoking Cancer absent present nonsmoker 0 99 0 01 smoker 0 90 0 10 gt gt gt print asia cp Cancer Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Models py line 82 in __getitem__ return self factors frozenset hyperedge KeyError frozenset a C e c n r Note that in this example the CPT for Cancer was removed by just send the string Cancer to the remove method This is a special way of removing CPTs that only works for BN objects For all model classes model copy O returns a copy of the model However the default behaviour of copy is to make a shallow copy where the model and its copy share the same domain the same dictionary mapping variables to their set of possible values In this case altering the domain of the copy also alters that of the original model Conditioning a model does exactly this observing a va
26. ble T A PE G S gt gt gt hg4 reachable T A G E gt gt gt hg4 separates T E A False gt gt gt hg4 separates T G A False gt gt gt hg4 separates UD PS D 4 D False gt gt gt hg4 separates L D 1 PS DA 9 1 True gt gt gt 3 2 3 Iterators for hypergraphs gt gt gt from gPy Hypergraphs import gt gt gt hg5 Hypergraph TE TAG AGS F00 gt gt gt for h in hg5 print h frozenset 0 F frozenset A T G frozenset A S G frozenset E T gt gt gt 3 3 Printing and displaying hypergraphs 3 4 Relations between hypergraphs 3 5 Altering hypergraphs Hypergraphs are mutable objects and so can be altered The two basic operators are add_hyperedge and remove_hyperedge which work as follows gt gt gt hgl Hypergraph BC AD AB CD gt gt gt print hgl C D B Ch A D A B gt gt gt hgi add hyperedge ABC gt gt gt print hgi IA B Ch C D B Ch A D A B D 20 gt gt gt hgi remove_hyperedge A B gt gt gt print hgi XA B C C D 1B C A D Note the flexibility with which hyperedges are specified Any sequence will do in fact any iterable 3 5 1 Altering hypergraphs with specific properties
27. ent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent present present present present present present present present present present nonsmoker smoker smoker smok
28. er smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker true false false false false false false false false true true true true true true true true false false false false false false false false true true true true true true true true false false false false false false false false true true true true true true true true false false false false false false false false true true true true true true true true false false false false false false false false true true true true true true true true false false false false false false false false true true present absent absent absent absent present present present present
29. ethods for iterating over the rows of CPTs For example parent insts allows you to iterate through the possible joint instan tiations of the parents in a CPT gt gt gt from gPy Examples import asia gt gt gt print asia Dyspnea Bronchitis TbOrCa Dyspnea absent present absent false 0 90 0 10 absent true 0 30 0 70 present false 0 20 0 80 present true 0 10 0 90 gt gt gt for row in asia Dyspnea parent insts print row absent false C absent true 40 C present false present true The parent_insts_data method iterates over the data values corresponding to each instantiation of the parents gt gt gt for data row in asia Dyspnea parent insts data print data row 0 90000000000000002 0 10000000000000001 0 29999999999999999 0 69999999999999996 0 20000000000000001 0 80000000000000004 0 10000000000000001 0 90000000000000002 Both of these methods return iterators and so have a next method for generating the next item in the iteration The next method is useful for parallel iteration gt gt gt data itr asia Dyspnea parent insts data gt gt gt for row in asia Dyspnea parent insts print row data itr next Cabsent false 0 90000000000000002 0 10000000000000001 C absent true 0 29999999999999999 0 69999999999999996 Cpresent false 0 20000000000000001 0 80000000000000004
30. f factor which define an in general different probability distribution for one of factor s variables the child variable conditional on each joint in stantiation of the other variables in the factor These are the factors which parameterise a Bayesian network 6 5 1 Constructing CPTs The only way of constructing a CPT is to use an existing factor and specify which variable is to be the child The simplest case is when the factor already happens to be a valid CPT gt gt gt from gPy Examples import asia gt gt gt factor asia Dyspnea 1 gt gt gt print factor Bronchitis Dyspnea TbOrCa absent absent false 0 90 absent absent true 0 30 absent present false 0 10 absent present true 0 70 present absent false 0 20 present absent true 0 10 present present false 0 80 present present true 0 90 gt gt gt from gPy Parameters import CPT gt gt gt cpt CPT factor Dyspnea gt gt gt print cpt Bronchitis TbOrCa Dyspnea absent present absent false 0 90 0 10 absent true 0 30 0 70 present false 0 20 0 80 present true 0 10 0 90 Note that a factor which is a CPT has a different printed representation to a normal factor Internally the only difference apart from the class is the specification of the child variable Potential pitfalls in constructing CPTs After a CPT object has been constructed the factor used to construct it has
31. g GraphicalHypergraph Hypergraph AB BC CD AD gt gt gt rg ReducedHypergraph Hypergraph AB BC CA Note that the simple hypergraph sg is printed out using and as delimiters to make evident that that it is a set of hyperedges Generally a hypergraph need not be a set of hyperedges due to the possible existence of repeated hyper edges Here are some failed attempts to construct particular sorts of hypergraphs gt gt gt hg1 Hypergraph ABC BC CD DA gt gt gt print hgl C IA B C TB C TA D C D gt gt gt rhg ReducedHypergraph hg1 Traceback most recent call last File lt stdin gt line 1 in module File home jc godot research gPy gPy Hypergraphs py line 1679 in __init__ raise RedundancyError s is not reduced due to 4s 4 hypergraph h gPy Hypergraphs RedundancyError A B Cj B C A D C D is not reduced du gt gt gt ghg GraphicalHypergraph hg1 Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Hypergraphs py line 1959 in init raise GraphicalityError s is not graphical hypergraph gPy Hypergraphs GraphicalityError A B C B C TA D C D D is not graphical gt gt gt dhg DecomposableHypergraph hg1 Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Hy
32. gPy User Manual James Cussens University of York 21 1 2009 Abstract gPy is a Python package intended to help students and others understand algo rithms related to graphical models particularly Bayesian networks Python was chosen since it is easy to learn high level and because it is executable pseudo code Why present an algorithm using non executable pseudo code when an executable version is possible The object oriented features of Python are used to reinforce commonalities between different graphical models gPy does not contain any algorithms not found elsewhere and since it is implemented in an interpreted language it will not provide the fastest imple mentation of those algorithms The only thing you need to have to use gPy is an installation of Python 2 4 or higher If you re running a reasonably recent distribution of Linux Python 2 4 will probably already be available In any event the latest version of Python for Linux Windows and Mac can be downloaded from www python org Contents 1 2 3 4 5 6 How to read this manual 3 Quick start 4 Hypergraphs 11 3 1 Constructing hypergraphs les 11 3 1 1 Constructing hypergraphs with specific properties 12 3 1 2 Constructing hypergraphs without checks 15 3 1 3 Constructing hypergraphs by copying existing hypergraphs 16 3 1 4 Constructing hypergraphs by modifying existing hyper graphs eoe eu ihr a ne UP Ru mu we LM 16 3 1 5 Potential pi
33. good C TA E TA D A C A B gt gt gt dg lessbad DecomposableHypergraph hg copy modify True elimination order BCADE gt gt gt print dg lessbad IA C TA B A D E D 17 hg here is in fact already decomposable but by choosing to eliminate A first we construct dg bad in which all hyperedges of hg have been merged into one big hyperedge The elimination order where A is left to last avoids the need to merge any hyperedges dg good whereas dg_lessbad does some unnecessary merging but less than dg bad Naturally it would be nice to automatically find good elimination orders Unfortunately this is a NP hard problem but we can still find reasonable strate gies In gPy if no elimination order is supplied restricted maximum cardinality search on hypergraphs 3 is used to find one Maximum cardinality search has the nice property that if the input hypergraph is already decomposable an ordering is found which does not create any new hyperedges gt gt gt dg DecomposableHypergraph hg copy O modify True gt gt gt print dg C TA E TA D A C A B If the input hypergraph is not decomposable then the ordering is usually rea sonably good but not always Here s an example of a bad ordering ABCDE creating a hypergraph with big hyperedges and the internally generated maxi mum cardinality search ordering doing a better job gt gt gt hg2 Hypergraph AB BC CD
34. itAsia Bronchitis Dyspnea TbOrCa Cancer TbOrCa Tuberculosis 53 Lines Cancer Smoking Tb rCa Bronchitis Smoking TbOrCaj TbOrCa XRay Bronchitis Smoking TbOrCa Bronchitis Dyspnea Tb rCa Bronchitis Smoking TbOrCa Cancer TbO0rCa Tuberculosis Cancer Smoking TbOrCa Cancer TbOrCa Tuberculosis Tuberculosis VisitAsia 7 4 3 Extracting single variable marginals from join forest models As a convenience gPy provides the var_marginal method which can be used to extract the marginal distribution over a single variable from a JFM var_marginal returns a parentless CPT The assumption is that the JFM in question has already been calibrated gt gt gt print asia_jfm var_marginal Dyspnea Dyspnea absent present gt gt gt print asia jfm var marginal Bronchitis Bronchitis absent present gt gt gt print asia_jfm var_marginal Bronchitis absent 0 55 54 Chapter 8 Samplers 8 1 Sampling from unstructured discrete distri butions To sample from arbitrary discrete distributions gPy provides the gPy Samplers MultinomialSampler class To construct an object of this class a dictionary mapping each value of the distribution to its probability must be provided gt gt gt from gPy Samplers import MultinomialSampler gt gt gt bias coin MultinomialSampler h 0 7 t 0 3 The MultinomialSampler class provides a single sample method
35. joint Bronchitis Cancer Dyspnea Smoking TbOrCa Tuberculosis VisitAsia XRay I R AA es Saree cz eee eae ian aana D Denen absent absent absent nonsmoker false absent no_visit abnormal 0 02 absent absent absent nonsmoker false absent no_visit normal 0 29 absent absent absent nonsmoker false absent visit abnormal 0 00 absent absent absent nonsmoker false absent visit normal 0 00 absent absent absent nonsmoker false present no_visit abnormal 0 00 absent absent absent nonsmoker false present no_visit normal 0 00 absent absent absent nonsmoker false present visit abnormal 0 00 absent absent absent nonsmoker false present visit normal 0 00 absent absent absent nonsmoker true absent no_visit abnormal 0 00 absent absent absent nonsmoker true absent no visit normal 0 00 absent absent absent nonsmoker true absent visit abnormal 0 00 absent absent absent nonsmoker true absent visit normal 0 00 absent absent absent nonsmoker true present no_visit abnormal 0 00 absent absent absent nonsmoker true present no visit normal 0 00 absent absent absent nonsmoker true present visit abnormal 0 00 absent absent absent nonsmoker true present visit normal 0 00 absent absent absent smoker false
36. moker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker false false false false true true true true true true true true false false false false false false false false true true true true true true true true false false false false false false false false true true true true true true true true false false false false false false false false true true true true true true true true false false false false false false false false true true true true true true true true false false false false false false false false true true true true true true true present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present present absent absent absent absent present present present no visit no visit visit visit no visit no visit visit vi
37. mple factoris a Factor object whereas e g asia Bronchitis is a CPT object a special case a subclass of a Factor object This is why they are printed out differently gt gt gt type example factor class gPy Parameters Factor gt gt gt gt type asia Bronchitis class gPy Parameters CPT Factors are functions mapping combinations of values of discrete variables to numbers These numbers don t have to probabilities gt gt gt example factor 5 gt gt gt print example factor Bronchitis Smoking absent nonsmoker 1 75 absent smoker 1 00 present nonsmoker 0 75 present smoker 1 50 Note that we just altered the object example factor by multiplying it by 5 Many gPy operations alter objects so it is sometimes useful to make copies gt gt gt my copy example factor copyO Simple numbers are allowed to be treated as factors Also we can iterate over all the CPTs in a BN These two facts make it easy to make a factor which is an explicit representation of the full joint distribution defined by asia If you re not familiar with using the Python interpreter be careful On the first line with put a space so that the j of joint lines up with the o of for This provides white space indentation which Python uses to make program blocks Just hit return when you get the second prompt gt gt gt joint 1 gt gt gt for cpt in asia joint cpt gt gt gt print
38. n sup pressed has a row of zeroes This is to flag that some cheating has been going on Naturally using this row as if actually defined a proper distribution over Tuberculosis will cause an error at a later stage 6 5 2 Extracting data from CPTs CPTs can be indexed exactly like any other factor is desired But they also come with an extra indexing possibilities If the index corresponds to a row of the CPT then a single variable CPT for the child is returned rather than just data gt gt gt cpt asial Dyspnea gt gt gt print cpt Bronchitis TbOrCa Dyspnea absent present absent false 0 90 0 10 absent true 0 30 0 70 present false 0 20 0 80 present true 0 10 0 90 gt gt gt print cpt absent true Dyspnea absent present 39 gt gt gt print cpt Bronchitis absent TbOrCa true Dyspnea absent present To get at individual numbers is a CPT there are two options either index exactly as for arbitrary factors gt gt gt print cpt f Bronchitis absent Tb rCa true Dyspnea present 0 7 or use two indices gt gt gt print cpt Bronchitis absent TbOrCa true present 0 7 Note how in the last example it was necessary to use present as an index rather than present This is because the index will be interpreted as a sequence Iterating over CPTs gPy provides a number of m
39. normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal OOOo0o0o0o00000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 present present present nonsmoker true absent visit abnormal 0 00 present present present nonsmoker true absent
40. normal normal abnormal normal abnormal OOOo0o0o0o000000000000000000000000000000000000000000000000000000000000000000000000000000000000500 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00 00 00 00 00 00 00 present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present absent absent absent absent absent absent absent absent absent abs
41. pergraph from an arbitrary hypergraph by simply deleting redundant hyperedges Graphical and decomposable hypergraphs can be con structed from arbitrary hypergraphs by merging hyperedges To do this set the optional modify constructor argument to True default is False Firstly con sider doing this for ReducedHypergraph objects since in this case there is one obvious choice for modification deleting redundant hyperedges gt gt gt hg Hypergraph BC AB A CD gt gt gt print hg B C TA B A C D gt gt gt rhg ReducedHypergraph hg copy modify True gt gt gt print rhg I B C TA B C D At time of writing gPy provides no way of constructing GraphicalHypergraph and ReducedGraphicalHypergraph objects from hypergraphs which fail to meet the necessary conditions However it is possible to construct a DecomposableHypergraph from a non decomposable hypergraph 16 Constructing decomposable hypergraphs To construct a decomposable hypergraph a DecomposableHypergraph object from an existing hypergraph there are a number of options If the existing hypergraph is decomposable then calls such as gt gt gt dg DecomposableHypergraph Hypergraph ABC BCD gt gt gt dg DecomposableHypergraph ABC BCD gt gt gt dg DecomposableHypergraph ABC BCD check False suffice as previously explained If it is not safe to ass
42. pergraphs py line 2049 in init JoinForest hypergraph modify trace elimination order File home jc godot research gPy gPy Hypergraphs py line 2159 in __init__ hypergraph uforest hypergraph join forest File home jc godot research gPy gPy Hypergraphs py line 814 in join forest raise DecomposabilityError s is not decomposable self gPy Hypergraphs DecomposabilityError A B C B C A D C D is not decomposable The constructor for the general hypergraph class Hypergraph can also be used in this way being sent a hypergraph as input rather than a collection of edges gt gt gt dg DecomposableHypergraph Hypergraph ABC BCD gt gt gt type dg class gPy Hypergraphs DecomposableHypergraph gt gt gt new hg Hypergraph dg gt gt gt type new hg class gPy Hypergraphs Hypergraph gt 14 This approach would typically be used if we want to forget that a particular hypergraph has certain properties For example suppose we wanted to add a redundant hyperedge to a hypergraph of class ReducedHypergraph thus render ing it no longer reduced Doing so would cause an exception so it is necessary to construct a Hypergraph object first and then add the redundant hyperedge gt gt gt rg ReducedHypergraph Hypergraph AB BC CA gt gt gt rg add_hyperedge A Traceback most recent call last File lt stdin gt line 1 in File home
43. present false 0 10 absent present true 0 70 present absent false 0 20 present absent true 0 10 present present false 0 80 32 present present true 0 90 gt gt gt for f in factor print f Dyspnea TbOrCa absent false 0 90 absent true 0 30 present false 0 10 present true 0 70 Dyspnea TbOrCa NODE esc ESEE absent false 0 20 absent true 0 10 present false 0 80 present true 0 90 This form of iteration takes advantage of the internal representation of a factor s data and is thus efficient However it is only rarely useful A more useful iterator is provided by the insts method which iterates over the joint instantiations of a factor gt gt gt print factor Bronchitis Dyspnea TbOrCa Tuscos gt absent absent false 0 90 absent absent true 0 30 absent present false 0 10 absent present true 0 70 present absent false 0 20 present absent true 0 10 present present false 0 80 present present true 0 90 gt gt gt for inst in factor insts print inst absent absent false Cabsent absent true C absent present false C absent present true 33 C present absent false present absent true C present present false present present true The values within a joint ins
44. present present present absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker smoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nonsmoker nons
45. r false absent visit normal 0 00 absent absent present nonsmoker false present no_visit abnormal 0 00 absent absent present nonsmoker false present no visit normal 0 00 absent absent present nonsmoker false present visit abnormal 0 00 absent absent present nonsmoker false present visit normal 0 00 absent absent present nonsmoker true absent no_visit abnormal 0 00 absent absent present nonsmoker true absent no visit normal 0 00 absent absent present nonsmoker true absent visit abnormal 0 00 absent absent present nonsmoker true absent visit normal 0 00 absent absent present nonsmoker true present no visit abnormal 0 00 absent absent present nonsmoker true present no visit normal 0 00 absent absent present nonsmoker true present visit abnormal 0 00 absent absent present nonsmoker true present visit normal 0 00 absent absent present smoker false absent no_visit abnormal 0 00 absent absent present smoker false absent no visit normal 0 02 absent absent present smoker false absent visit abnormal 0 00 absent absent present smoker false absent visit normal 0 00 absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent ab
46. resent absent true 0 10 present present false 0 80 present present true 0 90 gt gt gt factor absent present true 1 2 gt gt gt print factor Bronchitis Dyspnea TbOrCa absent absent false 0 70 absent absent true 0 30 absent present false 0 10 absent present true 1 20 present absent false 0 20 present absent true 0 10 present present false 0 80 present present true 0 90 gt gt gt factor 5 1 2 3 gt gt gt print factor Bronchitis Dyspnea TbOrCa purum najan nan na esej ee absent absent false 0 70 absent absent true 0 30 absent present false 0 10 absent present true 1 20 present absent false 0 20 present absent true 1 00 present present false 2 00 present present true 3 00 6 4 Iterating over factors Sometimes it is useful to iterate over factors There are various ways of doing this A for loop starting for f in factor will iterate over the factors corre sponding to slicing on the various values of the lexicographically first variable mentioned in factor A new gPy Parameters Factor object is yielded on each iteration gt gt gt from gPy Examples import asia gt gt gt factor asia Dyspnea 1 gt gt gt print factor Bronchitis Dyspnea TbOrCa cc aja cw cen mp ee absent absent false 0 90 absent absent true 0 30 absent
47. resent present absent absent present present TbOrCa Ms amm false 0 40 true 0 40 false 0 40 true 0 40 false 0 40 true 0 40 false 0 40 true 0 40 A much more efficient way of modifying data values while iterating is to take ad vantage of the fact that instantiations and data values follow the same ordering so the ith instantiation corresponds to the ith value Together with Python s builtin enumerate function this provides a quick and easy way of updating data values gt gt gt factor Bronchitis absent absent absent asia Dyspnea 1 gt gt gt print factor Dyspnea absent absent present TbOrCa E jobs false 0 90 true 0 30 false 0 10 35 absent present true 0 70 present absent false 0 20 present absent true 0 10 present present false 0 80 present present true 0 90 gt gt gt for i inst in enumerate factor insts if inst 1 absent factor i 0 4 gt gt gt print factor Bronchitis Dyspnea TbOrCa purum najan nan sn a aaa em absent absent false 0 40 absent absent true 0 40 absent present false 0 10 absent present true 0 70 present absent false 0 40 present absent true 0 40 present present false 0 80 present present true 0 90 6 5 Conditional probability tables Conditional probability tables from now on abbreviated to CPTs are a spe cial type o
48. riable to have a particular value removes from the domain all other values associated with that variable It follows that if the copy is to be conditioned or its domain altered in any other way then the copy needs to be a deep copy A deep copy is returned by model copy copy domain True Altering the domain of a copy constructd in this way by conditioning for example leaves the original unchanged in any way 7 3 Conditioning models To alter a model by conditioning on some observation use the condition method This method takes one argument which is a dictionary mapping variables to their observed values Here s an example of conditioning the asia Bayesian network gt gt gt asiac asia copy True gt gt gt print asiac Smoking Bronchitis absent present nonsmoker 0 70 0 30 44 smoker 0 40 0 60 Smoking Cancer absent present nonsmoker 0 99 0 01 smoker 0 90 0 10 Bronchitis TbOrCa Dyspnea absent present 0 0 10 0 0 70 0 0 80 0 0 90 TbOrCa lse true 00 0 00 00 1 00 00 1 00 00 1 00 absent false 0 9 absent true 0 3 present false 0 2 present true 0 1 Smoking nonsmoker smoker 0 50 0 50 Cancer Tuberculosis fa cc md 1 NA absent absent m absent present 0 present absent 0 present present 0 VisitAsia Tuberculosis absent present EEEN jaia no_visit 0 99 0 01 visit 0 95 0 05 VisitAsia no_visit visit 45 TbOrCa XRay
49. s This is generally not a convenient way of getting the right values in a factor It is usually much easier to construct a factor with dummy data values and then set individuals data values using the methods described in Section 6 3 Note also that when constructing factors g it was not necessary to declare values for its variables using the new_domain_variables This is because as suming we re in the same environment in which factor f was constructed these variables are already known This is because by default when variables are declared using new domain variables the mapping from variables to their val ues is stored in an internal default domain Also by default factors and indeed any object which has variables looks for the values of its variables in this internal default domain This is how come g picks up the right values for its variables Functions for displaying clearing and setting the internal default domain are available as functions in the gPy Variables module gt gt gt from gPy import Variables 29 gt gt gt Variables print default domain A frozenset y n C frozenset 1 2 31 B frozenset x o gt gt gt Variables clear_default_domain gt gt gt Variables print default domain gt gt gt Variables set_default_domain A yn B ox C 1 2 3 gt gt gt Variables print default domain A frozenset y
50. sent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent present present present present present present present present present present present present present present present absent absent absent absent absent absent absent absent absent absent absent absent present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present present absent absent absent absent absent absent absent absent absent absent absent absent absent absent absent present present present present present present present present present
51. sit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit visit no visit no visit visit abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal abnormal normal ab
52. smoker false 0 absent smoker true 0 present nonsmoker false 0 present nonsmoker true 0 present smoker false 0 present smoker true 0 Cancer TbOrCa Tuberculosis 5 2 i absent false absent absent false present absent true absent absent true present present false absent present false present present true absent present true present TbOrCa XRay em Ra Rz false abnormal 0 05 false normal 0 95 true abnormal 0 98 true normal 0 02 Tuberculosis VisitAsia RONDE RBS ro en uec absent no visit 0 98 absent visit 0 01 present no visit 0 01 present visit 0 00 Separators Bronchitis TbOrCa nico ml poene absent false 1 00 absent true 1 00 present false 1 00 present true 1 00 49 49 49 45 45 01 01 05 05 00 00 00 00 00 00 00 00 Cancer TbOrCa absent false 1 00 absent true 1 00 present false 1 00 present true 1 00 Smoking TbOrCa e e a tuns SP nonsmoker false 1 00 nonsmoker true 1 00 smoker false 1 00 smoker true 1 00 TbOrCa CN ea false 1 00 true 1 00 Tuberculosis eee eel ee eee pue absent 1 00 present 1 00 Join Forest Bronchitis Smoking TbOrCa tTbOrCa XRay Cancer Smoking TbOrCa Tuberculosis VisitAsia Bronchitis Dyspnea TbOrCa Cancer
53. t is a gPy Graphs UForest object Here s two examples of constructing JoinForest objects gt gt gt hg2 Hypergraph AB BC CD DA AE gt gt gt jf2a JoinForest hg2 copy O modify True elimination_order ABCDE gt gt gt print jf2a B C D E A B D E Vertices B C D E A B D E Lines A B D E B C D E gt gt gt jf2b JoinForest hg2 copy modify True 24 Hypergraph SimpleHypergraph GraphicalHypergraph ReducedHypergraph DecomposableHypergraph ReducedGraphicalHypergraph ReducedDecomposableHypergraph JoinForest ReducedJoinForest Figure 4 1 Hypergraph hierarchy including join forests 25 gt gt gt print jf2b IB C D A B D A E Vertices B C D A B D TA EH Lines A B D B C D A E A B D 26 Chapter 5 Graphs Chapter 6 Factors 6 1 Constructing factors Factors are the building blocks of graphical models In Chapter 2 there were some examples of extracting factors from the Asia Bayesian network This BN is available ready made from the gPy Examples module and so getting hold of its factors is easy In contrast in this section the process of constructing factors from scratch is described Here is an example of the basic method for making factors Python 2 4 3 1 Jul 26 2006 20 13 39 GCC 3 4
54. tantiation are ordered according to the lexico graphical ordering of the variables This makes it easy to generate a sequence of named instantiations using Python s builtin sorted and zip functions gt gt gt print factor Bronchitis Dyspnea TbOrCa absent absent false 0 90 absent absent true 0 30 absent present false 0 10 absent present true 0 70 present absent false 0 20 present absent true 0 10 present present false 0 80 present present true 0 90 gt gt gt sorted variables sorted factor variables gt gt gt for inst in factor insts print zip sorted variables inst Bronchitis absent Dyspnea absent CTbOrCa false Bronchitis absent Dyspnea absent TbOrCa true Bronchitis absent Dyspnea present TbOrCa false Bronchitis absent Dyspnea present TbOrCa true Bronchitis present Dyspnea absent TbOrCa false Bronchitis present Dyspnea absent TbOrCa true Bronchitis present Dyspnea present TbOrCa false Bronchitis present Dyspnea present TbOrCa true Alternatively dictionaries can be used to represent named instantiations These can then be
55. ter session it is just assigned to a dummy variable foo This is cor rect since one of its factors the one including Dyspnea is no longer a CPT By default gPy plays safe and makes all conditioned models members of the general SFR class If you want to force the class of an object to stay the same even after conditioning set the keep class flag model condition some dict keep class True In general it is possible to condition by specifying a set of values which are still possible values for a variable implicitly stating that other values are ruled out For this reason the values in the dictionary specifying the condition must be a iterable of values e g a set list or tuple It is easy to get tripped up by this by specifying a string as a dictionary value gt gt gt asia copy True condition Dyspnea absent Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Models py line 179 in condition raise ValueError ValueError Dyspnea has values absent present a is not one of them 47 7 4 Join forest representations 7 4 1 Creating join forest representations To run the probability propagation algorithm calibration on join forests a join forest is required If your original is not a join forest model for example it may be a Bayesian network then you need to construct a join forest from it Typically the hypergraph associated
56. tfalls in creating hypergraphs 18 3 2 Getting information about hypergraphs 19 3 2 1 Basic information LL ee 19 3 2 2 Paths in hypergraphs s 19 3 2 3 Iterators for hypergraphs as 20 3 3 Printing and displaying hypergraphs 20 3 4 Relations between hypergraphs is 20 3 5 Altering hypergraphs es e LL 20 3 5 1 Altering hypergraphs with specific properties 21 3 6 Generating objects from hypergraphs 1 1 1 1 1 1 21 3 6 1 Generating hypergraphs from hypergraphs aaa 21 3 6 2 Generating graphs from hypergraphs 21 3 7 Internal representation of hypergraphs 22 Join Forests 24 Graphs 27 Factors 28 6 1 Constructing factors eee 28 6 2 Copying factors soo vv RA A B DESI 30 6 3 Accessing and modifying data in factors 30 6 4 Iterating over factors ks 32 6 5 Conditional probability tables 4244401 36 6 5 1 Constructing CPTS LL 1 36 6 5 2 Extracting data from CPTs 222000 39 Models 42 7 1 Altering models as 42 7 2 Copyingmodels 22 43 7 3 Conditioning models es 44 7 4 Join forest representations as 48 7 41 Creating join forest representations 48 7 4 2 Calibrating join forest models sss 51 7 4 3 Extracting single variable marginals from join forest models 54 Samplers 55 8 1 Sampling from unstruct
57. ume that the input hypergraph is decomposable then it can be modified to become decomposable by having some of its hyperedges merged Each elimination ordering of the vertices of an arbitrary hypergraph determines a decomposable hypergraph so one option for making decomposable hypergraphs is to supply such an ordering gt gt gt hg Hypergraph A AB BC CD AD gt gt gt dg DecomposableHypergraph hg modify True elimination_order ABCD gt gt gt print dg B C D 1A B D gt gt gt print hg XB C D TA B D gt gt gt print type dg class gPy Hypergraphs DecomposableHypergraph gt gt gt gt print type hg lt class gPy Hypergraphs Hypergraph gt Note that hg and dg are now identical except for their class Had we wished to keep the original hg intact we would have sent hg copy to the DecomposableHypergraph constructor instead Some elimination orders are better than others in the sense that they do not create big hyperedges in the constructed decomposable hypergraph gt gt gt hg Hypergraph AB AC AD AE gt gt gt print hg C TA E TA D A C A B gt gt gt dg bad DecomposableHypergraph hg copy O modify True elimination order ABCDE gt gt gt dg good DecomposableHypergraph hg copy modify True elimination_order BCDEA gt gt gt print dg bad A B C D E gt gt gt print dg
58. ured discrete distributions 55 Chapter 1 How to read this manual This manual contains many examples of using gPy in a Python interactive ses sion To do these examples yourself just start the Python interpreter and type in the stuff displayed in this font which comes after the prompt gt gt gt Stuff in normal font is my commentary on what you are doing so does not provide it as input to the Python interpreter Many examples produce output also in this font and the Python module doctest has been used to check that the output you see here is what is actually produced There is one small exception Sometimes the output does not fit on one line of text so line breaks have been added to maintain readability Note that no attempt has been made to fit each Python examples on a single page Chapter 2 Quick start Assuming that you have installed gPy and Python so that your Python installation can find the gPy modules you can do the following to get a quick idea of how to use gPy Firstly grab the Asia Bayesian network from the gPy Examples modules This network has the name asia in that module so you do gt gt gt from gPy Examples import asia Check that the name asia really does refer to an object in the class BN gt gt gt type asia lt class gPy Models BN gt The obvious thing to do now is to have a look at the BN gt gt gt print asia Smoking Bronchitis absent present nonsmoker 0 70
59. visit normal 0 00 present present present nonsmoker true present no_visit abnormal 0 00 present present present nonsmoker true present no visit normal 0 00 present present present nonsmoker true present visit abnormal 0 00 present present present nonsmoker true present visit normal 0 00 present present present smoker false absent no_visit abnormal 0 00 present present present smoker false absent no visit normal 0 00 present present present smoker false absent visit abnormal 0 00 present present present smoker false absent visit normal 0 00 present present present smoker false present no visit abnormal 0 00 present present present smoker false present no visit normal 0 00 present present present smoker false present visit abnormal 0 00 present present present smoker false present visit normal 0 00 present present present smoker true absent no_visit abnormal 0 03 present present present smoker true absent no visit normal 0 00 present present present smoker true absent visit abnormal 0 00 present present present smoker true absent visit normal 0 00 present present present smoker true present no_visit abnormal 0 00 present present present smoker true present no visit normal 0
60. with your model will not be decomposable so this hypergraph will need to be altered before a join forest can be built for it Recall that a join forest has the hyperedges of a decomposable hypergraph for its vertices In the case of Bayesian networks unless all variables are mutu ally independent hardly typical the associated hypergraph will not even be reduced let alone decomposable Join forest models are object of class gPy Models JFR Here is an example of JFR creation the output has been altered to break long lines gt gt gt from gPy Examples import asia gt gt gt from gPy Models import JFR gt gt gt asia jfm JFR asia copy True modify True gt gt gt print asia jfm Cliques Bronchitis Dyspnea TbOrCa purum najan nan nn a raab em absent absent false 0 90 absent absent true 0 30 absent present false 0 10 absent present true 0 70 present absent false 0 20 present absent true 0 10 present present false 0 80 present present true 0 90 Bronchitis Smoking TbOrCa RE DES ia pa a absent nonsmoker false 0 70 absent nonsmoker true 0 70 absent smoker false 0 40 absent smoker true 0 40 present nonsmoker false 0 30 present nonsmoker true 0 30 present smoker false 0 60 present smoker true 0 60 Cancer Smoking TbOrCa 48 absent nonsmoker false 0 absent nonsmoker true 0 absent
61. ypergraph A A B C gt gt gt print hgl B C TA D A B 0 D gt gt gt print hg2 CXA B C i 4 14 A 4 B 10 A CA B A B B C 4 C A B B C C D tA DH UA B B C Table 3 1 Example hypergraphs 11 H Simple Reduced Graphical Decomposable RAL A A B N N Y Y 40 A A BH Y N Y A A B B C A Ch Y Y N N A B B C C D A DH Y Y Y N A B B C Y Y Y Y Table 3 2 Classification of example hypergraphs Each vertex should be some immutable object in most real applications it will be a string the name of some random variable Each hyperedge is an iterable of vertices this will normally just be a sequence such as a list or tuple but sets can be used as well Note that in Python a string is seen as a sequence of characters This was used in the construction of hg1 above Empty hypergraphs are permissible in fact the empty tuple is the default collection of hyperedges gt gt gt e Hypergraph gt gt gt print e Empty hypergraphs are useful as initial hypergraphs to which hyperedges can be added later 3 1 1 Constructing hypergraphs with specific properties gPy provides the classes SimpleHypergraph ReducedHypergraph GraphicalHypergraph ReducedGraphicalHypergraph DecomposableHypergraph and ReducedDecomposableHypergraph for hypergraphs with specific properties
Download Pdf Manuals
Related Search
Related Contents
Martha Stewart Living 1904200280 Instructions / Assembly Table des matières Ecostat 1001 SL 13256000 Ecostat 1001 SL 13254000 C:\BCI2000 User Docs\documentation\user manual Lab 7.2.6 Troubleshooting RIP – Instructor Version 2500 Carson Car Amplifier 1012 User's Manual Technicolor - Thomson DVD Player + TV Set + VCR User's Manual Copyright © All rights reserved.
Failed to retrieve file