Vanilla Option Pricing via C++
2015-05-03 11:44
369 查看
Box-Muller Algorithm, generate Gaussian random numbers
#ifndef BM_GAUSSIAN_HPP_INCLUDED
#define BM_GAUSSIAN_HPP_INCLUDED
#include <cstdlib>
#include <cmath>
#include <vector>
double uniformRandom(){
return (double)(rand()+1.0)/(double)(RAND_MAX+1.0);
}
void normalRandom(std::vector<double>& v){
double u1=uniformRandom();
double u2=uniformRandom();
v[0] = cos(8.0*atan(1.0)*u2)*sqrt(-2.0*log(u1));
v[1] = sin(8.0*atan(1.0)*u1)*sqrt(-2.0*log(u2));
}
#endif // BM_GAUSSIAN_HPP_INCLUDED
main function
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <./BM_Gaussian.hpp>
using namespace std;
int main(){
// set option contract parameters;
double T = 1.0;
double K = 100;
double S0 = 100;
double sigma = 0.1;
double r = 0.01;
int npaths = 100000;
int nsteps = 100/2;
double S;
vector<double> rnorm(2);
double sumpayoff = 0.0;
double dt = double(T)/double(nsteps);
for (int i=0; i<npaths; i++){
S = S0;
for (int j=0; j<nsteps; j++){
normalRandom(rnorm);
S *= exp((r-0.5*sigma*sigma)*dt + sigma*sqrt(dt)*rnorm[0]);
S *= exp((r-0.5*sigma*sigma)*dt + sigma*sqrt(dt)*rnorm[1]);
}
sumpayoff += max(S-K, 0.0);
}
double callprice = double(sumpayoff)/double(npaths);
cout << "Call Price: " << callprice << endl;
return 0;
}
#ifndef BM_GAUSSIAN_HPP_INCLUDED
#define BM_GAUSSIAN_HPP_INCLUDED
#include <cstdlib>
#include <cmath>
#include <vector>
double uniformRandom(){
return (double)(rand()+1.0)/(double)(RAND_MAX+1.0);
}
void normalRandom(std::vector<double>& v){
double u1=uniformRandom();
double u2=uniformRandom();
v[0] = cos(8.0*atan(1.0)*u2)*sqrt(-2.0*log(u1));
v[1] = sin(8.0*atan(1.0)*u1)*sqrt(-2.0*log(u2));
}
#endif // BM_GAUSSIAN_HPP_INCLUDED
main function
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <./BM_Gaussian.hpp>
using namespace std;
int main(){
// set option contract parameters;
double T = 1.0;
double K = 100;
double S0 = 100;
double sigma = 0.1;
double r = 0.01;
int npaths = 100000;
int nsteps = 100/2;
double S;
vector<double> rnorm(2);
double sumpayoff = 0.0;
double dt = double(T)/double(nsteps);
for (int i=0; i<npaths; i++){
S = S0;
for (int j=0; j<nsteps; j++){
normalRandom(rnorm);
S *= exp((r-0.5*sigma*sigma)*dt + sigma*sqrt(dt)*rnorm[0]);
S *= exp((r-0.5*sigma*sigma)*dt + sigma*sqrt(dt)*rnorm[1]);
}
sumpayoff += max(S-K, 0.0);
}
double callprice = double(sumpayoff)/double(npaths);
cout << "Call Price: " << callprice << endl;
return 0;
}
相关文章推荐
- Vanilla Option Pricing via C++
- Basket Call Option via C++
- Basket Call Option via C++
- American Option Pricing via Longstaff and Schwartz (2001)
- American Option Pricing via Longstaff and Schwartz (2001)
- 《Windows via C/C++》学习笔记 —— 作业
- Windows Via C/C++ 读书笔记 2
- Windows Via C/C++ 读书笔记 5 用户模式的线程同步
- Windows Via C/C++ 读书笔记 7 异步IO, 完成端口模式
- When Not to Create a Thread-come from WINDOWS VIA C C++
- Linux下C++编译提示错误:unrecognized command line option “-std=c++11”
- Windows via C/C++ notes
- Windows Via C/C++ Part Ⅰ Chapter 2: 字符和字符串(4)
- windowsViaC/C++设备IO之异步设备IO请求
- 《Windows Via C/C++》边学习,边翻译(一)错误处理(Error Handling)
- Windows via C/C++:终止进程
- Windows Via C/C++:vista下的应用程序权限控制
- Windows Via C/C++:线程入口点函数
- 《Windows via C/C++》学习笔记 —— 线程基础
- Draw the RGB data from kinect C++ via opengl