-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataTools.h
304 lines (277 loc) · 10 KB
/
DataTools.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
#pragma once
#include <nlohmann/json.hpp>
#include <aadc/aadc.h>
#include "SwapLegs.h"
using json = nlohmann::json;
////////////////////////////////////////////////////
//
// getArgumentsMap()
//
// Stores a map which connects json paths=name_of_variables,
// its values and corresponding AADC-indices.
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
typedef std::map<std::string, std::pair<idouble, aadc::AADCArgument>> ArgumentsMap;
inline ArgumentsMap& getArgumentsMap() {
static ArgumentsMap request_variable_inputs;
return request_variable_inputs;
}
////////////////////////////////////////////////////
//
// getParameter(const json& data)
//
// Helper function to extract values from input json data.
// This function are called when XVA's data are loaded. For a <double> version it returns a corresponding value from
// the json. In <idouble> version getParameter returns the same idouble variable, which is associated
// with the value from the json.
// it is important to save the inheritance, i.e. exactly the variable marked (and not its copy) should be used in further
//
// data XVA task data
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
template<typename mdouble>
inline mdouble getParameter(const json& data) {
return data.get<double>();
}
template<>
inline idouble getParameter<idouble>(const json& data) {
if (data.is_number()) {
return data.get<double>();
}
return getArgumentsMap()[data.get<std::string>()].first;
}
////////////////////////////////////////////////////
//
// createPWCurve()
//
// Creates PiecewiseLinearCurve using json representation.
//
// json data:
// "T" double Curve expiry in years
// "step" double Time discretization step
// "flat_rate" double Flat rate for the curve
// "bump_index" int If present, apply bump to bucket
// "bump_size" double Bump size
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
template<typename mdouble>
inline PiecewiseLinearCurve<mdouble> createPWCurve(const json& data) {
std::vector<double> times;
std::vector<mdouble> values;
double step = data["step"].get<double>();
double max_t = data["T"].get<double>();
mdouble flat_rate = getParameter<mdouble>(data["flat_rate"]);
double t = 0;
while (t < max_t) {
values.push_back(flat_rate);
times.push_back(t);
t+= step;
}
if (data.contains("bump_index")) {
int index(data["bump_index"].get<int>());
double bump_size=data["bump_size"].get<double>();
if (index < values.size()) values[index] += bump_size;
}
return PiecewiseLinearCurve<mdouble>(times, values);
}
////////////////////////////////////////////////////
//
// createDiscountCurve(const json& data, const qtime t0)
//
// Creates LinearInterpDiscountCurve (company_survival_curve, ctrpary_survival_curve) using json data
//
// json data:
// "step" int step of discretization
// "T" int Maximal time
// "flat_rate" double flat rate
// t0 initial time
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
template<typename mdouble>
inline LinearInterpDiscountCurve<mdouble> createDiscountCurve(const json& data, const qtime t0) {
std::vector<qtime> times;
std::vector<mdouble> values;
qtime step = data["step"].get<int>();
qtime max_t = data["T"].get<int>();
mdouble rate = getParameter<mdouble>(data["flat_rate"]);
qtime t = t0;
while (t < max_t) {
times.push_back(t);
values.push_back(rate);
t+= step;
}
if (data.contains("bump_index")) {
int index(data["bump_index"].get<int>());
double bump_size=data["bump_size"].get<double>();
if (index < values.size()) values[index] += bump_size;
}
return LinearInterpDiscountCurve<mdouble>(values, times, t0);
}
////////////////////////////////////////////////////
//
// readModelAndPricingTimes(
// std::vector<int>& model_times, std::vector<bool>& is_pricing,
// std::vector<int>& pricing_times, const json& data, const qtime t0
// )
//
// Fill vectors model_times, is_pricing and pricing_times
//
// model_times process time discretization points
// is_pricing indicates if process time is a pricing point
// pricing_times times where portfolio should be priced
// t0 initial time
// json data: parameters to create synthetic data
// "T" int maximal time
// "step" int step of discretization
// "PricingFreq" int interval between pricing times
//
////////////////////////////////////////////////////
inline void readModelAndPricingTimes(
std::vector<int>& model_times,
std::vector<bool>& is_pricing,
std::vector<int>& pricing_times,
const json& data,
const qtime t0
) {
int max_t(data["T"].get<int>());
int model_step(data["step"].get<int>());
int pricing_freq(data["PricingFreq"].get<int>());
int pr(0);
int t=t0;
while (t < max_t ) {
model_times.push_back(t);
if (pr == 0 || (t + model_step > max_t)) {
pricing_times.push_back(t);
pr = pricing_freq;
is_pricing.push_back(true);
} else {
is_pricing.push_back(false);
}
--pr;
t += model_step;
}
}
////////////////////////////////////////////////////
//
// generateFloatLeg(const qtime t0, const int& numPeriods, std::mt19937_64& gen)
//
// Creates FloatLeg with synthetic set of cashflows
//
// t0 initial time
// numPeriods number of cash flows
// gen random numbers generator
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
template<class mdouble>
inline std::shared_ptr<FloatLeg<mdouble>> generateFloatLeg(const qtime t0, const int& numPeriods, std::mt19937_64& gen) {
std::vector<double> notionals;
std::vector<qtime> start_times;
std::vector<qtime> end_times;
std::uniform_real_distribution<> uniform_distrib(0, 1);
std::uniform_int_distribution<int> unif_int_distrib(0, 2);
qtime start(t0+int(10 * 365 * uniform_distrib(gen)));
int period(int(2 * 365 * uniform_distrib(gen) + 30));
for (int ti = 0; ti < numPeriods; ++ti) {
start_times.push_back(start); start += period;
end_times.push_back(start);
notionals.push_back(2*uniform_distrib(gen) - 0.985);
}
return std::make_shared<FloatLeg<mdouble>>(notionals, start_times, end_times, end_times, unif_int_distrib(gen));
}
////////////////////////////////////////////////////
//
// generateFixedLeg(const qtime t0, const int& numPeriods, std::mt19937_64& gen)
//
// Creates FixedLeg with synthetic set of cashflows
//
// t0 initial time
// numPeriods number of cash flows
// gen random numbers generator
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
template<class mdouble>
inline std::shared_ptr<FixedLeg<mdouble>> generateFixedLeg(const qtime t0, const int& numPeriods, std::mt19937_64& gen) {
std::vector<double> notionals;
std::vector<qtime> start_times;
std::vector<qtime> end_times;
std::uniform_real_distribution<> uniform_distrib(0, 1);
qtime start(t0+int(10 * 365 * uniform_distrib(gen)));
int period(int(2 * 365 * uniform_distrib(gen) + 30));
for (int ti = 0; ti < numPeriods; ++ti) {
start_times.push_back(start); start += period;
end_times.push_back(start);
notionals.push_back(0.15 * (uniform_distrib(gen) * 2.0 - 1.0));
}
return std::make_shared<FixedLeg<mdouble>>(notionals, end_times);
}
////////////////////////////////////////////////////
//
// readPortfolio(
// std::vector<std::shared_ptr<FixedLeg<mdouble>>>& fixed_legs,
// std::vector<std::shared_ptr<FloatLeg<mdouble>>>& float_legs,
// const json& portfolio, qtime t0
// )
//
// Fill vectors of FixedLeg and FloatLeg
//
// fixed_legs vector of fixed legs
// float_legs vector of float legs
// json portfolio data:
// "NumRandomTrades" int Number of legs
// "NumPeriods" int Number of cash flows for each leg
// t0 initial time
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
template<typename mdouble>
inline void readPortfolio(
std::vector<std::shared_ptr<FixedLeg<mdouble>>>& fixed_legs,
std::vector<std::shared_ptr<FloatLeg<mdouble>>>& float_legs,
const json& portfolio,
const qtime t0
) {
std::mt19937_64 gen(17);
int num_trades(portfolio["NumRandomTrades"].get<int>());
int num_periods(portfolio["NumPeriods"].get<int>());
for (int ti = 0; ti < num_trades; ++ti) {
fixed_legs.push_back(generateFixedLeg<mdouble>(t0, num_periods, gen));
float_legs.push_back(generateFloatLeg<mdouble>(t0, num_periods, gen));
}
}
////////////////////////////////////////////////////
//
// generateRandomCurve(qtime t0, std::vector<qtime>& tenors, std::mt19937_64& gen)
//
// Creates LinearInterpDiscountCurve
//
// t0 initial time
// tenors interpolation points
// gen random numbers generator
//
// <mdouble>: double, aadc:idouble, adept::adouble
//
////////////////////////////////////////////////////
inline LinearInterpDiscountCurve<mdouble> generateRandomCurve(const qtime t0, const std::vector<qtime>& tenors, std::mt19937_64& gen) {
std::uniform_real_distribution<> uniform_distrib(0, 1);
std::vector<mdouble> zr(tenors.size());
double db = 1.0;
for (int i = 0; i < zr.size(); ++i) {
double fwd = uniform_distrib(gen) * 0.15;
db *= std::exp(-fwd * qYearFrac(i > 0 ? tenors[i - 1] : t0, tenors[i]));
zr[i] = -log(db) / qYearFrac(t0, tenors[i]);
}
return LinearInterpDiscountCurve<mdouble>(zr, tenors, t0);
}