Skip to content

Commit

Permalink
WIP: Converting BootstrapCalculator into a Calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
aeslaughter committed Jan 20, 2021
1 parent 57d204e commit 2dc175b
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 189 deletions.
31 changes: 18 additions & 13 deletions modules/stochastic_tools/include/reporters/StatisticsReporter.h
Expand Up @@ -15,7 +15,6 @@

#include "nlohmann/json.h"


/**
* ReporterContext that utilizes a Calculator object to compute its value and confidence levels
*/
Expand All @@ -27,8 +26,18 @@ class ReporterStatisticsContext : public ReporterContext<OutType>
ReporterState<OutType> & state,
const InType & data,
const ReporterProducerEnum & mode,
const MooseEnumItem & stat,
const StochasticTools::BootstrapCalculator * ci_calc);
const MooseEnumItem & stat);
/*
ReporterStatisticsContext(const libMesh::ParallelObject & other,
ReporterState<OutType> & state,
const InType & data,
const ReporterProducerEnum & mode,
const MooseEnumItem & stat,
const MooseEnumItem & ci_method,
const std::vector<Real> & ci_levels,
unsigned int replicates,
unsigned int seed);
*/
virtual void finalize() override;
virtual void store(nlohmann::json & json) const override;

Expand All @@ -43,7 +52,7 @@ class ReporterStatisticsContext : public ReporterContext<OutType>
std::unique_ptr<const StochasticTools::Calculator<InType, OutType>> _calc_ptr;

/// Storage for the BootstrapCalculator for the desired confidence interval calculations (optional)
const StochasticTools::BootstrapCalculator * _ci_calc_ptr;
std::unique_ptr<const StochasticTools::BootstrapCalculator<InType, OutType>> _ci_calc_ptr;

/// The results
std::vector<OutType> _ci_results;
Expand All @@ -55,13 +64,12 @@ ReporterStatisticsContext<InType, OutType>::ReporterStatisticsContext(
ReporterState<OutType> & state,
const InType & data,
const ReporterProducerEnum & mode,
const MooseEnumItem & stat,
const StochasticTools::BootstrapCalculator * ci_calc)
const MooseEnumItem & stat)
: ReporterContext<OutType>(other, state),
_data(data),
_data_mode(mode),
_calc_ptr(StochasticTools::makeCalculator<InType, OutType>(stat, other)),
_ci_calc_ptr(ci_calc)
_ci_calc_ptr(nullptr)
{
}

Expand All @@ -72,8 +80,9 @@ ReporterStatisticsContext<InType, OutType>::finalize()
this->_state.value() = _calc_ptr->compute(_data, _data_mode == REPORTER_MODE_DISTRIBUTED);
ReporterContext<OutType>::finalize();

//if (_ci_calc_ptr)
// _ci_results = _ci_calc_ptr->compute(_data, *_calc_ptr, _data_mode == REPORTER_MODE_DISTRIBUTED);
// if (_ci_calc_ptr)
// _ci_results = _ci_calc_ptr->compute(_data, *_calc_ptr, _data_mode ==
// REPORTER_MODE_DISTRIBUTED);
}

template <typename InType, typename OutType>
Expand Down Expand Up @@ -108,10 +117,6 @@ class StatisticsReporter : public GeneralReporter
virtual void initialize() final{};
virtual void finalize() final{};

protected:
/// Confidence level calculator, this is shared by all reporters that are declared.
std::unique_ptr<const StochasticTools::BootstrapCalculator> _ci_calculator = nullptr;

private:
/**
* Helper function for converting confidence levels given in (0, 0.5] into levels in (0, 1).
Expand Down
83 changes: 27 additions & 56 deletions modules/stochastic_tools/include/utils/BootstrapCalculators.h
Expand Up @@ -21,23 +21,12 @@ namespace StochasticTools
{
template <typename InType, typename OutType>
class Calculator;
class BootstrapCalculator;

/*
* Return available bootstrap statistics calculators.
*/
MooseEnum makeBootstrapCalculatorEnum();

/*
* Create const Bootstrap confidence level interface calculator for use by VectorPostprocessor
* objects.
*/
std::unique_ptr<const BootstrapCalculator> makeBootstrapCalculator(const MooseEnum &,
const libMesh::ParallelObject &,
const std::vector<Real> &,
unsigned int,
unsigned int);

/**
* Base class for computing bootstrap confidence level intervals. These classes follow the same
* design pattern as those Statistics.h.
Expand All @@ -46,45 +35,28 @@ std::unique_ptr<const BootstrapCalculator> makeBootstrapCalculator(const MooseEn
* @param replicates Number of bootstrap replicates to perform
* @param seed Seed for random number generator
*/
class BootstrapCalculator : public libMesh::ParallelObject
template <typename InType, typename OutType>
class BootstrapCalculator : public Calculator<InType, std::vector<OutType>>
{
public:
BootstrapCalculator(const libMesh::ParallelObject & other,
const std::string & method,
const std::string & name,
const std::vector<Real> & levels,
unsigned int replicates,
unsigned int seed);
virtual ~BootstrapCalculator() = default;

/**
* Compute the bootstrap confidence level intervals.
* @param data Vector of data from which statistics are to be computed
* @param calc Calculator object defining the statistic to be computed
* @param is_distributed Flag indicating if the dMuata is distributed in parallel
*/
virtual std::vector<Real> compute(const std::vector<Real> & data,
const Calculator<std::vector<Real>, Real> & calc,
const bool is_distributed) const = 0;

unsigned int seed,
const StochasticTools::Calculator<InType, OutType> & calc);
///@{
/// Return the input items (see ReporterStatisticsContext)
const std::string & name() const;
const std::vector<Real> & levels() const;
unsigned int replicates() const;
unsigned int seed() const;
///@}

protected:
// Compute Bootstrap estimates of a statistic
std::vector<Real> computeBootstrapEstimates(const std::vector<Real> &,
const Calculator<std::vector<Real>, Real> &,
const bool) const;
std::vector<OutType> computeBootstrapEstimates(const InType &, const bool) const;

// Randomly shuffle a vector of data
std::vector<Real> shuffle(const std::vector<Real> &, MooseRandom &, const bool) const;

// Calculation method
const std::string _method;
InType shuffle(const InType &, MooseRandom &, const bool) const;

// Confidence levels to compute in range (0, 1)
const std::vector<Real> _levels;
Expand All @@ -94,44 +66,43 @@ class BootstrapCalculator : public libMesh::ParallelObject

// Random seed for creating bootstrap replicates
const unsigned int _seed;

// The Calculator that computes the statistic of interest
const StochasticTools::Calculator<InType, OutType> & _calc;
};

/*
* Implement percentile method of Efron and Tibshirani (2003), Chapter 13.
*/
class Percentile : public BootstrapCalculator
template <typename InType, typename OutType>
class Percentile : public BootstrapCalculator<InType, OutType>
{
public:
Percentile(const libMesh::ParallelObject & other,
const std::vector<Real> & levels,
unsigned int replicates,
unsigned int seed);

virtual std::vector<Real> compute(const std::vector<Real> &,
const Calculator<std::vector<Real>, Real> &,
const bool) const override;
using BootstrapCalculator<InType, OutType>::BootstrapCalculator;
virtual std::vector<OutType> compute(const InType &, const bool) const override;
};

/*
* Implement BCa method of Efron and Tibshirani (2003), Chapter 14.
*/
class BiasCorrectedAccelerated : public BootstrapCalculator
template <typename InType, typename OutType>
class BiasCorrectedAccelerated : public BootstrapCalculator<InType, OutType>
{
public:
BiasCorrectedAccelerated(const libMesh::ParallelObject & other,
const std::vector<Real> & levels,
unsigned int replicates,
unsigned int seed);

virtual std::vector<Real> compute(const std::vector<Real> &,
const Calculator<std::vector<Real>, Real> &,
const bool) const override;
using BootstrapCalculator<InType, OutType>::BootstrapCalculator;
virtual std::vector<OutType> compute(const InType &, const bool) const override;

private:
// Compute the acceleration, see Efron and Tibshirani (2003), Ch. 14, Eq. 14.15, p 186.
Real acceleration(const std::vector<Real> &,
const Calculator<std::vector<Real>, Real> &,
const bool) const;
OutType acceleration(const InType &, const bool) const;
};

template <typename InType, typename OutType>
std::unique_ptr<const Calculator<InType, std::vector<OutType>>>
makeBootstrapCalculator(const MooseEnum &,
const libMesh::ParallelObject &,
const std::vector<Real> &,
unsigned int,
unsigned int,
const StochasticTools::Calculator<InType, OutType> & calc);
} // namespace
2 changes: 1 addition & 1 deletion modules/stochastic_tools/include/utils/Calculators.h
Expand Up @@ -38,7 +38,7 @@ MultiMooseEnum makeCalculatorEnum();
*
* Explicit instantiations are generated in the C file.
*/
template <typename InType = std::vector<Real>, typename OutType = Real>
template <typename InType, typename OutType>
class Calculator : public libMesh::ParallelObject
{
public:
Expand Down
Expand Up @@ -42,11 +42,18 @@ class Statistics : public GeneralVectorPostprocessor
/// Confidence levels to compute (see computeLevels)
const std::vector<Real> _ci_levels;

/// Confidence level replicates
const unsigned int _replicates;

/// Confidence level seed
const unsigned int _seed;

/// The VPP vector that will hold the statistics identifiers
VectorPostprocessorValue & _stat_type_vector;

/// Confidence level calculator
std::unique_ptr<const StochasticTools::BootstrapCalculator> _ci_calculator = nullptr;
std::unique_ptr<const StochasticTools::BootstrapCalculator<std::vector<Real>, Real>>
_ci_calculator = nullptr;

// The following vectors are sized to the number of statistics to be computed

Expand Down
14 changes: 9 additions & 5 deletions modules/stochastic_tools/src/reporters/StatisticsReporter.C
Expand Up @@ -64,6 +64,7 @@ StatisticsReporter::StatisticsReporter(const InputParameters & parameters)
// Statistics to be computed
const auto & compute_stats = getParam<MultiMooseEnum>("compute");

/*
// Bootstrap CI
std::unique_ptr<const StochasticTools::BootstrapCalculator> ci_calculator = nullptr;
const MooseEnum & ci_method = getParam<MooseEnum>("ci_method");
Expand All @@ -75,6 +76,7 @@ StatisticsReporter::StatisticsReporter(const InputParameters & parameters)
//_ci_calculator =
// StochasticTools::makeBootstrapCalculator(ci_method, *this, ci_levels, replicates, seed);
}
*/

// Stats for Reporters
if (isParamValid("reporters"))
Expand All @@ -92,7 +94,7 @@ StatisticsReporter::StatisticsReporter(const InputParameters & parameters)
const std::string s_name = r_name.getCombinedName() + "_" + item.name();
const auto & data = getReporterValueByName<std::vector<Real>>(r_name);
declareValueByName<Real, ReporterStatisticsContext<std::vector<Real>, Real>>(
s_name, REPORTER_MODE_ROOT, data, mode, item, _ci_calculator.get());
s_name, REPORTER_MODE_ROOT, data, mode, item);
}
}

Expand All @@ -103,17 +105,19 @@ StatisticsReporter::StatisticsReporter(const InputParameters & parameters)
const std::string s_name = r_name.getCombinedName() + "_" + item.name();
const auto & data = getReporterValueByName<std::vector<int>>(r_name);
declareValueByName<Real, ReporterStatisticsContext<std::vector<int>, Real>>(
s_name, REPORTER_MODE_ROOT, data, mode, item, _ci_calculator.get());
s_name, REPORTER_MODE_ROOT, data, mode, item);
}
}

else
unsupported_types.emplace_back(r_name);
}
}

if (!unsupported_types.empty())
paramError("reporters",
"The following reporter value(s) do not have a type supported by the StatisticsReporter:\n", MooseUtils::join(unsupported_types, ", "));
"The following reporter value(s) do not have a type supported by the "
"StatisticsReporter:\n",
MooseUtils::join(unsupported_types, ", "));
}

// Stats for VPP
Expand All @@ -134,7 +138,7 @@ StatisticsReporter::StatisticsReporter(const InputParameters & parameters)
{
const std::string s_name = vpp_name + "::" + vec_name + "_" + item.name();
declareValueByName<Real, ReporterStatisticsContext<std::vector<Real>, Real>>(
s_name, REPORTER_MODE_ROOT, data, mode, item, _ci_calculator.get());
s_name, REPORTER_MODE_ROOT, data, mode, item);
}
}
}
Expand Down

0 comments on commit 2dc175b

Please sign in to comment.