bashtage / arch

ARCH models in Python
Other
1.32k stars 245 forks source link

ARX exogenous regressors LinAlg problem #403

Closed alfsn closed 4 years ago

alfsn commented 4 years ago

Hi bashtage,

I have the following issue with an ARX regression, not sure what is the source of it.

I am including a simplified version of my code. For reference the last modification made was the [:end_date] on both the create_dummies formula and the arch_model(y=dfs['GGAL']['Returns'][:end_date]...) The code works without it.

I have also tried deleting this last step and fitting the model with .fit(last_obs=end_date) only to find the same mistake.

Traceback (most recent call last): File "<input>", line 1, in <module> File "C:/Users/Alfred/PycharmProjects/tesis/TESIS_estimador_modelos.py", line 86, in ARCH ARCH_model = model.fit(last_obs=end_date) File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\univariate\base.py", line 593, in fit resids = self.resids(self.starting_values()) File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\univariate\base.py", line 778, in starting_values params = np.asarray(self._fit_no_arch_normal_errors().params) File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\univariate\mean.py", line 618, in _fit_no_arch_normal_errors xpxi = np.linalg.inv(x.T.dot(x) / nobs) File "<__array_function__ internals>", line 6, in inv File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\numpy\linalg\linalg.py", line 551, in inv ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj) File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\numpy\linalg\linalg.py", line 97, in _raise_linalgerror_singular raise LinAlgError("Singular matrix") numpy.linalg.LinAlgError: Singular matrix

Thank you in advance for your help . Cheers, ARX-ARCH.txt

bashtage commented 4 years ago

Your final two columns are all 0s, which means x has rank 6 even thought it has 8 columns.

from arch import arch_model
import datetime as dt
import pandas_datareader as web
import numpy as np
import pandas as pd

start = dt.datetime(2007, 5, 2)
end = dt.datetime(2019, 12, 31)

dfs = {}
dfs['GGAL']=web.DataReader('GGAL','yahoo', start, end)
dfs['GGAL']["Returns"] =  dfs["GGAL"]['Adj Close'].pct_change()
dfs['GGAL'] = dfs['GGAL'].dropna()
end_date = np.datetime64('2018-12-01')

only_on_dict = {'125':              '03/11/2008',
                'AFJP':             '11/19/2008',
                'PASO_2011':        '08/14/2011',
                'PASO_2015':        '08/09/2015',
                '28D':              '12/28/2017',
                'Trump_War':        '07/06/2018',
                'PASO_2019':        '08/11/2019',
                'CEPO_2019':        '09/04/2019'}

def create_dummies(stock):
    dummies = pd.DataFrame()
    dummies['dates'] = dfs[stock][:end_date].index.to_list()

    for key, data in only_on_dict.items():
        dummies[key] = np.where(dummies['dates'] >= data, 1, 0)

    dummies.set_index(keys=dummies['dates'], inplace=True)
    dummies.drop('dates', axis=1, inplace=True)
    return dummies

x=create_dummies('GGAL')
print(np.linalg.matrix_rank(x))
model = arch_model(y=dfs['GGAL']['Returns'][:end_date],
                   x=x,
                   mean='ARX',
                   lags=2,
                   vol='ARCH',
                   p=2,
                   dist='normal')

ARCH_model = model.fit()
alfsn commented 4 years ago

Thank you! Closing since it worked excellently. Regards!