Skip to content

Commit 0aa61fe

Browse files
committed
Refactor SDE simulation parameters for clarity and correctness
- Replaced hardcoded drift and diffusion calculations with defined parameters for better readability. - Updated the Euler-Maruyama implementation to use the new parameters, enhancing maintainability. - Added array indexing access to the state concept for improved usability in simulations.
1 parent b5f628f commit 0aa61fe

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

examples/parallelism_usage_demo.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,22 +203,15 @@ void demonstrate_monte_carlo_simulation() {
203203
std::mt19937 rng(i); // Seed with simulation index
204204
std::normal_distribution<double> normal(0.0, 1.0);
205205

206-
// Create SDE system
207-
auto drift = [](double t, const std::vector<double>& x, std::vector<double>& fx) {
208-
double mu = 0.05;
209-
fx[0] = mu * x[0];
210-
};
211-
212-
auto diffusion = [](double t, const std::vector<double>& x, std::vector<double>& gx) {
213-
double sigma = 0.2;
214-
gx[0] = sigma * x[0];
215-
};
206+
// Define SDE parameters
207+
const double mu = 0.05; // drift coefficient
208+
const double sigma = 0.2; // volatility coefficient
216209

217210
// Simple Euler-Maruyama implementation
218211
std::vector<double> state = {initial_price};
219212
for (double t = 0.0; t < t_final; t += dt) {
220213
double dW = normal(rng) * std::sqrt(dt);
221-
state[0] += 0.05 * state[0] * dt + 0.2 * state[0] * dW;
214+
state[0] += mu * state[0] * dt + sigma * state[0] * dW;
222215
}
223216

224217
final_prices[i] = state[0];

examples/state_concept_usage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ int main() {
6464
auto begin() const { return data.begin(); }
6565
auto end() const { return data.end(); }
6666

67+
// Add array indexing access required by system_state concept
68+
double& operator[](std::size_t index) { return data[index]; }
69+
const double& operator[](std::size_t index) const { return data[index]; }
70+
6771
const std::string& get_name() const { return name; }
6872
};
6973

0 commit comments

Comments
 (0)