1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
#define __STDCPP_WANT_MATH_SPEC_FUNCS__ 1 #include <cmath> #include <iterator> #include <tuple>
namespace errest {
template<class RealType = double> struct gaussian { static RealType critical_score(RealType cl, size_t df) {
using std::log; using std::sqrt; using std::erf; using std::exp;
auto erfinv = [](RealType x) { constexpr RealType k = 0.88622692545275801; RealType step, y = [](RealType x) { RealType sign = (x < 0) ? -1.0 : 1.0; x = (1 - x)*(1 + x); RealType lnx = log(x); RealType t1 = RealType(4.330746750799873)+lnx/2; RealType t2 = 1 / RealType(0.147) * lnx; return sign * sqrt(-t1 + sqrt(t1 * t1 - t2)); }(x); do { step = k * (erf(y) - x) / exp(-y * y); y -= step; } while (step); return y; }; return sqrt(RealType(2)) * erfinv(cl); } };
template<class RealType = double> struct student { static RealType critical_score(RealType cl, size_t df) { using std::beta; using std::sqrt; using std::pow; using std::lgamma; using std::exp; using std::log; using std::nextafter;
auto pdf = [](RealType x, RealType df) { return pow(df / (df + x * x), (df + 1)/2) / (sqrt(df) * beta(df/2, RealType(0.5))); };
auto cdf = [](RealType x, RealType df) { auto ibeta = [](RealType a, RealType b, RealType x){ const RealType lbeta_ab = lgamma(a)+lgamma(b)-lgamma(a+b); const RealType first = exp(log(x)*a+log(1-x)*b-lbeta_ab) / a; RealType f = 2, c = 2, d = 1, step; int m = 0; do { RealType o = -((a+m)*(a+b+m)*x)/((a+2*m)*(a+2*m+1)); m++; RealType e = (m*(b-m)*x)/((a+2*m-1)*(a+2*m)); RealType dt = 1 + o/d; d = 1 + e/dt; RealType ct = 1 + o/c; c = 1 + e/ct; f *= (step = (ct+e)/(dt+e)); } while(step - 1.); return first * (f - 1); }; RealType root = sqrt(x*x+df); return ibeta(df/2, df/2, (x + root)/(2*root)); };
cl = 1 - (1 - cl) / 2; RealType l = 0, r = 0, step; while((step = (cdf(r, df) - cl) / pdf(r, df)) < 0) { l = r; r -= step; } while (r > nextafter(l, r)) { RealType m = (l + r) / 2; (cdf(m, df) - cl < 0 ? l : r) = m; } return nextafter(l, r); } };
template<template<class> class Distribution = gaussian, class RealType = double> struct estimator {
template<class InputIt> static std::tuple<RealType,RealType> test (InputIt first, InputIt last, RealType cl = 0.95) {
auto n = distance(first, last); auto df = n - 1;
using std::distance; using std::next; using std::sqrt; auto sum = [](InputIt first, InputIt last, auto&& trans, auto&& sum)->RealType { if (distance(first, last) == 1) { return trans(*first); } else { auto mid = first + distance(first, last) / 2; return sum(first, mid, trans, sum) + sum(mid, last, trans, sum); } };
auto trans_mean = [n](RealType x)->RealType { return x/n; }; auto mean = sum(first, last, trans_mean, sum); auto trans_var = [mean, df](RealType x)->RealType { return (x - mean) * (x - mean) / df; }; auto var = sum(first, last, trans_var, sum);
auto sigma = sqrt(var/n);
return { mean, Distribution<RealType>::critical_score(cl, df) * sigma }; } };
using gaussian_estimator = estimator<gaussian>; using student_estimator = estimator<student>;
};
#include <random> #include <vector> #include <iostream> #include <iomanip>
int main(int argc, char** argv) {
std::random_device rd; std::mt19937 gen(rd()); std::normal_distribution<double> dist{5,2};
constexpr double confidence_level = 0.95; constexpr double error_tolerance = 0.10;
double mean, error; size_t iteration = 5; std::vector<double> samples;
while (1) { std::cout << "Single Experiment Results: "; for (size_t i = 0; i < iteration; i++) { double sample = dist(gen); samples.push_back(sample); if (i < 10) std::cout << sample << " "; else if (i < 11) std::cout << "..."; } std::cout << std::endl;
std::tie(mean, error) = errest::student_estimator::test(samples.begin(), samples.end(), confidence_level);
std::cout << " * Error Estimator Report: " "collected " << samples.size() << " results, " "mean: " << mean << ", error: " << error << std::endl << std::endl;
if (error < mean * error_tolerance) break;
}
std::cout << "demo converged with " << (confidence_level * 100) << " percent confidence " "within " << (error_tolerance * 100) << " percent error range. " "is the \"truth\" 5 within the interval " "[" << mean-error << ", " << mean+error << "]?" << std::endl; return 0;
}
|