Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,21 @@ shark_add_test( RBM/BinaryLayer.cpp RBM_BinaryLayer)
shark_add_test( RBM/BipolarLayer.cpp RBM_BipolarLayer)
shark_add_test( RBM/GaussianLayer.cpp RBM_GaussianLayer)
shark_add_test( RBM/TruncatedExponentialLayer.cpp RBM_TruncatedExponentialLayer)

shark_add_test( RBM/MarkovChain.cpp RBM_MarkovChain)
#shark_add_test( RBM/GibbsOperator.cpp RBM_GibbsOperator)//not compiling anymore needs rewrite

shark_add_test( RBM/Energy.cpp RBM_Energy)
shark_add_test( RBM/AverageEnergyGradient.cpp RBM_AverageEnergyGradient)
shark_add_test( RBM/Analytics.cpp RBM_Analytics)

shark_add_test( RBM/ExactGradient.cpp RBM_ExactGradient)
#shark_add_test( RBM/ContrastiveDivergence.cpp RBM_ContrastiveDivergence) #does not compile currently
shark_add_test( RBM/TemperedMarkovChain.cpp RBM_TemperedMarkovChain)

shark_add_test( RBM/ParallelTemperingTraining.cpp RBM_PTTraining)
shark_add_test( RBM/PCDTraining.cpp RBM_PCDTraining)
shark_add_test( RBM/ContrastiveDivergenceTraining.cpp RBM_ContrastiveDivergenceTraining)
shark_add_test( RBM/ExactGradientTraining.cpp RBM_ExactGradientTraining)
#shark_add_test( RBM/ContrastiveDivergence.cpp RBM_ContrastiveDivergence) #does not compile currently
#shark_add_test( RBM/GibbsOperator.cpp RBM_GibbsOperator)//not compiling anymore needs rewrite

#Statistics
shark_add_test( Statistics/Tests.cpp Statistics_Tests)

#marking tests as slow
set_tests_properties( DirectSearch_HypervolumeContribution PROPERTIES LABELS "slow" )
Expand Down
102 changes: 102 additions & 0 deletions Test/Statistics/Tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@

#include <shark/Statistics/Tests.h>
#include <shark/Core/Random.h>

#define BOOST_TEST_MODULE Statistics_Tests
#include <boost/test/unit_test.hpp>
#include <boost/test/floating_point_comparison.hpp>

using namespace shark;

BOOST_AUTO_TEST_SUITE (Statistical_Tests)

//all results taken from R. note that the results are without continuity correction for ranksum tests!
BOOST_AUTO_TEST_CASE( TTest_Test) {
//mean is 49
RealVector sample = { 46, 48, 51, 49, 46, 51, 52, 47, 49, 48, 48, 51, 49, 50, 52, 47 };

{
statistics::TTest test(51);
BOOST_CHECK_CLOSE(test.statistics(sample),-4.0,1.e-10);
BOOST_CHECK_CLOSE(p(test,sample,statistics::Tail::Left),0.0005796584,1.e-5);
BOOST_CHECK_CLOSE(p(test,sample,statistics::Tail::Right),0.9994203,1.e-5);
BOOST_CHECK_CLOSE(p(test,sample,statistics::Tail::TwoSided),0.001159317,1.e-4);
}
}

BOOST_AUTO_TEST_CASE( TwoSample_TTest_Test) {
RealVector sampleX = { 104, 111, 116, 103, 97, 99, 109, 112, 94 };
RealVector sampleY = { 103, 110, 115, 102, 96, 98, 108, 111, 93, 104 };

//test for unequal variance(default)
{
statistics::TwoSampleTTest test;
BOOST_CHECK_CLOSE(test.statistics(sampleX, sampleY),0.2988072,1.e-4);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Left),0.6155935,1.e-5);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Right),0.3844065,1.e-4);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::TwoSided),0.768813,1.e-4);
}

//test for equal variance
{
statistics::TwoSampleTTest test(true);
BOOST_CHECK_CLOSE(test.statistics(sampleX, sampleY),0.2997885,1.e-4);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Left),0.6160134,1.e-5);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Right),0.3839866,1.e-4);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::TwoSided),0.7679731,1.e-4);
}
}

BOOST_AUTO_TEST_CASE( Paired_TTest_Test) {
RealVector sampleX = { 223, 259, 248, 220, 287, 191, 229, 270, 245, 201 };
RealVector sampleY = { 220, 244, 243, 211, 299 ,170, 210, 276, 252, 189 };

{
statistics::PairedTTest test;
BOOST_CHECK_CLOSE(test.statistics(sampleX, sampleY),1.638538,1.e-4);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Left),0.9321334,1.e-5);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Right),0.06786656,1.e-4);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::TwoSided),0.1357331,1.e-4);
}
}

BOOST_AUTO_TEST_CASE( WilcoxonRankSum_Test_NoTies) {
RealVector sampleX = { 0, 5, 5.5, 6, 7, 8, 9, 11, 13, 28, 29, 32, 33 };
RealVector sampleY = { 1, 2, 3, 4, 6.5, 8.5, 120 };
std::shuffle(sampleX.begin(),sampleX.end(),random::globalRng);
std::shuffle(sampleY.begin(),sampleY.end(),random::globalRng);
{
statistics::WilcoxonRankSumTest test;
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Left),0.928675,1.e-5);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Right),0.07132505,1.e-4);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::TwoSided),0.1426501,1.e-4);
}
}

BOOST_AUTO_TEST_CASE( WilcoxonRankSum_Test_Tied) {
RealVector sampleX = { 0, 5, 5.5, 6, 6, 7, 7, 7, 8, 9, 11, 13, 28, 29, 32, 33 };
RealVector sampleY = { 1, 2, 3, 4, 6, 6, 6, 6.5, 7, 7, 8.5, 120 };
std::shuffle(sampleX.begin(),sampleX.end(),random::globalRng);
std::shuffle(sampleY.begin(),sampleY.end(),random::globalRng);
{
statistics::WilcoxonRankSumTest test;
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Left),0.9579307,1.e-5);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Right),0.04206934,1.e-4);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::TwoSided),0.08413868,1.e-4);
}
}

BOOST_AUTO_TEST_CASE( WilcoxonSignedRank_Test) {
//diffs: 1, 3, 2.5, 2,0,1,1,0.5, 2, 2.5, 107
//ties: 1 1 2 2 2 1 1
RealVector sampleX = { 0, 5, 5.5, 6, 6, 7, 7, 7, 8, 9, 11, 13 };
RealVector sampleY = { 1, 2, 3, 4, 6, 6, 6, 6.5, 7, 7, 8.5, 120 };
{
statistics::WilcoxonSignedRankTest test;
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Left),0.9510063,1.e-5);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::Right),0.04899367,1.e-4);
BOOST_CHECK_CLOSE(p(test,sampleX, sampleY, statistics::Tail::TwoSided),0.09798734,1.e-4);
}
}

BOOST_AUTO_TEST_SUITE_END()
Loading