packages/engine/scram-node/src/uncertainty_analysis.h
Provides functionality for uncertainty analysis with Monte Carlo method.
Namespaces
| Name |
|---|
| scram |
| scram::mef |
| scram::core |
Classes
| Name | |
|---|---|
| class | scram::core::UncertaintyAnalysis <br>Uncertainty analysis and statistics for top event or gate probabilities with probability distributions of basic events. |
| class | scram::core::UncertaintyAnalyzer <br>Uncertainty analysis facility. |
Source code
cpp
/*
* Copyright (C) 2014-2018 Olzhas Rakhimov
* Copyright (C) 2023 OpenPRA ORG Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <utility>
#include <vector>
#include "analysis.h"
#include "probability_analysis.h"
#include "settings.h"
namespace scram::mef { // Decouple from the implementation dependence.
class Expression;
} // namespace scram::mef
namespace scram::core {
class UncertaintyAnalysis : public Analysis {
public:
explicit UncertaintyAnalysis(const ProbabilityAnalysis* prob_analysis);
virtual ~UncertaintyAnalysis() = default;
void Analyze() noexcept;
double mean() const { return mean_; }
double sigma() const { return sigma_; }
double error_factor() const { return error_factor_; }
const std::pair<double, double>& confidence_interval() const {
return confidence_interval_;
}
const std::vector<std::pair<double, double>>& distribution() const {
return distribution_;
}
const std::vector<double>& quantiles() const { return quantiles_; }
protected:
std::vector<std::pair<int, mef::Expression&>>
GatherDeviateExpressions(const Pdag* graph) noexcept;
void SampleExpressions(
const std::vector<std::pair<int, mef::Expression&>>& deviate_expressions,
Pdag::IndexMap<double>* p_vars) noexcept;
private:
virtual std::vector<double> Sample() noexcept = 0;
void CalculateStatistics(const std::vector<double>& samples) noexcept;
double mean_;
double sigma_;
double error_factor_;
std::pair<double, double> confidence_interval_;
std::vector<std::pair<double, double>> distribution_;
std::vector<double> quantiles_;
};
template <class Calculator>
class UncertaintyAnalyzer : public UncertaintyAnalysis {
public:
explicit UncertaintyAnalyzer(ProbabilityAnalyzer<Calculator>* prob_analyzer)
: UncertaintyAnalysis(prob_analyzer), prob_analyzer_(prob_analyzer) {}
private:
std::vector<double> Sample() noexcept override;
ProbabilityAnalyzer<Calculator>* prob_analyzer_;
};
template <class Calculator>
std::vector<double> UncertaintyAnalyzer<Calculator>::Sample() noexcept {
std::vector<std::pair<int, mef::Expression&>> deviate_expressions =
UncertaintyAnalysis::GatherDeviateExpressions(prob_analyzer_->graph());
Pdag::IndexMap<double> p_vars = prob_analyzer_->p_vars(); // Private copy!
std::vector<double> samples;
samples.reserve(Analysis::settings().num_trials());
for (int i = 0; i < Analysis::settings().num_trials(); ++i) {
UncertaintyAnalysis::SampleExpressions(deviate_expressions, &p_vars);
double result = prob_analyzer_->CalculateTotalProbability(p_vars);
assert(result >= 0 && result <= 1);
samples.push_back(result);
}
return samples;
}
} // namespace scram::coreUpdated on 2025-11-11 at 16:51:09 +0000
