SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KernelTwoSampleTestStatistic.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 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  CTwoDistributionsTestStatistic(p_and_q, q_start)
26 {
27  init();
28 
29  m_kernel=kernel;
30  SG_REF(kernel);
31 
32  /* init kernel once in the beginning */
34 }
35 
38 {
39  init();
40 
41  m_kernel=kernel;
42  SG_REF(kernel);
43 
44  /* init kernel once in the beginning */
46 }
47 
49 {
51 }
52 
53 void CKernelTwoSampleTestStatistic::init()
54 {
55  SG_ADD((CSGObject**)&m_kernel, "kernel", "Kernel for two sample test",
56  MS_AVAILABLE);
57  m_kernel=NULL;
58 }
59 
61 {
62  /* compute bootstrap 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  /* memory for index permutations, (would slow down loop) */
74  SGVector<index_t> ind_permutation(m_p_and_q->get_num_vectors());
75  ind_permutation.range_fill();
76 
77  /* check if kernel is a custom kernel. In that case, changing features is
78  * not what we want but just subsetting the kernel itself */
79  CCustomKernel* custom_kernel=(CCustomKernel*)m_kernel;
80 
81  for (index_t i=0; i<m_bootstrap_iterations; ++i)
82  {
83  /* idea: merge features of p and q, shuffle, and compute statistic.
84  * This is done using subsets here. add to custom kernel since
85  * it has no features to subset. CustomKernel has not to be
86  * re-initialised after each subset setting */
87  SGVector<int32_t>::permute_vector(ind_permutation);
88 
89  custom_kernel->add_row_subset(ind_permutation);
90  custom_kernel->add_col_subset(ind_permutation);
91 
92  /* compute statistic for this permutation of mixed samples */
93  results[i]=compute_statistic();
94 
95  /* remove subsets */
96  custom_kernel->remove_row_subset();
97  custom_kernel->remove_col_subset();
98  }
99  }
100  else
101  {
102  /* in this case, just use superclass method */
104  }
105 
106  return results;
107 }

SHOGUN Machine Learning Toolbox - Documentation