Repository navigation
Expand file tree
/
Copy pathHW.cpp
More file actions
75 lines (61 loc) · 2.67 KB
/
Copy pathHW.cpp
File metadata and controls
75 lines (61 loc) · 2.67 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cmath>
#include <vector>
#include <random>
#include <fstream>
// Function to simulate the Hull and White model
void simulateHullAndWhite(const double& r0, const std::vector<double>& theta,
const std::vector<double>& alpha, const std::vector<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] = r0;
// Simulate the Hull and White model
for (int i = 1; i <= numSteps; ++i) {
// Update time
time[i] = i * dt;
// Generate random increments
double dW = normalDistribution(generator);
// Calculate the integral terms for the explicit solution
double integral1 = 0.0;
double integral2 = 0.0;
double integral3 = 0.0;
for (int j = 0; j < i; ++j) {
double u = j * dt;
integral1 += alpha[j] * dt;
integral2 += theta[j] * std::exp(-integral1);
integral3 += sigma[j] * std::exp(-integral1) * dW;
}
// Update the interest rate using the Hull and White SDE
interestRate[i] = r0 * std::exp(-integral1) + integral2 + integral3;
}
// 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 Hull and White model
double r0 = 0.02; // Replace with the desired initial interest rate
std::vector<double> theta = {0.03, 0.02, 0.025}; // Replace with the desired deterministic function of time
std::vector<double> alpha = {0.01, 0.015, 0.012}; // Replace with the desired deterministic function of time
std::vector<double> sigma = {0.01, 0.015, 0.02}; // Replace with the desired deterministic function of time
double T = 1.0;
double dt = 0.01;
std::string outputPath = "hull_and_white_simulation.csv";
// Simulate the Hull and White model
simulateHullAndWhite(r0, theta, alpha, sigma, T, dt, outputPath);
std::cout << "Simulation completed. Results saved to " << outputPath << std::endl;
return 0;
}