-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregression_function.py
51 lines (40 loc) · 1.53 KB
/
regression_function.py
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import warnings as warnings
import scipy.stats as stats
import statsmodels.api as sm
from index_pair_classes import *
warnings.filterwarnings('ignore')
def regressionPair(gsi_index_data, pair_data, time_window, MA = True, plot=True):
GSIObj = SentimentIndex(gsi_index_data)
PairObj = TradePair(pair_data)
y = PairObj.pair_data['Value'][13:-1]
if MA:
x = GSIObj.movingAverage(time_window).values[12:-2]
else:
x = GSIObj.percentageChange(time_window).values[12:-2]
# Add constant term to the independent variable
x = sm.add_constant(x)
model = sm.OLS(y, x)
results = model.fit()
regression_coeffs = results.params
std_errors = results.bse
t_stat = regression_coeffs / std_errors
p_values = results.pvalues
if plot:
fig = plt.figure(figsize=(10, 3))
plt.scatter(x[:, 1], y, color='green', label='Actual Values')
xfit = np.linspace(x[:, 1].min(), x[:, 1].max(), 1000)
yfit = results.predict(sm.add_constant(xfit))
plt.plot(xfit, yfit, color='orange', label='Fitted Values')
plt.legend()
if time_window > 0:
plt.title(f'{time_window} Month Regression')
else:
plt.title(f'Standard Index Regression')
plt.xlabel('Index Percentile')
plt.ylabel('Pair Value')
plt.show()
return regression_coeffs[0], regression_coeffs[1], t_stat[0], t_stat[1], p_values[0], p_values[1]