29#include <ql/math/comparison.hpp>
30#include <ql/quotes/simplequote.hpp>
38 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<InterestRateIndex>>>& irIndices,
39 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<ZeroInflationIndex>>>& infIndices,
40 const std::vector<std::string>& indices,
const std::vector<std::string>& indexCurrencies,
43 simulationDates_(simulationDates), iborFallbackConfig_(iborFallbackConfig) {
47 for (
auto const& str : indices) {
51 for (
auto const& p : irIndices) {
55 for (
auto const& p : infIndices) {
66 QL_REQUIRE(!c.empty(),
"empty currency string");
70 for (Size i = 0; i <
indices_.size(); ++i) {
73 "fx index domestic currency (" <<
indices_[i].fx()->targetCurrency().code()
74 <<
") does not match base currency (" <<
currencies_.front()
77 "fx index foreign currency (" <<
indices_[i].fx()->sourceCurrency().code()
81 "fx index foreign currency (" <<
indexCurrencies_[i] <<
") not found in model currencies");
88 registerWith(i.second);
91 registerWith(i.second);
96 registerWith(i.index(d));
98 registerWith(i.index());
107std::size_t
ModelCGImpl::pay(
const std::size_t amount,
const Date& obsdate,
const Date& paydate,
108 const std::string& currency)
const {
120 QL_REQUIRE(c !=
currencies_.end(),
"currency " << currency <<
" not handled");
125 std::size_t fxSpot = 0;
147 g_->setVariable(
id, n);
156 QL_REQUIRE(c !=
currencies_.end(),
"currency " << currency <<
" not handled");
163 comp(
const std::string& indexInput) :
indexInput_(indexInput) {}
164 template <
typename T>
bool operator()(
const std::pair<
IndexInfo, QuantLib::ext::shared_ptr<T>>& p)
const {
172 const QuantLib::ext::shared_ptr<ZeroInflationIndex>& infIndex,
173 const Size indexNo,
const Date& limDate,
const Date& obsdate,
174 const Date& fwddate,
const Date& baseDate)
const {
176 Real f = infIndex->timeSeries()[limDate];
180 if (f != Null<Real>() && !(limDate >
referenceDate()) && (fwddate == Null<Date>() || limDate <= obsdate)) {
183 Date effectiveObsDate = std::min(obsdate, limDate);
184 if (effectiveObsDate >= baseDate) {
186 }
else if (returnMissingFixingAsNull) {
189 QL_FAIL(
"missing " << indexInput <<
" fixing for " << QuantLib::io::iso_date(limDate) <<
" (obsdate="
190 << QuantLib::io::iso_date(obsdate) <<
", fwddate=" << QuantLib::io::iso_date(fwddate)
191 <<
", basedate=" << QuantLib::io::iso_date(baseDate) <<
")");
197std::size_t
ModelCGImpl::eval(
const std::string& indexInput,
const Date& obsdate,
const Date& fwddate,
198 const bool returnMissingFixingAsNull,
const bool ignoreTodaysFixing)
const {
203 (ignoreTodaysFixing ?
"1" :
"0");
209 std::string index = indexInput;
213 QL_DEPRECATED_DISABLE_WARNING
214 if (indexInfo.
isInf()) {
217 "ModelCGImpl::eval(): did not find inflation index '" << indexInput <<
"' in model index list.");
218 Date baseDate = inf->second->zeroInflationTermStructure()->baseDate();
219 Date effectiveFixingDate = fwddate != Null<Date>() ? fwddate : obsdate;
220 std::pair<Date, Date> lim = inflationPeriod(effectiveFixingDate, inf->second->frequency());
221 std::size_t indexStart =
223 std::distance(
infIndices_.begin(), inf), lim.first, obsdate, fwddate, baseDate);
225 if (!indexInfo.
inf()->interpolated()) {
230 std::distance(
infIndices_.begin(), inf), lim.second + 1, obsdate,
233 std::size_t n =
cg_add(*
g_, indexStart,
235 cg_const(*
g_, (
static_cast<Real
>(effectiveFixingDate - lim.first) /
236 static_cast<Real
>(lim.second + 1 - lim.first)))));
237 g_->setVariable(
id, n);
240 QL_DEPRECATED_ENABLE_WARNING
246 if (fwddate == Null<Date>()) {
253 cg_const(*
g_, ir->second->fixing(ir->second->fixingCalendar().adjust(obsdate, Preceding)));
254 g_->setVariable(
id, n);
257 QL_FAIL(
"ir (fallback ibor) index '" << indexInput
258 <<
"' not found in ir indices list, internal error.");
262 QuantLib::ext::shared_ptr<Index> idx = indexInfo.
index(obsdate);
263 Real fixing = Null<Real>();
265 fixing = idx->fixing(idx->fixingCalendar().adjust(obsdate, Preceding));
268 if (fixing != Null<Real>()) {
270 g_->setVariable(
id, n);
275 if (returnMissingFixingAsNull) {
277 g_->setVariable(
id, n);
281 << idx->name() <<
" fixing for " << QuantLib::io::iso_date(obsdate)
282 <<
" (adjusted fixing date = "
283 << QuantLib::io::iso_date(idx->fixingCalendar().adjust(obsdate, Preceding)) <<
")");
291 QL_REQUIRE(obsdate >=
referenceDate(),
"if fwd date is given ("
292 << QuantLib::io::iso_date(fwddate) <<
"), obsdate ("
293 << QuantLib::io::iso_date(obsdate) <<
") must be >= refdate ("
301 if (indexInfo.
isIr()) {
306 << ir->second->name() <<
" fixing for (obsdate/fwddate) = ("
307 << QuantLib::io::iso_date(obsdate) <<
","
308 << QuantLib::io::iso_date(fwddate) <<
")");
309 g_->setVariable(
id, res);
318 if (indexInfo.
isFx())
319 indexInfo =
IndexInfo(
"FX-GENERIC-" + indexInfo.
fx()->sourceCurrency().code() +
"-" +
320 indexInfo.
fx()->targetCurrency().code());
329 QL_REQUIRE(indexInfo.
isFx(),
"ModelCGImpl::eval(): index " << index <<
" not handled");
332 if (indexInfo.
fx()->sourceCurrency() == indexInfo.
fx()->targetCurrency()) {
336 Size ind1 = Null<Size>(), ind2 = Null<Size>();
345 if (ind1 != Null<Size>()) {
348 if (ind2 != Null<Size>()) {
352 if (fwddate != Null<Date>()) {
355 QL_REQUIRE(ind1 !=
currencies_.end(),
"currency " << indexInfo.
fx()->sourceCurrency().code()
356 <<
" in index " << index <<
" not handled");
357 QL_REQUIRE(ind2 !=
currencies_.end(),
"currency " << indexInfo.
fx()->targetCurrency().code()
358 <<
" in index " << index <<
" not handled");
365 g_->setVariable(
id, res);
371 std::string
id =
"__fxspott0_" + forCcy +
"_" + domCcy;
376 QL_REQUIRE(c1 !=
currencies_.end(),
"currency " << forCcy <<
" not handled");
377 QL_REQUIRE(c2 !=
currencies_.end(),
"currency " << domCcy <<
" not handled");
378 Size cidx1 = std::distance(
currencies_.begin(), c1);
379 Size cidx2 = std::distance(
currencies_.begin(), c2);
385 g_->setVariable(
id, fx);
391 const std::size_t barrier,
const bool above)
const {
398 auto qlIndex = ind.
index(obsdate1);
405 if (qlIndex->fixingCalendar().isBusinessDay(d)) {
406 std::size_t f =
eval(index, d, Null<Date>(),
true);
420 TLOG(
"ignore missing fixing for " << qlIndex->name() <<
" on " << QuantLib::io::iso_date(d)
421 <<
" in ModelCGImpl::barrierProbability()");
433 std::size_t futureBarrierHit =
465 std::vector<std::pair<std::size_t, double>> res;
467 res.push_back(std::make_pair(f.first, f.second()));
481 const std::string&
id, std::function<
double(
void)> f) {
484 n =
cg_var(g,
id, ComputationGraph::VarDoesntExist::Create);
485 m.push_back(std::make_pair(n, f));
490Date
getSloppyDate(
const Date& d,
const bool sloppyDates,
const std::set<Date>& dates) {
493 auto s = std::lower_bound(dates.begin(), dates.end(), d);
494 if (s == dates.end())
495 return *dates.rbegin();
const std::string indexInput_
const std::vector< std::string > & indexCurrencies_
std::size_t variable(const std::string &name, const bool createIfNotExists=false)
QuantLib::ext::shared_ptr< Index > index(const Date &obsDate=Date()) const
QuantLib::ext::shared_ptr< ZeroInflationIndex > inf() const
QuantLib::ext::shared_ptr< FallbackIborIndex > irIborFallback(const IborFallbackConfig &iborFallbackConfig, const Date &asof=QuantLib::Date::maxDate()) const
QuantLib::ext::shared_ptr< FxIndex > fx() const
virtual const Date & referenceDate() const =0
QuantLib::ext::shared_ptr< QuantExt::ComputationGraph > g_
void calculate() const override
virtual std::size_t getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd) const =0
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::vector< std::vector< size_t > > randomVariates_
std::size_t pay(const std::size_t amount, const Date &obsdate, const Date &paydate, const std::string ¤cy) const override
Real extractT0Result(const RandomVariable &value) const override
std::size_t cgVersion() const override
std::size_t getInflationIndexFixing(const bool returnMissingFixingAsNull, const std::string &indexInput, const QuantLib::ext::shared_ptr< ZeroInflationIndex > &infIndex, const Size indexNo, const Date &limDate, const Date &obsdate, const Date &fwddate, const Date &baseDate) const
std::vector< std::pair< std::size_t, std::function< double(void)> > > & modelParameterFunctors() const override
virtual std::size_t getIrIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
std::size_t discount(const Date &obsdate, const Date &paydate, const std::string ¤cy) const override
std::vector< std::pair< std::size_t, std::function< double(void)> > > modelParameters_
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > infIndices_
virtual std::size_t getFutureBarrierProb(const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const =0
QuantLib::Date cgEvalDate_
std::size_t dt(const Date &d1, const Date &d2) const override
std::vector< IndexInfo > indices_
virtual std::size_t getFxSpot(const Size idx) const =0
ModelCGImpl(const DayCounter &dayCounter, const Size size, const std::vector< std::string > ¤cies, 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 std::set< Date > &simulationDates, const IborFallbackConfig &iborFallbackConfig)
virtual std::size_t getIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
std::size_t barrierProbability(const std::string &index, const Date &obsdate1, const Date &obsdate2, const std::size_t barrier, const bool above) const override
const DayCounter dayCounter_
virtual std::size_t getDiscount(const Size idx, const Date &s, const Date &t) const =0
virtual std::size_t getNumeraire(const Date &s) const =0
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< InterestRateIndex > > > irIndices_
std::size_t fxSpotT0(const std::string &forCcy, const std::string &domCcy) const override
std::vector< std::pair< std::size_t, double > > modelParameters() const override
const std::vector< std::vector< std::size_t > > & randomVariates() const override
const std::vector< std::string > indexCurrencies_
const IborFallbackConfig iborFallbackConfig_
const std::set< Date > simulationDates_
std::size_t eval(const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingMissingAsNull=false, const bool ignoreTodaysFixing=false) const override
SafeStack< ValueType > value
Map text representations to QuantLib/QuantExt types.
#define TLOG(text)
Logging Macro (Level = Data)
basis implementation for a script engine model
std::size_t cg_min(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_const(ComputationGraph &g, const double value)
std::size_t cg_indicatorGeq(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_subtract(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
RandomVariable expectation(const RandomVariable &r)
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_add(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_var(ComputationGraph &g, const std::string &name, const bool createIfNotExists)
std::size_t cg_div(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
std::size_t cg_indicatorGt(ComputationGraph &g, const std::size_t a, const std::size_t b, const std::string &label)
Size size(const ValueType &v)
std::string to_string(const LocationInfo &l)
std::size_t addModelParameter(ComputationGraph &g, std::vector< std::pair< std::size_t, std::function< double(void)> > > &m, const std::string &id, std::function< double(void)> f)
Date getSloppyDate(const Date &d, const bool sloppyDates, const std::set< Date > &dates)
Serializable Credit Default Swap.
Real at(const Size i) const
string conversion utilities