Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #35

Merged
merged 5 commits into from
Aug 16, 2024
Merged

Dev #35

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/random/number_generator/base_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

namespace OptionPricer {

class NumberGenerarator {
class NumberGenerator {
public:
explicit NumberGenerarator(std::shared_ptr<Distribution> dist);
explicit NumberGenerarator(std::shared_ptr<Distribution> dist, const unsigned int& seed);
explicit NumberGenerator(std::shared_ptr<Distribution> dist);
explicit NumberGenerator(std::shared_ptr<Distribution> dist, const unsigned int& seed);

virtual ~NumberGenerarator();
virtual ~NumberGenerator();

virtual double generate(const int& step) = 0;

Expand Down
8 changes: 4 additions & 4 deletions include/random/number_generator/base_quasi_random_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

namespace OptionPricer {

class QRNGenerator : public NumberGenerarator {
class QuasiRandomNumberGenerator : public NumberGenerator {
public:
explicit QRNGenerator(std::shared_ptr<Distribution> dist, const unsigned int& dim);
QRNGenerator(std::shared_ptr<Distribution> dist, const unsigned int& seed, const unsigned int& dim);
explicit QuasiRandomNumberGenerator(std::shared_ptr<Distribution> dist, const unsigned int& dim);
QuasiRandomNumberGenerator(std::shared_ptr<Distribution> dist, const unsigned int& seed, const unsigned int& dim);

~QRNGenerator() override;
~QuasiRandomNumberGenerator() override;

unsigned int getDim() const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OptionPricer {

class FaureGenerator final: public QRNGenerator {
class FaureGenerator final: public QuasiRandomNumberGenerator {
public:
explicit FaureGenerator(std::shared_ptr<Distribution> dist, const unsigned int& dim);
FaureGenerator(std::shared_ptr<Distribution> dist, const unsigned int& seed, const unsigned int& dim);
Expand Down
6 changes: 3 additions & 3 deletions include/random/number_generator/random_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace OptionPricer {

class RNGenerator final: public NumberGenerarator {
class RandomNumberGenerator final: public NumberGenerator {
public:
explicit RNGenerator(std::shared_ptr<Distribution> dist);
RNGenerator(std::shared_ptr<Distribution> dist, const unsigned int& seed);
explicit RandomNumberGenerator(std::shared_ptr<Distribution> dist);
RandomNumberGenerator(std::shared_ptr<Distribution> dist, const unsigned int& seed);

[[nodiscard]] double generate(const int& step) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace OptionPricer {

class SobolGenerator final: public QRNGenerator {
class SobolGenerator final: public QuasiRandomNumberGenerator {
public:
explicit SobolGenerator(std::shared_ptr<Distribution> dist, const unsigned int& dim);
SobolGenerator(std::shared_ptr<Distribution> dist, const unsigned int& seed, const unsigned int& dim);
Expand Down
6 changes: 3 additions & 3 deletions include/solver/monte_carlo/base_mc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ namespace OptionPricer {
public:
MCPricer(std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator);
std::shared_ptr<NumberGenerator> generator);

virtual ~MCPricer();

[[nodiscard]] virtual double calculate_price(const unsigned long& N) const = 0;
[[nodiscard]] virtual double calculatePrice(const unsigned long& N) const = 0;

protected:
std::shared_ptr<IMarketData> marketData_;
std::shared_ptr<StockModel> stockModel_;
std::shared_ptr<NumberGenerarator> generator_;
std::shared_ptr<NumberGenerator> generator_;
};

}
Expand Down
2 changes: 1 addition & 1 deletion include/solver/monte_carlo/base_mc_path_dependent.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace OptionPricer {
public:
PathDependentMCPricer(std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

protected:
Expand Down
4 changes: 2 additions & 2 deletions include/solver/monte_carlo/builder/base_mc_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace OptionPricer {

MCBuilder& setMarketData(std::shared_ptr<IMarketData> marketData);
MCBuilder& setStockPriceModel(std::shared_ptr<StockModel> stockModel);
MCBuilder& setNumberGenerator(std::shared_ptr<NumberGenerarator> generator);
MCBuilder& setNumberGenerator(std::shared_ptr<NumberGenerator> generator);

virtual MCBuilder& setOption(std::shared_ptr<Option> option) = 0;
virtual std::unique_ptr<MCPricer> build() = 0;

protected:
std::shared_ptr<IMarketData> marketData_;
std::shared_ptr<StockModel> stockModel_; // Requires specific option to be provided default values
std::shared_ptr<NumberGenerarator> generator_;
std::shared_ptr<NumberGenerator> generator_;
};

}
Expand Down
7 changes: 4 additions & 3 deletions include/solver/monte_carlo/mc_american.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ namespace OptionPricer {
AmericanMCPricer(std::shared_ptr<AmericanOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
std::shared_ptr<BasisFunctionStrategy> basisFunctionStrategy,
std::shared_ptr<RegressionStrategy> regressionStrategy,
const unsigned int& steps);

double calculate_price(const unsigned long& N) const override;
double calculatePrice(const unsigned long& N) const override;
double standardPrice(const unsigned long& N, const double &dt, const double &discountFactor) const ;
double brownianBridgePrice(const unsigned long& N, const double &dt, const double &discountFactor) const ;

private:
std::shared_ptr<BasisFunctionStrategy> basisFunctionStrategy_;
Expand All @@ -28,5 +30,4 @@ namespace OptionPricer {

}


#endif //MC_AMERICAN_H
8 changes: 4 additions & 4 deletions include/solver/monte_carlo/mc_asian.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace OptionPricer {
AsianMCPricer(std::shared_ptr<AsianOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

[[nodiscard]] double calculate_price(const unsigned long& N) const override;
[[nodiscard]] double calculatePrice(const unsigned long& N) const override;

protected:
virtual double computeSumPrices(const double& S_t) const = 0;
Expand All @@ -29,7 +29,7 @@ namespace OptionPricer {
ArithmeticAsianMCPricer(std::shared_ptr<ArithmeticAsianOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

private:
Expand All @@ -44,7 +44,7 @@ namespace OptionPricer {
GeometricAsianMCPricer(std::shared_ptr<GeometricAsianOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

private:
Expand Down
8 changes: 4 additions & 4 deletions include/solver/monte_carlo/mc_barrier.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace OptionPricer {
BarrierMCPricer(std::shared_ptr<BarrierOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

[[nodiscard]] double calculate_price(const unsigned long& N) const override;
[[nodiscard]] double calculatePrice(const unsigned long& N) const override;

protected:
virtual bool checkHasCrossed(const double& S_t, bool& hasCrossed) const = 0;
Expand All @@ -28,7 +28,7 @@ namespace OptionPricer {
KnockInBarrierMCPricer(std::shared_ptr<KnockInBarrierOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

protected:
Expand All @@ -41,7 +41,7 @@ namespace OptionPricer {
KnockOutBarrierMCPricer(std::shared_ptr<KnockOutBarrierOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

protected:
Expand Down
8 changes: 4 additions & 4 deletions include/solver/monte_carlo/mc_lookback.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace OptionPricer {
LookbackMCPricer(std::shared_ptr<LookbackOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

[[nodiscard]] double calculate_price(const unsigned long& N) const override;
[[nodiscard]] double calculatePrice(const unsigned long& N) const override;

protected:
virtual double computePayoff(const double& S_t, const double& S_min, const double& S_max) const = 0;
Expand All @@ -26,7 +26,7 @@ namespace OptionPricer {
FloatingStrikeLookbackMCPricer(std::shared_ptr<FloatingStrikeLookbackOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

private:
Expand All @@ -38,7 +38,7 @@ namespace OptionPricer {
FixedStrikeLookbackMCPricer(std::shared_ptr<FixedStrikeLookbackOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator,
std::shared_ptr<NumberGenerator> generator,
const unsigned int& steps);

private:
Expand Down
4 changes: 2 additions & 2 deletions include/solver/monte_carlo/mc_single_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace OptionPricer {
SinglePathMCPricer(std::shared_ptr<SinglePathOption> option,
std::shared_ptr<IMarketData> marketData,
std::shared_ptr<StockModel> stockModel,
std::shared_ptr<NumberGenerarator> generator);
std::shared_ptr<NumberGenerator> generator);

~SinglePathMCPricer() override = default;

[[nodiscard]] double calculate_price(const unsigned long& N) const override;
[[nodiscard]] double calculatePrice(const unsigned long& N) const override;

private:
std::shared_ptr<SinglePathOption> option_;
Expand Down
70 changes: 53 additions & 17 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
#include "random/distribution/standard_normal_distribution.h"
#include "random/number_generator/sobol_quasi_random_generator.h"
#include <Eigen/Dense>
#include <gtest/internal/gtest-param-util.h>
#include <random/number_generator/random_generator.h>
#include <solver/monte_carlo/basis_function/chebyshev.h>
#include <solver/monte_carlo/basis_function/laguerre.h>
#include <solver/monte_carlo/regression/lasso.h>
#include <solver/monte_carlo/regression/ridge.h>
#include "solver/monte_carlo/basis_function/legendre.h"

Expand All @@ -30,49 +35,80 @@ int main() {
double T = 1.0;
double K = 100.0;
double S = 100.0;
double sigma = 0.2;
double r = 0.05;
double B = 120;
double dim = 52;
double sigma = 0.15;
double r = 0.03;
int dim = 150;

auto marketData = MarketData::getInstance();
marketData->addStock(ticker, S, sigma);
marketData->setR(r);

ParameterObject params;
params.setParameter("ticker", "AAPL");
params.setParameter("ticker", ticker);
params.setParameter("T", T);
params.setParameter("K", K);
params.setParameter("B", B);
params.setParameter("direction", BarrierDirection::Up);

auto normal = std::make_shared<StandardNormalDistribution>();
auto generator = std::make_shared<SobolGenerator>(normal, dim);
auto generator = std::make_shared<RandomNumberGenerator>(normal);
auto brownianMotion = std::make_shared<GeometricBrownianMotionModel>(ticker, marketData);
auto laguerre = std::make_shared<LaguerreBasisFunction>(4);
auto regression = std::make_shared<LeastSquaresRegression>();

AmericanOptionFactory factory;
auto call = factory.createCallOption(params);
auto put = factory.createPutOption(params);

auto pricer = std::make_unique<AmericanMCPricer>(put, marketData, brownianMotion, generator, laguerre, regression, dim);
AmericanMCBuilder builder;
auto americanPricer = builder.setOption(call).setSteps(25).build();
auto americanPricer = builder.setOption(put)
.setSteps(dim)
.setNumberGenerator(generator).build();

MCSolver mcSolver;
mcSolver.setN(100000);
mcSolver.setPricer(std::move(americanPricer));
mcSolver.setN(200000);
mcSolver.setPricer(std::move(pricer));

std::cout << "S = 100, dim = 50, P = ";
std::cout << mcSolver.solve() << "\n";

auto ridgeStrat = std::make_shared<RidgeRegression>(0);
auto americanPricer2 = builder.setRegressionStrategy(ridgeStrat).build();

marketData->updateStockPrice(ticker, 90);
auto americanPricer2 = builder.build();
mcSolver.setPricer(std::move(americanPricer2));

std::cout << "S = 90, dim = 50, P = ";
std::cout << mcSolver.solve() << "\n";

auto legendreStrat = std::make_shared<LegendreBasisFunction>(5);
auto americanPricer3 = builder.setBasisFunctionStrategy(legendreStrat).build();
marketData->updateStockPrice(ticker, 110);
auto americanPricer3 = builder.build();

mcSolver.setPricer(std::move(americanPricer3));
std::cout << "S = 110, dim = 50, P = ";
std::cout << mcSolver.solve() << "\n";

/*
dim = 100;
marketData->updateStockPrice("AAPL", 100);
auto generator2 = std::make_shared<SobolGenerator>(normal, dim);
auto americanPricer4 = builder.setSteps(dim).setNumberGenerator(generator2).build();

std::cout << "S = 100, dim = 100, P = ";
mcSolver.setPricer(std::move(americanPricer4));
std::cout << mcSolver.solve() << "\n";

marketData->updateStockPrice("AAPL", 90);
auto americanPricer5 = builder.build();

mcSolver.setPricer(std::move(americanPricer5));
std::cout << "S = 90, dim = 100, P = ";
std::cout << mcSolver.solve() << "\n";

marketData->updateStockPrice("AAPL", 110);
auto americanPricer6 = builder.build();

mcSolver.setPricer(std::move(americanPricer6));
std::cout << "S = 110, dim = 100, P = ";
std::cout << mcSolver.solve() << "\n";
*/

return 0;

/*
Expand Down
6 changes: 3 additions & 3 deletions src/random/number_generator/base_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace OptionPricer {

NumberGenerarator::NumberGenerarator(std::shared_ptr<Distribution> dist)
NumberGenerator::NumberGenerator(std::shared_ptr<Distribution> dist)
: dist_(std::move(dist)) {
std::random_device rd;
seed_ = rd();
}

NumberGenerarator::NumberGenerarator(std::shared_ptr<Distribution> dist, const unsigned int &seed)
NumberGenerator::NumberGenerator(std::shared_ptr<Distribution> dist, const unsigned int &seed)
: dist_(std::move(dist)), seed_(seed) {
if (seed_ < 0) {
throw std::invalid_argument("seed parameetr must be a positve integer");
}
}

NumberGenerarator::~NumberGenerarator() = default;
NumberGenerator::~NumberGenerator() = default;

}
Loading