// statistics_functions_example1.cpp // Copyright Paul A. Bristow 2006. // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) // Example 2 of using Thorsten Ottosen's statistics function. // Problems using list, so abandoned in favour of Eric Niebler's accumulator. //#include // using boost::math::students_t; // Probability of students_t(df, t). //#define _SCL_SECURE_NO_DEPRECATE = 1 // avoid C4996 warning. //#define _SCL_SECURE_NO_DEPRECATE = 0 // get C4996 warning. #define _SCL_SECURE_NO_DEPRECATE = 1 // no C4996 warning. #ifdef _MSC_VER// needed for Boost.Array using const double # pragma warning(disable: 4510) // default constructor could not be generated. # pragma warning(disable: 4512) // assignment operator could not be generated. # pragma warning(disable: 4610) // can never be instantiated - user defined constructor required. #endif #include using boost::array; #include // Various basic statistics functions. using boost::algorithm::mean; #include using boost::math::students_t; // Probability of students_t(df, t). #include using std::cout; using std::endl; #include using std::ostream_iterator; #include using std::setprecision; using std::setw; #include using std::sqrt; #include using std::vector; #include using std::copy; #include using std::list; int main() { cout << "Example 1 of TO's statistics functions. "; #if defined(__FILE__) && defined(__TIMESTAMP__) cout << " " << __FILE__ << ' ' << __TIMESTAMP__ << ' '<< _MSC_FULL_VER; #endif cout << endl; const int n = 5; double data [n] = {10.08, 10.11, 10.09, 10.10, 10.12}; // C array. //const array adata = {10.08, 10.11, 10.09, 10.10, 10.12}; // Boost array c3892 connot assign to variable that is const array cddata = {10.08, 10.11, 10.09, 10.10, 10.12}; // Boost array C4510 default constructor could not be generated with const double //const array adata = {10.08, 10.11, 10.09, 10.10, 10.12}; // Boost array c4510 & c4512 assignment operator could not be generated // warning C4610: class 'boost::array' can never be instantiated - user defined constructor required. // const array adata = {10.08, 10.11, 10.09, 10.10, 10.12}; // Boost array //array adata = {10.08, 10.11, 10.09, 10.10, 10.12}; // Boost array array adata ; // Boost array copy (&data[0], &data[n], adata.begin()); // Note [n] (not [n-1]) because last is 1 beyond the end. // copy (&data[0], &data[n], adata.begin()); can't use this if double is const! // Error C3892: '_Dest' : you cannot assign to a variable that is const copy (cddata.begin(), cddata.end(), ostream_iterator(cout, " ")); cout << endl; double cdm = mean(cddata.begin(), cddata.end()); BOOST_ASSERT(cddata.size() == n); cout <<"array size is " << cddata.size() << endl; cout << "array mean is " << cdm << endl; copy (adata.begin(), adata.end(), ostream_iterator(cout, " ")); cout << endl; double am = mean(adata.begin(), adata.end()); BOOST_ASSERT(adata.size() == n); cout <<"array size is " << adata.size() << endl; cout << "array mean is " << am << endl; vector vdata; // Std vector //vector vdata; // Std vector // vdata = {10.08, 10.11, 10.09, 10.10, 10.12}; is NOT allowed :-(( vdata.reserve(5); vdata.assign(&data[0], &data[5]); cout << "vdata size = " << vdata.size() << endl; BOOST_ASSERT(vdata.size() == n); //copy (adata.begin(), adata.end(), vdata.begin()); // Asserts "vector iterator not dereferencable". copy (vdata.begin(), vdata.end(), ostream_iterator(cout, " ")); cout << endl; double vm = mean(vdata.begin(), vdata.end()); cout << "vector mean is " << vm << endl; using boost::algorithm::variance; using boost::algorithm::std_deviation; double vv = variance(vdata.begin(), vdata.end()); cout << "vector variance is " << vv << endl; double vsd = std_deviation(vdata.begin(), vdata.end()); cout << "vector std_deviation is " << vsd << endl; using boost::algorithm::sorted_median; using boost::algorithm::unsorted_median; // double vsm = unsorted_median(vdata.begin(), vdata.end()); //cout << "vector sorted_median is " << vsm << endl; // Using contain list doesn't yet work for me - asked Thorsten Ottosen. list ldata; ldata.assign(&data[0], &data[5]); BOOST_ASSERT(ldata.size() == n); copy (ldata.begin(), ldata.end(), ostream_iterator(cout, " ")); cout << endl; double lm = mean(ldata.begin(), ldata.end()); cout << "list mean is " << lm << endl; //double standard = 10.11; //double t = (mean(vdata.begin(), vdata.end()) - standard) //* std::sqrt(static_cast(n)) /// std_deviation(vdata.begin(), vdata.end()); //cout << "Student's t = " << t << endl; // Student's t = -1.41421 //double degrees_of_freedom = n-1; //cout.precision(5); // Useful accuracy is only a few decimal digits, but seems to give at least 5. //cout << "Probability of Student's t is " << students_t(degrees_of_freedom, abs(t)) << endl; // is 0.8849 , is 1 tailed. //// So there is insufficient evidence of a difference to meet a 95% (1 in 20) criterion. return 0; } // int main() /* Output is: */