-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculationFunction.cpp
66 lines (54 loc) · 1.54 KB
/
CalculationFunction.cpp
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
#include "CalculationFunction.h"
#include <vector>
#include <cmath>
#include <numeric>
#include "Matrix.h"
using namespace std;
namespace fre{
typedef vector<double> Vector;
typedef vector<Vector> Matrix;
// Calculate Abnormal Returns
Vector calculateAR(const Vector& actualReturn, const Vector& expectedReturn) {
Vector AR(actualReturn.size());
AR = actualReturn - expectedReturn;
return AR;
}
// Calculate Average Abnormal Returns
Vector calculateAverage(const Matrix& AR, int& numDays) {
Vector avg;
int d = 2 * numDays;
avg.resize(d);
for (int i = 0; i < d; i++) {
avg[i] = 0.0;
}
for (int i = 0; i < AR.size(); i++) {
avg = (i * avg + AR[i]) / (i + 1.0);
}
return avg;
}
// Calculate Cumulative Abnormal Returns
Vector calculateCAAR(const Vector& AAR){
Vector CAAR;
double temp = 0;
for (int i = 0; i < AAR.size(); i++){
temp += AAR[i];
CAAR.push_back(temp);
}
return CAAR;
}
// Calculate Standard Deviation of Cumulative Abnormal Returns
Vector calculateSTD(const Matrix& caarValues) {
int numSamples = caarValues.size();
int numDays = caarValues[0].size();
Vector avgCAAR = calculateAverage(caarValues, numDays);
Vector stdDev(numDays, 0.0);
for (int i = 0; i < numDays; i++) {
double sumSquaredDiffs = 0.0;
for (int j = 0; j < numSamples; j++) {
sumSquaredDiffs += pow(caarValues[j][i] - avgCAAR[i], 2);
}
stdDev[i] = sqrt(sumSquaredDiffs / numSamples);
}
return stdDev;
}
}