Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
crcirpp.cpp File Reference
#include "utilities.hpp"
#include "toplevelfixture.hpp"
#include <boost/test/unit_test.hpp>
#include <ql/currencies/europe.hpp>
#include <ql/experimental/callablebonds/callablebond.hpp>
#include <ql/indexes/ibor/euribor.hpp>
#include <ql/instruments/callabilityschedule.hpp>
#include <ql/instruments/creditdefaultswap.hpp>
#include <ql/math/optimization/levenbergmarquardt.hpp>
#include <ql/math/randomnumbers/rngtraits.hpp>
#include <ql/math/statistics/incrementalstatistics.hpp>
#include <ql/methods/montecarlo/multipathgenerator.hpp>
#include <ql/methods/montecarlo/pathgenerator.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/termstructures/credit/flathazardrate.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/daycounters/actualactual.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <ql/pricingengines/credit/midpointcdsengine.hpp>
#include <qle/instruments/cdsoption.hpp>
#include <qle/methods/multipathgeneratorbase.hpp>
#include <qle/models/crossassetmodel.hpp>
#include <qle/models/cdsoptionhelper.hpp>
#include <qle/models/crcirpp.hpp>
#include <qle/models/cirppconstantfellerparametrization.hpp>
#include <qle/processes/crcirppstateprocess.hpp>
#include <boost/make_shared.hpp>
#include <fstream>
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/covariance.hpp>
#include <boost/accumulators/statistics/density.hpp>
#include <boost/accumulators/statistics/error_of_mean.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/variates/covariate.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testMartingaleProperty)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE()

BOOST_AUTO_TEST_CASE ( testMartingaleProperty  )

Definition at line 120 of file crcirpp.cpp.

120 {
121
122 BOOST_TEST_MESSAGE("Testing martingale property in credit-CIR++ model for Brigo-Alfonsi discretizations...");
123
124 QuantLib::ext::shared_ptr<StochasticProcess> process = model->stateProcess();
125 QL_REQUIRE(process != NULL, "process has null pointer!");
126
127 // CIRplusplusStateProcess::Discretization discType = CIRplusplusStateProcess::Discretization::Reflection;
128 // CIRplusplusStateProcess::Discretization discType = CIRplusplusStateProcess::Discretization::PartialTruncation;
129 // CIRplusplusStateProcess::Discretization discType = CIRplusplusStateProcess::Discretization::FullTruncation;
130 // QuantLib::ext::shared_ptr<StochasticProcess> process = QuantLib::ext::make_shared<CIRplusplusStateProcess>(model.get(),
131 // discType); BOOST_TEST_MESSAGE("Simulation type of negative variance process "<<discType);
132 Size n = 10000; // number of paths
133 Size seed = 42; // rng seed
134 // Time T = 25.0; // maturity of payoff
135 // Time T2 = 40.0; // zerobond maturity
136 Time T = 10.0; // maturity of payoff
137 Time T2 = 20.0; // zerobond maturity
138 // QL_REQUIRE(T2 == model->maxSimulationHorizon(), "Forward measure horizon must be equalt to the maturity of the
139 // zero bond");
140 Size steps = static_cast<Size>(T * 52); // number of steps taken (euler)
141
142 TimeGrid grid(T, steps);
143 // LowDiscrepancy::rsg_type sg = LowDiscrepancy::make_sequence_generator( 9 * steps, seed);
144 // MultiPathGenerator<LowDiscrepancy::rsg_type> pg(process, grid, sg, false);
145 MultiPathGeneratorMersenneTwister pg(process, grid, seed, true);
146 accumulator_set<double, stats<tag::mean, tag::error_of<tag::mean>>> meanTest_y;
147 accumulator_set<double, stats<tag::variance, tag::error_of<tag::mean>>> varTest_y;
148
149 accumulator_set<double, stats<tag::mean, tag::error_of<tag::mean>>> sp;
150 accumulator_set<double, stats<tag::mean, tag::error_of<tag::mean>>> numeraire;
151
152 accumulator_set<double, stats<tag::density>> histXAcc(tag::density::cache_size = 10000,
153 tag::density::num_bins = 50);
154
155 // typedef boost::iterator_range<std::vector<std::pair<double, double>>::iterator> histogram_type;
156
157 // //create an accumulator
158 // acc histXAcc( tag::density::num_bins = 20, tag::density::cache_size = 10);
159
160 std::ofstream of;
161 // of.open("y_paths.txt");
162 for (Size j = 0; j < n; ++j) {
163 Sample<MultiPath> path = pg.next();
164 Size l = path.value[0].length() - 1;
165 Real y = path.value[0][l];
166 Real num = path.value[1][l];
167 sp(model->survivalProbability(T, T2, y) * num);
168 numeraire(num);
169 meanTest_y(y);
170 varTest_y(y);
171 histXAcc(y);
172 }
173 // histogram_type histX = density(histXAcc);
174 // double total = 0.0;
175 // std::ofstream ofHistX;
176 // ofHistX.open("y_hist.txt");
177
178 // Real binSize = histX[1].first - histX[0].first;
179 // for (Size i = 0; i < histX.size(); i++) {
180 // ofHistX << histX[i].first << "," << histX[i].second / binSize << std::endl;
181 // total += histX[i].second;
182 // }
183 // BOOST_TEST_MESSAGE("Total cdf(x): " << total); // should be 1 (and it is)
184
185 // total = 0.0;
186 // std::ofstream ofPDFX;
187 // ofPDFX.open("y_pdf.txt");
188 // // for (Real x = - 5.0; x <= 7.0; x += 0.1)
189 // for (Real x = 0.00018; x <= 0.01; x += 0.001) {
190 // ofPDFX << x << "," << model->density(x, T) << std::endl;
191 // total += model->density(x, T);
192 // }
193 // BOOST_TEST_MESSAGE("Total cdf(x): " << total); // should be 1 (and it is)
194
195 // of.close();
196 // ofPDFX.close();
197 // ofHistX.close();
198
199 // Real kappa = model->parametrization()->kappa(0);
200 // Real theta = model->parametrization()->theta(0);
201 // Real sigma = model->parametrization()->sigma(0);
202 // Real y0 = model->parametrization()->y0(0);
203 // Real sigma2 = sigma * sigma;
204
205 BOOST_TEST_MESSAGE("\nBrigo-Alfonsi:");
206 // BOOST_TEST_MESSAGE("Mean = " << mean(meanTest_y) << " +- " << error_of<tag::mean>(meanTest_y) << " analytic "
207 // <<y0 * exp(-kappa*T) + theta * (1- exp(-kappa*T))); BOOST_TEST_MESSAGE("Variance = " << variance(varTest_y) << "
208 // +- " << error_of<tag::mean>(varTest_y)<<" analytic " <<y0*sigma2/theta*(exp(-kappa*T) - exp(-2*kappa*T)) +
209 // theta*sigma2/2/kappa*(1- exp(-2*kappa *T))*(1- exp(-2*kappa *T)) );
210 BOOST_TEST_MESSAGE("SP = " << mean(sp) << " +- " << error_of<tag::mean>(sp) << " vs analytical "
211 << dts->survivalProbability(T2));
212 BOOST_TEST_MESSAGE("Num = " << mean(numeraire) << " +- " << error_of<tag::mean>(numeraire) << " vs analytical "
213 << dts->survivalProbability(T));
214
215 Real tol2 = 12.0E-4;
216 Real expectedSP = dts->survivalProbability(T);
217 Real expectedCondSP = dts->survivalProbability(T2);
218 if (std::abs(mean(numeraire) - expectedSP) > tol2)
219 BOOST_FAIL("Martingale test failed for SP(t) (Brigo-Alfonsi discr.), expected "
220 << expectedSP << ", got " << mean(numeraire) << ", tolerance " << tol2);
221 if (std::abs(mean(sp) - expectedCondSP) > tol2)
222 BOOST_FAIL("Martingale test failed for SP(t,T) (Brigo-Alfonsi discr.), expected "
223 << expectedCondSP << ", got " << mean(sp) << ", tolerance " << tol2);
224
225} // testIrTSMartingaleProperty
Instantiation of MultiPathGenerator with standard PseudoRandom traits.
std::vector< Size > steps
+ Here is the call graph for this function: