Skip to content

Commit 21d71ca

Browse files
mgronckijenkins
authored andcommitted
QPR-12427 code cleanup
1 parent d9d1220 commit 21d71ca

11 files changed

Lines changed: 59 additions & 50 deletions

File tree

OREAnalytics/orea/engine/amcvaluationengine.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ std::vector<QuantExt::RandomVariable>
9292
simulatePathInterface2(const QuantLib::ext::shared_ptr<AmcCalculator>& amcCalc, const std::vector<Real>& pathTimes,
9393
std::vector<std::vector<RandomVariable>>& paths, const std::vector<size_t>& pathIdx,
9494
const std::vector<size_t>& timeIdx,
95-
const bool moveStateToPreviousTime, const std::string& tradeLabel,
95+
const std::string& tradeLabel,
9696
const std::string& tradeType) {
9797
QL_REQUIRE(pathIdx.size() == timeIdx.size(),
9898
"internal error, mismatch between relevant path idx and timegrid idx, please contact dev");
9999
try {
100-
return amcCalc->simulatePath(pathTimes, paths, pathIdx, timeIdx, moveStateToPreviousTime);
100+
return amcCalc->simulatePath(pathTimes, paths, pathIdx, timeIdx);
101101
} catch (const std::exception& e) {
102102
StructuredTradeErrorMessage(tradeLabel, tradeType, "error during amc path simulation for trade.", e.what())
103103
.log();
@@ -441,7 +441,7 @@ void runCoreEngine(const QuantLib::ext::shared_ptr<ore::data::Portfolio>& portfo
441441

442442
if (!sgd->withCloseOutLag()) {
443443
// no close-out lag, fill depth 0 with npv on path
444-
auto res = simulatePathInterface2(amcCalculators[j], pathTimes, paths, allTimes, allTimes, false, tradeLabel[j],
444+
auto res = simulatePathInterface2(amcCalculators[j], pathTimes, paths, allTimes, allTimes, tradeLabel[j],
445445
tradeType[j]);
446446
Real v = outputCube->getT0(tradeId[j], 0);
447447
outputCube->setT0(v +
@@ -467,10 +467,10 @@ void runCoreEngine(const QuantLib::ext::shared_ptr<ore::data::Portfolio>& portfo
467467
if (sgd->withMporStickyDate()) {
468468
// sticky date mpor mode. simulate the valuation times...
469469
auto res = simulatePathInterface2(amcCalculators[j], pathTimes, paths, valuationTimeIdx,
470-
valuationTimeIdx, false, tradeLabel[j], tradeType[j]);
470+
valuationTimeIdx, tradeLabel[j], tradeType[j]);
471471
// ... and then the close-out times, but times moved to the valuation times
472472
auto resLag = simulatePathInterface2(amcCalculators[j], pathTimes, paths, closeOutTimeIdx,
473-
valuationTimeIdx, true, tradeLabel[j], tradeType[j]);
473+
valuationTimeIdx, tradeLabel[j], tradeType[j]);
474474
Real v = outputCube->getT0(tradeId[j], 0);
475475
outputCube->setT0(v +
476476
res[0].at(0) * fx(fxBuffer, currencyIndex[j], 0, 0) *
@@ -519,7 +519,7 @@ void runCoreEngine(const QuantLib::ext::shared_ptr<ore::data::Portfolio>& portfo
519519
}
520520
} else {
521521
// actual date mpor mode: simulate all times in one go
522-
auto res = simulatePathInterface2(amcCalculators[j], pathTimes, paths, allTimes, allTimes, false,
522+
auto res = simulatePathInterface2(amcCalculators[j], pathTimes, paths, allTimes, allTimes,
523523
tradeLabel[j], tradeType[j]);
524524
Real v = outputCube->getT0(tradeId[j], 0);
525525
outputCube->setT0(v + res[0].at(0) * fx(fxBuffer, currencyIndex[j], 0, 0) *

OREAnalytics/orea/scenario/clonedscenariogenerator.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,36 @@ namespace ore {
2424
namespace analytics {
2525

2626
ClonedScenarioGenerator::ClonedScenarioGenerator(const QuantLib::ext::shared_ptr<ScenarioGenerator>& scenarioGenerator,
27-
const std::vector<Date>& dates, const Size nSamples) : dates_(dates) {
27+
const std::vector<Date>& dates, const Size nSamples) {
2828
DLOG("Build cloned scenario generator for " << dates.size() << " dates and " << nSamples << " samples.");
29+
for (size_t i = 0; i < dates.size(); ++i) {
30+
dates_[dates[i]] = i;
31+
}
32+
firstDate_ = dates.front();
2933
scenarioGenerator->reset();
3034
scenarios_.resize(nSamples * dates_.size());
3135
for (Size i = 0; i < nSamples; ++i) {
32-
for (Size j = 0; j < dates_.size(); ++j) {
33-
scenarios_[i * dates_.size() + j] = scenarioGenerator->next(dates_[j])->clone();
34-
}
36+
for (Size j = 0; j < dates.size(); ++j) {
37+
scenarios_[i * dates.size() + j] = scenarioGenerator->next(dates[j])->clone();
38+
}
3539
}
3640
}
3741

3842
QuantLib::ext::shared_ptr<Scenario> ClonedScenarioGenerator::next(const Date& d) {
39-
if (d == dates_.front()) { // new path
40-
nDate_++;
41-
i_ = 0;
43+
if (d == firstDate_) { // new path
44+
++nSim_;
4245
}
43-
size_t currentStep = (nDate_ - 1) * dates_.size() + i_;
46+
auto stepIdx = dates_.find(d);
47+
QL_REQUIRE(stepIdx != dates_.end(), "ClonedScenarioGenerator::next(" << d << "): invalid date " << d);
48+
size_t timePos = stepIdx->second;
49+
size_t currentStep = (nSim_ - 1) * dates_.size() + timePos;
4450
QL_REQUIRE(currentStep < scenarios_.size(),
4551
"ClonedScenarioGenerator::next(" << d << "): no more scenarios stored.");
46-
if (d == dates_[i_]) {
47-
i_++;
48-
return scenarios_[currentStep];
49-
} else {
50-
auto it = std::find(dates_.begin(), dates_.end(), d);
51-
size_t pos = (nDate_ - 1) * dates_.size() + std::distance(dates_.begin(), it);
52-
QL_REQUIRE(it != dates_.end(), "ClonedScenarioGenerator::next(" << d << "): invalid date " << d);
53-
return scenarios_[pos];
54-
}
52+
return scenarios_[currentStep];
5553
}
5654

5755
void ClonedScenarioGenerator::reset() {
58-
i_ = 0;
59-
nDate_ = 0;
56+
nSim_ = 0;
6057
}
6158

6259
} // namespace analytics

OREAnalytics/orea/scenario/clonedscenariogenerator.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class ClonedScenarioGenerator : public ScenarioGenerator {
3636
virtual void reset() override;
3737

3838
private:
39-
std::vector<Date> dates_;
40-
Size nDate_ = 0;
39+
std::map<Date, size_t> dates_;
40+
Date firstDate_;
41+
Size nSim_ = 0;
4142
std::vector<QuantLib::ext::shared_ptr<Scenario>> scenarios_;
42-
Size i_ = 0;
4343
};
4444

4545
} // namespace analytics

OREData/ored/scripting/engines/scriptedinstrumentamccalculator.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,24 @@ QuantLib::Currency ScriptedInstrumentAmcCalculator::npvCurrency() { return parse
3232

3333
std::vector<QuantExt::RandomVariable> ScriptedInstrumentAmcCalculator::simulatePath(
3434
const std::vector<QuantLib::Real>& pathTimes, std::vector<std::vector<QuantExt::RandomVariable>>& paths,
35-
const std::vector<size_t>& relevantPathIndex, const std::vector<size_t>& relevantTimeIndex, const bool stickyCloseOutRun) {
35+
const std::vector<size_t>& relevantPathIndex, const std::vector<size_t>& relevantTimeIndex) {
3636

37+
QL_REQUIRE(relevantPathIndex.size() == relevantTimeIndex.size(),
38+
"ScriptedInstrumentAmcCalculator::simulatePath: Mismatch between relevantPathIndex size and "
39+
"relevantTimeIndex size, internal error");
40+
41+
bool stickyCloseOutRun = false;
42+
for (size_t i = 0; i < relevantPathIndex.size(); ++i) {
43+
if (relevantPathIndex[i] != relevantTimeIndex[i]) {
44+
stickyCloseOutRun = true;
45+
break;
46+
}
47+
}
3748
// inject the global paths into our local model, notice that this will change the size of the model
3849

3950
auto amcModel = QuantLib::ext::dynamic_pointer_cast<AmcModel>(model_);
4051
QL_REQUIRE(amcModel, "expected an AmcModel");
41-
amcModel->injectPaths(&pathTimes, &paths, &relevantPathIndex, &relevantTimeIndex, stickyCloseOutRun);
52+
amcModel->injectPaths(&pathTimes, &paths, &relevantPathIndex, &relevantTimeIndex);
4253

4354
// the rest is similar to what is done in the ScriptedInstrumentPricingEngine:
4455

@@ -52,7 +63,7 @@ std::vector<QuantExt::RandomVariable> ScriptedInstrumentAmcCalculator::simulateP
5263
// make sure we reset the injected path data after the calculation
5364
struct InjectedPathReleaser {
5465
~InjectedPathReleaser() {
55-
QuantLib::ext::dynamic_pointer_cast<AmcModel>(model)->injectPaths(nullptr, nullptr, nullptr, nullptr, false);
66+
QuantLib::ext::dynamic_pointer_cast<AmcModel>(model)->injectPaths(nullptr, nullptr, nullptr, nullptr);
5667
}
5768
QuantLib::ext::shared_ptr<Model> model;
5869
};

OREData/ored/scripting/engines/scriptedinstrumentamccalculator.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ class ScriptedInstrumentAmcCalculator : public QuantExt::AmcCalculator {
4949
std::vector<QuantExt::RandomVariable> simulatePath(const std::vector<QuantLib::Real>& pathTimes,
5050
std::vector<std::vector<QuantExt::RandomVariable>>& paths,
5151
const std::vector<size_t>& relevantPathIndex,
52-
const std::vector<size_t>& relevantTimeIndex,
53-
const bool stickyCloseOutRun) override;
52+
const std::vector<size_t>& relevantTimeIndex) override;
5453

5554
private:
5655
const std::string npv_;

OREData/ored/scripting/models/amcmodel.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ class AmcModel {
3333
virtual ~AmcModel() {}
3434
virtual void injectPaths(const std::vector<QuantLib::Real>* pathTimes,
3535
const std::vector<std::vector<QuantExt::RandomVariable>>* variates,
36-
const std::vector<size_t>* pathIndexes, const std::vector<size_t>* timeIndexes,
37-
const bool stickyCloseOutRun) = 0;
36+
const std::vector<size_t>* pathIndexes, const std::vector<size_t>* timeIndexes) = 0;
3837
};
3938

4039
} // namespace data

OREData/ored/scripting/models/gaussiancam.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,16 +690,14 @@ Size GaussianCam::trainingSamples() const { return mcParams_.trainingSamples; }
690690

691691
void GaussianCam::injectPaths(const std::vector<QuantLib::Real>* pathTimes,
692692
const std::vector<std::vector<QuantExt::RandomVariable>>* paths,
693-
const std::vector<size_t>* pathIndexes, const std::vector<size_t>* timeIndexes,
694-
const bool stickyCloseOutRun) {
693+
const std::vector<size_t>* pathIndexes, const std::vector<size_t>* timeIndexes) {
695694

696695
if (pathTimes == nullptr) {
697696
// reset injected path data
698697
injectedPathTimes_ = nullptr;
699698
injectedPaths_ = nullptr;
700699
injectedPathRelevantPathIndexes_ = nullptr;
701700
injectedPathRelevantTimeIndexes_ = nullptr;
702-
injectedPathStickyCloseOutRun_ = false; // arbitrary
703701
return;
704702
}
705703

@@ -732,7 +730,6 @@ void GaussianCam::injectPaths(const std::vector<QuantLib::Real>* pathTimes,
732730
injectedPaths_ = paths;
733731
injectedPathRelevantPathIndexes_ = pathIndexes;
734732
injectedPathRelevantTimeIndexes_ = timeIndexes;
735-
injectedPathStickyCloseOutRun_ = stickyCloseOutRun;
736733
update();
737734
}
738735

OREData/ored/scripting/models/gaussiancam.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ class GaussianCam : public ModelImpl, public AmcModel {
7575
// AMCModel interface implementation
7676
void injectPaths(const std::vector<QuantLib::Real>* pathTimes,
7777
const std::vector<std::vector<QuantExt::RandomVariable>>* paths,
78-
const std::vector<size_t>* pathIndexes, const std::vector<size_t>* timeIndexes,
79-
const bool stickyCloseOutRun) override;
78+
const std::vector<size_t>* pathIndexes, const std::vector<size_t>* timeIndexes) override;
8079

8180
private:
8281
// ModelImpl interface implementation
@@ -143,7 +142,6 @@ class GaussianCam : public ModelImpl, public AmcModel {
143142
const std::vector<std::vector<QuantExt::RandomVariable>>* injectedPaths_ = nullptr;
144143
const std::vector<size_t>* injectedPathRelevantPathIndexes_;
145144
const std::vector<size_t>* injectedPathRelevantTimeIndexes_;
146-
bool injectedPathStickyCloseOutRun_;
147145
Size overwriteModelSize_ = Null<Size>();
148146

149147
// stored regression coefficients, state size (before possible transform) and (optional) coordinate transform

QuantExt/qle/pricingengines/amccalculator.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class AmcCalculator {
4848
simulatePath(const std::vector<QuantLib::Real>& pathTimes,
4949
std::vector<std::vector<QuantExt::RandomVariable>>& paths,
5050
const std::vector<size_t>& relevantPathIndex,
51-
const std::vector<size_t>& relevantTimeIndex,
52-
const bool stickyCloseOutRun) = 0;
51+
const std::vector<size_t>& relevantTimeIndex) = 0;
5352
};
5453

5554
} // namespace QuantExt

QuantExt/qle/pricingengines/mcmultilegbaseengine.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -960,18 +960,28 @@ McMultiLegBaseEngine::MultiLegBaseAmcCalculator::MultiLegBaseAmcCalculator(
960960

961961
std::vector<QuantExt::RandomVariable> McMultiLegBaseEngine::MultiLegBaseAmcCalculator::simulatePath(
962962
const std::vector<QuantLib::Real>& pathTimes, std::vector<std::vector<QuantExt::RandomVariable>>& paths,
963-
const std::vector<size_t>& relevantPathIndex, const std::vector<size_t>& relevantTimeIndex,
964-
const bool stickyCloseOutRun) {
963+
const std::vector<size_t>& relevantPathIndex, const std::vector<size_t>& relevantTimeIndex) {
965964

966-
// check input path consistency
965+
// check input path consistency
967966

968-
QL_REQUIRE(!paths.empty(), "MultiLegBaseAmcCalculator::simulatePath(): no future path times, this is not allowed.");
967+
QL_REQUIRE(!paths.empty(),
968+
"MultiLegBaseAmcCalculator::simulatePath(): no future path times, this is not allowed.");
969969
QL_REQUIRE(pathTimes.size() == paths.size(),
970970
"MultiLegBaseAmcCalculator::simulatePath(): inconsistent pathTimes size ("
971971
<< pathTimes.size() << ") and paths size (" << paths.size() << ") - internal error.");
972972
QL_REQUIRE(relevantPathIndex.size() == xvaTimes_.size(),
973973
"MultiLegBaseAmcCalculator::simulatePath() inconsistent relevant path indexes ("
974974
<< relevantPathIndex.size() << ") and xvaTimes (" << xvaTimes_.size() << ") - internal error.");
975+
976+
bool stickyCloseOutRun = false;
977+
978+
for (size_t i = 0; i < relevantPathIndex.size(); ++i) {
979+
if (relevantPathIndex[i] != relevantTimeIndex[i]) {
980+
stickyCloseOutRun = true;
981+
break;
982+
}
983+
}
984+
975985
/* put together the relevant simulation times on the input paths and check for consistency with xva times,
976986
also put together the effective paths by filtering on relevant simulation times and model indices */
977987
std::vector<std::vector<const RandomVariable*>> effPaths(

0 commit comments

Comments
 (0)