Belle II Software development
CovarianceMatrixUtil Struct Reference

Collection of functions related to covariance matrices. More...

#include <CovarianceMatrixUtil.h>

Static Public Member Functions

template<int N>
static CovarianceMatrix< N > identity ()
 Constructs an identity matrix.
 
template<int N>
static void transport (const JacobianMatrix< N, N > &jacobian, CovarianceMatrix< N > &cov)
 Transport the covariance matrix inplace with the given jacobian matrix.
 
template<int M, int N>
static CovarianceMatrix< M > transported (const JacobianMatrix< M, N > &jacobian, const CovarianceMatrix< N > &cov)
 Return a copy of the covariance matrix transported with the given jacobian matrix.
 
template<int N>
static void scale (const ParameterVector< N > &scales, CovarianceMatrix< N > &cov)
 Scale the covariance inplace by the given factors in each parameter.
 
template<int N>
static CovarianceMatrix< N > scale (const ParameterVector< N > &scales, const CovarianceMatrix< N > &cov)
 Return a copy of the covariance scaled by the given factors in each parameter.
 
template<int N1, int N2>
static CovarianceMatrix< N1+N2 > stackBlocks (const CovarianceMatrix< N1 > &block1, const CovarianceMatrix< N2 > &block2)
 Combines two covariance matrices by putting them in two blocks on the diagonal of a larger matrix.
 
template<class ACovarianceMatrix , int I = 0, int N = 0>
static ACovarianceMatrix getSub (const CovarianceMatrix< N > &cov)
 Gets a subcovariance from a covariance matrix.
 
template<int N>
static CovarianceMatrix< N > fromPrecision (const PrecisionMatrix< N > &prec)
 Convert precision matrix to covariance matrix - allows for reduced rank.
 
template<int N>
static PrecisionMatrix< N > toPrecision (const CovarianceMatrix< N > &cov)
 Convert covariance matrix to precision matrix - allows for reduced rank.
 
template<int N>
static CovarianceMatrix< N > fromFullPrecision (const PrecisionMatrix< N > &prec)
 Convert covariance matrix to precision matrix - assumes full rank.
 
template<int N>
static PrecisionMatrix< N > fullToPrecision (const CovarianceMatrix< N > &cov)
 Convert precision matrix to covariance matrix - assumes full rank.
 
template<int N>
static double average (const ParameterVector< N > &par1, const CovarianceMatrix< N > &cov1, const ParameterVector< N > &par2, const CovarianceMatrix< N > &cov2, ParameterVector< N > &par, CovarianceMatrix< N > &cov)
 Averages two parameter vectors taking into account their respective covariances.
 
template<int M, int N1>
static double average (const ParameterVector< N1 > &par1, const CovarianceMatrix< N1 > &cov1, const JacobianMatrix< N1, M > &ambiguity1, const ParameterVector< M > &par2, const CovarianceMatrix< M > &cov2, ParameterVector< M > &par, CovarianceMatrix< M > &cov)
 Averages two parameter vectors, one being from a projected space, taking into account their respective covariances and the ambiguity of the projection to the actual state.
 
template<int M, int N1, int N2>
static double average (const ParameterVector< N1 > &par1, const CovarianceMatrix< N1 > &cov1, const JacobianMatrix< N1, M > &ambiguity1, const ParameterVector< N2 > &par2, const CovarianceMatrix< N2 > &cov2, const JacobianMatrix< N2, M > &ambiguity2, ParameterVector< M > &par, CovarianceMatrix< M > &cov)
 Averages two parameter vectors from a projected space taking into account their respective covariances and ambiguity matrices.
 
template<int M, int N1>
static double kalmanUpdate (const ParameterVector< N1 > &par1, const CovarianceMatrix< N1 > &cov1, const JacobianMatrix< N1, M > &ambiguity1, ParameterVector< M > &par2, CovarianceMatrix< M > &cov2)
 Updates a parameter and its covariance by integrating information from parameter vector in a projected space and its covariances.
 

Detailed Description

Collection of functions related to covariance matrices.

Definition at line 36 of file CovarianceMatrixUtil.h.

Member Function Documentation

◆ average() [1/3]

static double average ( const ParameterVector< N > &  par1,
const CovarianceMatrix< N > &  cov1,
const ParameterVector< N > &  par2,
const CovarianceMatrix< N > &  cov2,
ParameterVector< N > &  par,
CovarianceMatrix< N > &  cov 
)
inlinestatic

Averages two parameter vectors taking into account their respective covariances.

Parameters
par1First parameters
cov1Covariance matrix to the first parameters
par2Second parameters
cov2Covariance matrix to the second parameters
[out]parAveraged parameters
[out]covAveraged covariance matrix
Returns
Chi square deviation of orignal parameters to the average

Definition at line 146 of file CovarianceMatrixUtil.h.

152 {
153 PrecisionMatrix<N> precision1 = toPrecision(cov1);
154 PrecisionMatrix<N> precision2 = toPrecision(cov2);
155 PrecisionMatrix<N> precision;
156 double chi2 =
157 PrecisionMatrixUtil::average(par1, precision1, par2, precision2, par, precision);
158 cov = fromPrecision(precision);
159 return chi2;
160 }
static CovarianceMatrix< N > fromPrecision(const PrecisionMatrix< N > &prec)
Convert precision matrix to covariance matrix - allows for reduced rank.
static PrecisionMatrix< N > toPrecision(const CovarianceMatrix< N > &cov)
Convert covariance matrix to precision matrix - allows for reduced rank.
static double average(const ParameterVector< N > &parameter1, const PrecisionMatrix< N > &precision1, const ParameterVector< N > &parameter2, const PrecisionMatrix< N > &precision2, ParameterVector< N > &parameter, PrecisionMatrix< N > &precision)
Averages two parameter vectors taking into account their respective precision.

◆ average() [2/3]

static double average ( const ParameterVector< N1 > &  par1,
const CovarianceMatrix< N1 > &  cov1,
const JacobianMatrix< N1, M > &  ambiguity1,
const ParameterVector< M > &  par2,
const CovarianceMatrix< M > &  cov2,
ParameterVector< M > &  par,
CovarianceMatrix< M > &  cov 
)
inlinestatic

Averages two parameter vectors, one being from a projected space, taking into account their respective covariances and the ambiguity of the projection to the actual state.

Parameters
par1First parameters
cov1Covariance matrix to the first parameters
ambiguity1Projection ambiguity of the first parameters
par2Second parameters
cov2Covariance matrix to the second parameters
[out]parAveraged parameters
[out]covAveraged covariance matrix
Returns
Chi square deviation of orignal parameters to the average

Definition at line 177 of file CovarianceMatrixUtil.h.

184 {
185 /* Naive implementation */
186 JacobianMatrix<M, M> ambiguity2 = identity<M>();
187 return average(par1, cov1, ambiguity1, par2, cov2, ambiguity2, par, cov);
188
189 /* Kalman implementation - may be faster */
190 // par = par2;
191 // cov = cov2;
192 // return kalmanUpdate(par1, cov1, ambiguity1, par, cov);
193 }
static double average(const ParameterVector< N > &par1, const CovarianceMatrix< N > &cov1, const ParameterVector< N > &par2, const CovarianceMatrix< N > &cov2, ParameterVector< N > &par, CovarianceMatrix< N > &cov)
Averages two parameter vectors taking into account their respective covariances.

◆ average() [3/3]

static double average ( const ParameterVector< N1 > &  par1,
const CovarianceMatrix< N1 > &  cov1,
const JacobianMatrix< N1, M > &  ambiguity1,
const ParameterVector< N2 > &  par2,
const CovarianceMatrix< N2 > &  cov2,
const JacobianMatrix< N2, M > &  ambiguity2,
ParameterVector< M > &  par,
CovarianceMatrix< M > &  cov 
)
inlinestatic

Averages two parameter vectors from a projected space taking into account their respective covariances and ambiguity matrices.

Parameters
par1First parameters
cov1Covariance matrix to the first parameters
ambiguity1Projection ambiguity of the first parameters
par2Second parameters
cov2Covariance matrix to the second parameters
ambiguity2Projection ambiguity of the second parameters
[out]parAveraged parameters
[out]covAveraged covariance matrix
Returns
Chi square deviation of original parameters to the average

Definition at line 210 of file CovarianceMatrixUtil.h.

218 {
219 PrecisionMatrix<N1> precision1 = toPrecision(cov1);
220 PrecisionMatrix<N2> precision2 = toPrecision(cov2);
221 PrecisionMatrix<M> precision;
222 double chi2 = PrecisionMatrixUtil::average(par1,
223 precision1,
224 ambiguity1,
225 par2,
226 precision2,
227 ambiguity2,
228 par,
229 precision);
230 cov = fromPrecision(precision);
231 return chi2;
232 }

◆ fromFullPrecision()

static CovarianceMatrix< N > fromFullPrecision ( const PrecisionMatrix< N > &  prec)
inlinestatic

Convert covariance matrix to precision matrix - assumes full rank.

Definition at line 118 of file CovarianceMatrixUtil.h.

119 {
120 CovarianceMatrix<N> cov;
121 mapToEigen(cov) = mapToEigen(prec).inverse();
122 return cov;
123 }

◆ fromPrecision()

static CovarianceMatrix< N > fromPrecision ( const PrecisionMatrix< N > &  prec)
inlinestatic

Convert precision matrix to covariance matrix - allows for reduced rank.

Definition at line 100 of file CovarianceMatrixUtil.h.

101 {
102 CovarianceMatrix<N> cov;
103 mapToEigen(cov) = mapToEigen(prec).colPivHouseholderQr().inverse();
104 return cov;
105 }

◆ fullToPrecision()

static PrecisionMatrix< N > fullToPrecision ( const CovarianceMatrix< N > &  cov)
inlinestatic

Convert precision matrix to covariance matrix - assumes full rank.

Definition at line 127 of file CovarianceMatrixUtil.h.

128 {
129 PrecisionMatrix<N> prec;
130 mapToEigen(prec) = mapToEigen(cov).inverse();
131 return prec;
132 }

◆ getSub()

static ACovarianceMatrix getSub ( const CovarianceMatrix< N > &  cov)
inlinestatic

Gets a subcovariance from a covariance matrix.

Definition at line 91 of file CovarianceMatrixUtil.h.

92 {
93 constexpr const int M =
94 std::remove_reference_t<decltype(mapToEigen(ACovarianceMatrix()))>::RowsAtCompileTime;
95 return cov.template block<M, M>(I, I);
96 }

◆ identity()

static CovarianceMatrix< N > identity ( )
inlinestatic

Constructs an identity matrix.

Definition at line 40 of file CovarianceMatrixUtil.h.

41 {
43 }
static PlainMatrix< T, M, N > Identity()
Construct an identity matrix.
Definition: PlainMatrix.h:74

◆ kalmanUpdate()

static double kalmanUpdate ( const ParameterVector< N1 > &  par1,
const CovarianceMatrix< N1 > &  cov1,
const JacobianMatrix< N1, M > &  ambiguity1,
ParameterVector< M > &  par2,
CovarianceMatrix< M > &  cov2 
)
inlinestatic

Updates a parameter and its covariance by integrating information from parameter vector in a projected space and its covariances.

The update is expressed in terms of Kalman's equations.

This function updates the values of the second parameters in place.

Parameters
par1First parameters
cov1Covariance matrix to the first parameters
ambiguity1Projection ambiguity of the first parameters
[in,out]par2Second parameters - updated inplace
[in,out]cov2Second covariance matrix - updated inplace
Returns
Chi square deviation of orignal parameters to the average

Definition at line 249 of file CovarianceMatrixUtil.h.

254 {
255 /* Kalman update - smart version */
256 const auto& ePar1 = mapToEigen(par1);
257 const auto& eCov1 = mapToEigen(cov1);
258 const auto& eAmbiguity1 = mapToEigen(ambiguity1);
259
260 auto&& ePar2 = mapToEigen(par2);
261 auto&& eCov2 = mapToEigen(cov2);
262
263 // Apriori residual
264 auto residual = ePar1 - eAmbiguity1 * ePar2;
265 auto residualCov = eCov1 + eAmbiguity1 * eCov2 * eAmbiguity1.transpose();
266 auto residualPrecision = residualCov.inverse();
267
268 // Chi2 - calculate on the apriori residuals, which is the same value as for the posterior.
269 Eigen::Matrix<double, 1, 1> chi2 = residual.transpose() * residualPrecision * residual;
270
271 // Update parameters
272 auto kalmanGain = eCov2 * eAmbiguity1.transpose() * residualPrecision;
273 eCov2 -= kalmanGain * eAmbiguity1 * eCov2;
274 ePar2 += kalmanGain * residual;
275
276 return chi2[0];
277
278 // Posterior residual - here for reference, should be compiled out.
279 auto postResidual = ePar1 - eAmbiguity1 * ePar2;
280 auto postResidualCov = eCov1 - eAmbiguity1 * eCov2 * eAmbiguity1.transpose();
281 auto postResidualPrecision = postResidualCov.inverse();
282 Eigen::Matrix<double, 1, 1> postChi2 =
283 postResidual.transpose() * postResidualPrecision * postResidual;
284 (void)postChi2;
285 assert(std::fabs(postChi2[0] - chi2[0]) < 1e-8);
286 }

◆ scale() [1/2]

static CovarianceMatrix< N > scale ( const ParameterVector< N > &  scales,
const CovarianceMatrix< N > &  cov 
)
inlinestatic

Return a copy of the covariance scaled by the given factors in each parameter.

Definition at line 71 of file CovarianceMatrixUtil.h.

73 {
74 return transported(JacobianMatrixUtil::scale(scales), cov);
75 }
static CovarianceMatrix< M > transported(const JacobianMatrix< M, N > &jacobian, const CovarianceMatrix< N > &cov)
Return a copy of the covariance matrix transported with the given jacobian matrix.
static JacobianMatrix< N > scale(const ParameterVector< N > &scales)
Calculates the jacobian matrix for a scaling in each parameter.

◆ scale() [2/2]

static void scale ( const ParameterVector< N > &  scales,
CovarianceMatrix< N > &  cov 
)
inlinestatic

Scale the covariance inplace by the given factors in each parameter.

Definition at line 64 of file CovarianceMatrixUtil.h.

65 {
66 return transport(JacobianMatrixUtil::scale(scales), cov);
67 }
static void transport(const JacobianMatrix< N, N > &jacobian, CovarianceMatrix< N > &cov)
Transport the covariance matrix inplace with the given jacobian matrix.

◆ stackBlocks()

static CovarianceMatrix< N1+N2 > stackBlocks ( const CovarianceMatrix< N1 > &  block1,
const CovarianceMatrix< N2 > &  block2 
)
inlinestatic

Combines two covariance matrices by putting them in two blocks on the diagonal of a larger matrix.

Definition at line 80 of file CovarianceMatrixUtil.h.

82 {
83 CovarianceMatrix < N1 + N2 > result;
84 mapToEigen(result) << mapToEigen(block1), Eigen::Matrix<double, N1, N2>::Zero(),
85 Eigen::Matrix<double, N2, N1>::Zero(), mapToEigen(block2);
86 return result;
87 }

◆ toPrecision()

static PrecisionMatrix< N > toPrecision ( const CovarianceMatrix< N > &  cov)
inlinestatic

Convert covariance matrix to precision matrix - allows for reduced rank.

Definition at line 109 of file CovarianceMatrixUtil.h.

110 {
111 PrecisionMatrix<N> prec;
112 mapToEigen(prec) = mapToEigen(cov).colPivHouseholderQr().inverse();
113 return prec;
114 }

◆ transport()

static void transport ( const JacobianMatrix< N, N > &  jacobian,
CovarianceMatrix< N > &  cov 
)
inlinestatic

Transport the covariance matrix inplace with the given jacobian matrix.

Definition at line 47 of file CovarianceMatrixUtil.h.

48 {
49 mapToEigen(cov) = mapToEigen(jacobian) * mapToEigen(cov) * mapToEigen(jacobian).transpose();
50 }

◆ transported()

static CovarianceMatrix< M > transported ( const JacobianMatrix< M, N > &  jacobian,
const CovarianceMatrix< N > &  cov 
)
inlinestatic

Return a copy of the covariance matrix transported with the given jacobian matrix.

Definition at line 54 of file CovarianceMatrixUtil.h.

56 {
57 CovarianceMatrix<M> result;
58 mapToEigen(result) = mapToEigen(jacobian) * mapToEigen(cov) * mapToEigen(jacobian).transpose();
59 return result;
60 }

The documentation for this struct was generated from the following file: