# Pricing Engine Design for exotic XVA simulations (using the example of FX TARFs)

In my previous post I wrote about some ideas to efficiently approximate the value of a fx exotic (a fx tarf in fact). One main motivation is to use such a fast pricing in a XVA simulation.

This post is dedicated to the design I came up with to fit the idea as accurately as possible into the existing QuantLib architecture.

The next post will then present details about the approximation scheme itself, some numerical examples comparing the approximation with a full pricing under several market and time decay scenarios, and performance tests.

Good design is of utmost importance. Even in a prosperous neighbourhood a broken window, not swiftly repaired, will soon lead to a second and after a while a degenerate area. Luckily Luigi would not allow this to happen in QuantLib city of course.

These are my thoughts on the design in general:

• we should have two pricing engines, one MC and one Proxy engine. We should not have only one engine with some additional methods to extract the approximate NPVs in a proprietary way
• the proxy engine should behave just like any other engine, e.g. the time decay should consistently be deduced from the global evaluation date, not through some special parameter, and the relevant market data should be given by standard structures
• the implementation of new instruments and pricing engines following the same idea should be easy and be based on a common interface
• the end user interface used in client code should be easy to use and foolproof
• XVA simulation is an application of the proxy engine, but there is no strict connection to this use case – you can also use the proxy engine “just” to compute npvs faster if high accuracy is not required

Or in short: There should be nothing special about the whole thing. Curb your enthusiasm, just do a plain solid job, don’t try to impress anyone. Okay. Let’s start with the instrument class.

FxTarf(const Schedule schedule,
const boost::shared_ptr<FxIndex> &index,
const Real sourceNominal,
const boost::shared_ptr<StrikedTypePayoff> &shortPositionPayoff,
const boost::shared_ptr<StrikedTypePayoff> &longPositionPayoff,
const Real target,
const CouponType couponType = capped,
const Real shortPositionGearing = 1.0,
const Real longPositionGearing = 1.0,
const Handle<Quote> accumulatedAmount = Handle<Quote>(),
const Handle<Quote> lastAmount = Handle<Quote>());


The constructor takes

• the structure’s schedule (meaning the value dates),
• the underlying fx index representing for example an ECB fx fixing (this is a new class too, because there doesn’t seem to be a fx index in QuantLib yet, but I do not go into details about that here),
• the nominal of the structure (in foreign, asset or source currency, there are many names for that)
• the counterparties’ payoff profiles, which are normally short puts and long calls, all sharing the same strike
• the target level where the structure knocks out
• the coupon type (see the previous post I linked above)
• the gearing of the two sides of the deal

The last two parameters accumulatedAmount and lastAmount are optional. If not given, the FxTarf computes the already accumulated amount reading the index’s historic fixings.

If specified on the other hand, historic fixings are ignored and the given accumulated amount is used. The lastAmount in this context is needed only in case the last fixing already occured, but the associated payment is still in the future. The reason to introduce these somewhat redundant parameters is as follows. On one hand it may be handy not to set all historic fixings for the fx index, but directly set the accumulated amount. Maybe you get the deal information from some source system and it already provides the current accumulated amount along with the deal data. More importantly, during a XVA simulation you might not want to set all fixings in the IndexManager. You can do that, if you like, but it may not be handy, because after each path you have to erase all fixings again or maybe you do not simulate each fixing date even and just want to interpolate the accumulated fixings. In any case it is just a convenience parameter. Use it or just ignore it.

For a full pricing of the tarf we can use a monte carlo engine which is constructed in the usual way, for example

boost::shared_ptr<PricingEngine> mcEngine
= MakeMcFxTarfEngine<>(gkProcess)
.withStepsPerYear(steps)
.withSamples(samples)
.withSeed(42)
.withProxy();


The parameters here are the (generalized) Black Scholes process, the time steps to simulate per year, the seed for the RNG.

The last modifier .withProxy() is the only special thing here: By default the engine will just compute an npv (and error estimate) as any other mc engine does. If the engine is set up with the proxy flag on the other hand, additional information during the simulation is collected and analyzed to produce some proxy information object that can be used later for approximate pricings. We will see in a minute how.

We need this modifier because the simulation gets slower when creating the proxy, so we should be able to switch it off.

Now we set the engine and can compute the (full) npv:

tarf.setPricingEngine(mcEngine);
std::cout << "MCEngine NPV = " << tarf.NPV() << " error estimate = "
<< tarf.errorEstimate() << std::endl;


If we just want the proxy pricing, we can of course skip the full npv calculation, but it doesn’t hurt since the npv is always produced, even if only calculating the information for the proxy engine.

To use the proxy engine we have to start with an engine which can produce this piece of information. The proxy engine is then feeded with the proxy object:

boost::shared_ptr<PricingEngine> proxyEngine =
boost::make_shared<ProxyFxTarfEngine>(tarf.proxy(),
fxQuote,
gkProcess->riskFreeRate());
tarf.setPricingEngine(proxyEngine);
std::cout << "ProxyEngine NPV = " << tarf.NPV() << std::endl;


The proxy engine is constructed with

• the proxy description produced by the mc engine, which can be retrieved from the instrument by .proxy() (this is a special result, which seems important enough not to bury it in the additional results heap — this is nothing innovative though, it’s like the bps or fair rate for swaps as an example)
• the fx quote used for the pricing – i.e. the essential market data needed for the proxy pricing
• the discount curve for the pricing (which is taken as the domestic rate curve from our black scholes process in our example client code)

In addtion the global evaluation date will determine the reference date for the valuation, just as usual.

If the instrument does not provide a proxy object (e.g. because the mc engine was told so, see above), or if the proxy object is not suitable for the proxy engine to be constructed, an exception will be thrown.

What is going on behind the scenes: I added an interface for instruments that are capable of proxy pricing:

class ProxyInstrument {
public:
//! Base class proxy descriptions for approximate pricing engines
struct ProxyDescription {
// check if proxy description is valid
virtual void validate() const = 0;
};

virtual boost::shared_ptr<ProxyDescription> proxy() const = 0;
};


The only method to implement is proxy which should return an object containing the information necessary for a compatible proxy engine to compute approximate npvs (see below for what compatible means). The information itself should be an object derived from ProxyInstrument::ProxyDescription. It has to provide a validate method that should check the provided data for consistency.

How is the fx tarf instrument implemented w.r.t. this interface:

class FxTarf : public Instrument, public ProxyInstrument {
public:
//! proxy description
struct Proxy : ProxyDescription {
struct ProxyFunction {
virtual Real operator()(const Real spot) const = 0;
};
// maximum number of open fixings
Size maxNumberOpenFixings;
// last payment date, the npvs are forward npvs w.r.t. this date
Date lastPaymentDate;
// buckets for accumulated amonut, e.g.
// 0.0, 0.1, 0.2, 0.3, 0.4 means
// [0.0,0.1) has index 0
// [0.1,0.2) has index 1
// ...
// [0.4,target] has index 4
std::vector<Real> accBucketLimits;
// proxy functions
// first index is openFixings-1
// second index is accAmountIndex
// A function F should implement
// operator()(Real spot) = npv
std::vector<std::vector<boost::shared_ptr<ProxyFunction> > > functions;
void validate() const {
QL_REQUIRE(functions.size() == maxNumberOpenFixings,
"maximum number of open fixings ("
<< maxNumberOpenFixings
<< ") must be equal to function rows ("
<< functions.size() << ")");
for (Size i = 0; i < functions.size(); ++i) {
QL_REQUIRE(functions[i].size() == accBucketLimits.size(),
"number of acc amount buckets ("
<< accBucketLimits.size()
<< ") must be equal to function columns ("
<< functions[i].size() << ") in row " << i);
}
}
};
/* ... */


This says that the specific (or one possible) proxy information of a fx tarf consists of some descriptive data, which is

• the maximum number of open (future) fixings
• the last payment date of the structure – see below
• a bucketing of the accumulated amount

which together define a segmentation for the approximating function. On each segment the approximating function is then given by a Proxy::ProxyFunction which is at this level of abstraction just an arbitrary function Real to Real, returning the npv for a given fx spot. The npv is expressed on forward basis as of the last payment date of the structure, so that the proxy engine can discount back from this latest possible pricing date to the evaluation date. Remember that this is in general (and typically) different from the one used for the mc pricing.

The validate method checks if the function matrix is filled consistently with the segmentation information.

The proxy object is part of the results class for the instrument, which is again just using the standard formalism:

class FxTarf::results : public Instrument::results {
public:
void reset();
boost::shared_ptr<FxTarf::Proxy> proxy;
};


The proxy engine expects a proxy object, which is checked to be one that is useful for engine is in the constructor, using a dynamic downcast.

ProxyFxTarfEngine(
boost::shared_ptr<ProxyInstrument::ProxyDescription> proxy,
Handle<Quote> exchangeRate, Handle<YieldTermStructure> discount)
: FxTarfEngine(discount), exchangeRate_(exchangeRate) {
registerWith(exchangeRate_);
proxy_ = boost::dynamic_pointer_cast<FxTarf::Proxy>(proxy);

QL_REQUIRE(proxy_, "no FxTarf::Proxy given");

}


The third level of specialization is in the monte carlo engine, where the specific proxy object’s function type is implemented, derived from the definitions in the FxTarf class:

template <class RNG = PseudoRandom, class S = Statistics>
class McFxTarfEngine : public FxTarfEngine,
public McSimulation<SingleVariate, RNG, S> {
public:
/*! proxy function giving a function spot => npv for one segment
(bucket accumulated amount, number of open fixings)
the function is given by two quadratic polynomials on intervals
(-\infty,cutoff] and (cutoff,\infty).
Only the ascending (long calls) or descending (long puts) branch
is used and then extrapolated flat.
*/
class QuadraticProxyFunction : public FxTarf::Proxy::ProxyFunction {
public:
QuadraticProxyFunction(Option::Type, const Real cutoff, const Real a1,
const Real b1, const Real c1, const Real a2,
const Real b2, const Real c2);
Real operator()(const Real spot) const;

private:
Option::Type type_;
const Real a1_, b1_, c1_, a2_, b2_, c2_;
const Real cutoff_;
int flatExtrapolationType1_,
flatExtrapolationType2_; // +1 = right, -1 = left
Real extrapolationPoint1_, extrapolationPoint2_;
};


At this place we finally fix the specific form of the approximating functions, which are in essence given by two quadratic polynomials. I will give more details on and a motiviation for this in the next post.

Finally it appeared useful to derive the mc and proxy engines from a common base engine, which handles some trivial boundary cases (like all fixings are done or the determination of the npv of a fixed amount that is not yet settled), so we have the hierachy

class FxTarfEngine : public FxTarf::engine {}

template <class RNG = PseudoRandom, class S = Statistics>
class McFxTarfEngine : public FxTarfEngine,
public McSimulation<SingleVariate, RNG, S> {}

class ProxyFxTarfEngine : public FxTarfEngine {}


If you are interested in the code you can look into my repository. It may already work, but I did not test everything yet. More on this next week.