SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
KernelIndependenceTest.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 #include <shogun/kernel/Kernel.h>
37 
38 using namespace shogun;
39 
42 {
43  init();
44 }
45 
47  CKernel* kernel_q, CFeatures* p, CFeatures* q) :
48  CIndependenceTest(p, q)
49 {
50  init();
51 
52  m_kernel_p=kernel_p;
53  SG_REF(kernel_p);
54 
55  m_kernel_q=kernel_q;
56  SG_REF(kernel_q);
57 }
58 
60 {
63 }
64 
65 void CKernelIndependenceTest::init()
66 {
67  SG_ADD((CSGObject**)&m_kernel_p, "kernel_p", "Kernel for samples from p",
68  MS_AVAILABLE);
69  SG_ADD((CSGObject**)&m_kernel_q, "kernel_q", "Kernel for samples from q",
70  MS_AVAILABLE);
71 
72  m_kernel_p=NULL;
73  m_kernel_q=NULL;
74 }
75 
77 {
78  SG_DEBUG("entering!\n")
79 
80  /* compute sample statistics for null distribution */
81  SGVector<float64_t> results;
82 
83  /* only do something if a custom kernel is used: use the power of pre-
84  * computed kernel matrices
85  */
88  {
89  /* allocate memory */
91 
92  /* memory for index permutations */
93  SGVector<index_t> ind_permutation(m_p->get_num_vectors());
94  ind_permutation.range_fill();
95 
96  /* check if kernel is a custom kernel. In that case, changing features is
97  * not what we want but just subsetting the kernel itself */
98  CCustomKernel* custom_kernel_p=(CCustomKernel*)m_kernel_p;
99 
100  for (index_t i=0; i<m_num_null_samples; ++i)
101  {
102  /* idea: shuffle samples from p while keeping samples from q intact
103  * and compute statistic. This is done using subsets here. add to
104  * custom kernel since it has no features to subset. CustomKernel
105  * has not to be re-initialised after each subset setting */
106  CMath::permute(ind_permutation);
107 
108  custom_kernel_p->add_row_subset(ind_permutation);
109  custom_kernel_p->add_col_subset(ind_permutation);
110 
111  /* compute statistic for this permutation of mixed samples */
112  results[i]=compute_statistic();
113 
114  /* remove subsets */
115  custom_kernel_p->remove_row_subset();
116  custom_kernel_p->remove_col_subset();
117  }
118  }
119  else
120  {
121  /* in this case, just use superclass method */
123  }
124 
125 
126  SG_DEBUG("leaving!\n")
127  return results;
128 }
129 
131 {
132  /* ref before unref to avoid problems when instances are equal */
133  SG_REF(kernel_p);
135  m_kernel_p=kernel_p;
136 }
137 
139 {
140  /* ref before unref to avoid problems when instances are equal */
141  SG_REF(kernel_q);
143  m_kernel_q=kernel_q;
144 }
145 
147 {
149  return m_kernel_p;
150 }
151 
153 {
155  return m_kernel_q;
156 }
157 
159 {
160  SG_DEBUG("entering!\n");
161 
163 
164  /* distinguish between custom and normal kernels */
166  {
167  /* custom kernels need to to be initialised when a subset is added */
168  CCustomKernel* custom_kernel_p=(CCustomKernel*)m_kernel_p;
169  K=custom_kernel_p->get_kernel_matrix();
170  }
171  else
172  {
173  /* need to init the kernel if kernel is not precomputed - if subsets of
174  * features are in the stack (for permutation), this will handle it */
175  m_kernel_p->init(m_p, m_p);
177  }
178 
179  SG_DEBUG("leaving!\n");
180 
181  return K;
182 }
183 
185 {
186  SG_DEBUG("entering!\n");
187 
189 
190  /* now second half of data for L */
192  {
193  /* custom kernels need to to be initialised - no subsets here */
194  CCustomKernel* custom_kernel_q=(CCustomKernel*)m_kernel_q;
195  L=custom_kernel_q->get_kernel_matrix();
196  }
197  else
198  {
199  /* need to init the kernel if kernel is not precomputed */
200  m_kernel_q->init(m_q, m_q);
202  }
203 
204  SG_DEBUG("leaving!\n");
205 
206  return L;
207 }
208 
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Kernel.cpp:98
void range_fill(T start=0)
Definition: SGVector.cpp:173
static void permute(SGVector< T > v, CRandom *rand=NULL)
Definition: Math.h:1144
int32_t index_t
Definition: common.h:62
virtual void add_row_subset(SGVector< index_t > subset)
The Custom Kernel allows for custom user provided kernel matrices.
Definition: CustomKernel.h:36
SGMatrix< float64_t > get_kernel_matrix_L()
virtual SGVector< float64_t > sample_null()
virtual int32_t get_num_vectors() const =0
virtual void set_kernel_q(CKernel *kernel_q)
SGMatrix< float64_t > get_kernel_matrix()
Definition: Kernel.h:219
#define SG_REF(x)
Definition: SGObject.h:51
virtual void remove_col_subset()
virtual void remove_row_subset()
virtual void add_col_subset(SGVector< index_t > subset)
SGMatrix< float64_t > get_kernel_matrix_K()
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
Provides an interface for performing the independence test. Given samples from the joint distributio...
virtual SGVector< float64_t > sample_null()
#define SG_UNREF(x)
Definition: SGObject.h:52
#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 set_kernel_p(CKernel *kernel_p)
virtual EKernelType get_kernel_type()=0
The class Features is the base class of all feature objects.
Definition: Features.h:68
The Kernel base class.
Definition: Kernel.h:158
#define SG_ADD(...)
Definition: SGObject.h:81
virtual float64_t compute_statistic()=0

SHOGUN Machine Learning Toolbox - Documentation