Home
gPy User Manual - University of York
Contents
1. true Dyspnea absent present 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 Bronchitis absent TbOrCa 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 27 Iterating over CPTs gPy provides a number of methods for iterating over the rows of CPTs For example parent insts allows vou 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 asial 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 asial Dvspnea l parent insts print row C absent false C absent true 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 asial Dvspnea l parent insts
2. 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 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 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
3. 7 Sen 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 Dvspnea 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 24 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 creating CPTs After a CPT object has been created the factor used to create it has 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 create a CPT gt gt gt print f Bronchitis Dyspnea TbOrCa SSS ces ao nini ete ae 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 f cop
4. A B D set frozenset A D J frozenset C D 2 3 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 10 gt gt gt print hgi I B C TA D TA B C D J gt gt gt hgi add hvperedge ABC gt gt gt print hgi TA B Ch B C TA D TA B IC D gt gt gt hgi remove_hyperedge A B gt gt gt print hgi I A B C TB C TA D TC D Note the flexibility with which hyperedges are specified Any seguence will do in fact any iterable 2 4 Copying hypergraphs As well as constructing hypergraphs directly from collections of hyperedges 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 hg1 HyperGraph AB BC CD DA gt gt gt hgi_cp hgl copy gt gt gt hg1 add_hyperedge EF gt gt gt hgi_cp remove_hyperedge AB gt gt gt print hgi IE F TB C A D TA B IC D gt gt gt print hgi cp IC D TB C SA D 2 5 Making graphs from hypergraphs Each hypergraph H has an associated undirected graph G H G H has the same vertices as H and two vertices are connected in G H if and only
5. Present Present Smoker False Present No Visit Normal I 0 00 Present Present Present Smoker False Present Visit Abnormal 0 00 Present Present Present Smoker False Present Visit Normal I 0 00 Present Present Present Smoker True Absent No Visit Abnormal 0 03 Present Present Present Smoker True Absent No Visit Normal I 0 00 Present Present Present Smoker True Absent Visit Abnormal 0 00 Present Present Present Smoker True Absent Visit Normal I 0 00 Present Present Present Smoker True Present No Visit Abnormal 0 00 Present Present Present Smoker True Present No Visit Normal I 0 00 Present Present Present Smoker True Present Visit Abnormal 0 00 Present Present Present Smoker True Present Visit Normal I 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 Chapter 2 Hypergraphs In gPy hupergraphs rather than graphs are the central structure although the latter still play an important role A hypergraph H is simply a collec
6. gt gt gt asia jfm JFM asia copy modify True gt gt gt asia jfm calibrate gt gt gt print asia jfm Cligues Bronchitis Dyspnea TbOrCa I gt gt gt gt gt gt gt 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 38 present present true Bronchitis Smoking TbOrCa So Ls eaaa j absent nonsmoker false absent nonsmoker true absent smoker false absent smoker true present nonsmoker false present nonsmoker true present smoker false present smoker true Cancer Smoking TbOrCa SESS Ses an absent nonsmoker false absent nonsmoker true absent smoker false absent smoker true present nonsmoker false present nonsmoker true present smoker false present smoker true Cancer TbOrCa Tuberculosis La LNR U absent false absent absent false present absent true absent absent true present present false absent present false present present true l absent present true present TbOrCa XRay Bessa eee eee false abnormal 0 05 false normal 0 89 true abnormal 0 06 true normal 0 00 OO D fo 39 0 03 49 01 45 00 00 00 00 05 34 01 18 02 15 00 27
7. JOM Forests voi ee ae eA A ed a a oe aaa acy 19 3 Graphs 22 4 Factors 23 4 1 Extracting data from factors a 23 4 2 Conditional probability tables 2 0 0 0 2 0 0 24 4 2 1 Constructing CPIS roeas py Daa e a a ih 24 4 2 2 Extracting data from CPTs LL 000000 27 5 Models 29 5 1 Altering models L 29 5 22 Copying models via s eeii p 8 TA S Yra A ee a 30 5 3 Conditioning models lt 0 02200 00 31 5 4 Join forest models ks 34 5 4 1 Creating join forest models 0 34 5 4 2 Calibrating join forest models 0 38 5 4 3 Extracting single variable marginals from join forest models 41 6 Samplers 42 6 1 Sampling from unstructured discrete distributions 42 Chapter 1 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 guick idea of how to use gPy Just start the Python interpreter and type in the stuff which comes after the prompt gt gt gt below Stuff in normal font is my commentary on what you are doing so do not provide it as input to the Python interpreter Firstly grab the Asia Bayesian network from the gPv 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 BNM gt gt gt tvpe asia lt class gPy Models BNM gt The obvious thing
8. gt ghg GraphicalHyperGraph hg1 Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Structures py line 996 in __init__ raise GraphicalityError 4 s is not graphical hypergraph gPy Structures GraphicalityError TA B C B C TA D C D J 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 Structures py line 1158 in __init_ raise DecomposabilityError 4s is not decomposable hypergraph gPy Structures DecomposabilityError A B C TB C A D C D J is not decomp 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 1 gt gt gt type dg lt class gPy Structures DecomposableHyperGraph gt gt gt gt new hg HyperGraph dg gt gt gt type new_hg lt class gPy Structures HyperGraph gt 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 create a HyperGraph object first and then add
9. gt gt gt id hg _hyperedges 1215346996 gt gt gt print rg I B C TA C SA B A gt gt gt type rg lt class gPy Structures 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 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 imme diately garbage collected This approach was taken in many of the examples given above Alternatively a named input hypergraph can 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 create a new hypergraph from an existing one and wish the two to have inde pendent existences just use a copy which will be a deep copy of the existing hypergraph to create the new one gt gt gt hg HyperGraph AB BC CD gt gt gt rhg ReducedHyperGraph hg copy gt gt gt hg add_hyperedge A gt gt gt print hg I IB C TA B A IC D gt gt gt print rhg B C A B C D 2 7 2 Creating hypergraphs without checks In some cases you may
10. hyperedge KeyError frozenset a C e c n r For all model classes model copy 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 variable 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 created in this way by conditioning for example leaves the original unchanged in any way 5 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 print asia Smoking Bronchitis absent present nonsmoker 0 70 0 30 smoker 0 40 0 60 Smoking Cancer absent present 31 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
11. 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 joint Bronchitis Cancer Dyspnea Smoking TbOrCa Tuberculosis VisitAsia XRay 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 N
12. 03 94 00 00 01 00 00 05 00 Tuberculosis KN hare absent absent present present Separators Bronchitis Tb ESA EAS absent fa absent tr present fa present tr Cancer TbOrc Aldea Daves ee absent false absent true present false present true Smoking TbO Aer eee nonsmoker fal nonsmoker tru smoker fal smoker tru TbOrCa eee pasa false 0 94 true 0 06 Tuberculosis NAK a nah absent present Join Forest VisitAsia no_visit visit no_visit visit OrCa a ae eer lse 0 52 ue 0 03 lse 0 41 ue 0 04 a ee 0 94 0 01 0 00 0 05 rCa an RA se 0 49 e 0 01 se 0 45 e 0 05 0 99 0 01 0 98 0 01 0 01 0 00 40 Bronchitis Smoking TbOrCa TbOrCa XRay Cancer Smoking TbOrCa Tuberculosi Vertices Bronchitis Smoking TbOrCa TbOrCa XRay Cancer Smoking TbOrCa Tuberculosis Lines Cancer Smoking TbOrCa Bronchitis Smoking TbOrCa TbOrCa XRay Bronchitis Smoking TbOrCa Bronchitis Dyspnea TbOrCa Bronchitis Smoking TbOrCa Cancer TbOrCa Tuberculosis Cancer Smoking Tb rCa Cancer TbOrCa Tuberculosis Tuberculosis VisitAsia 5 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
13. 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 SEO jj A absent l 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 e AKE LiL no visit 0 99 0 01 visit 0 95 0 05 VisitAsia no_visit visit TbOrCa XRay abnormal normal LSS ji false l 0 05 0 95 true 0 98 0 02 32 gt gt gt asia condition Dyspnea absent gt gt gt print asia Bronchitis Dyspnea TbOrCa Sn A E absent absent false 0 90 absent absent true 0 30 present absent false 0 20 present absent true 0 10 Bronchitis Smoking So ener es absent nonsmoker 0 70 absent smoker 0 40 present nonsmoker 0 30 present smoker 0 60 Cancer Smoking festa Aa gt absent nonsmoker 0 99 absent smoker 0 90 present nonsmoker 0 01 present smoker 0 10 Cancer TbOrCa Tuberculosis as Sesa a Taman 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 Smoking i eee gt nonsmoker 0 50 smoker 0 50 33 TbOrCa XRay false abnormal 0 05 false normal 0 9
14. 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 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 Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Present Present NonSmoker NonSmoker NonSmoker NonSmoker NonSmoker NonSmoker NonSmoker NonSmoker NonSmoker Smoker Smoker Smoker Smoker Smoker Smoker Smoker Sm
15. 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 41 Chapter 6 Samplers 6 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 at time of writing gt gt gt for i in xrange 30 print bias coin sample hthhhtthhthhthhhththhhthhhttth 42 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 43
16. NonSmoker False Absent No Visit Normal 0 03 Absent Absent Present NonSmoker False Absent Visit Abnormal 0 00 Absent Absent Present NonSmoker 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 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 Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Absent Abse
17. join forest is required If your original is not a join forest model for example 34 it may be a Bayesian network then you need to create a join forest from it Typically the hypergraph associated 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 gPv Models JFM Here is an example of JFM 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 JFM gt gt gt asia jfm JFM asia modify True gt gt gt print asia jfm Cliques Bronchitis Dyspnea TbOrCa Spa aja sak icena Ia B 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 a ari rag a sees absent nonsmoker false 0 35 absent nonsmoker true 0 35 absent smoker false 0 20 absent smoker true 0 20 present nonsmoker false 0 15 present nonsmoker t
18. know that a particular hypergraph has the properties required for a hypergraph class below it in the hierarchy Fig 2 2 and wish 16 to avoid the cost of pointlessly checking that it meets the necessary conditions In this case an object of the relevant class can be created 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 reguired otherwise you can for example create a ReducedHyperGraph object that is not reduced gt gt gt print hg B C TA B 4A C D gt gt gt r ReducedHyperGraph hg copy Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Structures py line 678 in __init_ if hypergraph is None gPy Structures RedundancyError B C A B A C D is not reduced gt gt gt r ReducedHyperGraph hg copy check False gt gt gt print r B C A B A C D 2 7 3 Creating hypergraphs by modifying existing hyper graphs It is often useful to create a hypergraph meeting certain conditions from a hy pergraph which does not meet these conditions For example we can create a reduced hypergraph from an arbitrary hypergraph by simply deleting redundant hyperedges Graphical and decomposable hypergraphs can be created from ar bitrary hypergraphs by merging hyperedges To do this set the optional modify constructor argument t
19. 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 18 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 modify True gt gt gt print dg TA E TA D TA C SA 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 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 IB C D E A B D E gt gt gt print dg2b f IB C D TA B D TA E 2 8 Join Forests To each decomposable hyperg
20. the classes HyperGraph ReducedHyperGraph GraphicalHyperGraph GraphicalReducedHyperGraph and DecomposableHyperGraph for the various types of hypergraph Recall Fig 2 2 which gives the hierarchical relationships between these classes In Section 2 1 the method of constructing HyperGraph objects was described 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 have identical attributes to the inputted object Here s some examples of successful attempts to create various objects gt gt gt from gPv Structures import gt gt gt dg DecomposableHyperGraph HyperGraph ABC BCD gt gt gt gg GraphicalHyperGraph HyperGraph AB BC CD AD gt gt gt rg ReducedHyperGraph HyperGraph AB BC CA and some failed attempts gt gt gt print hgi 1 TA B C TB C TA D IC D gt gt gt rhg ReducedHyperGraph hg1 Traceback most recent call last File lt stdin gt line 1 in 14 File home jc godot research gPy gPy Structures py line 668 in __init_ raise RedundancyError s is not reduced hypergraph gPy Structures RedundancyError A B C B C TA D TC D is not reduced gt gt
21. to do now is to have a look at the BN gt gt gt print asia Smoking Bronchitis absent present nonsmoker 0 70 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 e A aj ama ne 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 seo saps ee no_visit 0 99 0 01 visit 0 95 0 05 VisitAsia no_visit visit TbOrCa XRay abnormal normal Denes eee er eo 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 Note from this output that each CPT in the BN has a name We can use these names to get hold of particular CPTs gt gt gt print asial 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 multiplv two CPTs we just use the x operator gt gt gt example factor asial Bronchitis l asial Smoking l gt gt gt print example facto
22. 0 Join Forest Bronchitis Smoking TbOrCa TbOrCa XRav Cancer Smoking TbOrCa Tuberculosis VisitAsia Bronchitis Dyspnea TbOrCa Cancer TbOrCa Tuberculosis Vertices Bronchitis Smoking TbOrCa TbOrCa XRay Cancer Smoking TbOrCa 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 TbOrCa Tuberculosis Cancer Smoking Tb rCa Cancer TbOrCa Tuberculosis Tuberculosis VisitAsia If the modify flag is not set and the input model is not decomposable then various exceptions will be raised Here are two examples of this behaviour gt gt gt from gPy Models import RM JFM gt gt gt from gPy Examples import asia gt gt gt asia_jfm_ex JFM asia Traceback most recent call last File lt stdin gt line 1 in 37 File home jc godot research gPy gPy Models py line 630 in __init__ join forest JoinForest hm hvpergraph modifv True elimination order File home jc godot research gPy gPy Structures py line 1321 in __init_ hvpergraph uforest hvpergraph join forest File home jc godot research gPy gPy Structures py line 409 in join forest alpha beta gamma inv gamma r self maximum_cardinality_search File home jc godot research gPy g
23. 0 present false absent 0 00 present false present 0 00 present true l absent 1 00 present true present 1 00 gt gt gt cpt CPT f copy Tuberculosis cpt force True Traceback most recent call last File lt stdin gt line 1 in 7 File home jc godot research gPy gPy Parameters py line 911 in __init__ self datalindx prob sum ZeroDivisionError float division gt gt gt cpt CPT f copy 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 been sup pressed has a row of zeroes This is to flag that some cheating has been going 26 on Naturally using this row as if actually defined a proper distribution over Tuberculosis will cause an error at a later stage 4 2 2 Extracting data from CPTs CPTs can be indexed exactiv like anv other factor is desired But thev 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 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 cptl absent
24. 5 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 ens Ones no visit 0 99 visit 0 01 Note that the conditioned asia is no longer a Bayesian network This is correct since one of its factors the one including Dyspnea is no longer a CPT By de fault gPy plays safe and makes all conditioned models members of the general HM class If you want to force the class of an object to stay the same even after condi tioning 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 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 5 4 Join forest models 5 4 1 Creating join forest models To run the probability propagation algorithm calibration on join forests a
25. 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 A B D gt gt gt print hg I IB C D A B D gt gt gt print tvpe dg lt class gPy Structures DecomposableHyperGraph gt gt gt gt print type hg lt class gPy Structures 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 created decomposable hypergraph gt gt gt hg HyperGraph AB AC AD AE gt gt gt print hg I A E TA D SA C SA B gt gt gt dg bad DecomposableHyperGraph hg copy modify True elimination order ABCDE gt gt gt dg good DecomposableHyperGraph hg copy modifv True elimination order BCDEA gt gt gt print dg bad A B C D E gt gt gt print dg_good I A E TA D SA C A B gt gt gt dg lessbad DecomposableHyperGraph hg copy modifv True elimination order BCADE gt gt gt print dg_lessbad f TA C TA B A D E hg here is in fact already decomposable but by choosing to eliminate A first we create dg_bad in which all hyperedges of hg have been merged into
26. Py Structures py line 679 in maximum cardinalitv raise RedundancyError s is not reduced self gPy Structures RedundancyError TbOrCa XRay VisitAsia Tuberculosis VisitAsia Bronchitis Smoking Smoking Bronchitis Dyspnea TbOrCa Cancer Smoking Cancer TbOrCa Tuberculosis is not reduced gt gt gt asia_jfm_ex JFM RM asia copy modify True Traceback most recent call last File lt stdin gt line 1 in File home jc godot research gPy gPy Models py line 630 in __init__ join forest JoinForest hm hvpergraph modifv True elimination order File home jc godot research gPy gPy Structures py line 1321 in __init__ hvpergraph uforest hvpergraph join forest File home jc godot research gPy gPy Structures py line 415 in join forest raise DecomposabilityError s is not decomposable self gPy Structures DecomposabilityError TbOrCa XRay Bronchitis Smoking Tuberculosis VisitAsia Bronchitis Dyspnea TbOrCa Cancer Smoking Cancer TbOrCa Tuberculosis is not decomposable 5 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 JFM 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
27. 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 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 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_Vis
28. construction of hg1 above Empty hypergraphs are permissible in fact the empty tuple is the default collection of hyperedges The following are thus eguivalent gt gt gt e HyperGraph gt gt gt e HyperGraph gt gt gt print e I Empty hypergraphs are useful as initial hypergraphs to which hyperedges can be added later 2 2 Internal representation of hypergraphs Internally each hyperedge is stored as a frozenset of vertices Each hypergraph has a private attribute hyperedges which is just the set of these hyperedges which constitute the hypergraph This attribute contains all the information reguired to define the hypergraph However it is useful to also maintain a mapping from each vertex to the the set of hyperedges which contain it This is essentially the dual hypergraph 1 This mapping a Python dictionary is the private attribute _hyperedges_containing_vertex Here is the internal representation of the hg1 hypergraph given earlier with added line breaks gt gt gt for att val in vars hgi items print att print val print _hyperedges set frozenset C B l frozenset A D frozenset A B frozenset C D _hyperedges_containing vertex f A set frozenset A D frozenset A B C set frozenset C B frozenset C D 7B set frozenset C B frozenset
29. data print data row LO 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 asial Dvspnea l parent insts data gt gt gt for row in asial Dvspnea l parent insts print row l data_itr next C absent false 0 90000000000000002 0 10000000000000001 C absent true 0 29999999999999999 0 69999999999999996 C present false 0 20000000000000001 0 80000000000000004 C present true 0 10000000000000001 0 90000000000000002 28 Chapter 5 Models 5 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 gPv Examples import asia gt gt gt asia remove l Bronchitis Smoking gt gt gt print asia 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 are Ei T aa es anan nonsmoker 0 99 0 01 smoker 0 90 0 10 Cancer Tuberculosis TbOrCa false true m D L abs
30. ent Present NonSmoker False Absent Visit Normal I 0 00 Present Present Present NonSmoker False Present No Visit Abnormal 0 00 Present Present Present NonSmoker False Present No Visit Normal I 0 00 Present Present Present NonSmoker False Present Visit Abnormal 0 00 Present Present Present NonSmoker False Present Visit Normal I 0 00 Present Present Present NonSmoker True Absent No Visit Abnormal 0 00 Present Present Present NonSmoker True Absent No Visit Normal I 0 00 Present Present Present NonSmoker True Absent Visit Abnormal 0 00 Present Present Present NonSmoker True Absent Visit Normal I 0 00 Present Present Present NonSmoker True Present No Visit Abnormal 0 00 Present Present Present NonSmoker True Present No Visit Normal I 0 00 Present Present Present NonSmoker True Present Visit Abnormal 0 00 Present Present Present NonSmoker True Present Visit Normal I 0 00 Present Present Present Smoker False Absent No Visit Abnormal 0 00 Present Present Present Smoker False Absent No Visit Normal I 0 00 Present Present Present Smoker False Absent Visit Abnormal 0 00 Present Present Present Smoker False Absent Visit Normal I 0 00 Present Present Present Smoker False Present No Visit Abnormal 0 00 Present
31. ent absent 1 00 0 00 absent present 0 00 1 00 29 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 0 99 0 01 gt gt gt type asia lt class gPy Models HM gt In the example immediately above the factor with Bronchitis and Smoking as variables has been removed As a result the asia object is no longer a BNM Bayesian network object and has become a mere HM general hierarchical model object Nonetheless since its factors all remain CPTs they are printed out as such 5 2 Copying models Some methods alter the model object upon which they are called Methods which perform conditioning see Section 5 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 gt gt gt from gPy Examples import asia gt gt gt asia_cp asia copy 30 gt gt gt asia cp remove Cancer gt gt gt print asial Cancer l 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
32. gPy User Manual James Cussens University of York 14 1 2007 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 Contents 1 Quick Start 2 2 Hypergraphs 9 2 1 Constructing hypergraphs lt a 9 2 2 Internal representation of hypergraphs 10 2 3 Altering hypergraphs saba es ria ks 10 2 4 Copying hypergraphs a 11 2 5 Making graphs from hypergraphs ks 11 2 6 Hypergraph subclasses 00000 12 2 7 Constructing hypergraphs with specific properties 14 2 7 1 Potential pitfalls in creating hypergraphs 16 2 7 2 Creating hypergraphs without checks 16 2 7 3 Creating hypergraphs by modifying existing hypergraphs 17 2 8
33. if they are both elements of some hyperedge in H The graph of a hypergraph is generated using the graph method of gPy Structures HyperGraph gt gt gt print hgi A B C TB C TA D IC D gt gt gt print hgi graph Vertices PASS 2B KO 2D Lines A B or l DLbQAVDAa 11 D Figure 2 1 The graph of the hypergraph A B C B C A D C D Fig 2 1 provides an example of a graph generated from a hypergraph 2 6 Hypergraph subclasses The gPy Structures HyperGraph class is for general hypergraphs gPy also defines subclasses of gPy Structures HyperGraph for hypergraphs with certain properties The hierarchy is given in Fig 2 2 A reduced hypergraph contains no hyperedge contained within another To understand what a graphical hypergraph is we need to make reference to its graph Recall that each hypergraph H has an associated undirected graph G H as explained in Section 2 5 It is also the case that each undirected graph G has an associated hypergraph C G defined in terms of its cliques A clique in a graph is a maximally complete subset of vertices of the graph Each vertex in a clique is connected to all other vertices in the clique that s completeness and no vertex can be added to the clique without it becoming incomplete that s maximal completeness Since each clique is just a set of vertices it follows that the set of all cliques of a graph is a hypergraph the c
34. is viewed as an index for the internal data attribute a list which holds the data Here are some examples of using these various indexing options gt gt gt print factor Bronchitis Dyspnea TbOrCa Rese agente sen as esas 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 23 gt gt gt factor 2 0 10000000000000001 gt gt gt factor absent absent true 0 29999999999999999 gt gt gt factorlf Dvspnea absent Bronchitis absent TbOrCa true 0 29999999999999999 gt gt gt factor 2 4 0 10000000000000001 0 69999999999999996 4 2 Conditional probability tables Conditional probability tables from now on abbreviated to CPTs are a spe cial type of 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 4 2 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 print factor Bronchitis Dyspnea TbOrCa
35. it 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 Abnormal Normal Abnormal Normal Abnormal voocovcovcococovcovcocovcovcococovcococovcocovcovcocoococoovcocoocovcovcoococovcovcocoocoocooovcovcovoocovcooovcoovo o 00 00 00 00 00 02 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 01 00 00
36. lique hypergraph of the graph To generate the clique hypergraph gPy provides the gPy Structures UGraph method hypergraph Here we generate the hypergraph of the graph displayed in Fig 2 1 gt gt gt g1 hgi graph gt gt gt print gi Vertices LA B 2073 2D Lines A B A C D 6 D W 12 HyperGraph ReducedHyperGraph GraphicalHyperGraph GraphicalReducedHyperGraph DecomposableHyperGraph Figure 2 2 Hypergraph hierarchy 13 gt gt gt print gi hvpergraph TA B C SA C D It is interesting to examine the relationship between a hypergraph H gt gt gt print hgi I TA B C B C TA D TC D J and C G H the cligue hvpergraph of its graph gt gt gt print hgi graph hvpergraph A B C SA C D It is not difficult to prove that for any hvpergraph H every hvperedge in H is contained within some hvperedge in C G H However the reverse does not always follow In the example above the hvperedge A C D of C G H is not contained in any hvperedge of H This means that H is not graphical This leads us finally to the definition of a graphical hypergraph a hypergraph H is graphical iff every hyperedge of C G H is contained in some hyperedge of H It follows that if a hypergraph H is both graphical and reduced then H C G H TODO Stuff on decomposability 2 7 Constructing hypergraphs with specific prop erties gPy provides
37. nt Absent Absent Absent Absent Absent Absent 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 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 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 Present Present Present Present P
38. o True default is False Firstly consider doing this for ReducedHyperGraph objects since in this case there is one obvious choice for modification deleting redundant hyperedges gt gt gt print hg IB C TA B A IC D gt gt gt rhg ReducedHyperGraph hg copy modify True gt gt gt print rhg I IB C gt TA B C D At time of writing gPy provides no way of creating GraphicalHyperGraph and GraphicalReducedHyperGraph objects from hypergraphs which fail to meet the necessary conditions However it is possible to create a DecomposableHyperGraph from a non decomposable hypergraph Creating decomposable hypergraphs To create a decomposable hvpergraph a DecomposableHyperGraph object from an existing hypergraph there are a number of options If the existing hypergraph is decomposable then calls such as 17 gt gt gt dg DecomposableHyperGraph HyperGraph ABC BCD 1 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 assume 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
39. oker 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 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 True False False Present Absent Absent Absent Absent Present Present Present Present Absent Absent Absent Absent Present Present Present Present Absent Absent Absent Absent Present Present Present Pre
40. onSmoker True Present Visit Abnormal 0 00 Absent Absent Absent NonSmoker True Present Visit Normal 0 00 Absent Absent Absent Smoker l False Absent No Visit Abnormal 0 01 Absent Absent Absent Smoker l False Absent No Visit Normal 0 15 Absent Absent Absent Smoker l False Absent Visit Abnormal 0 00 Absent Absent Absent Smoker l False Absent Visit Normal 0 00 Absent Absent Absent Smoker l False Present No Visit Abnormal 0 00 Absent Absent Absent Smoker l 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 l Absent No Visit Abnormal 0 00 Absent Absent Absent Smoker True l Absent No Visit Normal 0 00 Absent Absent Absent Smoker True l Absent Visit Abnormal 0 00 Absent Absent Absent Smoker True l 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
41. ormal 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 oo000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060009 00 00 00 00 00 00 00 00 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 Present Present Present NonSmoker False Absent Visit Abnormal 0 00 Present Pres
42. r Bronchitis Smoking Absent NonSmoker 0 35 Absent Smoker 0 20 Present NonSmoker 0 15 Present Smoker 0 30 example factor is 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 lt class gPv Parameters Factor 2 gt gt gt tvpe asial Bronchitis lt class gPv Parameters CPT P Factors are functions mapping combinations of values of discrete variables to numbers These numbers don t have to probabilities gt gt gt example factor x 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 copy 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
43. raph 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 is a 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 hi and ho are in the same tree in F then for any hyperedge hg on the unique path between hi and ho we have hi N hg C hg 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 created similarly to DecomposableHyperGraph ob jects with one differences the JoinForest constructor does not accept a check argument since the creation of a join forest inevitablv checks that the input hypergraph is decomposable The JoinForest class is a direct subclass of the DecomposableHyperGraph class see Fig 2 3 for the full hierarchv of hy pergraphs JoinForest objects have an additional _uforest attribute which contains the join forest itself The join forest is a gPy Structures UForest object Here s two examples of creating JoinForest objects 19 HyperGraph ReducedHyperGraph GraphicalHyperGraph GraphicalReducedHyperGraph DecomposableHyperGraph JoinFo
44. resent Present Present Present Present Present Present Present Present Present Present Present Present Present Present Present Present Present Absent Absent Absent Absent Absent Absent Absent 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 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 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
45. rest Figure 2 3 Full hypergraph hierarchy including join forests 20 gt gt gt hg2 HyperGraph AB BC CD DA AE gt gt gt jf2a JoinForest hg2 copy modify True elimination_order ABCDE gt gt gt print jf2a IB C D E A B D E Vertices B C D E TA B D EH Lines A B D E TB C D E gt gt gt jf2b JoinForest hg2 copy modify True gt gt gt print jf2b 1 B C D TA B D SA E Vertices B C D A B D SA EH Lines A B D B C D A E TA B D 21 Chapter 3 Graphs Chapter 4 Factors 4 1 Extracting data from factors Data can be extracted from a factor using the usual Python indexing syntax There are a three permissible forms of index Dictionary If a dictionary is supplied as an index the keys of the dictionary should be the variables in the factor Associated with each variable kev 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 seguence 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 seguence
46. rue 0 15 present smoker false 0 30 present smoker true 0 30 Cancer Smoking TbOrCa 22222 Ss Sessa absent nonsmoker false 0 99 absent nonsmoker true 0 99 absent smoker false 0 90 absent smoker true 0 90 35 present nonsmoker false present nonsmoker true present smoker false present smoker true Cancer TbOrCa Tuberculosis i i aes ata absent false absent absent false present absent true absent absent l true present present false absent present false present present true absent present true present TbOrCa XRay L 2 4 false abnormal 0 05 false normal 0 95 true abnormal 0 98 true normal 0 02 Tuberculosis VisitAsia i sea 2 2 absent no visit 0 98 absent visit 0 01 present no visit 0 01 present visit 0 00 Separators Bronchitis TbOrCa S fetes AL absent false l 1 00 absent I true 1 00 present false 1 00 present true 1 00 Cancer TbOrCa LLS ji i ee absent l false l 1 00 3 6 00 00 00 00 00 00 00 00 absent true 1 00 present false 1 00 present true 1 00 Smoking TbOrCa S uu NDE nonsmoker false 1 00 nonsmoker true 1 00 smoker false 1 00 smoker true 1 00 TbOrCa NENG ji false l 1 00 true 1 00 Tuberculosis AE et ot as ji ra absent 1 00 present 1 0
47. sent 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 Normal Abnormal Normal Abnormal Normal Abnormal Normal Abnormal Normal Abnormal Normal Abnormal Normal Abnormal Normal Abn
48. 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 jc godot research gPy gPy Structures py line 689 in add_hyperedge raise RedundancyError s would make s no longer reduced hvperedge self gPy Structures RedundancyError A would make B C A C A B J no longer reduce gt gt gt hg HyperGraph rg gt gt gt hg add_hyperedge A gt gt gt print hg I IB C TA C TA B A As a convenience it is also possible to create any type of hypergraph directly from hyperedges exactly as described for the HyperGraph class in Section 2 1 gt gt gt dg DecomposableHyperGraph ABC BCD 15 gt gt gt gg GraphicalHyperGraph AB BC CD AD gt gt gt rg ReducedHyperGraph AB BC CA When called in this way a temporary internal HyperGraph is created from the hyperedges and the object is constructed from this temporary HyperGraph exactly as it had been supplied as an argument 2 7 1 Potential pitfalls in creating hypergraphs Recall that a new hypergraph created in this way from an old one has identical attributes to the original hypergraph This can lead a hypergraph to get into an illegal state Continuing the example immediately above gt gt gt id rg hvperedges 1215346996
49. tion of subsets of a finite set H These subsets h H are known as huperedges H is called the base set The elements of H are known as vertices In gPy hypergraphs are restricted so that H J ey h every element of the base set is contained in at least one hyperedge Table 2 1 gives some example hypergraphs don t worry about the final 3 columns for now 2 1 Constructing hypergraphs In gPy hypergraphs are object of the class gPy Structures 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 Structures import HyperGraph gt gt gt hg1 HyperGraph AB BC CD DA gt gt gt hg2 HyperGraph A A B C gt gt gt print hgi I B C TA D A B C D J gt gt gt print hg2 A B C D A Reduced Decomposable H 10 A A B A B B C A Ch A B B C C D tA DH UA B B C Table 2 1 Classification of some example hypergraphs Graphical 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 seguence such as a list or tuple but sets can be used as well Note that in Python a string is seen as a seguence of characters This was used in the
50. y Dyspnea Options for creating CPTs Firstly note that the default behaviour for creating CPTs is not to check that the resulting object is a valid CPT This is because it is often useful to create fake CPTs To check that the factor used to create a CPT is indeed a CPT use the cpt_check flag gt gt gt cpt CPT f 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 25 gt gt gt cpt CPT f copvO 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 created even in this situation To do so use the allow dummies flag gt gt gt print f Cancer TbOrCa Tuberculosis aan e absent false absent 1 00 absent false present 0 00 absent true absent 0 00 absent true present 1 0
Download Pdf Manuals
Related Search
Related Contents
3-Phase BLDC Motor Control with Sensorless Back EMF Wireless Networks for Mobile Robots - Robotics UWA Safety and Operating Instructions New features of the WinISIS system Indice - Testo Manuale delle istruzioni No. Cat. 37.3001 ENERGY Sunday, February 09, 2014 ELSEC – CONTROL AMBIENTAL 7650 iCON News 4+5/0 Page 1 Copyright © All rights reserved.
Failed to retrieve file