Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added hypothesis test (experimental) base class
- Loading branch information
Showing
3 changed files
with
177 additions
and
6 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/shogun/statistics/experimental/HypothesisTestExp.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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 selfied 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 <algorithm> | ||
#include <shogun/lib/SGVector.h> | ||
#include <shogun/mathematics/Math.h> | ||
#include <shogun/statistics/experimental/HypothesisTestExp.h> | ||
#include <shogun/statistics/experimental/internals/TestTypes.h> | ||
#include <shogun/statistics/experimental/internals/DataManager.h> | ||
#include <shogun/statistics/experimental/internals/KernelManager.h> | ||
|
||
using namespace shogun; | ||
using namespace internal; | ||
|
||
struct CHypothesisTestExp::Self | ||
{ | ||
Self(index_t num_distributions, index_t num_kernels) | ||
: data_manager(num_distributions), kernel_manager(num_kernels) | ||
{ | ||
} | ||
DataManager data_manager; | ||
KernelManager kernel_manager; | ||
}; | ||
|
||
CHypothesisTestExp::CHypothesisTestExp(index_t num_distributions, index_t num_kernels) : CSGObject() | ||
{ | ||
self = std::unique_ptr<Self>(new CHypothesisTestExp::Self(num_distributions, num_kernels)); | ||
} | ||
|
||
CHypothesisTestExp::~CHypothesisTestExp() | ||
{ | ||
} | ||
|
||
DataManager& CHypothesisTestExp::get_data_manager() | ||
{ | ||
return self->data_manager; | ||
} | ||
|
||
const DataManager& CHypothesisTestExp::get_data_manager() const | ||
{ | ||
return self->data_manager; | ||
} | ||
|
||
KernelManager& CHypothesisTestExp::get_kernel_manager() | ||
{ | ||
return self->kernel_manager; | ||
} | ||
|
||
const KernelManager& CHypothesisTestExp::get_kernel_manager() const | ||
{ | ||
return self->kernel_manager; | ||
} | ||
|
||
float64_t CHypothesisTestExp::compute_p_value(float64_t statistic) | ||
{ | ||
SGVector<float64_t> values = sample_null(); | ||
|
||
std::sort(values.vector, values.vector + values.vlen); | ||
float64_t i = values.find_position_to_insert(statistic); | ||
|
||
return 1.0 - i / values.vlen; | ||
} | ||
|
||
float64_t CHypothesisTestExp::compute_threshold(float64_t alpha) | ||
{ | ||
float64_t result = 0; | ||
SGVector<float64_t> values = sample_null(); | ||
|
||
std::sort(values.vector, values.vector + values.vlen); | ||
return values[index_t(CMath::floor(values.vlen * (1 - alpha)))]; | ||
} | ||
|
||
float64_t CHypothesisTestExp::perform_test() | ||
{ | ||
return compute_p_value(compute_statistic()); | ||
} | ||
|
||
bool CHypothesisTestExp::perform_test(float64_t alpha) | ||
{ | ||
float64_t p_value = perform_test(); | ||
return p_value < alpha; | ||
} | ||
|
||
const char* CHypothesisTestExp::get_name() const | ||
{ | ||
return "HypothesisTestExp"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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/>. | ||
*/ | ||
|
||
#ifndef HYPOTHESIS_TEST_EXP_H_ | ||
#define HYPOTHESIS_TEST_EXP_H_ | ||
|
||
#include <memory> | ||
#include <shogun/lib/config.h> | ||
#include <shogun/base/SGObject.h> | ||
|
||
namespace shogun | ||
{ | ||
|
||
class CFeatures; | ||
|
||
namespace internal | ||
{ | ||
|
||
class DataManager; | ||
class KernelManager; | ||
|
||
} | ||
|
||
class CHypothesisTestExp : public CSGObject | ||
{ | ||
public: | ||
CHypothesisTestExp(index_t num_distributions, index_t num_kernels); | ||
virtual ~CHypothesisTestExp(); | ||
|
||
virtual float64_t compute_statistic() = 0; | ||
|
||
virtual float64_t compute_p_value(float64_t statistic); | ||
virtual float64_t compute_threshold(float64_t alpha); | ||
|
||
float64_t perform_test(); | ||
bool perform_test(float64_t alpha); | ||
|
||
virtual SGVector<float64_t> sample_null() = 0; | ||
|
||
virtual const char* get_name() const; | ||
private: | ||
struct Self; | ||
std::unique_ptr<Self> self; | ||
protected: | ||
internal::DataManager& get_data_manager(); | ||
const internal::DataManager& get_data_manager() const; | ||
|
||
internal::KernelManager& get_kernel_manager(); | ||
const internal::KernelManager& get_kernel_manager() const; | ||
}; | ||
|
||
} | ||
|
||
#endif // HYPOTHESIS_TEST_EXP_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters