Advertisement
Nam_Hoang_Waw

Up Out European Put Option - Pricing

Mar 2nd, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include"Random.h"
  4. #include"UpOutPut.h"
  5.  
  6. UpOutPutOption::UpOutPutOption(
  7.     int nInt_,
  8.     double strike_,
  9.     double spot_,
  10.     double vol_,
  11.     double r_,
  12.     double expiry_,
  13.     double barrier_){
  14.         nInt = nInt_;
  15.         strike = strike_;
  16.         spot = spot_;
  17.         vol = vol_;
  18.         r = r_;
  19.         expiry = expiry_;
  20.         barrier = barrier_;
  21.         generatePath();
  22.     }
  23.     // initialize the inputs of the option.
  24.  
  25. void UpOutPutOption::generatePath(){
  26.     double thisDrift = (r * expiry - 0.5 * vol * vol * expiry) / double(nInt);
  27.     double cumShocks = 0;
  28.     thisPath.clear();
  29.  
  30.     for(int i = 0; i < nInt; i++){
  31.         cumShocks += (thisDrift + vol * sqrt(expiry / double(nInt)) * GetOneGaussianByBoxMuller());
  32.         thisPath.push_back(spot * exp(cumShocks));
  33.     }
  34. }
  35. // Generating price paths
  36.  
  37.  
  38. double UpOutPutOption::MaxPrice(std::vector<double> thisVec)    {
  39.  
  40.     double MaxValue = thisVec[0];
  41.     int VecSize = int(thisVec.size()/2);
  42.     for ( int i = 0; i < VecSize; i++ ){
  43.         MaxValue = (thisVec[i] > MaxValue) ? thisVec[i] : MaxValue;
  44.     }
  45.     return MaxValue;
  46. }
  47.  
  48. // Finding max value within the first half of the period
  49.  
  50. double UpOutPutOption::UpOutPut(int nReps){
  51.     double runningSum=0;
  52.     double Payoff=0;
  53.  
  54.     for(int i=0;i<nReps;i++){
  55.  
  56.         generatePath();
  57.         // generate price path
  58.  
  59.         if(UpOutPutOption::MaxPrice(thisPath)<barrier){
  60.             Payoff = strike - thisPath.back();
  61.             runningSum += Payoff > 0 ? Payoff : 0;
  62.  
  63.         // any path that violates the barrier condition will return payoff = 0 or else the payoff is different from zero
  64.         }
  65.     }
  66.     return ((runningSum/nReps)*double(exp(-r*expiry)));
  67.     // return the average value of running sum, discounted back to current value
  68. };
  69.  
  70. // Calculate Up Out Put Option
  71.  
  72. double mean (std::vector<double> thisVec)   {
  73.  
  74.     double runningSum = 0.0;
  75.     int thisSize = thisVec.size();
  76.  
  77.     for(int i = 0; i < thisSize; i++){
  78.         runningSum += thisVec[i];
  79.     }
  80.  
  81.     return runningSum/double(thisSize);
  82. }
  83.  
  84. // calculating the mean value of the option price vector
  85.  
  86. double stdDev (std::vector<double> thisVec) {
  87.  
  88.     double runningSum = 0.0;
  89.     int thisSize = thisVec.size();
  90.  
  91.     for ( int i = 0; i < thisSize; i++ ){
  92.         runningSum += pow((thisVec[i]- mean(thisVec) ), 2);
  93.     }
  94.  
  95.     return sqrt(runningSum/(thisSize - 1));
  96. }
  97.  
  98. // calculating the std.dev of the price vector
  99.  
  100.  
  101. void OptionMC(UpOutPutOption myOption, int n1, int n2){
  102.  
  103.     std::vector<double> price_vector;
  104.  
  105.     for(int i=0; i<n2; i++){
  106.         price_vector.push_back(myOption.UpOutPut(n1));
  107.     }
  108.  
  109.     double mean_vec = mean(price_vector);
  110.     double stdDev_vec = stdDev(price_vector);
  111.  
  112.     std::cout << std::endl << "StdDev = " << stdDev_vec;
  113. }
  114.  
  115. // function that calculated mean and standard deviation of theoretical price
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement