Skip to content

Commit

Permalink
added hypothesis test (experimental) base class
Browse files Browse the repository at this point in the history
  • Loading branch information
lambday authored and karlnapf committed Jul 3, 2016
1 parent 59de0d4 commit 950a8ff
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 6 deletions.
102 changes: 102 additions & 0 deletions 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 <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";
}
69 changes: 69 additions & 0 deletions 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 <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_
12 changes: 6 additions & 6 deletions src/shogun/statistics/experimental/internals/TestTypes.h
Expand Up @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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;
};

}
Expand Down

0 comments on commit 950a8ff

Please sign in to comment.