21 #include "lib/stat/statistic.hpp" 36 const size_t NUM_POINTS = 1
'000; 39 using lib::test::roughEQ; 41 using error::LUMIERA_ERROR_INVALID; 44 /**************************************************************//** 45 * @test verifies the proper working of statistic helper functions. 46 * - calculate mean and standard derivation 47 * - one-dimensional linear regression 48 * @see DataCSV_test.hpp 51 class Statistic_test : public Test 56 demonstrate_DataSpan(); 57 check_baseStatistics(); 58 check_wightedLinearRegression(); 59 check_TimeSeriesLinearRegression(); 66 demonstrate_DataSpan() 68 auto dat = VecD{0,1,2,3,4,5}; 71 CHECK (not isnil (all)); 72 CHECK (dat.size() == all.size()); 75 CHECK (i != all.end()); 80 DataSpan innr{*i, dat.back()}; 81 CHECK (util::join(innr) == "1, 2, 3, 4"_expect); 82 CHECK (2 == innr.at(1)); 85 CHECK (5 == innr[4]); // »undefined behaviour« 87 VERIFY_ERROR (INVALID, innr.at(4) ) 89 CHECK (1+2+3+4 == lib::explore(innr).resultSum()); 95 check_baseStatistics () 97 auto dat = VecD{4,2,5,8,6}; 98 DataSpan all = lastN(dat, dat.size()); 99 DataSpan rst = lastN(dat, 4); 100 CHECK (2 == *rst.begin()); 101 CHECK (4 == rst.size()); 102 CHECK (5 == all.size()); 104 CHECK (5.0 == average (all)); 105 CHECK (5.25 == average(rst)); 107 // Surprise : divide by N-1 since it is a guess for the real standard derivation 108 CHECK (sdev (all, 5.0) == sqrt(20/(5-1))); 110 CHECK (5.0 == averageLastN (dat,20)); 111 CHECK (5.0 == averageLastN (dat, 5)); 112 CHECK (5.25 == averageLastN (dat, 4)); 113 CHECK (7.0 == averageLastN (dat, 2)); 114 CHECK (6.0 == averageLastN (dat, 1)); 115 CHECK (0.0 == averageLastN (dat, 0)); 128 check_wightedLinearRegression() 130 RegressionData points{{1,1, 1} 135 auto [socket,gradient 139 ,sdev] = computeLinearRegression (points); 141 CHECK (socket == -1); 142 CHECK (gradient == 1); 143 CHECK (util::join (predicted) == "0, 4, 2"_expect ); 144 CHECK (util::join (deltas) == "1, 1, -1"_expect ); 145 CHECK (maxDelta == 1); 146 CHECK (correlation == "0.81649658"_expect ); 147 CHECK (sdev == "1.7320508"_expect ); 157 check_TimeSeriesLinearRegression() 159 auto dirt = [] { return ranRange(-0.5,+0.5); }; 160 auto fun = [&](uint i){ auto x = double(i)/NUM_POINTS; 164 data.reserve (NUM_POINTS); 165 for (uint i=0; i<NUM_POINTS; ++i) 166 data.push_back (fun(i) + dirt()); 168 auto [socket,gradient,correlation] = computeTimeSeriesLinearRegression (data); 170 // regression line should roughly connect 0 to 1, 171 // yet slightly shifted downwards, cutting through the parabolic curve 172 CHECK (roughEQ (gradient*NUM_POINTS, 1, 0.08)); 173 CHECK (roughEQ (socket, -0.16, 0.3 )); 174 CHECK (correlation > 0.65); 178 LAUNCHER (Statistic_test, "unit calculation"); 181 }}} // namespace lib::stat::test
Helpers typically used while writing tests.
Implementation namespace for support and library code.
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
Generating (pseudo) random numbers with controlled seed.
Building tree expanding and backtracking evaluations within hierarchical scopes.