Theoretica
A C++ numerical and automatic mathematical library
errorprop.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_ERRORPROP_H
7 #define THEORETICA_ERRORPROP_H
8 
9 #include "../core/core_traits.h"
10 #include "../algebra/vec.h"
11 #include "../autodiff/autodiff.h"
12 #include "../statistics/statistics.h"
13 #include "../pseudorandom/montecarlo.h"
14 #include "../pseudorandom/sampling.h"
15 
16 
17 namespace theoretica {
18 
19 
20  namespace stats {
21 
22 
28  template <
29  typename Matrix = mat<real>, typename Dataset = vec<real>,
30  enable_vector<Dataset> = true
31  >
32  inline Matrix covar_mat(const std::vector<Dataset>& v) {
33 
34  Matrix cm;
35  cm.resize(v.size(), v.size());
36 
37  for (unsigned int i = 0; i < cm.rows(); ++i)
38  for (unsigned int j = 0; j < cm.cols(); ++j)
39  cm(i, j) = stats::covariance(v[i], v[j]);
40 
41  return cm;
42  }
43 
44 
56  template<
57  unsigned int N = 0,
58  typename MultiDualFunction = autodiff::dreal_t<N>(*)(autodiff::dvec_t<N>)>
60  MultiDualFunction f,
61  const vec<real, N>& x_best, const vec<real, N>& delta_x) {
62 
63  real err_sqr = 0;
64  const multidual<N> df = f(multidual<N>::make_argument(x_best));
65 
66  for (unsigned int i = 0; i < x_best.size(); ++i)
67  err_sqr += square(df.Dual().get(i) * delta_x.get(i));
68 
69  return sqrt(err_sqr);
70  }
71 
72 
86  template <
87  unsigned int N = 0, unsigned int M = 0,
88  typename MultiDualFunction = autodiff::dreal_t<N>(*)(autodiff::dvec_t<N>)>
90  MultiDualFunction f,
91  const vec<real, N>& x_best, const mat<real, M, M>& cm) {
92 
93 
94  if(cm.rows() != x_best.size()) {
95  TH_MATH_ERROR("error_propagation", cm.rows(), INVALID_ARGUMENT);
96  return nan();
97  }
98 
99  if(cm.cols() != x_best.size()) {
100  TH_MATH_ERROR("error_propagation", cm.cols(), INVALID_ARGUMENT);
101  return nan();
102  }
103 
104  real err_sqr = 0;
105  const multidual<N> df = f(multidual<N>::make_argument(x_best));
106 
107  for (unsigned int i = 0; i < cm.rows(); ++i)
108  for (unsigned int j = 0; j < cm.cols(); ++j)
109  err_sqr += df.Dual().get(i) * df.Dual().get(j)
110  * cm(i, j);
111 
112  return sqrt(err_sqr);
113  }
114 
115 
127  template<
128  unsigned int N = 0,
129  typename MultiDualFunction = multidual<N>(*)(autodiff::dvec_t<N>),
130  typename Dataset = vec<real, N>>
132  MultiDualFunction f,
133  const std::vector<Dataset>& v) {
134 
135  vec<real, N> x_mean;
136  x_mean.resize(v.size());
137 
138  for (unsigned int i = 0; i < v.size(); ++i)
139  x_mean[i] = stats::mean(v[i]);
140 
141  return error_propagation(
142  f, x_mean, covar_mat<mat<real, N, N>, Dataset>(v));
143  }
144 
145 
164  template<typename Function>
166  Function f, std::vector<pdf_sampler>& rv, unsigned int N = 1E+6) {
167 
168  return stats::stdev(sample_mc(f, rv, N));
169  }
170  }
171 }
172 
173 
174 #endif
A generic matrix with a fixed number of rows and columns.
Definition: mat.h:132
TH_CONSTEXPR unsigned int rows() const
Get the number of rows of the matrix.
Definition: mat.h:465
TH_CONSTEXPR unsigned int cols() const
Get the number of columns of the matrix.
Definition: mat.h:471
Multidual number algebra for functions of the form .
Definition: multidual.h:26
A statically allocated N-dimensional vector with elements of the given type.
Definition: vec.h:88
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition: vec.h:412
Type get(unsigned int i) const
Getters and setters.
Definition: vec.h:322
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition: vec.h:402
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
real stdev(const histogram &h)
Compute the standard deviation of the values of a histogram.
Definition: histogram.h:320
real covariance(const Dataset1 &X, const Dataset2 &Y, unsigned int constraints=1)
Compute the covariance between two datasets with the given number of constraints.
Definition: statistics.h:262
real error_propagation(MultiDualFunction f, const vec< real, N > &x_best, const vec< real, N > &delta_x)
Automatically propagate uncertainties under quadrature on an arbitrary function given the uncertainti...
Definition: errorprop.h:59
real mean(const histogram &h)
Compute the mean of the values of a histogram.
Definition: histogram.h:296
real error_propagation_mc(Function f, std::vector< pdf_sampler > &rv, unsigned int N=1E+6)
Propagate the statistical error on a given function using the Monte Carlo method, by generating a sam...
Definition: errorprop.h:165
Matrix covar_mat(const std::vector< Dataset > &v)
Build the covariance matrix given a vector of datasets by computing the covariance between all couple...
Definition: errorprop.h:32
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
dual2 sqrt(dual2 x)
Compute the square root of a second order dual number.
Definition: dual2_functions.h:54
Vector sample_mc(Function f, std::vector< pdf_sampler > &rv, unsigned int N)
Generate a Monte Carlo sample of values of a given function of arbitrary variables following the give...
Definition: montecarlo.h:376
constexpr real E
The Euler mathematical constant (e)
Definition: constants.h:227
dual2 square(dual2 x)
Return the square of a second order dual number.
Definition: dual2_functions.h:23
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54