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 452 453 454 455 456 457 458
|
<!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: DiscreteHedging.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>DiscreteHedging.cpp</h1>This is an example of using the QuantLib Monte Carlo framework.<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 computes profit and loss of a discrete interval hedging</span>
<span class="comment"> strategy and compares with the results of Derman & Kamal's (Goldman Sachs</span>
<span class="comment"> Equity Derivatives Research) Research Note: "When You Cannot Hedge</span>
<span class="comment"> Continuously: The Corrections to Black-Scholes"</span>
<span class="comment"> http://www.ederman.com/emanuelderman/GSQSpapers/when_you_cannot_hedge.pdf</span>
<span class="comment"></span>
<span class="comment"> Suppose an option hedger sells an European option and receives the</span>
<span class="comment"> Black-Scholes value as the options premium.</span>
<span class="comment"> Then he follows a Black-Scholes hedging strategy, rehedging at discrete,</span>
<span class="comment"> evenly spaced time intervals as the underlying stock changes. At</span>
<span class="comment"> expiration, the hedger delivers the option payoff to the option holder,</span>
<span class="comment"> and unwinds the hedge. We are interested in understanding the final</span>
<span class="comment"> profit or loss of this strategy.</span>
<span class="comment"></span>
<span class="comment"> If the hedger had followed the exact Black-Scholes replication strategy,</span>
<span class="comment"> re-hedging continuously as the underlying stock evolved towards its final</span>
<span class="comment"> value at expiration, then, no matter what path the stock took, the final</span>
<span class="comment"> P&L would be exactly zero. When the replication strategy deviates from</span>
<span class="comment"> the exact Black-Scholes method, the final P&L may deviate from zero. This</span>
<span class="comment"> deviation is called the replication error. When the hedger rebalances at</span>
<span class="comment"> discrete rather than continuous intervals, the hedge is imperfect and the</span>
<span class="comment"> replication is inexact. The more often hedging occurs, the smaller the</span>
<span class="comment"> replication error.</span>
<span class="comment"></span>
<span class="comment"> We examine the range of possibilities, computing the replication error.</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="comment">/* The ReplicationError class carries out Monte Carlo simulations to evaluate</span>
<span class="comment"> the outcome (the replication error) of the discrete hedging strategy over</span>
<span class="comment"> different, randomly generated scenarios of future stock price evolution.</span>
<span class="comment">*/</span>
<span class="keyword">class </span>ReplicationError
{
<span class="keyword">public</span>:
ReplicationError(Option::Type type,
<a name="a1"></a><a class="code" href="group__types.html#g14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> maturity,
<a name="a2"></a><a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> strike,
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> s0,
<a name="a3"></a><a class="code" href="group__types.html#gaa95ab7fe66935e3f7535413fad2a7d3" title="volatility">Volatility</a> sigma,
<a name="a4"></a><a class="code" href="group__types.html#gede435af51236692b1107d7639581d39" title="interest rates">Rate</a> r)
: maturity_(maturity), payoff_(type, strike), s0_(s0),
sigma_(sigma), r_(r) {
<span class="comment">// value of the option</span>
<a name="a5"></a><a class="code" href="group__types.html#g642a971a0bcbbd2fb26c35e1a06e5761" title="discount factor between dates">DiscountFactor</a> rDiscount = std::exp(-r_*maturity_);
<a class="code" href="group__types.html#g642a971a0bcbbd2fb26c35e1a06e5761" title="discount factor between dates">DiscountFactor</a> qDiscount = 1.0;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> forward = s0_*qDiscount/rDiscount;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> stdDev = std::sqrt(sigma_*sigma_*maturity_);
boost::shared_ptr<StrikedTypePayoff> payoff(
<span class="keyword">new</span> <a name="_a6"></a><a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(payoff_));
<a name="_a7"></a><a class="code" href="class_quant_lib_1_1_black_calculator.html" title="Black 1976 calculator class.">BlackCalculator</a> black(payoff,forward,stdDev,rDiscount);
std::cout << <span class="stringliteral">"Option value: "</span> << black.value() << std::endl;
<span class="comment">// store option's vega, since Derman and Kamal's formula needs it</span>
vega_ = black.vega(maturity_);
std::cout << std::endl;
std::cout << std::setw(8) << <span class="stringliteral">" "</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">" "</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">"P&L"</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">"P&L"</span> << <span class="stringliteral">" | "</span>
<< std::setw(12) << <span class="stringliteral">"Derman&Kamal"</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">"P&L"</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">"P&L"</span> << std::endl;
std::cout << std::setw(8) << <span class="stringliteral">"samples"</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">"trades"</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">"mean"</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">"std.dev."</span> << <span class="stringliteral">" | "</span>
<< std::setw(12) << <span class="stringliteral">"formula"</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">"skewness"</span> << <span class="stringliteral">" | "</span>
<< std::setw(8) << <span class="stringliteral">"kurtosis"</span> << std::endl;
std::cout << std::string(78, <span class="charliteral">'-'</span>) << std::endl;
}
<span class="comment">// the actual replication error computation</span>
<span class="keywordtype">void</span> compute(<a name="a8"></a><a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> nTimeSteps, <a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> nSamples);
<span class="keyword">private</span>:
<a class="code" href="group__types.html#g14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> maturity_;
<a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a> payoff_;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> s0_;
<a class="code" href="group__types.html#gaa95ab7fe66935e3f7535413fad2a7d3" title="volatility">Volatility</a> sigma_;
<a class="code" href="group__types.html#gede435af51236692b1107d7639581d39" title="interest rates">Rate</a> r_;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> vega_;
};
<span class="comment">// The key for the MonteCarlo simulation is to have a PathPricer that</span>
<span class="comment">// implements a value(const Path& path) method.</span>
<span class="comment">// This method prices the portfolio for each Path of the random variable</span>
<span class="keyword">class </span>ReplicationPathPricer : <span class="keyword">public</span> <a name="_a9"></a><a class="code" href="class_quant_lib_1_1_path_pricer.html" title="base class for path pricers">PathPricer</a><Path> {
<span class="keyword">public</span>:
<span class="comment">// real constructor</span>
ReplicationPathPricer(Option::Type type,
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> strike,
<a class="code" href="group__types.html#gede435af51236692b1107d7639581d39" title="interest rates">Rate</a> r,
<a class="code" href="group__types.html#g14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> maturity,
<a class="code" href="group__types.html#gaa95ab7fe66935e3f7535413fad2a7d3" title="volatility">Volatility</a> sigma)
: type_(type), strike_(strike),
r_(r), maturity_(maturity), sigma_(sigma) {
<a name="a10"></a><a class="code" href="errors_8hpp.html#7a9bcab8006882bc7d5302a0861ab1a6" title="throw an error if the given pre-condition is not verified">QL_REQUIRE</a>(strike_ > 0.0, <span class="stringliteral">"strike must be positive"</span>);
<a class="code" href="errors_8hpp.html#7a9bcab8006882bc7d5302a0861ab1a6" title="throw an error if the given pre-condition is not verified">QL_REQUIRE</a>(r_ >= 0.0,
<span class="stringliteral">"risk free rate (r) must be positive or zero"</span>);
<a class="code" href="errors_8hpp.html#7a9bcab8006882bc7d5302a0861ab1a6" title="throw an error if the given pre-condition is not verified">QL_REQUIRE</a>(maturity_ > 0.0, <span class="stringliteral">"maturity must be positive"</span>);
<a class="code" href="errors_8hpp.html#7a9bcab8006882bc7d5302a0861ab1a6" title="throw an error if the given pre-condition is not verified">QL_REQUIRE</a>(sigma_ >= 0.0,
<span class="stringliteral">"volatility (sigma) must be positive or zero"</span>);
}
<span class="comment">// The value() method encapsulates the pricing code</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> operator()(<span class="keyword">const</span> <a name="_a11"></a><a class="code" href="class_quant_lib_1_1_path.html" title="single-factor random walk">Path</a>& path) <span class="keyword">const</span>;
<span class="keyword">private</span>:
Option::Type type_;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> strike_;
<a class="code" href="group__types.html#gede435af51236692b1107d7639581d39" title="interest rates">Rate</a> r_;
<a class="code" href="group__types.html#g14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> maturity_;
<a class="code" href="group__types.html#gaa95ab7fe66935e3f7535413fad2a7d3" title="volatility">Volatility</a> sigma_;
};
<span class="comment">// Compute Replication Error as in the Derman and Kamal's research note</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 class="code" href="group__types.html#g14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> maturity = 1.0/12.0; <span class="comment">// 1 month</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> strike = 100;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> underlying = 100;
<a class="code" href="group__types.html#gaa95ab7fe66935e3f7535413fad2a7d3" title="volatility">Volatility</a> <a name="a12"></a><a class="code" href="group__manips.html#gc402ef7c87f63f7c603ee87210b5750c" title="output volatilities as percentages">volatility</a> = 0.20; <span class="comment">// 20%</span>
<a class="code" href="group__types.html#gede435af51236692b1107d7639581d39" title="interest rates">Rate</a> riskFreeRate = 0.05; <span class="comment">// 5%</span>
ReplicationError rp(Option::Call, maturity, strike, underlying,
volatility, riskFreeRate);
<a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> scenarios = 50000;
<a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> hedgesNum;
hedgesNum = 21;
rp.compute(hedgesNum, scenarios);
hedgesNum = 84;
rp.compute(hedgesNum, scenarios);
<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;
}
}
<span class="comment">/* The actual computation of the Profit&Loss for each single path.</span>
<span class="comment"></span>
<span class="comment"> In each scenario N rehedging trades spaced evenly in time over</span>
<span class="comment"> the life of the option are carried out, using the Black-Scholes</span>
<span class="comment"> hedge ratio.</span>
<span class="comment">*/</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> ReplicationPathPricer::operator()(<span class="keyword">const</span> <a class="code" href="class_quant_lib_1_1_path.html" title="single-factor random walk">Path</a>& path)<span class="keyword"> const </span>{
<a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> n = path.<a name="a13"></a><a class="code" href="class_quant_lib_1_1_path.html#0004e5f7457adee64d3a05ee24509aaa">length</a>()-1;
<a class="code" href="errors_8hpp.html#7a9bcab8006882bc7d5302a0861ab1a6" title="throw an error if the given pre-condition is not verified">QL_REQUIRE</a>(n>0, <span class="stringliteral">"the path cannot be empty"</span>);
<span class="comment">// discrete hedging interval</span>
<a class="code" href="group__types.html#g14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> dt = maturity_/n;
<span class="comment">// For simplicity, we assume the stock pays no dividends.</span>
<a class="code" href="group__types.html#gede435af51236692b1107d7639581d39" title="interest rates">Rate</a> stockDividendYield = 0.0;
<span class="comment">// let's start</span>
<a class="code" href="group__types.html#g14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> t = 0;
<span class="comment">// stock value at t=0</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> stock = path.<a name="a14"></a><a class="code" href="class_quant_lib_1_1_path.html#df80e67a5954b0635539898d1de0bac3" title="initial asset value">front</a>();
<span class="comment">// money account at t=0</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> money_account = 0.0;
<span class="comment">/************************/</span>
<span class="comment">/*** the initial deal ***/</span>
<span class="comment">/************************/</span>
<span class="comment">// option fair price (Black-Scholes) at t=0</span>
<a class="code" href="group__types.html#g642a971a0bcbbd2fb26c35e1a06e5761" title="discount factor between dates">DiscountFactor</a> rDiscount = std::exp(-r_*maturity_);
<a class="code" href="group__types.html#g642a971a0bcbbd2fb26c35e1a06e5761" title="discount factor between dates">DiscountFactor</a> qDiscount = std::exp(-stockDividendYield*maturity_);
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> forward = stock*qDiscount/rDiscount;
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> stdDev = std::sqrt(sigma_*sigma_*maturity_);
boost::shared_ptr<StrikedTypePayoff> payoff(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(type_,strike_));
<a class="code" href="class_quant_lib_1_1_black_calculator.html" title="Black 1976 calculator class.">BlackCalculator</a> black(payoff,forward,stdDev,rDiscount);
<span class="comment">// sell the option, cash in its premium</span>
money_account += black.value();
<span class="comment">// compute delta</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> delta = black.delta(stock);
<span class="comment">// delta-hedge the option buying stock</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> stockAmount = delta;
money_account -= stockAmount*stock;
<span class="comment">/**********************************/</span>
<span class="comment">/*** hedging during option life ***/</span>
<span class="comment">/**********************************/</span>
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> step = 0; step < n-1; step++){
<span class="comment">// time flows</span>
t += dt;
<span class="comment">// accruing on the money account</span>
money_account *= std::exp( r_*dt );
<span class="comment">// stock growth:</span>
stock = path[step+1];
<span class="comment">// recalculate option value at the current stock value,</span>
<span class="comment">// and the current time to maturity</span>
rDiscount = std::exp(-r_*(maturity_-t));
qDiscount = std::exp(-stockDividendYield*(maturity_-t));
forward = stock*qDiscount/rDiscount;
stdDev = std::sqrt(sigma_*sigma_*(maturity_-t));
<a class="code" href="class_quant_lib_1_1_black_calculator.html" title="Black 1976 calculator class.">BlackCalculator</a> black(payoff,forward,stdDev,rDiscount);
<span class="comment">// recalculate delta</span>
delta = black.delta(stock);
<span class="comment">// re-hedging</span>
money_account -= (delta - stockAmount)*stock;
stockAmount = delta;
}
<span class="comment">/*************************/</span>
<span class="comment">/*** option expiration ***/</span>
<span class="comment">/*************************/</span>
<span class="comment">// last accrual on my money account</span>
money_account *= std::exp( r_*dt );
<span class="comment">// last stock growth</span>
stock = path[n];
<span class="comment">// the hedger delivers the option payoff to the option holder</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> optionPayoff = <a class="code" href="class_quant_lib_1_1_plain_vanilla_payoff.html" title="Plain-vanilla payoff.">PlainVanillaPayoff</a>(type_, strike_)(stock);
money_account -= optionPayoff;
<span class="comment">// and unwinds the hedge selling his stock position</span>
money_account += stockAmount*stock;
<span class="comment">// final Profit&Loss</span>
<span class="keywordflow">return</span> money_account;
}
<span class="comment">// The computation over nSamples paths of the P&L distribution</span>
<span class="keywordtype">void</span> ReplicationError::compute(<a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> nTimeSteps, <a class="code" href="group__types.html#gf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> nSamples)
{
<a class="code" href="errors_8hpp.html#7a9bcab8006882bc7d5302a0861ab1a6" title="throw an error if the given pre-condition is not verified">QL_REQUIRE</a>(nTimeSteps>0, <span class="stringliteral">"the number of steps must be > 0"</span>);
<span class="comment">// hedging interval</span>
<span class="comment">// Time tau = maturity_ / nTimeSteps;</span>
<span class="comment">/* Black-Scholes framework: the underlying stock price evolves</span>
<span class="comment"> lognormally with a fixed known volatility that stays constant</span>
<span class="comment"> throughout time.</span>
<span class="comment"> */</span>
<a name="_a15"></a><a class="code" href="class_quant_lib_1_1_calendar.html" title="calendar class">Calendar</a> calendar = <a name="_a16"></a><a class="code" href="class_quant_lib_1_1_t_a_r_g_e_t.html" title="TARGET calendar">TARGET</a>();
<a name="_a17"></a><a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> today = Date::todaysDate();
<a name="_a18"></a><a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> dayCount = <a name="_a19"></a><a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>();
<a name="_a20"></a><a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<Quote></a> stateVariable(
boost::shared_ptr<Quote>(<span class="keyword">new</span> <a name="_a21"></a><a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(s0_)));
<a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<YieldTermStructure></a> riskFreeRate(
boost::shared_ptr<YieldTermStructure>(
<span class="keyword">new</span> <a name="_a22"></a><a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(today, r_, dayCount)));
<a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<YieldTermStructure></a> dividendYield(
boost::shared_ptr<YieldTermStructure>(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(today, 0.0, dayCount)));
<a class="code" href="class_quant_lib_1_1_handle.html" title="Shared handle to an observable.">Handle<BlackVolTermStructure></a> <a class="code" href="group__manips.html#gc402ef7c87f63f7c603ee87210b5750c" title="output volatilities as percentages">volatility</a>(
boost::shared_ptr<BlackVolTermStructure>(
<span class="keyword">new</span> <a name="_a23"></a><a class="code" href="class_quant_lib_1_1_black_constant_vol.html" title="Constant Black volatility, no time-strike dependence.">BlackConstantVol</a>(today, calendar, sigma_, dayCount)));
boost::shared_ptr<StochasticProcess1D> diffusion(
<span class="keyword">new</span> <a name="_a24"></a><a class="code" href="class_quant_lib_1_1_black_scholes_merton_process.html" title="Merton (1973) extension to the Black-Scholes stochastic process.">BlackScholesMertonProcess</a>(stateVariable, dividendYield,
riskFreeRate, volatility));
<span class="comment">// Black Scholes equation rules the path generator:</span>
<span class="comment">// at each step the log of the stock</span>
<span class="comment">// will have drift and sigma^2 variance</span>
<a name="_a25"></a><a class="code" href="class_quant_lib_1_1_inverse_cumulative_rsg.html" title="Inverse cumulative random sequence generator.">PseudoRandom::rsg_type</a> rsg =
PseudoRandom::make_sequence_generator(nTimeSteps, 0);
<span class="keywordtype">bool</span> brownianBridge = <span class="keyword">false</span>;
<span class="keyword">typedef</span> <a name="_a26"></a><a class="code" href="struct_quant_lib_1_1_single_variate.html" title="default Monte Carlo traits for single-variate models">SingleVariate<PseudoRandom>::path_generator_type</a> generator_type;
boost::shared_ptr<generator_type> myPathGenerator(<span class="keyword">new</span>
generator_type(diffusion, maturity_, nTimeSteps,
rsg, brownianBridge));
<span class="comment">// The replication strategy's Profit&Loss is computed for each path</span>
<span class="comment">// of the stock. The path pricer knows how to price a path using its</span>
<span class="comment">// value() method</span>
boost::shared_ptr<PathPricer<Path> > myPathPricer(<span class="keyword">new</span>
ReplicationPathPricer(payoff_.optionType(), payoff_.strike(),
r_, maturity_, sigma_));
<span class="comment">// a statistics accumulator for the path-dependant Profit&Loss values</span>
<a name="_a27"></a><a class="code" href="class_quant_lib_1_1_generic_risk_statistics.html" title="empirical-distribution risk measures">Statistics</a> statisticsAccumulator;
<span class="comment">// The Monte Carlo model generates paths using myPathGenerator</span>
<span class="comment">// each path is priced using myPathPricer</span>
<span class="comment">// prices will be accumulated into statisticsAccumulator</span>
<a name="_a28"></a><a class="code" href="class_quant_lib_1_1_monte_carlo_model.html" title="General-purpose Monte Carlo model for path samples.">MonteCarloModel<SingleVariate,PseudoRandom></a>
MCSimulation(myPathGenerator,
myPathPricer,
statisticsAccumulator,
<span class="keyword">false</span>);
<span class="comment">// the model simulates nSamples paths</span>
MCSimulation.addSamples(nSamples);
<span class="comment">// the sampleAccumulator method</span>
<span class="comment">// gives access to all the methods of statisticsAccumulator</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> PLMean = MCSimulation.sampleAccumulator().mean();
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> PLStDev = MCSimulation.sampleAccumulator().standardDeviation();
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> PLSkew = MCSimulation.sampleAccumulator().skewness();
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> PLKurt = MCSimulation.sampleAccumulator().kurtosis();
<span class="comment">// Derman and Kamal's formula</span>
<a class="code" href="group__types.html#g4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> theorStD = std::sqrt(M_PI/4/nTimeSteps)*vega_*sigma_;
std::cout << std::fixed
<< std::setw(8) << nSamples << <span class="stringliteral">" | "</span>
<< std::setw(8) << nTimeSteps << <span class="stringliteral">" | "</span>
<< std::setw(8) << std::setprecision(3) << PLMean << <span class="stringliteral">" | "</span>
<< std::setw(8) << std::setprecision(2) << PLStDev << <span class="stringliteral">" | "</span>
<< std::setw(12) << std::setprecision(2) << theorStD << <span class="stringliteral">" | "</span>
<< std::setw(8) << std::setprecision(2) << PLSkew << <span class="stringliteral">" | "</span>
<< std::setw(8) << std::setprecision(2) << PLKurt << std::endl;
}
</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>
|