Create core structure
This commit is contained in:
10
CMakeLists.txt
Normal file
10
CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(QuantEngine)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_FLAGS "-O3 -march=native")
|
||||
|
||||
find_package(Eigen3 REQUIRED)
|
||||
|
||||
add_subdirectory(src)
|
||||
|
||||
4
Notes.md
Normal file
4
Notes.md
Normal file
@@ -0,0 +1,4 @@
|
||||
### Coding Language
|
||||
First we need to decide on the language that we will use.
|
||||
As C++ is very efficient, the core engine will be written in C++, and
|
||||
exposed to the Research layer via `pybind11`.
|
||||
@@ -1,3 +1,5 @@
|
||||
# pricing
|
||||
|
||||
Monte Carlo pricing of European options under Black–Scholes
|
||||
|
||||
### Project structure
|
||||
|
||||
9
src/CMakeLists.txt
Normal file
9
src/CMakeLists.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
add_library(qengine
|
||||
black_scholes.cpp
|
||||
monte_carlo.cpp
|
||||
payoff.cpp
|
||||
main.cpp
|
||||
)
|
||||
|
||||
target_include_directories(qengine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(qengine Eigen3::Eigen)
|
||||
5
src/black_scholes.cpp
Normal file
5
src/black_scholes.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by David Doebel on 03.03.2026.
|
||||
//
|
||||
|
||||
#include "black_scholes.hpp"
|
||||
30
src/black_scholes.hpp
Normal file
30
src/black_scholes.hpp
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by David Doebel on 03.03.2026.
|
||||
//
|
||||
|
||||
#ifndef OPTION_PRICING_BLACK_SCHOLES_HPP
|
||||
#define OPTION_PRICING_BLACK_SCHOLES_HPP
|
||||
|
||||
#include <cmath>
|
||||
|
||||
class BlackScholes {
|
||||
public:
|
||||
BlackScholes(double S0, double r, double sigma, double T)
|
||||
: S0_(S0), r_(r), sigma_(sigma), T_(T) {}
|
||||
|
||||
double terminal_price(double Z) const {
|
||||
return S0_ * std::exp(
|
||||
(r_ - 0.5 * sigma_ * sigma_) * T_
|
||||
+ sigma_ * std::sqrt(T_) * Z
|
||||
);
|
||||
}
|
||||
|
||||
double discount() const {
|
||||
return std::exp(-r_ * T_);
|
||||
}
|
||||
|
||||
private:
|
||||
double S0_, r_, sigma_, T_;
|
||||
};
|
||||
|
||||
#endif //OPTION_PRICING_BLACK_SCHOLES_HPP
|
||||
22
src/main.cpp
Normal file
22
src/main.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by David Doebel on 03.03.2026.
|
||||
//
|
||||
|
||||
#include "black_scholes.hpp"
|
||||
#include "monte_carlo.hpp"
|
||||
#include "payoff.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
|
||||
BlackScholes model(100.0, 0.05, 0.2, 1.0);
|
||||
CallPayoff payoff(100.0);
|
||||
|
||||
MonteCarloEngine mc;
|
||||
|
||||
double price = mc.price(model, payoff, 1000000);
|
||||
|
||||
std::cout << "MC Price: " << price << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
5
src/monte_carlo.cpp
Normal file
5
src/monte_carlo.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by David Doebel on 03.03.2026.
|
||||
//
|
||||
|
||||
#include "monte_carlo.hpp"
|
||||
36
src/monte_carlo.hpp
Normal file
36
src/monte_carlo.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by David Doebel on 03.03.2026.
|
||||
//
|
||||
|
||||
#ifndef OPTION_PRICING_MONTE_CARLO_HPP
|
||||
#define OPTION_PRICING_MONTE_CARLO_HPP
|
||||
#pragma once
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
class MonteCarloEngine {
|
||||
public:
|
||||
MonteCarloEngine(unsigned long seed = 42)
|
||||
: gen_(seed), dist_(0.0, 1.0) {}
|
||||
|
||||
template<typename Model, typename Payoff>
|
||||
double price(const Model& model,
|
||||
const Payoff& payoff,
|
||||
std::size_t N) {
|
||||
|
||||
double sum = 0.0;
|
||||
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
double Z = dist_(gen_);
|
||||
double ST = model.terminal_price(Z);
|
||||
sum += payoff(ST);
|
||||
}
|
||||
|
||||
return model.discount() * sum / N;
|
||||
}
|
||||
|
||||
private:
|
||||
std::mt19937_64 gen_;
|
||||
std::normal_distribution<> dist_;
|
||||
};
|
||||
#endif //OPTION_PRICING_MONTE_CARLO_HPP
|
||||
11
src/payoff.cpp
Normal file
11
src/payoff.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// Created by David Doebel on 03.03.2026.
|
||||
//
|
||||
|
||||
#include "payoff.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
double CallPayoff::operator()(double ST) const {
|
||||
return std::max(ST - K_, 0.0);
|
||||
}
|
||||
21
src/payoff.hpp
Normal file
21
src/payoff.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by David Doebel on 03.03.2026.
|
||||
//
|
||||
|
||||
#ifndef OPTION_PRICING_PAYOFF_HPP
|
||||
#define OPTION_PRICING_PAYOFF_HPP
|
||||
class Payoff {
|
||||
public:
|
||||
virtual double operator()(double ST) const = 0;
|
||||
virtual ~Payoff() = default;
|
||||
};
|
||||
|
||||
class CallPayoff : public Payoff {
|
||||
public:
|
||||
CallPayoff(double K) : K_(K) {}
|
||||
|
||||
double operator()(double ST) const override;
|
||||
private:
|
||||
double K_;
|
||||
};
|
||||
#endif //OPTION_PRICING_PAYOFF_HPP
|
||||
Reference in New Issue
Block a user