Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
currencyswap.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2019 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
26
28
29namespace ore {
30namespace data {
31
32using namespace QuantLib;
33using namespace QuantExt;
34
35namespace {
36struct CcyComp {
37 bool operator()(const Currency& c1, const Currency& c2) const { return c1.code() < c2.code(); }
38};
39} // namespace
40
41QuantLib::ext::shared_ptr<PricingEngine> CamAmcCurrencySwapEngineBuilder::engineImpl(const std::vector<Currency>& ccys,
42 const Currency& base) {
43
44 std::set<Currency, CcyComp> allCurrencies(ccys.begin(), ccys.end());
45 allCurrencies.insert(base);
46
47 std::string ccysStr = base.code();
48 for (auto const& c : ccys) {
49 ccysStr += "_" + c.code();
50 }
51
52 DLOG("Building multi leg option engine for ccys " << ccysStr << " (from externally given CAM)");
53
54 QL_REQUIRE(!ccys.empty(), "CamMcMultiLegOptionEngineBuilder: no currencies given");
55
56 std::vector<Size> externalModelIndices;
57 std::vector<Handle<YieldTermStructure>> discountCurves;
58 std::vector<Size> cIdx;
59 std::vector<QuantLib::ext::shared_ptr<IrModel>> lgm;
60 std::vector<QuantLib::ext::shared_ptr<FxBsParametrization>> fx;
61
62 // base ccy is the base ccy of the external cam by definition
63 // but in case we only have one currency, we don't need this
64 bool needBaseCcy = allCurrencies.size() > 1;
65
66 // add the IR and FX components in the order they appear in the CAM; this way
67 // we can sort the external model indices and be sure that they match up with
68 // the indices 0,1,2,3,... of the projected model we build here
69 for (Size i = 0; i < cam_->components(CrossAssetModel::AssetType::IR); ++i) {
70 if ((i == 0 && needBaseCcy) || std::find(allCurrencies.begin(), allCurrencies.end(),
71 cam_->irlgm1f(i)->currency()) != allCurrencies.end()) {
72 lgm.push_back(cam_->lgm(i));
73 externalModelIndices.push_back(cam_->pIdx(CrossAssetModel::AssetType::IR, i));
74 cIdx.push_back(cam_->cIdx(CrossAssetModel::AssetType::IR, i));
75 if (i > 0) {
76 fx.push_back(cam_->fxbs(i - 1));
77 externalModelIndices.push_back(cam_->pIdx(CrossAssetModel::AssetType::FX, i - 1));
78 cIdx.push_back(cam_->cIdx(CrossAssetModel::AssetType::FX, i - 1));
79 }
80 }
81 }
82
83 std::sort(externalModelIndices.begin(), externalModelIndices.end());
84 std::sort(cIdx.begin(), cIdx.end());
85
86 // build correlation matrix
87 Matrix corr(cIdx.size(), cIdx.size(), 1.0);
88 for (Size i = 0; i < cIdx.size(); ++i) {
89 for (Size j = 0; j < i; ++j) {
90 corr(i, j) = corr(j, i) = cam_->correlation()(cIdx[i], cIdx[j]);
91 }
92 }
93
94 Handle<CrossAssetModel> model(QuantLib::ext::make_shared<CrossAssetModel>(lgm, fx, corr));
95
96 // we assume that the model has the pricing discount curves attached already, so
97 // we leave the discountCurves vector empty here
98
99 // build the pricing engine
100
101 auto engine = QuantLib::ext::make_shared<McCamCurrencySwapEngine>(
102 model, ccys, base, parseSequenceType(engineParameter("Training.Sequence")),
103 parseSequenceType(engineParameter("Pricing.Sequence")), parseInteger(engineParameter("Training.Samples")),
104 parseInteger(engineParameter("Pricing.Samples")), parseInteger(engineParameter("Training.Seed")),
105 parseInteger(engineParameter("Pricing.Seed")), parseInteger(engineParameter("Training.BasisFunctionOrder")),
106 parsePolynomType(engineParameter("Training.BasisFunction")),
107 parseSobolBrownianGeneratorOrdering(engineParameter("BrownianBridgeOrdering")),
108 parseSobolRsgDirectionIntegers(engineParameter("SobolDirectionIntegers")), discountCurves, simulationDates_,
109 externalModelIndices, parseBool(engineParameter("MinObsDate")),
110 parseRegressorModel(engineParameter("RegressorModel", {}, false, "Simple")),
111 parseRealOrNull(engineParameter("RegressionVarianceCutoff", {}, false, std::string())));
112
113 return engine;
114}
115
116} // namespace data
117} // namespace ore
const std::vector< Date > simulationDates_
const QuantLib::ext::shared_ptr< QuantExt::CrossAssetModel > cam_
QuantLib::ext::shared_ptr< PricingEngine > engineImpl(const std::vector< Currency > &ccys, const Currency &base) override
const string & engine() const
Return the engine name.
const string & model() const
Return the model name.
std::string engineParameter(const std::string &p, const std::vector< std::string > &qualifiers={}, const bool mandatory=true, const std::string &defaultValue="") const
Build a cross asset model.
Cross asset model data.
FX component data for the cross asset model.
SequenceType parseSequenceType(const std::string &s)
Convert string to sequence type.
Definition: parsers.cpp:668
QuantLib::LsmBasisSystem::PolynomialType parsePolynomType(const std::string &s)
Convert text to QuantLib::LsmBasisSystem::PolynomialType.
Definition: parsers.cpp:527
bool parseBool(const string &s)
Convert text to bool.
Definition: parsers.cpp:144
SobolRsg::DirectionIntegers parseSobolRsgDirectionIntegers(const std::string &s)
Convert text to QuantLib::SobolRsg::DirectionIntegers.
Definition: parsers.cpp:579
Real parseRealOrNull(const string &s)
Convert text to Real, empty string to Null<Real>()
Definition: parsers.cpp:120
QuantExt::McMultiLegBaseEngine::RegressorModel parseRegressorModel(const std::string &s)
Convert text to QuantExt::McMultiLegBaseEngine::RegressorModel.
Definition: parsers.cpp:1418
SobolBrownianGenerator::Ordering parseSobolBrownianGeneratorOrdering(const std::string &s)
Convert text to QuantLib::SobolBrownianGenerator::Ordering.
Definition: parsers.cpp:567
Integer parseInteger(const string &s)
Convert text to QuantLib::Integer.
Definition: parsers.cpp:136
Linear Gauss Markov model data.
Classes and functions for log message handling.
@ data
Definition: log.hpp:77
#define DLOG(text)
Logging Macro (Level = Debug)
Definition: log.hpp:554
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string conversion utilities