 Shopping Bag ( 0 items )

All (14) from $3.00

New (9) from $82.14

Used (5) from $3.00
More About This Textbook
Overview
In this book, author Daniel J. Duffy brings C++ to the next level by applying it to the design and implementation of classes, libraries and applications for option and derivative pricing models. He employs modern software engineering techniques to produce industrialstrength applications:
Included with the book is a CD containing the source code in the Datasim Financial Toolkit. You can use this to get up to speed with your C++ applications by reusing existing classes and libraries.
'Unique... Let's all give a warm welcome to modern pricing tools.'
 Paul Wilmott, mathematician, author and fund manager
Product Details
Related Subjects
Meet the Author
Read an Excerpt
Table of Contents
1 Executive Overview of this Book 1
1.1 What is this book? 1
1.2 What’s special about this book? 1
1.3 Who is this book for? 2
1.4 Software requirements 3
1.5 The structure of this book 4
1.6 Pedagogical approach 5
1.7 What this book is not 6
1.8 Source code on the CD 6
PART I TEMPLATE PROGRAMMING IN C++
2 A Gentle Introduction to Templates in C++ 9
2.1 Introduction and objectives 9
2.2 Motivation and background 10
2.3 Defining a template 11
2.3.1 An example 13
2.4 Template instantiation 15
2.5 Function templates 16
2.5.1 An example 17
2.6 Default values and typedefs 18
2.7 Guidelines when implementing templates 18
2.8 Conclusions and summary 19
3 An Introduction to the Standard Template Library 20
3.1 Introduction and objectives 20
3.2 A Bird’seye view of STL 20
3.3 Sequence containers 23
3.4 Associative containers 27
3.5 Iterators in STL 30
3.6 Algorithms 33
3.7 Using STL for financial instruments 35
3.8 Conclusions and summary 35
4 STL for Financial Engineering Applications 36
4.1 Introduction and objectives 36
4.2 Clever data structures 36
4.3 Set theory and STL 40
4.4 Useful algorithms 43
4.5 STL adaptor containers 45
4.6 Conclusions and summary 46
5 The Property Pattern in Financial Engineering 47
5.1 Introduction and objectives 47
5.2 The Property pattern 47
5.3 An example 51
5.4 Extending the Property pattern: property sets and property lists 52
5.5 Properties and exotic options 57
5.6 Conclusions and summary 59
PART II BUILDING BLOCK CLASSES
6 Arrays, Vectors and Matrices 63
6.1 Introduction and objectives 63
6.2 Motivation and background 64
6.3 A layered approach 66
6.4 The Array and Matrix classes in detail 66
6.5 The Vector and NumericMatrix classes in detail 72
6.6 Associative arrays and matrices 74
6.7 Conclusions and summary 77
7 Arrays and Matrix Properties 78
7.1 Introduction and objectives 78
7.2 An overview of the functionality 78
7.3 Software requirements 79
7.4 The core processes 80
7.5 Other function categories 85
7.6 Using the functions 87
7.7 An introduction to exception handling 88
7.8 Conclusions and summary 90
8 Numerical Linear Algebra 91
8.1 Introduction and objectives 91
8.2 An introduction to numerical linear algebra 91
8.3 Tridiagonal systems 94
8.4 Block tridiagonal systems 99
8.5 What requirements should our matrix satisfy? 101
8.6 Conclusions and summary 102
9 Modelling Functions in C++ 103
9.1 Introduction and objectives 103
9.2 Function pointers in C++ 103
9.3 Function objects in STL 106
9.4 Some function types 109
9.5 Creating your own function classes 111
9.6 Arrays of functions 114
9.7 Vector functions 115
9.8 Realvalued functions 115
9.9 Vectorvalued functions 116
9.10 Conclusions and summary 116
10 C++ Classes for Statistical Distributions 117
10.1 Introduction and objectives 117
10.2 Discrete and continuous probability distribution functions 117
10.3 Continuous distributions 119
10.4 Discrete distributions 124
10.5 Tests 127
10.6 Conclusions and summary 128
PART III ORDINARY AND STOCHASTIC DIFFERENTIAL EQUATIONS
11 Numerical Solution of Initial Value Problems: Fundamentals 131
11.1 Introduction and objectives 131
11.2 A model problem 132
11.3 Discretisation 133
11.4 Common schemes 134
11.5 Some theoretical issues 136
11.6 Fitting: Special schemes for difficult problems 137
11.7 Nonlinear scalar problems and predictor–corrector methods 138
11.8 Extrapolation techniques 139
11.9 C++ design and implementation 140
11.10 Generalisations 143
11.11 Conclusions and summary 144
12 Stochastic Processes and Stochastic Differential Equations 145
12.1 Introduction and objectives 145
12.2 Random variables and random processes 145
12.3 An introduction to stochastic differential equations 151
12.4 Some finite difference schemes 152
12.5 Which scheme to use? 153
12.6 Systems of SDEs 154
12.7 Conclusions and summary 154
13 TwoPoint Boundary Value Problems 155
13.1 Introduction and objectives 155
13.2 Description of problem 155
13.3 (Traditional) centreddifference schemes 157
13.4 Approximation of the boundary conditions 158
13.5 Exponentially fitted schemes and convection–diffusion 160
13.6 Approximating the derivatives 160
13.7 Design issues 161
13.8 Conclusions and summary 163
14 Matrix Iterative Methods 164
14.1 Introduction and objectives 164
14.2 Iterative methods 165
14.3 The Jacobi method 165
14.4 Gauss–Seidel method 166
14.5 Successive overrelaxation (SOR) 166
14.6 Other methods 166
14.7 The linear complementarity problem 168
14.8 Implementation 169
14.9 Conclusions and summary 171
PART IV PROGRAMMING THE BLACK–SCHOLES ENVIRONMENT
15 An Overview of Computational Finance 175
15.1 Introduction and objectives 175
15.2 The development life cycle 175
15.3 Partial differential equations 176
15.4 Numerical approximation of PDEs 177
15.5 The class of finite difference schemes 179
15.6 Special schemes for special problems 179
15.7 Implementation issues and the choice of programming language 180
15.8 Origins and application areas 180
15.9 Conclusions and summary 181
16 Finite Difference Schemes for Black–Scholes 182
16.1 Introduction and objectives 182
16.2 Model problem: The onedimensional heat equation 182
16.3 The Black–Scholes equation 186
16.4 Initial conditions and exotic options payoffs 187
16.5 Implementation 190
16.6 Method of lines: A whirlwind introduction 190
16.7 Conclusions and summary 191
17 Implicit Finite Difference Schemes for Black–Scholes 192
17.1 Introduction and objectives 192
17.2 Fully implicit method 193
17.3 An introduction to the Crank–Nicolson method 194
17.4 A critique of Crank–Nicolson 195
17.5 Is there hope? the Keller scheme 199
17.6 Conclusions and summary 202
18 Special Schemes for Plain and Exotic Options 203
18.1 Introduction and objectives 203
18.2 Motivating exponentially fitted schemes 203
18.3 Exponentially fitted schemes for parabolic problems 205
18.4 What happens when the volatility goes to zero? 208
18.5 Exponential fitting with explicit time 209
18.6 Exponential fitting and exotic options 210
18.7 Some final remarks 211
19 My First Finite Difference Solver 212
19.1 Introduction and objectives 212
19.2 Modelling partial differential equations in C++ 214
19.3 Finite difference schemes as C++ classes, Part I 218
19.4 Finite difference schemes as C++ classes, Part II 219
19.5 Initialisation issues 220
19.6 Interfacing with Excel 224
19.7 Conclusions and summary 224
20 An Introduction to ADI and Splitting Schemes 225
20.1 Introduction and objectives 225
20.2 A model problem 226
20.3 Motivation and history 227
20.4 Basic ADI scheme for the heat equation 228
20.4.1 Threedimensional heat equation 229
20.5 Basic splitting scheme for the heat equation 230
20.6 Approximating crossderivatives 231
20.7 Handling boundary conditions 232
20.8 Algorithms and design issues 234
20.9 Conclusions and summary 236
21 Numerical Approximation of TwoFactor Derivative Models 237
21.1 Introduction and objectives 237
21.2 Twofactor models in financial engineering 237
21.3 Finite difference approximations 241
21.4 ADI schemes for Asian options 242
21.5 Splitting schemes 243
21.6 Conclusions and summary 243
PART V DESIGN PATTERNS
22 A C++ Application for Displaying Numeric Data 247
22.1 Introduction and objectives 247
22.2 Input mechanisms 248
22.3 Conversion and processing mechanisms 249
22.4 Output and display mechanisms 250
22.5 Putting it all together 252
22.6 Output 252
22.7 Other functionality 252
22.8 Using Excel and property sets 258
22.9 Extensions and the road to design patterns 259
22.10 Conclusions and summary 260
23 Object Creational Patterns 261
23.1 Introduction and objectives 261
23.2 The Singleton pattern 263
23.3 The Prototype pattern 270
23.4 Factory Method pattern (virtual constructor) 272
23.5 Abstract Factory pattern 275
23.6 Applications to financial engineering 279
23.7 Conclusions and summary 279
24 Object Structural Patterns 281
24.1 Introduction and objectives 281
24.2 Kinds of structural relationships between classes 281
24.3 Whole–Part pattern 286
24.4 The Composite pattern 288
24.5 The Fa¸cade pattern 289
24.6 The Bridge pattern 290
24.7 Conclusions and summary 295
25 Object Behavioural Patterns 296
25.1 Introduction and objectives 296
25.2 Kinds of behavioural patterns 297
25.3 Iterator pattern 298
25.4 The Visitor pattern 301
25.5 Notification patterns 305
25.6 Conclusions and summary 307
PART VI DESIGN AND DEPLOYMENT ISSUES
26 An Introduction to the Extensible Markup Language 311
26.1 Introduction and objectives 311
26.2 A short history of XML 312
26.3 The XML structure 312
26.4 Document Type Definition 315
26.5 Extensible Stylesheet Language Transformation (XSLT) 320
26.6 An application of XML: Financial products Markup Language 324
26.7 Conclusions and summary 327
27 Advanced XML and Programming Interface 328
27.1 Introduction and objectives 328
27.2 XML Schema 328
27.3 Accessing XML data: The Document Object Model 334
27.4 DOM and C++: The essentials 335
27.5 DOM, entities and property sets 338
27.6 XML structures for plain and barrier options 342
27.7 Conclusions and summary 345
28 Interfacing C++ and Excel 346
28.1 Introduction and objectives 346
28.2 Object model in Excel: An overview 346
28.3 Under the bonnet: Technical details of C++ interfacing to Excel 348
28.4 Implementing the core process 351
28.5 Extensions 354
28.6 Application areas 355
28.7 Conclusions and summary 355
29 Advanced Excel Interfacing 356
29.1 Introduction and objectives 356
29.2 Status report and new requirements 356
29.3 A gentle introduction to Excel addins 357
29.4 Automation addin in detail 359
29.5 Creating a COM addin 367
29.6 Future trends 373
29.7 Conclusions and summary 373
30 An Extended Application: Option Strategies and Portfolios 374
30.1 Introduction and objectives 374
30.2 Spreads 374
30.3 Combinations: Straddles and strangles 375
30.4 Designing and implementing spreads 376
30.5 Delta hedging 378
30.6 An example 379
30.7 Tips and guidelines 381
Appendices
A1 My C++ refresher 383
A2 Dates and other temporal types 394
References 397
Index 401
First Chapter
Financial Instrument Pricing Using C++
By Daniel J. Duffy
John Wiley & Sons
ISBN: 0470855096Chapter One
Executive Overview of this Book1.1 WHAT IS THIS BOOK?
The goal of this book is to model financial instruments, such as options, bonds and interestrate products by partial differential equations, finite differences and C++. It is intended for IT and quantitative finance professionals who know this material and wish to deepen their knowledge and for those readers who use techniques such as Monte Carlo, Fourier transform methods, stochastic differential equations and lattice methods (for example, the binomial method) for instrument pricing.
We integrate a number of wellknown areas to create a traceable and maintainable path from when a financial engineer proposes a new model to when he or she codes the resulting equations in C++. When viewed as a black box, the core process in this book is to produce C++ classes and code for financial engineering applications. Furthermore, we give lots of examples of code that developers can use without much hassle. The accompanying CD contains all the source code in this book. We provide guidelines, algorithms and reusable code to help the reader to achieve these ends. The main activities that realise the core process are:
Activity 1: Map the financial model to a partial differential equation (PDE)
Activity 2: Approximate the PDE by the finite difference method (FDM)
Activity 3: Implement the FDM usingC++ and design patterns.
In this book we shall concentrate on Activities 2 and 3. Since this is a book on the application of C++ to financial engineering we concentrate on mapping the numerical algorithms from Activity 2 to robust and flexible C++ code and classes. However, we shall provide sufficient motivation and background information to help the reader to understand the complete 'instrument life cycle'. This life cycle describes the processes, activities, decisions and alternatives that describe how to program models for financial instruments in C++.
The topics in this book relate to finance, partial differential equations, numerical schemes and C++ code, and for this reason we use the term Computational Finance to sum up these related activities (see Seydel, 2003, where the same phrase is used). The foundations for partial differential equations and finite difference schemes for financial engineering applications are discussed in Duffy (2004b).
1.2 WHAT'S SPECIAL ABOUT THIS BOOK?
This book is part of a larger, ongoing project. It is the outcome of one part of this project and concentrates on showing how to program finite difference schemes in C++. Our approach is novel in a number of respects.
1. We use modern objectoriented and generic design patterns in C++ to solve a range of partial, stochastic and ordinary differential equations in financial engineering. Traditionally, engineers have used packages such as Matlab, Maple, the C language or other specialised libraries. Each alternative solution has its own benefits of course, but using C++ means that your code is portable, flexible and futureproof (C++ will still be used 20 years from now). Using C++ means that you are not tied into one vendor or operating system.
2. We give a thorough introduction to finite difference methods, how to apply them to BlackScholes type equations and how to map them to C++ code. We avoid glib recipetype schemes that work well for toy problems but do not always scale to reallife problems. In particular, we show how to program the famous CrankNicolson scheme and discuss when it breaks down, especially in applications with small volatility, discontinuous payoff functions or nonlinearities. We propose new schemes that overcome these problems and produce uniformly good approximations to the delta of an option. The book discusses finite difference schemes for both onefactor and twofactor problems.
3. Successful software always needs to be adapted and extended, and to this end we design our classes and applications so that they can easily be modified. Our book is novel in the sense that we apply the powerful and proven design patterns (see Gamma et al., 1995; Buschmann et al., 1996) to help us to produce applications that can be extended or adapted to different organisational, hardware and software contexts.
4. Last, but not least, it is vital that our software artefacts are well documented. We document these artefacts at the design level and, in particular, we use the defacto Unified Modeling Language (UML) to visually display the structural, functional and behavioural relationships between the classes and objects in our applications.
In short, this book describes in a stepbystep manner how to create 'good' software for financial engineering applications; it also integrates established techniques from fluid mechanics, numerical analysis and software design to produce a coherent and seamless approach to the design and implementation of financial models in C++.
1.3 WHO IS THIS BOOK FOR?
This book is meant for IT and quantitative finance professionals (risk managers, product development and derivatives research groups) who work in financial institutions and software companies and are involved in designing and implementing pricing models in C++. This book deals with fundamental issues such as C++ design and implementation, design patterns, finite difference methods and advanced software environments. Thus, it is of value to financial engineers ('Quants'), software developers and financial modellers.
We feel that the book is useful for universities and other educational institutes that deliver financial courses. This is not a book on instrument theory as such, and we assume that the reader has knowledge of option theory as presented in books by Hull (2000) and Wilmott (1998), for example. We also assume that the reader has had some exposure to differential equations, differential and integral calculus and matrix algebra. Finally, the reader should have a working knowledge of C++.
As we have already mentioned in this chapter, the book is suited not only to those readers from a partial differential equation (PDE) background but also to those who use techniques such as Monte Carlo, Fourier transform methods, stochastic differential equations (SDEs) and the binomial method for instrument pricing. We do our best to show that finite differences compare well with, and even outperform, these former methods, especially for complex and nonlinear onefactor and twofactor BlackScholes models.
Finally, the real option theory is emerging and many of the techniques in this book can be used in decision support systems in the oil, gas and energy industries. Thus, the book is also of interest to engineers, scientists and financial engineers in these fields.
1.4 SOFTWARE REQUIREMENTS
We have written this book from a number of viewpoints that have to do with what we call software quality. In general, we adopt the ISO 9126 quality characteristics (see Kitchenham and Pfleeger, 1996) as our working model. ISO 9126 describes how good a software product is. It consists of six toplevel characteristics:
Functionality: The ability of the software to satisfy stated or implied customer needs.
Reliability: Does the software maintain its level of performance for a stated period of time?
Usability: Is the software easy to understand, learn or integrate with other applications?
Efficiency: Describes the response times in the application and the corresponding resources that are needed.
Maintainability: How easy is it to modify, adapt and test the application? How stable is the application under change?
Portability: The ease with which the application can be adapted to work in some new software or hardware environment.
Any one (or all) of the above requirements may be important for your new or existing software project. In general, the more requirements your applications must satisfy the more time it will take to satisfy them. In this book we classify applications into three broad categories, depending on the level of flexibility that they must have:
Low flexibility: These are either throwaway prototypes or simple programs in order to test a piece of code or check the validity of some new model
Medium flexibility: The code and classes in this category can be customised (by changing its source code if necessary) and used in your own applications
High flexibility: The code in this category can be used in your applications without any changes.
It is important to know at the outset how flexible our solutions must be; on the one hand, we do not want to 'overengineer' our application, but nor do we want to produce code that is difficult to maintain, understand or falls apart when we modify it. This book will provide you with guidelines to help you to produce good designs for financial engineering applications.
We layer the software in this book by examining it at four different levels:
Foundation classes and building blocks: Reusable components for vectors, lists, matrices and other containers. We make ample use of the Standard Template Library (STL).
Mechanisms: Tightly coupled groups of generic functions that are related to a specific piece of functionality. An example is a set of functions for Date manipulations (cash flows, interest rate curves).
Halfproducts: Readytouse libraries that you can use as part of your own applications. We can place these halfproducts in assemblies and DLLs.
Applications: Dedicated applications for the user (not the developer). These applications are usually executables.
There are many advantages associated with taking this layered approach to software development, as we shall see in this book.
1.5 THE STRUCTURE OF THIS BOOK
This book is partitioned into six major parts, each of which deals with a major topic and consists of a number of chapters. These chapters deal with techniques that help to achieve the goals of each part.
Part I This part is an introduction to C++ template classes. We define what templates are, how to create them and how to use them in financial engineering applications. We give an overview of the Standard Template Library (STL). This is a C++ library consisting of template classes for a wide range of data containers, algorithms and functionality for navigating in these containers. We develop a number of template classes based on STL that we can use in financial engineering applications.
Part II In this part we create classes and code that will be used when approximating partial differential equations by finite difference methods. First, we create template classes for arrays, vectors and matrices as well as the corresponding mathematical operations on them. Furthermore, we introduce several classes that solve linear systems of equations. These classes implement direct and iterative matrix solvers in numerical linear algebra. Second, we create a number of other foundation classes that we need in numerical differentiation and integration. Finally, some useful classes for statistics are introduced.
Part III This part represents the start of the book as far as the mathematical core is concerned. We motivate the finite difference method by applying it to a simple firstorder ordinary differential equation in Chapter 11. This chapter discusses the most important ideas and schemes that will serve us well in later chapters. Continuing from Chapter 11, we introduce stochastic differential equations and the finite difference schemes needed in order to approximate them. We also propose several schemes to approximate twopoint boundary value problems. Special attention is paid to the CrankNicolson scheme and why it fails to approximate the solution of the convectiondiffusion equation in certain circumstances. It is in this part of the book that we introduce the class of exponentially fitted schemes and explain how they resolve the spurious oscillation problems associated with CrankNicolson.
Part IV In this part we introduce the onefactor and twofactor BlackScholes equations and devise appropriate finite difference schemes for them. Before we reach this level of Nirvana, we begin with the onedimensional heat equation and discuss explicit and implicit finite difference schemes to approximate its solution. The schemes are extensions of the timeindependent schemes that we introduced in Part III. Slightly increasing the level of difficulty, we discuss the CrankNicolson and fully implicit schemes for the onedimensional convectiondiffusion equation (and its specialisation, the BlackScholes equation). We analyse the schemes in some detail, discussing why they work, when they do not work and how to produce fitted schemes that approximate the solution and the delta of the BlackScholes equation.
Proceeding to twofactor problems, we propose Alternating Direction Implicit (ADI) and splitting methods and compare their relative merits.
Part V In this part we give an introduction to design patterns. Design is about alternatives and we have many choices when designing a system as the choices are determined by the software requirements. We begin with an introduction to some general design principles. In particular, we focus on templates and inheritance and why they are competitors. We also introduce the important notion of delegation whose understanding is fundamental to design patterns.
The main objective in Part V is to show how the famous design patterns of GOF (see Gamma et al., 1995) are applied to financial engineering applications. We pay special attention to choosing appropriate examples and to a discussion of the advantages of design patterns in this particular context. Three chapters are devoted to the Creational, Structural and Behavioural patterns.
Part VI This part contains a number of chapters that are of particular interest to financial engineers and IT personnel who write financial engineering applications. First, we give an introduction to the Extensible Markup Language (XML), a W3C standard for interoperable data representation. We also describe how it is used in option pricing applications in this book. XML will become more important in the financial world in the coming years as evidenced by the work seen with FpML and FIX. We also discuss classes and code that allow C++ code to communicate with Excel. Finally, we introduce a number of design patterns that are very useful for the current work.
1.6
Continues...