Archivos Mensuales: febrero 2013

Analytical credit exposure, an example code in C++

In this post we quickly review some concepts related to counterparty risk, and we provide a C++ code computing some measures of credit exposure.

If you read Jon Gregory’s* book Counterparty credit risk and credit value adjustment: A continuing challenge for global financial markets, you should recognize that we actually ‘ported’ to C++ Jon Gregory’s spreadsheets Spreadsheet2.2 and Spreadsheet2.2b from the workbook chapter2.xls available at CVA Central.

First, let’s consider a single asset (eg an OTC option) and let:
V(t) be the value of the asset at time t,
f be the probability density function (pdf) of V(t) and
– T be the time to expiration.


MtM represents the value that could be potentially lost if a counterparty defaulted.


Replacement cost may be different from MtM (eg on illiquid markets). Nonetheless, it is standard practice to base credit exposure on the current MtM value of the given transactions.


There is an asymmetry of potential losses with respect to MtM. That is an institution loses if their MtM is positive. But, it does not gain if MtM is negative. Thus, only positive exposure is of interest as far as credit risk is concerned.

Exposure = Max(MtM, 0) = MtM+


PFE aims at characterising MtM at some point in time in the future. It is the possible exposure over a given time horizon to a given confidence level. PFE has much in common with the concept of value-at-risk.

PFE = \int_{-\infty}^{\infty}Max(x, 0)f(x)dx

In practice, credit exposure is computed by taking the sum of all positive values or replacement costs of the asset over discrete simulation horizons (n).

PFE = \sum_{i=0}^{i=n}Max(V(t), 0)


The current exposure is the cost of replacement of the exposures as of today. The expected exposure at time t is the mean exposure at time t, that is

EE = \int_{0}^{\infty}V(t)f_tV(t)dV(t)

We are now turning fo the C++ code that is a class definition file, a class implementation file, and a test file:

// Analytical_Credit_Exposure.hpp

// Édouard Tallent @ TaGoMa.Tech
// February 2013
// QuantCorner:


class AnalyticalCE
    double EE() const;
    double PFE() const;
    double EEThreshold() const;
    double PFEThreshold() const;

    double mu;      // expected MtM
    double sig;     // s.d of the MtM
    double alpha;   // confidence level
    double t;       // threshold variable

    AnalyticalCE(); // Default constructor
    AnalyticalCE(const AnalyticalCE& ace);  // Copy constructor
    virtual ~AnalyticalCE();    // Virtual destructor
    AnalyticalCE& operator = (const AnalyticalCE& cred);    // Assignment operator 

// Analytical_Credit_Exposure.cpp

// Édouard Tallent @ TaGoMa.Tech
// February 2013
// QuantCorner:

#include <cmath>
#include <boost\math\distributions\normal.hpp>
#include "Analytical_Credit_Exposure.hpp"

double AnalyticalCE::EE() const
    return mu * boost::math::cdf(boost::math::normal(), mu/sig) +
        sig * boost::math::pdf(boost::math::normal(), mu/sig);

double AnalyticalCE::PFE() const
    return mu + boost::math::quantile(boost::math::normal(), alpha) * sig;

double AnalyticalCE::EEThreshold() const
    return mu * (boost::math::cdf(boost::math::normal(), mu/sig) -
        boost::math::cdf(boost::math::normal(), (mu - t)/sig)) +
        sig * (boost::math::pdf(boost::math::normal(), mu/sig) -
        boost::math::pdf(boost::math::normal(), (mu - t)/sig)) +
        t * boost::math::cdf(boost::math::normal(), (mu - t)/sig);

double AnalyticalCE::PFEThreshold() const
    return std::min(t , mu + boost::math::quantile(boost::math::normal(), alpha) * sig);

AnalyticalCE::AnalyticalCE() : mu(0.0), sig(20.0), alpha(0.95), t(0.10)
    // Constructor initializer

AnalyticalCE::AnalyticalCE(const AnalyticalCE& ace) : mu(,
    sig(ace.sig), alpha(ace.alpha), t(ace.t)
    // Copy constructor

    // Destructor

AnalyticalCE& AnalyticalCE::operator = (const AnalyticalCE& cred)
    if (this == &cred)
        return *this;
// Analytical_Credit_Exposure_test.cpp

// Édouard Tallent @ TaGoMa.Tech
// February 2013
// QuantCorner:

#include "Analytical_Credit_Exposure.hpp"
#include <iostream>

int main()
        // Creation of an AnalyticalCE object
        AnalyticalCE risk; = 0.00;
        risk.sig = 0.20;
        risk.alpha = 0.90;
        risk.t = 0.10;

        // Print out to the console
        std::cout << "Expected exposure:\t\t\t\t" << risk.EE() << std::endl;
        std::cout << "Potential future exposure:\t\t\t" << risk.PFE() << std::endl;
        std::cout << "Expected exposure for a given threshold:\t" << risk.EEThreshold() << std::endl;
        std::cout << "Potential future exposure for a given threshold:" << risk.PFEThreshold() << std::endl;
    catch(std::exception& e)
        std::cout << "Error Message:\t" << e.what() << std::endl;

    return 0;

Eventually, the console output:
analytical credit exposure console

* Thank you to Jon Gregoy for taking the time to answer my questions by email.