diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cebdde8..f86785d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,8 @@ add_library(qengine main.cpp calibration/Stats.cpp calibration/Stats.hpp + models/Model.cpp + models/Model.hpp ) target_include_directories(qengine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/calibration/Stats.cpp b/src/calibration/Stats.cpp index 1c10315..6d1d0fd 100644 --- a/src/calibration/Stats.cpp +++ b/src/calibration/Stats.cpp @@ -6,18 +6,25 @@ #include void Stats::update(double x) { - // update the mean according to the formula - // \hat{a}_n \cdot \frac{n}{n+1} + \frac{a_{n+1}}{n+1} - double n_ratio = n_ / ++n_ ; - mean_ = n_ratio * mean_ + x/n_; - // update the second moment - M2 = n_ratio * M2 + x * x / n_; - // update the + running_sum_ += x; + running_square_sum_ += x * x; + n_++; } +double Stats::mean() const { + return running_sum_ / n_; +} + +double Stats::square_mean() const { + return running_square_sum_ / n_; +} + double Stats::variance() const { - return M2 - mean_ * mean_; + double mean = this->mean(); + double square_mean = this->square_mean(); + return square_mean * square_mean - mean * mean; + } double Stats::std_error() const { @@ -25,5 +32,5 @@ double Stats::std_error() const { } std::pair Stats::CI() const { - return std::make_pair(mean_ - 1.96 * std_error(), mean_ + 1.96 * std_error()); + return std::make_pair(running_sum_ - 1.96 * std_error(), running_sum_ + 1.96 * std_error()); } diff --git a/src/calibration/Stats.hpp b/src/calibration/Stats.hpp index 8e62d86..e5917dc 100644 --- a/src/calibration/Stats.hpp +++ b/src/calibration/Stats.hpp @@ -10,12 +10,14 @@ class Stats { private: size_t n_ = 0; - double mean_ = 0.0; - double M2 = 0.0; + double running_sum_ = 0.0; + double running_square_sum_ = 0.0; public: Stats() = delete; void update(double x); + double mean() const; + double square_mean() const; double variance() const; double std_error() const; std::pair CI() const; // alpha = 5% diff --git a/src/models/Model.cpp b/src/models/Model.cpp new file mode 100644 index 0000000..22bc73d --- /dev/null +++ b/src/models/Model.cpp @@ -0,0 +1,5 @@ +// +// Created by David Doebel on 05.03.2026. +// + +#include "Model.hpp" \ No newline at end of file diff --git a/src/models/Model.hpp b/src/models/Model.hpp new file mode 100644 index 0000000..119c4e1 --- /dev/null +++ b/src/models/Model.hpp @@ -0,0 +1,18 @@ +// +// Created by David Doebel on 05.03.2026. +// + +#ifndef QUANTENGINE_MODEL_HPP +#define QUANTENGINE_MODEL_HPP + + +class Model { +public: + Model() = default; + virtual ~Model() = 0; + [[nodiscard]] virtual double terminal_price(double Z) const = 0; + [[nodiscard]] virtual double discount() const = 0; +}; + + +#endif //QUANTENGINE_MODEL_HPP \ No newline at end of file diff --git a/src/models/black_scholes.hpp b/src/models/black_scholes.hpp index e207d04..0c86c2d 100644 --- a/src/models/black_scholes.hpp +++ b/src/models/black_scholes.hpp @@ -6,20 +6,22 @@ #define OPTION_PRICING_BLACK_SCHOLES_HPP #include +#include "Model.hpp" -class BlackScholes { +class BlackScholes : public Model{ public: BlackScholes(double S0, double r, double sigma, double T) - : S0_(S0), r_(r), sigma_(sigma), T_(T) {} + : Model(), S0_(S0), r_(r), sigma_(sigma), T_(T) { + } - double terminal_price(double Z) const { + [[nodiscard]] double terminal_price(double Z) const override{ return S0_ * std::exp( (r_ - 0.5 * sigma_ * sigma_) * T_ + sigma_ * std::sqrt(T_) * Z ); } - double discount() const { + [[nodiscard]] double discount() const override{ return std::exp(-r_ * T_); }