Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cmath>
- #include"Random.h"
- #include"UpOutPut.h"
- UpOutPutOption::UpOutPutOption(
- int nInt_,
- double strike_,
- double spot_,
- double vol_,
- double r_,
- double expiry_,
- double barrier_){
- nInt = nInt_;
- strike = strike_;
- spot = spot_;
- vol = vol_;
- r = r_;
- expiry = expiry_;
- barrier = barrier_;
- generatePath();
- }
- // initialize the inputs of the option.
- void UpOutPutOption::generatePath(){
- double thisDrift = (r * expiry - 0.5 * vol * vol * expiry) / double(nInt);
- double cumShocks = 0;
- thisPath.clear();
- for(int i = 0; i < nInt; i++){
- cumShocks += (thisDrift + vol * sqrt(expiry / double(nInt)) * GetOneGaussianByBoxMuller());
- thisPath.push_back(spot * exp(cumShocks));
- }
- }
- // Generating price paths
- double UpOutPutOption::MaxPrice(std::vector<double> thisVec) {
- double MaxValue = thisVec[0];
- int VecSize = int(thisVec.size()/2);
- for ( int i = 0; i < VecSize; i++ ){
- MaxValue = (thisVec[i] > MaxValue) ? thisVec[i] : MaxValue;
- }
- return MaxValue;
- }
- // Finding max value within the first half of the period
- double UpOutPutOption::UpOutPut(int nReps){
- double runningSum=0;
- double Payoff=0;
- for(int i=0;i<nReps;i++){
- generatePath();
- // generate price path
- if(UpOutPutOption::MaxPrice(thisPath)<barrier){
- Payoff = strike - thisPath.back();
- runningSum += Payoff > 0 ? Payoff : 0;
- // any path that violates the barrier condition will return payoff = 0 or else the payoff is different from zero
- }
- }
- return ((runningSum/nReps)*double(exp(-r*expiry)));
- // return the average value of running sum, discounted back to current value
- };
- // Calculate Up Out Put Option
- double mean (std::vector<double> thisVec) {
- double runningSum = 0.0;
- int thisSize = thisVec.size();
- for(int i = 0; i < thisSize; i++){
- runningSum += thisVec[i];
- }
- return runningSum/double(thisSize);
- }
- // calculating the mean value of the option price vector
- double stdDev (std::vector<double> thisVec) {
- double runningSum = 0.0;
- int thisSize = thisVec.size();
- for ( int i = 0; i < thisSize; i++ ){
- runningSum += pow((thisVec[i]- mean(thisVec) ), 2);
- }
- return sqrt(runningSum/(thisSize - 1));
- }
- // calculating the std.dev of the price vector
- void OptionMC(UpOutPutOption myOption, int n1, int n2){
- std::vector<double> price_vector;
- for(int i=0; i<n2; i++){
- price_vector.push_back(myOption.UpOutPut(n1));
- }
- double mean_vec = mean(price_vector);
- double stdDev_vec = stdDev(price_vector);
- std::cout << std::endl << "StdDev = " << stdDev_vec;
- }
- // function that calculated mean and standard deviation of theoretical price
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement