source: tags/MetisMQIDemo/src/main/java/weka/classifiers/bayes/net/search/ci/CISearchAlgorithm.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 4.1 KB
Line 
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * CISearchAlgorithm.java
19 * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers.bayes.net.search.ci;
24
25import weka.classifiers.bayes.BayesNet;
26import weka.classifiers.bayes.net.ParentSet;
27import weka.classifiers.bayes.net.search.local.LocalScoreSearchAlgorithm;
28import weka.core.Instances;
29import weka.core.RevisionUtils;
30
31/**
32 <!-- globalinfo-start -->
33 * The CISearchAlgorithm class supports Bayes net structure search algorithms that are based on conditional independence test (as opposed to for example score based of cross validation based search algorithms).
34 * <p/>
35 <!-- globalinfo-end -->
36 *
37 <!-- options-start -->
38 * Valid options are: <p/>
39 *
40 * <pre> -mbc
41 *  Applies a Markov Blanket correction to the network structure,
42 *  after a network structure is learned. This ensures that all
43 *  nodes in the network are part of the Markov blanket of the
44 *  classifier node.</pre>
45 *
46 * <pre> -S [BAYES|MDL|ENTROPY|AIC|CROSS_CLASSIC|CROSS_BAYES]
47 *  Score type (BAYES, BDeu, MDL, ENTROPY and AIC)</pre>
48 *
49 <!-- options-end -->
50 *
51 * @author Remco Bouckaert (rrb@xm.co.nz)
52 * @version $Revision: 1.7 $
53 */
54public class CISearchAlgorithm 
55        extends LocalScoreSearchAlgorithm {
56       
57        /** for serialization */
58        static final long serialVersionUID = 3165802334119704560L;
59       
60        BayesNet  m_BayesNet;
61        Instances m_instances;
62           
63        /**
64         * Returns a string describing this object
65         * @return a description of the classifier suitable for
66         * displaying in the explorer/experimenter gui
67         */
68        public String globalInfo() {
69          return 
70              "The CISearchAlgorithm class supports Bayes net structure "
71            + "search algorithms that are based on conditional independence "
72            + "test (as opposed to for example score based of cross validation "
73            + "based search algorithms).";
74        }
75
76        /** IsConditionalIndependent tests whether two nodes X and Y are independent
77         *  given a set of variables Z. The test compares the score of the Bayes network
78         * with and without arrow Y->X where all nodes in Z are parents of X.
79         * @param iAttributeX - index of attribute representing variable X
80         * @param iAttributeY - index of attribute representing variable Y
81         * @param iAttributesZ - array of integers representing indices of attributes in set Z
82         * @param nAttributesZ - cardinality of Z
83         * @return true if X and Y conditionally independent given Z
84         */
85        protected boolean isConditionalIndependent(
86                int iAttributeX, 
87                int iAttributeY, 
88                int [] iAttributesZ, 
89                int nAttributesZ) {
90                ParentSet oParentSetX = m_BayesNet.getParentSet(iAttributeX);
91                // clear parent set of AttributeX
92                while (oParentSetX.getNrOfParents() > 0) {
93                        oParentSetX.deleteLastParent(m_instances);
94                }
95               
96                // insert parents in iAttributeZ
97                for (int iAttributeZ = 0; iAttributeZ < nAttributesZ; iAttributeZ++) {
98                        oParentSetX.addParent( iAttributesZ[iAttributeZ], m_instances);
99                }
100               
101                double fScoreZ = calcNodeScore(iAttributeX);
102                double fScoreZY = calcScoreWithExtraParent(iAttributeX, iAttributeY);
103                if (fScoreZY <= fScoreZ) {
104                        // the score does not improve by adding Y to the parent set of X
105                        // so we conclude that nodes X and Y are conditionally independent
106                        // given the set of variables Z
107                        return true;
108                }
109                return false;
110        } // IsConditionalIndependent
111
112        /**
113         * Returns the revision string.
114         *
115         * @return              the revision
116         */
117        public String getRevision() {
118          return RevisionUtils.extract("$Revision: 1.7 $");
119        }
120} // class CISearchAlgorithm
Note: See TracBrowser for help on using the repository browser.