12 #include "gtest/gtest.h" 17 #include "libmesh/communicator.h" 18 #include "libmesh/parallel_object.h" 41 _generator.seed(seed);
44 Real sample()
const {
return _generator.randNormal(_mean, _std); }
45 std::vector<Real>
sample(std::size_t n)
const 47 std::vector<Real> data(n);
49 data.begin(), data.end(), [&](
Real &
v) {
v = _generator.randNormal(_mean, _std); });
55 const Real z = computeZ(level);
56 const Real mean_std = _std / std::sqrt(n);
62 const Real z = computeZ(level);
64 const Real gg = std::exp(std::lgamma(nr / 2.) - std::lgamma((nr - 1.) / 2.));
65 const Real std_std = _std * std::sqrt(1.0 - 2. / (nr - 1.) * gg * gg);
72 const Real alpha = level < 0.5 ? level : 1. - level;
74 return level < 0.5 ? -z : z;
85 const Real mean_dist = 1993;
86 const Real std_dist = 27;
87 const std::size_t nsamp = 1000;
90 const unsigned int replicates = 1e4;
91 const std::vector<Real> levels = {0.05, 0.1, 0.2, 0.8, 0.9, 0.95};
103 MooseEnum boot(
"percentile",
"percentile",
true);
109 const auto data = sampler.
sample(nsamp);
110 const Real mean_samp = mean_calc->compute(data,
false);
111 const Real std_samp = std_calc->compute(data,
false);
112 const std::vector<Real> mean_ci = mean_boot_calc->compute(data,
false);
113 const std::vector<Real> std_ci = std_boot_calc->compute(data,
false);
121 EXPECT_NEAR(mean_ci[l] - mean_samp, mean_ref, std::abs(mean_ref *
tol));
122 EXPECT_NEAR(std_ci[l] - std_samp, std_ref, std::abs(std_ref *
tol));
129 const Real mean_dist = 1993;
130 const Real std_dist = 27;
131 const std::size_t nsamp = 1000;
134 const unsigned int replicates = 1e4;
135 const std::vector<Real> levels = {0.05, 0.1, 0.2, 0.8, 0.9, 0.95};
153 const auto data = sampler.
sample(nsamp);
154 const Real mean_samp = mean_calc->compute(data,
false);
155 const Real std_samp = std_calc->compute(data,
false);
156 const std::vector<Real> mean_ci = mean_boot_calc->compute(data,
false);
157 const std::vector<Real> std_ci = std_boot_calc->compute(data,
false);
165 EXPECT_NEAR(mean_ci[l] - mean_samp, mean_ref, std::abs(mean_ref *
tol));
166 EXPECT_NEAR(std_ci[l] - std_samp, std_ref, std::abs(std_ref *
tol));
170 TEST(BootstrapCalculators, Percentile_Vec)
173 const std::size_t nsamp = 1000;
174 const std::size_t nval = 26;
175 std::vector<NormalSampler> samplers;
177 samplers.emplace_back( 1993 + 42 *
k, 27 + 7 *
k, 1945);
180 const unsigned int replicates = 1e4;
181 const std::vector<Real> levels = {0.05, 0.1, 0.2, 0.8, 0.9, 0.95};
189 auto mean_calc = makeCalculator<std::vector<std::vector<Real>>, std::vector<Real>>(calc[0], po);
190 auto std_calc = makeCalculator<std::vector<std::vector<Real>>, std::vector<Real>>(calc[1], po);
193 MooseEnum boot(
"percentile",
"percentile",
true);
194 auto mean_boot_calc = makeBootstrapCalculator<std::vector<std::vector<Real>>, std::vector<Real>>(
195 boot, po, levels, replicates, 2613, *mean_calc);
196 auto std_boot_calc = makeBootstrapCalculator<std::vector<std::vector<Real>>, std::vector<Real>>(
197 boot, po, levels, replicates, 2613, *std_calc);
200 std::vector<std::vector<Real>> data(nsamp);
201 for (
auto & dt : data)
202 for (
const auto & samp : samplers)
203 dt.push_back(samp.sample());
204 const std::vector<Real> mean_samp = mean_calc->compute(data,
false);
205 const std::vector<Real> std_samp = std_calc->compute(data,
false);
206 const std::vector<std::vector<Real>> mean_ci = mean_boot_calc->compute(data,
false);
207 const std::vector<std::vector<Real>> std_ci = std_boot_calc->compute(data,
false);
214 const Real mean_ref = samplers[
k].meanConfidence(levels[l], nsamp);
215 const Real std_ref = samplers[
k].stdConfidence(levels[l], nsamp);
216 EXPECT_NEAR(mean_ci[l][
k] - mean_samp[
k], mean_ref, std::abs(mean_ref *
tol));
217 EXPECT_NEAR(std_ci[l][
k] - std_samp[
k], std_ref, std::abs(std_ref *
tol));
221 TEST(BootstrapCalculators, BiasCorrectedAccelerated_Vec)
224 const std::size_t nsamp = 1000;
225 const std::size_t nval = 26;
226 std::vector<NormalSampler> samplers;
228 samplers.emplace_back( 1993 + 42 *
k, 27 + 7 *
k, 1945);
231 const unsigned int replicates = 1e4;
232 const std::vector<Real> levels = {0.05, 0.1, 0.2, 0.8, 0.9, 0.95};
240 auto mean_calc = makeCalculator<std::vector<std::vector<Real>>, std::vector<Real>>(calc[0], po);
241 auto std_calc = makeCalculator<std::vector<std::vector<Real>>, std::vector<Real>>(calc[1], po);
245 auto mean_boot_calc = makeBootstrapCalculator<std::vector<std::vector<Real>>, std::vector<Real>>(
246 boot, po, levels, replicates, 2613, *mean_calc);
247 auto std_boot_calc = makeBootstrapCalculator<std::vector<std::vector<Real>>, std::vector<Real>>(
248 boot, po, levels, replicates, 2613, *std_calc);
251 std::vector<std::vector<Real>> data(nsamp);
252 for (
auto & dt : data)
253 for (
const auto & samp : samplers)
254 dt.push_back(samp.sample());
255 const std::vector<Real> mean_samp = mean_calc->compute(data,
false);
256 const std::vector<Real> std_samp = std_calc->compute(data,
false);
257 const std::vector<std::vector<Real>> mean_ci = mean_boot_calc->compute(data,
false);
258 const std::vector<std::vector<Real>> std_ci = std_boot_calc->compute(data,
false);
265 const Real mean_ref = samplers[
k].meanConfidence(levels[l], nsamp);
266 const Real std_ref = samplers[
k].stdConfidence(levels[l], nsamp);
267 EXPECT_NEAR(mean_ci[l][
k] - mean_samp[
k], mean_ref, std::abs(mean_ref *
tol));
268 EXPECT_NEAR(std_ci[l][
k] - std_samp[
k], std_ref, std::abs(std_ref *
tol));
Real meanConfidence(Real level, std::size_t n) const
TEST(BootstrapCalculators, Percentile)
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
std::vector< Real > sample(std::size_t n) const
Real stdConfidence(Real level, std::size_t n) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
static const std::string v
static const std::string alpha
IntRange< T > make_range(T beg, T end)
These tests are meant to use the bootstrap calculators and test against analytical confidence interva...
NormalSampler(Real mean, Real std, unsigned int seed)
virtual Real quantile(const Real &p) const override
static const std::string k
auto index_range(const T &sizable)
static Real computeZ(Real level)