Repository navigation
Expand file tree
/
Copy pathHoLee.cpp
More file actions
59 lines (47 loc) · 1.83 KB
/
Copy pathHoLee.cpp
File metadata and controls
59 lines (47 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cmath>
#include <vector>
#include <random>
#include <fstream>
// Function to simulate the Ho and Lee model
void simulateHoAndLee(const double& theta, const double& sigma, const double& T, const double& dt, const std::string& outputPath) {
// Set up random number generation
std::random_device rd;
std::mt19937 generator(rd());
std::normal_distribution<double> normalDistribution(0.0, 1.0);
// Calculate the number of time steps
int numSteps = static_cast<int>(T / dt);
// Initialize vectors to store time and interest rate values
std::vector<double> time(numSteps + 1);
std::vector<double> interestRate(numSteps + 1);
// Initialize the initial interest rate
interestRate[0] = 0.0;
// Simulate the Ho and Lee model
for (int i = 1; i <= numSteps; ++i) {
// Update time
time[i] = i * dt;
// Generate a random increment
double dW = normalDistribution(generator);
// Update the interest rate using the Ho and Lee SDE
interestRate[i] = interestRate[i - 1] + theta * time[i] * dt + sigma * std::sqrt(dt) * dW;
}
// Output the results to a CSV file
std::ofstream outputFile(outputPath);
outputFile << "Time,InterestRate\n";
for (int i = 0; i <= numSteps; ++i) {
outputFile << time[i] << "," << interestRate[i] << "\n";
}
outputFile.close();
}
int main() {
// Parameters for the Ho and Lee model
double theta = 0.02; // Replace with the desired deterministic function of time
double sigma = 0.01;
double T = 1.0;
double dt = 0.01;
std::string outputPath = "ho_and_lee_simulation.csv";
// Simulate the Ho and Lee model
simulateHoAndLee(theta, sigma, T, dt, outputPath);
std::cout << "Simulation completed. Results saved to " << outputPath << std::endl;
return 0;
}