This example is the equivalent of the single C language example that comes bundled with the ISDA standard model. It shows how to value three contracts with different coupon rates in conjunction with flat interest and credit curves.
// Copyright (C) 2009 Bojan Nikolic <bojan@bnikolic.co.uk>
//
// LICENSE TERMS
//
// Licensor of this file is BN Algorithms Limited, UK. Licensed for
// on-screen viewing on your computer only. Distribution
// prohibited. This file is provided "as-is", without warranty of any
// kind, either expressed or implied, including, without limitation,
// warranties that the program is free of defects, merchantable, fit
// for a particular purpose or non-infringing. The entire risk as to
// the quality of the documentation is with you. Should this file
// prove defective in any respect, you (not the licensor) assume the
// cost of any necessary correction. This disclaimer of warranty
// constitutes an essential part of this license.
//
/// \file bncds_t2_ex1.cpp
///
/// This example is the equivalent to the C-language example supplied
/// with the ISDA model: flat credit and inerest rate curves are
/// created and a set of three CDS contract with different coupons
/// valued off them
#include <iostream>
#include <boost/format.hpp>
#include <boost/array.hpp>
#include "bncds/cds.hpp"
#include "bncds/cdsstd.hpp"
#include "bncds/irconstr.hpp"
#include "bncds/spread.hpp"
#include "bncds/discount.hpp"
#include "bncds/pricing.hpp"
#include "bncds/jw/date.hpp"
void example(std::ostream &os)
{
const BNCDS::jw::Date baseDate(2008, 1, 3);
const BNCDS::jw::Date today(2008, 2, 1);
const BNCDS::jw::Date valueDate(2008, 2, 1);
const BNCDS::jw::Date startDate(2008, 2, 8);
const BNCDS::jw::Date endDate(2008, 2, 12);
const BNCDS::jw::Date stepinDate(2008, 2, 9);
const double notional = 1e7;
BNCDS::Spread spread(3600.0/1e4,
0.4);
BNCDS::jw::Curve flatzero(BNCDS::mkStdFlatCurve(baseDate,
1e-9));
BNCDS::Discount discount(flatzero);
BNCDS::SimpleStdCDS cds(startDate,
endDate,
0);
boost::array<double,3> coupons = { { 0, 3600, 7200 } };
for (size_t i=0; i < coupons.size(); ++i)
{
cds.couponr=coupons[i]/1e4;
const double charge = BNCDS::PV(today,
valueDate,
stepinDate,
cds,
spread,
discount);
std::cout<<std::endl
<<boost::format("Upfront charge @ cpn = %ibps = %f")
% coupons[i]
% (charge * notional);
}
std::cout<<std::endl<<std::endl;
}
int main(int argc,
char** argv)
{
example(std::cout);
}
This example shows how to calculate the present value (PV) of a non-contingent set of cashflows. It also illustrates how to:
// Copyright (C) 2009 Bojan Nikolic <bojan@bnikolic.co.uk>
//
// LICENSE TERMS
//
// Licensor of this file is BN Algorithms Limited, UK. Licensed for
// on-screen viewing on your computer only. Distribution
// prohibited. This file is provided "as-is", without warranty of any
// kind, either expressed or implied, including, without limitation,
// warranties that the program is free of defects, merchantable, fit
// for a particular purpose or non-infringing. The entire risk as to
// the quality of the documentation is with you. Should this file
// prove defective in any respect, you (not the licensor) assume the
// cost of any necessary correction. This disclaimer of warranty
// constitutes an essential part of this license.
/// \file bncds_t2_cashflow.cpp
///
/// \brief Compute the PV of a set of cashflows
///
/// This example shows how to create a list of cashflows from
/// information contained in a CDS contract structure. It then shows
/// how to find the present value of the cashflows from an IR curve
/// and how to bump to curve to find the change in PV.
#include <iostream>
#include <boost/format.hpp>
#include <boost/assign/list_of.hpp>
#include "bncds/irconstr.hpp"
#include "bncds/ircurve_pub.hpp"
#include "bncds/cdsstd.hpp"
#include "bncds/jw/date.hpp"
#include "bncds/jw/stringcnv.hpp"
#include "bncds/jw/convert.hpp"
#include "bncds/jw/curve.hpp"
#include "bncds/jw/cashflowlist.hpp"
/// This just fills up an interest rate data object with some (not
/// necessarilly realistic) sample data
void fillSampleIRCurve(BNCDS::IRCurvePub &irc)
{
irc.m_types=boost::assign::list_of
('M')('M')('M')('M')('M')
('S')('S')('S')('S')('S')('S')('S')('S')('S');
irc.m_terms=boost::assign::list_of
("1M")("2M")("3M")("6M")("9M")
("1Y")("2Y")("3Y")("4Y")("5Y")("6Y")("7Y")("8Y")("9Y");
irc.m_rates=boost::assign::list_of
(0.02)(0.02)(0.03)(0.03)(0.03)
(0.04)(0.04)(0.04)(0.04)(0.04)(0.04)(0.04)(0.04)(0.04);
irc.m_mmDCC=BNCDS::dayCC("Act/360");
irc.m_fixedSwapFreq=BNCDS::dayIntrvlToFreq("6M");
irc.m_floatSwapFreq=BNCDS::dayIntrvlToFreq("6M");
irc.m_fixedSwapDCC=BNCDS::dayCC("30/360");
irc.m_floatSwapDCC=BNCDS::dayCC("30/360");
irc.m_badDayConv='M';
}
void example(std::ostream &os)
{
// Create and fill-in the interest rate data
// This is the interest rate object
BNCDS::IRCurvePub irc;
// This fills it up with sample data
fillSampleIRCurve(irc);
const BNCDS::jw::Date startDate(2008, 1, 1);
const BNCDS::jw::Date endDate(2008, 12, 31);
/// Create a simple CDS contract
BNCDS::SimpleStdCDS cds(startDate,
endDate,
0.05);
/// Create cash-flows from the fee leg of the CDS
BNCDS::jw::CashFlowList cashflows(cds);
// Create a discount curve from the IR data
BNCDS::jw::Curve curve1(startDate,
irc);
os<<boost::format("PV of cashflows %g")
% cashflows.PV(curve1,
BNCDS::jw::Curve::Linear)
<<std::endl;
// Bump the IR data: 3M by 2%
irc["3M"] += 0.02;
// Create a new curve from bumped data
BNCDS::jw::Curve curve2(startDate,
irc);
os<<boost::format("PV after bump: %g")
% cashflows.PV(curve2,
BNCDS::jw::Curve::Linear)
<<std::endl;
}
int main(int argc,
char** argv)
{
example(std::cout);
}
This example shows how to use a series of CDS contracts with varying coupon rates but with a zero net present value (i.e., par rates), to compute the credit spread curve that is implied.
The example then continues on to value a separate CDS contract from the credit curve implied the original set of contracts.
// Copyright (C) 2009 Bojan Nikolic <bojan@bnikolic.co.uk>
//
// LICENSE TERMS
//
// Licensor of this file is BN Algorithms Limited, UK. Licensed for
// on-screen viewing on your computer only. Distribution
// prohibited. This file is provided "as-is", without warranty of any
// kind, either expressed or implied, including, without limitation,
// warranties that the program is free of defects, merchantable, fit
// for a particular purpose or non-infringing. The entire risk as to
// the quality of the documentation is with you. Should this file
// prove defective in any respect, you (not the licensor) assume the
// cost of any necessary correction. This disclaimer of warranty
// constitutes an essential part of this license.
/// \file bncds_t2_cleanspread.cpp
/// This example demonstrates strapping a credit curve from a series
/// of CDS contracts with par rates and then valueing a CDS off this
/// curve
#include <iostream>
#include <boost/format.hpp>
#include <boost/assign/list_of.hpp>
#include "bncds/irconstr.hpp"
#include "bncds/ircurve_pub.hpp"
#include "bncds/cdsstd.hpp"
#include "bncds/discount.hpp"
#include "bncds/spread.hpp"
#include "bncds/pricing.hpp"
#include "bncds/jw/date.hpp"
#include "bncds/jw/stringcnv.hpp"
#include "bncds/jw/convert.hpp"
#include "bncds/jw/curve.hpp"
#include "bncds/jw/curve_io.hpp"
#include "bncds/jw/cashflowlist.hpp"
#include "bncds/jw/cdsfns.hpp"
#include "bncds/jw/stubmethod.hpp"
#include "bncds/jw/dateinterval.hpp"
void example(std::ostream &os)
{
const BNCDS::jw::Date startDate(2008, 1, 1);
// Create a flat curve with zero rates
BNCDS::jw::Curve flatzero(BNCDS::mkStdFlatCurve(startDate,
1e-9));
std::vector<TDate> endDates=boost::assign::list_of
(BNCDS::jw::Date(2008, 4, 1))
(BNCDS::jw::Date(2008, 7, 1))
(BNCDS::jw::Date(2008, 10, 1))
(BNCDS::jw::Date(2009, 1, 1))
(BNCDS::jw::Date(2010, 1, 1))
(BNCDS::jw::Date(2011, 1, 1));
std::vector<double> rates=boost::assign::list_of
(0.02)(0.02)(0.03)(0.03)(0.03)(0.03);
boost::shared_ptr<BNCDS::jw::Curve> pcurve=
BNCDS::jw::SCleanSpreadCurve(startDate,
flatzero,
startDate,
startDate,
startDate,
endDates,
rates,
0.0,
true,
BNCDS::jw::DateInterval("6M"),
BNCDS::dayCC("Act/360"),
BNCDS::jw::StubMethod("f/s"),
'M',
"None");
/// Print the curve
os<< (*pcurve);
// Now create a new CDS contract and value of the strapped
// curve. Note the coupon is low, so expect a positive PV
BNCDS::SimpleStdCDS cds(startDate,
(BNCDS::jw::Date(2009, 7, 1)),
0.01);
// Use the flat zero curve as the discount curve
BNCDS::Discount discount(flatzero);
// Create the spread from the strapped curve
BNCDS::Spread spread(*pcurve,
0.4);
const double pv=BNCDS::PV(startDate,
startDate,
startDate,
cds,
spread,
discount);
os<<"PV: "<<pv
<<std::endl;
}
int main(int argc,
char** argv)
{
example(std::cout);
}
This example shows the reverse operation to the previous example, i.e., how to compute the required coupons to make a set of CDS contract with par prices (zero present value) given the credit spread curve.
// Copyright (C) 2009 Bojan Nikolic <bojan@bnikolic.co.uk>
//
// LICENSE TERMS
//
// Licensor of this file is BN Algorithms Limited, UK. Licensed for
// on-screen viewing on your computer only. Distribution
// prohibited. This file is provided "as-is", without warranty of any
// kind, either expressed or implied, including, without limitation,
// warranties that the program is free of defects, merchantable, fit
// for a particular purpose or non-infringing. The entire risk as to
// the quality of the documentation is with you. Should this file
// prove defective in any respect, you (not the licensor) assume the
// cost of any necessary correction. This disclaimer of warranty
// constitutes an essential part of this license.
/// \file bncds_t2_parspreads.cpp
/// This example demonstrates calculation of par CDS spreads given a
/// spread and discount curves
#include <iostream>
#include <boost/assign/list_of.hpp>
#include "bncds/cdsstd.hpp"
#include "bncds/discount.hpp"
#include "bncds/spread.hpp"
#include "bncds/pricing.hpp"
#include "bncds/irconstr.hpp"
#include "bncds/jw/date.hpp"
#include "bncds/jw/stringcnv.hpp"
#include "bncds/jw/convert.hpp"
#include "bncds/jw/curve.hpp"
#include "bncds/jw/curve_io.hpp"
#include "bncds/jw/cashflowlist.hpp"
#include "bncds/jw/cdsfns.hpp"
#include "bncds/jw/stubmethod.hpp"
#include "bncds/jw/dateinterval.hpp"
void example(std::ostream &os)
{
const BNCDS::jw::Date today(2008, 1, 1);
// Create a flat curve with zero rates
BNCDS::jw::Curve flatzero(BNCDS::mkStdFlatCurve(today,
1e-9));
// End dates at which we want the par-spreads
std::vector<TDate> endDates=boost::assign::list_of
(BNCDS::jw::Date(2008, 4, 1))
(BNCDS::jw::Date(2008, 7, 1))
(BNCDS::jw::Date(2008, 10, 1))
(BNCDS::jw::Date(2009, 1, 1))
(BNCDS::jw::Date(2010, 1, 1))
(BNCDS::jw::Date(2011, 1, 1));
// The spreads will go here
std::vector<double> parspreads;
BNCDS::jw::CdsParSpreads(today,
today,
today,
endDates,
true,
BNCDS::jw::DateInterval("6M"),
BNCDS::jw::StubMethod("f/s"),
BNCDS::dayCC("Act/360"),
'M',
"None",
flatzero,
flatzero,
0.5,
parspreads);
for (size_t i=0; i<parspreads.size(); ++i)
os<<"Spread : "<<parspreads[i]
<<std::endl;
}
int main(int argc,
char** argv)
{
example(std::cout);
}