Skip to content

Commit b22c6d9

Browse files
committed
refactor: renamed everything, finished pipeline, and converted each step to return vectors of results
1 parent b801467 commit b22c6d9

23 files changed

Lines changed: 289 additions & 192 deletions

include/lfmc/Estimator.hpp

Lines changed: 0 additions & 17 deletions
This file was deleted.

include/lfmc/PathGenerator.hpp

Lines changed: 0 additions & 41 deletions
This file was deleted.

include/lfmc/Payoff.hpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

include/lfmc/Pipeline.hpp

Lines changed: 0 additions & 6 deletions
This file was deleted.

include/lfmc/estimator.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include "lfmc/types.hpp"
4+
5+
#include <expected>
6+
#include <string>
7+
8+
namespace lfmc {
9+
10+
class Estimator {
11+
public:
12+
virtual std::expected<void, std::string> add_payoffs(const Payoffs& payoffs) = 0;
13+
virtual bool converged() const = 0;
14+
virtual std::expected<double, std::string> result() const = 0;
15+
// virtual void merge(Estimator const& other) = 0;
16+
virtual ~Estimator() = default;
17+
};
18+
19+
class MonteCarloEstimator : public Estimator {
20+
private:
21+
double sum = 0.0;
22+
std::size_t count = 0;
23+
24+
public:
25+
std::expected<void, std::string> add_payoffs(const Payoffs& payoffs) override;
26+
bool converged() const override;
27+
std::expected<double, std::string> result() const override;
28+
// void merge(Estimator const& other) override;
29+
};
30+
31+
} // namespace lfmc

include/lfmc/path_generator.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include "lfmc/numerical_scheme.hpp"
4+
#include "lfmc/stochastic_process.hpp"
5+
#include "lfmc/types.hpp"
6+
7+
namespace lfmc {
8+
9+
template <StochasticProcess Process, typename Scheme>
10+
requires NumericalScheme<Scheme, Process>
11+
class PathGenerator {
12+
private:
13+
Process process_;
14+
Scheme scheme_;
15+
16+
public:
17+
PathGenerator(Process process, Scheme scheme)
18+
: process_(std::move(process)), scheme_(std::move(scheme)) {}
19+
20+
// TODO move to cpp file
21+
std::vector<Path> generate_paths(const std::vector<Normals>& normals, size_t steps,
22+
double T) const {
23+
const double dt = T / static_cast<double>(steps);
24+
25+
std::vector<Path> paths;
26+
for (const auto& norm : normals) {
27+
Path path(steps + 1);
28+
29+
double t = 0.0;
30+
double x = process_.initial();
31+
path.push_back(x);
32+
33+
for (size_t i = 0; i < steps; ++i) {
34+
x = scheme_.step(process_, t, x, dt, norm[i]);
35+
path.push_back(x);
36+
t += dt;
37+
}
38+
}
39+
40+
return paths;
41+
}
42+
};
43+
44+
} // namespace lfmc

include/lfmc/payoff.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include "lfmc/types.hpp"
4+
5+
namespace lfmc {
6+
7+
class Payoff {
8+
public:
9+
virtual Payoffs generate_payoffs(const std::vector<Path>& paths) const = 0;
10+
virtual ~Payoff() = default;
11+
};
12+
13+
class EuropeanCall : public Payoff {
14+
private:
15+
double strike_;
16+
17+
public:
18+
explicit EuropeanCall(double strike);
19+
20+
Payoffs generate_payoffs(const std::vector<Path>& paths) const override;
21+
};
22+
23+
class EuropeanPut : public Payoff {
24+
private:
25+
double strike_;
26+
27+
public:
28+
explicit EuropeanPut(double strike);
29+
30+
Payoffs generate_payoffs(const std::vector<Path>& paths) const override;
31+
};
32+
33+
} // namespace lfmc

include/lfmc/pipeline.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include "lfmc/estimator.hpp"
4+
#include "lfmc/path_generator.hpp"
5+
#include "lfmc/payoff.hpp"
6+
#include "lfmc/random_source.hpp"
7+
8+
#include <expected>
9+
#include <memory>
10+
11+
namespace lfmc {
12+
13+
template <StochasticProcess SP, NumericalScheme<SP> NS> class Pipeline {
14+
private:
15+
std::unique_ptr<RandomSource> random_source_;
16+
std::unique_ptr<PathGenerator<SP, NS>> path_generator_;
17+
std::unique_ptr<Payoff> payoff_;
18+
std::unique_ptr<Estimator> estimator_;
19+
20+
public:
21+
Pipeline(std::unique_ptr<RandomSource> random_source,
22+
std::unique_ptr<PathGenerator<SP, NS>> path_generator, std::unique_ptr<Payoff> payoff,
23+
std::unique_ptr<Estimator> estimator)
24+
: random_source_(std::move(random_source)), path_generator_(std::move(path_generator)),
25+
payoff_(std::move(payoff)), estimator_(std::move(estimator)) {}
26+
27+
std::expected<double, std::string> run(size_t steps, double T) {
28+
while (!estimator_->converged()) {
29+
auto normals = random_source_->generate_normals(steps);
30+
auto paths = path_generator_->generate_paths(normals, steps, T);
31+
auto payoffs = payoff_->generate_payoffs(paths);
32+
auto result = estimator_->add_payoffs(payoffs);
33+
34+
if (!result) {
35+
return std::unexpected(result.error());
36+
}
37+
}
38+
39+
return estimator_->result();
40+
}
41+
};
42+
43+
} // namespace lfmc

0 commit comments

Comments
 (0)