SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
void range_fill(T start=0)
Definition: SGVector.cpp:171
static void permute(SGVector< T > v, CRandom *rand=NULL)
Definition: Math.h:1144
virtual CFeatures * get_p()
virtual CFeatures * get_q()
int32_t index_t
Definition: common.h:62
virtual int32_t get_num_vectors() const =0
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual void set_p(CFeatures *p)
#define SG_REF(x)
Definition: SGObject.h:54
Hypothesis test base class. Provides an interface for statistical hypothesis testing via three method...
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
virtual SGVector< float64_t > sample_null()
#define SG_UNREF(x)
Definition: SGObject.h:55
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void remove_subset()
Definition: Features.cpp:322
The class Features is the base class of all feature objects.
Definition: Features.h:68
#define SG_ADD(...)
Definition: SGObject.h:84
virtual float64_t compute_statistic()=0
virtual void add_subset(SGVector< index_t > subset)
Definition: Features.cpp:310
virtual void set_q(CFeatures *q)

SHOGUN Machine Learning Toolbox - Documentation