-1
$\begingroup$

I am working on a 1 Factor Libor Market Model (LMM) in C++ and I working my implementation of the formula to find my Discount Bond matrix via the following formula: enter image description here

In the case of my model alpha is constant at .25 and my matrix L is 20 x 20. Here is my code:

#include <random>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <cmath>
#include <limits>
#include <cstdlib>
#include <chrono>
#include <iterator>
#include <algorithm>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/kurtosis.hpp>
#include <boost/accumulators/statistics/variance.hpp>
#include <boost/accumulators/statistics/skewness.hpp>
#include <dlib/optimization.h>
#include <dlib/matrix.h>
#include "mleFunctor.h"
#include "mleDerFunctor.h"



using namespace boost::accumulators;
using namespace dlib;

typedef matrix<double, 0, 1> column_vector;

//Generate Gaussian via Box-Muller from Mark Joshi's C++ Design Patterns and Derivatives Pricing
double GetOneGaussianByBoxMuller()
{
    double result;

    double x;
    double y;

    double sizeSquared;
    do
    {
        x = 2.0*std::rand() / static_cast<double>(RAND_MAX) - 1;
        y = 2.0*std::rand() / static_cast<double>(RAND_MAX) - 1;
        sizeSquared = x*x + y*y;
    } while (sizeSquared >= 1.0);

    result = x*sqrt(-2 * log(sizeSquared) / sizeSquared);

    return result;
}

double libSum(matrix<double,20,20> v, matrix<double, 20, 20> lib, int r,int c , double d, int index,std::vector<double> W)
{
    double sum = 0.0;
    for (auto k = index + 1; k < lib.nr()-1; ++k)
    {
        sum += ((d*v(k,c-1)*lib(k,c-1))/(1+d*lib(k,c-1)))*v(k,c-1) * lib(r, c-1)*(W[c] - W[c-1]);
    }

    return sum;
}

matrix<double> findDF(matrix<double> ell)
{
    matrix<double,20,20> Df;
    set_all_elements(Df, 0);
    double prod = 1.0;

    //Need to execute the formula in the question
    for (auto c = 0;c < ell.nc(); ++c) 
    {
        for (auto r = 0; r < ell.nr(); ++r)
        {
            //formula for D goes here
        }
    }


    return Df;
}

void lmm()
{
    double dt = .25;
    std::vector<double> W(20);
    std::vector<double> T;
    matrix<double, 20, 20> L;
    W[0] = 0;
    for (auto c = 1; c < W.size(); ++c)
    {
        W[c] = W[c - 1] + sqrt(dt)*GetOneGaussianByBoxMuller();
    }
    for (auto i = 0; i < 20; ++i)
    {
        T.push_back(i*.25);
    }

    set_all_elements(L, 0);
    set_colm(L, 0) = .003641; //3M Libor Rate on November 16,2015
    matrix<double,20,20> vol;
    set_all_elements(vol,.15);

    for (auto c = 1; c < L.nc(); ++c)
    {
        for (auto r = c; r < L.nc()-1; ++r)
        {
            L(r, c) = L(r, c-1) - libSum(vol, L,r,c, .25, c,W) + vol(r,c-1) * L(r, c-1 )*(W[c] - W[c-1]);
        }
    }

    for (auto c = 1; c < L.nc(); ++c)
    {

        L(19, c) = L(19, c - 1)  + vol(19, c - 1) * L(19, c - 1)*(W[c] - W[c - 1]);

    }
    std::ofstream outfile("LMMFlatVol.csv");
    outfile << L << std::endl;
    matrix<double> df = findDF(L);
}

int main()
{
    lmm();
    return 0;
}

Any help related to building the loop structure in the function findDf() is much appreciated.

$\endgroup$
0
$\begingroup$

I have every reason to believe this is a homework so I won't code it for you. In fact, your question is not totally clear, you haven't told us what are the rows and what are the columns. We know it's a 20x20 matrix, but what it's that supposed to be?

A common implementation is illustrated below:

enter image description here

So you can have a diagonal matrix where the columns are the times. Each column specifies a term structure. Note that this is not the only representation, but it makes perfect sense to me.

To price a discounted bond, you would need the forward rates for each successive period up to the maturity of the bond. You will do it by the forward rates in the diagonal of the matrix. For example, L1(T(0)) is the LIBOR spot rate from today to T(1). L2(T(1)) is the LIBOR forward rate from T(1) to T(2).

To do it in C++, you will simply need to iterate the diagonal entries. This shouldn't be a problem for someone learning quantitative finance. Ask stack overflow.com if you're having issues.

$\endgroup$
  • $\begingroup$ It is actually a semester long research project. I did ask on stack overflow and no one has yet to respond. I have numerous papers that lay out the theory and I understand the problem at hand quite well. I am just having a mental block as to the loop structure in code. $\endgroup$ – salisboss Nov 19 '15 at 0:31
  • $\begingroup$ @salisboss People one stackoverflow wouldn't know what forward rate is. They can't answer anything relates to LIBOR modelling. $\endgroup$ – SmallChess Nov 19 '15 at 0:40
  • $\begingroup$ @salisboss I'm sure my approach works (in fact it's recommend by a book). Do you want to do it this way? If you prefer something else, you should update the question to reflect more how your matrix would look like, then I'll see how I can help you to code the function. $\endgroup$ – SmallChess Nov 19 '15 at 0:41
  • $\begingroup$ I did phrase the question appropriate to the audience but thanks. $\endgroup$ – salisboss Nov 19 '15 at 0:46
  • $\begingroup$ I know that I only the discount factors on the diagonal. I was going to try and build the entire matrix but your method works too. I will code that up and make sure I understand it. Thanks. I guess I was trying to more than I needed to. $\endgroup$ – salisboss Nov 19 '15 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.