Simultaneous Equations Modelling in Python
Simultaneous Equations Modelling in Python
1
The steps for constructing regression specifications remain similar, albeit via a different library. The
first step of these would be adding a constant term to the data by the function add_constant from
Statsmodels. Subsequently, the 2SLS regression model instance is created by performing the function
IV2SLS. In its brackets, we specify four parameters: the dependent variable, the exogenous variable,
the endogenous variable and the instruments. Specifically, the first parameter dependent is defined
as the series ’inflation’. exog are const dprod dcredit dmoney, while the variable rsandp is set as
endog. Last but not least, the list of Instruments comprises the variables ’rterm’ and ’dspread’.
The res_2sls regression result instance is then generated by the function fit. However, in this case,
we type the argument cov_type=’unadjusted’ for the function.
Parameter Estimates
==============================================================================
Parameter Std. Err. T-stat P-value Lower CI Upper CI
------------------------------------------------------------------------------
const 0.2129 0.0369 5.7777 0.0000 0.1407 0.2852
dprod 0.0309 0.0500 0.6172 0.5371 -0.0671 0.1289
dcredit -0.0052 0.0019 -2.7214 0.0065 -0.0089 -0.0015
dmoney -0.0028 0.0011 -2.6408 0.0083 -0.0049 -0.0007
rsandp 0.1037 0.0333 3.1092 0.0019 0.0383 0.1690
==============================================================================
Endogenous: rsandp
Instruments: rterm, dspread
Unadjusted Covariance (Homoskedastic)
Debiased: False
Similarly, the inputs for the ’rsandp’ equation would be specified as in the follow code cell and the
output for the returns equation is shown below.
2
exog = data[['const','dprod','dcredit','dmoney']],\
endog = data.inflation,\
instruments = data[['rterm','dspread']])
res_2sls2 = ivmod.fit(cov_type='unadjusted')
print(res_2sls2)
Parameter Estimates
==============================================================================
Parameter Std. Err. T-stat P-value Lower CI Upper CI
------------------------------------------------------------------------------
const -1.1624 0.6697 -1.7357 0.0826 -2.4750 0.1502
dprod -0.2366 0.4376 -0.5406 0.5888 -1.0942 0.6211
dcredit 0.0368 0.0186 1.9851 0.0471 0.0005 0.0732
dmoney 0.0185 0.0108 1.7087 0.0875 -0.0027 0.0397
inflation 6.3039 2.2728 2.7736 0.0055 1.8493 10.759
==============================================================================
Endogenous: inflation
Instruments: rterm, dspread
Unadjusted Covariance (Homoskedastic)
Debiased: False
The results show that the stock index returns are a positive and significant determinant of inflation
(changes in the money supply negatively affect inflation), while inflation also has a positive effect on
the stock returns, albeit less significantly so.
3
The Generalised method of moments for instrumental variables
Secciones 7.8 material impreso y Anexo adjunto al final (The Generalised Method of Moments)
Apart from 2SLS, there are other ways to address the endogeneity issue in a system of equations.
Following the previous section using inflation and stock returns, we apply the same "macro.pickle"
Python workfile to explore a different technique: the Generalised Method of Moments (GMM). First,
recall the estimated models as follows:
The steps for constructing regression specifications are the same as the previous ones, albeit via a
different regression function. The first step is writing the regression formula. Within the GMM
setting, the regression model is the dependent variable followed by the exogenous variables with en-
dogenous variables and instruments added afterwards in squared brackets. Specifically, the formula
statement is as follows:
where inflation is the dependent variable; 1 is the constant term; dprod, dcredit, and dmoney
are exogenous variables; rsandp is the endogenous variable; rterm and dspread are instru-
ments. Subsequently, the GMM regression model instance is created by performing the function
IVGMM.from_formula. In its brackets, we input the formula specified, data and weighting scheme.
Next, the covariance type is set as robust in the fit. Executing the cell will lead the regression results
to be displayed in the output window.
4
IV-GMM Estimation Summary
==============================================================================
Dep. Variable: inflation R-squared: -1.8273
Estimator: IV-GMM Adj. R-squared: -1.8571
No. Observations: 384 F-statistic: 20.058
Date: Sun, Nov 11 2018 P-value (F-stat) 0.0005
Time: 22:16:12 Distribution: chi2(4)
Cov. Estimator: robust
Parameter Estimates
==============================================================================
Parameter Std. Err. T-stat P-value Lower CI Upper CI
------------------------------------------------------------------------------
Intercept 0.2129 0.0422 5.0416 0.0000 0.1302 0.2957
dprod 0.0309 0.0699 0.4413 0.6590 -0.1062 0.1679
dcredit -0.0052 0.0017 -2.9732 0.0029 -0.0086 -0.0018
dmoney -0.0028 0.0011 -2.5944 0.0095 -0.0049 -0.0007
rsandp 0.1037 0.0419 2.4768 0.0133 0.0216 0.1857
==============================================================================
Endogenous: rsandp
Instruments: rterm, dspread
GMM Covariance
Debiased: False
Robust (Heteroskedastic)
Similarly, the second specification for the ’rsandp’ equation would be written as in the following
code cell and the output for the returns equation is shown below.
Parameter Estimates
==============================================================================
Parameter Std. Err. T-stat P-value Lower CI Upper CI
------------------------------------------------------------------------------
5
Intercept -1.1624 0.8919 -1.3033 0.1925 -2.9105 0.5857
dprod -0.2366 0.6369 -0.3714 0.7103 -1.4849 1.0117
dcredit 0.0368 0.0185 1.9929 0.0463 0.0006 0.0731
dmoney 0.0185 0.0104 1.7849 0.0743 -0.0018 0.0388
inflation 6.3039 3.1875 1.9777 0.0480 0.0565 12.551
==============================================================================
Endogenous: inflation
Instruments: rterm, dspread
GMM Covariance
Debiased: False
Robust (Heteroskedastic)
The results show that the stock index returns are a positive and significant determinant of inflation
(changes in the money supply negatively affect inflation), while inflation also has a positive effect on
the stock returns, albeit less significantly so.
6
VAR estimation
Sección 7.10 material impreso
In this section, a VAR is estimated in order to examine whether there are lead–lag relationships
between the returns to three exchange rates against the US dollar: the euro, the British pound and
the Japanese yen. The data are daily and run from 14 December 1998 to 3 July 2018, giving a total of
7,142 observations. The data are contained in the Excel file ’currencies.xls’.
First, we import the dataset into the NoteBook. Next, we construct a set of continuously com-
pounded percentage returns called ’reur’, ’rgbp’ and ’rjpy’ using a custom function LogDiff. More-
over, these new variables are saved in a workfile currencies.pickle for the future usage.
def LogDiff(x):
x_diff = 100*np.log(x/x.shift(1))
x_diff = x_diff.dropna()
return x_diff
data = pd.DataFrame({'reur':LogDiff(data['EUR']),
'rgbp':LogDiff(data['GBP']),
'rjpy':LogDiff(data['JPY'])})
VAR estimation in Python can be accomplished by importing the function VAR from the library
statsmodels.tsa.api. The VAR specification appears as in the code cell [2]. We define the dependent
variables to be ’reur’, ’rgbp’ and ’rjpy’. Next we need to specify the number of lags to be included
for each of these variables. In this case, the maximum number of lags is two, i.e., the first lag and
the second lag. Let us write the argument maxlags=2 for the regression instance and estimate this
VAR(2) model. The regression output appears as below.
In [2]: # VAR
model = smt.VAR(data)
res = model.fit(maxlags=2)
print(res.summary())
7
Time: 15:35:25
--------------------------------------------------------------------
No. of Equations: 3.00000 BIC: -5.41698
Nobs: 7139.00 HQIC: -5.43024
Log likelihood: -10960.3 FPE: 0.00435167
AIC: -5.43720 Det(Omega_mle): 0.00433889
--------------------------------------------------------------------
Results for equation reur
==========================================================================
coefficient std. error t-stat prob
--------------------------------------------------------------------------
const 0.000137 0.005444 0.025 0.980
L1.reur 0.147497 0.015678 9.408 0.000
L1.rgbp -0.018356 0.017037 -1.077 0.281
L1.rjpy -0.007098 0.012120 -0.586 0.558
L2.reur -0.011808 0.015663 -0.754 0.451
L2.rgbp 0.006623 0.017032 0.389 0.697
L2.rjpy -0.005427 0.012120 -0.448 0.654
==========================================================================
8
rgbp 0.634447 1.000000 0.164311
rjpy 0.270764 0.164311 1.000000
At the top of the table, we find information for the model as a whole, including values of the infor-
mation criteria, while further down we find coefficient estimates and goodness-of-fit measures for
each of the equations separately. Each regression equation is separated by a horizontal line.
We will shortly discuss the interpretation of the output, but the example so far has assumed that
we know the appropriate lag length for the VAR. However, in practice, the first step in the construction
of any VAR model, once the variables that will enter the VAR have been decided, will be to determine
the appropriate lag length. This can be achieved in a variety of ways, but one of the easiest is to em-
ploy a multivariate information criterion. In Python, this can be done by calling the built-in function
select_order based on the regression model instance. In the specification brackets of the function, we
specify the maximum number of lags to entertain including in the model, and for this example, we
arbitrarily enter 10. By executing the code cell, we should be able to observe the following output.
Python presents the values of various information criteria and other methods for determining the lag
order. In this case, the Akaike (AIC) and Akaike’s Final Prediction Error Criterion (FPE) both select
a lag length of four as optimal, while Schwarz’s (SBIC) criterion chooses a VAR(1) and the Hannan-
Quinn (HQIC) criteria selects a VAR(2). Let us estimate a VAR(1) and examine the results. Does the
model look as if it fits the data well? Why or why not?
Next, we run a Granger causality test. We call the statsmodels.tsa.api function VAR again and
construct a VAR regression instance. Next, we run the built-in function test_causality and specify
the parameters as follows: causing variable ’rgbp’, caused variable ’reur’, performing test options
’wald’ and significance level for computing critical values, 0.05. It is unfortunate that the Granger
causality can only be tested between two variables in Python since we want to run the test among
9
all variables. However, this can be done by separately estimating each of the pairwise combinations
and noting down the statistics in each case (Table 1).31
#--------------------------------------------------
# Equation reur, Excluded rgbp
resCausality = res.test_causality(causing=['rgbp'],
caused=['reur'],
kind='wald',signif=0.05 )
31 Inthe code cell, we only demonstrate the command for the equation reur due to limited space. Users can modify it
for other equations.
10
Table 1: Granger Causality Wald tests
Equation Excluded chi2 Critical value p-value df
reur rgbp 1.186 5.991 0.553 2
reur rjpy 0.6260 5.991 0.731 2
reur All 1.764 9.488 0.779 4
rgbp reur 12.88 5.991 0.002 2
rgbp rjpy 12.92 5.991 0.002 2
rgbp All 28.99 9.488 0.000 4
rjpy reur 7.320 5.991 0.026 2
rjpy rgbp 17.12 5.991 0.000 2
rjpy All 17.38 9.488 0.002 4
The results show only modest evidence of lead-lag interactions between the series. Since we have
estimated a tri-variate VAR, three panels are displayed, with one for each dependent variable in the
system. There is causality from EUR to GBP and from JPY to GBP that is significant at the 1% level.
We also find significant causality at the 5% level from EUR to JPY and GBP to JPY, but no causality
from any of the currencies to EUR. These results might be interpreted as suggesting that information
is incorporated slightly more quickly in the pound-dollar rate and yen-dollar rates than into the
euro-dollar rate.
It is preferable to visualise the impact of changes in one variable on the others at different hori-
zons. One way to achieve this is to obtain the impulse responses for the estimated model. To do so,
we first re-define the dependent variables (reur rgbp rjpy) and select a VAR model with one lag of
each variable. This can be done by inputting the argument maxlags=1. We then specify that we want
to generate a graph for the irf, that is, the built-in function from the VAR result instance. Finally, we
need to select the number of periods over which we want to generate the IRFs. We arbitrarily select
20 and feed it to irf. Type the command irf.plot() and Python produces the impulse response graphs
(Figure 27) as below.
Out[5]:
11
Figure 27: Impulse Responses
As one would expect given the parameter estimates and the Granger causality test results, only a few
linkages between the series are established here. The responses to the shocks are very small, except
for the response of a variable to its own shock, and they die down to almost nothing after the first
lag.
Note that plots of the variance decompositions (also known as forecast error variance decompo-
sitions, or fevd in Python) can also be generated using the fevd function. Instead of plotting the IRFs
by the irf function, we choose fevd – that is, the forecast-error variance decompositions. Bar charts
for the variance decompositions would appear as follows (see Figure 28).
12
fevd = res.fevd(20)
fevd.plot()
Out[6]:
To illustrate how to interpret the FEVDs, let us have a look at the effect that a shock to the euro rates
has on the other two rates and on later values of the euro series itself, which are shown in the first
row of the FEVD plot. Interestingly, while the percentage of the errors that is attributable to own
shocks is 100% in the case of the euro rate (dark black bar), for the pound, the euro series explains
around 40% of the variation in returns (top middle graph), and for the yen, the euro series explains
around 7% of the variation.
13
We should remember that the ordering of the variables has an effect on the impulse responses and
variance decompositions, and when, as in this case, theory does not suggest an obvious ordering of
the series, some sensitivity analysis should be undertaken. Let us assume we would like to test how
sensitive the FEVDs are to a different way of ordering. We first generate a new DataFrame data1
with the reverse order of the columns to be used previously, which is rjpy rgbp reur. To inspect and
compare the FEVDs for this ordering and the previous one, we can create graphs of the FEVDs by
implementing the VAR regression and fevd function again. We can then compare the FEVDs of the
reverse order (Figure 29) with those of the previous order.
model = smt.VAR(data1)
res = model.fit(maxlags=1)
Out[7]:
14
Figure 29: Variance Decompositions for Different Orderings
15
1
Anexo
•
2
for the latter. lf we have k parameters to estímate, we need k sample moments. So,
for example, if the observed data (y) are assumed to follow a normal distribution,
there are two parameters we would need to estímate: the mean and the variance.
To estímate the population mean (call this µ0), we know that E[yi ] - µ0 = O. We
also know that the sample moments will converge to their population counterparts
asymptotically by the law of large numbers. So, as the number of data points T
increases, we have that
1 T
-:¡.¿Y, -1.1,0- O as r- oo
l=l
Thus the f1rst sample moment condition is found by taking the usual sample average
of y,, y
T
1
-;¡LY,-J..lo = O (14.51)
l=l
We would then adopt the same approach to match the second moment
(14.52)
and thus
(14.53)
and so we have
(14.54)
lf we had a more complex distribution with more than two parameters, we would
simply continue to compute the third, fourth, ... moments until we had the same
number as parameters to estimate.
In the context of estimation of the parameters in a regression model, the method
of moments relies principally on the assumption that the explanatory variables are
orthogonal to the disturbances in the model
Elu,xi] =O (14.55)
Solving these moment conditions would lead to the familiar OLS estimator for fi
given in equation (4.8) of Chapter 4. Again, in practice we use the sample analogue
of the moments of E(y).
In terms of its properties, the method of moments is a consistent estimator but
sometimes not efficient. The maximum likelihood technique (see Chapter 9 of this
book for details) uses information from the entire assumed distribution function, and
OLS requires an assumption about the independence of the error terms while the
method of moments (and GMM) use only information on specif1c moments and thus
the latter is more flexible and less restrictive.
The sample analogue of this equation is effectively the mean of each moment
condition
T
111¡(y,,xr ;fi) = ¿ m¡(y,,x, ;fi) = O (14.58}
r=I
Note that it does not matter here whether we divide by l/T or not since this term
would cancel out anyway. As discussed above, if L = k, these L equations will have
a unique solution and thus for such exactly identified systems, ali of the moment
conditions in the equation above will be exactly zero, but there will be more than
14
Sce Chaptcr 7 for a detailed discussion of this conccpt in a diffcrent comcxt.
4
one solution when L > k. In such cases we woulcl choose the parameters that come
as near as possible to solving this, which would mean that the sample moment vector
is as close to zero as possible. This would be written as
where m(/3) = (111 1, •••, 111¿) are the L moment conditions (which will be a function of
the estimated parameters, /3, and W is the weighting matrix which must be positive
definite. It is possible to show that the optima! W is the inverse of the variance
[
covariance matrix of the moment conditions
T ]-1
w = � e� m(/3) in(/3)') (14.60)
in Ferson and Foerster ( 1994) has suggested that GMM estimators may be oversized
for modest numbers of data points.
Under sorne assumptions, it is possible to show that the GMM estimator is
asymptotically normal with mean equal to the true parameter vector and a variance
that is an inverse function of the sample size and of the partial derivatives of the
moments with respect to the parameters - see Hansen ( 1982).
The Fama-MacBeth procedure involves two steps to implement the model (see
Section l 4.2 earlier in this chapter): first, a set of time-series regressions to estímate
the factor exposures, B, and second, a set of cross-sectional regressions to estímate the
risk premia, /\. If we further define µ, to be a K x 1 vector of means of each of
the factors, the f1rst of these stages to estímate the factor loadings would involve
the regressions
R1 = A + 8ft + lit (14.62)
where y;, 1 represents the log ofreal GDP per capita in country i at time t, x;, 1 in eludes
ali of the explanatory variables except the previous leve! of GDP per capita (which
is separated out), f3 is a vector of slope parameters and u;,r is a disturbance term. The
additional term, 1J has an i subscript but no t subscript, indicating that it varíes by
country and not over time. This is a vector of parameters that allows the intercept
to be different for each country. These are known as country jixed effects, and are
discussed in detail in Chapter 11. The authors turn equation (14.66) into a f1rst
difference form
In this equation, the country-specif1c effects (17¡) have dropped out when using a
difference form since they do not vary over time. GMM is employed as the core
estimation approach rather than OLS. lf we write the error term in this equation
(14.67) as v;. 1 = (u;, 1 - u;, 1-1) for simplicity, then we could use the following moment
conditions
E[y;,1-sVi,t] =O (14.68)
E[.r;,1-slJ;,i] =O (14.69)
':lbl 1 .5 GMM estimates of the effect of stock markets and bank lending on economic
growth
(0.229)
(0.001)***
lnílation rate 0.866
(0.336)
Black market premium -0.788
(O.738)
Bank credit 0.749 0.683 -0.471 0.370 0.626
overidentifying restrictions are satisf1ed and that the moments are close to zero
and therefore the proposed models are adequate. Likewise, for the autocorrelation
test reported in the second row of that panel, the p-values are ali greater than 0.1
(albeit only marginally in specification (3)), and therefore there is no evidence of
autocorrelation in the residuals from the f1tted model. The final row presenrs the p
values from Wald tests, akin to the regression F-statistic which measures the joint
signif1cance of ali parameters in the model. In this case, we would want to reject
the null hypothesis that ali of the parameters are zero, but we are unable to do so
in any of the specif1cations ancl thus the model in differences form clisplayed here
fails this test. This is not the case, however, for the moclels in other forms or for the
more complex hybricl between the levels ancl clifferences form not presentecl here -
see Beck and Levine (2004) for further cletails.
The main conclusion from the study is that both stock market depth and bank
lending - and thus overall financia! development - enhance economic growth as
both have positive and statistically signif1cant parameter estimates in the majority
of specif 1 cations that Beck and Levine examine. As is ohen the case, while GMM
is demonstrably superior from an econometric perspective, as the authors note, the
conclusions are mostly not qualitatively altered compared with the case where OLS
is used.