diff --git a/src/shogun/statistics/experimental/HypothesisTestExp.cpp b/src/shogun/statistics/experimental/HypothesisTestExp.cpp new file mode 100644 index 00000000000..6a78bdea4f2 --- /dev/null +++ b/src/shogun/statistics/experimental/HypothesisTestExp.cpp @@ -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 . + */ + +#include +#include +#include +#include +#include +#include +#include + +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(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 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 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"; +} diff --git a/src/shogun/statistics/experimental/HypothesisTestExp.h b/src/shogun/statistics/experimental/HypothesisTestExp.h new file mode 100644 index 00000000000..d1311a0e181 --- /dev/null +++ b/src/shogun/statistics/experimental/HypothesisTestExp.h @@ -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 . + */ + +#ifndef HYPOTHESIS_TEST_EXP_H_ +#define HYPOTHESIS_TEST_EXP_H_ + +#include +#include +#include + +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 sample_null() = 0; + + virtual const char* get_name() const; +private: + struct Self; + std::unique_ptr 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_ diff --git a/src/shogun/statistics/experimental/internals/TestTypes.h b/src/shogun/statistics/experimental/internals/TestTypes.h index 7981002098e..47f786f21cd 100644 --- a/src/shogun/statistics/experimental/internals/TestTypes.h +++ b/src/shogun/statistics/experimental/internals/TestTypes.h @@ -43,7 +43,7 @@ namespace internal struct OneDistributionTest { /** defines the number of feature objects required */ - enum { num_feats = 1 }; + static constexpr index_t num_feats = 1; }; /** @@ -52,7 +52,7 @@ struct OneDistributionTest struct TwoDistributionTest { /** defines the number of feature objects required */ - enum { num_feats = 2 }; + static constexpr index_t num_feats = 2; }; /** @@ -61,7 +61,7 @@ struct TwoDistributionTest struct ThreeDistributionTest { /** defines the number of feature objects required */ - enum { num_feats = 3 }; + static constexpr index_t num_feats = 3; }; /** @@ -70,7 +70,7 @@ struct ThreeDistributionTest struct GoodnessOfFitTest : OneDistributionTest { /** defines the number of kernel objects required */ - enum { num_kernels = 1 }; + static constexpr index_t num_kernels = 1; }; /** @@ -79,7 +79,7 @@ struct GoodnessOfFitTest : OneDistributionTest struct TwoSampleTest : TwoDistributionTest { /** defines the number of kernel objects required */ - enum { num_kernels = 1 }; + static constexpr index_t num_kernels = 1; }; /** @@ -88,7 +88,7 @@ struct TwoSampleTest : TwoDistributionTest struct IndependenceTest : TwoDistributionTest { /** defines the number of kernel objects required */ - enum { num_kernels = 2 }; + static constexpr index_t num_kernels = 2; }; }