SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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>
36 
37 using namespace shogun;
38 
41 {
42  init();
43 }
44 
46  CKernel* kernel_q, CFeatures* p, CFeatures* q) :
47  CIndependenceTest(p, q)
48 {
49  init();
50 
51  m_kernel_p=kernel_p;
52  SG_REF(kernel_p);
53 
54  m_kernel_q=kernel_q;
55  SG_REF(kernel_q);
56 }
57 
59 {
62 }
63 
64 void CKernelIndependenceTest::init()
65 {
66  SG_ADD((CSGObject**)&m_kernel_p, "kernel_p", "Kernel for samples from p",
67  MS_AVAILABLE);
68  SG_ADD((CSGObject**)&m_kernel_q, "kernel_q", "Kernel for samples from q",
69  MS_AVAILABLE);
70 
71  m_kernel_p=NULL;
72  m_kernel_q=NULL;
73 }
74 
76 {
77  SG_DEBUG("entering!\n")
78 
79  /* compute sample statistics for null distribution */
80  SGVector<float64_t> results;
81 
82  /* only do something if a custom kernel is used: use the power of pre-
83  * computed kernel matrices
84  */
87  {
88  /* allocate memory */
90 
91  /* memory for index permutations */
92  SGVector<index_t> ind_permutation(m_p->get_num_vectors());
93  ind_permutation.range_fill();
94 
95  /* check if kernel is a custom kernel. In that case, changing features is
96  * not what we want but just subsetting the kernel itself */
97  CCustomKernel* custom_kernel_p=(CCustomKernel*)m_kernel_p;
98 
99  for (index_t i=0; i<m_num_null_samples; ++i)
100  {
101  /* idea: shuffle samples from p while keeping samples from q intact
102  * and compute statistic. This is done using subsets here. add to
103  * custom kernel since it has no features to subset. CustomKernel
104  * has not to be re-initialised after each subset setting */
105  SGVector<index_t>::permute_vector(ind_permutation);
106 
107  custom_kernel_p->add_row_subset(ind_permutation);
108  custom_kernel_p->add_col_subset(ind_permutation);
109 
110  /* compute statistic for this permutation of mixed samples */
111  results[i]=compute_statistic();
112 
113  /* remove subsets */
114  custom_kernel_p->remove_row_subset();
115  custom_kernel_p->remove_col_subset();
116  }
117  }
118  else
119  {
120  /* in this case, just use superclass method */
122  }
123 
124 
125  SG_DEBUG("leaving!\n")
126  return results;
127 }
128 
130 {
131  /* ref before unref to avoid problems when instances are equal */
132  SG_REF(kernel_p);
134  m_kernel_p=kernel_p;
135 }
136 
138 {
139  /* ref before unref to avoid problems when instances are equal */
140  SG_REF(kernel_q);
142  m_kernel_q=kernel_q;
143 }
144 
146 {
148  return m_kernel_p;
149 }
150 
152 {
154  return m_kernel_q;
155 }
156 

SHOGUN Machine Learning Toolbox - Documentation