bashtage / arch

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

Error when trying to forecast with a fixed model #396

Closed alfsn closed 4 years ago

alfsn commented 4 years ago

Hi Bashtage,

Am attaching the full code in the txt in case it may be of help.

I am getting the following Traceback:

`Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "D:\PyCharm\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "D:\PyCharm\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Alfred/PycharmProjects/tesis/fixed_model_evaluator.py", line 54, in <module>
    params, res, vol_fcast = fixed_model('GGAL', 'ARCH', 'normal', horizon=0, last_obs=end, p=1, q=0)
  File "C:/Users/Alfred/PycharmProjects/tesis/fixed_model_evaluator.py", line 47, in fixed_model
    res = am.fix(params=params['Value'].tolist(), first_obs=dfs[stock].index.min(), last_obs=last_obs)
  File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\univariate\base.py", line 458, in fix
    self._adjust_sample(first_obs, last_obs)
  File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\univariate\mean.py", line 565, in _adjust_sample
    _first_obs_index = cutoff_to_index(first_obs, index, 0)
  File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\utility\array.py", line 250, in cutoff_to_index
    int_index = date_to_index(cutoff, index)
  File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\utility\array.py", line 190, in date_to_index
    raise ValueError("date_index must be a datetime64 array")
ValueError: date_index must be a datetime64 array`

The code I am attempting to run is inside a function.

last_obs = '2019-12-01'

res = am.fix(params=params['Value'].tolist(), first_obs=dfs[stock].index.min(), last_obs=last_obs)

vol_fcast = res.forecast(horizon=horizon)

I have tried alternate specifications of last_obs

last_obs=np.datetime64('2019-12-01')

last_obs=np.arange(['2019-12-01', 2019-06-01'], dtype=datetime64[D])`

but did not work

I think this may have been solved in #156 and #157, but have not been able to work around it.

Sorry if this is not a bug! I am starting out with programming and would find any comments on this very helpful

Thanks in advance, Regards,

fixed_model_evaluator.txt

bashtage commented 4 years ago

You have horizon=0. Horizon must be 1 or more.

bashtage commented 4 years ago

If this doesn't work, please let me know. Closing since doesn't seem to be any fix needed.

alfsn commented 4 years ago

Thanks for your reply! I corrected the horizon, trying 1, 2 or 3, but the Value error is raised before this.

Once again, I attempted the operation with all of these:

`end = '2019-12-01'

end_date = np.datetime64(end)

dates = np.array([start, end], dtype='datetime64[D]')

start_end = np.array([start_date, end_date], dtype='datetime64')`

And once again:

Traceback (most recent call last): File "<input>", line 1, in <module> File "D:\PyCharm\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "D:\PyCharm\JetBrains\PyCharm 2019.1.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "C:/Users/Alfred/PycharmProjects/tesis/fixed_model_evaluator.py", line 56, in <module> params, res, vol_fcast = fixed_model('GGAL', 'ARCH', 'normal', horizon=3, last_obs=dates, p=1, q=0) File "C:/Users/Alfred/PycharmProjects/tesis/fixed_model_evaluator.py", line 49, in fixed_model res = am.fix(params=params['Value'].tolist(), first_obs=dfs[stock].index.min(), last_obs=last_obs) File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\univariate\base.py", line 458, in fix self._adjust_sample(first_obs, last_obs) File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\univariate\mean.py", line 565, in _adjust_sample _first_obs_index = cutoff_to_index(first_obs, index, 0) File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\utility\array.py", line 250, in cutoff_to_index int_index = date_to_index(cutoff, index) File "C:\Users\Alfred\PycharmProjects\pythonforfinance\venv\lib\site-packages\arch\utility\array.py", line 190, in date_to_index raise ValueError("date_index must be a datetime64 array") ValueError: date_index must be a datetime64 array

Thanks in advance. Regards,

bashtage commented 4 years ago

Here is a simplified workign example:

import arch
import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sb

# from descriptiva import dfs, stocks, return_lists, route, route_graphs, route_models

end = '2019-12-01'
start_date = np.datetime64('2019-06-01')
end_date = np.datetime64('2019-12-01')

start_end = np.array([start_date, end_date], dtype='datetime64')

dates = np.arange(start_date, end_date, dtype='datetime64[D]')

df = pd.read_csv("GGAL.csv")
df = df.set_index("Date")
df.index = pd.to_datetime(df.index)
dfs = df[["Adj Close"]].pct_change().dropna()
dfs.columns=["GGAL"]
print(df.head())

stock = 'GGAL'
vol = 'ARCH'
dist = 'normal'
horizon=1
last_obs=end
p=1
q=0

stock = str(stock.upper())
model = str(vol.upper())

am = arch.arch_model(dfs[stock], vol=vol, dist=dist, p=p, q=q)
res = am.fit()

res = am.fix(params=res.params, first_obs=dfs[stock].index.min(), last_obs=last_obs)

vol_fcast = res.forecast(horizon=horizon)
print(vol_fcast.variance)

Data zipped is below.

GGAL.zip

alfsn commented 4 years ago

Worked like a charm. Really grateful for the work you do. Thank you so much. Regards.