-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChainManager.h
295 lines (244 loc) · 8 KB
/
ChainManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Read the documentation to learn more about C++ code generator
// versioning.
// %X% %Q% %Z% %W%
#ifndef CHAINMANAGER_H
#define CHAINMANAGER_H 1
#include <xsTypes.h>
#include <memory>
#include <iosfwd>
// Chain
#include <XSFit/MCMC/Chain.h>
class MarginGrid;
class RandomizerBase;
class ChainManager
{
public:
typedef std::map<string,Chain*> ChainContainer;
struct Stats
{
Stats();
// Data Members for Class Attributes
std::vector<Real> means;
Real totalMean;
Real totalVar;
Real varInChains;
Real rubinGelman;
std::vector<Real> fracRepeats;
std::vector<Real> geweke;
public:
protected:
private:
private: //## implementation
};
~ChainManager();
static ChainManager* Instance ();
void addChain (Chain* chain);
bool removeChain (const string& fileName);
void clearChains ();
bool widthConflict (const Chain* newChain) const;
void recalc ();
void calcStat (size_t iPar, const string& modName);
void calcDevInfCrit (Real& effNumPars, Real& devInfCrit);
void findBestFit(RealArray& parVals, Real& statVal);
void removeChains (const IntegerVector& chainNums);
bool checkLengths ();
void getRandomPoint (RealArray& parVals) const;
void getLastPoint (RealArray& parVals) const;
void setChainProposal (const string& proposalName, const string& optInitArg = string(""));
void reportChainProposal () const;
bool checkCovarForSynch () const;
std::pair<Real,Real> getParErrorRange (const Real confidence, const size_t parNum, const string& modName) const;
void appendToChain (const string& chainName, const size_t addLength, const Real temperature, const string& format);
//az- void appendToChain (const string& chainName, const size_t addLength, const size_t walkers, const string& format);
//az+
void appendToChain (const string& chainName, const size_t addLength, const size_t walkers, const Real temperature, const string& format);
//az+/
size_t length () const;
size_t width () const;
bool allLengthsSame () const;
bool isSynchedWithFitParams () const;
void isSynchedWithFitParams (bool value);
// Internal bookkeeping array, primarily used to determine
// which file contains the nth point numbered from the
// start of the first chain file.
const std::vector<size_t>& accumulatedLengths () const;
RandomizerBase* chainProposal ();
const RealArray& chainSVDevalue () const;
const RealArray& chainSVDevector () const;
const RealArray& chainCovariance () const;
const ChainManager::Stats& lastStatCalc () const;
const Real& lastDevInfCritCalc () const;
const Real& lastEffNumParsCalc () const;
const ChainManager::ChainContainer& chains () const;
const Chain* getChain (const string& fileName) const;
MarginGrid* marginGrid () const;
void marginGrid (MarginGrid* value);
public:
// Additional Public Declarations
// Functors which can be passed to forEachChainPoint.
class CollectParVals
{
public:
CollectParVals(size_t iPar) : m_iPar(iPar) {}
void operator () (const std::vector<Real>& chainPoint)
{
// For speed, no checking performed here.
m_parVals.push_back(chainPoint[m_iPar]);
}
std::vector<Real>& parVals() {return m_parVals;}
private:
const size_t m_iPar;
std::vector<Real> m_parVals;
};
class CollectSumVar
{
public:
CollectSumVar(size_t nPar);
void operator () (const std::vector<Real>& chainPoint)
{
for (size_t j=0; j<m_nPar; ++j)
{
Real chainVals_j = chainPoint[j];
m_chainSum[j] += chainVals_j;
const size_t jOffset = j*m_nPar;
for (size_t k=0; k<m_nPar; ++k)
{
m_chainVariance[jOffset+k] += chainPoint[k]*chainVals_j;
}
}
}
double* getChainSum() {return m_chainSum;}
double* getChainVariance() {return m_chainVariance;}
private:
const size_t m_nPar;
std::unique_ptr<double[]> m_apChainSum;
std::unique_ptr<double[]> m_apChainVariance;
double* m_chainSum;
double* m_chainVariance;
};
protected:
ChainManager();
// Additional Protected Declarations
private:
ChainManager(const ChainManager &right);
ChainManager & operator=(const ChainManager &right);
const Chain* getChainForRandomPoint (size_t globalLocation, size_t& locInChain) const;
size_t findParPosition (const size_t parNum, const string& modName) const;
// Additional Private Declarations
template <class Op>
void forEachChainPoint(Op& oper) const;
private: //## implementation
// Data Members for Class Attributes
static ChainManager* s_instance;
size_t m_length;
size_t m_width;
bool m_allLengthsSame;
bool m_isSynchedWithFitParams;
std::vector<size_t> m_accumulatedLengths;
RandomizerBase* m_chainProposal;
RealArray m_chainSVDevalue;
RealArray m_chainSVDevector;
RealArray m_chainCovariance;
ChainManager::Stats m_lastStatCalc;
Real m_lastDevInfCritCalc;
Real m_lastEffNumParsCalc;
// Data Members for Associations
ChainManager::ChainContainer m_chains;
MarginGrid* m_marginGrid;
std::vector<Chain::ParamID> m_covarParams;
// Additional Implementation Declarations
};
std::ostream& operator<< (std::ostream& os, const ChainManager::Stats& right);
template <class Op>
void ChainManager::forEachChainPoint(Op& oper) const
{
ChainContainer::const_iterator itChain = m_chains.begin();
ChainContainer::const_iterator itEnd = m_chains.end();
while (itChain != itEnd)
{
Chain* chain = itChain->second;
const int len = chain->length();
chain->openForReadPoints();
for (int i=0; i<len; ++i)
{
std::vector<Real> chainVals;
chain->readPoint(chainVals);
oper(chainVals);
}
chain->closeFile();
++itChain;
}
}
// Class ChainManager::Stats
// Class ChainManager
inline size_t ChainManager::length () const
{
return m_length;
}
inline size_t ChainManager::width () const
{
return m_width;
}
inline bool ChainManager::allLengthsSame () const
{
return m_allLengthsSame;
}
inline bool ChainManager::isSynchedWithFitParams () const
{
return m_isSynchedWithFitParams;
}
inline void ChainManager::isSynchedWithFitParams (bool value)
{
m_isSynchedWithFitParams = value;
}
inline const std::vector<size_t>& ChainManager::accumulatedLengths () const
{
return m_accumulatedLengths;
}
inline RandomizerBase* ChainManager::chainProposal ()
{
return m_chainProposal;
}
inline const RealArray& ChainManager::chainSVDevalue () const
{
return m_chainSVDevalue;
}
inline const RealArray& ChainManager::chainSVDevector () const
{
return m_chainSVDevector;
}
inline const RealArray& ChainManager::chainCovariance () const
{
return m_chainCovariance;
}
inline const ChainManager::Stats& ChainManager::lastStatCalc () const
{
return m_lastStatCalc;
}
inline const Real& ChainManager::lastDevInfCritCalc () const
{
return m_lastDevInfCritCalc;
}
inline const Real& ChainManager::lastEffNumParsCalc () const
{
return m_lastEffNumParsCalc;
}
inline const ChainManager::ChainContainer& ChainManager::chains () const
{
return m_chains;
}
inline const Chain* ChainManager::getChain (const string& fileName) const
{
ChainContainer::const_iterator it = m_chains.find(fileName);
const Chain* chain = (it != m_chains.end()) ? it->second : 0;
return chain;
}
inline MarginGrid* ChainManager::marginGrid () const
{
return m_marginGrid;
}
inline void ChainManager::marginGrid (MarginGrid* value)
{
m_marginGrid = value;
}
#endif