MaCh3  2.4.2
Reference Guide
Public Member Functions | Private Member Functions | Private Attributes | List of all members
PCAHandler Class Reference

Class responsible for handling Principal Component Analysis (PCA) of covariance matrix. More...

#include <Parameters/PCAHandler.h>

Collaboration diagram for PCAHandler:
[legend]

Public Member Functions

 PCAHandler ()
 Constructor. More...
 
virtual ~PCAHandler ()
 Destructor. More...
 
void Print () const
 KS: Print info about PCA parameters. More...
 
int GetNumberPCAedParameters () const
 Retrieve number of parameters in PCA base. More...
 
void SetupPointers (std::vector< double > *fCurr_Val, std::vector< double > *fProp_Val)
 KS: Setup pointers to current and proposed parameter value which we need to convert them to PCA base each step. More...
 
void ConstructPCA (TMatrixDSym *CovMatrix, const int firstPCAd, const int lastPCAd, const double eigen_thresh, const int _fNumPar)
 CW: Calculate eigen values, prepare transition matrices and remove param based on defined threshold. More...
 
void TransferToPCA ()
 Transfer param values from normal base to PCA base. More...
 
void TransferToParam ()
 Transfer param values from PCA base to normal base. More...
 
void ThrowParameters (const std::vector< std::unique_ptr< TRandom3 >> &random_number, double **throwMatrixCholDecomp, double *randParams, double *corr_throw, const std::vector< double > &fPreFitValue, const std::vector< double > &fLowBound, const std::vector< double > &fUpBound, int _fNumPar)
 Throw the parameters according to the covariance matrix. This shouldn't be used in MCMC code ase it can break Detailed Balance;. More...
 
void AcceptStep () _noexcept_
 Accepted this step. More...
 
void CorrelateSteps (const std::vector< double > &IndivStepScale, const double GlobalStepScale, const double *_restrict_ randParams, const double *_restrict_ corr_throw) _noexcept_
 Use Cholesky throw matrix for better step proposal. More...
 
void SetInitialParameters ()
 KS: Transfer the starting parameters to the PCA basis, you don't want to start with zero.. More...
 
void SetBranches (TTree &tree, const bool SaveProposal, const std::vector< std::string > &Names)
 set branches for output file More...
 
void ToggleFixAllParameters (const std::vector< std::string > &Names)
 fix parameters at prior values More...
 
void ToggleFixParameter (const int i, const std::vector< std::string > &Names)
 fix parameters at prior values More...
 
void SetParametersPCA (const std::vector< double > &pars)
 Set values for PCA parameters in PCA base. More...
 
bool IsParameterFixedPCA (const int i) const
 Is parameter fixed in PCA base or not. More...
 
const TMatrixD GetEigenVectors () const
 Get eigen vectors of covariance matrix, only works with PCA. More...
 
void SetParPropPCA (const int i, const double value)
 Set proposed value for parameter in PCA base. More...
 
void SetParCurrPCA (const int i, const double value)
 Set current value for parameter in PCA base. More...
 
double GetParPropPCA (const int i) const
 Get current parameter value using PCA. More...
 
double GetPreFitValuePCA (const int i) const
 Get current parameter value using PCA. More...
 
double GetParCurrPCA (const int i) const
 Get current parameter value using PCA. More...
 
const TMatrixD GetTransferMatrix () const
 Get transfer matrix allowing to go from PCA base to normal base. More...
 
const TVectorD GetEigenValues () const
 Get eigen values for all parameters, if you want for decomposed only parameters use GetEigenValuesMaster. More...
 
const std::vector< double > GetEigenValuesMaster () const
 Get eigen value of only decomposed parameters, if you want for all parameters use GetEigenValues. More...
 
bool IsParameterDecomposed (const int i) const
 Check if parameter in PCA base is decomposed or not. More...
 

Private Member Functions

void SanitisePCA (TMatrixDSym *CovMatrix)
 KS: Make sure decomposed matrix isn't correlated with undecomposed. More...
 

Private Attributes

std::vector< double > _fPreFitValuePCA
 Prefit value for PCA params. More...
 
TVectorD _fParPropPCA
 CW: Current parameter value in PCA base. More...
 
TVectorD _fParCurrPCA
 CW: Proposed parameter value in PCA base. More...
 
std::vector< double > _fErrorPCA
 Tells if parameter is fixed in PCA base or not. More...
 
std::vector< int > isDecomposedPCA
 If param is decomposed this will return -1, if not this will return enumerator to param in normal base. This way we can map stuff like step scale etc between normal base and undecomposed param in eigen base. More...
 
int NumParPCA
 Number of parameters in PCA base. More...
 
TMatrixD TransferMat
 Matrix used to converting from PCA base to normal base. More...
 
TMatrixD TransferMatT
 Matrix used to converting from normal base to PCA base. More...
 
TVectorD eigen_values
 Eigen value only of particles which are being decomposed. More...
 
TMatrixD eigen_vectors
 Eigen vectors only of params which are being decomposed. More...
 
std::vector< double > eigen_values_master
 Eigen values which have dimension equal to _fNumParPCA, and can be used in CorrelateSteps. More...
 
int nKeptPCApars
 Total number that remained after applying PCA Threshold. More...
 
int FirstPCAdpar
 Index of the first param that is being decomposed. More...
 
int LastPCAdpar
 Index of the last param that is being decomposed. More...
 
double eigen_threshold
 CW: Threshold based on which we remove parameters in eigen base. More...
 
std::vector< double > * _pCurrVal
 Pointer to current value of the parameter. More...
 
std::vector< double > * _pPropVal
 Pointer to proposed value of the parameter. More...
 

Detailed Description

Class responsible for handling Principal Component Analysis (PCA) of covariance matrix.

Author
Clarence Wret

Introduction

This class applies Principal Component Analysis (PCA) to covariance matrices using eigen-decomposition. The main benefit is that PCA rotates the parameter space into a basis where parameters are uncorrelated. In practice, this helps MCMC fits move more efficiently, since correlated directions in the original space often slow down convergence.

Dimensionality Reduction

PCA makes it possible to drop directions in parameter space with very small eigenvalues. These correspond to combinations of parameters that are poorly constrained or dominated by statistical noise. By applying a threshold (e.g. discarding eigenvalues smaller than 1e-4 of the largest one), the effective number of parameters can be reduced.

This is particularly useful in large MCMC fits, where many parameters may contribute little information. Removing them reduces dimensionality while keeping the dominant structure of the parameter space intact, often leading to faster and more stable fits.

Partial Decomposition

PCA does not need to be applied to the full covariance matrix. This class supports eigen-decomposition of only a submatrix. This is useful when only a subset of parameters is strongly correlated and benefits from PCA, while the rest remain in the original parameter basis.

Definition at line 58 of file PCAHandler.h.

Constructor & Destructor Documentation

◆ PCAHandler()

PCAHandler::PCAHandler ( )

Constructor.

Definition at line 5 of file PCAHandler.cpp.

5  {
6 // ********************************************
7  _pCurrVal = nullptr;
8  _pPropVal = nullptr;
9 }
std::vector< double > * _pCurrVal
Pointer to current value of the parameter.
Definition: PCAHandler.h:260
std::vector< double > * _pPropVal
Pointer to proposed value of the parameter.
Definition: PCAHandler.h:262

◆ ~PCAHandler()

PCAHandler::~PCAHandler ( )
virtual

Destructor.

Definition at line 12 of file PCAHandler.cpp.

12  {
13 // ********************************************
14 }

Member Function Documentation

◆ AcceptStep()

void PCAHandler::AcceptStep ( )

Accepted this step.

Definition at line 179 of file PCAHandler.cpp.

179  {
180 // ********************************************
181  // Update the book-keeping for the output
182  #ifdef MULTITHREAD
183  #pragma omp parallel for
184  #endif
185  for (int i = 0; i < NumParPCA; ++i) {
186  _fParCurrPCA(i) = _fParPropPCA(i);
187  }
188  // Then update the parameter basis
189  TransferToParam();
190 }
TVectorD _fParPropPCA
CW: Current parameter value in PCA base.
Definition: PCAHandler.h:229
int NumParPCA
Number of parameters in PCA base.
Definition: PCAHandler.h:237
void TransferToParam()
Transfer param values from PCA base to normal base.
Definition: PCAHandler.cpp:255
TVectorD _fParCurrPCA
CW: Proposed parameter value in PCA base.
Definition: PCAHandler.h:231

◆ ConstructPCA()

void PCAHandler::ConstructPCA ( TMatrixDSym *  CovMatrix,
const int  firstPCAd,
const int  lastPCAd,
const double  eigen_thresh,
const int  _fNumPar 
)

CW: Calculate eigen values, prepare transition matrices and remove param based on defined threshold.

Parameters
CovMatrixSymmetric covariance matrix used for eigen decomposition.
firstPCAdIndex of the first PCA component to include.
lastPCAdIndex of the last PCA component to include.
eigen_threshThreshold for eigenvalues below which parameters are discarded.
_fNumParTotal number of parameters in the original (non-PCA) basis.

Definition at line 25 of file PCAHandler.cpp.

26  {
27 // ********************************************
28  FirstPCAdpar = firstPCAd;
29  LastPCAdpar = lastPCAd;
30  eigen_threshold = eigen_thresh;
31 
32  // Check that covariance matrix exists
33  if (CovMatrix == nullptr) {
34  MACH3LOG_ERROR("Covariance matrix for has not yet been set");
35  MACH3LOG_ERROR("Can not construct PCA until it is set");
36  throw MaCh3Exception(__FILE__ , __LINE__ );
37  }
38 
39  if(FirstPCAdpar > CovMatrix->GetNrows()-1 || LastPCAdpar>CovMatrix->GetNrows()-1) {
40  MACH3LOG_ERROR("FirstPCAdpar and LastPCAdpar are higher than the number of parameters");
41  MACH3LOG_ERROR("first: {} last: {}, params: {}", FirstPCAdpar, LastPCAdpar, CovMatrix->GetNrows()-1);
42  throw MaCh3Exception(__FILE__ , __LINE__ );
43  }
44  if(FirstPCAdpar < 0 || LastPCAdpar < 0){
45  MACH3LOG_ERROR("FirstPCAdpar and LastPCAdpar are less than 0 but not default -999");
46  MACH3LOG_ERROR("first: {} last: {}", FirstPCAdpar, LastPCAdpar);
47  throw MaCh3Exception(__FILE__ , __LINE__ );
48  }
49  MACH3LOG_INFO("PCAing parameters {} through {} inclusive", FirstPCAdpar, LastPCAdpar);
50  int NumUnPCAdPars = CovMatrix->GetNrows()-(LastPCAdpar-FirstPCAdpar+1);
51 
52  // KS: Make sure we are not doing anything silly with PCA
53  SanitisePCA(CovMatrix);
54 
55  TMatrixDSym submat(CovMatrix->GetSub(FirstPCAdpar,LastPCAdpar,FirstPCAdpar,LastPCAdpar));
56 
57  //CW: Calculate how many eigen values this threshold corresponds to
58  TMatrixDSymEigen eigen(submat);
59  eigen_values.ResizeTo(eigen.GetEigenValues());
60  eigen_vectors.ResizeTo(eigen.GetEigenVectors());
61  eigen_values = eigen.GetEigenValues();
62  eigen_vectors = eigen.GetEigenVectors();
63  double sum = 0;
64  // Loop over eigen values and sum them up
65  for (int i = 0; i < eigen_values.GetNrows(); ++i) {
66  sum += eigen_values(i);
67  }
68  nKeptPCApars = eigen_values.GetNrows();
69  //CW: Now go through again and see how many eigen values correspond to threshold
70  for (int i = 0; i < eigen_values.GetNrows(); ++i) {
71  // Get the relative size of the eigen value
72  double sig = eigen_values(i)/sum;
73  // Check against the threshold
74  if (sig < eigen_threshold) {
75  nKeptPCApars = i;
76  break;
77  }
78  }
79  NumParPCA = NumUnPCAdPars+nKeptPCApars;
80  MACH3LOG_INFO("Threshold of {} on eigen values relative sum of eigen value ({}) generates {} eigen vectors, plus we have {} unpcad pars, for a total of {}", eigen_threshold, sum, nKeptPCApars, NumUnPCAdPars, NumParPCA);
81  //DB Create array of correct size so eigen_values can be used in CorrelateSteps
82  eigen_values_master = std::vector<double>(NumParPCA, 1.0);
84 
85  // Now construct the transfer matrices
86  //These matrices will be as big as number of unPCAd pars plus number of eigenvalues kept
87  TransferMat.ResizeTo(CovMatrix->GetNrows(), NumParPCA);
88  TransferMatT.ResizeTo(CovMatrix->GetNrows(), NumParPCA);
89 
90  // Get a subset of the eigen vector matrix
91  TMatrixD temp(eigen_vectors.GetSub(0, eigen_vectors.GetNrows()-1, 0, nKeptPCApars-1));
92 
93  //Make transfer matrix which is two blocks of identity with a block of the PCA transfer matrix in between
94  TMatrixD temp2;
95  temp2.ResizeTo(CovMatrix->GetNrows(), NumParPCA);
96 
97  //First set the whole thing to 0
98  temp2.Zero();
99 
100  //Set the first identity block for non-PCAed params before PCA block, before PCA XRows == YRows
101  for(int iRow = 0; iRow < FirstPCAdpar; iRow++) {
102  temp2(iRow, iRow) = 1.0;
103  }
104 
105  //Set the transfer matrix block for the PCAd pars
106  temp2.SetSub(FirstPCAdpar,FirstPCAdpar,temp);
107 
108  //Set the second identity block starting after PCA block, remember XRows != YRows.
109  // XRows -> normal base, YRows, PCA base
110  for(int iRow = 0;iRow < (CovMatrix->GetNrows()-1)-LastPCAdpar; iRow++) {
111  const int OrigRow = LastPCAdpar + 1 + iRow;
112  const int PCARow = FirstPCAdpar + nKeptPCApars + iRow;
113  temp2(OrigRow, PCARow) = 1.;
114  }
115 
116  TransferMat = temp2;
117  // Copy the contents
119  // And then transpose
120  TransferMatT.T();
121 
122  // Make the PCA parameter arrays
123  _fParCurrPCA.ResizeTo(NumParPCA);
124  _fParPropPCA.ResizeTo(NumParPCA);
125  _fPreFitValuePCA.resize(NumParPCA);
126 
127  //KS: make easy map so we could easily find un-decomposed parameters
128  _fErrorPCA.assign(NumParPCA, 1);
129  isDecomposedPCA.assign(NumParPCA, -1);
130  // First non PCA-ed block, since this is before PCA-ed block we don't need any mapping
131  for (int i = 0; i < FirstPCAdpar; ++i) {
132  isDecomposedPCA[i] = i;
133  }
134 
135  // Second non-PCA-ed block, keep in mind this is in PCA base so we cannot use LastPCAdpar
136  // we must shift them back into the original parameter index range.
137  const int shift = _fNumPar - NumParPCA;
138  for (int i = FirstPCAdpar + nKeptPCApars; i < NumParPCA; ++i) {
139  isDecomposedPCA[i] = i + shift;
140  }
141 
142  #ifdef DEBUG_PCA
143  //KS: Let's dump all useful matrices to properly validate PCA
144  DebugPCA(sum, temp, submat, CovMatrix->GetNrows());
145  #endif
146 }
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
Custom exception class used throughout MaCh3.
std::vector< int > isDecomposedPCA
If param is decomposed this will return -1, if not this will return enumerator to param in normal bas...
Definition: PCAHandler.h:235
TMatrixD TransferMat
Matrix used to converting from PCA base to normal base.
Definition: PCAHandler.h:240
std::vector< double > eigen_values_master
Eigen values which have dimension equal to _fNumParPCA, and can be used in CorrelateSteps.
Definition: PCAHandler.h:248
TMatrixD eigen_vectors
Eigen vectors only of params which are being decomposed.
Definition: PCAHandler.h:246
TMatrixD TransferMatT
Matrix used to converting from normal base to PCA base.
Definition: PCAHandler.h:242
TVectorD eigen_values
Eigen value only of particles which are being decomposed.
Definition: PCAHandler.h:244
int LastPCAdpar
Index of the last param that is being decomposed.
Definition: PCAHandler.h:255
int nKeptPCApars
Total number that remained after applying PCA Threshold.
Definition: PCAHandler.h:251
std::vector< double > _fErrorPCA
Tells if parameter is fixed in PCA base or not.
Definition: PCAHandler.h:233
std::vector< double > _fPreFitValuePCA
Prefit value for PCA params.
Definition: PCAHandler.h:227
void SanitisePCA(TMatrixDSym *CovMatrix)
KS: Make sure decomposed matrix isn't correlated with undecomposed.
Definition: PCAHandler.cpp:150
int FirstPCAdpar
Index of the first param that is being decomposed.
Definition: PCAHandler.h:253
double eigen_threshold
CW: Threshold based on which we remove parameters in eigen base.
Definition: PCAHandler.h:257

◆ CorrelateSteps()

void PCAHandler::CorrelateSteps ( const std::vector< double > &  IndivStepScale,
const double  GlobalStepScale,
const double *_restrict_  randParams,
const double *_restrict_  corr_throw 
)

Use Cholesky throw matrix for better step proposal.

Definition at line 194 of file PCAHandler.cpp.

197  {
198 // ************************************************
199  // Throw around the current step
200  #ifdef MULTITHREAD
201  #pragma omp parallel for
202  #endif
203  for (int i = 0; i < NumParPCA; ++i)
204  {
205  if (_fErrorPCA[i] > 0.)
206  {
207  double IndStepScale = 1.;
208  //KS: If undecomposed parameter apply individual step scale and Cholesky for better acceptance rate
209  if(isDecomposedPCA[i] >= 0)
210  {
211  IndStepScale *= IndivStepScale[isDecomposedPCA[i]];
212  IndStepScale *= corr_throw[isDecomposedPCA[i]];
213  }
214  //If decomposed apply only random number
215  else
216  {
217  IndStepScale *= randParams[i];
218  //KS: All PCA-ed parameters have the same step scale
219  IndStepScale *= IndivStepScale[FirstPCAdpar];
220  }
221  _fParPropPCA(i) = _fParCurrPCA(i)+GlobalStepScale*IndStepScale*eigen_values_master[i];
222  }
223  }
224  // Then update the parameter basis
225  TransferToParam();
226 }

◆ GetEigenValues()

const TVectorD PCAHandler::GetEigenValues ( ) const
inline

Get eigen values for all parameters, if you want for decomposed only parameters use GetEigenValuesMaster.

Definition at line 200 of file PCAHandler.h.

200  {
201  return eigen_values;
202  }

◆ GetEigenValuesMaster()

const std::vector<double> PCAHandler::GetEigenValuesMaster ( ) const
inline

Get eigen value of only decomposed parameters, if you want for all parameters use GetEigenValues.

Definition at line 205 of file PCAHandler.h.

205  {
206  return eigen_values_master;
207  }

◆ GetEigenVectors()

const TMatrixD PCAHandler::GetEigenVectors ( ) const
inline

Get eigen vectors of covariance matrix, only works with PCA.

Definition at line 148 of file PCAHandler.h.

148  {
149  return eigen_vectors;
150  }

◆ GetNumberPCAedParameters()

int PCAHandler::GetNumberPCAedParameters ( ) const
inline

Retrieve number of parameters in PCA base.

Definition at line 69 of file PCAHandler.h.

69 { return NumParPCA; }

◆ GetParCurrPCA()

double PCAHandler::GetParCurrPCA ( const int  i) const
inline

Get current parameter value using PCA.

Parameters
iParameter index

Definition at line 188 of file PCAHandler.h.

188  {
189  return _fParCurrPCA(i);
190  }

◆ GetParPropPCA()

double PCAHandler::GetParPropPCA ( const int  i) const
inline

Get current parameter value using PCA.

Parameters
iParameter index

Definition at line 174 of file PCAHandler.h.

174  {
175  return _fParPropPCA(i);
176  }

◆ GetPreFitValuePCA()

double PCAHandler::GetPreFitValuePCA ( const int  i) const
inline

Get current parameter value using PCA.

Parameters
iParameter index

Definition at line 181 of file PCAHandler.h.

181  {
182  return _fPreFitValuePCA[i];
183  }

◆ GetTransferMatrix()

const TMatrixD PCAHandler::GetTransferMatrix ( ) const
inline

Get transfer matrix allowing to go from PCA base to normal base.

Definition at line 194 of file PCAHandler.h.

194  {
195  return TransferMat;
196  }

◆ IsParameterDecomposed()

bool PCAHandler::IsParameterDecomposed ( const int  i) const
inline

Check if parameter in PCA base is decomposed or not.

Parameters
iParameter index

Definition at line 212 of file PCAHandler.h.

212  {
213  if(isDecomposedPCA[i] >= 0) return false;
214  else return true;
215  }

◆ IsParameterFixedPCA()

bool PCAHandler::IsParameterFixedPCA ( const int  i) const
inline

Is parameter fixed in PCA base or not.

Parameters
iParameter index

Definition at line 141 of file PCAHandler.h.

141  {
142  if (_fErrorPCA[i] < 0) { return true; }
143  else { return false; }
144  }

◆ Print()

void PCAHandler::Print ( ) const

KS: Print info about PCA parameters.

Definition at line 270 of file PCAHandler.cpp.

270  {
271 // ********************************************
272  MACH3LOG_INFO("PCA:");
273  for (int i = 0; i < NumParPCA; ++i) {
274  MACH3LOG_INFO("PCA {:<2} Current: {:<10.2f} Proposed: {:<10.2f}", i, _fParCurrPCA(i), _fParPropPCA(i));
275  }
276 }

◆ SanitisePCA()

void PCAHandler::SanitisePCA ( TMatrixDSym *  CovMatrix)
private

KS: Make sure decomposed matrix isn't correlated with undecomposed.

Definition at line 150 of file PCAHandler.cpp.

150  {
151 // ********************************************
152  constexpr double correlation_threshold = 1e-6;
153 
154  bool found_significant_correlation = false;
155 
156  int N = CovMatrix->GetNrows();
157  for (int i = FirstPCAdpar; i <= LastPCAdpar; ++i) {
158  for (int j = 0; j < N; ++j) {
159  // Skip if j is inside the decomposed range (we only want cross-correlations)
160  if (j >= FirstPCAdpar && j <= LastPCAdpar) continue;
161 
162  double corr_val = (*CovMatrix)(i, j);
163  if (std::fabs(corr_val) > correlation_threshold) {
164  found_significant_correlation = true;
165  MACH3LOG_ERROR("Significant correlation detected between decomposed parameter '{}' "
166  "and undecomposed parameter '{}': {:.6e}", i, j, corr_val);
167  }
168  }
169  }
170 
171  if (found_significant_correlation) {
172  MACH3LOG_ERROR("There are correlations between undecomposed and decomposed part of matrices, this will not work");
173  throw MaCh3Exception(__FILE__ , __LINE__);
174  }
175 }

◆ SetBranches()

void PCAHandler::SetBranches ( TTree &  tree,
const bool  SaveProposal,
const std::vector< std::string > &  Names 
)

set branches for output file

Definition at line 279 of file PCAHandler.cpp.

279  {
280 // ********************************************
281  for (int i = 0; i < NumParPCA; ++i) {
282  tree.Branch(Form("%s_PCA", Names[i].c_str()), &_fParCurrPCA.GetMatrixArray()[i], Form("%s_PCA/D", Names[i].c_str()));
283  }
284 
285  if(SaveProposal)
286  {
287  for (int i = 0; i < NumParPCA; ++i) {
288  tree.Branch(Form("%s_PCA_Prop", Names[i].c_str()), &_fParPropPCA.GetMatrixArray()[i], Form("%s_PCA_Prop/D", Names[i].c_str()));
289  }
290  }
291 }

◆ SetInitialParameters()

void PCAHandler::SetInitialParameters ( )

KS: Transfer the starting parameters to the PCA basis, you don't want to start with zero..

Parameters
IndStepScalePer parameter step scale, we set so each PCA param uses same step scale [eigen value takes care of size]

Definition at line 245 of file PCAHandler.cpp.

245  {
246 // ********************************************
247  TransferToPCA();
248  for (int i = 0; i < NumParPCA; ++i) {
250  }
251 }
void TransferToPCA()
Transfer param values from normal base to PCA base.
Definition: PCAHandler.cpp:230

◆ SetParametersPCA()

void PCAHandler::SetParametersPCA ( const std::vector< double > &  pars)
inline

Set values for PCA parameters in PCA base.

Parameters
parsvector with new values of PCA params

Definition at line 125 of file PCAHandler.h.

125  {
126  if (int(pars.size()) != NumParPCA) {
127  MACH3LOG_ERROR("Parameter arrays of incompatible size! Not changing parameters! has size {} but was expecting {}", pars.size(), NumParPCA);
128  throw MaCh3Exception(__FILE__ , __LINE__ );
129  }
130  int parsSize = int(pars.size());
131  for (int i = 0; i < parsSize; i++) {
132  _fParPropPCA(i) = pars[i];
133  }
134  //KS: Transfer to normal base
135  TransferToParam();
136  }

◆ SetParCurrPCA()

void PCAHandler::SetParCurrPCA ( const int  i,
const double  value 
)
inline

Set current value for parameter in PCA base.

Parameters
iParameter index
valuenew value

Definition at line 165 of file PCAHandler.h.

165  {
166  _fParCurrPCA(i) = value;
167  // And then transfer back to the parameter basis
168  TransferToParam();
169  }

◆ SetParPropPCA()

void PCAHandler::SetParPropPCA ( const int  i,
const double  value 
)
inline

Set proposed value for parameter in PCA base.

Parameters
iParameter index
valuenew value

Definition at line 156 of file PCAHandler.h.

156  {
157  _fParPropPCA(i) = value;
158  // And then transfer back to the parameter basis
159  TransferToParam();
160  }

◆ SetupPointers()

void PCAHandler::SetupPointers ( std::vector< double > *  fCurr_Val,
std::vector< double > *  fProp_Val 
)

KS: Setup pointers to current and proposed parameter value which we need to convert them to PCA base each step.

Parameters
fCurr_Valpointer to current position of parameter
fProp_Valpointer to proposed position of parameter

Definition at line 17 of file PCAHandler.cpp.

18  {
19 // ********************************************
20  _pCurrVal = fCurr_Val;
21  _pPropVal = fProp_Val;
22 }

◆ ThrowParameters()

void PCAHandler::ThrowParameters ( const std::vector< std::unique_ptr< TRandom3 >> &  random_number,
double **  throwMatrixCholDecomp,
double *  randParams,
double *  corr_throw,
const std::vector< double > &  fPreFitValue,
const std::vector< double > &  fLowBound,
const std::vector< double > &  fUpBound,
int  _fNumPar 
)

Throw the parameters according to the covariance matrix. This shouldn't be used in MCMC code ase it can break Detailed Balance;.

Todo:
KS: We don't check if param is out of bounds. This is more problematic for PCA params.

Definition at line 318 of file PCAHandler.cpp.

325  {
326 // ********************************************
327  //KS: Do not multithread!
328  for (int i = 0; i < NumParPCA; ++i) {
329  // Check if parameter is fixed first: if so don't randomly throw
330  if (IsParameterFixedPCA(i)) continue;
331 
332  if(!IsParameterDecomposed(i))
333  {
334  (*_pPropVal)[i] = fPreFitValue[i] + corr_throw[i];
335  int throws = 0;
336  // Try again if we the initial parameter proposal falls outside of the range of the parameter
337  while ((*_pPropVal)[i] > fUpBound[i] || (*_pPropVal)[i] < fLowBound[i]) {
338  randParams[i] = random_number[M3::GetThreadIndex()]->Gaus(0, 1);
339  const double corr_throw_single = M3::MatrixVectorMultiSingle(throwMatrixCholDecomp, randParams, _fNumPar, i);
340  (*_pPropVal)[i] = fPreFitValue[i] + corr_throw_single;
341  if (throws > 10000)
342  {
343  //KS: Since we are multithreading there is danger that those messages
344  //will be all over the place, small price to pay for faster code
345  MACH3LOG_WARN("Tried {} times to throw parameter {} but failed", throws, i);
346  MACH3LOG_WARN("Setting _fPropVal: {} to {}", (*_pPropVal)[i], fPreFitValue[i]);
347  MACH3LOG_WARN("I live at {}:{}", __FILE__, __LINE__);
348  (*_pPropVal)[i] = fPreFitValue[i];
349  }
350  throws++;
351  }
352  (*_pCurrVal)[i] = (*_pPropVal)[i];
353 
354  } else {
355  // KS: We have to multiply by number of parameters in PCA base
358  }
359  } // end of parameter loop
360 
362  for (int i = 0; i < _fNumPar; ++i) {
363  (*_pPropVal)[i] = std::max(fLowBound[i], std::min((*_pPropVal)[i], fUpBound[i]));
364  (*_pCurrVal)[i] = (*_pPropVal)[i];
365  }
366 }
#define MACH3LOG_WARN
Definition: MaCh3Logger.h:36
bool IsParameterFixedPCA(const int i) const
Is parameter fixed in PCA base or not.
Definition: PCAHandler.h:141
double GetParPropPCA(const int i) const
Get current parameter value using PCA.
Definition: PCAHandler.h:174
void SetParPropPCA(const int i, const double value)
Set proposed value for parameter in PCA base.
Definition: PCAHandler.h:156
double GetPreFitValuePCA(const int i) const
Get current parameter value using PCA.
Definition: PCAHandler.h:181
bool IsParameterDecomposed(const int i) const
Check if parameter in PCA base is decomposed or not.
Definition: PCAHandler.h:212
void SetParCurrPCA(const int i, const double value)
Set current value for parameter in PCA base.
Definition: PCAHandler.h:165
int GetThreadIndex()
thread index inside parallel loop
Definition: Monitor.h:86
double MatrixVectorMultiSingle(double **_restrict_ matrix, const double *_restrict_ vector, const int Length, const int i)
KS: Custom function to perform multiplication of matrix and single element which is thread safe.

◆ ToggleFixAllParameters()

void PCAHandler::ToggleFixAllParameters ( const std::vector< std::string > &  Names)

fix parameters at prior values

Definition at line 294 of file PCAHandler.cpp.

294  {
295 // ********************************************
296  for (int i = 0; i < NumParPCA; i++) ToggleFixParameter(i, Names);
297 }
void ToggleFixParameter(const int i, const std::vector< std::string > &Names)
fix parameters at prior values
Definition: PCAHandler.cpp:300

◆ ToggleFixParameter()

void PCAHandler::ToggleFixParameter ( const int  i,
const std::vector< std::string > &  Names 
)

fix parameters at prior values

Definition at line 300 of file PCAHandler.cpp.

300  {
301 // ********************************************
302  int isDecom = -1;
303  for (int im = 0; im < NumParPCA; ++im) {
304  if(isDecomposedPCA[im] == i) {isDecom = im;}
305  }
306  if(isDecom < 0) {
307  MACH3LOG_ERROR("Parameter {} is PCA decomposed can't fix this", Names[i]);
308  //throw MaCh3Exception(__FILE__ , __LINE__ );
309  } else {
310  _fErrorPCA[isDecom] *= -1.0;
311  if(IsParameterFixedPCA(i)) MACH3LOG_INFO("Setting un-decomposed {}(parameter {}/{} in PCA base) to fixed at {}", Names[i], i, isDecom, (*_pCurrVal)[i]);
312  else MACH3LOG_INFO("Setting un-decomposed {}(parameter {}/{} in PCA base) free", Names[i], i, isDecom);
313  }
314 }

◆ TransferToParam()

void PCAHandler::TransferToParam ( )

Transfer param values from PCA base to normal base.

Definition at line 255 of file PCAHandler.cpp.

255  {
256 // ********************************************
257  // Make the temporary vectors
258  TVectorD fParProp_vec = TransferMat*_fParPropPCA;
259  TVectorD fParCurr_vec = TransferMat*_fParCurrPCA;
260  #ifdef MULTITHREAD
261  #pragma omp parallel for
262  #endif
263  for(int i = 0; i < static_cast<int>(_pCurrVal->size()); ++i) {
264  (*_pPropVal)[i] = fParProp_vec(i);
265  (*_pCurrVal)[i] = fParCurr_vec(i);
266  }
267 }

◆ TransferToPCA()

void PCAHandler::TransferToPCA ( )

Transfer param values from normal base to PCA base.

Definition at line 230 of file PCAHandler.cpp.

230  {
231 // ********************************************
232  // Make the temporary vectors
233  TVectorD fParCurr_vec(static_cast<Int_t>(_pCurrVal->size()));
234  TVectorD fParProp_vec(static_cast<Int_t>(_pCurrVal->size()));
235  for(int i = 0; i < static_cast<int>(_pCurrVal->size()); ++i) {
236  fParCurr_vec(i) = (*_pCurrVal)[i];
237  fParProp_vec(i) = (*_pPropVal)[i];
238  }
239 
240  _fParCurrPCA = TransferMatT*fParCurr_vec;
241  _fParPropPCA = TransferMatT*fParProp_vec;
242 }

Member Data Documentation

◆ _fErrorPCA

std::vector<double> PCAHandler::_fErrorPCA
private

Tells if parameter is fixed in PCA base or not.

Definition at line 233 of file PCAHandler.h.

◆ _fParCurrPCA

TVectorD PCAHandler::_fParCurrPCA
private

CW: Proposed parameter value in PCA base.

Definition at line 231 of file PCAHandler.h.

◆ _fParPropPCA

TVectorD PCAHandler::_fParPropPCA
private

CW: Current parameter value in PCA base.

Definition at line 229 of file PCAHandler.h.

◆ _fPreFitValuePCA

std::vector<double> PCAHandler::_fPreFitValuePCA
private

Prefit value for PCA params.

Definition at line 227 of file PCAHandler.h.

◆ _pCurrVal

std::vector<double>* PCAHandler::_pCurrVal
private

Pointer to current value of the parameter.

Definition at line 260 of file PCAHandler.h.

◆ _pPropVal

std::vector<double>* PCAHandler::_pPropVal
private

Pointer to proposed value of the parameter.

Definition at line 262 of file PCAHandler.h.

◆ eigen_threshold

double PCAHandler::eigen_threshold
private

CW: Threshold based on which we remove parameters in eigen base.

Definition at line 257 of file PCAHandler.h.

◆ eigen_values

TVectorD PCAHandler::eigen_values
private

Eigen value only of particles which are being decomposed.

Definition at line 244 of file PCAHandler.h.

◆ eigen_values_master

std::vector<double> PCAHandler::eigen_values_master
private

Eigen values which have dimension equal to _fNumParPCA, and can be used in CorrelateSteps.

Definition at line 248 of file PCAHandler.h.

◆ eigen_vectors

TMatrixD PCAHandler::eigen_vectors
private

Eigen vectors only of params which are being decomposed.

Definition at line 246 of file PCAHandler.h.

◆ FirstPCAdpar

int PCAHandler::FirstPCAdpar
private

Index of the first param that is being decomposed.

Definition at line 253 of file PCAHandler.h.

◆ isDecomposedPCA

std::vector<int> PCAHandler::isDecomposedPCA
private

If param is decomposed this will return -1, if not this will return enumerator to param in normal base. This way we can map stuff like step scale etc between normal base and undecomposed param in eigen base.

Definition at line 235 of file PCAHandler.h.

◆ LastPCAdpar

int PCAHandler::LastPCAdpar
private

Index of the last param that is being decomposed.

Definition at line 255 of file PCAHandler.h.

◆ nKeptPCApars

int PCAHandler::nKeptPCApars
private

Total number that remained after applying PCA Threshold.

Definition at line 251 of file PCAHandler.h.

◆ NumParPCA

int PCAHandler::NumParPCA
private

Number of parameters in PCA base.

Definition at line 237 of file PCAHandler.h.

◆ TransferMat

TMatrixD PCAHandler::TransferMat
private

Matrix used to converting from PCA base to normal base.

Definition at line 240 of file PCAHandler.h.

◆ TransferMatT

TMatrixD PCAHandler::TransferMatT
private

Matrix used to converting from normal base to PCA base.

Definition at line 242 of file PCAHandler.h.


The documentation for this class was generated from the following files: