Skip to content

Commit

Permalink
new features. structure revised
Browse files Browse the repository at this point in the history
  • Loading branch information
ridoluc committed May 20, 2023
1 parent 26541f5 commit 06cda0a
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 48 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
cmake_minimum_required(VERSION 3.0.0)
project(OptionPricing VERSION 0.1.0)

set(CMAKE_CXX_COMPILER "clang++")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -c")

include(CTest)
enable_testing()

add_executable(OptionPricing main.cpp)
add_library(CallPut CallPut.cpp)

add_executable(OptionPricing main.cpp )
target_link_libraries(OptionPricing PRIVATE CallPut)



set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
Expand Down
8 changes: 5 additions & 3 deletions CallPut.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include "CallPut.h"


CallOption::CallOption(double _expiry, double _strike): VanillaOption(_expiry, _strike){};
CallOption::CallOption(double _expiry, double _strike): VanillaOption(_expiry, _strike) { };

double CallOption::payoff(double spot) const
double CallOption::payoff(double spot) const
{
double pf = spot - strike;
return pf > 0. ? pf : 0.;
}

PutOption::PutOption(double _expiry, double _strike): VanillaOption(_expiry, _strike){};


PutOption::PutOption(double _expiry, double _strike): VanillaOption(_expiry, _strike) { };

double PutOption::payoff(double spot) const
{
Expand Down
7 changes: 5 additions & 2 deletions CallPut.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#ifndef CALLPUT_H
#define CALLPUT_H

#include "PayOff.h"
#include "VanillaOption.h"

class CallOption: public PayOff, public VanillaOption
{
public:
CallOption(double _expiry, double _strike);
~CallOption();
virtual ~CallOption() {};

double payoff(double spot) const;

Expand All @@ -18,11 +19,13 @@ class PutOption: public PayOff, public VanillaOption
{
public:
PutOption(double _expiry, double _strike);
~PutOption();
virtual ~PutOption() {};

double payoff(double spot) const;

private:
};



#endif //CALLPUT_H
38 changes: 38 additions & 0 deletions MCEstimator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef MCESTIMATOR_H
#define MCESTIMATOR_H

#include"PayOff.h"

double MCEstimator(
const PayOff & claim,
const double T,
const double r,
const double sigma,
const double S_0,
const unsigned int sample )
{

RandVar r_var;


const double drift = (r - 0.5 * sigma * sigma) * T;
const double std_dev = sigma * sqrt(T);

double P_avg = 0;
double S_T, n;


for(int i = 0; i< sample; i++)
{
n = r_var.getNormal();
S_T = S_0 * exp( drift + std_dev * n );
P_avg += claim.payoff(S_T);
}

P_avg = exp(- r * T) * P_avg / sample;

return P_avg;
}


#endif // MCESTIMATOR_H
5 changes: 3 additions & 2 deletions PayOff.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class PayOff
{
public:
PayOff(){};
virtual double payoff(double spot_price) const = 0;
virtual ~PayOff(){};
virtual double payoff(double ) const = 0;
virtual ~PayOff() {};
private:
};


#endif // PAYOFF_H
77 changes: 77 additions & 0 deletions RandomVariable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#ifndef RANDOMVARIABLE_H
#define RANDOMVARIABLE_H

#include<cstdlib>
#include<vector>

class RandVar
{
public:
RandVar();
RandVar(unsigned int seed);
~RandVar() {};

double getUniform() ;
double getNormal() ;

std::vector<double> getUniform(const unsigned int N) ;
std::vector<double> getNormal(const unsigned int N) ;


};

RandVar::RandVar() {}

RandVar::RandVar(unsigned int seed)
{
std::srand(seed);
}

double RandVar::getUniform()
{
return (double) std::rand()/RAND_MAX;
}

double RandVar::getNormal()
{
/* To be improved.
- Wasting one variable
- check for machine 0
*/
double u1 = getUniform();
double u2 = getUniform();

double r = std::sqrt( -2 * std::log(u1) );
double n1 = r * cos(2* M_PI * u2);

return n1;
}


std::vector<double> RandVar::getUniform(const unsigned int N)
{
std::vector<double> res;

for(int i = 0; i<N; i++)
{
res.push_back(getUniform());
}

return res;
}

std::vector<double> RandVar::getNormal(const unsigned int N)
{
std::vector<double> res;

for(int i = 0; i<N; i++)
{
res.push_back(getNormal());
}

return res;
}



#endif //RANDOMVARIABLE_H
8 changes: 8 additions & 0 deletions StochasticProcess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef STOCHASTICPROCESS_H
#define STOCHASTICPROCESS_H

// TO DO

#endif //STOCHASTICPROCESS_H


27 changes: 27 additions & 0 deletions Stock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef STOCK_H
#define STOCK_H

#include "PayOff.h"

class Stock: public PayOff
{
public:
Stock(double _spot, double _return, double _vol)
: spot_price(_spot), returns(_return), volatility(_vol) {};
// Stock(const Stock & _stock);

double virtual payoff(double s) const {return s;}

double getReturn() {return returns;}
double getVol() {return volatility;}
double getSpot() {return spot_price;}

private:
double spot_price;
double returns;
double volatility;
};



#endif //STOCK_H
52 changes: 52 additions & 0 deletions StockPath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef STOCKPATH_H
#define STOCKPATH_H

#include <vector>
#include "Stock.h"
#include "RandomVariable.h"

class StockPath : public Stock
{
public:
StockPath(const Stock & _stock, double _period, unsigned long _steps);

std::vector<double> generatePath();

private:
unsigned long steps;
double period_length;
double dt;
};

StockPath::StockPath(const Stock & _stock, double _period, unsigned long _steps)
:Stock(_stock)
{
steps = _steps;
period_length = _period;
dt = period_length / steps;
}

std::vector<double> StockPath::generatePath()
{
RandVar rng;

std::vector<double> res;

double vol = getVol() * sqrt(dt);
double square_vol = getVol() * getVol() * dt;
double drift = getReturn() * dt - 0.5 * square_vol;

double last = getSpot();
res.push_back(last);

for(int i = 0; i< steps; i++)
{
last = last * exp(drift + vol * rng.getNormal());
res.push_back(last);
}

return res;
}


#endif //STOCKPATH_H
11 changes: 4 additions & 7 deletions VanillaOption.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
#ifndef VANILLAOPTION_H
#define VANILLAOPTION_H

#include "PayOff.h"


class VanillaOption
{
public:
VanillaOption(double, double);
VanillaOption(double _expiry, double _strike): strike(_strike), expiry(_expiry){};
~VanillaOption() {}

inline const double getExpiry() {return expiry;}
inline const double getStrike() {return strike;}
inline double getExpiry() const {return expiry;}
inline double getStrike() const {return strike;}

protected:
double expiry;
double strike;
};

VanillaOption::VanillaOption(double _expiry, double _strike) : strike(_strike), expiry(_expiry)
{}



Expand Down
Loading

0 comments on commit 06cda0a

Please sign in to comment.