26 template<
class SampleT, sdt Dim = 1,
class Real__ = Real>
33 typedef typename Real_::DoubleType Double_;
43 typedef function<Vec (const SampleList&)>
Func;
59 Bootstrap(
const Func& func,
RandomGen& gen,
const vector<SampleT>& samples, Real alpha = 0.05,
szt bootSampleCount = 50) :
60 func(func), gen(gen), samples(samples), alpha(alpha), bootSampleCount(bootSampleCount),
61 _lower(0), _upper(0), _progress(0), sampleTotal(0), idx(0),
62 bootRes(nullptr), jackRes(nullptr), jackMean(0)
64 if (samples.size() == 0)
return;
65 bootRes.set(
new Vec[bootSampleCount]);
66 bootSamples.resize(samples.size());
67 jackRes.set(
new Vec[samples.size()]);
79 void calc(Real progressDelta = 1);
84 const Vec&
lower()
const {
return _lower; }
86 const Vec&
upper()
const {
return _upper; }
93 Vec sum =
reduce(samples,
Vec().fromZero(), [](
const Vec& a,
const SampleT* e) {
return a + *e; });
94 return samples.size() > 0 ? sum / (
Real)samples.size() :
Vec().
fromZero();
104 Vec sumDev =
reduce(samples,
Vec().fromZero(), [&](
const Vec& a,
const SampleT* e) -> Real {
return a + (*e - mean).elemSqr(); });
105 return samples.size() > 1 ? sumDev / (
Real)(samples.size()-1) :
Vec().
fromZero();
113 const vector<SampleT>& samples;
128 SampleList bootSamples;
134 template<
class SampleT, sdt Dim,
class Real>
140 _progress =
Alge::min(_progress + progressDelta, 1);
141 sdt sampleAcc = _progress*(bootSampleCount + samples.size()) - sampleTotal;
142 sampleTotal += sampleAcc;
144 if (samples.size() == 0)
149 if (sampleTotal - sampleAcc < bootSampleCount)
152 for (; idx < bootSampleCount; ++idx, ++sampleCount)
154 if (sampleCount >= sampleAcc)
157 for (
szt i = 0; i < samples.size(); ++i)
158 bootSamples[i] = &samples[
Discrete_d(gen, 0, samples.size()-1).next()];
160 bootRes[idx] = func(const_cast<const SampleList&>(bootSamples));
164 for (
szt i = 0; i < samples.size(); ++i)
165 bootSamples[i] = &samples[i];
166 origRes = func(const_cast<const SampleList&>(bootSamples));
173 for (; idx < samples.size(); ++idx, ++sampleCount)
175 if (sampleCount >= sampleAcc)
178 bootSamples.erase(bootSamples.begin() + idx);
180 jackRes[idx] = func(const_cast<const SampleList&>(bootSamples));
181 jackMean += jackRes[idx];
183 bootSamples.insert(bootSamples.begin() + idx, &samples[idx]);
185 jackMean /=
Real(samples.size());
187 vector<Real> ecdf(bootSampleCount);
189 for (
sdt i = 0; i < Vec::s_size; ++i)
194 for (
szt j = 0; j < bootSampleCount; ++j)
196 Real val = bootRes[j][i];
197 if (val < origRes[i])
199 else if (val == origRes[i])
203 Real z =
Gaussian().cdfInv((sumLess + sumEq/2) / bootSampleCount);
204 std::sort(ecdf.begin(), ecdf.end());
209 for (
szt j = 0; j < samples.size(); ++j)
211 Real dev = jackMean[i] - jackRes[j][i];
214 sumDevCube += sqr*dev;
220 Real idxLower =
Gaussian().cdf(z + (z+za) / (1-acc*(z+za))) * (ecdf.size()-1);
221 Real idxUpper =
Gaussian().cdf(z + (z-za) / (1-acc*(z-za))) * (ecdf.size()-1);
224 _lower[i] = idxLower < ecdf.size()-1 ?
Interp::linear(
Alge::frac(idxLower), ecdf[idxLower], ecdf[idxLower+1]) : ecdf[idxLower];
225 _upper[i] = idxUpper < ecdf.size()-1 ?
Interp::linear(
Alge::frac(idxUpper), ecdf[idxUpper], ecdf[idxUpper+1]) : ecdf[idxUpper];
Real progress() const
Current progress of calculation, from 0 (start) to 1 (complete).
Definition: Bootstrap.h:82
static Real pow(Real x, Real y)
x raised to exponent y
Definition: Alge.h:73
Algebra.
Definition: Alge.h:13
Random number generator interface.
Definition: Gen.h:11
Gaussian_< Real > Gaussian
Definition: Gaussian.h:56
~Bootstrap()
Definition: Bootstrap.h:70
static Real frac(Real x)
Remove the whole part, leaving just the fraction.
Definition: Alge.h:42
Vec< dim, Real > Vec
Definition: Bootstrap.h:41
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
Interpolation math.
Definition: Quat.h:10
Vec operator()(const SampleList &samples)
Definition: Bootstrap.h:91
Accum reduce(Range &&, Seqs &&..., Accum &&initVal, Func &&)
Accumulate a series of sequences into an output.
static const sdt dim
Definition: Bootstrap.h:40
static std::common_type< Num, Num2 >::type min(Num a, Num2 b)
Get the minimum of two numbers.
Definition: Alge.h:89
vector< const SampleT * > SampleList
Definition: Bootstrap.h:42
function< Vec(const SampleList &)> Func
Definition: Bootstrap.h:43
Bootstrap(const Func &func, RandomGen &gen, const vector< SampleT > &samples, Real alpha=0.05, szt bootSampleCount=50)
Constructor, set up constants for all calculation calls.
Definition: Bootstrap.h:59
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
Discrete_< int64 > Discrete_d
Definition: Discrete.h:52
MatrixS & fromZero()
Zero all elements.
Definition: Base.h:70
void calc(Real progressDelta=1)
Perform bootstrap calculation. The calculation can be split up over multiple calls.
Definition: Bootstrap.h:135
double Real
Definition: Real.h:14
const Vec & lower() const
Get lower bound of confidence interval (calculation must be complete)
Definition: Bootstrap.h:84
Vec operator()(const SampleList &samples)
Definition: Bootstrap.h:101
static T linear(Real t, const T &a, const T &b)
Linear interpolation. t range is [0,1].
Definition: Interp.h:28
static Num sqr(Num x)
Square.
Definition: Alge.h:60
Real__ Real
Definition: Bootstrap.h:30
const Vec & upper() const
Get upper bound of confidence interval (calculation must be complete)
Definition: Bootstrap.h:86
Functor to estimate the sample variance. This is the unbiased estimator (mean over all possible sampl...
Definition: Bootstrap.h:99
Monte Carlo (random-based) method to estimate the interval of confidence in a function's result...
Definition: Bootstrap.h:27
Pointer to a unique, non-shared, object. Finalizer is run upon destruction (deletes object by default...
Definition: SharedPtr.h:164
Generate a normally (Gaussian) distributed random variate.
Definition: Gaussian.h:26
Functor to estimate the sample mean.
Definition: Bootstrap.h:89
Global Honeycomb namespace.