-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
185 lines (150 loc) · 7.32 KB
/
Copy pathmain.cpp
File metadata and controls
185 lines (150 loc) · 7.32 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// main.cpp
// Groups A & B - Exact Pricing Methods - Complete Assignment Solution
//
// This program systematically addresses every requirement from the assignment,
// answers all questions directly, and demonstrates all functionality.
//
// Created by: Andre Boiko, 2025-09-12
//
#include "EuropeanOption.h"
#include "PerpAmericanOption.h"
#include "Global.h"
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void printSection(const string& title) {
cout << "\n" << string(70, '=') << endl;
cout << title << endl;
cout << string(70, '=') << endl;
}
void printResult(const string& description, double result, double expected = 0.0, bool hasExpected = false) {
cout << description << ": " << fixed << setprecision(6) << result;
if (hasExpected) {
cout << " (Expected: " << expected << ")";
double tolerance = 1e-4;
bool match = abs(result - expected) < tolerance;
cout << " [" << (match ? "PASS" : "FAIL") << "] (tol=" << tolerance << ")";
}
cout << endl;
}
int main()
{
//cout << "PROBLEM 1: European Put (Exact Method)" << endl;
//// Problem 1 parameters: S=108, K=120, r=4.5%, T=1.45, sig=0.51, b=0
//EuropeanOption problem1(1.45, 120.0, 0.51, 0.045, 0.0, 108.0);
//// Constructor: (T, K, sig, r, b, S)
//cout << "Parameters: S=108, K=120, r=4.5%, T=1.45, sig=0.51, b=0" << endl;
//cout << "Put Price: " << fixed << setprecision(6) << problem1.PutPrice() << endl;
//cout << "Put Delta: " << problem1.PutDelta() << endl;
//cout << "Put Gamma: " << problem1.Gamma() << endl;
//cout << "COMPUTATIONAL FINANCE - GROUPS A & B" << endl;
//cout << string(70, '=') << endl;
cout << "European Option Pricing\n";
EuropeanOption b1(0.25, 65.0, 0.30, 0.08, 0.08, 60.0);
printResult("Batch 1 Call", b1.CallPrice(), 2.13337, true);
printResult("Batch 1 Put", b1.PutPrice(), 5.84628, true);
EuropeanOption b2(1.0, 100.0, 0.20, 0.0, 0.0, 100.0);
printResult("Batch 2 Call", b2.CallPrice(), 7.96557, true);
printResult("Batch 2 Put", b2.PutPrice(), 7.96557, true);
EuropeanOption b3(1.0, 10.0, 0.50, 0.12, 0.12, 5.0);
printResult("Batch 3 Call", b3.CallPrice(), 0.204058, true);
printResult("Batch 3 Put", b3.PutPrice(), 4.07326, true);
EuropeanOption b4(30.0, 100.0, 0.30, 0.08, 0.08, 100.0);
printResult("Batch 4 Call", b4.CallPrice(), 92.17570, true);
printResult("Batch 4 Put", b4.PutPrice(), 1.24750, true);
cout << "\nPut-Call Parity Check\n";
double call1 = b1.CallPrice();
double putFromParity1 = b1.PutFromCallParity();
double directPut1 = b1.PutPrice();
cout << "Batch 1 Parity Diff: " << abs(putFromParity1 - directPut1) << endl;
vector<EuropeanOption> allBatches = { b1, b2, b3, b4 };
for (size_t i = 0; i < allBatches.size(); ++i)
cout << "Batch " << (i + 1) << ": "
<< (allBatches[i].CheckParity() ? "OK" : "FAIL") << endl;
vector<double> S = CreateMesh(10.0, 50.0, 1.0);
EuropeanOption vopt(1.0, 30.0, 0.2, 0.05, 0.05, 30.0);
vector<double> callVec = vopt.CallPriceVector(S);
vector<double> putVec = vopt.PutPriceVector(S);
cout << "\nS\tCall\tPut\n";
for (size_t i = 0; i < min(size_t(10), S.size()); ++i)
cout << S[i] << "\t" << setprecision(3) << callVec[i] << "\t" << putVec[i] << endl;
vector<double> T = CreateMesh(0.5, 3.0, 0.5);
vector<double> callT = vopt.CallPriceByT(T);
vector<double> putT = vopt.PutPriceByT(T);
cout << "\nT\tCall\tPut\n";
for (size_t i = 0; i < T.size(); ++i)
cout << T[i] << "\t" << callT[i] << "\t" << putT[i] << endl;
vector<double> sig = CreateMesh(0.1, 0.5, 0.1);
vector<double> callSig = vopt.CallPriceBySig(sig);
vector<double> putSig = vopt.PutPriceBySig(sig);
cout << "\nSig\tCall\tPut\n";
for (size_t i = 0; i < sig.size(); ++i)
cout << sig[i] << "\t" << callSig[i] << "\t" << putSig[i] << endl;
vector<double> K = CreateMesh(25.0, 35.0, 2.5);
vector<double> callK, putK;
double K0 = vopt.GetK();
for (double k : K) {
vopt.SetParameters(vopt.GetT(), k, vopt.GetSig(), vopt.GetR(), vopt.GetB(), vopt.GetS());
callK.push_back(vopt.CallPrice());
putK.push_back(vopt.PutPrice());
}
vopt.SetParameters(vopt.GetT(), K0, vopt.GetSig(), vopt.GetR(), vopt.GetB(), vopt.GetS());
cout << "\nK\tCall\tPut\n";
for (size_t i = 0; i < K.size(); ++i)
cout << K[i] << "\t" << callK[i] << "\t" << putK[i] << endl;
vector<vector<EuropeanOption>> optMatrix(2, vector<EuropeanOption>(2));
vector<vector<double>> spotMatrix(2, vector<double>(2));
vector<double> vols = { 0.2, 0.3 };
vector<double> spots = { 95, 105 };
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
optMatrix[i][j] = EuropeanOption(1.0, 100.0, vols[i], 0.05, 0.05, spots[j]);
vector<vector<double>> priceM = EuropeanOption::CallPriceMatrix(spotMatrix, optMatrix);
cout << "\nMatrix Pricing (Call)\n";
for (int i = 0; i < 2; ++i)
cout << vols[i] << "\t" << priceM[i][0] << "\t" << priceM[i][1] << endl;
EuropeanOption greeks(0.5, 100.0, 0.36, 0.1, 0.0, 105.0);
printResult("Delta (Call)", greeks.CallDelta(), 0.5946, true);
printResult("Delta (Put)", greeks.PutDelta(), -0.3566, true);
printResult("Gamma", greeks.Gamma());
printResult("Theta", greeks.CallTheta());
printResult("Vega", greeks.CallVega());
printResult("Rho", greeks.CallRho());
vector<double> Sg = CreateMesh(25.0, 35.0, 1.0);
vector<double> dV = vopt.CallDeltaVector(Sg);
cout << "\nS\tDelta\n";
for (size_t i = 0; i < Sg.size(); ++i)
cout << Sg[i] << "\t" << dV[i] << endl;
vector<double> hVals = { 0.1, 0.01, 0.001, 0.0001 };
double aDelta = greeks.CallDelta();
double aGamma = greeks.Gamma();
cout << "\nh\tNumΔ\tΔerr\tNumΓ\tΓerr\n";
for (double h : hVals) {
double nD = greeks.CallDeltaNumerical(h);
double nG = greeks.GammaNumerical(h);
cout << h << "\t" << nD << "\t" << abs(aDelta - nD)
<< "\t" << nG << "\t" << abs(aGamma - nG) << endl;
}
PerpAmericanOption pao(100.0, 0.1, 0.1, 0.02, 110.0);
printResult("Perp Call", pao.CallPrice(), 18.5035, true);
printResult("Perp Put", pao.PutPrice(), 3.03106, true);
vector<double> S_perp = CreateMesh(80.0, 120.0, 10.0);
vector<double> pc = pao.CallPriceVector(S_perp);
vector<double> pp = pao.PutPriceVector(S_perp);
cout << "\nS\tCall\tPut\n";
for (size_t i = 0; i < S_perp.size(); ++i)
cout << S_perp[i] << "\t" << pc[i] << "\t" << pp[i] << endl;
double gCall = CallPriceGlobal(60.0, 65.0, 0.25, 0.08, 0.30, 0.08);
double gPut = PutPriceGlobal(60.0, 65.0, 0.25, 0.08, 0.30, 0.08);
printResult("Global Call", gCall, 2.13337, true);
printResult("Global Put", gPut, 5.84628, true);
double gDelta = CallDeltaGlobal(105.0, 100.0, 0.5, 0.1, 0.36, 0.0);
printResult("Global Delta", gDelta, 0.5946, true);
vector<double> globalCallVec = CallPriceVectorGlobal(S, 30.0, 1.0, 0.05, 0.2, 0.05);
cout << "\nGlobal Vector Match: "
<< (fabs(globalCallVec[0] - callVec[0]) < 1e-6 ? "YES" : "NO") << endl;
return 0;
return 0;
}