Cover....1
Half Title....2
Title Page....6
Copyright Page....7
Contents....8
Introduction....18
1 Getting Started....24
1.1 Installing your development environment....24
1.1.1 For Windows....24
1.1.2 For Unix....24
1.1.3 For MacOS X....24
1.2 Running an example program....25
1.3 Compiling and running the code....26
1.3.1 Compiling on Windows....27
1.3.2 Compiling on Unix....29
1.4 Understanding the example code....31
1.5 Configuring the compiler....35
1.6 Making decisions....36
1.7 Exercises....37
1.8 Summary....38
2 Basic Data Types and Operators....40
2.1 Memory terminology....40
2.2 Basic data types....41
2.2.1 Integers....41
2.2.2 Floating point numbers....43
2.2.3 Booleans....43
2.2.4 Characters....43
2.3 Casting....45
2.4 Memory addresses....49
2.5 Operators....51
2.5.1 The sizeof operator....51
2.5.2 Mathematical operations....51
2.5.3 Comparison operators....52
2.5.4 Logical operators....52
2.5.5 Bitwise operators....52
2.5.6 Combining operators....53
2.5.7 Assignment operators....53
2.5.8 If statements revisited....55
2.6 Summary....58
3 Functions....60
3.1 The C++ function syntax....60
3.2 Recursion....64
3.3 Libraries....65
3.4 Declaring and defining functions....65
3.5 Functions that don’t return a value....67
3.6 Specifying default values....68
3.7 Overloading functions....69
3.8 Global and local variables....70
3.9 Namespaces....71
3.10 Summary....75
4 Flow of Control....78
4.1 while loops....78
4.2 do-while loops....80
4.3 for loops....81
4.4 break, continue, return....83
4.5 throw statements....84
4.6 switch statements....86
4.7 Scope....88
4.8 Flow of control in operators....88
4.8.1 Short circuit evaluation....89
4.8.2 The ternary operator....89
4.8.3 The comma operator....90
4.9 Summary....92
5 Working with Multiple Files....94
5.1 The project FMLib....94
5.2 Header files....95
5.3 Creating our project....96
5.3.1 Creating the first header file....96
5.3.2 Some code that uses the functions....98
5.3.3 Write the definitions....99
5.4 How header files work....100
5.4.1 The meaning of include....100
5.4.2 Pragma once....100
5.4.3 Information hiding....101
5.4.4 Inline....103
5.5 A complete example....104
5.6 Summary....105
6 Unit Testing....108
6.1 A testing framework for C++....109
6.2 Macros....109
6.3 The macros in testing.h....110
6.3.1 The ASSERT macro....110
6.3.2 The ASSERT_APPROX_EQUAL macro....110
6.3.3 The INFO macro....111
6.3.4 The DEBUG_PRINT macro....111
6.3.5 The TEST macro....112
6.4 Using testing.h....112
6.5 What have we gained?....114
6.6 Testing normcdf....115
6.7 Summary....117
7 Using C++ Classes....120
7.1 Vectors....120
7.2 Pass by reference and const....123
7.2.1 Pass by reference....124
7.2.2 The const keyword....125
7.2.3 Pass by reference without const....127
7.3 Using ofstream....127
7.4 Working with string....129
7.5 Building strings efficiently....130
7.6 Writing a pie chart....131
7.6.1 A web-based chart....132
7.6.2 Create a header file....134
7.6.3 Write a source file....135
7.6.4 Enable testing in your files....135
7.6.5 Write functions to generate the boiler plate....135
7.6.6 Write a simple version of the chart data....136
7.6.7 Write a test of what we’ve done so far....137
7.6.8 Write the interesting code....137
7.6.9 Testing the interesting code....138
7.6.10 Wrap it all up into a single function....139
7.7 The architecture of the World Wide Web....140
7.8 Summary....144
8 User-Defined Types....146
8.1 Terminology....146
8.2 Writing your own class....147
8.2.1 Writing the declaration....147
8.2.2 Using a class....149
8.2.3 Passing objects between functions....150
8.2.4 How have classes helped?....150
8.3 Adding functions to classes....151
8.3.1 Using const on member functions....153
8.4 A financial example....154
8.4.1 What have we gained?....156
8.5 Recommendations on writing classes....157
8.6 Encapsulation....158
8.6.1 Implementing PieChart....160
8.6.2 Using PieChart....160
8.7 Constructors....161
8.7.1 Writing a default constructor....162
8.7.2 An alternative, and superior syntax....163
8.8 Constructors with parameters....164
8.9 Summary....167
9 Monte Carlo Pricing in C++....168
9.1 A function to simulate stock prices....169
9.2 Writing a Monte Carlo pricer....174
9.3 Generating random numbers for Monte Carlo....177
9.4 Summary....181
10 Interfaces....182
10.1 An interface for pricing options....182
10.2 Describing an interface in C++....184
10.3 Examples of interfaces....187
10.4 Interfaces in object-oriented programming....189
10.5 What’s wrong with if statements?....191
10.6 An interface for integration....192
10.7 Summary....196
11 Arrays, Strings, and Pointers....198
11.1 Arrays, the C alternative to vector....199
11.2 Pointers....202
11.2.1 new and delete....202
11.2.2 Pointer operators....203
11.2.3 Looping with pointers....205
11.2.4 Using pointers in practice....208
11.3 Pointers to text....208
11.4 Pass by pointer....210
11.5 Don’t return pointers to local variables....212
11.6 Using pointers to share data....214
11.6.1 Sharing with shared_ptr....217
11.7 Sharing data with references....220
11.8 The C++ memory model....222
11.8.1 The stack....223
11.8.2 The heap....225
11.9 Summary....227
12 More Sophisticated Classes....228
12.1 Inlining member functions....228
12.2 The this keyword....229
12.3 Inheritance....230
12.3.1 What have we gained?....232
12.3.2 Terminology....232
12.4 Overriding methods — the virtual keyword....233
12.4.1 A note on the keyword virtual....234
12.5 Abstract functions =0....235
12.6 Multiple layers....235
12.6.1 UML....236
12.6.2 Another object hierarchy....238
12.6.3 Multiple inheritance....238
12.6.4 Calling superclass methods....239
12.7 Forward declarations and the structure of cpp files....240
12.8 The static keyword....241
12.9 The protected keyword....243
12.10 Summary....245
13 The Portfolio Class....246
13.1 The Priceable interface....246
13.2 The Portfolio interface and implementation....247
13.2.1 Implementation of PortfolioImpl....250
13.3 Testing....251
13.4 UML....253
13.5 Limitations....254
13.6 Summary....255
14 Delta Hedging....256
14.1 Discrete-time delta hedging....256
14.2 Implementing the delta hedging strategy in C++....258
14.2.1 Class declaration....258
14.2.2 Implementation of runSimulation....260
14.2.3 Implementing the other methods of HedgingSimulator....261
14.2.4 Changes to CallOption....263
14.3 Testing the simulation....264
14.4 Interpreting and extending our simulation....264
14.5 Summary....267
15 Debugging and Development Tools....268
15.1 Debugging strategies....268
15.1.1 Unit tests....268
15.1.2 Reading your code....269
15.1.3 Logging statements....269
15.1.4 Using a debugger....270
15.1.5 Divide and conquer....270
15.2 Debugging with Visual Studio....271
15.2.1 Obtaining a stack trace in Visual Studio....271
15.2.2 Breakpoints and single stepping in Visual Studio....273
15.3 Debugging with GDB....275
15.3.1 Using GDB to obtain a stack trace....276
15.3.2 Breakpoints and single stepping with GDB....279
15.3.3 Other commands and features....280
15.4 Other development tools and practices....281
15.4.1 Version control....281
15.4.2 Bug tracking....282
15.4.3 Testing framework....282
15.4.4 Automated build....283
15.4.5 Continuous integration....284
15.4.6 Logging....284
15.4.7 Static analysis....284
15.4.8 Memory-leak detection....285
15.4.9 Profiling tools....285
15.4.10 Example....286
15.5 Summary....287
16 A Matrix Class....290
16.1 Basic functionality of Matrix....290
16.2 The constructor and destructor of Matrix....292
16.2.1 Virtual destructors....294
16.2.2 When is a destructor needed?....295
16.2.3 Additional constructors....296
16.3 Const pointers....297
16.4 Operator overloading....298
16.4.1 Overloading +....298
16.4.2 Overloading other arithmetic operators....300
16.4.3 Overloading comparison operators....301
16.4.4 Overloading the << operator....302
16.4.4.1 Remarks on return by reference....303
16.4.5 Overloading the () operator....303
16.4.6 Overloading +=....304
16.5 The rule of three....305
16.5.1 Overriding the assignment operator....305
16.5.2 Writing a copy constructor....306
16.5.3 The easy way to abide by the rule of three....307
16.5.4 Move operators....308
16.6 Completing the Matrix class....308
16.7 Array Programming....309
16.7.1 Implementing an efficient matrix class....309
16.7.2 Array programming....310
16.7.3 Array programming in the option classes....311
16.7.4 Array programming for the BlackScholesModel....312
16.7.5 Array programming the Monte Carlo pricer....313
16.7.6 Performance....313
16.8 Summary....315
17 An Overview of Templates....318
17.1 Template functions....318
17.2 Template classes....320
17.3 Templates as an alternative to interfaces....322
17.4 Summary....325
18 The Standard Template Library....326
18.1 typedef....327
18.2 auto....329
18.3 Using iterators with vectors....330
18.4 for loops and containers....332
18.5 The container set....333
18.6 The container vector....334
18.7 The container list....335
18.8 The container initializer_list....338
18.9 The containers map and unordered_map....338
18.9.1 How a map works....340
18.9.2 How an unordered_map works....341
18.10 Storing complex types in containers....343
18.11 A mathematical model for multiple stocks....343
18.12 Using the Standard Template Library in FMLib....345
18.13 Summary....350
19 Function Objects and Lambda Functions....352
19.1 Function objects....352
19.2 Lambda functions....353
19.3 Function pointers....356
19.4 Sorting with lambda functions....357
19.5 Summary....359
20 Threads....360
20.1 Concurrent programming in C++....361
20.1.1 Creating threads....361
20.1.2 Mutual exclusion....362
20.1.3 Global variables and race conditions....365
20.1.4 Problems with locking....366
20.2 The command design pattern....369
20.3 Monte Carlo pricing....370
20.3.1 Random number generation with multiple threads....371
20.3.2 A multi-threaded pricer....372
20.3.3 Implementing Task....373
20.3.4 Using the Executor....374
20.3.5 Remarks upon the design....374
20.4 Coordinating threads....375
20.4.1 The Pipeline pattern....375
20.4.2 How Pipeline is implemented....378
20.5 Summary....381
21 Next Steps....382
21.1 Programming....382
21.1.1 Libraries....382
21.1.2 Software development....382
21.1.3 C++ language features....383
21.1.4 Other languages....383
21.2 Financial mathematics....384
A Risk-Neutral Pricing....386
A.1 The players in financial markets....386
A.2 Derivatives contracts....389
A.3 Risk-neutral pricing....393
A.4 Modelling stock prices....395
A.5 Monte Carlo pricing....400
A.6 Hedging....402
A.7 Summary....405
Bibliography....406
Index....408
If you know a little bit about financial mathematics but don’t yet know a lot about programming, then C++ for Financial Mathematics is for you.
C++ is an essential skill for many jobs in quantitative finance, but learning it can be a daunting prospect. This book gathers together everything you need to know to price derivatives in C++ without unnecessary complexities or technicalities. It leads the reader step-by-step from programming novice to writing a sophisticated and flexible financial mathematics library. At every step, each new idea is motivated and illustrated with concrete financial examples.
As employers understand, there is more to programming than knowing a computer language. As well as covering the core language features of C++, this book teaches the skills needed to write truly high quality software. These include topics such as unit tests, debugging, design patterns and data structures.
The book teaches everything you need to know to solve realistic financial problems in C++. It can be used for self-study or as a textbook for an advanced undergraduate or master’s level course.