Estimating Structural VAR Model

Hello everybody

I’m going to estimate an SVAR model (Model A) of 6 variables using the following code:

import statsmodels.api as sm
from statsmodels.tsa.api import SVAR

A = np.zeros((6, 6)) # Initialize A matrix

Impose constraints from theoretical restrictions

A[0,:] = [1, 0, 0, 0, 0, 0] # Oil
A[1,:] = [0, 1, 0, 1, 0, 1] # M2
A[2,:] = [1, 1, 1, 0, 0, 0] # ER
A[3,:] = [0, 1, 1, 1, 0, 1] # Inf
A[4,:] = [1, 0, 1, 0, 1, 1] # YTM
A[5,:] = [0, 1, 1, 1, 1, 1] # TPX

svar_model = SVAR(df, svar_type=“A”, A=A)

svar_results = svar_model.fit(maxlags=4, solver=‘ncg’, trend=“ct”, maxiter=10000, maxfun=1000) # the problem is here

The last line caused the following error:

ValueError: shape mismatch: value array of shape (0,) could not be broadcast to indexing result of shape (0,6,6)

Could you please help me?

Hi Mahdi_vakii,

Thanks for posting the question. The ValueError caused by shape mismatch is a very common error whenever broadcasting is conducted, which makes it difficult to locate where the issue lies in this case : )

Would you mind sharing a longer error message that shows where this error is raised in the source code or, even better, providing a minimal workable example so that I can replicate this error?

Thanks,
Humphrey

Thank you so much dear Hunmphery for your answer
The error is:


ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_12216/802341051.py in
16
17 # Estimate the model with the new start_params
—> 18 svar_results = svar_model.fit(maxlags=4, solver=‘ncg’, trend=“ct”, maxiter=10000, maxfun=1000) # the problem is here

~\anaconda3-new\lib\site-packages\statsmodels\tsa\vector_ar\svar_model.py in fit(self, A_guess, B_guess, maxlags, method, ic, trend, verbose, s_method, solver, override, maxiter, maxfun)
181 start_params = self._get_init_params(A_guess, B_guess)
182
→ 183 return self._estimate_svar(start_params, lags, trend=trend,
184 solver=solver, override=override,
185 maxiter=maxiter, maxfun=maxfun)

~\anaconda3-new\lib\site-packages\statsmodels\tsa\vector_ar\svar_model.py in _estimate_svar(self, start_params, lags, maxiter, maxfun, trend, solver, override)
249 self.sigma_u = omega
250
→ 251 A, B = self._solve_AB(start_params, override=override,
252 solver=solver,
253 maxiter=maxiter)

~\anaconda3-new\lib\site-packages\statsmodels\tsa\vector_ar\svar_model.py in _solve_AB(self, start_params, maxiter, override, solver)
349 A_len = len(A[A_mask])
350
→ 351 A[A_mask] = start_params[:A_len]
352 B[B_mask] = start_params[A_len:]
353

ValueError: shape mismatch: value array of shape (0,) could not be broadcast to indexing result of shape (0,6,6)

Hi Mahdi_vakili,

The error message indicates that it might be how you define the matrix A that is causing the issue. When defining matrix A, we need to indicate elements that should be estimated using 'e' or 'E'.

From the source code, you can find a line that checks those elements:

A_mask = np.logical_or(A == 'E', A == 'e')

Please kindly see the discussion here for more detail: Scipy Stats Project: SVAR Estimation

Thank you from the bottom of my heart. I truly appreciate you that helped me solving the problem.

1 Like