SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KernelIndependenceTestStatistic.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  CKernel* kernel_p, CKernel* kernel_q, CFeatures* p_and_q,
25  index_t q_start) : CTwoDistributionsTestStatistic(m_p_and_q, q_start)
26 {
27  init();
28 
29  m_kernel_p=kernel_p;
30  m_kernel_q=kernel_q;
31  SG_REF(kernel_p);
32  SG_REF(kernel_q);
33 }
34 
36  CKernel* kernel_p, CKernel* kernel_q, CFeatures* p, CFeatures* q) :
38 {
39  init();
40 
41  m_kernel_p=kernel_p;
42  SG_REF(kernel_p);
43 
44  m_kernel_q=kernel_q;
45  SG_REF(kernel_q);
46 }
47 
49 {
52 }
53 
54 void CKernelIndependenceTestStatistic::init()
55 {
56  SG_ADD((CSGObject**)&m_kernel_p, "kernel_p", "Kernel for samples from p",
57  MS_AVAILABLE);
58  SG_ADD((CSGObject**)&m_kernel_q, "kernel_q", "Kernel for samples from q",
59  MS_AVAILABLE);
60  m_kernel_p=NULL;
61  m_kernel_q=NULL;
62 }
63 
65 {
66  SG_DEBUG("entering CKernelIndependenceTestStatistic::bootstrap_null()\n");
67 
68  /* compute bootstrap statistics for null distribution */
69  SGVector<float64_t> results;
70 
71  /* only do something if a custom kernel is used: use the power of pre-
72  * computed kernel matrices
73  */
76  {
77  /* allocate memory */
79 
80  /* memory for index permutations, (would slow down loop) */
81  SGVector<index_t> ind_permutation(m_p_and_q->get_num_vectors());
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_p=(CCustomKernel*)m_kernel_p;
87  CCustomKernel* custom_kernel_q=(CCustomKernel*)m_kernel_q;
88 
89  for (index_t i=0; i<m_bootstrap_iterations; ++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  SGVector<int32_t>::permute_vector(ind_permutation);
96 
97  custom_kernel_p->add_row_subset(ind_permutation);
98  custom_kernel_p->add_col_subset(ind_permutation);
99  custom_kernel_q->add_row_subset(ind_permutation);
100  custom_kernel_q->add_col_subset(ind_permutation);
101 
102  /* compute statistic for this permutation of mixed samples */
103  results[i]=compute_statistic();
104 
105  /* remove subsets */
106  custom_kernel_p->remove_row_subset();
107  custom_kernel_p->remove_col_subset();
108  custom_kernel_q->remove_row_subset();
109  custom_kernel_q->remove_col_subset();
110  }
111  }
112  else
113  {
114  /* in this case, just use superclass method */
116  }
117 
118 
119  SG_DEBUG("leaving CKernelIndependenceTestStatistic::bootstrap_null()\n");
120  return results;
121 }
122 

SHOGUN Machine Learning Toolbox - Documentation