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>
14 
15 using namespace shogun;
16 
19 {
20  init();
21 }
22 
24  CFeatures* p_and_q, index_t q_start) :
25  CTwoSampleTest(p_and_q, q_start)
26 {
27  init();
28 
29  m_kernel=kernel;
30  SG_REF(kernel);
31 }
32 
34  CFeatures* p, CFeatures* q) : CTwoSampleTest(p, q)
35 {
36  init();
37 
38  m_kernel=kernel;
39  SG_REF(kernel);
40 }
41 
43 {
45 }
46 
47 void CKernelTwoSampleTest::init()
48 {
49  SG_ADD((CSGObject**)&m_kernel, "kernel", "Kernel for two sample test",
50  MS_AVAILABLE);
51  m_kernel=NULL;
52 }
53 
55 {
56  SG_DEBUG("entering!\n");
57 
58  REQUIRE(m_kernel, "No kernel set!\n");
60  "No features and no custom kernel set!\n");
61 
62  /* compute sample statistics for null distribution */
63  SGVector<float64_t> results;
64 
65  /* only do something if a custom kernel is used: use the power of pre-
66  * computed kernel matrices
67  */
69  {
70  /* allocate memory */
72 
73  /* in case of custom kernel, there are no features */
74  index_t num_data;
76  num_data=m_kernel->get_num_vec_lhs();
77  else
78  num_data=m_p_and_q->get_num_vectors();
79 
80  /* memory for index permutations, (would slow down loop) */
81  SGVector<index_t> ind_permutation(num_data);
82  ind_permutation.range_fill();
83 
84  /* check if kernel is a custom kernel. In that case, changing features is
85  * not what we want but just subsetting the kernel itself */
86  CCustomKernel* custom_kernel=(CCustomKernel*)m_kernel;
87 
88  for (index_t i=0; i<m_num_null_samples; ++i)
89  {
90  /* idea: merge features of p and q, shuffle, and compute statistic.
91  * This is done using subsets here. add to custom kernel since
92  * it has no features to subset. CustomKernel has not to be
93  * re-initialised after each subset setting */
94  SGVector<int32_t>::permute_vector(ind_permutation);
95 
96  custom_kernel->add_row_subset(ind_permutation);
97  custom_kernel->add_col_subset(ind_permutation);
98 
99  /* compute statistic for this permutation of mixed samples */
100  results[i]=compute_statistic();
101 
102  /* remove subsets */
103  custom_kernel->remove_row_subset();
104  custom_kernel->remove_col_subset();
105  }
106  }
107  else
108  {
109  /* in this case, just use superclass method */
110  results=CTwoSampleTest::sample_null();
111  }
112 
113  SG_DEBUG("leaving!\n");
114 
115  return results;
116 }

SHOGUN Machine Learning Toolbox - Documentation