-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (92 loc) · 3.33 KB
/
main.py
File metadata and controls
122 lines (92 loc) · 3.33 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
from matplotlib import use as m_use
m_use('TkAgg')
from bcb import sgs
import pandas as pd
from sklearn.linear_model import LinearRegression
import numpy as np
import math
import seaborn as sns
import matplotlib.pyplot as plt
from statsmodels.tsa.filters.hp_filter import hpfilter
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.stattools import acf
from scipy.stats import jarque_bera
from scipy.stats import probplot
import pylab
data = sgs.get({'GDP' : 4380, # Seas adjusted GDP
'Infl' : 189, # IGP-M
'MS' : 27841, # Money Supply
'i' : 4390 # Nominal interest rate
}, start = '2000-01-01', end = '2023-04-01')
data = pd.DataFrame(data)
data = data.dropna()
# Deflating
data['ms'] = data['MS']/np.cumprod(data['Infl'] /100 + 1)
adfuller(np.log(data['ms'])) # Not stationary :/
data['ms_1'] = data['ms'].shift(1) # Using lagged value to diminish autocorrelation
data['gdp'] = data['GDP']/np.cumprod(data['Infl'] /100 + 1)
data['gdp_1'] = data['gdp'].shift(1)
adfuller(np.log(data['gdp'])) # Not stationary :/
data = data.dropna()
# Hodrick–Prescott filtering on log values
y = np.array(hpfilter(np.log(data['ms']), lamb = 1600)[0])
X = np.array([hpfilter(np.log(data['gdp']), lamb = 1600)[0],
hpfilter(np.log(data['i']), lamb = 1600)[0],
hpfilter(np.log(data['ms_1']), lamb = 1600)[0]])
X = X.transpose()
model = LinearRegression(fit_intercept=True)
model.fit(X, y)
model.coef_ # Elasticities
e_gdp, e_i = model.coef_[:-1]
model.score(X, y) #R2 values
print('Elasticity for GDP: %.3f\n'\
'Elasticity for i: %.3f\n' % (e_gdp, e_i))
residuals = y - model.predict(X)
sns.lineplot(y = residuals, x = range(0, len(residuals)))
acf(residuals, nlags = 10)
# Checking for normality
sns.distplot(x = residuals)
probplot(residuals, dist = 'norm', plot = pylab)
stat, p = jarque_bera(residuals)
print('---------------------------------\n'\
'Jarque-Bera test for normality\n'\
'---------------------------------\n'\
'Jarque-Bera: %.3f P-value: %.3f\n' % (stat, p))
if p >= 0.05 :
print('Evidence of normality\n')
else :
print('No evidence of normality\n')
print('---------------------------------')
# Trend
data['Trend'] = range(1, len(data)+ 1)
data['Trend2'] = data['Trend']^2
y = np.array(np.log(data['ms']))
X = np.array([np.log(data['gdp']),
np.log(data['i']),
data['Trend'],
data['Trend2']])
X = X.transpose()
model = LinearRegression(fit_intercept=True)
model.fit(X, y)
model.coef_ # Elasticities
e_gdp, e_i = model.coef_[:-2]
model.score(X, y) #R2 values
print('Elasticity for GDP: %.3f\n'\
'Elasticity for i: %.3f\n' % (e_gdp, e_i))
residuals = y - model.predict(X)
sns.lineplot(y = residuals, x = range(0, len(residuals)))
acf(residuals, nlags = 10)
# Evidences of autocorrelation. Estimates are not credible
# Checking for normality
sns.distplot(x = residuals)
probplot(residuals, dist = 'norm', plot = pylab)
stat, p = jarque_bera(residuals)
print('---------------------------------\n'\
'Jarque-Bera test for normality\n'\
'---------------------------------\n'\
'Jarque-Bera: %.3f P-value: %.3f\n' % (stat, p))
if p >= 0.05 :
print('Evidence of normality\n')
else :
print('No evidence of normality\n')
print('---------------------------------')