File: _equity_option_8cpp-example.html

package info (click to toggle)
quantlib-refman-html 1.2-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 84,552 kB
  • ctags: 5,132
  • sloc: makefile: 33
file content (482 lines) | stat: -rw-r--r-- 40,652 bytes parent folder | download
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!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>EquityOption.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="185" height="50" 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 1.2</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="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="todo.html">Todo List</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.7.6.1 -->
</div>
<div class="header">
  <div class="headertitle">
<div class="title">EquityOption.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>This example evaluates European, American and Bermudan options using different methods.</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">// the only header you need to use QuantLib</span>
<span class="preprocessor">#include &lt;ql/quantlib.hpp&gt;</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&amp;threadid=9481</span>
<span class="comment">   Is there anyone with a definitive word about this?</span>
<span class="comment">*/</span>
<span class="comment">// #include &lt;float.h&gt;</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 &lt;boost/timer.hpp&gt;</span>
<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;iomanip&gt;</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#gab9c87440c314438e51a899a03d2442d0" 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 &lt;&lt; std::endl;

        <span class="comment">// set up dates</span>
        <a name="_a1"></a><a class="code" href="class_quant_lib_1_1_calendar.html" title="calendar class">Calendar</a> calendar = <a name="_a2"></a><a class="code" href="class_quant_lib_1_1_t_a_r_g_e_t.html" title="TARGET calendar">TARGET</a>();
        <a name="_a3"></a><a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> todaysDate(15, May, 1998);
        <a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> settlementDate(17, May, 1998);
        <a name="a4"></a><a class="code" href="class_quant_lib_1_1_singleton.html#ab7455b7e1235d292c444095842349291" title="access to the unique instance">Settings::instance</a>().<a name="a5"></a><a class="code" href="class_quant_lib_1_1_settings.html#a95055e9410ed0465a5f30d3ffc90c1d3" title="the date at which pricing is to be performed.">evaluationDate</a>() = todaysDate;

        <span class="comment">// our options</span>
        Option::Type type(Option::Put);
        <a name="a6"></a><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> underlying = 36;
        <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> strike = 40;
        <a name="a7"></a><a class="code" href="group__types.html#gae7427f4743503002b0c6eeeefae91a3d" title="spreads on interest rates">Spread</a> dividendYield = 0.00;
        <a name="a8"></a><a class="code" href="group__types.html#gaede435af51236692b1107d7639581d39" title="interest rates">Rate</a> riskFreeRate = 0.06;
        <a name="a9"></a><a class="code" href="group__types.html#gaaa95ab7fe66935e3f7535413fad2a7d3" title="volatility">Volatility</a> <a name="a10"></a><a class="code" href="group__manips.html#gac402ef7c87f63f7c603ee87210b5750c" title="output volatilities as percentages">volatility</a> = 0.20;
        <a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> maturity(17, May, 1999);
        <a name="_a11"></a><a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> dayCounter = <a name="_a12"></a><a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>();

        std::cout &lt;&lt; <span class="stringliteral">&quot;Option type = &quot;</span>  &lt;&lt; type &lt;&lt; std::endl;
        std::cout &lt;&lt; <span class="stringliteral">&quot;Maturity = &quot;</span>        &lt;&lt; maturity &lt;&lt; std::endl;
        std::cout &lt;&lt; <span class="stringliteral">&quot;Underlying price = &quot;</span>        &lt;&lt; underlying &lt;&lt; std::endl;
        std::cout &lt;&lt; <span class="stringliteral">&quot;Strike = &quot;</span>                  &lt;&lt; strike &lt;&lt; std::endl;
        std::cout &lt;&lt; <span class="stringliteral">&quot;Risk-free interest rate = &quot;</span> &lt;&lt; <a name="a13"></a><a class="code" href="group__manips.html#gac63ea3dad10259db3764c7d15a61cde5" title="output rates and spreads as percentages">io::rate</a>(riskFreeRate)
                  &lt;&lt; std::endl;
        std::cout &lt;&lt; <span class="stringliteral">&quot;Dividend yield = &quot;</span> &lt;&lt; <a class="code" href="group__manips.html#gac63ea3dad10259db3764c7d15a61cde5" title="output rates and spreads as percentages">io::rate</a>(dividendYield)
                  &lt;&lt; std::endl;
        std::cout &lt;&lt; <span class="stringliteral">&quot;Volatility = &quot;</span> &lt;&lt; <a class="code" href="group__manips.html#gac402ef7c87f63f7c603ee87210b5750c" title="output volatilities as percentages">io::volatility</a>(volatility)
                  &lt;&lt; std::endl;
        std::cout &lt;&lt; std::endl;
        std::string method;
        std::cout &lt;&lt; std::endl ;

        <span class="comment">// write column headings</span>
        <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> widths[] = { 35, 14, 14, 14 };
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;Method&quot;</span>
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;European&quot;</span>
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;Bermudan&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;American&quot;</span>
                  &lt;&lt; std::endl;

        std::vector&lt;Date&gt; exerciseDates;
        <span class="keywordflow">for</span> (<a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> i=1; i&lt;=4; i++)
            exerciseDates.push_back(settlementDate + 3*i*Months);

        boost::shared_ptr&lt;Exercise&gt; europeanExercise(
                                         <span class="keyword">new</span> <a name="_a14"></a><a class="code" href="class_quant_lib_1_1_european_exercise.html" title="European exercise.">EuropeanExercise</a>(maturity));

        boost::shared_ptr&lt;Exercise&gt; bermudanExercise(
                                         <span class="keyword">new</span> <a name="_a15"></a><a class="code" href="class_quant_lib_1_1_bermudan_exercise.html" title="Bermudan exercise.">BermudanExercise</a>(exerciseDates));

        boost::shared_ptr&lt;Exercise&gt; americanExercise(
                                         <span class="keyword">new</span> <a name="_a16"></a><a class="code" href="class_quant_lib_1_1_american_exercise.html" title="American exercise.">AmericanExercise</a>(settlementDate,
                                                              maturity));

        <a name="_a17"></a><a class="code" href="class_quant_lib_1_1_handle.html">Handle&lt;Quote&gt;</a> underlyingH(
            boost::shared_ptr&lt;Quote&gt;(<span class="keyword">new</span> <a name="_a18"></a><a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(underlying)));

        <span class="comment">// bootstrap the yield/dividend/vol curves</span>
        <a name="_a19"></a><a class="code" href="class_quant_lib_1_1_handle.html">Handle&lt;YieldTermStructure&gt;</a> flatTermStructure(
            boost::shared_ptr&lt;YieldTermStructure&gt;(
                <span class="keyword">new</span> <a name="_a20"></a><a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(settlementDate, riskFreeRate, dayCounter)));
        <a class="code" href="class_quant_lib_1_1_handle.html">Handle&lt;YieldTermStructure&gt;</a> flatDividendTS(
            boost::shared_ptr&lt;YieldTermStructure&gt;(
                <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(settlementDate, dividendYield, dayCounter)));
        <a name="_a21"></a><a class="code" href="class_quant_lib_1_1_handle.html">Handle&lt;BlackVolTermStructure&gt;</a> flatVolTS(
            boost::shared_ptr&lt;BlackVolTermStructure&gt;(
                <span class="keyword">new</span> <a name="_a22"></a><a class="code" href="class_quant_lib_1_1_black_constant_vol.html" title="Constant Black volatility, no time-strike dependence.">BlackConstantVol</a>(settlementDate, calendar, volatility,
                                     dayCounter)));
        boost::shared_ptr&lt;StrikedTypePayoff&gt; payoff(
                                        <span class="keyword">new</span> <a name="_a23"></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&lt;BlackScholesMertonProcess&gt; bsmProcess(
                 <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>(underlyingH, flatDividendTS,
                                               flatTermStructure, flatVolTS));

        <span class="comment">// options</span>
        <a name="_a25"></a><a class="code" href="class_quant_lib_1_1_vanilla_option.html" title="Vanilla option (no discrete dividends, no barriers) on a single asset.">VanillaOption</a> europeanOption(payoff, europeanExercise);
        <a class="code" href="class_quant_lib_1_1_vanilla_option.html" title="Vanilla option (no discrete dividends, no barriers) on a single asset.">VanillaOption</a> bermudanOption(payoff, bermudanExercise);
        <a class="code" href="class_quant_lib_1_1_vanilla_option.html" title="Vanilla option (no discrete dividends, no barriers) on a single asset.">VanillaOption</a> americanOption(payoff, americanExercise);

        <span class="comment">// Analytic formulas:</span>

        <span class="comment">// Black-Scholes for European</span>
        method = <span class="stringliteral">&quot;Black-Scholes&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                                     <span class="keyword">new</span> <a name="_a26"></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>(bsmProcess)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::endl;

        <span class="comment">// semi-analytic Heston for European</span>
        method = <span class="stringliteral">&quot;Heston semi-analytic&quot;</span>;
        boost::shared_ptr&lt;HestonProcess&gt; hestonProcess(
            <span class="keyword">new</span> <a name="_a27"></a><a class="code" href="class_quant_lib_1_1_heston_process.html" title="Square-root stochastic-volatility Heston process.">HestonProcess</a>(flatTermStructure, flatDividendTS,
                              underlyingH, volatility*volatility,
                              1.0, volatility*volatility, 0.001, 0.0));
        boost::shared_ptr&lt;HestonModel&gt; hestonModel(
                                              <span class="keyword">new</span> <a name="_a28"></a><a class="code" href="class_quant_lib_1_1_heston_model.html" title="Heston model for the stochastic volatility of an asset.">HestonModel</a>(hestonProcess));
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                                     <span class="keyword">new</span> <a name="_a29"></a><a class="code" href="class_quant_lib_1_1_analytic_heston_engine.html" title="analytic Heston-model engine based on Fourier transform">AnalyticHestonEngine</a>(hestonModel)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::endl;

        <span class="comment">// semi-analytic Bates for European</span>
        method = <span class="stringliteral">&quot;Bates semi-analytic&quot;</span>;
        boost::shared_ptr&lt;BatesProcess&gt; batesProcess(
            <span class="keyword">new</span> <a name="_a30"></a><a class="code" href="class_quant_lib_1_1_bates_process.html" title="Square-root stochastic-volatility Bates process.">BatesProcess</a>(flatTermStructure, flatDividendTS,
                             underlyingH, volatility*volatility,
                             1.0, volatility*volatility, 0.001, 0.0,
                             1e-14, 1e-14, 1e-14));
        boost::shared_ptr&lt;BatesModel&gt; batesModel(<span class="keyword">new</span> <a name="_a31"></a><a class="code" href="class_quant_lib_1_1_bates_model.html" title="Bates stochastic-volatility model.">BatesModel</a>(batesProcess));
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                                                <span class="keyword">new</span> <a name="_a32"></a><a class="code" href="class_quant_lib_1_1_bates_engine.html" title="Bates model engines based on Fourier transform.">BatesEngine</a>(batesModel)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::endl;

        <span class="comment">// Barone-Adesi and Whaley approximation for American</span>
        method = <span class="stringliteral">&quot;Barone-Adesi/Whaley&quot;</span>;
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                       <span class="keyword">new</span> <a name="_a33"></a><a class="code" href="class_quant_lib_1_1_barone_adesi_whaley_approximation_engine.html" title="Barone-Adesi and Whaley pricing engine for American options (1987)">BaroneAdesiWhaleyApproximationEngine</a>(bsmProcess)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// Bjerksund and Stensland approximation for American</span>
        method = <span class="stringliteral">&quot;Bjerksund/Stensland&quot;</span>;
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                      <span class="keyword">new</span> <a name="_a34"></a><a class="code" href="class_quant_lib_1_1_bjerksund_stensland_approximation_engine.html" title="Bjerksund and Stensland pricing engine for American options (1993)">BjerksundStenslandApproximationEngine</a>(bsmProcess)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// Integral</span>
        method = <span class="stringliteral">&quot;Integral&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                                             <span class="keyword">new</span> <a name="_a35"></a><a class="code" href="class_quant_lib_1_1_integral_engine.html" title="Pricing engine for European vanilla options using integral approach.">IntegralEngine</a>(bsmProcess)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::endl;

        <span class="comment">// Finite differences</span>
        <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> timeSteps = 801;
        method = <span class="stringliteral">&quot;Finite differences&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                 <span class="keyword">new</span> <a name="_a36"></a><a class="code" href="class_quant_lib_1_1_f_d_european_engine.html" title="Pricing engine for European options using finite-differences.">FDEuropeanEngine&lt;CrankNicolson&gt;</a>(bsmProcess,
                                                     timeSteps,timeSteps-1)));
        bermudanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                 <span class="keyword">new</span> <a name="_a37"></a><a class="code" href="class_quant_lib_1_1_f_d_bermudan_engine.html" title="Finite-differences Bermudan engine.">FDBermudanEngine&lt;CrankNicolson&gt;</a>(bsmProcess,
                                                     timeSteps,timeSteps-1)));
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                 <span class="keyword">new</span> <a name="_a38"></a><a class="code" href="class_quant_lib_1_1_f_d_american_engine.html" title="Finite-differences pricing engine for American one asset options.">FDAmericanEngine&lt;CrankNicolson&gt;</a>(bsmProcess,
                                                     timeSteps,timeSteps-1)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; bermudanOption.NPV()
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// Binomial method: Jarrow-Rudd</span>
        method = <span class="stringliteral">&quot;Binomial Jarrow-Rudd&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                <span class="keyword">new</span> <a name="_a39"></a><a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;JarrowRudd&gt;</a>(bsmProcess,timeSteps)));
        bermudanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;JarrowRudd&gt;</a>(bsmProcess,timeSteps)));
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;JarrowRudd&gt;</a>(bsmProcess,timeSteps)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; bermudanOption.NPV()
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;
        method = <span class="stringliteral">&quot;Binomial Cox-Ross-Rubinstein&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                      <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;CoxRossRubinstein&gt;</a>(bsmProcess,
                                                                   timeSteps)));
        bermudanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                      <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;CoxRossRubinstein&gt;</a>(bsmProcess,
                                                                   timeSteps)));
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                      <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;CoxRossRubinstein&gt;</a>(bsmProcess,
                                                                   timeSteps)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; bermudanOption.NPV()
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// Binomial method: Additive equiprobabilities</span>
        method = <span class="stringliteral">&quot;Additive equiprobabilities&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;AdditiveEQPBinomialTree&gt;</a>(bsmProcess,
                                                                   timeSteps)));
        bermudanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;AdditiveEQPBinomialTree&gt;</a>(bsmProcess,
                                                                   timeSteps)));
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;AdditiveEQPBinomialTree&gt;</a>(bsmProcess,
                                                                   timeSteps)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; bermudanOption.NPV()
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// Binomial method: Binomial Trigeorgis</span>
        method = <span class="stringliteral">&quot;Binomial Trigeorgis&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;Trigeorgis&gt;</a>(bsmProcess,timeSteps)));
        bermudanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;Trigeorgis&gt;</a>(bsmProcess,timeSteps)));
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;Trigeorgis&gt;</a>(bsmProcess,timeSteps)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; bermudanOption.NPV()
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// Binomial method: Binomial Tian</span>
        method = <span class="stringliteral">&quot;Binomial Tian&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                      <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;Tian&gt;</a>(bsmProcess,timeSteps)));
        bermudanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                      <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;Tian&gt;</a>(bsmProcess,timeSteps)));
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                      <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;Tian&gt;</a>(bsmProcess,timeSteps)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; bermudanOption.NPV()
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// Binomial method: Binomial Leisen-Reimer</span>
        method = <span class="stringliteral">&quot;Binomial Leisen-Reimer&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
              <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;LeisenReimer&gt;</a>(bsmProcess,timeSteps)));
        bermudanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
              <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;LeisenReimer&gt;</a>(bsmProcess,timeSteps)));
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
              <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;LeisenReimer&gt;</a>(bsmProcess,timeSteps)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; bermudanOption.NPV()
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// Binomial method: Binomial Joshi</span>
        method = <span class="stringliteral">&quot;Binomial Joshi&quot;</span>;
        europeanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                    <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;Joshi4&gt;</a>(bsmProcess,timeSteps)));
        bermudanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                    <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;Joshi4&gt;</a>(bsmProcess,timeSteps)));
        americanOption.setPricingEngine(boost::shared_ptr&lt;PricingEngine&gt;(
                    <span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_binomial_vanilla_engine.html" title="Pricing engine for vanilla options using binomial trees.">BinomialVanillaEngine&lt;Joshi4&gt;</a>(bsmProcess,timeSteps)));
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; bermudanOption.NPV()
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// Monte Carlo Method: MC (crude)</span>
        timeSteps = 1;
        method = <span class="stringliteral">&quot;MC (crude)&quot;</span>;
        <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> mcSeed = 42;
        boost::shared_ptr&lt;PricingEngine&gt; mcengine1;
        mcengine1 = <a name="_a40"></a><a class="code" href="class_quant_lib_1_1_make_m_c_european_engine.html" title="Monte Carlo European engine factory.">MakeMCEuropeanEngine&lt;PseudoRandom&gt;</a>(bsmProcess)
            .withSteps(timeSteps)
            .<a name="a41"></a>withAbsoluteTolerance(0.02)
            .<a name="a42"></a>withSeed(mcSeed);
        europeanOption.setPricingEngine(mcengine1);
        <span class="comment">// Real errorEstimate = europeanOption.errorEstimate();</span>
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::endl;

        <span class="comment">// Monte Carlo Method: QMC (Sobol)</span>
        method = <span class="stringliteral">&quot;QMC (Sobol)&quot;</span>;
        <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> nSamples = 32768;  <span class="comment">// 2^15</span>

        boost::shared_ptr&lt;PricingEngine&gt; mcengine2;
        mcengine2 = <a class="code" href="class_quant_lib_1_1_make_m_c_european_engine.html" title="Monte Carlo European engine factory.">MakeMCEuropeanEngine&lt;LowDiscrepancy&gt;</a>(bsmProcess)
            .withSteps(timeSteps)
            .<a name="a43"></a>withSamples(nSamples);
        europeanOption.setPricingEngine(mcengine2);
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; europeanOption.NPV()
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::endl;

        <span class="comment">// Monte Carlo Method: MC (Longstaff Schwartz)</span>
        method = <span class="stringliteral">&quot;MC (Longstaff Schwartz)&quot;</span>;
        boost::shared_ptr&lt;PricingEngine&gt; mcengine3;
        mcengine3 = <a name="_a44"></a><a class="code" href="class_quant_lib_1_1_make_m_c_american_engine.html" title="Monte Carlo American engine factory.">MakeMCAmericanEngine&lt;PseudoRandom&gt;</a>(bsmProcess)
            .withSteps(100)
            .<a name="a45"></a>withAntitheticVariate()
            .<a name="a46"></a>withCalibrationSamples(4096)
            .<a name="a47"></a>withAbsoluteTolerance(0.02)
            .<a name="a48"></a>withSeed(mcSeed);
        americanOption.setPricingEngine(mcengine3);
        std::cout &lt;&lt; std::setw(widths[0]) &lt;&lt; std::left &lt;&lt; method
                  &lt;&lt; std::fixed
                  &lt;&lt; std::setw(widths[1]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[2]) &lt;&lt; std::left &lt;&lt; <span class="stringliteral">&quot;N/A&quot;</span>
                  &lt;&lt; std::setw(widths[3]) &lt;&lt; std::left &lt;&lt; americanOption.NPV()
                  &lt;&lt; std::endl;

        <span class="comment">// End test</span>
        <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> seconds = timer.elapsed();
        <a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> hours = int(seconds/3600);
        seconds -= hours * 3600;
        <a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> minutes = int(seconds/60);
        seconds -= minutes * 60;
        std::cout &lt;&lt; <span class="stringliteral">&quot; \nRun completed in &quot;</span>;
        <span class="keywordflow">if</span> (hours &gt; 0)
            std::cout &lt;&lt; hours &lt;&lt; <span class="stringliteral">&quot; h &quot;</span>;
        <span class="keywordflow">if</span> (hours &gt; 0 || minutes &gt; 0)
            std::cout &lt;&lt; minutes &lt;&lt; <span class="stringliteral">&quot; m &quot;</span>;
        std::cout &lt;&lt; std::fixed &lt;&lt; std::setprecision(0)
                  &lt;&lt; seconds &lt;&lt; <span class="stringliteral">&quot; s\n&quot;</span> &lt;&lt; std::endl;
        <span class="keywordflow">return</span> 0;

    } <span class="keywordflow">catch</span> (std::exception&amp; e) {
        std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
        <span class="keywordflow">return</span> 1;
    } <span class="keywordflow">catch</span> (...) {
        std::cerr &lt;&lt; <span class="stringliteral">&quot;unknown error&quot;</span> &lt;&lt; std::endl;
        <span class="keywordflow">return</span> 1;
    }
}
</pre></div> </div><!-- contents -->
</div><!-- contents -->

</div>

<div class="footer">
<div class="endmatter">
Documentation generated by
<a href="http://www.doxygen.org">Doxygen</a> 1.7.6.1
</div>
</div>

</div>

</body>
</html>