CMP++: Uncertainty Quantification & Bayesian Calibration
Loading...
Searching...
No Matches
sobol.h
Go to the documentation of this file.
1
8#ifndef CMP_SOBOL_H
9#define CMP_SOBOL_H
10
11#include <cmp_defines.h>
12#include <statistics.h>
13#include <grid.h>
14#include <stdexcept>
15#include <utility>
16#include <memory>
17
22namespace cmp::sobol {
23
32 Eigen::VectorXd firstOrder;
33
35 Eigen::VectorXd totalOrder;
36
38 Eigen::VectorXd secondOrder;
39
46 double getFirstOrder(size_t i) const {
47 if(i >= firstOrder.size()) throw std::out_of_range("Index out of range");
48 return firstOrder(i);
49 }
50
57 double getTotalOrder(size_t i) const {
58 if(i >= totalOrder.size()) throw std::out_of_range("Index out of range");
59 return totalOrder(i);
60 }
61
72 double getSecondOrder(size_t i, size_t j) const {
73 if(secondOrder.size() == 0) throw std::logic_error("Second order Sobol indices were not computed.");
74 if(i >= firstOrder.size() || j >= firstOrder.size()) throw std::out_of_range("Index out of range");
75 if(i == j) throw std::invalid_argument("Indices must be different for second order Sobol index.");
76 if(i > j) std::swap(i, j);
77 size_t index = i * (2 * firstOrder.size() - i - 1) / 2 + (j - i - 1);
78 return secondOrder(index);
79 }
80};
81
117 private:
119 size_t nObs_ = 0;
120
122 size_t nTotalObs_ = 0;
123
125 size_t dim_ = 0;
126
128 bool secondOrder_ = false;
129
130 public:
132 SobolSaltelli() = default;
133
140 SobolSaltelli(size_t nObs, size_t dim, bool secondOrder = false)
141 : nObs_(nObs), dim_(dim), secondOrder_(secondOrder) {
142 nTotalObs_ = (2 + dim_ + (secondOrder_ ? dim_ * (dim_ - 1) / 2 : 0)) * nObs_;
143 }
144
149 size_t nTotalObs() const {
150 return nTotalObs_;
151 }
152
157 size_t dim() const {
158 return dim_;
159 }
160
165 size_t nObs() const {
166 return nObs_;
167 }
168
176 Eigen::MatrixXd evaluationGrid(size_t nObs, std::shared_ptr<cmp::grid::Grid> gridType, bool secondOrder = false);
177
189 Eigen::VectorXd sliceSaltelliOutput(const Eigen::VectorXd &Y, const Eigen::VectorXs &idx, size_t nObs, size_t dim, bool secondOrder);
190
196 SobolResults compute(const Eigen::VectorXd &Y);
197
208 std::pair<SobolResults, SobolResults> computeWithBootstrap(const Eigen::VectorXd &Y, size_t nBootstrap, size_t randomSeed = 42);
209
210};
211}
214#endif // CMP_SOBOL_H
Performs Global Sensitivity Analysis (GSA) using the Saltelli formulation of Sobol indices.
Definition sobol.h:116
Eigen::VectorXd sliceSaltelliOutput(const Eigen::VectorXd &Y, const Eigen::VectorXs &idx, size_t nObs, size_t dim, bool secondOrder)
Slices the output vector Y from the Sobol evaluation grid to keep only valid or specified indices.
Definition sobol.cpp:42
SobolSaltelli(size_t nObs, size_t dim, bool secondOrder=false)
Constructs a Sobol-Saltelli analyzer with the given configuration.
Definition sobol.h:140
size_t nTotalObs_
The total number of evaluations required across all matrices.
Definition sobol.h:122
size_t nTotalObs() const
Gets the total number of evaluations required based on , , and analysis order.
Definition sobol.h:149
std::pair< SobolResults, SobolResults > computeWithBootstrap(const Eigen::VectorXd &Y, size_t nBootstrap, size_t randomSeed=42)
Computes Sobol indices with bootstrap resampling to estimate statistical confidence intervals.
Definition sobol.cpp:121
size_t dim_
The dimensionality of the input parameter space.
Definition sobol.h:125
SobolSaltelli()=default
Default constructor.
Eigen::MatrixXd evaluationGrid(size_t nObs, std::shared_ptr< cmp::grid::Grid > gridType, bool secondOrder=false)
Constructs the Sobol evaluation grid using the Saltelli sampling scheme.
Definition sobol.cpp:3
bool secondOrder_
Flag indicating whether second-order indices should be computed.
Definition sobol.h:128
size_t dim() const
Gets the number of dimension parameters .
Definition sobol.h:157
size_t nObs() const
Gets the number of base samples .
Definition sobol.h:165
size_t nObs_
The number of base samples .
Definition sobol.h:119
SobolResults compute(const Eigen::VectorXd &Y)
Computes Sobol indices from the output vector Y obtained by evaluating the Saltelli grid.
Definition sobol.cpp:67
Matrix< size_t, Eigen::Dynamic, 1 > VectorXs
Definition cmp_defines.h:20
Definition sobol.h:22
Header file for statistical functions and classes.
Struct to hold calculated Sobol sensitivity indices.
Definition sobol.h:30
Eigen::VectorXd totalOrder
Vector storing the total-order Sobol indices.
Definition sobol.h:35
double getTotalOrder(size_t i) const
Gets the total-order Sobol index for input parameter .
Definition sobol.h:57
double getSecondOrder(size_t i, size_t j) const
Gets the second-order Sobol index for interaction between parameters and .
Definition sobol.h:72
Eigen::VectorXd firstOrder
Vector storing the first-order (main effect) Sobol indices.
Definition sobol.h:32
Eigen::VectorXd secondOrder
Vector storing the second-order (interaction effect) Sobol indices (if computed).
Definition sobol.h:38
double getFirstOrder(size_t i) const
Gets the first-order Sobol index for input parameter .
Definition sobol.h:46