Tuesday, February 2, 2016

Getting Started With VS2013 on UoM computers

On windows one of the best IDEs (Integrated Development Environment) to use on Windows is Visual Studio 2013. Visual Studio is primarily written for developers to build complex applications using graphics, audio and interactions with the user. However, during this course we are only interested in coding mathematics, so will not really touch on the many things that Visual Studio can do for you. You will need to know how to edit, compile and run simple Win32 console applications. The following ‘How To’ is for the Windows network at the University of Manchester. First click on the start menu and type “visual”, you should see Visual Studio 2013 appear to select.

You will first see a welcome screen, select the “Not now, maybe later” option.

At the next screen just select the default (general) environment and press the start button. Once the program has loaded (this may take some time), you can should now go to the menu at the top and select to open a new project. Given that each and every time you want to create a new program you will need to go through this same process, you should bookmark this page for future reference.

From the available templates on the left hand bar, select “Visual C++” and then “Empty Project” from the centre window. Change the name of the project to something that makes sense (Assignment 1, Binomial Tree, etc.) and then click ok.

You should now have a blank project. In order to write code we now need to create a new cpp file. For the most part of this course you should work on a one file one project basis, there can be only one main source file so new problems will require a new project! Go to the right hand bar and right click on the “Source Files” directory, and from the resulting menu select “Add- >New Item”:

From this screen we can now add our cpp file, rename it to whatever you like and click add.

Now we need to put some code in that file, so copy/paste the following into the file:

// include the input output stream library
// so that you can write to the screen
#include <iostream>
// use the std namespace
using namespace std;
// all programs must have just ONE main function
int main()
 // output a message to the screen
 cout << " Hello world!!!!!\n";
 // finish the function by returning an integer
 return 0;

and then save the file.

You are now ready to compile and run your first c++ program. Go to the “DEBUG” menu at the top and select “Start without debugging”, or use the shortcut “Cntrl F5”. Click on yes to build your solution and you should see it compile and then a black window will momentarily appear and disappear again.

Obviously this is no good as you cannot see any output from the program. In order to fix this we need to go to the “PROJECT” menu and select the properties option for your project, my project is called “Project3” so I select “Project3 Properties...”.

Once the properties window is open select “Configuration Properties- >Linker- >System” in the left hand sidebar.

Now you need to change the “SubSystem” setting to get the program to stay on screen. Either by entering manually or by selecting from the drop down menu (press the small triangle on the right hand side) set this to


You are finally ready to go! Press “Cntrl F5” and yes to build the project and the black console will now stay on screen. You should see this:

NOTE: you will need to reset this option in the property pages every time you open a new project. After a few goes it will become like second nature

Wednesday, March 18, 2015

C++ Coding - Black Scholes Option Pricing - Explicit Finite Difference

The example question for these solutions can be found on my website (click here).

6.1 Explicit Finite Difference For Option Pricing

In this example we are going to price a European call option with explicit finite difference.

Friday, March 13, 2015

C++ Coding - Black Scholes Option Pricing - Binomial Trees

The example question for these solutions can be found on my website (click here).

5.1 Binomial Tree For Option Pricing

The two most popular models for using binomial trees to price options are

We wish to generate a stock price tree, so denote the value of the underlying asset after timestep i and upstate j by Sij and we have that:

S  =  S ujdi−j
 ij    0

Sunday, February 22, 2015

C++ Coding - Black Scholes Option Pricing - Monte Carlo

3.2 Monte Carlo: Black Scholes European Call Option

Now we are going to value an European call option using Monte-Carlo. The setup is very simple, we just need to sum up the payoffs from a bunch of sample paths and then take the average. First start with an empty program except for the random number generator, as follows

Monday, January 12, 2015

Visual Studio 2012 at the University of Manchester

Getting started on Visual Studio 2012
A previous post of mine dealt with setting up an empty project on Visual Studio 2012, which you can find if you click here. If you follow the instructions there you will find that the terminal disappears even when you run the program with "Start Without Debugging". This is because some of the default properties are not set up correctly on opening an empty project.

Saturday, December 20, 2014

C++ Coding - Time to first exit (C++11 VS2012)

4.1 Time To First Exit

Calculate the expected hitting time E[t] for a Brownian motion X(t), where the process must hit either X(t) = 0 or X(t) = 1 for t < T. If neither boundary is hit within the time T then t = T.

Assume that the process X follows the SDE

dX  =  μdt + σdW
where μ = 0.01 is the drift and σ = 0.75 is the standard deviation of the Brownian motion. We have that the initial start point of the Brownian motion is X(t = 0) = 0.56, and we set T = 1.

Friday, December 19, 2014

C++ Coding - Random Numbers (Update c++11 VS2012)

The example question for these solutions can be found on my website (click here).

2.1 Random Numbers

To use the new random number generator we need to include the random library, the cmath library for any calculations and also iostream to show results onscreen. We first create a new project with an empty program with the correct libraries, and then declare a variable of type mt19937. This declares a new random number generator, which generates pseudo random sequence of integers defined by the Mersenne Twister algorithm. A computer can only generate a random sequence of integers, but of course we can then take that sequence of integers and convert it to any required distribution. Some of the conversions are simple but others are more complex, luckily we now have inbuilt c++ conversion to all standard distributions (more on this later). This means you will always have to create a generator to pass as an argument to the probability distribution you want to generate.