SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KernelTwoSampleTest.cpp
Go to the documentation of this file.
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 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012-2013 Heiko Strathmann
8  */
9 
12 #include <shogun/kernel/Kernel.h>
15 
16 using namespace shogun;
17 
20 {
21  init();
22 }
23 
25  CFeatures* p_and_q, index_t q_start) :
26  CTwoSampleTest(p_and_q, q_start)
27 {
28  init();
29 
30  m_kernel=kernel;
31  SG_REF(kernel);
32 }
33 
35  CFeatures* p, CFeatures* q) : CTwoSampleTest(p, q)
36 {
37  init();
38 
39  m_kernel=kernel;
40  SG_REF(kernel);
41 }
42 
44 {
46 }
47 
48 void CKernelTwoSampleTest::init()
49 {
50  SG_ADD((CSGObject**)&m_kernel, "kernel", "Kernel for two sample test",
51  MS_AVAILABLE);
52  m_kernel=NULL;
53 }
54 
56 {
57  SG_DEBUG("entering!\n");
58 
59  REQUIRE(m_kernel, "No kernel set!\n");
61  "No features and no custom kernel set!\n");
62 
63  /* compute sample statistics for null distribution */
64  SGVector<float64_t> results;
65 
66  /* only do something if a custom kernel is used: use the power of pre-
67  * computed kernel matrices
68  */
70  {
71  /* allocate memory */
73 
74  /* in case of custom kernel, there are no features */
75  index_t num_data;
77  num_data=m_kernel->get_num_vec_lhs();
78  else
79  num_data=m_p_and_q->get_num_vectors();
80 
81  /* memory for index permutations, (would slow down loop) */
82  SGVector<index_t> ind_permutation(num_data);
83  ind_permutation.range_fill();
84 
85  /* check if kernel is a custom kernel. In that case, changing features is
86  * not what we want but just subsetting the kernel itself */
87  CCustomKernel* custom_kernel=(CCustomKernel*)m_kernel;
88 
89  for (index_t i=0; i<m_num_null_samples; ++i)
90  {
91  /* idea: merge features of p and q, shuffle, and compute statistic.
92  * This is done using subsets here. add to custom kernel since
93  * it has no features to subset. CustomKernel has not to be
94  * re-initialised after each subset setting */
95  CMath::permute(ind_permutation);
96 
97  custom_kernel->add_row_subset(ind_permutation);
98  custom_kernel->add_col_subset(ind_permutation);
99 
100  /* compute statistic for this permutation of mixed samples */
101  results[i]=compute_statistic();
102 
103  /* remove subsets */
104  custom_kernel->remove_row_subset();
105  custom_kernel->remove_col_subset();
106  }
107  }
108  else
109  {
110  /* in this case, just use superclass method */
111  results=CTwoSampleTest::sample_null();
112  }
113 
114  SG_DEBUG("leaving!\n");
115 
116  return results;
117 }

SHOGUN Machine Learning Toolbox - Documentation