-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
BTestMMD.cpp
122 lines (109 loc) · 3.19 KB
/
BTestMMD.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
/*
* 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/lib/SGMatrix.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Statistics.h>
#include <shogun/distance/CustomDistance.h>
#include <shogun/statistical_testing/BTestMMD.h>
#include <shogun/statistical_testing/internals/DataManager.h>
#include <shogun/statistical_testing/internals/mmd/WithinBlockDirect.h>
using namespace shogun;
using namespace internal;
CBTestMMD::CBTestMMD() : CMMD()
{
}
CBTestMMD::~CBTestMMD()
{
}
void CBTestMMD::set_blocksize(index_t blocksize)
{
get_data_manager().set_blocksize(blocksize);
}
void CBTestMMD::set_num_blocks_per_burst(index_t num_blocks_per_burst)
{
get_data_manager().set_num_blocks_per_burst(num_blocks_per_burst);
}
const std::function<float32_t(SGMatrix<float32_t>)> CBTestMMD::get_direct_estimation_method() const
{
return mmd::WithinBlockDirect();
}
const float64_t CBTestMMD::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);
const index_t Bx=dm.blocksize_at(0);
const index_t By=dm.blocksize_at(1);
return Nx*Ny*statistic*CMath::sqrt((Bx+By)/float64_t(Nx+Ny))/(Nx+Ny);
}
const float64_t CBTestMMD::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);
return variance*CMath::sq(Bx*By/float64_t(Bx+By));
}
float64_t CBTestMMD::compute_p_value(float64_t statistic)
{
float64_t result=0;
switch (get_null_approximation_method())
{
case NAM_MMD1_GAUSSIAN:
{
float64_t sigma_sq=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 CBTestMMD::compute_threshold(float64_t alpha)
{
float64_t result=0;
switch (get_null_approximation_method())
{
case NAM_MMD1_GAUSSIAN:
{
float64_t sigma_sq=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> CBTestMMD::compute_distance()
{
auto distance=std::shared_ptr<CCustomDistance>(new CCustomDistance());
return distance;
}
const char* CBTestMMD::get_name() const
{
return "BTestMMD";
}