C++ for Financial Mathematics
eBook - ePub

C++ for Financial Mathematics

John Armstrong

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

C++ for Financial Mathematics

John Armstrong

Book details
Book preview
Table of contents
Citations

About This Book

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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is C++ for Financial Mathematics an online PDF/ePUB?
Yes, you can access C++ for Financial Mathematics by John Armstrong in PDF and/or ePUB format, as well as other popular books in Business & Finance. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781498750073
Edition
1
Subtopic
Finance

Chapter 1

Getting Started

We start by learning how to write and run a simple example program to compute compound interest. First we will need to install and configure the software required to write C++ programs. Next we will see how to write a simple program.

1.1 Installing your development environment

You will need to install some sort of development environment on your computer in order to write C++ programs. A development environment is rather like a word processor, except it allows you to write software rather than documents. Just as with word processors, there are quite a few different development environments you can choose from. We will give some recommendations that have been tested to work with this couse.

1.1.1 For Windows

You will need to download Microsoft Visual Studio Express for Windows Desktop1. This software is freely available.

1.1.2 For Unix

If you are using Unix you will need a text editor and the programs g++ and make. If they are not already installed, follow the instructions for your Unix distribution, which you should be able to readily find online. Another program you should install is gdb, which we will discuss in Chapter 15.

1.1.3 For MacOS X

Install Apple’s XCode2 development environment.

1.2 Running an example program

Let us start with an example program.
Don’t worry what it does for now. The first thing we need to do is work out how to run it.
# include <iostream >
# include <cmath >
using namespace std;

int main () {
// Interesting code starts
int principal ;
double interestRate ;
int numberOfYears ;

cout << "How much are you investing ?\n";
cin >> principal ;
cout << " What ’s the annual interest rate (%)?\ n";
cin >> interestRate ;
cout << "How long for ( years )?\ n";
cin >> numberOfYears ;

double finalBalance =
pow (1.0 + interestRate * 0.01 , numberOfYears )
* principal ;
double interest = finalBalance - principal ;

cout << "You will earn ";
cout << interest ;
cout << "\n";
/* Interesting code ends */
return 0;
}
You can also find this code on the website of the book3. Follow the link for the project “InterestCalculator” for Chapter 1.

1.3 Compiling and running the code

Running C++ code isn’t as easy as you might like.
The reason for this is that C++ is what is called a “compiled language”.
The chip inside a computer that does most of the work is called the CPU (central processing unit). It does not know the C++ language or indeed any computer language which is pleasant to program in. The CPU speaks a language called assembly language, also known as machine code. In machine code, all instructions are coded up as a sequence of numbers. Each number is a code word for some action the CPU should take. Programming in machine code directly is completely unbearable. What is worse, different CPUs may use different versions of assembly language, so you have to rewrite your code for different computers.
To get around this, one programs in “higher level languages” which are written in ways that humans can understand. At some point, the program’s instructions need to be converted to machine code.
In an “interpreted” language, the instructions are converted to machine code every time they are executed. MATLAB and Python are examples of interpreted languages.
In a “compiled” language, the instructions are converted to machine code before the program is ever run. This process is called compilation. C++ is a compiled language, so you must compile your code before you can run it.
Historically, the advantage of compiled languages was that they run faster. The reason for this is that converting things to machine code takes time. If you do this every time the code is run, it will necessarily run slower. The big disadvantage was that you have to recompile your code if you change the type of computer you want to run it on.
These days, computers are so fast that this advantage is not really relevant any more. Modern languages can be compiled very fast and even use “just in time compilers” that observe how the software is being used by the user and perform optimisations based on this. This is one of the reasons why the claim C++ is faster than languages such as Java and C# is a bit of a myth.
A good development allows you to compile and then run your code at the touch of a button. But before we can do this, we need to get our code into our development environment.
We will now describe the steps you need to go through to compile the example code. Jump to the relevant section for your computer and follow the instructions to the letter. Note that a guide to compiling on Macs can be found on the website accompanying this book.
You will need to get everything exactly right. If you cannot get the code to work, there is a zip file called InterestCalculator on the website for this book. This contains working versions of the code that you can use.

1.3.1 Compiling on Windows

  • Open Visual Studio.
  • Select File→New→Project . . .
  • Select Empty→Project
  • Enter the Name InterestCalculator and press OK.
  • Note the name of the folder where your project is being saved.
  • Notice that to the right of the screen you have an area marked Solution Explorer inside which there is a picture of a folder marked Source Files. Right click on this and select Add→New item. . . .
  • Select the option “C++ file” and enter the name main.cpp and press Add.
  • This creates a file called main.cpp which we will use to store our code. On the right-hand side of the screen you will see a text editor window where you can edit the code for main.cpp.
  • Copy and paste the example code from the website4into the editor window.
  • Select Project→Interest Calculator Properties. . ., then select Linker→System and set the SubSystem to Console (/SUBSYSTEM:CONSOLE) using the drop down.
  • Press OK
  • Press CTRL + F5 to compile and run your program
This should have worked if you have managed to follow every instruction exactly. If it fails, close Visual Studio, delete all the files in the directory you noted down and try again! But this time be more careful.
Setting up your first-ever project is probably the most fiddly and tedious task you will have to perform in this book.
Why are there so many steps to creating your project?
Firstly a typical C++ project contains a lot of different files, so in practice you don’t normally run through such a complex process very often. Most of the steps above are only needed when you create a new project. The two steps that you would be likely to perform repeatedly are:
  1. creating new C++ source files by right clicking on the Source Files folder;
  2. pressing CTRL and F5 to compile and run your program.
The “project” groups together all of your files and allows you to set in one place the configuration options for all your files. This is why it makes sense to have a “project” as well as just the C++ files.
Secondly, you can write different types of programs on Windows. Most programs have Windows user interfaces, but very old fashioned programs have text input through the “console”. Console programs are easier to write, but not the default on Windows. So we have to tell Windows that is the kind of program we want. This is why we must set the SubSystem.
Let us examine all the files that have been created.
If you open Windows Explorer (by pressing the windows key and E) you should be able to browse to where your project has been saved and you will see that a lot of different files have been created.
Most of these are used internally by Visual Studio and so are of no interest. However, the following are interesting.
  • InterestCalculator/main.cpp. This contains the code we wrote.
  • Debug/InterestCalculator.exe. This contains the machine code created by the compilation process. You could give someone else...

Table of contents

  1. Cover
  2. Half Title
  3. Title Page
  4. Copyright Page
  5. Contents
  6. Introduction
  7. 1 Getting Started
  8. 2 Basic Data Types and Operators
  9. 3 Functions
  10. 4 Flow of Control
  11. 5 Working with Multiple Files
  12. 6 Unit Testing
  13. 7 Using C++ Classes
  14. 8 User-Defined Types
  15. 9 Monte Carlo Pricing in C++
  16. 10 Interfaces
  17. 11 Arrays, Strings, and Pointers
  18. 12 More Sophisticated Classes
  19. 13 The Portfolio Class
  20. 14 Delta Hedging
  21. 15 Debugging and Development Tools
  22. 16 A Matrix Class
  23. 17 An Overview of Templates
  24. 18 The Standard Template Library
  25. 19 Function Objects and Lambda Functions
  26. 20 Threads
  27. 21 Next Steps
  28. A Risk-Neutral Pricing
  29. Bibliography
  30. Index
Citation styles for C++ for Financial Mathematics

APA 6 Citation

Armstrong, J. (2017). C++ for Financial Mathematics (1st ed.). CRC Press. Retrieved from https://www.perlego.com/book/2051554/c-for-financial-mathematics-pdf (Original work published 2017)

Chicago Citation

Armstrong, John. (2017) 2017. C++ for Financial Mathematics. 1st ed. CRC Press. https://www.perlego.com/book/2051554/c-for-financial-mathematics-pdf.

Harvard Citation

Armstrong, J. (2017) C++ for Financial Mathematics. 1st edn. CRC Press. Available at: https://www.perlego.com/book/2051554/c-for-financial-mathematics-pdf (Accessed: 15 October 2022).

MLA 7 Citation

Armstrong, John. C++ for Financial Mathematics. 1st ed. CRC Press, 2017. Web. 15 Oct. 2022.