Quantitative Finance
eBook - ePub

Quantitative Finance

An Object-Oriented Approach in C++

  1. 354 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Quantitative Finance

An Object-Oriented Approach in C++

About this book

Quantitative Finance: An Object-Oriented Approach in C++ provides readers with a foundation in the key methods and models of quantitative finance. Keeping the material as self-contained as possible, the author introduces computational finance with a focus on practical implementation in C++.

Through an approach based on C++ classes and templates, the text highlights the basic principles common to various methods and models while the algorithmic implementation guides readers to a more thorough, hands-on understanding. By moving beyond a purely theoretical treatment to the actual implementation of the models using C++, readers greatly enhance their career opportunities in the field.

The book also helps readers implement models in a trading or research environment. It presents recipes and extensible code building blocks for some of the most widespread methods in risk management and option pricing.

Web ResourceThe author's website provides fully functional C++ code, including additional C++ source files and examples. Although the code is used to illustrate concepts (not as a finished software product), it nevertheless compiles, runs, and deals with full, rather than toy, problems. The website also includes a suite of practical exercises for each chapter covering a range of difficulty levels and problem complexity.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Quantitative Finance by Erik Schlogl in PDF and/or ePUB format, as well as other popular books in Mathematics & Finance. We have over one million books available in our catalogue for you to explore.

Information

CHAPTER 1

A brief review of the C++ programming language

The purpose of this chapter is to introduce to the reader the elements of the C++ programming language used throughout the book. In order to keep the book as self–contained as possible, the key features of the language are briefly discussed. However, readers without prior exposure to C++ may wish to supplement this chapter by consulting one of the many excellent textbooks, such as Shtern (2000), or the book on C++ by its inventor, Bjarne Stroustrup.1 Readers already familiar with C++ can safely skip this chapter. All the C++ source code discussed in this and in subsequent chapters is available for download from the website for this book, at http://www.schlogl.com/QF.

1.1 Getting started

The canonical way to introduce a programming language is to present a “Hello World!” program, which can then be modified to try out various elements of the language. Consider Listing 1.1. The C++ statement, which displays the “Hello World!” message on the screen, is
 cout ≪ "Hello World!" ≪ endl; 
This is done by pushing "Hello World!" to the standard output stream cout , followed by an end–of–line marker endl . A C++ program statement ends with a semicolon.
The body of our C++ program is contained in the function
int main (int argc, char *argv[])
Essentially, this is the function called by the operating system when the program is invoked.
Comments can be included in the source code in two ways: Anything to the right of a double forward slash (//) is ignored by the compiler, and sections of the source file delimited by /* and */ (i.e. multiline comments) are also ignored.2
The functionality, which allows us to write "Hello World!" to the standard
 /** \file Hello.cpp \brief "Hello world!" program. */ #include <cstdlib< #include <iostream> using namespace std; int main(int argc, char .argv[]) { // Display "Hello World!" message cout ≪ "Hello World!" ≪ endl; return EXIT_SUCCESS; } 
Listing 1.1 A “Hello World!” program
output stream in statement (1.1), is defined and implemented in the C++ Standard Library. In order to use this functionality, we have to include the header file iostream. The preprocessor directive
 #include <iostream> 
effectively causes the compiler to read the contents of the file iostream as if it were inserted into the source code at this location. All the required definitions (of cout, endl and the stream operator ) are found in this header file.3 Any functionality used in a particular C++ source code file must either be defined within that file, or its definition included via a header file. Different parts of the Standard Library require the use of different header files, and producing a custom C++ library, as we will proceed to do in the course of this book, requires creating C++ header files declaring functionality, as well as C++ source code files implementing it.
Note that the program we have implemented here is what in Microsoft Windows parlance is called a console application, i.e. a program without a graphical user interface (GUI), which is invoked from a command window and writes its output to the same. We will use such console applications as the test bed for our C++ library. For user–friendly applications, Microsoft Excel will serve as the front–end for the library code. Interfacing between C++ and Excel is discussed in Appendix A.
C++ applications are typically written using an Integrated Development Environment (IDE). On the website, the reader will find the project files required to compile and link the C++ programs discussed in this book under the Eclipse CDT and Microsoft Visual Studio IDEs.
 /** \file Declare.cpp \brief Example program for variable declarations. Copyright 2005 by Erik Schloegl */ #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char .argv[]) { int i = −3; cout ≪ "An integer: " ≪ i ≪ endl; double x = 3.5; cout ≪ "A double precision floating point number: "; cout ≪ x ≪ endl; bool b = true; cout ≪ "A Boolean value: " ≪ b ≪ endl; char c = ’$’; cout ≪ "A character: " ≪ c ≪ endl; return EXIT_SUCCESS; } 
Listing 1.2 Examples of variable declarations

1.2 Procedural programming in C++

While C++ is a language, which fully supports object–oriented programming (and we will be making extensive use of this in subsequent chapters), algorithms still need to be implemented using iteration, conditional statements, and the like. These basic features of the language will be introduced in this section.

1.2.1 Built-in data types and the definition and declaration of variables

In C++, the definition of a variable associates a name with a type and allocates space in machine memory for the variable. In contrast, a declaration only associates a name with a type; the requisite space for the variable is assumed to have been allocated elsewhere. Each variable must be defined exactly once, while its declaration may (need to) be repeated. In most cases, declaration and definition are identical, and we will use the two roughly synonymously in the sequel.4
The following fundamental data types are available in C++:
  • Integers, declared as int.
  • Floating point numbers, declared as float or double (these days, most programs use double).
  • Booleans (variables which can hold the value true or false), declared as bool.
  • Characters, declared as char.
Integers can also be declared using the additional modifiers unsigned (for non-negative integer variables) and long or short. Variables declared as long or short may represent a greater or smaller range of possible integers, but the actual implementation is machine and/or compiler dependent (See Stroustrup (1997)).
Within a given scope, all variables used in a C++ program must be declared exactly once. Variables can be declared anywhere within a program, but the scope of a declaration is limited by the brackets { }. The outermost scope is given by a source file itself “ variables declared outside any { } block are called global. Variables are assigned values using the operator =. Listing 1.2 gives examples of variable declarations.
C++ also allows the declaration of pointers and references to variables. A pointer is the machine memory address holding a variable of a particular type, and a reference refers to contents of a particular block of machine memory. The statement
 doublefe x; 
declares x to be a reference to a variable of type double. Thus the following statements
 double y = 0.0; double& x = y; x = 1.0; cout ≪ y ≪ endl; 
will print 1, whereas omitting the ampersand & in the declaration of x would result in 0 being displayed. The statement
 double* p; 
declares p to be a pointer variable to hold the address of a double. The ampersand operator can be used to determine the pointer to an existing variable. Conversely, a pointer is de-referenced (i.e. the contents of the memory to which it points is accessed) using the operator *. Thus
 double y = 0.0; double* x = &y; *x = 1.0; cout ≪ y ≪ endl; 
will again print 1.
Arrays of any data type can be created using square brackets. Thus
int integer_array[5];
creates an array of five integers. Array elements can be accessed using the operator [ ], e.g.
int integer_array[5];
Note that the base index of such arrays is zero, i.e. the first element of the above array is accessed by integer_array[0]. The variable integer_array itself is a pointer to the first element in the array; thus
 int integer_array[5]; int* p = integer_array; p[0] = 105; cout ≪ integer_array[0] ≪ endl; 
will display 105. Note also that the size of the array must be a constant known at compile time. Arrays, the size of which is only known at run time, must be dynamically allocated using the new operator (see Section 1.2.5). However, the object–oriented features of C++ allow such dynamic memory allocation to be encapsulated in utility classes,5 e.g. for vectors and matrices, obviating the need to worry about these issues when writing higher level programs. Constants can be declared using the qualifier const , e.g.
 const int one = 1; 
They must be initialised (assigned a value) at the time of declaration and cannot be modified. Additionally, sets of constant integer values can be defined as so–called enumerations using enum , e.g.
 enum currency {USD, EUR, JPY, AUD}; 
defines a new type currency, which may take the values USD , EUR , JPY or AUD (defined by default to be represented as integers 0, 1, 2, 36). We can then declare a variable domestic to be of type currency and initi...

Table of contents

  1. Cover
  2. Half Title
  3. Title Page
  4. Copyright Page
  5. Dedication Page
  6. Contents
  7. Preface
  8. Acknowledgements
  9. 1 A brief review of the C++ programming language
  10. 2 Basic building blocks
  11. 3 Lattice models for option pricing
  12. 4 The Black/Scholes world
  13. 5 Finite difference methods for partial differential equations
  14. 6 Implied volatility and volatility smiles
  15. 7 Monte Carlo simulation
  16. 8 The Heath/Jarrow/Morton model
  17. A Interfacing between C++ and Microsoft Excel
  18. B Automatic generation of documentation using Doxygen
  19. References
  20. Index