SHOGUN  6.1.3
MMD.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/kernel/Kernel.h>
40 
41 using namespace shogun;
42 using namespace internal;
43 using std::unique_ptr;
44 using std::shared_ptr;
45 
46 struct CMMD::Self
47 {
48  Self()
49  {
50  num_null_samples = DEFAULT_NUM_NULL_SAMPLES;
51  stype = DEFAULT_STYPE;
52  null_approximation_method = DEFAULT_NULL_APPROXIMATION_METHOD;
53  strategy=unique_ptr<CKernelSelectionStrategy>(new CKernelSelectionStrategy());
54  }
55 
59  std::unique_ptr<CKernelSelectionStrategy> strategy;
60 
61  static constexpr index_t DEFAULT_NUM_NULL_SAMPLES = 250;
62  static constexpr EStatisticType DEFAULT_STYPE = ST_UNBIASED_FULL;
63  static constexpr ENullApproximationMethod DEFAULT_NULL_APPROXIMATION_METHOD = NAM_PERMUTATION;
64 };
65 
67 {
68  init();
69 }
70 
71 CMMD::CMMD(CFeatures* samples_from_p, CFeatures* samples_from_q) : CTwoSampleTest(samples_from_p, samples_from_q)
72 {
73  init();
74 }
75 
76 void CMMD::init()
77 {
78 #if EIGEN_VERSION_AT_LEAST(3,1,0)
79  Eigen::initParallel();
80 #endif
81  self=unique_ptr<Self>(new Self());
82 }
83 
85 {
86  cleanup();
87 }
88 
90 {
91  self->strategy->use_method(method)
92  .use_weighted(weighted);
93 }
94 
96  index_t num_folds, float64_t alpha)
97 {
98  self->strategy->use_method(method)
99  .use_num_runs(num_runs)
100  .use_num_folds(num_folds)
101  .use_alpha(alpha);
102 }
103 
105 {
106  return self->strategy.get();
107 }
108 
110 {
111  self->strategy->add_kernel(kernel);
112 }
113 
115 {
116  SG_DEBUG("Entering!\n");
117  auto& data_mgr=get_data_mgr();
118  data_mgr.set_train_mode(true);
119  CMMD::set_kernel(self->strategy->select_kernel(this));
120  data_mgr.set_train_mode(false);
121  SG_DEBUG("Leaving!\n");
122 }
123 
125 {
126  get_kernel_mgr().restore_kernel_at(0);
127 }
128 
130 {
131  self->num_null_samples=null_samples;
132 }
133 
135 {
136  return self->num_null_samples;
137 }
138 
140 {
141  self->stype=stype;
142 }
143 
145 {
146  return self->stype;
147 }
148 
150 {
151  self->null_approximation_method=nmethod;
152 }
153 
155 {
156  return self->null_approximation_method;
157 }
158 
159 const char* CMMD::get_name() const
160 {
161  return "MMD";
162 }
virtual const char * get_name() const
Definition: MMD.cpp:159
int32_t index_t
Definition: common.h:72
EKernelSelectionMethod
Definition: TestEnums.h:61
virtual void select_kernel()
Definition: MMD.cpp:114
void set_num_null_samples(index_t null_samples)
Definition: MMD.cpp:129
index_t num_null_samples
Definition: MMD.cpp:56
Kernel two sample test base class. Provides an interface for performing a two-sample test using a ker...
Definition: TwoSampleTest.h:58
EStatisticType stype
Definition: MMD.cpp:57
CKernelSelectionStrategy const * get_kernel_selection_strategy() const
Definition: MMD.cpp:104
double float64_t
Definition: common.h:60
void add_kernel(CKernel *kernel)
Definition: MMD.cpp:109
EStatisticType
Definition: TestEnums.h:40
internal::DataManager & get_data_mgr()
index_t get_num_null_samples() const
Definition: MMD.cpp:134
internal::KernelManager & get_kernel_mgr()
void cleanup()
Definition: MMD.cpp:124
#define SG_DEBUG(...)
Definition: SGIO.h:106
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
EStatisticType get_statistic_type() const
Definition: MMD.cpp:144
The class Features is the base class of all feature objects.
Definition: Features.h:69
std::unique_ptr< CKernelSelectionStrategy > strategy
Definition: MMD.cpp:59
The Kernel base class.
void set_statistic_type(EStatisticType stype)
Definition: MMD.cpp:139
void set_null_approximation_method(ENullApproximationMethod nmethod)
Definition: MMD.cpp:149
virtual void set_kernel(CKernel *kernel)
virtual ~CMMD()
Definition: MMD.cpp:84
void set_kernel_selection_strategy(EKernelSelectionMethod method, bool weighted=false)
Definition: MMD.cpp:89
ENullApproximationMethod null_approximation_method
Definition: MMD.cpp:58
ENullApproximationMethod
Definition: TestEnums.h:53
ENullApproximationMethod get_null_approximation_method() const
Definition: MMD.cpp:154

SHOGUN Machine Learning Toolbox - Documentation