27#include <ql/math/comparison.hpp>
28#include <ql/quotes/simplequote.hpp>
36 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<InterestRateIndex>>>& irIndices,
37 const std::vector<std::pair<std::string, QuantLib::ext::shared_ptr<ZeroInflationIndex>>>& infIndices,
38 const std::vector<std::string>& indices,
const std::vector<std::string>& indexCurrencies,
41 simulationDates_(simulationDates), iborFallbackConfig_(iborFallbackConfig) {
45 for (
auto const& str : indices) {
49 for (
auto const& p : irIndices) {
53 for (
auto const& p : infIndices) {
64 QL_REQUIRE(!c.empty(),
"empty currency string");
68 for (Size i = 0; i <
indices_.size(); ++i) {
71 "fx index domestic currency (" <<
indices_[i].fx()->targetCurrency().code()
72 <<
") does not match base currency (" <<
currencies_.front()
75 "fx index foreign currency (" <<
indices_[i].fx()->sourceCurrency().code()
79 "fx index foreign currency (" <<
indexCurrencies_[i] <<
") not found in model currencies");
86 registerWith(i.second);
89 registerWith(i.second);
94 registerWith(i.index(d));
96 registerWith(i.index());
102 const std::string& currency)
const {
109 QL_REQUIRE(c !=
currencies_.end(),
"currency " << currency <<
" not handled");
140 QL_REQUIRE(c !=
currencies_.end(),
"currency " << currency <<
" not handled");
147 comp(
const std::string& indexInput) :
indexInput_(indexInput) {}
148 template <
typename T>
bool operator()(
const std::pair<
IndexInfo, QuantLib::ext::shared_ptr<T>>& p)
const {
156 const QuantLib::ext::shared_ptr<ZeroInflationIndex>& infIndex,
157 const Size indexNo,
const Date& limDate,
const Date& obsdate,
158 const Date& fwddate,
const Date& baseDate)
const {
160 Real f = infIndex->timeSeries()[limDate];
164 if (f != Null<Real>() && !(limDate >
referenceDate()) && (fwddate == Null<Date>() || limDate <= obsdate)) {
167 Date effectiveObsDate = std::min(obsdate, limDate);
168 if (effectiveObsDate >= baseDate) {
170 }
else if (returnMissingFixingAsNull) {
173 QL_FAIL(
"missing " << indexInput <<
" fixing for " << QuantLib::io::iso_date(limDate) <<
" (obsdate="
174 << QuantLib::io::iso_date(obsdate) <<
", fwddate=" << QuantLib::io::iso_date(fwddate)
175 <<
", basedate=" << QuantLib::io::iso_date(baseDate) <<
")");
182 const bool returnMissingFixingAsNull,
const bool ignoreTodaysFixing)
const {
185 std::string index = indexInput;
189 QL_DEPRECATED_DISABLE_WARNING
191 if (indexInfo.
isInf()) {
194 "ModelImpl::eval(): did not find inflation index '" << indexInput <<
"' in model index list.");
195 Date baseDate = inf->second->zeroInflationTermStructure()->baseDate();
196 Date effectiveFixingDate = fwddate != Null<Date>() ? fwddate : obsdate;
197 std::pair<Date, Date> lim = inflationPeriod(effectiveFixingDate, inf->second->frequency());
200 std::distance(
infIndices_.begin(), inf), lim.first, obsdate, fwddate, baseDate);
202 if (!indexInfo.
inf()->interpolated()) {
205 ALOG(
"Interpolated Inflation Indices are deprecated, adjust your script to handle interpolation there");
208 std::distance(
infIndices_.begin(), inf), lim.second + 1,
209 obsdate, fwddate, baseDate);
212 (indexEnd - indexStart) *
RandomVariable(
size(),
static_cast<Real
>(effectiveFixingDate - lim.first) /
213 static_cast<Real
>(lim.second + 1 - lim.first));
215 QL_DEPRECATED_ENABLE_WARNING
221 if (fwddate == Null<Date>()) {
228 ir->second->fixing(ir->second->fixingCalendar().adjust(obsdate, Preceding)));
230 QL_FAIL(
"ir (fallback ibor) index '" << indexInput
231 <<
"' not found in ir indices list, internal error.");
235 QuantLib::ext::shared_ptr<Index> idx = indexInfo.
index(obsdate);
236 Real fixing = Null<Real>();
238 fixing = idx->fixing(idx->fixingCalendar().adjust(obsdate, Preceding));
241 if (fixing != Null<Real>())
246 if (returnMissingFixingAsNull)
250 << idx->name() <<
" fixing for " << QuantLib::io::iso_date(obsdate)
251 <<
" (adjusted fixing date = "
252 << QuantLib::io::iso_date(idx->fixingCalendar().adjust(obsdate, Preceding)) <<
")");
260 QL_REQUIRE(obsdate >=
referenceDate(),
"if fwd date is given ("
261 << QuantLib::io::iso_date(fwddate) <<
"), obsdate ("
262 << QuantLib::io::iso_date(obsdate) <<
") must be >= refdate ("
270 if (indexInfo.
isIr()) {
274 QL_REQUIRE(res.
initialised(),
"internal error: could not project "
275 << ir->second->name() <<
" fixing for (obsdate/fwddate) = ("
276 << QuantLib::io::iso_date(obsdate) <<
","
277 << QuantLib::io::iso_date(fwddate) <<
")");
286 if (indexInfo.
isFx())
287 indexInfo =
IndexInfo(
"FX-GENERIC-" + indexInfo.
fx()->sourceCurrency().code() +
"-" +
288 indexInfo.
fx()->targetCurrency().code());
297 QL_REQUIRE(indexInfo.
isFx(),
"ModelImpl::eval(): index " << index <<
" not handled");
300 if (indexInfo.
fx()->sourceCurrency() == indexInfo.
fx()->targetCurrency()) {
304 Size ind1 = Null<Size>(), ind2 = Null<Size>();
313 if (ind1 != Null<Size>()) {
316 if (ind2 != Null<Size>()) {
320 if (fwddate != Null<Date>()) {
323 QL_REQUIRE(ind1 !=
currencies_.end(),
"currency " << indexInfo.
fx()->sourceCurrency().code()
324 <<
" in index " << index <<
" not handled");
325 QL_REQUIRE(ind2 !=
currencies_.end(),
"currency " << indexInfo.
fx()->targetCurrency().code()
326 <<
" in index " << index <<
" not handled");
339 QL_REQUIRE(c1 !=
currencies_.end(),
"currency " << forCcy <<
" not handled");
340 QL_REQUIRE(c2 !=
currencies_.end(),
"currency " << domCcy <<
" not handled");
341 Size cidx1 = std::distance(
currencies_.begin(), c1);
342 Size cidx2 = std::distance(
currencies_.begin(), c2);
359 auto qlIndex = ind.
index(obsdate1);
366 if (qlIndex->fixingCalendar().isBusinessDay(d)) {
370 barrierHit = barrierHit || f >= barrier;
372 barrierHit = barrierHit || f <= barrier;
378 TLOG(
"ignore missing fixing for " << qlIndex->name() <<
" on " << QuantLib::io::iso_date(d)
379 <<
" in ModelImpl::barrierProbability()");
387 return barrierHitAsNumber;
const std::string indexInput_
const std::vector< std::string > & indexCurrencies_
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
virtual Size size() const
virtual RandomVariable getInfIndexValue(const Size indexNo, const Date &d, const Date &fwd) const =0
const std::vector< std::string > currencies_
Real extractT0Result(const RandomVariable &value) const override
Real fxSpotT0(const std::string &forCcy, const std::string &domCcy) const override
RandomVariable barrierProbability(const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const override
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< ZeroInflationIndex > > > infIndices_
ModelImpl(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)
RandomVariable discount(const Date &obsdate, const Date &paydate, const std::string ¤cy) const override
virtual RandomVariable getFutureBarrierProb(const std::string &index, const Date &obsdate1, const Date &obsdate2, const RandomVariable &barrier, const bool above) const =0
std::vector< IndexInfo > indices_
RandomVariable 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
virtual Real getFxSpot(const Size idx) const =0
virtual RandomVariable getDiscount(const Size idx, const Date &s, const Date &t) const =0
RandomVariable eval(const std::string &index, const Date &obsdate, const Date &fwddate, const bool returnMissingMissingAsNull=false, const bool ignoreTodaysFixing=false) const override
std::vector< std::pair< IndexInfo, QuantLib::ext::shared_ptr< InterestRateIndex > > > irIndices_
virtual RandomVariable getIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
RandomVariable pay(const RandomVariable &amount, const Date &obsdate, const Date &paydate, const std::string ¤cy) const override
virtual RandomVariable getNumeraire(const Date &s) const =0
const std::vector< std::string > indexCurrencies_
const IborFallbackConfig iborFallbackConfig_
const std::set< Date > simulationDates_
virtual RandomVariable getIrIndexValue(const Size indexNo, const Date &d, const Date &fwd=Null< Date >()) const =0
SafeStack< ValueType > value
Map text representations to QuantLib/QuantExt types.
#define ALOG(text)
Logging Macro (Level = Alert)
#define TLOG(text)
Logging Macro (Level = Data)
basis implementation for a script engine model
RandomVariable expectation(const RandomVariable &r)
RandomVariable applyInverseFilter(RandomVariable x, const Filter &f)
Size size(const ValueType &v)
Serializable Credit Default Swap.
Real at(const Size i) const