-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall.cpp
372 lines (325 loc) · 11.8 KB
/
all.cpp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#ifndef _ALL_CPP
#define _ALL_CPP
#include <inttypes.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/LU>
#include <random>
#include <vector>
class Estimator {
public:
virtual std::pair<int, Eigen::MatrixXd> estimate(int N,
std::mt19937& mt) = 0;
virtual Eigen::MatrixXd getTheoretical() const = 0;
};
namespace SE {
class Model {
public:
Model() {}
virtual Eigen::MatrixXd f(const Eigen::MatrixXd& x) const = 0;
virtual int fSize() const = 0;
virtual Eigen::MatrixXd xRvs(std::mt19937& mt) const = 0;
virtual Eigen::MatrixXd getTheoretical() const = 0;
};
class MC : public ::Estimator {
private:
Model& model;
public:
MC(Model& model) : model{model} {}
std::pair<int, Eigen::MatrixXd> estimate(int N, std::mt19937& mt);
Eigen::MatrixXd getTheoretical() const;
};
} // namespace SE
namespace NE {
class Model {
public:
Model() {}
virtual Eigen::MatrixXd f(const Eigen::MatrixXd& gy) const = 0;
virtual Eigen::MatrixXd g(const std::vector<Eigen::MatrixXd>& yz) const = 0;
virtual int fSize() const = 0;
virtual int gSize() const = 0;
virtual Eigen::MatrixXd yRvs(std::mt19937& mt) const = 0;
virtual Eigen::MatrixXd zRvs(const Eigen::MatrixXd& y,
std::mt19937& mt) const = 0;
virtual int ySize() const = 0;
virtual int zSize() const = 0;
virtual std::vector<Eigen::MatrixXd> psaRvs(std::mt19937& mt) const {
auto y = yRvs(mt);
auto z = zRvs(y, mt);
return {y, z};
}
virtual Eigen::MatrixXd getTheoretical() const = 0;
};
class TestCase : public Model {
public:
TestCase();
Eigen::MatrixXd f(const Eigen::MatrixXd& gy) const;
Eigen::MatrixXd g(const std::vector<Eigen::MatrixXd>& yz) const;
int fSize() const;
int gSize() const;
Eigen::MatrixXd yRvs(std::mt19937& mt) const;
Eigen::MatrixXd zRvs(const Eigen::MatrixXd& y, std::mt19937& mt) const;
int ySize() const;
int zSize() const;
Eigen::MatrixXd getTheoretical() const;
};
class NMC : public ::Estimator {
private:
Model& model;
int adjustM(int N);
Eigen::MatrixXd gamma(const Eigen::MatrixXd& y, int M, std::mt19937& mt);
public:
NMC(Model& model) : model{model} {}
std::pair<int, Eigen::MatrixXd> estimate(int N, std::mt19937& mt);
Eigen::MatrixXd getTheoretical() const;
};
class Smoothing : public ::Estimator {
private:
Model& model;
virtual Eigen::MatrixXd smoothing(const Eigen::MatrixXd& xs,
const Eigen::MatrixXd& ys) = 0;
public:
Smoothing(Model& model) : model{model} {}
std::pair<int, Eigen::MatrixXd> estimate(int N, std::mt19937& mt);
Eigen::MatrixXd getTheoretical() const;
};
class Proposed : public Smoothing {
private:
Eigen::MatrixXd smoothing(const Eigen::MatrixXd& xs,
const Eigen::MatrixXd& ys);
std::vector<int> nSplit(int N, int K);
int nAdjust(const std::vector<int>& no);
std::vector<std::vector<std::pair<Eigen::MatrixXd, Eigen::MatrixXd>>>
grouping(std::vector<std::pair<Eigen::MatrixXd, Eigen::MatrixXd>>& xys,
const std::vector<int>& no);
virtual Eigen::MatrixXd xTildes(
const std::vector<
std::vector<std::pair<Eigen::MatrixXd, Eigen::MatrixXd>>>&
xyChunks);
public:
Proposed(Model& model) : Smoothing{model} {}
};
class ProposedWithRegression : public Proposed {
private:
Eigen::MatrixXd xTildes(
const std::vector<
std::vector<std::pair<Eigen::MatrixXd, Eigen::MatrixXd>>>&
xyChunks);
public:
ProposedWithRegression(Model& model) : Proposed{model} {}
};
class Strong : public Smoothing {
private:
Eigen::MatrixXd smoothing(const Eigen::MatrixXd& xs,
const Eigen::MatrixXd& ys);
const std::string tmpPath;
public:
Strong(Model& model, std::string tmpPath)
: Smoothing{model}, tmpPath{tmpPath} {}
};
} // namespace NE
namespace EVSI {
class Model {
public:
Model() {}
virtual Eigen::MatrixXd f_d(const Eigen::MatrixXd& theta) const = 0;
virtual int dSize() const = 0;
virtual Eigen::MatrixXd thetaRvs(std::mt19937& mt) const = 0;
virtual Eigen::MatrixXd phiRvs(const Eigen::MatrixXd& theta,
std::mt19937& mt) const = 0;
virtual int phiSize() const = 0;
virtual Eigen::MatrixXd thetaRvsGivenPhi(const Eigen::MatrixXd& phi,
std::mt19937& mt) const = 0;
virtual Eigen::MatrixXd getTheoretical() const = 0;
};
class TestCase : public Model {
private:
const int M;
public:
TestCase(int M) : M{M} {}
Eigen::MatrixXd f_d(const Eigen::MatrixXd& theta) const;
int dSize() const;
Eigen::MatrixXd thetaRvs(std::mt19937& mt) const;
Eigen::MatrixXd phiRvs(const Eigen::MatrixXd& theta,
std::mt19937& mt) const;
int phiSize() const;
Eigen::MatrixXd thetaRvsGivenPhi(const Eigen::MatrixXd& phi,
std::mt19937& mt) const;
Eigen::MatrixXd getTheoretical() const;
};
class Medical : public Model {
private:
typedef struct {
double L, Q_E, Q_SE, C_E, C_SE, C_T_1, C_T_2, C_T_3, P_E_1, logOR_E_2,
logOR_E_3, P_E_2, P_E_3, P_SE_1, P_SE_2, P_SE_3, lambda;
} thetaType;
thetaType matrixToStruct(const Eigen::MatrixXd& thetaMatrix) const;
Eigen::MatrixXd structToMatrix(const thetaType& thetaStruct) const;
double sigmoid(double x) const;
double logit(double p) const;
public:
Medical() {}
Eigen::MatrixXd f_d(const Eigen::MatrixXd& theta) const;
int dSize() const;
Eigen::MatrixXd thetaRvs(std::mt19937& mt) const;
Eigen::MatrixXd phiRvs(const Eigen::MatrixXd& theta,
std::mt19937& mt) const;
int phiSize() const;
Eigen::MatrixXd thetaRvsGivenPhi(const Eigen::MatrixXd& phi,
std::mt19937& mt) const;
Eigen::MatrixXd getTheoretical() const;
};
class NMC : public ::Estimator {
private:
Model& model;
class modelFirstTerm : public NE::Model {
private:
EVSI::Model& modelEVSI;
std::pair<int, std::vector<Eigen::MatrixXd>>& sharedSamples;
public:
modelFirstTerm(
EVSI::Model& model,
std::pair<int, std::vector<Eigen::MatrixXd>>& sharedSamples)
: Model{}, modelEVSI{model}, sharedSamples{sharedSamples} {};
Eigen::MatrixXd f(const Eigen::MatrixXd& gy) const;
Eigen::MatrixXd g(const std::vector<Eigen::MatrixXd>& yz) const;
int fSize() const;
int gSize() const;
Eigen::MatrixXd yRvs(std::mt19937& mt) const;
Eigen::MatrixXd zRvs(const Eigen::MatrixXd& y, std::mt19937& mt) const;
int ySize() const;
int zSize() const;
Eigen::MatrixXd getTheoretical() const;
} modelFirstTerm;
NE::NMC estimatorFirstTerm;
class modelSecondTerm : public SE::Model {
private:
EVSI::Model& modelEVSI;
std::pair<int, std::vector<Eigen::MatrixXd>>& sharedSamples;
public:
modelSecondTerm(
EVSI::Model& model,
std::pair<int, std::vector<Eigen::MatrixXd>>& sharedSamples)
: modelEVSI{model}, sharedSamples{sharedSamples} {};
Eigen::MatrixXd f(const Eigen::MatrixXd& x) const;
int fSize() const;
Eigen::MatrixXd xRvs(std::mt19937& mt) const;
Eigen::MatrixXd getTheoretical() const;
} modelSecondTerm;
SE::MC estimatorSecondTerm;
std::pair<int, std::vector<Eigen::MatrixXd>> sharedSamples;
void initSharedSamples(int N, std::mt19937& mt);
public:
NMC(Model& model)
: model{model},
modelFirstTerm{model, sharedSamples},
estimatorFirstTerm(modelFirstTerm),
modelSecondTerm{model, sharedSamples},
estimatorSecondTerm(modelSecondTerm) {}
std::pair<int, Eigen::MatrixXd> estimate(int N, std::mt19937& mt);
Eigen::MatrixXd getTheoretical() const;
};
class Smoothing : public ::Estimator {
private:
Model& model;
NE::Smoothing& estimatorFirstTerm;
SE::MC& estimatorSecondTerm;
std::pair<int, std::vector<std::vector<Eigen::MatrixXd>>> sharedSamples;
void initSharedSamples(int N, std::mt19937& mt);
protected:
class modelFirstTerm : public NE::Model {
private:
EVSI::Model& modelEVSI;
std::pair<int, std::vector<std::vector<Eigen::MatrixXd>>>&
sharedSamples;
public:
modelFirstTerm(
EVSI::Model& model,
std::pair<int, std::vector<std::vector<Eigen::MatrixXd>>>&
sharedSamples)
: Model{}, modelEVSI{model}, sharedSamples{sharedSamples} {};
Eigen::MatrixXd f(const Eigen::MatrixXd& gy) const;
Eigen::MatrixXd g(const std::vector<Eigen::MatrixXd>& yz) const;
int fSize() const;
int gSize() const;
Eigen::MatrixXd yRvs(std::mt19937& mt) const;
Eigen::MatrixXd zRvs(const Eigen::MatrixXd& y, std::mt19937& mt) const;
std::vector<Eigen::MatrixXd> psaRvs(std::mt19937& mt) const;
int ySize() const;
int zSize() const;
Eigen::MatrixXd getTheoretical() const;
} modelFirstTerm;
class modelSecondTerm : public SE::Model {
private:
EVSI::Model& modelEVSI;
std::pair<int, std::vector<std::vector<Eigen::MatrixXd>>>&
sharedSamples;
public:
modelSecondTerm(
EVSI::Model& model,
std::pair<int, std::vector<std::vector<Eigen::MatrixXd>>>&
sharedSamples)
: modelEVSI{model}, sharedSamples{sharedSamples} {};
Eigen::MatrixXd f(const Eigen::MatrixXd& x) const;
int fSize() const;
Eigen::MatrixXd xRvs(std::mt19937& mt) const;
Eigen::MatrixXd getTheoretical() const;
} modelSecondTerm;
public:
Smoothing(Model& model,
NE::Smoothing& estimatorFirstTerm,
SE::MC& estimatorSecondTerm)
: model{model},
estimatorFirstTerm(estimatorFirstTerm),
estimatorSecondTerm(estimatorSecondTerm),
modelFirstTerm{model, sharedSamples},
modelSecondTerm{model, sharedSamples} {}
std::pair<int, Eigen::MatrixXd> estimate(int N, std::mt19937& mt);
Eigen::MatrixXd getTheoretical() const;
};
class Proposed : public Smoothing {
private:
NE::Proposed estimatorFirstTerm;
SE::MC estimatorSecondTerm;
public:
Proposed(Model& model)
: Smoothing{model, estimatorFirstTerm, estimatorSecondTerm},
estimatorFirstTerm(modelFirstTerm),
estimatorSecondTerm(modelSecondTerm) {}
};
class ProposedWithRegression : public Smoothing {
private:
NE::ProposedWithRegression estimatorFirstTerm;
SE::MC estimatorSecondTerm;
public:
ProposedWithRegression(Model& model)
: Smoothing{model, estimatorFirstTerm, estimatorSecondTerm},
estimatorFirstTerm(modelFirstTerm),
estimatorSecondTerm(modelSecondTerm) {}
};
class Strong : public Smoothing {
private:
NE::Strong estimatorFirstTerm;
SE::MC estimatorSecondTerm;
public:
Strong(Model& model, std::string tmpPath)
: Smoothing{model, estimatorFirstTerm, estimatorSecondTerm},
estimatorFirstTerm(modelFirstTerm, tmpPath),
estimatorSecondTerm(modelSecondTerm) {}
};
} // namespace EVSI
#include "methods/evsi/nmc.cpp"
#include "methods/evsi/smoothing.cpp"
#include "methods/ne/nmc.cpp"
#include "methods/ne/proposed.cpp"
#include "methods/ne/smoothing.cpp"
#include "methods/ne/strong.cpp"
#include "methods/se/mc.cpp"
#include "models/evsi/medical.cpp"
#include "models/evsi/testcase.cpp"
#include "models/ne/testcase.cpp"
#include "visualize.cpp"
#endif