-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.h
328 lines (293 loc) · 7.35 KB
/
functions.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
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
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <cmath>
#include <limits>
#include <array>
#include <Eigen/Dense>
/// Function for regression model
/// \f$ f(t) = e^{-Dt} \f$
/// This is reference function for implementing new functions
class FunctionRef
{
public:
const static size_t nParams = 1;
typedef Eigen::Matrix<double, nParams ,1> VParams;
/// \f$ f(t) \f$
inline static double CalcFT (const VParams & params, const double t)
{
const double& D = params[0];
return exp(-D*t);
}
/// \f$ \frac{df(t)}{d w_i} \f$ where \f$ w_i \f$ - model param
inline static double CalcDFDIParam(size_t iParam, const VParams & params, const double t)
{
switch(iParam)
{
case 0 :
const double& D = params[0];
return -t*exp(-D*t);
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
/// \f$ \int f(t) dt \f$
/// This function used only for generating test data and shouldn't be
/// implemented in another functions
inline static double calcIntF(const VParams & params, const double t)
{
const double& D = params[0];
return -exp(-D*t)/D;
}
// Special for RegressionModelLn
inline static double calcLnFT (const VParams & params, const double t)
{
const double& D = params[0];
return -D*t;
}
// Special for RegressionModelLn
inline static double calcDFDIParamDivFT(size_t iParam, const VParams & params, const double t)
{
switch(iParam)
{
case 0 :
return -t;
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
inline static double GetParamLowerLimits(size_t iParam)
{
switch(iParam)
{
case 0 :
return 1e-300;
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
inline static double GetParamUpperLimits(size_t iParam)
{
return std::numeric_limits<double>::max();
}
inline static double GetDefaultParam(size_t iParam)
{
switch(iParam)
{
case 0 :
return 0.0001;
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
};
typedef FunctionRef Function1;
/// Function for regression model
/// \f$ f(t) = (1+t)^{-a} \f$
/// This is reference function for implementing new functions
class Function2
{
public:
const static size_t nParams = 1;
typedef Eigen::Matrix<double, nParams ,1> VParams;
/// \f$ f(t) \f$
inline static double CalcFT (const VParams & params, const double t)
{
const double& a = params[0];
return pow(1+t,-a);
}
/// \f$ \frac{df(t)}{d w_i} \f$ where \f$ w_i \f$ - model param
static double CalcDFDIParam(size_t iParam, const VParams & params, const double t)
{
const double& a = params[0];
switch(iParam)
{
case 0 :
return -pow(1+t,-a)*log(t+1);
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
inline static double GetParamLowerLimits(size_t iParam)
{
switch(iParam)
{
case 0 :
return 1e-300;
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
inline static double GetParamUpperLimits(size_t iParam)
{
return std::numeric_limits<double>::max();
}
inline static double GetDefaultParam(size_t iParam)
{
switch(iParam)
{
case 0 :
return 0.01;
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
};
/// Function for regression model
/// \f$ f(t) = (1+b*D*t)^{-1/b} \f$
/// This is reference function for implementing new functions
class Function3
{
public:
const static size_t nParams = 2;
typedef Eigen::Matrix<double, nParams ,1> VParams;
/// \f$ f(t) \f$
inline static double CalcFT (const VParams & params, const double t)
{
const double& D = params[0];
const double& b = params[1];
return pow(1+b*D*t, -1.0/b);
}
/// \f$ \frac{df(t)}{d w_i} \f$ where \f$ w_i \f$ - model param
inline static double CalcDFDIParam(size_t iParam, const VParams & params, const double t)
{
const double& D = params[0];
const double& b = params[1];
const double dbt = D*b*t;
const double dbtp1 = dbt+1;
switch(iParam)
{
case 0 :
return -t*pow(dbtp1, -1.0/b-1);
break;
case 1 :
return 1/(b*b) *(-dbt+dbtp1*log(dbtp1))*pow(dbtp1, -(b+1)/b);
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
inline static double GetParamLowerLimits(size_t iParam)
{
switch(iParam)
{
case 0 :
return 1e-300;
break;
case 1:
return 1e-300;
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
inline static double GetParamUpperLimits(size_t iParam)
{
return std::numeric_limits<double>::max();
}
inline static double GetDefaultParam(size_t iParam)
{
switch(iParam)
{
case 0 :
return 1e-5;
break;
case 1 :
return 2.0;
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
};
/// Function for regression model
/// \f$ f(t) = (1+t)^{-a}, t<tau \f$
/// \f$ f(t) = (1+b*D(t-tau))^{-1/b}, t>=tau \f$
/// This is reference function for implementing new functions
class Function4
{
public:
const static size_t nParams = 3;
typedef Eigen::Matrix<double, nParams ,1> VParams;
/// \f$ f(t) \f$
inline static double CalcFT (const VParams & params, const double t)
{
const double& b = params[0];
const double& a = params[1];
const double& tau = params[2];
const double D=a*pow(t+1,-a-1);
if(t<tau)
{
Function2::VParams tmp;
tmp[0] = a;
return Function2::CalcFT(tmp, t);
}
else
{
return Function3::CalcFT({D, b}, t-tau);
}
}
/// \f$ \frac{df(t)}{d w_i} \f$ where \f$ w_i \f$ - model param
inline static double CalcDFDIParam(size_t iParam, const VParams & params, const double t)
{
const double& b = params[0];
const double& a = params[1];
const double& tau = params[2];
const double D=a*pow(t+1,-a-1);
switch(iParam)
{
case 0 :
if(t<tau)
return 0;
else
return Function3::CalcDFDIParam(0, {D, b}, t-tau);
break;
case 1:
if(t<tau)
{
Function2::VParams tmp;
tmp[0] = a;
return Function2::CalcDFDIParam(0, tmp, t);
}
else
return 0;
break;
case 2:
if(t<tau)
return 0;
else
return D*pow(D*b*(t-tau)+1, -(b+1)/b);
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
inline static double GetParamLowerLimits(size_t iParam)
{
switch(iParam)
{
case 0 :
case 1 :
case 2 :
return 1e-300;
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
inline static double GetParamUpperLimits(size_t iParam)
{
return std::numeric_limits<double>::max();
}
inline static double GetDefaultParam(size_t iParam)
{
switch(iParam)
{
case 0 :
return 2.0;
break;
case 1 :
return 0.01;
break;
case 2 :
return 10000;
break;
}
throw std::invalid_argument("Param index exceeds params count");
}
};
#endif // FUNCTIONS_H