SHOGUN  6.0.0
BTestMMD.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2012 - 2013 Heiko Strathmann
4  * Written (w) 2014 - 2017 Soumyajit De
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * The views and conclusions contained in the software and documentation are those
28  * of the authors and should not be interpreted as representing official policies,
29  * either expressed or implied, of the Shogun Development Team.
30  */
31 
32 #include <shogun/lib/SGMatrix.h>
40 
41 using namespace shogun;
42 using namespace internal;
43 
45 {
46 }
47 
49 {
50 }
51 
53 {
54  get_data_mgr().set_blocksize(blocksize);
55 }
56 
57 void CBTestMMD::set_num_blocks_per_burst(index_t num_blocks_per_burst)
58 {
59  get_data_mgr().set_num_blocks_per_burst(num_blocks_per_burst);
60 }
61 
62 const std::function<float32_t(SGMatrix<float32_t>)> CBTestMMD::get_direct_estimation_method() const
63 {
64  return mmd::WithinBlockDirect();
65 }
66 
67 float64_t CBTestMMD::normalize_statistic(float64_t statistic) const
68 {
69  const DataManager& data_mgr=get_data_mgr();
70  const index_t Nx=data_mgr.num_samples_at(0);
71  const index_t Ny=data_mgr.num_samples_at(1);
72  const index_t Bx=data_mgr.blocksize_at(0);
73  const index_t By=data_mgr.blocksize_at(1);
74  return Nx*Ny*statistic*CMath::sqrt((Bx+By)/float64_t(Nx+Ny))/(Nx+Ny);
75 }
76 
77 const float64_t CBTestMMD::normalize_variance(float64_t variance) const
78 {
79  const DataManager& data_mgr=get_data_mgr();
80  const index_t Bx=data_mgr.blocksize_at(0);
81  const index_t By=data_mgr.blocksize_at(1);
82  return variance*CMath::sq(Bx*By/float64_t(Bx+By));
83 }
84 
86 {
87  float64_t result=0;
89  {
90  case NAM_MMD1_GAUSSIAN:
91  {
92  float64_t sigma_sq=compute_variance();
93  float64_t std_dev=CMath::sqrt(sigma_sq);
94  result=1.0-CStatistics::normal_cdf(statistic, std_dev);
95  break;
96  }
97  default:
98  {
99  result=CHypothesisTest::compute_p_value(statistic);
100  break;
101  }
102  }
103  return result;
104 }
105 
107 {
108  float64_t result=0;
110  {
111  case NAM_MMD1_GAUSSIAN:
112  {
113  float64_t sigma_sq=compute_variance();
114  float64_t std_dev=CMath::sqrt(sigma_sq);
115  result=1.0-CStatistics::inverse_normal_cdf(1-alpha, 0, std_dev);
116  break;
117  }
118  default:
119  {
121  break;
122  }
123  }
124  return result;
125 }
126 
127 const char* CBTestMMD::get_name() const
128 {
129  return "BTestMMD";
130 }
index_t & num_samples_at(index_t i)
int32_t index_t
Definition: common.h:72
virtual float64_t compute_p_value(float64_t statistic)
void set_blocksize(index_t blocksize)
Definition: DataManager.cpp:91
virtual float64_t compute_variance()
static T sq(T x)
Definition: Math.h:445
void set_num_blocks_per_burst(index_t num_blocks_per_burst)
Definition: BTestMMD.cpp:57
virtual float64_t compute_threshold(float64_t alpha)
virtual float64_t compute_p_value(float64_t statistic)
Definition: BTestMMD.cpp:85
const index_t blocksize_at(index_t i) const
double float64_t
Definition: common.h:60
static float64_t inverse_normal_cdf(float64_t y0, float64_t mean=0, float64_t std_dev=1)
Definition: Statistics.cpp:347
internal::DataManager & get_data_mgr()
void set_num_blocks_per_burst(index_t num_blocks_per_burst)
virtual const char * get_name() const
Definition: BTestMMD.cpp:127
void set_blocksize(index_t blocksize)
Definition: BTestMMD.cpp:52
virtual ~CBTestMMD()
Definition: BTestMMD.cpp:48
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
const ENullApproximationMethod get_null_approximation_method() const
static float64_t normal_cdf(float64_t x, float64_t std_dev=1)
Definition: Statistics.cpp:507
Class DataManager for fetching/streaming test data block-wise. It can handle data coming from multipl...
Definition: DataManager.h:63
static float32_t sqrt(float32_t x)
Definition: Math.h:454
virtual float64_t compute_threshold(float64_t alpha)
Definition: BTestMMD.cpp:106

SHOGUN Machine Learning Toolbox - Documentation