SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 }
void range_fill(T start=0)
Definition: SGVector.cpp:171
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
virtual SGVector< float64_t > sample_null()
virtual int32_t get_num_vectors() const =0
virtual float64_t compute_statistic()=0
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual int32_t get_num_vec_lhs()
Definition: Kernel.h:517
#define SG_REF(x)
Definition: SGObject.h:54
virtual void remove_col_subset()
virtual void remove_row_subset()
virtual void add_col_subset(SGVector< index_t > subset)
Provides an interface for performing the classical two-sample test i.e. Given samples from two distri...
Definition: TwoSampleTest.h:54
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 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:159
#define SG_ADD(...)
Definition: SGObject.h:84

SHOGUN Machine Learning Toolbox - Documentation