1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="robots" content="none">
<title>QuantLib: Replication.cpp</title>
<link rel="stylesheet" href="quantlib.css" type="text/css">
<link rel="stylesheet" href="print.css" type="text/css" media="print">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
<div id="container">
<div id="header">
<img class="titleimage"
src="QL-title.jpg" width="212" height="47" border="0"
alt="QuantLib">
<br>
<h3 class="subtitle">A free/open-source library for quantitative finance</h3>
</div>
<div id="menu">
<h3 class="navbartitle">Version 0.9.0</h3>
<hr>
<h3 class="navbartitle">Getting started</h3>
<ul class="navbarlist">
<li class="navlink"><a href="index.html">Introduction</a></li>
<li class="navlink"><a href="overview.html">Project overview</a></li>
<li class="navlink"><a href="where.html">Where to get QuantLib</a></li>
<li class="navlink"><a href="install.html">Installation</a></li>
<li class="navlink"><a href="config.html">Configuration</a></li>
<li class="navlink"><a href="usage.html">Usage</a></li>
<li class="navlink"><a href="history.html">Version history</a></li>
<li class="navlink"><a href="resources.html">Additional resources</a></li>
<li class="navlink"><a href="group.html">The QuantLib group</a></li>
<li class="navlink"><a href="license.html">Copyright and license</a></li>
</ul>
<hr>
<h3 class="navbartitle">Reference manual</h3>
<ul class="navbarlist">
<li class="navlink"><a href="modules.html">Modules</a></li>
<li class="navlink"><a href="hierarchy.html">Class Hierarchy</a></li>
<li class="navlink"><a href="annotated.html">Compound List</a></li>
<li class="navlink"><a href="files.html">File List</a></li>
<li class="navlink"><a href="functions.html">Compound Members</a></li>
<li class="navlink"><a href="globals.html">File Members</a></li>
<li class="navlink"><a href="bug.html">Known Bugs</a></li>
<li class="navlink"><a href="caveats.html">Caveats</a></li>
<li class="navlink"><a href="test.html">Test Suite</a></li>
<li class="navlink"><a href="examples.html">Examples</a></li>
</ul>
</div>
<div id="content">
<!--Doxygen-generated content-->
<!-- Generated by Doxygen 1.5.4 -->
<h1>Replication.cpp</h1>This example uses the CompositeInstrument class to perform static replication of a down-and-out barrier option.<p>
<div class="fragment"><pre class="fragment"><span class="comment">/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */</span>
<span class="comment">/* This example showcases the CompositeInstrument class. Such class</span>
<span class="comment"> is used to build a static replication of a down-and-out barrier</span>
<span class="comment"> option, as outlined in Section 10.2 of Mark Joshi's "The Concepts</span>
<span class="comment"> and Practice of Mathematical Finance" to which we refer the</span>
<span class="comment"> reader.</span>
<span class="comment">*/</span>
<span class="comment">// the only header you need to use QuantLib</span>
<span class="preprocessor">#include <ql/quantlib.hpp></span>
<span class="preprocessor">#ifdef BOOST_MSVC</span>
<span class="preprocessor"></span><span class="comment">/* Uncomment the following lines to unmask floating-point</span>
<span class="comment"> exceptions. Warning: unpredictable results can arise...</span>
<span class="comment"></span>
<span class="comment"> See http://www.wilmott.com/messageview.cfm?catid=10&threadid=9481</span>
<span class="comment"> Is there anyone with a definitive word about this?</span>
<span class="comment">*/</span>
<span class="comment">// #include <float.h></span>
<span class="comment">// namespace { unsigned int u = _controlfp(_EM_INEXACT, _MCW_EM); }</span>
<span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="preprocessor">#include <boost/timer.hpp></span>
<span class="preprocessor">#include <iostream></span>
<span class="preprocessor">#include <iomanip></span>
<span class="keyword">using namespace </span>QuantLib;
<span class="preprocessor">#if defined(QL_ENABLE_SESSIONS)</span>
<span class="preprocessor"></span><span class="keyword">namespace </span>QuantLib {
<a name="a0"></a><a class="code" href="group__types.html#gb9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> sessionId() { <span class="keywordflow">return</span> 0; }
}
<span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="keywordtype">int</span> main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>* []) {
<span class="keywordflow">try</span> {
boost::timer timer;
std::cout << std::endl;
<a name="_a1"></a><a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> today(29, May, 2006);
Settings::instance().evaluationDate() = today;
<span class="comment">// the option to replicate</span>
Barrier::Type barrierType = Barrier::DownOut;
<a name="a2"></a><a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> barrier = 70.0;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> rebate = 0.0;
Option::Type type = Option::Put;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> underlyingValue = 100.0;
boost::shared_ptr<SimpleQuote> underlying(
<span class="keyword">new</span> <a name="_a3"></a><a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(underlyingValue));
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> strike = 100.0;
boost::shared_ptr<SimpleQuote> riskFreeRate(<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(0.04));
boost::shared_ptr<SimpleQuote> <a name="a4"></a><a class="code" href="group__manips.html#gc402ef7c87f63f7c603ee87210b5750c" title="output volatilities as percentages">volatility</a>(<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(0.20));
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> maturity = today + 1*Years;
std::cout << std::endl ;
<span class="comment">// write column headings</span>
<a name="a5"></a><a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> widths[] = { 45, 15, 15 };
<a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> totalWidth = widths[0]+widths[1]+widths[2];
std::string rule(totalWidth, <span class="charliteral">'-'</span>), dblrule(totalWidth, <span class="charliteral">'='</span>);
std::cout << dblrule << std::endl;
std::cout << <span class="stringliteral">"Initial market conditions"</span> << std::endl;
std::cout << dblrule << std::endl;
std::cout << std::setw(widths[0]) << std::left << <span class="stringliteral">"Option"</span>
<< std::setw(widths[1]) << std::left << <span class="stringliteral">"NPV"</span>
<< std::setw(widths[2]) << std::left << <span class="stringliteral">"Error"</span>
<< std::endl;
std::cout << rule << std::endl;
<span class="comment">// bootstrap the yield/vol curves</span>
<a name="_a6"></a><a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> dayCounter = <a name="_a7"></a><a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>();
<a name="_a8"></a><a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<Quote></a> h1(riskFreeRate);
<a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<Quote></a> h2(<a class="code" href="group__manips.html#gc402ef7c87f63f7c603ee87210b5750c" title="output volatilities as percentages">volatility</a>);
<a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<YieldTermStructure></a> flatRate(
boost::shared_ptr<YieldTermStructure>(
<span class="keyword">new</span> <a name="_a9"></a><a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(0, <a name="_a10"></a><a class="code" href="class_quant_lib_1_1_null_calendar.html" title="Calendar for reproducing theoretical calculations.">NullCalendar</a>(),
h1, dayCounter)));
<a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<BlackVolTermStructure></a> flatVol(
boost::shared_ptr<BlackVolTermStructure>(
<span class="keyword">new</span> <a name="_a11"></a><a class="code" href="class_quant_lib_1_1_black_constant_vol.html" title="Constant Black volatility, no time-strike dependence.">BlackConstantVol</a>(0, <a class="code" href="class_quant_lib_1_1_null_calendar.html" title="Calendar for reproducing theoretical calculations.">NullCalendar</a>(),
h2, dayCounter)));
<span class="comment">// instantiate the option</span>
boost::shared_ptr<Exercise> exercise(
<span class="keyword">new</span> <a name="_a12"></a><a class="code" href="class_quant_lib_1_1_european_exercise.html" title="European exercise.">EuropeanExercise</a>(maturity));
boost::shared_ptr<StrikedTypePayoff> payoff(
<span class="keyword">new</span> <a name="_a13"></a><a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(type, strike));
boost::shared_ptr<BlackScholesProcess> bsProcess(
<span class="keyword">new</span> <a name="_a14"></a><a class="code" href="class_quant_lib_1_1_black_scholes_process.html" title="Black-Scholes (1973) stochastic process.">BlackScholesProcess</a>(<a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<Quote></a>(underlying),
flatRate, flatVol));
boost::shared_ptr<PricingEngine> barrierEngine(
<span class="keyword">new</span> <a name="_a15"></a><a class="code" href="class_quant_lib_1_1_analytic_barrier_engine.html" title="Pricing engine for barrier options using analytical formulae.">AnalyticBarrierEngine</a>(bsProcess));
boost::shared_ptr<PricingEngine> europeanEngine(
<span class="keyword">new</span> <a name="_a16"></a><a class="code" href="class_quant_lib_1_1_analytic_european_engine.html" title="Pricing engine for European vanilla options using analytical formulae.">AnalyticEuropeanEngine</a>(bsProcess));
<a name="_a17"></a><a class="code" href="class_quant_lib_1_1_barrier_option.html" title="Barrier option on a single asset.">BarrierOption</a> referenceOption(barrierType, barrier, rebate,
payoff, exercise);
referenceOption.setPricingEngine(barrierEngine);
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> referenceValue = referenceOption.NPV();
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Original barrier option"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << referenceValue
<< std::setw(widths[2]) << std::left << <span class="stringliteral">"N/A"</span>
<< std::endl;
<span class="comment">// Replicating portfolios</span>
<a name="_a18"></a><a class="code" href="class_quant_lib_1_1_composite_instrument.html" title="Composite instrument">CompositeInstrument</a> portfolio1, portfolio2, portfolio3;
<span class="comment">// Final payoff first (the same for all portfolios):</span>
<span class="comment">// as shown in Joshi, a put struck at K...</span>
boost::shared_ptr<Instrument> put1(
<span class="keyword">new</span> <a name="_a19"></a><a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(payoff, exercise));
put1->setPricingEngine(europeanEngine);
portfolio1.<a name="a20"></a><a class="code" href="class_quant_lib_1_1_composite_instrument.html#6c2e5140cb8d3d49eb526feba0d0fda0" title="adds an instrument to the composite">add</a>(put1);
portfolio2.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#6c2e5140cb8d3d49eb526feba0d0fda0" title="adds an instrument to the composite">add</a>(put1);
portfolio3.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#6c2e5140cb8d3d49eb526feba0d0fda0" title="adds an instrument to the composite">add</a>(put1);
<span class="comment">// ...minus a digital put struck at B of notional K-B...</span>
boost::shared_ptr<StrikedTypePayoff> digitalPayoff(
<span class="keyword">new</span> <a name="_a21"></a><a class="code" href="class_quant_lib_1_1_cash_or_nothing_payoff.html" title="Binary cash-or-nothing payoff.">CashOrNothingPayoff</a>(Option::Put, barrier, 1.0));
boost::shared_ptr<Instrument> digitalPut(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(digitalPayoff, exercise));
digitalPut->setPricingEngine(europeanEngine);
portfolio1.<a name="a22"></a><a class="code" href="class_quant_lib_1_1_composite_instrument.html#f781618b4ac4bee06da95a7c8b2ee662" title="shorts an instrument from the composite">subtract</a>(digitalPut, strike-barrier);
portfolio2.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#f781618b4ac4bee06da95a7c8b2ee662" title="shorts an instrument from the composite">subtract</a>(digitalPut, strike-barrier);
portfolio3.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#f781618b4ac4bee06da95a7c8b2ee662" title="shorts an instrument from the composite">subtract</a>(digitalPut, strike-barrier);
<span class="comment">// ...minus a put option struck at B.</span>
boost::shared_ptr<StrikedTypePayoff> lowerPayoff(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(Option::Put, barrier));
boost::shared_ptr<Instrument> put2(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(lowerPayoff, exercise));
put2->setPricingEngine(europeanEngine);
portfolio1.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#f781618b4ac4bee06da95a7c8b2ee662" title="shorts an instrument from the composite">subtract</a>(put2);
portfolio2.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#f781618b4ac4bee06da95a7c8b2ee662" title="shorts an instrument from the composite">subtract</a>(put2);
portfolio3.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#f781618b4ac4bee06da95a7c8b2ee662" title="shorts an instrument from the composite">subtract</a>(put2);
<span class="comment">// Now we use puts struck at B to kill the value of the</span>
<span class="comment">// portfolio on a number of points (B,t). For the first</span>
<span class="comment">// portfolio, we'll use 12 dates at one-month's distance.</span>
<a class="code" href="group__types.html#gb9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> i;
<span class="keywordflow">for</span> (i=12; i>=1; i--) {
<span class="comment">// First, we instantiate the option...</span>
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> innerMaturity = today + i*Months;
boost::shared_ptr<Exercise> innerExercise(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_exercise.html" title="European exercise.">EuropeanExercise</a>(innerMaturity));
boost::shared_ptr<StrikedTypePayoff> innerPayoff(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(Option::Put, barrier));
boost::shared_ptr<Instrument> putn(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(innerPayoff, innerExercise));
putn->setPricingEngine(europeanEngine);
<span class="comment">// ...second, we evaluate the current portfolio and the</span>
<span class="comment">// latest put at (B,t)...</span>
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> killDate = today + (i-1)*Months;
Settings::instance().evaluationDate() = killDate;
underlying->setValue(barrier);
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> portfolioValue = portfolio1.<a name="a23"></a><a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> putValue = putn->NPV();
<span class="comment">// ...finally, we estimate the notional that kills the</span>
<span class="comment">// portfolio value at that point...</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> notional = portfolioValue/putValue;
<span class="comment">// ...and we subtract from the portfolio a put with such</span>
<span class="comment">// notional.</span>
portfolio1.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#f781618b4ac4bee06da95a7c8b2ee662" title="shorts an instrument from the composite">subtract</a>(putn, notional);
}
<span class="comment">// The portfolio being complete, we return to today's market...</span>
Settings::instance().evaluationDate() = today;
underlying->setValue(underlyingValue);
<span class="comment">// ...and output the value.</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> portfolioValue = portfolio1.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Replicating portfolio (12 dates)"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
<span class="comment">// For the second portfolio, we'll use 26 dates at two-weeks'</span>
<span class="comment">// distance.</span>
<span class="keywordflow">for</span> (i=52; i>=2; i-=2) {
<span class="comment">// Same as above.</span>
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> innerMaturity = today + i*Weeks;
boost::shared_ptr<Exercise> innerExercise(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_exercise.html" title="European exercise.">EuropeanExercise</a>(innerMaturity));
boost::shared_ptr<StrikedTypePayoff> innerPayoff(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(Option::Put, barrier));
boost::shared_ptr<Instrument> putn(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(innerPayoff, innerExercise));
putn->setPricingEngine(europeanEngine);
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> killDate = today + (i-2)*Weeks;
Settings::instance().evaluationDate() = killDate;
underlying->setValue(barrier);
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> portfolioValue = portfolio2.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> putValue = putn->NPV();
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> notional = portfolioValue/putValue;
portfolio2.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#f781618b4ac4bee06da95a7c8b2ee662" title="shorts an instrument from the composite">subtract</a>(putn, notional);
}
Settings::instance().evaluationDate() = today;
underlying->setValue(underlyingValue);
portfolioValue = portfolio2.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Replicating portfolio (26 dates)"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
<span class="comment">// For the third portfolio, we'll use 52 dates at one-week's</span>
<span class="comment">// distance.</span>
<span class="keywordflow">for</span> (i=52; i>=1; i--) {
<span class="comment">// Same as above.</span>
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> innerMaturity = today + i*Weeks;
boost::shared_ptr<Exercise> innerExercise(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_exercise.html" title="European exercise.">EuropeanExercise</a>(innerMaturity));
boost::shared_ptr<StrikedTypePayoff> innerPayoff(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(Option::Put, barrier));
boost::shared_ptr<Instrument> putn(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_european_option.html" title="European option on a single asset.">EuropeanOption</a>(innerPayoff, innerExercise));
putn->setPricingEngine(europeanEngine);
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> killDate = today + (i-1)*Weeks;
Settings::instance().evaluationDate() = killDate;
underlying->setValue(barrier);
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> portfolioValue = portfolio3.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> putValue = putn->NPV();
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> notional = portfolioValue/putValue;
portfolio3.<a class="code" href="class_quant_lib_1_1_composite_instrument.html#f781618b4ac4bee06da95a7c8b2ee662" title="shorts an instrument from the composite">subtract</a>(putn, notional);
}
Settings::instance().evaluationDate() = today;
underlying->setValue(underlyingValue);
portfolioValue = portfolio3.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Replicating portfolio (52 dates)"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
<span class="comment">// Now we modify the market condition to see whether the</span>
<span class="comment">// replication holds. First, we change the underlying value so</span>
<span class="comment">// that the option is out of the money.</span>
std::cout << dblrule << std::endl;
std::cout << <span class="stringliteral">"Modified market conditions: out of the money"</span>
<< std::endl;
std::cout << dblrule << std::endl;
std::cout << std::setw(widths[0]) << std::left << <span class="stringliteral">"Option"</span>
<< std::setw(widths[1]) << std::left << <span class="stringliteral">"NPV"</span>
<< std::setw(widths[2]) << std::left << <span class="stringliteral">"Error"</span>
<< std::endl;
std::cout << rule << std::endl;
underlying->setValue(110.0);
referenceValue = referenceOption.NPV();
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Original barrier option"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << referenceValue
<< std::setw(widths[2]) << std::left << <span class="stringliteral">"N/A"</span>
<< std::endl;
portfolioValue = portfolio1.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Replicating portfolio (12 dates)"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
portfolioValue = portfolio2.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Replicating portfolio (26 dates)"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
portfolioValue = portfolio3.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Replicating portfolio (52 dates)"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
<span class="comment">// Next, we change the underlying value so that the option is</span>
<span class="comment">// in the money.</span>
std::cout << dblrule << std::endl;
std::cout << <span class="stringliteral">"Modified market conditions: in the money"</span> << std::endl;
std::cout << dblrule << std::endl;
std::cout << std::setw(widths[0]) << std::left << <span class="stringliteral">"Option"</span>
<< std::setw(widths[1]) << std::left << <span class="stringliteral">"NPV"</span>
<< std::setw(widths[2]) << std::left << <span class="stringliteral">"Error"</span>
<< std::endl;
std::cout << rule << std::endl;
underlying->setValue(90.0);
referenceValue = referenceOption.NPV();
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Original barrier option"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << referenceValue
<< std::setw(widths[2]) << std::left << <span class="stringliteral">"N/A"</span>
<< std::endl;
portfolioValue = portfolio1.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Replicating portfolio (12 dates)"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
portfolioValue = portfolio2.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Replicating portfolio (26 dates)"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
portfolioValue = portfolio3.<a class="code" href="class_quant_lib_1_1_instrument.html#5a137dafb316bb3644a8f92bbf4c1abb" title="returns the net present value of the instrument.">NPV</a>();
error = portfolioValue - referenceValue;
std::cout << std::setw(widths[0]) << std::left
<< <span class="stringliteral">"Replicating portfolio (52 dates)"</span>
<< std::fixed
<< std::setw(widths[1]) << std::left << portfolioValue
<< std::setw(widths[2]) << std::left << error
<< std::endl;
<span class="comment">// Finally, a word of warning for those (shame on them) who</span>
<span class="comment">// run the example but do not read the code.</span>
std::cout << dblrule << std::endl;
std::cout
<< std::endl
<< <span class="stringliteral">"The replication seems to be less robust when volatility and \n"</span>
<< <span class="stringliteral">"risk-free rate are changed. Feel free to experiment with \n"</span>
<< <span class="stringliteral">"the example and contribute a patch if you spot any errors."</span>
<< std::endl;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> seconds = timer.elapsed();
<a class="code" href="group__types.html#gb9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> hours = int(seconds/3600);
seconds -= hours * 3600;
<a class="code" href="group__types.html#gb9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> minutes = int(seconds/60);
seconds -= minutes * 60;
std::cout << <span class="stringliteral">" \nRun completed in "</span>;
<span class="keywordflow">if</span> (hours > 0)
std::cout << hours << <span class="stringliteral">" h "</span>;
<span class="keywordflow">if</span> (hours > 0 || minutes > 0)
std::cout << minutes << <span class="stringliteral">" m "</span>;
std::cout << std::fixed << std::setprecision(0)
<< seconds << <span class="stringliteral">" s\n"</span> << std::endl;
<span class="keywordflow">return</span> 0;
} <span class="keywordflow">catch</span> (std::exception& e) {
std::cout << e.what() << std::endl;
<span class="keywordflow">return</span> 1;
} <span class="keywordflow">catch</span> (...) {
std::cout << <span class="stringliteral">"unknown error"</span> << std::endl;
<span class="keywordflow">return</span> 1;
}
}
</pre></div>
</div>
<div class="footer">
<div class="endmatter">
Documentation generated by
<a href="http://www.doxygen.org">Doxygen</a> 1.5.4
</div>
</div>
</div>
</body>
</html>
|