SHOGUN
v2.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 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
/* init kernel once in the beginning */
33
m_kernel
->
init
(
m_p_and_q
,
m_p_and_q
);
34
}
35
36
CKernelTwoSampleTestStatistic::CKernelTwoSampleTestStatistic
(
CKernel
* kernel,
37
CFeatures
* p,
CFeatures
* q) :
CTwoDistributionsTestStatistic
(p, q)
38
{
39
init();
40
41
m_kernel
=kernel;
42
SG_REF
(kernel);
43
44
/* init kernel once in the beginning */
45
m_kernel
->
init
(
m_p_and_q
,
m_p_and_q
);
46
}
47
48
CKernelTwoSampleTestStatistic::~CKernelTwoSampleTestStatistic
()
49
{
50
SG_UNREF
(
m_kernel
);
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
60
SGVector<float64_t>
CKernelTwoSampleTestStatistic::bootstrap_null
()
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
*/
68
if
(
m_kernel
->
get_kernel_type
()==
K_CUSTOM
)
69
{
70
/* allocate memory */
71
results=
SGVector<float64_t>
(
m_bootstrap_iterations
);
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 */
103
results=
CTwoDistributionsTestStatistic::bootstrap_null
();
104
}
105
106
return
results;
107
}
SHOGUN
Machine Learning Toolbox - Documentation