[QuantLib] Sobol Sequence

의외로 QuantLib에 기반한 샘플 코드를 구하기 쉽지 않다.

아래는 QuantLib Sobol 난수를 사용한 Monte Carlo Option Pricing의 예로,
QuantLib
으로 Sobol Sequence를 생성하는 기능을 활용하기 위함.

컴파일 환경: Visual Studio 2010 Express + Boost 1.47 +
QuantLib 1.1

 

    1 #include<cmath>

    2 #include<ql/quantlib.hpp>

    3 

    4 using namespace QuantLib;

    5 

    6 int main() {

    7 

    8     const double PI = 3.141592653589793238462;

    9 

   10     int nSim = 100000;

   11     double underlying = 36;

   12     double strike = 40;

   13     double dividendYield = 0.00;

   14     double riskFreeRate = 0.06;

   15     double volatility = 0.20;

   16     double maturity = 1;

   17     unsigned type = 1; // call: 0, put: 1

   18 

   19     double dt = maturity;

   20     double s = 0;

   21 

   22     double payoff;

   23     double price = 0;

   24 

   25     SobolRsg sobolGen(2);

   26     std::vector<Real> sampleSobol(sobolGen.dimension());

   27     //sobolGen.skipTo(4096);

   28 

   29     for(int i = 0; i < nSim; i++) {

   30 

   31         sampleSobol = sobolGen.nextSequence().value;

   32 

   33         double d0 = sampleSobol[0];

   34         double d1 = sampleSobol[1];

   35         // Box-Muller

   36         double d2 = sqrt(-2 * log(d0)) * cos(2 * PI * d1);

   37 

   38         //std::cout << d2 << “, “;

   39 

   40         s = underlying * exp((riskFreeRate – dividendYield – 0.5 * volatility * volatility) * dt

   41            + volatility * d2 * sqrt(dt));

   42 

   43         if(type==0) {

   44            if(s > strike) {

   45                payoff = s – strike;

   46            } else {

   47                payoff = 0;      

   48            }

   49         } else {

   50            if(s < strike) {

   51                payoff = strike – s;

   52            } else {

   53                payoff = 0;

   54            }

   55         }

   56 

   57         price += payoff;

   58     }

   59 

   60     price /= nSim;

   61     price *= exp(-riskFreeRate * maturity);

   62 

   63     std::cout << price << std::endl;

   64 

   65 }

 

Leave a comment