Skip to content

Commit

Permalink
added rest of the files
Browse files Browse the repository at this point in the history
  • Loading branch information
lambday authored and karlnapf committed Jul 1, 2016
1 parent 391f644 commit a611c66
Show file tree
Hide file tree
Showing 38 changed files with 1,704 additions and 40 deletions.
114 changes: 114 additions & 0 deletions src/shogun/hypothesistest/BTestMMD.cpp
@@ -0,0 +1,114 @@
/*
* 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/>.
*/

#include <shogun/hypothesistest/BTestMMD.h>
#include <shogun/hypothesistest/internals/DataManager.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/Statistics.h>

using namespace shogun;
using namespace internal;

CBTestMMD::CBTestMMD() : CMMD<CBTestMMD>()
{
}

CBTestMMD::~CBTestMMD()
{
}

void CBTestMMD::set_blocksize(index_t blocksize)
{
get_data_manager().set_blocksize(blocksize);
}

void CBTestMMD::set_num_blocks_per_burst(index_t num_blocks_per_burst)
{
get_data_manager().set_num_blocks_per_burst(num_blocks_per_burst);
}

mmd::WithinBlockDirect CBTestMMD::get_direct_estimation_method()
{
static mmd::WithinBlockDirect method;
return method;
}

const float64_t CBTestMMD::normalize_statistic(float64_t statistic) const
{
const DataManager& dm = get_data_manager();
const index_t Nx = dm.num_samples_at(0);
const index_t Ny = dm.num_samples_at(1);
const index_t Bx = dm.blocksize_at(0);
const index_t By = dm.blocksize_at(1);
return Nx * Ny * statistic * CMath::sqrt((Bx + By)/float64_t(Nx + Ny)) / (Nx + Ny);
}

const float64_t CBTestMMD::normalize_variance(float64_t variance) const
{
const DataManager& dm = get_data_manager();
const index_t Bx = dm.blocksize_at(0);
const index_t By = dm.blocksize_at(1);
return variance * CMath::sq(Bx * By / float64_t(Bx + By));
}

float64_t CBTestMMD::compute_p_value(float64_t statistic)
{
float64_t result = 0;
switch (get_null_approximation_method())
{
case N_METHOD::MMD1_GAUSSIAN:
{
float64_t sigma_sq = compute_variance();
float64_t std_dev = CMath::sqrt(sigma_sq);
result = 1.0 - CStatistics::normal_cdf(statistic, std_dev);
break;
}
default:
{
result = CHypothesisTest::compute_p_value(statistic);
break;
}
}
return result;
}

float64_t CBTestMMD::compute_threshold(float64_t alpha)
{
float64_t result = 0;
switch (get_null_approximation_method())
{
case N_METHOD::MMD1_GAUSSIAN:
{
float64_t sigma_sq = compute_variance();
float64_t std_dev = CMath::sqrt(sigma_sq);
result = 1.0 - CStatistics::inverse_normal_cdf(1 - alpha, 0, std_dev);
break;
}
default:
{
result = CHypothesisTest::compute_threshold(alpha);
break;
}
}
return result;
}

const char* CBTestMMD::get_name() const
{
return "BTestMMD";
}
51 changes: 51 additions & 0 deletions src/shogun/hypothesistest/BTestMMD.h
@@ -0,0 +1,51 @@
/*
* 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 B_TEST_MMD_H_
#define B_TEST_MMD_H_

#include <shogun/hypothesistest/MMD.h>
#include <shogun/hypothesistest/internals/mmd/WithinBlockDirect.h>

namespace shogun
{

class CBTestMMD : public CMMD<CBTestMMD>
{
friend class CMMD;
public:
CBTestMMD();
virtual ~CBTestMMD();

virtual void ugly_hack_for_class_list() override {}

void set_blocksize(index_t blocksize);
void set_num_blocks_per_burst(index_t num_blocks_per_burst);

virtual float64_t compute_p_value(float64_t statistic) override;
virtual float64_t compute_threshold(float64_t alpha) override;

virtual const char* get_name() const;
private:
static internal::mmd::WithinBlockDirect get_direct_estimation_method();
const float64_t normalize_statistic(float64_t statistic) const;
const float64_t normalize_variance(float64_t variance) const;
};

}
#endif // B_TEST_MMD_H_
8 changes: 4 additions & 4 deletions src/shogun/hypothesistest/HypothesisTest.cpp
Expand Up @@ -19,10 +19,10 @@
#include <algorithm>
#include <shogun/lib/SGVector.h>
#include <shogun/mathematics/Math.h>
#include <shogun/hypothsistest/experimental/HypothesisTest.h>
#include <shogun/hypothsistest/experimental/internals/TestTypes.h>
#include <shogun/hypothsistest/experimental/internals/DataManager.h>
#include <shogun/hypothsistest/experimental/internals/KernelManager.h>
#include <shogun/hypothesistest/HypothesisTest.h>
#include <shogun/hypothesistest/internals/TestTypes.h>
#include <shogun/hypothesistest/internals/DataManager.h>
#include <shogun/hypothesistest/internals/KernelManager.h>

using namespace shogun;
using namespace internal;
Expand Down
62 changes: 62 additions & 0 deletions src/shogun/hypothesistest/IndependenceTest.cpp
@@ -0,0 +1,62 @@
/*
* 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/>.
*/

#include <shogun/kernel/Kernel.h>
#include <shogun/hypothesistest/IndependenceTest.h>
#include <shogun/hypothesistest/internals/KernelManager.h>
#include <shogun/hypothesistest/internals/TestTypes.h>

using namespace shogun;
using namespace internal;

CIndependenceTest::CIndependenceTest() : CTwoDistributionTest(IndependenceTest::num_kernels)
{
}

CIndependenceTest::~CIndependenceTest()
{
}

void CIndependenceTest::set_kernel_p(CKernel* kernel_p)
{
auto& km = get_kernel_manager();
km.kernel_at(0) = kernel_p;
}

CKernel* CIndependenceTest::get_kernel_p() const
{
const auto& km = get_kernel_manager();
return km.kernel_at(0);
}

void CIndependenceTest::set_kernel_q(CKernel* kernel_q)
{
auto& km = get_kernel_manager();
km.kernel_at(1) = kernel_q;
}

CKernel* CIndependenceTest::get_kernel_q() const
{
const auto& km = get_kernel_manager();
return km.kernel_at(1);
}

const char* CIndependenceTest::get_name() const
{
return "IndependenceTest";
}
48 changes: 48 additions & 0 deletions src/shogun/hypothesistest/IndependenceTest.h
@@ -0,0 +1,48 @@
/*
* 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 INDEPENDENCE_TEST_H_
#define INDEPENDENCE_TEST_H_

#include <shogun/hypothesistest/TwoDistributionTest.h>

namespace shogun
{

class CKernel;

class CIndependenceTest : public CTwoDistributionTest
{
public:
CIndependenceTest();
virtual ~CIndependenceTest();

void set_kernel_p(CKernel* kernel_p);
CKernel* get_kernel_p() const;

void set_kernel_q(CKernel* kernel_q);
CKernel* get_kernel_q() const;

virtual float64_t compute_statistic() = 0;
virtual SGVector<float64_t> sample_null() = 0;

virtual const char* get_name() const;
};

}
#endif // INDEPENDENCE_TEST_H_

0 comments on commit a611c66

Please sign in to comment.