SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
statistics
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-2013 Heiko Strathmann
8
*/
9
10
#include <
shogun/statistics/KernelTwoSampleTestStatistic.h
>
11
#include <
shogun/features/Features.h
>
12
#include <
shogun/kernel/Kernel.h
>
13
#include <
shogun/kernel/CustomKernel.h
>
14
15
using namespace
shogun;
16
17
CKernelTwoSampleTestStatistic::CKernelTwoSampleTestStatistic
() :
18
CTwoDistributionsTestStatistic
()
19
{
20
init();
21
}
22
23
CKernelTwoSampleTestStatistic::CKernelTwoSampleTestStatistic
(
CKernel
* kernel,
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
33
CKernelTwoSampleTestStatistic::CKernelTwoSampleTestStatistic
(
CKernel
* kernel,
34
CFeatures
* p,
CFeatures
* q) :
CTwoDistributionsTestStatistic
(p, q)
35
{
36
init();
37
38
m_kernel
=kernel;
39
SG_REF
(kernel);
40
}
41
42
CKernelTwoSampleTestStatistic::~CKernelTwoSampleTestStatistic
()
43
{
44
SG_UNREF
(
m_kernel
);
45
}
46
47
void
CKernelTwoSampleTestStatistic::init()
48
{
49
SG_ADD
((
CSGObject
**)&
m_kernel
,
"kernel"
,
"Kernel for two sample test"
,
50
MS_AVAILABLE
);
51
m_kernel
=NULL;
52
}
53
54
SGVector<float64_t>
CKernelTwoSampleTestStatistic::bootstrap_null
()
55
{
56
REQUIRE
(
m_kernel
,
"%s::bootstrap_null(): No kernel set!\n"
,
get_name
());
57
REQUIRE
(
m_kernel
->
get_kernel_type
()==
K_CUSTOM
||
m_p_and_q
,
58
"%s::bootstrap_null(): No features and no custom kernel set!\n"
,
59
get_name
());
60
61
/* compute bootstrap statistics for null distribution */
62
SGVector<float64_t>
results;
63
64
/* only do something if a custom kernel is used: use the power of pre-
65
* computed kernel matrices
66
*/
67
if
(
m_kernel
->
get_kernel_type
()==
K_CUSTOM
)
68
{
69
/* allocate memory */
70
results=
SGVector<float64_t>
(
m_bootstrap_iterations
);
71
72
/* memory for index permutations, (would slow down loop) */
73
74
/* in case of custom kernel, there are no features */
75
index_t
num_data;
76
if
(
m_kernel
->
get_kernel_type
()==
K_CUSTOM
)
77
num_data=
m_kernel
->
get_num_vec_lhs
();
78
else
79
num_data=
m_p_and_q
->
get_num_vectors
();
80
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_bootstrap_iterations
; ++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=
CTwoDistributionsTestStatistic::bootstrap_null
();
111
}
112
113
return
results;
114
}
SHOGUN
Machine Learning Toolbox - Documentation