Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
multistatenpvcalculator.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 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
19/*! \file engine/multistatenpvcalculator.hpp
20 \brief a calculator that computes npvs for a vector of credit states
21 \ingroup simulation
22*/
23
25
27
28namespace ore {
29namespace analytics {
30
31void MultiStateNPVCalculator::calculate(const QuantLib::ext::shared_ptr<Trade>& trade, Size tradeIndex,
32 const QuantLib::ext::shared_ptr<SimMarket>& simMarket,
33 QuantLib::ext::shared_ptr<NPVCube>& outputCube,
34 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet, const Date& date,
35 Size dateIndex, Size sample, bool isCloseOut) {
36 if (!isCloseOut) {
37 std::vector<Real> stateNpvs = multiStateNpv(tradeIndex, trade, simMarket);
38 for (Size i = 0; i < stateNpvs.size(); ++i) {
39 outputCube->set(stateNpvs[i], tradeIndex, dateIndex, sample, index_ + i);
40 }
41 }
42}
43
44void MultiStateNPVCalculator::calculateT0(const QuantLib::ext::shared_ptr<Trade>& trade, Size tradeIndex,
45 const QuantLib::ext::shared_ptr<SimMarket>& simMarket,
46 QuantLib::ext::shared_ptr<NPVCube>& outputCube,
47 QuantLib::ext::shared_ptr<NPVCube>& outputCubeNettingSet) {
48 std::vector<Real> stateNpvs = multiStateNpv(tradeIndex, trade, simMarket);
49 for (Size i = 0; i < stateNpvs.size(); ++i) {
50 outputCube->setT0(stateNpvs[i], tradeIndex, index_ + i);
51 }
52}
53
54std::vector<Real> MultiStateNPVCalculator::multiStateNpv(Size tradeIndex, const QuantLib::ext::shared_ptr<Trade>& trade,
55 const QuantLib::ext::shared_ptr<SimMarket>& simMarket) {
56 // handle expired trades
57
58 if (trade->instrument()->qlInstrument()->isExpired()) {
59 return std::vector<Real>(states_, 0.0);
60 }
61
62 if (auto tmp = trade->instrument()->qlInstrument()->additionalResults().find("stateNpv");
63 tmp != trade->instrument()->qlInstrument()->additionalResults().end()) {
64
65 // we have a stateNpv result
66
67 std::vector<Real> stateNpv;
68 try {
69 stateNpv = boost::any_cast<std::vector<Real>>(tmp->second);
70 } catch (const std::exception& e) {
71 QL_FAIL("unexpected type of result stateNpv: " << e.what());
72 }
73 for (auto& n : stateNpv) {
74 if (!close_enough(n, 0.0)) {
75 n *= trade->instrument()->multiplier() * trade->instrument()->multiplier2();
76 Real fx = fxRates_[tradeCcyIndex_[tradeIndex]];
77 Real numeraire = simMarket->numeraire();
78 n *= fx / numeraire;
79 }
80 }
81
82 return stateNpv;
83
84 } else {
85
86 // we do not have a stateNpv result and use the usual npv for all states
87
88 std::vector<Real> npv(states_, NPVCalculator::npv(tradeIndex, trade, simMarket));
89 return npv;
90 }
91}
92
93} // namespace analytics
94} // namespace ore
std::vector< Real > multiStateNpv(Size tradeIndex, const QuantLib::ext::shared_ptr< Trade > &trade, const QuantLib::ext::shared_ptr< SimMarket > &simMarket)
void calculateT0(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet) override
void calculate(const QuantLib::ext::shared_ptr< Trade > &trade, Size tradeIndex, const QuantLib::ext::shared_ptr< SimMarket > &simMarket, QuantLib::ext::shared_ptr< NPVCube > &outputCube, QuantLib::ext::shared_ptr< NPVCube > &outputCubeNettingSet, const Date &date, Size dateIndex, Size sample, bool isCloseOut=false) override
virtual Real npv(Size tradeIndex, const QuantLib::ext::shared_ptr< Trade > &trade, const QuantLib::ext::shared_ptr< SimMarket > &simMarket)
a calculator that computes npvs for a vector of credit states
Filter close_enough(const RandomVariable &x, const RandomVariable &y)