Implementing two equation SVAR model using statsmodels in python

58 views
Skip to first unread message

Gaggles

unread,
Jul 5, 2024, 9:18:56 AMJul 5
to pystatsmodels
I am trying to estimate a two equation SVAR model using statsmodels but I am facing an issue. For reference, I am using the following code where 'subset' is just a pandas dataframe with two columns (the two series for the variables of the system) and a time series index in datetime format. 


lags = 20
   
A = np.array([[1, 'E'], [0, 1]])  
   
A_guess = np.asarray([0.0002])

model = SVAR(subset, svar_type='A', A=A)

results = model.fit(A_guess = A_guess, maxlags=20, maxiter = 10000000, maxfun=1000000, solver='bfgs', trend="n")


Running the code I get the following error


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File /Users/test/Desktop/anders_project/code/sql_server_retrieval.py:27
     24     model = SVAR(subset, svar_type='A', A=A)
     26     # Fit the model
---> 27     results = model.fit(maxlags=20, maxiter = 10000000, maxfun=1000000, solver='bfgs', trend="n")
     28     var_results[ticker] = results
     31 # # %% (C)(R) VAR model estimation -> apparently I cannot estimate without intercept so need to find an alternative; also, there seem to be data issues so i need to clean data/remove outliers first
     32
     33 # from statsmodels.tsa.vector_ar.var_model import VAR
   (...)
     66 #     print(f"Results for {ticker}:")
     67 #     result.summary()

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/statsmodels/tsa/vector_ar/svar_model.py:180, in SVAR.fit(self, A_guess, B_guess, maxlags, method, ic, trend, verbose, s_method, solver, override, maxiter, maxfun)
    177 # initialize starting parameters
    178 start_params = self._get_init_params(A_guess, B_guess)
--> 180 return self._estimate_svar(start_params, lags, trend=trend,
    181                            solver=solver, override=override,
    182                            maxiter=maxiter, maxfun=maxfun)

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/statsmodels/tsa/vector_ar/svar_model.py:248, in SVAR._estimate_svar(self, start_params, lags, maxiter, maxfun, trend, solver, override)
    245 omega = sse / df_resid
    246 self.sigma_u = omega
--> 248 A, B = self._solve_AB(start_params, override=override,
...
--> 279     A[A_mask] = params[:A_len]
    280 if B is not None:
    281     B[B_mask] = params[A_len:A_len+B_len]

TypeError: NumPy boolean array indexing assignment requires a 0 or 1-dimensional input, input has 2 dimensions


I tried estimating the model without time series index, without A_guess and using np.nan instead of 'E' which are all suggestions I found online but the issue persists.

Does therefore anyone have an idea how to resolve the issue? There is very little content on this topic online so I hope that someone here might be able to help. 

Thank you in advance!

josef...@gmail.com

unread,
Jul 5, 2024, 9:29:15 AMJul 5
to pystat...@googlegroups.com
Can you check the unit tests for examples.
I’m at the seaside with only occasional internet 

My guess without looking at the code is that A_guess needs to have the same dimension as A.
However a bug in svar is also possible 

Josef
--
You received this message because you are subscribed to the Google Groups "pystatsmodels" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pystatsmodels+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pystatsmodels/1a03c949-3fe3-4a87-89a1-377a0cce83c6n%40googlegroups.com.

Gaggles

unread,
Jul 5, 2024, 9:56:09 AMJul 5
to pystatsmodels
I did a unit test and both time series are stationary. I also know that A_guess has to have as many values as the number of parameters in A that you would like to estimate which are represented by the 'E' in A. In my case that is one parameter so A_guess should contain one value what is the case. But I might be wrong so please correct me if you spot a mistake!
To unsubscribe from this group and stop receiving emails from it, send an email to pystatsmodel...@googlegroups.com.

josef...@gmail.com

unread,
Jul 5, 2024, 12:52:40 PMJul 5
to pystat...@googlegroups.com
I tried an example and it looks like there are several bugs for this case.
One likely reason is that numpy got more strict with shape mismatch in masked assignments.
Without enough unit test coverage, those cases did not get the maintenance fixes. For example we had to add many np.squeeze to work with recent numpy versions.
There might also be old bugs when there are only zero or one parameter to estimate in A and B.
All exceptions that I saw are shape mismatches with extra or missing dimensions.

Can you open an issue?
I try to look into it next week when I have better internet access.
To unsubscribe from this group and stop receiving emails from it, send an email to pystatsmodels+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pystatsmodels" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pystatsmodels+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pystatsmodels/935e2e06-f032-40c6-99ce-6a9f2a5cdca1n%40googlegroups.com.

Gaggles

unread,
Jul 6, 2024, 1:28:28 PMJul 6
to pystatsmodels
Thanks for your help! How and where can I open an issue?

To unsubscribe from this group and stop receiving emails from it, send an email to pystatsmodel...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pystatsmodels" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pystatsmodel...@googlegroups.com.

josef...@gmail.com

unread,
Jul 6, 2024, 2:19:48 PMJul 6
to pystat...@googlegroups.com

Gaggles

unread,
Jul 11, 2024, 10:44:10 AMJul 11
to pystatsmodels
Thanks, I opened an issue. You can find it here.
Reply all
Reply to author
Forward
0 new messages