https://mooseframework.inl.gov
Loading...
Searching...
No Matches
BootstrapCalculators.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12#include "Calculators.h"
13#include "NormalDistribution.h"
15
16#include "Shuffle.h"
17#include "MooseTypes.h"
18#include "MooseObject.h"
19#include "MooseEnum.h"
20#include "MooseError.h"
21#include "MooseRandom.h"
22
23#include "libmesh/parallel.h"
24#include "libmesh/parallel_sync.h"
25
26#include <memory>
27#include <vector>
28
29class MooseEnum;
30class MooseEnumItem;
31class MooseRandom;
32
33namespace StochasticTools
34{
35template <typename InType, typename OutType>
36class Calculator;
37
38/*
39 * Return available bootstrap statistics calculators.
40 */
42
51template <typename InType, typename OutType>
53{
54public:
56 const std::string & name,
57 const std::vector<Real> & levels,
58 unsigned int replicates,
59 unsigned int seed,
61 virtual std::vector<OutType> compute(const InType &, const bool) = 0;
62 const std::string & name() const { return _name; }
63
64protected:
65 // Compute Bootstrap estimates of a statistic
66 std::vector<OutType> computeBootstrapEstimates(const InType &, const bool);
67
68 // Confidence levels to compute in range (0, 1)
69 const std::vector<Real> _levels;
70
71 // Number of bootstrap replicates
72 const unsigned int _replicates;
73
74 // Random seed for creating bootstrap replicates
75 const unsigned int _seed;
76
77 // The Calculator that computes the statistic of interest
79
80private:
81 const std::string _name;
82};
83
84/*
85 * Implement percentile method of Efron and Tibshirani (2003), Chapter 13.
86 */
87template <typename InType, typename OutType>
88class Percentile : public BootstrapCalculator<InType, OutType>
89{
90public:
91 using BootstrapCalculator<InType, OutType>::BootstrapCalculator;
92 virtual std::vector<OutType> compute(const InType &, const bool) override;
93};
94
95/*
96 * Implement BCa method of Efron and Tibshirani (2003), Chapter 14.
97 */
98template <typename InType, typename OutType>
99class BiasCorrectedAccelerated : public BootstrapCalculator<InType, OutType>
100{
101public:
102 using BootstrapCalculator<InType, OutType>::BootstrapCalculator;
103 virtual std::vector<OutType> compute(const InType &, const bool) override;
104
105private:
106 // Compute the acceleration, see Efron and Tibshirani (2003), Ch. 14, Eq. 14.15, p 186.
107 OutType acceleration(const InType &, const bool);
108};
109
110/*
111 * Simple struct that makeBootstrapCalculator wraps around, this is so building calculators
112 * can be partially specialized.
113 */
114template <typename InType, typename OutType>
116{
117 static std::unique_ptr<BootstrapCalculator<InType, OutType>>
118 build(const MooseEnum &,
120 const std::vector<Real> &,
121 unsigned int,
122 unsigned int,
124};
125
126template <typename InType, typename OutType>
127std::unique_ptr<BootstrapCalculator<InType, OutType>>
130 const std::vector<Real> &,
131 unsigned int,
132 unsigned int,
134
135template <typename InType, typename OutType>
137 const libMesh::ParallelObject & other,
138 const std::string & name,
139 const std::vector<Real> & levels,
140 unsigned int replicates,
141 unsigned int seed,
143 : libMesh::ParallelObject(other),
144 _levels(levels),
145 _replicates(replicates),
146 _seed(seed),
147 _calc(calc),
148 _name(name)
149{
150 mooseAssert(*std::min_element(levels.begin(), levels.end()) > 0,
151 "The supplied levels must be greater than zero.");
152 mooseAssert(*std::max_element(levels.begin(), levels.end()) < 1,
153 "The supplied levels must be less than one");
154}
155
156template <typename InType, typename OutType>
157std::vector<OutType>
159 const bool is_distributed)
160{
161 MooseRandom generator;
162 generator.seed(0, _seed);
163
164 // Compute replicate statistics
165 std::vector<OutType> values(_replicates);
166 auto calc_update = [this](const typename InType::value_type & val)
167 { _calc.updateCalculator(val); };
168 for (std::size_t i = 0; i < _replicates; ++i)
169 {
170 _calc.initializeCalculator();
172 data, calc_update, generator, 0, is_distributed ? &this->_communicator : nullptr);
173 _calc.finalizeCalculator(is_distributed);
174 values[i] = _calc.getValue();
175 }
177 return values;
178}
179
180// makeBootstrapCalculator /////////////////////////////////////////////////////////////////////////
181template <typename InType, typename OutType>
182std::unique_ptr<BootstrapCalculator<InType, OutType>>
184 const libMesh::ParallelObject & other,
185 const std::vector<Real> & levels,
186 unsigned int replicates,
187 unsigned int seed,
189{
191 item, other, levels, replicates, seed, calc);
192}
193
194} // namespace
std::array< Real, 2 > values
const std::string name
Definition Setup.h:21
void seed(std::size_t i, unsigned int seed)
OutType acceleration(const InType &, const bool)
virtual std::vector< OutType > compute(const InType &, const bool) override
Base class for computing bootstrap confidence level intervals.
BootstrapCalculator(const libMesh::ParallelObject &other, const std::string &name, const std::vector< Real > &levels, unsigned int replicates, unsigned int seed, StochasticTools::Calculator< InType, OutType > &calc)
std::vector< OutType > computeBootstrapEstimates(const InType &, const bool)
virtual std::vector< OutType > compute(const InType &, const bool)=0
StochasticTools::Calculator< InType, OutType > & _calc
virtual std::vector< OutType > compute(const InType &, const bool) override
void resampleWithFunctor(const std::vector< T > &data, const ActionFunctor &functor, MooseRandom &generator, const std::size_t seed_index=0)
Enum for batch type in stochastic tools MultiApp.
MooseEnum makeBootstrapCalculatorEnum()
std::unique_ptr< BootstrapCalculator< InType, OutType > > makeBootstrapCalculator(const MooseEnum &, const libMesh::ParallelObject &, const std::vector< Real > &, unsigned int, unsigned int, StochasticTools::Calculator< InType, OutType > &)
void inplaceSort(std::vector< T > &values)
The following methods are specializations for using the Parallel::packed_range_* routines for a vecto...
static std::unique_ptr< BootstrapCalculator< InType, OutType > > build(const MooseEnum &, const libMesh::ParallelObject &, const std::vector< Real > &, unsigned int, unsigned int, StochasticTools::Calculator< InType, OutType > &)