-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
265 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.