Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
projectedcrossassetmodel.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 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
20
21namespace QuantExt {
22
23QuantLib::ext::shared_ptr<CrossAssetModel>
24getProjectedCrossAssetModel(const QuantLib::ext::shared_ptr<CrossAssetModel>& model,
25 const std::vector<std::pair<CrossAssetModel::AssetType, Size>>& selectedComponents,
26 std::vector<Size>& projectedStateProcessIndices) {
27
28 projectedStateProcessIndices.clear();
29
30 // vectors holding the selected parametrizations and associated indices in the correlation matrix
31
32 std::vector<QuantLib::ext::shared_ptr<Parametrization>> parametrizations;
33 std::vector<Size> correlationIndices;
34
35 // loop over selected components and fill
36 // - parametrizations
37 // - correlation indices
38 // - state process indices
39
40 for (auto const& c : selectedComponents) {
41 parametrizations.push_back(model->parametrizations().at(model->idx(c.first, c.second)));
42 for (Size b = 0; b < model->brownians(c.first, c.second); ++b)
43 correlationIndices.push_back(model->cIdx(c.first, c.second, b));
44 for (Size p = 0; p < model->stateVariables(c.first, c.second); ++p)
45 projectedStateProcessIndices.push_back(model->pIdx(c.first, c.second, p));
46 }
47
48 // build correlation matrix
49
50 Matrix correlation(correlationIndices.size(), correlationIndices.size());
51 for (Size j = 0; j < correlationIndices.size(); ++j) {
52 correlation(j, j) = 1.0;
53 for (Size k = 0; k < j; ++k) {
54 correlation(k, j) = correlation(j, k) =
55 model->correlation()(correlationIndices.at(j), correlationIndices.at(k));
56 }
57 }
58
59 // build projected cam and return it
60
61 return QuantLib::ext::make_shared<CrossAssetModel>(parametrizations, correlation, model->salvagingAlgorithm(),
62 model->measure(), model->discretization());
63}
64
65std::vector<Size> getStateProcessProjection(const QuantLib::ext::shared_ptr<CrossAssetModel>& model,
66 const QuantLib::ext::shared_ptr<CrossAssetModel>& projectedModel) {
67
68 std::vector<Size> stateProcessProjection(projectedModel->stateProcess()->size(), Null<Size>());
69
70 for (Size i = 0; i < model->components(CrossAssetModel::AssetType::IR); ++i) {
71 for (Size j = 0; j < projectedModel->components(CrossAssetModel::AssetType::IR); ++j) {
72 if (projectedModel->ir(j)->currency() == model->ir(i)->currency()) {
73 for (Size k = 0; k < projectedModel->stateVariables(CrossAssetModel::AssetType::IR, j); ++k) {
74 stateProcessProjection[projectedModel->pIdx(CrossAssetModel::AssetType::IR, j, k)] =
75 model->pIdx(CrossAssetModel::AssetType::IR, i, k);
76 }
77 }
78 }
79 }
80
81 for (Size i = 0; i < model->components(CrossAssetModel::AssetType::FX); ++i) {
82 for (Size j = 0; j < projectedModel->components(CrossAssetModel::AssetType::FX); ++j) {
83 if (projectedModel->fx(j)->currency() == model->fx(i)->currency()) {
84 for (Size k = 0; k < projectedModel->stateVariables(CrossAssetModel::AssetType::FX, j); ++k) {
85 stateProcessProjection[projectedModel->pIdx(CrossAssetModel::AssetType::FX, j, k)] =
86 model->pIdx(CrossAssetModel::AssetType::FX, i, k);
87 }
88 }
89 }
90 }
91
92 for (Size i = 0; i < model->components(CrossAssetModel::AssetType::INF); ++i) {
93 for (Size j = 0; j < projectedModel->components(CrossAssetModel::AssetType::INF); ++j) {
94 if (projectedModel->inf(j)->name() == model->inf(i)->name()) {
95 for (Size k = 0; k < projectedModel->stateVariables(CrossAssetModel::AssetType::INF, j); ++k) {
96 stateProcessProjection[projectedModel->pIdx(CrossAssetModel::AssetType::INF, j, k)] =
97 model->pIdx(CrossAssetModel::AssetType::INF, i, k);
98 }
99 }
100 }
101 }
102
103 for (Size i = 0; i < model->components(CrossAssetModel::AssetType::CR); ++i) {
104 for (Size j = 0; j < projectedModel->components(CrossAssetModel::AssetType::CR); ++j) {
105 if (projectedModel->cr(j)->name() == model->cr(i)->name()) {
106 for (Size k = 0; k < projectedModel->stateVariables(CrossAssetModel::AssetType::CR, j); ++k) {
107 stateProcessProjection[projectedModel->pIdx(CrossAssetModel::AssetType::CR, j, k)] =
108 model->pIdx(CrossAssetModel::AssetType::CR, i, k);
109 }
110 }
111 }
112 }
113
114 for (Size i = 0; i < model->components(CrossAssetModel::AssetType::EQ); ++i) {
115 for (Size j = 0; j < projectedModel->components(CrossAssetModel::AssetType::EQ); ++j) {
116 if (projectedModel->eq(j)->name() == model->eq(i)->name()) {
117 for (Size k = 0; k < projectedModel->stateVariables(CrossAssetModel::AssetType::EQ, j); ++k) {
118 stateProcessProjection[projectedModel->pIdx(CrossAssetModel::AssetType::EQ, j, k)] =
119 model->pIdx(CrossAssetModel::AssetType::EQ, i, k);
120 }
121 }
122 }
123 }
124
125 return stateProcessProjection;
126}
127
128} // namespace QuantExt
QuantLib::ext::shared_ptr< CrossAssetModel > getProjectedCrossAssetModel(const QuantLib::ext::shared_ptr< CrossAssetModel > &model, const std::vector< std::pair< CrossAssetModel::AssetType, Size > > &selectedComponents, std::vector< Size > &projectedStateProcessIndices)
std::vector< Size > getStateProcessProjection(const QuantLib::ext::shared_ptr< CrossAssetModel > &model, const QuantLib::ext::shared_ptr< CrossAssetModel > &projectedModel)
cross asset model projection utils