31#include <ql/math/comparison.hpp>
32#include <ql/math/matrixutilities/pseudosqrt.hpp>
33#include <ql/quotes/simplequote.hpp>
42 const Handle<YieldTermStructure>& curve,
const std::string& index,
43 const std::string& indexCurrency,
const Handle<BlackScholesModelWrapper>& model,
44 const std::set<Date>& simulationDates,
46 :
BlackScholesCGBase(paths, {currency}, {curve}, {}, {}, {}, {index}, {indexCurrency}, model, {}, simulationDates,
47 iborFallbackConfig) {}
50 const Size paths,
const std::vector<std::string>& currencies,
const std::vector<Handle<YieldTermStructure>>& curves,
51 const std::vector<Handle<Quote>>& fxSpots,
52 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<InterestRateIndex>>>& irIndices,
53 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<ZeroInflationIndex>>>& infIndices,
54 const std::vector<std::string>& indices,
const std::vector<std::string>& indexCurrencies,
55 const Handle<BlackScholesModelWrapper>& model,
56 const std::map<std::pair<std::string, std::string>, Handle<QuantExt::CorrelationTermStructure>>& correlations,
58 :
ModelCGImpl(curves.at(0)->dayCounter(), paths, currencies, irIndices, infIndices, indices, indexCurrencies,
59 simulationDates, iborFallbackConfig),
64 QL_REQUIRE(!
model_.empty(),
"model is empty");
65 QL_REQUIRE(!
curves_.empty(),
"no curves given");
67 <<
") does not match number of curves ("
70 "number of currencies (" <<
currencies_.size() <<
") does not match number of fx spots ("
74 "mismatch of processes size (" <<
model_->processes().size() <<
") and number of indices ("
82 registerWith(o.second);
112 std::vector<Real> times;
114 times.push_back(
curves_.front()->timeFromReference(d));
130 Date expiry =
indices_[indexNo].comm(d)->expiryDate();
132 if (expiry != Date())
137 effFwd = std::max(effFwd, d);
142 if (effFwd != Null<Date>()) {
143 auto p =
model_->processes().at(indexNo);
156 Date effFixingDate = d;
157 if (fwd != Null<Date>())
160 effFixingDate =
irIndices_.at(indexNo).second->fixingCalendar().adjust(effFixingDate);
163 return addModelParameter(
id, [index, effFixingDate]() {
return index->fixing(effFixingDate); });
167 Date effFixingDate = d;
168 if (fwd != Null<Date>())
172 return addModelParameter(
id, [index, effFixingDate]() {
return index->fixing(effFixingDate); });
177 comp(
const std::string& indexInput) :
indexInput_(indexInput) {}
178 template <
typename T>
bool operator()(
const std::pair<
IndexInfo, QuantLib::ext::shared_ptr<T>>& p)
const {
186 const Date& start,
const Date& end,
const Real spread,
const Real gearing,
187 const Integer lookback,
const Natural rateCutoff,
const Natural fixingDays,
188 const bool includeSpread,
const Real cap,
const Real floor,
189 const bool nakedOption,
const bool localCapFloor)
const {
193 "BlackScholesCGBase::fwdCompAvg(): did not find ir index " << indexInput <<
" - this is unexpected.");
194 auto on = QuantLib::ext::dynamic_pointer_cast<OvernightIndex>(index->second);
195 QL_REQUIRE(on,
"BlackScholesCGBase::fwdCompAvg(): expected on index for " << indexInput);
197 QL_REQUIRE(cap > 999998.0 && floor < -999998.0,
198 "BlackScholesCGBase:fwdCompAvg(): cap (" << cap <<
") / floor (" << floor <<
") not supported");
199 QuantLib::ext::shared_ptr<QuantLib::FloatingRateCoupon> coupon;
200 QuantLib::ext::shared_ptr<QuantLib::FloatingRateCouponPricer> pricer;
202 coupon = QuantLib::ext::make_shared<QuantExt::AverageONIndexedCoupon>(
203 end, 1.0, start, end, on, gearing, spread, rateCutoff, on->dayCounter(), lookback * Days, fixingDays);
204 pricer = QuantLib::ext::make_shared<AverageONIndexedCouponPricer>();
206 coupon = QuantLib::ext::make_shared<QuantExt::OvernightIndexedCoupon>(end, 1.0, start, end, on, gearing, spread, Date(),
207 Date(), on->dayCounter(),
false, includeSpread,
208 lookback * Days, rateCutoff, fixingDays);
209 pricer = QuantLib::ext::make_shared<OvernightIndexedCouponPricer>();
211 coupon->setPricer(pricer);
212 std::string
id =
"__fwdCompAvg_" + std::to_string(
g_->size());
233 std::string
id =
"__fxspot_" + std::to_string(idx);
241 QL_REQUIRE(c1 !=
currencies_.end(),
"currency " << forCcy <<
" not handled");
242 QL_REQUIRE(c2 !=
currencies_.end(),
"currency " << domCcy <<
" not handled");
243 Size cidx1 = std::distance(
currencies_.begin(), c1);
244 Size cidx2 = std::distance(
currencies_.begin(), c2);
247 fx *=
fxSpots_.at(cidx1 - 1)->value();
249 fx /=
fxSpots_.at(cidx2 - 1)->value();
255 QL_REQUIRE(c !=
currencies_.end(),
"currency " << currency <<
" not handled");
257 return curves_.at(cidx)->discount(paydate);
261 const boost::optional<long>& memSlot,
const std::size_t addRegressor1,
262 const std::size_t addRegressor2)
const {
266 QL_REQUIRE(!memSlot,
"BlackScholesCGBase::npv() with memSlot not yet supported!");
276 std::vector<std::size_t> state;
284 state.push_back(addRegressor1);
286 state.push_back(addRegressor2);
const std::string indexInput_
const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > & correlations_
black scholes model base class for n underlyings (fx, equity or commodity)
void performCalculations() const override
std::map< Date, std::vector< std::size_t > > underlyingPaths_
std::size_t npv(const std::size_t amount, const Date &obsdate, const std::size_t filter, const boost::optional< long > &memSlot, const std::size_t addRegressor1, const std::size_t addRegressor2) const override
std::size_t underlyingPathsCgVersion_
std::size_t getNumeraire(const Date &s) const override
std::size_t getFxSpot(const Size idx) const override
Real getDirectFxSpotT0(const std::string &forCcy, const std::string &domCcy) const override
std::size_t getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
const Date & referenceDate() const override
Real getDirectDiscountT0(const Date &paydate, const std::string ¤cy) const override
std::set< Date > effectiveSimulationDates_
std::size_t getIrIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
BlackScholesCGBase(const Size paths, const std::vector< std::string > ¤cies, const std::vector< Handle< YieldTermStructure > > &curves, const std::vector< Handle< Quote > > &fxSpots, const std::vector< std::pair< std::string, QuantLib::ext::shared_ptr< InterestRateIndex > > > &irIndices, const std::vector< std::pair< std::string, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > &infIndices, const std::vector< std::string > &indices, const std::vector< std::string > &indexCurrencies, const Handle< BlackScholesModelWrapper > &model, const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > &correlations, const std::set< Date > &simulationDates, const IborFallbackConfig &iborFallbackConfig)
const std::vector< Handle< Quote > > fxSpots_
std::size_t fwdCompAvg(const bool isAvg, const std::string &indexInput, const Date &obsdate, const Date &start, const Date &end, const Real spread, const Real gearing, const Integer lookback, const Natural rateCutoff, const Natural fixingDays, const bool includeSpread, const Real cap, const Real floor, const bool nakedOption, const bool localCapFloor) const override
const Handle< BlackScholesModelWrapper > model_
std::size_t getDiscount(const Size idx, const Date &s, const Date &t) const override
std::size_t getIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const override
const std::map< std::pair< std::string, std::string >, Handle< QuantExt::CorrelationTermStructure > > correlations_
const std::vector< Handle< YieldTermStructure > > curves_
std::vector< Size > positionInTimeGrid_
QuantLib::ext::shared_ptr< QuantExt::ComputationGraph > g_
void calculate() const override
std::size_t addModelParameter(const std::string &id, std::function< double(void)> f) const
void performCalculations() const override
const std::vector< std::string > currencies_
std::size_t cgVersion() const override
std::size_t discount(const Date &obsdate, const Date &paydate, const std::string ¤cy) const override
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > infIndices_
std::vector< IndexInfo > indices_
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< InterestRateIndex > > > irIndices_
SafeStack< Filter > filter
const QuantLib::ext::shared_ptr< ModelCG > model_
Map text representations to QuantLib/QuantExt types.
std::size_t cg_const(ComputationGraph &g, const double value)
std::size_t cg_mult(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_conditionalExpectation(ComputationGraph &g, const std::size_t regressand, const std::vector< std::size_t > ®ressor, const std::size_t filter, const std::string &label)
std::size_t cg_div(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::string to_string(const LocationInfo &l)
Serializable Credit Default Swap.
string conversion utilities