forked from thiagowinkler/pairs-trading-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearRegression.cpp
111 lines (89 loc) · 3.3 KB
/
LinearRegression.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
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
#include "LinearRegression.h"
// Constructor
LinearRegression::LinearRegression(TimeSeries* Y, TimeSeries* X)
{
double sumXY = 0.0;
double sumX = 0.0;
double sumY = 0.0;
double sumX2 = 0.0;
double sumY2 = 0.0;
this->Y = Y;
this->X = X;
// For every value in the sample of the two time series
for(TimeSeries::iterator itY = this->Y->in_sample_begin(), itX = this->X->in_sample_begin(); itY != this->Y->in_sample_end() && itX != this->X->in_sample_end(); ++itY, ++itX)
{
sumXY += (*itX).second->get_value() * (*itY).second->get_value();
sumX += (*itX).second->get_value();
sumY += (*itY).second->get_value();
sumX2 += (*itX).second->get_value() * (*itX).second->get_value();
sumY2 += (*itY).second->get_value() * (*itY).second->get_value();
}
// Correlation between the two time series
this->rho = (this->Y->sample_size() * sumXY - sumX * sumY) / (sqrt((this->Y->sample_size() * sumX2 - sumX * sumX) * (this->Y->sample_size() * sumY2 - sumY * sumY)));
// Intercept of the linear regression
this->beta = (this->Y->sample_size() * sumXY - sumX * sumY) / (this->Y->sample_size() * sumX2 - sumX * sumX);
// Slope of the linear regression
this->alpha = (sumY - this->beta * sumX) / this->Y->sample_size();
return;
}
// Destructor
LinearRegression::~LinearRegression()
{
return;
}
// Re-estimate the linear regression
void LinearRegression::reset()
{
double sumXY = 0.0;
double sumX = 0.0;
double sumY = 0.0;
double sumX2 = 0.0;
double sumY2 = 0.0;
// For every value in the sample of the two time series
for(TimeSeries::iterator itY = this->Y->in_sample_begin(), itX = this->X->in_sample_begin(); itY != this->Y->in_sample_end() && itX != this->X->in_sample_end(); ++itY, ++itX)
{
sumXY += (*itX).second->get_value() * (*itY).second->get_value();
sumX += (*itX).second->get_value();
sumY += (*itY).second->get_value();
sumX2 += (*itX).second->get_value() * (*itX).second->get_value();
sumY2 += (*itY).second->get_value() * (*itY).second->get_value();
}
// Correlation between the two time series
this->rho = (this->Y->sample_size() * sumXY - sumX * sumY) / (sqrt((this->Y->sample_size() * sumX2 - sumX * sumX) * (this->Y->sample_size() * sumY2 - sumY * sumY)));
// Intercept of the linear regression
this->beta = (this->Y->sample_size() * sumXY - sumX * sumY) / (this->Y->sample_size() * sumX2 - sumX * sumX);
// Slope of the linear regression
this->alpha = (sumY - this->beta * sumX) / this->Y->sample_size();
return;
}
// Calculate the series of residuals
TimeSeries* LinearRegression::residuals(std::string name)
{
TimeSeries* R;
double value = 0.0;
R = new TimeSeries(name);
// For every value in the two time series
for(TimeSeries::iterator itY = this->Y->series_begin(), itX = this->X->series_begin(); itY != this->Y->series_end() && itX != this->X->series_end(); ++itY, ++itX)
{
// R(t) = Y(t) - alpha - beta * X(t)
value = (*itY).second->get_value() - this->get_alpha() - this->get_beta() * (*itX).second->get_value();
R->insert((*itY).second->get_date(), value);
}
R->set_sample_date(this->Y->get_sample_date());
return R;
}
// Getter of the correlation
double LinearRegression::get_rho()
{
return this->rho;
}
// Getter of the intercept
double LinearRegression::get_alpha()
{
return this->alpha;
}
// Getter of the slope
double LinearRegression::get_beta()
{
return this->beta;
}