SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IndependenceTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2012-2013 Heiko Strathmann
4  * Written (w) 2014 Soumyajit De
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  */
31 
34 
35 using namespace shogun;
36 
38 {
39  init();
40 }
41 
43  : CHypothesisTest()
44 {
45  init();
46 
47  SG_REF(p);
48  SG_REF(q);
49 
50  m_p=p;
51  m_q=q;
52 }
53 
55 {
56  SG_UNREF(m_p);
57  SG_UNREF(m_q);
58 }
59 
60 void CIndependenceTest::init()
61 {
62  SG_ADD((CSGObject**)&m_p, "p", "Samples from p", MS_NOT_AVAILABLE);
63  SG_ADD((CSGObject**)&m_q, "q", "Samples from q", MS_NOT_AVAILABLE);
64 
65  m_p=NULL;
66  m_q=NULL;
67 }
68 
70 {
71  SG_DEBUG("entering!\n")
72 
73  REQUIRE(m_p, "No features p!\n");
74  REQUIRE(m_q, "No features q!\n");
75 
76  /* compute sample statistics for null distribution */
78 
79  /* memory for index permutations. Adding of subset has to happen
80  * inside the loop since it may be copied if there already is one set.
81  *
82  * subset for selecting samples from p. In this case we want to
83  * shuffle only samples from p while keeping samples from q fixed */
84  SGVector<index_t> ind_permutation(m_p->get_num_vectors());
85  ind_permutation.range_fill();
86 
87  for (index_t i=0; i<m_num_null_samples; ++i)
88  {
89  /* idea: shuffle samples from p while keeping samples from q fixed
90  * and compute statistic. This is done using subsets here */
91 
92  /* create index permutation and add as subset to features from p */
93  CMath::permute(ind_permutation);
94 
95  /* compute statistic for this permutation of mixed samples */
96  m_p->add_subset(ind_permutation);
97  results[i]=compute_statistic();
98  m_p->remove_subset();
99  }
100 
101  SG_DEBUG("leaving!\n")
102  return results;
103 }
104 
106 {
107  /* ref before unref to avoid problems when instances are equal */
108  SG_REF(p);
109  SG_UNREF(m_p);
110  m_p=p;
111 }
112 
114 {
115  /* ref before unref to avoid problems when instances are equal */
116  SG_REF(q);
117  SG_UNREF(m_q);
118  m_q=q;
119 }
120 
122 {
123  SG_REF(m_p);
124  return m_p;
125 }
126 
128 {
129  SG_REF(m_q);
130  return m_q;
131 }
132 

SHOGUN Machine Learning Toolbox - Documentation