24#include <ql/cashflows/coupon.hpp>
25#include <ql/pricingengines/bond/bondfunctions.hpp>
26#include <ql/quotes/simplequote.hpp>
27#include <ql/timegrid.hpp>
36 const std::string& baseCcy,
const std::map<std::string, Handle<YieldTermStructure>>& discountCurves,
37 const std::map<std::string, Handle<Quote>>& fxSpots,
const QuantLib::ext::shared_ptr<QuantExt::LinearGaussMarkovModel>& model,
38 const Real sy,
const Size ny,
const Real sx,
const Size nx,
const Handle<YieldTermStructure>& treasuryCurve,
39 const Handle<DefaultProbabilityTermStructure>& defaultCurve,
const Handle<Quote>& recoveryRate,
40 const Size timeStepsPerYear)
42 fxSpots_(fxSpots), treasuryCurve_(treasuryCurve), defaultCurve_(defaultCurve), recoveryRate_(recoveryRate),
43 timeStepsPerYear_(timeStepsPerYear) {
46 registerWith(d.second);
48 registerWith(s.second);
49 fxSpots_[baseCcy] = Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(1.0));
58 "RiskParticipationAgreementEngineTLock::calculate(): empty discount curve for base ccy " <<
baseCcy_);
59 QL_REQUIRE(!
defaultCurve_.empty(),
"RiskParticipationAgreementEngineTLock::calculate(): empty default curve");
61 "RiskParticipationAgreementEngineTLock::calculate(): empty recovery and trade does not specify "
70 Real effectiveRecoveryRate =
77 for (
auto const& l :
arguments_.protectionFee) {
78 for (
auto const& c : l) {
82 "RiskParticipationAgreementEngineTLock::calculate(): empty discount curve for ccy "
85 "RiskParticipationAgreementEngineTLock::calculate(): empty fx spot for ccy pair "
91 auto cpn = QuantLib::ext::dynamic_pointer_cast<Coupon>(c);
94 Date end = cpn->accrualEndDate();
96 Date mid = start + (end - start) / 2;
110 Real protection = 0.0;
121 protection = optionPv *
defaultCurve_->defaultProbability(0.0, t) * (1.0 - effectiveRecoveryRate) *
133 std::max<Size>(1, std::lround(
static_cast<Real
>(std::max<Size>(
timeStepsPerYear_, 1)) * t_term + 0.5));
135 TimeGrid grid(t_term, effTimeSteps);
138 std::vector<Real> optionPv(grid.size(), 0.0);
145 for (Size i = grid.size() - 1; i > 0; --i) {
146 Real t_from = grid[i];
147 Real t_to = grid[i - 1];
149 underlyingPv =
rollback(underlyingPv, t_from, t_to);
155 for (Size i = 0; i < grid.size(); ++i) {
158 t0 = 0.5 * (grid[i - 1] + grid[i]);
161 if (i < grid.size() - 1) {
162 t1 = 0.5 * (grid[i] + grid[i + 1]);
165 if (t1 > t0 && !QuantLib::close_enough(t0, t1)) {
167 optionPv[i] *
defaultCurve_->defaultProbability(t0, t1) * (1.0 - effectiveRecoveryRate);
190 BondFunctions::yield(*
arguments_.bond, price,
arguments_.dayCounter, Compounded, Semiannual, settlement);
191 Real dv01 = price / 100.0 *
193 Duration::Modified, settlement);
207 for (Size i = 0; i < states.size(); ++i) {
209 modelCurve.
state(states[i]);
212 Real price = BondFunctions::cleanPrice(*
arguments_.bond, modelCurve, settlement);
214 BondFunctions::yield(*
arguments_.bond, price,
arguments_.dayCounter, Compounded, Semiannual, settlement);
215 Real dv01 = price / 100.0 *
217 Duration::Modified, settlement);
219 result.
set(i, multiplier * (
arguments_.referenceRate - yield) * dv01 *
const Instrument::results * results_
const boost::shared_ptr< LinearGaussMarkovModel > & model() const
RandomVariable rollback(const RandomVariable &v, const Real t1, const Real t0) const
RandomVariable stateGrid(const Real t) const
void move(const Date &d, const Real s)
void calculate() const override
std::map< std::string, Handle< Quote > > fxSpots_
std::map< std::string, Handle< YieldTermStructure > > discountCurves_
NumericLgmRiskParticipationAgreementEngineTLock(const std::string &baseCcy, const std::map< std::string, Handle< YieldTermStructure > > &discountCurves, const std::map< std::string, Handle< Quote > > &fxSpots, const QuantLib::ext::shared_ptr< QuantExt::LinearGaussMarkovModel > &model, const Real sy, const Size ny, const Real sx, const Size nx, const Handle< YieldTermStructure > &treasuryCurve, const Handle< DefaultProbabilityTermStructure > &defaultCurve, const Handle< Quote > &recoveryRate, const Size timeStepsPerYear)
Handle< YieldTermStructure > treasuryCurve_
Handle< DefaultProbabilityTermStructure > defaultCurve_
QuantExt::RandomVariable computePayoff() const
Handle< Quote > recoveryRate_
RandomVariable max(RandomVariable x, const RandomVariable &y)
RandomVariable expectation(const RandomVariable &r)
Serializable Credit Default Swap.
Real at(const Size i) const
void set(const Size i, const Real v)
Swap::arguments * arguments_