-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
LinearTimeMMD.cpp
157 lines (143 loc) · 4.35 KB
/
LinearTimeMMD.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
/*
* Restructuring Shogun's statistical hypothesis testing framework.
* Copyright (C) 2016 Soumyajit De
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <shogun/io/SGIO.h>
#include <shogun/lib/SGMatrix.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Statistics.h>
#include <shogun/distance/CustomDistance.h>
#include <shogun/statistical_testing/LinearTimeMMD.h>
#include <shogun/statistical_testing/internals/DataManager.h>
#include <shogun/statistical_testing/internals/mmd/WithinBlockDirect.h>
using namespace shogun;
using namespace internal;
CLinearTimeMMD::CLinearTimeMMD() : CMMD()
{
}
CLinearTimeMMD::CLinearTimeMMD(CFeatures* samples_from_p, CFeatures* samples_from_q) : CMMD()
{
set_p(samples_from_p);
set_q(samples_from_q);
}
CLinearTimeMMD::~CLinearTimeMMD()
{
}
void CLinearTimeMMD::set_num_blocks_per_burst(index_t num_blocks_per_burst)
{
auto& dm=get_data_manager();
auto min_blocksize=dm.get_min_blocksize();
if (min_blocksize==2)
{
// only possible when number of samples from both the distributions are the same
auto N=dm.num_samples_at(0);
for (auto i=2; i<N; ++i)
{
if (N%i==0)
{
min_blocksize=i*2;
break;
}
}
}
dm.set_blocksize(min_blocksize);
dm.set_num_blocks_per_burst(num_blocks_per_burst);
SG_SDEBUG("Block contains %d and %d samples, from P and Q respectively!\n", dm.blocksize_at(0), dm.blocksize_at(1));
}
const std::function<float32_t(SGMatrix<float32_t>)> CLinearTimeMMD::get_direct_estimation_method() const
{
return mmd::WithinBlockDirect();
}
const float64_t CLinearTimeMMD::normalize_statistic(float64_t statistic) const
{
const DataManager& dm = get_data_manager();
const index_t Nx = dm.num_samples_at(0);
const index_t Ny = dm.num_samples_at(1);
return CMath::sqrt(Nx * Ny / float64_t(Nx + Ny)) * statistic;
}
const float64_t CLinearTimeMMD::normalize_variance(float64_t variance) const
{
const DataManager& dm = get_data_manager();
const index_t Bx = dm.blocksize_at(0);
const index_t By = dm.blocksize_at(1);
const index_t B = Bx + By;
if (get_statistic_type() == EStatisticType::UNBIASED_INCOMPLETE)
{
return variance * B * (B - 2) / 16;
}
return variance * Bx * By * (Bx - 1) * (By - 1) / (B - 1) / (B - 2);
}
const float64_t CLinearTimeMMD::gaussian_variance(float64_t variance) const
{
const DataManager& dm = get_data_manager();
const index_t Bx = dm.blocksize_at(0);
const index_t By = dm.blocksize_at(1);
const index_t B = Bx + By;
if (get_statistic_type() == EStatisticType::UNBIASED_INCOMPLETE)
{
return variance * 4 / (B - 2);
}
return variance * (B - 1) * (B - 2) / (Bx - 1) / (By - 1) / B;
}
float64_t CLinearTimeMMD::compute_p_value(float64_t statistic)
{
float64_t result = 0;
switch (get_null_approximation_method())
{
case ENullApproximationMethod::MMD1_GAUSSIAN:
{
float64_t sigma_sq = gaussian_variance(compute_variance());
float64_t std_dev = CMath::sqrt(sigma_sq);
result = 1.0 - CStatistics::normal_cdf(statistic, std_dev);
break;
}
default:
{
result = CHypothesisTest::compute_p_value(statistic);
break;
}
}
return result;
}
float64_t CLinearTimeMMD::compute_threshold(float64_t alpha)
{
float64_t result = 0;
switch (get_null_approximation_method())
{
case ENullApproximationMethod::MMD1_GAUSSIAN:
{
float64_t sigma_sq = gaussian_variance(compute_variance());
float64_t std_dev = CMath::sqrt(sigma_sq);
result = 1.0 - CStatistics::inverse_normal_cdf(1 - alpha, 0, std_dev);
break;
}
default:
{
result = CHypothesisTest::compute_threshold(alpha);
break;
}
}
return result;
}
std::shared_ptr<CCustomDistance> CLinearTimeMMD::compute_distance()
{
auto distance=std::shared_ptr<CCustomDistance>(new CCustomDistance());
return distance;
}
const char* CLinearTimeMMD::get_name() const
{
return "LinearTimeMMD";
}