Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
blackscholesmodelbuilderbase.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
21
22namespace ore {
23namespace data {
24
26 const Handle<YieldTermStructure>& curve, const QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
27 const std::set<Date>& simulationDates, const std::set<Date>& addDates, const Size timeStepsPerYear)
28 : BlackScholesModelBuilderBase(std::vector<Handle<YieldTermStructure>>{curve},
29 std::vector<QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess>>{process},
30 simulationDates, addDates, timeStepsPerYear) {}
31
33 const std::vector<Handle<YieldTermStructure>>& curves,
34 const std::vector<QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess>>& processes,
35 const std::set<Date>& simulationDates, const std::set<Date>& addDates, const Size timeStepsPerYear)
36 : curves_(curves), processes_(processes), simulationDates_(simulationDates), addDates_(addDates),
37 timeStepsPerYear_(timeStepsPerYear) {
38
39 QL_REQUIRE(!curves_.empty(), "BlackScholesModelBuilderBase: no curves given");
40
41 marketObserver_ = QuantLib::ext::make_shared<MarketObserver>();
42
43 for (auto const& c : curves_)
44 registerWith(c);
45
46 for (auto const& p : processes_) {
47 registerWith(p->blackVolatility());
48 registerWith(p->riskFreeRate());
49 registerWith(p->dividendYield());
50 marketObserver_->registerWith(p->stateVariable());
51 }
52
53 registerWith(marketObserver_);
54
55 // notify observers of all market data changes, not only when not calculated
56 alwaysForwardNotifications();
57
59 for (auto const& p : processes_) {
60 vols_.push_back(p->blackVolatility());
61 allCurves_.push_back(p->riskFreeRate());
62 allCurves_.push_back(p->dividendYield());
63 }
64}
65
67 const Handle<YieldTermStructure>& curve, const QuantLib::ext::shared_ptr<GeneralizedBlackScholesProcess>& process)
68 : BlackScholesModelBuilderBase(curve, process, {}, {}, 1) {}
69
70Handle<BlackScholesModelWrapper> BlackScholesModelBuilderBase::model() const {
71 calculate();
72 return model_;
73}
74
77 return calibrationPointsChanged(false) || marketObserver_->hasUpdated(false) || forceCalibration_;
78}
79
81 forceCalibration_ = true;
83 forceCalibration_ = false;
84}
85
87 Date referenceDate = curves_.front()->referenceDate();
90 for (auto const& d : simulationDates_) {
91 if (d >= referenceDate)
93 }
94
95 std::vector<Real> times;
96 for (auto const& d : effectiveSimulationDates_) {
97 times.push_back(curves_.front()->timeFromReference(d));
98 }
99
100 Size steps = std::max(std::lround(timeStepsPerYear_ * times.back() + 0.5), 1l);
101 discretisationTimeGrid_ = TimeGrid(times.begin(), times.end(), steps);
102}
103
105 if (requiresRecalibration()) {
106
107 // update vol and curves cache
108
110
111 // reset market observer's updated flag
112
113 marketObserver_->hasUpdated(true);
114
115 // setup model
116
117 model_.linkTo(QuantLib::ext::make_shared<BlackScholesModelWrapper>(getCalibratedProcesses(), effectiveSimulationDates_,
119
120 // notify model observers
121 model_->notifyObservers();
122 }
123}
124
126
127 // get times for curves and times / strikes for vols
128
129 std::vector<std::vector<Real>> curveTimes = getCurveTimes();
130 std::vector<std::vector<std::pair<Real, Real>>> volTimesStrikes = getVolTimesStrikes();
131
132 // build data
133
134 std::vector<std::vector<Real>> curveData;
135 for (Size i = 0; i < curveTimes.size(); ++i) {
136 curveData.push_back(std::vector<Real>());
137 for (Size j = 0; j < curveTimes[i].size(); ++j) {
138 curveData.back().push_back(allCurves_[i]->discount(curveTimes[i][j]));
139 }
140 }
141
142 std::vector<std::vector<Real>> volData;
143 for (Size i = 0; i < volTimesStrikes.size(); ++i) {
144 volData.push_back(std::vector<Real>());
145 for (Size j = 0; j < volTimesStrikes[i].size(); ++j) {
146 Real k = volTimesStrikes[i][j].second;
147 // check for null = ATM
148 if (k == Null<Real>())
149 k = atmForward(processes_[i]->x0(), processes_[i]->riskFreeRate(), processes_[i]->dividendYield(),
150 volTimesStrikes[i][j].first);
151 volData.back().push_back(vols_[i]->blackVol(volTimesStrikes[i][j].first, k));
152 }
153 }
154
155 // check if something has changed
156 return cache_.hasChanged(curveTimes, curveData, volTimesStrikes, volData, updateCache);
157}
158
159} // namespace data
160} // namespace ore
builder for an array of black scholes processes
virtual void forceRecalculate()
virtual std::vector< std::vector< Real > > getCurveTimes() const =0
std::vector< Handle< YieldTermStructure > > allCurves_
BlackScholesModelBuilderBase(const std::vector< Handle< YieldTermStructure > > &curves, const std::vector< QuantLib::ext::shared_ptr< GeneralizedBlackScholesProcess > > &processes, const std::set< Date > &simulationDates, const std::set< Date > &addDates, const Size timeStepsPerYear)
virtual std::vector< QuantLib::ext::shared_ptr< GeneralizedBlackScholesProcess > > getCalibratedProcesses() const =0
std::vector< Handle< BlackVolTermStructure > > vols_
bool calibrationPointsChanged(const bool updateCache) const
virtual std::vector< std::vector< std::pair< Real, Real > > > getVolTimesStrikes() const =0
Handle< BlackScholesModelWrapper > model() const
QuantLib::ext::shared_ptr< MarketObserver > marketObserver_
RelinkableHandle< BlackScholesModelWrapper > model_
const std::vector< QuantLib::ext::shared_ptr< GeneralizedBlackScholesProcess > > processes_
const std::vector< Handle< YieldTermStructure > > curves_
bool hasChanged(const std::vector< std::vector< Real > > &curveTimes, const std::vector< std::vector< Real > > &curveData, const std::vector< std::vector< std::pair< Real, Real > > > &volTimesStrikes, const std::vector< std::vector< Real > > &volData, const bool updateCache)
@ data
Definition: log.hpp:77
Date referenceDate
Definition: utilities.cpp:442
Shared utilities for model building and calibration.
Real atmForward(const Real s0, const Handle< YieldTermStructure > &r, const Handle< YieldTermStructure > &q, const Real t)
helper function that computes the atm forward
Definition: utilities.cpp:483
Serializable Credit Default Swap.
Definition: namespaces.docs:23
std::vector< Size > steps