-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MultiKernelQuadraticTimeMMD.cpp
203 lines (172 loc) · 6.61 KB
/
MultiKernelQuadraticTimeMMD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2012 - 2013 Heiko Strathmann
* Written (w) 2014 - 2016 Soumyajit De
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/
#include <shogun/io/SGIO.h>
#include <shogun/lib/SGVector.h>
#include <shogun/kernel/ShiftInvariantKernel.h>
#include <shogun/distance/CustomDistance.h>
#include <shogun/statistical_testing/QuadraticTimeMMD.h>
#include <shogun/statistical_testing/MultiKernelQuadraticTimeMMD.h>
#include <shogun/statistical_testing/internals/FeaturesUtil.h>
#include <shogun/statistical_testing/internals/KernelManager.h>
#include <shogun/statistical_testing/internals/mmd/MultiKernelMMD.h>
#include <shogun/statistical_testing/internals/mmd/MultiKernelPermutationTest.h>
using namespace shogun;
using namespace internal;
using namespace mmd;
using std::unique_ptr;
struct CMultiKernelQuadraticTimeMMD::Self
{
Self(CQuadraticTimeMMD* owner);
void update_pairwise_distance(CDistance *distance);
CQuadraticTimeMMD *m_owner;
unique_ptr<CCustomDistance> m_pairwise_distance;
EDistanceType m_dtype;
KernelManager m_kernel_mgr;
};
CMultiKernelQuadraticTimeMMD::Self::Self(CQuadraticTimeMMD *owner) : m_owner(owner),
m_pairwise_distance(nullptr), m_dtype(D_UNKNOWN)
{
}
void CMultiKernelQuadraticTimeMMD::Self::update_pairwise_distance(CDistance* distance)
{
ASSERT(distance);
if (m_dtype==distance->get_distance_type())
{
ASSERT(m_pairwise_distance!=nullptr);
SG_SINFO("Precomputed distance exists for %s!\n", distance->get_name());
}
else
{
auto precomputed_distance=m_owner->compute_joint_distance(distance);
m_pairwise_distance=unique_ptr<CCustomDistance>(precomputed_distance);
m_dtype=distance->get_distance_type();
}
}
CMultiKernelQuadraticTimeMMD::CMultiKernelQuadraticTimeMMD() : CSGObject()
{
self=unique_ptr<Self>(new Self(nullptr));
}
CMultiKernelQuadraticTimeMMD::CMultiKernelQuadraticTimeMMD(CQuadraticTimeMMD* owner) : CSGObject()
{
self=unique_ptr<Self>(new Self(owner));
}
CMultiKernelQuadraticTimeMMD::~CMultiKernelQuadraticTimeMMD()
{
cleanup();
}
void CMultiKernelQuadraticTimeMMD::add_kernel(CShiftInvariantKernel *kernel)
{
ASSERT(self->m_owner);
REQUIRE(kernel, "Kernel instance cannot be NULL!\n");
self->m_kernel_mgr.push_back(kernel);
}
void CMultiKernelQuadraticTimeMMD::cleanup()
{
ASSERT(self->m_owner);
self->m_kernel_mgr.clear();
self->m_pairwise_distance=nullptr;
self->m_dtype=D_UNKNOWN;
}
SGVector<float64_t> CMultiKernelQuadraticTimeMMD::statistic()
{
ASSERT(self->m_owner);
return statistic(self->m_kernel_mgr);
}
SGVector<float64_t> CMultiKernelQuadraticTimeMMD::variance_h0()
{
ASSERT(self->m_owner);
SG_NOTIMPLEMENTED;
return SGVector<float64_t>();
}
SGVector<float64_t> CMultiKernelQuadraticTimeMMD::variance_h1()
{
ASSERT(self->m_owner);
SG_NOTIMPLEMENTED;
return SGVector<float64_t>();
}
SGVector<float64_t> CMultiKernelQuadraticTimeMMD::p_values()
{
ASSERT(self->m_owner);
return p_values(self->m_kernel_mgr);
}
SGVector<bool> CMultiKernelQuadraticTimeMMD::perform_test(float64_t alpha)
{
SGVector<float64_t> pvalues=p_values();
SGVector<bool> rejections(pvalues.size());
for (auto i=0; i<pvalues.size(); ++i)
{
rejections[i]=pvalues[i]<alpha;
}
return rejections;
}
SGVector<float64_t> CMultiKernelQuadraticTimeMMD::statistic(const KernelManager& kernel_mgr)
{
SG_DEBUG("Entering");
REQUIRE(kernel_mgr.num_kernels()>0, "Number of kernels (%d) have to be greater than 0!\n", kernel_mgr.num_kernels());
const auto nx=self->m_owner->get_num_samples_p();
const auto ny=self->m_owner->get_num_samples_q();
const auto stype = self->m_owner->get_statistic_type();
CDistance* distance=kernel_mgr.get_distance_instance();
self->update_pairwise_distance(distance);
kernel_mgr.set_precomputed_distance(self->m_pairwise_distance.get());
SG_UNREF(distance);
MultiKernelMMD compute(nx, ny, stype);
SGVector<float64_t> result=compute(kernel_mgr);
kernel_mgr.unset_precomputed_distance();
for (auto i=0; i<result.vlen; ++i)
result[i]=self->m_owner->normalize_statistic(result[i]);
SG_DEBUG("Leaving");
return result;
}
SGVector<float64_t> CMultiKernelQuadraticTimeMMD::p_values(const KernelManager& kernel_mgr)
{
SG_DEBUG("Entering");
REQUIRE(self->m_owner->get_null_approximation_method()==ENullApproximationMethod::NAM_PERMUTATION,
"Multi-kernel tests requires the H0 approximation method to be PERMUTATION!\n");
REQUIRE(kernel_mgr.num_kernels()>0, "Number of kernels (%d) have to be greater than 0!\n", kernel_mgr.num_kernels());
const auto nx=self->m_owner->get_num_samples_p();
const auto ny=self->m_owner->get_num_samples_q();
const auto stype = self->m_owner->get_statistic_type();
const auto num_null_samples = self->m_owner->get_num_null_samples();
CDistance* distance=kernel_mgr.get_distance_instance();
self->update_pairwise_distance(distance);
kernel_mgr.set_precomputed_distance(self->m_pairwise_distance.get());
SG_UNREF(distance);
MultiKernelPermutationTest compute(nx, ny, num_null_samples, stype);
SGVector<float64_t> result=compute(kernel_mgr);
kernel_mgr.unset_precomputed_distance();
SG_DEBUG("Leaving");
return result;
}
const char* CMultiKernelQuadraticTimeMMD::get_name() const
{
return "MultiKernelQuadraticTimeMMD";
}