|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r72911 - sandbox/statistics/non_parametric/libs/statistics/detail/non_parametric/test
From: erwann.rogard_at_[hidden]
Date: 2011-07-05 07:23:17
Author: e_r
Date: 2011-07-05 07:23:16 EDT (Tue, 05 Jul 2011)
New Revision: 72911
URL: http://svn.boost.org/trac/boost/changeset/72911
Log:
cleaning up directory
Text files modified:
sandbox/statistics/non_parametric/libs/statistics/detail/non_parametric/test/contingency_table1.cpp | 103 ++++++++++++++++---------
sandbox/statistics/non_parametric/libs/statistics/detail/non_parametric/test/contingency_table2.cpp | 154 ++++++++++++++++++++++++---------------
2 files changed, 161 insertions(+), 96 deletions(-)
Modified: sandbox/statistics/non_parametric/libs/statistics/detail/non_parametric/test/contingency_table1.cpp
==============================================================================
--- sandbox/statistics/non_parametric/libs/statistics/detail/non_parametric/test/contingency_table1.cpp (original)
+++ sandbox/statistics/non_parametric/libs/statistics/detail/non_parametric/test/contingency_table1.cpp 2011-07-05 07:23:16 EDT (Tue, 05 Jul 2011)
@@ -5,25 +5,30 @@
// Software License, Version 1.0. (See accompanying file //
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) //
///////////////////////////////////////////////////////////////////////////////
+#include <boost/test/test_tools.hpp>
+#include <boost/test/unit_test.hpp>
+
#include <string>
#include <boost/mpl/int.hpp>
+#include <boost/mpl/vector/vector10.hpp>
+#include <boost/mpl/detail/wrapper.hpp>
+#include <boost/typeof/typeof.hpp>
#include <boost/fusion/include/make_map.hpp>
#include <boost/fusion/container/map/detail/sequence_to_map.hpp>
-#include <boost/mpl/vector/vector10.hpp>
#include <boost/accumulators/framework/accumulator_set.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/assign/list_of.hpp>
-#include <boost/statistics/detail/non_parametric/contingency_table/include/pearson_chisq/independence.hpp>
#include <boost/statistics/detail/non_parametric/contingency_table/include/factor.hpp>
#include <boost/statistics/detail/non_parametric/contingency_table/include/cells.hpp>
+#include <boost/statistics/detail/non_parametric/contingency_table/include/pearson_chisq/independence.hpp>
-#include <libs/statistics/detail/non_parametric/test/contingency_table1.h>
+#include <boost/statistics/detail/non_parametric/contingency_table/pearson_chisq/independence/tag.hpp>
void test_contingency_table1()
{
-
+ namespace ac = boost::accumulators;
namespace ct = boost::statistics::detail::contingency_table;
- namespace pearson_chisq = ct::pearson_chisq;
+ namespace ps = ct::pearson_chi_square_statistic;
typedef double val_;
typedef boost::mpl::int_<0> x_; typedef int data_x_;
@@ -35,62 +40,84 @@
>::type sample_;
typedef boost::mpl::vector2<x_,y_> keys_;
- typedef ct::pearson_chisq::tag::independence<keys_> hypothesis_;
- typedef boost::accumulators::stats< hypothesis_> stats_;
- typedef boost::accumulators::accumulator_set< sample_, stats_, long int > acc_;
+ typedef ps::tag::independence_between<keys_> hypothesis_;
+ typedef ac::stats< hypothesis_> stats_;
+ typedef ac::accumulator_set< sample_, stats_, long int > acc_;
using namespace boost::assign;
- acc_ acc(( ct::factor::_map_of_levels = boost::fusion::make_map<x_,y_>(
+ acc_ acc(( ct::_map_of_levels = boost::fusion::make_map<x_,y_>(
list_of(-1)(1), list_of("a")("b")("c") ) ));
{
// Check levels
- BOOST_ASSERT( ct::cells::cells_count<keys_>( acc ) == 2 * 3 );
- BOOST_ASSERT( ct::factor::extract::levels<x_>( acc ).count( -1 ) == 1 );
- BOOST_ASSERT( ct::factor::extract::levels<x_>( acc ).count( 1 ) == 1 );
- BOOST_ASSERT( ct::factor::extract::levels<x_>( acc ).count( 2 ) == 0 );
- BOOST_ASSERT( ct::factor::extract::levels<y_>( acc ).count( "a" ) == 1 );
- BOOST_ASSERT( ct::factor::extract::levels<y_>( acc ).count( "b" ) == 1 );
- BOOST_ASSERT( ct::factor::extract::levels<y_>( acc ).count( "c" ) == 1 );
- BOOST_ASSERT( ct::factor::extract::levels<y_>( acc ).count( "d" ) == 0 );
+ BOOST_CHECK( ct::cells_count<keys_>( acc ) == 2 * 3 );
+ BOOST_CHECK( ct::extract::levels<x_>( acc ).count( -1 ) == 1 );
+ BOOST_CHECK( ct::extract::levels<x_>( acc ).count( 1 ) == 1 );
+ BOOST_CHECK( ct::extract::levels<x_>( acc ).count( 2 ) == 0 );
+ BOOST_CHECK( ct::extract::levels<y_>( acc ).count( "a" ) == 1 );
+ BOOST_CHECK( ct::extract::levels<y_>( acc ).count( "b" ) == 1 );
+ BOOST_CHECK( ct::extract::levels<y_>( acc ).count( "c" ) == 1 );
+ BOOST_CHECK( ct::extract::levels<y_>( acc ).count( "d" ) == 0 );
}
{ //.....|.....|.....|.....|.....|.....|.....|
// |non- | x = | y = |
//total|empty|...........|.................|
// Filling the cells //count|cells| -1 | 1 | "a" | "b" | "c" |
- using namespace boost::accumulators; //.....|count|.....|.....|.....|.....|.....|
+ using namespace ac; //.....|count|.....|.....|.....|.....|.....|
acc( boost::fusion::make_map<x_,y_,z_>( -1, "a", 1 ), weight = 1 ); // 1 | 1 | 1 | 0 | 1 | 0 | 0 |
acc( boost::fusion::make_map<x_,y_,z_>( 1, "b", 1 ), weight = 2 ); // 3 | 2 | 1 | 2 | 1 | 2 | 0 |
}
{ // Check degrees of freedom
// lost df = (r-1) + (c-1) = 1 + 2 = 3;
// df = rc -r -c + 1 = 3 * 2 - 3 -2 + 1 = 2
- BOOST_ASSERT( pearson_chisq::lost_df( acc, hypothesis_() ) == 3);
- BOOST_ASSERT( pearson_chisq::df( acc, hypothesis_() ) == 2);
+
+ typedef boost::mpl::detail::wrapper<hypothesis_> h0_;
+ BOOST_CHECK( ps::lost_degrees_of_freedom( h0_(), acc ) == 3 );
+ BOOST_CHECK( ps::degrees_of_freedom( h0_(),acc ) == 2 );
}
{
// Check cells
typedef boost::mpl::vector1<x_> vec_x_;
typedef boost::mpl::vector1<y_> vec_y_;
std::size_t n;
- n = boost::accumulators::extract::weighted_count( acc );
- BOOST_ASSERT( n == 3 );
- n = ct::cells::non_empty_cells_count<keys_>( acc );
- BOOST_ASSERT( n == 2 );
- n = ct::cells::extract::cells<vec_x_>( acc )[ boost::fusion::make_map<x_>( -1 ) ];
- BOOST_ASSERT( n == 1 );
- n = ct::cells::extract::cells<vec_x_>( acc )[ boost::fusion::make_map<x_>( 1 ) ];
- BOOST_ASSERT( n == 2 );
- n = ct::cells::extract::cells<vec_y_>( acc )[ boost::fusion::make_map<y_>( "a" ) ];
- BOOST_ASSERT( n == 1 );
- n = ct::cells::extract::cells<vec_y_>( acc )[ boost::fusion::make_map<y_>( "b" ) ];
- BOOST_ASSERT( n == 2 );
- n = ct::cells::extract::cells<vec_y_>( acc )[ boost::fusion::make_map<y_>( "c" ) ];
- BOOST_ASSERT( n == 0 );
- n = ct::cells::extract::cells<keys_>( acc )[ boost::fusion::make_map<x_,y_,z_>( -1, "a", 1 ) ];
- BOOST_ASSERT( n == 1 );
- n = ct::cells::extract::cells<keys_>( acc )[ boost::fusion::make_map<x_,y_,z_>( 1, "b", 1 ) ];
- BOOST_ASSERT( n == 2 );
+ n = ac::extract::weighted_count( acc );
+ BOOST_CHECK( n == 3 );
+
+ n = ct::non_empty_cells_count<vec_x_>(acc);
+ BOOST_CHECK( n == 2 );
+ n = ct::non_empty_cells_count<vec_y_>(acc);
+ BOOST_CHECK( n == 2 );
+ n = ct::non_empty_cells_count<keys_>( acc );
+ BOOST_CHECK( n == 2 );
+
+ n = ct::cells_count<vec_x_>(acc);
+ BOOST_CHECK( n == 2 );
+ n = ct::cells_count<vec_y_>(acc);
+ BOOST_CHECK( n == 3 );
+ n = ct::cells_count<keys_>( acc );
+ BOOST_CHECK( n == 6 );
+
+ n = ct::count_matching<vec_x_>(
+ acc, boost::fusion::make_map<x_>( -1 ) );
+ BOOST_CHECK( n == 1 );
+ n = ct::count_matching<vec_x_>(
+ acc, boost::fusion::make_map<x_>( 1 ) );
+ BOOST_CHECK( n == 2 );
+ n = ct::count_matching<vec_y_>(
+ acc, boost::fusion::make_map<y_>( "a" ) );
+ BOOST_CHECK( n == 1 );
+ n = ct::count_matching<vec_y_>(
+ acc, boost::fusion::make_map<y_>( "b" ) );
+ BOOST_CHECK( n == 2 );
+ n = ct::count_matching<vec_y_>(
+ acc, boost::fusion::make_map<y_>( "c" ) );
+ BOOST_CHECK( n == 0 );
+ n = ct::count_matching<keys_>( acc,
+ boost::fusion::make_map<x_,y_,z_>( -1, "a", 1 ) );
+ BOOST_CHECK( n == 1 );
+ n = ct::count_matching<keys_>( acc,
+ boost::fusion::make_map<x_,y_,z_>( 1, "b", 1 ) );
+ BOOST_CHECK( n == 2 );
}
}
Modified: sandbox/statistics/non_parametric/libs/statistics/detail/non_parametric/test/contingency_table2.cpp
==============================================================================
--- sandbox/statistics/non_parametric/libs/statistics/detail/non_parametric/test/contingency_table2.cpp (original)
+++ sandbox/statistics/non_parametric/libs/statistics/detail/non_parametric/test/contingency_table2.cpp 2011-07-05 07:23:16 EDT (Tue, 05 Jul 2011)
@@ -8,8 +8,13 @@
#include <cmath>
#include <string>
+#include <boost/test/unit_test.hpp>
+
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector/vector10.hpp>
+#include <boost/mpl/detail/wrapper.hpp>
+
+#include <boost/typeof/typeof.hpp>
#include <boost/assign/list_of.hpp>
@@ -23,8 +28,10 @@
#include <boost/statistics/detail/non_parametric/contingency_table/include/factor.hpp>
#include <boost/statistics/detail/non_parametric/contingency_table/include/pearson_chisq/independence.hpp>
+#include <boost/statistics/detail/non_parametric/contingency_table/include/pearson_chisq/common.hpp>
-#include <libs/statistics/detail/non_parametric/test/contingency_table2.h>
+// Analysis of a dataset, and check that the results agree with the R software.
+// Source : http://www.math.wustl.edu/~victor/classes/ma322/r-eg-20.txt
/*
# Read the count data for this problem
@@ -62,8 +69,7 @@
{
namespace ct = boost::statistics::detail::contingency_table;
- namespace pearson_chisq = ct::pearson_chisq;
- using namespace boost::accumulators;
+ namespace ps = ct::pearson_chi_square_statistic;
typedef double val_;
typedef boost::mpl::int_<0> r_; typedef std::string data_r_;
@@ -79,10 +85,10 @@
typedef boost::mpl::vector2<r_,t_> sum_over_cols_;
typedef boost::mpl::vector2<c_,t_> sum_over_rows_;
- typedef ct::pearson_chisq::tag::independence<all_three_factors_> indep_r_c_t_;
- typedef ct::pearson_chisq::tag::independence<sum_over_tiers_> indep_r_c_;
- typedef ct::pearson_chisq::tag::independence<sum_over_cols_> indep_r_t_;
- typedef ct::pearson_chisq::tag::independence<sum_over_rows_> indep_c_t_;
+ typedef ps::tag::independence_between<all_three_factors_> indep_r_c_t_;
+ typedef ps::tag::independence_between<sum_over_tiers_> indep_r_c_;
+ typedef ps::tag::independence_between<sum_over_cols_> indep_r_t_;
+ typedef ps::tag::independence_between<sum_over_rows_> indep_c_t_;
typedef boost::accumulators::stats<
indep_r_c_t_,
@@ -93,8 +99,11 @@
typedef boost::accumulators::accumulator_set< sample_, stats_, long int > acc_;
using namespace boost::assign;
- acc_ acc(( ct::factor::_map_of_levels = boost::fusion::make_map<r_,c_,t_>(
- list_of("r1")("r2")("r3")("r4"), list_of("c1")("c2")("c3"), list_of("t1")("t2") ) ));
+ acc_ acc(( ct::_map_of_levels = boost::fusion::make_map<r_,c_,t_>(
+ list_of("r1")("r2")("r3")("r4"),
+ list_of("c1")("c2")("c3"),
+ list_of("t1")("t2") )
+ ));
typedef boost::fusion::result_of::make_map<
r_,c_,t_,data_r_,data_c_,data_t_>::type result_of_make_map_;
@@ -102,35 +111,38 @@
fp_ make_sample = boost::fusion::make_map<r_,c_,t_>;
- acc( make_sample( "r1", "c1", "t1" ), weight = 12 );
- acc( make_sample( "r1", "c1", "t2" ), weight = 4 );
- acc( make_sample( "r1", "c2", "t1" ), weight = 34 );
- acc( make_sample( "r1", "c2", "t2" ), weight = 47 );
- acc( make_sample( "r1", "c3", "t1" ), weight = 23 );
- acc( make_sample( "r1", "c3", "t2" ), weight = 11 );
- acc( make_sample( "r2", "c1", "t1" ), weight = 35 );
- acc( make_sample( "r2", "c1", "t2" ), weight = 34 );
- acc( make_sample( "r2", "c2", "t1" ), weight = 31 );
- acc( make_sample( "r2", "c2", "t2" ), weight = 10 );
- acc( make_sample( "r2", "c3", "t1" ), weight = 11 );
- acc( make_sample( "r2", "c3", "t2" ), weight = 18 );
- acc( make_sample( "r3", "c1", "t1" ), weight = 12 );
- acc( make_sample( "r3", "c1", "t2" ), weight = 18 );
- acc( make_sample( "r3", "c2", "t1" ), weight = 32 );
- acc( make_sample( "r3", "c2", "t2" ), weight = 13 );
- acc( make_sample( "r3", "c3", "t1" ), weight = 9 );
- acc( make_sample( "r3", "c3", "t2" ), weight = 19 );
- acc( make_sample( "r4", "c1", "t1" ), weight = 12 );
- acc( make_sample( "r4", "c1", "t2" ), weight = 9 );
- acc( make_sample( "r4", "c2", "t1" ), weight = 12 );
- acc( make_sample( "r4", "c2", "t2" ), weight = 33 );
- acc( make_sample( "r4", "c3", "t1" ), weight = 14 );
- acc( make_sample( "r4", "c3", "t2" ), weight = 25 );
+ {
+ using namespace boost::accumulators;
- using namespace std;
+ acc( make_sample( "r1", "c1", "t1" ), weight = 12 );
+ acc( make_sample( "r1", "c1", "t2" ), weight = 4 );
+ acc( make_sample( "r1", "c2", "t1" ), weight = 34 );
+ acc( make_sample( "r1", "c2", "t2" ), weight = 47 );
+ acc( make_sample( "r1", "c3", "t1" ), weight = 23 );
+ acc( make_sample( "r1", "c3", "t2" ), weight = 11 );
+ acc( make_sample( "r2", "c1", "t1" ), weight = 35 );
+ acc( make_sample( "r2", "c1", "t2" ), weight = 34 );
+ acc( make_sample( "r2", "c2", "t1" ), weight = 31 );
+ acc( make_sample( "r2", "c2", "t2" ), weight = 10 );
+ acc( make_sample( "r2", "c3", "t1" ), weight = 11 );
+ acc( make_sample( "r2", "c3", "t2" ), weight = 18 );
+ acc( make_sample( "r3", "c1", "t1" ), weight = 12 );
+ acc( make_sample( "r3", "c1", "t2" ), weight = 18 );
+ acc( make_sample( "r3", "c2", "t1" ), weight = 32 );
+ acc( make_sample( "r3", "c2", "t2" ), weight = 13 );
+ acc( make_sample( "r3", "c3", "t1" ), weight = 9 );
+ acc( make_sample( "r3", "c3", "t2" ), weight = 19 );
+ acc( make_sample( "r4", "c1", "t1" ), weight = 12 );
+ acc( make_sample( "r4", "c1", "t2" ), weight = 9 );
+ acc( make_sample( "r4", "c2", "t1" ), weight = 12 );
+ acc( make_sample( "r4", "c2", "t2" ), weight = 33 );
+ acc( make_sample( "r4", "c3", "t1" ), weight = 14 );
+ acc( make_sample( "r4", "c3", "t2" ), weight = 25 );
+ }
- val_ stat;
- long df;
+ using namespace std;
+ val_ stat, p_value;
+ long df;
/*
#### Output and interpretation (at significance level alpha=0.05):
@@ -147,13 +159,18 @@
#
*/
- // #include <boost/test/unit_test.hpp>
- // #include <boost/test/floating_point_comparison.hpp>
-
- df = pearson_chisq::df(acc,indep_r_c_t_());
- stat = pearson_chisq::statistic<val_>( acc, indep_r_c_t_() );
- BOOST_ASSERT( df == 17 );
- BOOST_ASSERT( fabs(stat - 102.17) < 0.01 );
+ {
+ typedef boost::mpl::detail::wrapper<indep_r_c_t_> h0_;
+ df = ps::degrees_of_freedom( h0_(), acc);
+ stat = ps::value( h0_(), acc );
+ p_value = cdf( complement(
+ ps::asy_distribution( h0_(), acc ),
+ stat
+ ) );
+ BOOST_CHECK( df == 17 );
+ BOOST_CHECK( fabs( stat - 102.17 ) < 0.01 );
+ BOOST_CHECK( fabs( p_value - 3.514e-14 ) < 0.001e-14 );
+ }
/*
> # 2-way Chi squared test of partial independence: R versus CT
@@ -168,11 +185,18 @@
#
*/
- df = pearson_chisq::df(acc,indep_c_t_());
- stat = pearson_chisq::statistic<val_>( acc, indep_c_t_() );
- BOOST_ASSERT( df == 2 );
- BOOST_ASSERT( fabs( stat == 2.3704 )< 0.001 );
-
+ {
+ typedef boost::mpl::detail::wrapper<indep_c_t_> h0_;
+ df = ps::degrees_of_freedom( h0_(), acc);
+ stat = ps::value( h0_(), acc );
+ p_value = cdf( complement(
+ ps::asy_distribution( h0_(), acc ),
+ stat
+ ) );
+ BOOST_CHECK( df == 2 );
+ BOOST_CHECK( fabs( stat == 2.3704 )< 0.001 );
+ BOOST_CHECK( fabs( p_value - 0.3057 ) < 0.0001 );
+ }
/*
>
> # 2-way Chi squared test of partial independence: C versus RT
@@ -187,12 +211,18 @@
# HA: r,t are NOT mutually independent
*/
-
- df = pearson_chisq::df(acc,indep_r_t_());
- stat = pearson_chisq::statistic<val_>( acc, indep_r_t_() );
- BOOST_ASSERT( df == 3 );
- BOOST_ASSERT( fabs( stat - 10.057 ) < 0.001 );
-
+ {
+ typedef boost::mpl::detail::wrapper<indep_r_t_> h0_;
+ df = ps::degrees_of_freedom( h0_(), acc);
+ stat = ps::value( h0_(), acc );
+ p_value = cdf( complement(
+ ps::asy_distribution( h0_(), acc ),
+ stat
+ ) );
+ BOOST_CHECK( df == 3 );
+ BOOST_CHECK( fabs( stat - 10.057 ) < 0.001 );
+ BOOST_CHECK( fabs( p_value - 0.01809 ) < 0.00001 );
+ }
/*
> # 2-way Chi squared test of partial independence: T versus RC
> summary(xtabs(count~r+c))
@@ -208,10 +238,18 @@
#
*/
- df = pearson_chisq::df(acc,indep_r_c_());
- stat = pearson_chisq::statistic<val_>( acc, indep_r_c_() );
- BOOST_ASSERT( df == 6 );
- BOOST_ASSERT( fabs( stat - 58.67 ) < 0.01 );
+ {
+ typedef boost::mpl::detail::wrapper<indep_r_c_> h0_;
+ df = ps::degrees_of_freedom( h0_(), acc);
+ stat = ps::value( h0_(), acc );
+ p_value = cdf( complement(
+ ps::asy_distribution( h0_(), acc ),
+ stat
+ ) );
+ BOOST_CHECK( df == 6 );
+ BOOST_CHECK( fabs( stat - 58.67 ) < 0.01 );
+ BOOST_CHECK( fabs( p_value - 8.363e-11 ) < 0.001e-11 );
+ }
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk