Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
statistic-test.cpp
Go to the documentation of this file.
1/*
2 Statistic(Test) - validate simple statistic calculations
3
4 Copyright (C)
5 2009, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
19#include "lib/test/run.hpp"
22#include "lib/iter-explorer.hpp"
23#include "lib/format-util.hpp"
24#include "lib/random.hpp"
25#include "lib/util.hpp"
26#include "lib/format-cout.hpp"
28
29
30
31namespace lib {
32namespace stat{
33namespace test{
34
35 namespace {
36 const size_t NUM_POINTS = 1'000;
37 }
38
40 using util::isnil;
41 using error::LUMIERA_ERROR_INVALID;
42
43
44 /**************************************************************/
51 class Statistic_test : public Test
52 {
53 void
61
62
63
65 void
67 {
68 auto dat = VecD{0,1,2,3,4,5};
69
70 DataSpan all{dat};
71 CHECK (not isnil (all));
72 CHECK (dat.size() == all.size());
73
74 auto i = all.begin();
75 CHECK (i != all.end());
76 CHECK (0 == *i);
77 ++i;
78 CHECK (1 == *i);
79
80 DataSpan innr{*i, dat.back()};
81 CHECK (util::join(innr) == "1, 2, 3, 4"_expect);
82 CHECK (2 == innr.at(1));
83 CHECK (2 == innr[1]);
84 CHECK (4 == innr[3]);
85 CHECK (5 == innr[4]); // »undefined behaviour«
86
87 VERIFY_ERROR (INVALID, innr.at(4) )
88
89 CHECK (1+2+3+4 == lib::explore(innr).resultSum());
90 }
91
92
94 void
96 {
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());
103
104 CHECK (5.0 == average (all));
105 CHECK (5.25 == average(rst));
106
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)));
109
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));
116 }
117
118
127 void
129 {
130 RegressionData points{{1,1, 1}
131 ,{5,5, 1}
132 ,{3,1, 2}
133 };
134
135 auto [socket,gradient
136 ,predicted,deltas
137 ,correlation
138 ,maxDelta
139 ,sdev] = computeLinearRegression (points);
140
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 );
148 }
149
150
151
156 void
158 {
159 auto dirt = [] { return ranRange(-0.5,+0.5); };
160 auto fun = [&](uint i){ auto x = double(i)/NUM_POINTS;
161 return x*x;
162 };
163 VecD data;
164 data.reserve (NUM_POINTS);
165 for (uint i=0; i<NUM_POINTS; ++i)
166 data.push_back (fun(i) + dirt());
167
168 auto [socket,gradient,correlation] = computeTimeSeriesLinearRegression (data);
169
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);
175 }
176 };
177
178 LAUNCHER (Statistic_test, "unit calculation");
179
180
181}}} // namespace lib::stat::test
182
Read-only view into a segment within a sequence of data.
Definition statistic.hpp:93
iterator begin() const
size_t size() const
Helpers typically used while writing tests.
Automatically use custom string conversion in C++ stream output.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.
unsigned int uint
Definition integral.hpp:29
Building tree expanding and backtracking evaluations within hierarchical scopes.
DataSpan< double > lastN(VecD const &data, size_t n)
auto computeTimeSeriesLinearRegression(DataSpan< D > const &series)
Compute linear regression over a time series with zero-based indices.
std::vector< double > VecD
Definition statistic.hpp:57
double sdev(DataSpan< D > const &data, D mean)
auto computeLinearRegression(DataSpan< RegressionPoint > const &points)
Compute simple linear regression with a single predictor variable (x).
double averageLastN(VecD const &data, size_t n)
double average(DataSpan< D > const &data)
std::vector< RegressionPoint > RegressionData
constexpr meta::enable_if< std::is_floating_point< F >, bool > roughEQ(F val, N target, F limit=ROUGH_PRECISION)
Implementation namespace for support and library code.
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
double ranRange(double start, double bound)
Definition random.hpp:142
Test runner and basic definitions for tests.
string join(COLL &&coll, string const &delim=", ")
enumerate a collection's contents, separated by delimiter.
bool isnil(lib::time::Duration const &dur)
Generating (pseudo) random numbers with controlled seed.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Support for generic statistics calculations.
A collection of frequently used helper functions to support unit testing.
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...