Skip to content

Commit 9d31f9b

Browse files
mgronckijenkins
authored andcommitted
QPR-12427 work in progress
1 parent e1f1505 commit 9d31f9b

4 files changed

Lines changed: 75 additions & 40 deletions

File tree

OREAnalytics/orea/engine/amcvaluationengine.cpp

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,29 @@ void runCoreEngine(const boost::shared_ptr<ore::data::Portfolio>& portfolio,
407407

408408
LOG("Run simulation...");
409409

410+
for (size_t i = 0; i < sgd->getGrid()->dates().size(); i++){
411+
std::cout << i << " Date " << sgd->getGrid()->dates()[i] << std::endl;
412+
}
413+
414+
for (size_t i = 0; i < sgd->getGrid()->times().size(); i++) {
415+
std::cout << i << " time " << sgd->getGrid()->times()[i] << std::endl;
416+
}
417+
418+
for (size_t i = 0; i < sgd->getGrid()->isCloseOutDate().size(); i++) {
419+
std::cout << i << " isCloseOutDate " << sgd->getGrid()->isCloseOutDate()[i] << std::endl;
420+
}
421+
422+
for (size_t i = 0; i < sgd->getGrid()->isValuationDate().size(); i++) {
423+
std::cout << i << " isValuationDate " << sgd->getGrid()->isValuationDate()[i] << std::endl;
424+
}
425+
410426
// set up vectors indicating valuation times, close-out times and all times
411427

412428
std::vector<bool> allTimes(pathTimes.size(), true);
413429
std::vector<bool> valuationTimes(pathTimes.size()), closeOutTimes(pathTimes.size());
414430
for (Size i = 0; i < pathTimes.size(); ++i) {
431+
std::cout << pathTimes[i] << " " << sgd->getGrid()->isValuationDate()[i] << " "
432+
<< sgd->getGrid()->isCloseOutDate()[i] << std::endl;
415433
valuationTimes[i] = sgd->getGrid()->isValuationDate()[i];
416434
closeOutTimes[i] = sgd->getGrid()->isCloseOutDate()[i];
417435
}
@@ -449,12 +467,16 @@ void runCoreEngine(const boost::shared_ptr<ore::data::Portfolio>& portfolio,
449467
} else {
450468
// with close-out lag, fill depth 0 with valuation date npvs, depth 1 with (inflated) close-out npvs
451469
if (sgd->withMporStickyDate()) {
470+
std::cout << "Simulate with sticky date the valuationTimes" << std::endl;
452471
// sticky date mpor mode. simulate the valuation times...
453472
auto res = simulatePathInterface2(amcCalculators[j], pathTimes, paths, valuationTimes, false,
454473
tradeLabel[j], tradeType[j]);
455474
// ... and then the close-out times, but times moved to the valuation times
475+
std::cout << "Simulate with sticky date the closeOutTimes" << std::endl;
476+
456477
auto resLag = simulatePathInterface2(amcCalculators[j], pathTimes, paths, closeOutTimes, true,
457478
tradeLabel[j], tradeType[j]);
479+
std::cout << "Finished closeOutTime simulation" << std::endl;
458480
Real v = outputCube->getT0(tradeId[j], 0);
459481
outputCube->setT0(v +
460482
res[0].at(0) * fx(fxBuffer, currencyIndex[j], 0, 0) *
@@ -463,33 +485,39 @@ void runCoreEngine(const boost::shared_ptr<ore::data::Portfolio>& portfolio,
463485
resFee[0][0],
464486
tradeId[j], 0);
465487
int dateIndex = -1;
466-
std::map<QuantLib::Date, std::pair<double, size_t>> dateIndexCache;
488+
std::map<QuantLib::Date, std::queue<std::tuple<QuantLib::Date, double, size_t>>> closeOutDateToValuationDate;
467489
for (Size k = 0; k < sgd->getGrid()->dates().size(); ++k) {
490+
468491
Real t = sgd->getGrid()->timeGrid()[k + 1];
469-
492+
std::cout << k << " " << t << std::endl;
470493
if (sgd->getGrid()->isCloseOutDate()[k]) {
471494
Date closeOutDate = sgd->getGrid()->dates()[k];
472-
Date valuationDate = sgd->getGrid()->valuationDateFromCloseOutDate(closeOutDate);
473-
auto dateIndexIt = dateIndexCache.find(valuationDate);
474-
QL_REQUIRE(dateIndexIt != dateIndexCache.end(),
475-
"The valuation date (" << ore::data::to_string(valuationDate)
476-
<< ") needs to before the corresponding close out date ("
477-
<< ore::data::to_string(closeOutDate) << ")");
478-
auto [timeValueDate, timeIndexValueDate] = dateIndexIt->second;
495+
auto dateIndexIt = closeOutDateToValuationDate.find(closeOutDate);
496+
QL_REQUIRE(dateIndexIt != closeOutDateToValuationDate.end() && !dateIndexIt->second.empty(),
497+
"The valuation date needs to before the corresponding close out date");
498+
Date valuationDate;
499+
double valuationTime;
500+
size_t valuationIndex;
501+
std::tie(valuationDate, valuationTime, valuationIndex) = dateIndexIt->second.front();
502+
dateIndexIt->second.pop();
503+
std::cout << "Is closeOut date: " << to_string(closeOutDate) << " for valuation Date " << to_string(valuationDate) << std::endl;
479504
for (Size i = 0; i < outputCube->samples(); ++i) {
480-
Real v = outputCube->get(tradeId[j], dateIndex, i, 1);
505+
Real v = outputCube->get(tradeId[j], valuationIndex, i, 1);
481506
outputCube->set(
482507
v +
483508
resLag[dateIndex + 1][i] * fx(fxBuffer, currencyIndex[j], k + 1, i) *
484-
num(model, irStateBuffer, currencyIndex[j], k + 1, timeValueDate, i) *
509+
num(model, irStateBuffer, currencyIndex[j], k + 1, valuationTime, i) *
485510
effectiveMultiplier[j] +
486511
resFee[dateIndex + 1][i],
487-
tradeId[j], timeIndexValueDate, i, 1);
512+
tradeId[j], valuationIndex, i, 1);
488513
}
489514
}
490515
if (sgd->getGrid()->isValuationDate()[k]) {
491516
Date valuationDate = sgd->getGrid()->dates()[k];
492-
dateIndexCache[valuationDate] = std::make_pair(t, ++dateIndex);
517+
Date closeOutDate = sgd->getGrid()->closeOutDateFromValuationDate(valuationDate);
518+
std::cout << "Is valuationDate date: " << to_string(valuationDate) << " with Index " << to_string(dateIndex + 1)
519+
<< " and closeOutDate " << to_string(closeOutDate) << std::endl;
520+
closeOutDateToValuationDate[closeOutDate].push(std::make_tuple(valuationDate, t, ++dateIndex));
493521
for (Size i = 0; i < outputCube->samples(); ++i) {
494522
Real v = outputCube->get(tradeId[j], dateIndex, i, 0);
495523
outputCube->set(v +
@@ -510,32 +538,34 @@ void runCoreEngine(const boost::shared_ptr<ore::data::Portfolio>& portfolio,
510538
numRatio(model, irStateBuffer, currencyIndex[j], 0, 0.0, 0) *
511539
effectiveMultiplier[j],
512540
tradeId[j], 0);
513-
std::map<QuantLib::Date, std::pair<double, size_t>> dateIndexCache;
541+
std::map<QuantLib::Date, std::queue<std::tuple<QuantLib::Date, double, size_t>>>
542+
closeOutDateToValuationDate;
514543
int dateIndex = -1;
515544
for (Size k = 1; k < res.size(); ++k) {
516545
Real t = sgd->getGrid()->timeGrid()[k];
517546
if (sgd->getGrid()->isCloseOutDate()[k - 1]) {
518547
Date closeOutDate = sgd->getGrid()->dates()[k - 1];
519-
Date valuationDate = sgd->getGrid()->valuationDateFromCloseOutDate(closeOutDate);
520-
auto dateIndexIt = dateIndexCache.find(valuationDate);
521-
QL_REQUIRE(dateIndexIt != dateIndexCache.end(),
522-
"The valuation date (" << ore::data::to_string(valuationDate)
523-
<< ") needs to before the corresponding close out date ("
524-
<< ore::data::to_string(closeOutDate) << ")");
525-
auto [_, timeIndexValueDate] = dateIndexIt->second;
548+
auto dateIndexIt = closeOutDateToValuationDate.find(closeOutDate);
549+
QL_REQUIRE(dateIndexIt != closeOutDateToValuationDate.end() && !dateIndexIt->second.empty(),
550+
"The valuation date needs to before the corresponding close out date");
551+
Date valuationDate;
552+
double valuationTime;
553+
size_t valuationIndex;
554+
std::tie(valuationDate, valuationTime, valuationIndex) = dateIndexIt->second.front();
526555
for (Size i = 0; i < outputCube->samples(); ++i) {
527-
Real v = outputCube->get(tradeId[j], dateIndex, i, 1);
556+
Real v = outputCube->get(tradeId[j], valuationIndex, i, 1);
528557
outputCube->set(v +
529558
res[k][i] * fx(fxBuffer, currencyIndex[j], k, i) *
530559
num(model, irStateBuffer, currencyIndex[j], k, t, i) *
531560
effectiveMultiplier[j] +
532561
resFee[k][i],
533-
tradeId[j], timeIndexValueDate, i, 1);
562+
tradeId[j], valuationIndex, i, 1);
534563
}
535564
}
536565
if (sgd->getGrid()->isValuationDate()[k - 1]) {
537566
Date valuationDate = sgd->getGrid()->dates()[k - 1];
538-
dateIndexCache[valuationDate] = std::make_pair(t, ++dateIndex);
567+
Date closeOutDate = sgd->getGrid()->closeOutDateFromValuationDate(valuationDate);
568+
closeOutDateToValuationDate[closeOutDate].push(std::make_tuple(valuationDate, t, ++dateIndex));
539569
for (Size i = 0; i < outputCube->samples(); ++i) {
540570
Real v = outputCube->get(tradeId[j], dateIndex, i, 0);
541571
outputCube->set(v +

OREAnalytics/orea/engine/valuationengine.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ void ValuationEngine::buildCube(const boost::shared_ptr<data::Portfolio>& portfo
192192
double upTime = 0;
193193
++cubeDateIndex;
194194
Date valueDate = dg_->valuationDates()[i];
195-
Date closeOutDate = dg_->closeOutDates()[i];
195+
std::cout << to_string(valueDate) << std::endl;
196+
Date closeOutDate = dg_->closeOutDateFromValuationDate(valueDate);
196197
std::tie(priceTime, upTime) = populateCube(
197198
valueDate, cubeDateIndex, sample, true, false, scenarioUpdated, trades, tradeHasError, calculators,
198199
outputCube, outputCubeNettingSet, counterparties, cptyCalculators, outputCptyCube);
@@ -205,7 +206,7 @@ void ValuationEngine::buildCube(const boost::shared_ptr<data::Portfolio>& portfo
205206
updateTime += upTime;
206207
}
207208
} else {
208-
std::map<Date, size_t> valueDateIndexCache;
209+
std::map<Date, std::queue<size_t>> closeOutDateToValueDateIndex;
209210
for (Size i = 0; i < dates.size(); ++i) {
210211
Date d = dates[i];
211212
// Process auxiliary close-out dates first (may coincide with a valuation date, see below)
@@ -216,8 +217,10 @@ void ValuationEngine::buildCube(const boost::shared_ptr<data::Portfolio>& portfo
216217
if (dg_->isCloseOutDate()[i]) {
217218
double priceTime = 0;
218219
double upTime = 0;
219-
Date valueDate = dg_->valuationDateFromCloseOutDate(d);
220-
size_t valueDateIndex = valueDateIndexCache[valueDate];
220+
QL_REQUIRE(closeOutDateToValueDateIndex.count(d) == 1 && !closeOutDateToValueDateIndex[d].empty(),
221+
"Need to calculate valuation date before close out date");
222+
size_t valueDateIndex = closeOutDateToValueDateIndex[d].front();
223+
closeOutDateToValueDateIndex[d].pop();
221224
std::tie(priceTime, upTime) = populateCube(
222225
d, valueDateIndex, sample, false, false, scenarioUpdated, trades, tradeHasError, calculators,
223226
outputCube, outputCubeNettingSet, counterparties, cptyCalculators, outputCptyCube);
@@ -229,7 +232,8 @@ void ValuationEngine::buildCube(const boost::shared_ptr<data::Portfolio>& portfo
229232
double priceTime = 0;
230233
double upTime = 0;
231234
++cubeDateIndex;
232-
valueDateIndexCache[d] = cubeDateIndex;
235+
Date closeOutDate = dg_->closeOutDateFromValuationDate(d);
236+
closeOutDateToValueDateIndex[closeOutDate].push(cubeDateIndex);
233237
std::tie(priceTime, upTime) = populateCube(
234238
d, cubeDateIndex, sample, true, false, scenarioUpdated, trades, tradeHasError, calculators,
235239
outputCube, outputCubeNettingSet, counterparties, cptyCalculators, outputCptyCube);

OREData/ored/utilities/dategrid.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void DateGrid::truncate(Size len) {
205205
}
206206

207207
void DateGrid::addCloseOutDates(const QuantLib::Period& p) {
208-
closeOutToValuation_.clear();
208+
valuationCloseOutMap_.clear();
209209
if (p == QuantLib::Period(0, QuantLib::Days)) {
210210
for (Size i = 0; i < dates_.size(); ++i) {
211211
if (i == 0) {
@@ -219,9 +219,10 @@ void DateGrid::addCloseOutDates(const QuantLib::Period& p) {
219219
isValuationDate_[i] = true;
220220
}
221221
if (isCloseOutDate_[i] && i > 0)
222-
closeOutToValuation_[dates_[i]] = dates_[i - 1];
222+
valuationCloseOutMap_[dates_[i-1]] = dates_[i];
223223
}
224224
} else {
225+
std::set<QuantLib::Date> tmpCloseOutDates;
225226
std::set<QuantLib::Date> tmpDates;
226227
std::set<QuantLib::Date> tmpValueDates;
227228
for (Size i = 0; i < dates_.size(); ++i) {
@@ -230,7 +231,9 @@ void DateGrid::addCloseOutDates(const QuantLib::Period& p) {
230231
c = calendar_.adjust(dates_[i] + p);
231232
else
232233
c = calendar_.advance(dates_[i], p, Following, false);
233-
closeOutToValuation_[c] = dates_[i];
234+
tmpCloseOutDates.insert(c);
235+
std::cout << dates_[i] << " " << c << std::endl;
236+
valuationCloseOutMap_[dates_[i]] = c;
234237
tmpDates.insert(dates_[i]);
235238
tmpDates.insert(c);
236239
tmpValueDates.insert(dates_[i]);
@@ -241,7 +244,7 @@ void DateGrid::addCloseOutDates(const QuantLib::Period& p) {
241244
isValuationDate_ = std::vector<bool>(dates_.size(), true);
242245
for(size_t i = 0; i < dates_.size(); ++i){
243246
Date d = dates_[i];
244-
if(closeOutToValuation_.count(d) == 1){
247+
if (tmpCloseOutDates.count(d) == 1) {
245248
isCloseOutDate_[i] = true;
246249
}
247250
if(tmpValueDates.count(d) == 0){
@@ -299,9 +302,9 @@ QuantLib::TimeGrid DateGrid::closeOutTimeGrid() const {
299302
return TimeGrid(times.begin(), times.end());
300303
}
301304

302-
QuantLib::Date DateGrid::valuationDateFromCloseOutDate(const QuantLib::Date& closeOutDate) const {
303-
auto it = closeOutToValuation_.find(closeOutDate);
304-
QL_REQUIRE(it != closeOutToValuation_.end(), "close out date " << closeOutDate << " not found in dategrid");
305+
QuantLib::Date DateGrid::closeOutDateFromValuationDate(const QuantLib::Date& d) const {
306+
auto it = valuationCloseOutMap_.find(d);
307+
QL_REQUIRE(it != valuationCloseOutMap_.end(), "Not a valid valuationDate");
305308
return it->second;
306309
}
307310

OREData/ored/utilities/dategrid.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ class DateGrid {
101101
QuantLib::TimeGrid closeOutTimeGrid() const;
102102
//@}
103103

104-
//! Given a close out date it returns the corresponding value date
105-
QuantLib::Date valuationDateFromCloseOutDate(const QuantLib::Date& closeOutDate) const;
106-
//@}
104+
QuantLib::Date closeOutDateFromValuationDate(const QuantLib::Date& d) const;
107105

108106
//! Accessor methods
109107
const QuantLib::Date& operator[](QuantLib::Size i) const { return dates_[i]; };
@@ -116,7 +114,7 @@ class DateGrid {
116114
QuantLib::Calendar calendar_;
117115
QuantLib::DayCounter dayCounter_;
118116
std::vector<QuantLib::Date> dates_;
119-
std::map<QuantLib::Date, QuantLib::Date> closeOutToValuation_;
117+
std::map<QuantLib::Date, QuantLib::Date> valuationCloseOutMap_;
120118
std::vector<QuantLib::Period> tenors_;
121119
std::vector<QuantLib::Time> times_;
122120
QuantLib::TimeGrid timeGrid_;

0 commit comments

Comments
 (0)