Vanilla Option Pricing via C++

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值