30 #include "lib/stat/statistic.hpp" 45 const size_t NUM_POINTS = 1
'000; 48 using lib::test::roughEQ; 50 using error::LUMIERA_ERROR_INVALID; 53 /**************************************************************//** 54 * @test verifies the proper working of statistic helper functions. 55 * - calculate mean and standard derivation 56 * - one-dimensional linear regression 57 * @see DataCSV_test.hpp 60 class Statistic_test : public Test 65 demonstrate_DataSpan(); 66 check_baseStatistics(); 67 check_wightedLinearRegression(); 68 check_TimeSeriesLinearRegression(); 75 demonstrate_DataSpan() 77 auto dat = VecD{0,1,2,3,4,5}; 80 CHECK (not isnil (all)); 81 CHECK (dat.size() == all.size()); 84 CHECK (i != all.end()); 89 DataSpan innr{*i, dat.back()}; 90 CHECK (util::join(innr) == "1, 2, 3, 4"_expect); 91 CHECK (2 == innr.at(1)); 94 CHECK (5 == innr[4]); // »undefined behaviour« 96 VERIFY_ERROR (INVALID, innr.at(4) ) 98 CHECK (1+2+3+4 == lib::explore(innr).resultSum()); 104 check_baseStatistics () 106 auto dat = VecD{4,2,5,8,6}; 107 DataSpan all = lastN(dat, dat.size()); 108 DataSpan rst = lastN(dat, 4); 109 CHECK (2 == *rst.begin()); 110 CHECK (4 == rst.size()); 111 CHECK (5 == all.size()); 113 CHECK (5.0 == average (all)); 114 CHECK (5.25 == average(rst)); 116 // Surprise : divide by N-1 since it is a guess for the real standard derivation 117 CHECK (sdev (all, 5.0) == sqrt(20/(5-1))); 119 CHECK (5.0 == averageLastN (dat,20)); 120 CHECK (5.0 == averageLastN (dat, 5)); 121 CHECK (5.25 == averageLastN (dat, 4)); 122 CHECK (7.0 == averageLastN (dat, 2)); 123 CHECK (6.0 == averageLastN (dat, 1)); 124 CHECK (0.0 == averageLastN (dat, 0)); 137 check_wightedLinearRegression() 139 RegressionData points{{1,1, 1} 144 auto [socket,gradient 148 ,sdev] = computeLinearRegression (points); 150 CHECK (socket == -1); 151 CHECK (gradient == 1); 152 CHECK (util::join (predicted) == "0, 4, 2"_expect ); 153 CHECK (util::join (deltas) == "1, 1, -1"_expect ); 154 CHECK (maxDelta == 1); 155 CHECK (correlation == "0.81649658"_expect ); 156 CHECK (sdev == "1.7320508"_expect ); 166 check_TimeSeriesLinearRegression() 168 auto dirt = [] { return runi() - 0.5; }; 169 auto fun = [&](uint i){ auto x = double(i)/NUM_POINTS; 173 data.reserve (NUM_POINTS); 174 for (uint i=0; i<NUM_POINTS; ++i) 175 data.push_back (fun(i) + dirt()); 177 auto [socket,gradient,correlation] = computeTimeSeriesLinearRegression (data); 179 // regression line should roughly connect 0 to 1, 180 // yet slightly shifted downwards, cutting through the parabolic curve 181 CHECK (roughEQ (gradient*NUM_POINTS, 1, 0.08)); 182 CHECK (roughEQ (socket, -0.16, 0.3 )); 183 CHECK (correlation > 0.65); 187 LAUNCHER (Statistic_test, "unit calculation"); 190 }}} // namespace lib::stat::test
Helpers typically used while writing tests.
Implementation namespace for support and library code.
Simple 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.