ConvergenceWarning:最大似然减慢了内核运行时间?

时间:2015-01-14 18:12:32

标签: python time-series forecasting statsmodels

我使用非常nicht代码对象arma_order_select_ic来为chor选择p值和q值找出最低的信息准则。

我不确定我是否做得对,或者代码是否偶然发现了一些错误......

在:

y = indexed_df
res = arma_order_select_ic(y, max_ar=7, max_ma=7, ic=['aic', 'bic', 'hqic'], trend='c', fit_kw=dict(method='css'))
print res
print ('AIC-order: {}' .format(res.aic_min_order))
print ('BIC-order: {}' .format(res.bic_min_order))
print ('HQIC-order: {}' .format(res.hqic_min_order)) 

出:

/Applications/anaconda/lib/python2.7/site-packages/statsmodels-0.6.1-py2.7-macosx-10.5-x86_64.egg/statsmodels/base/model.py:466: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
  "Check mle_retvals", ConvergenceWarning)

另外:它打印出三个矩阵式列表(对于每个IC一个矩阵)以及最终建议:

AIC-order: (7, 5)
BIC-order: (7, 0)
HQIC-order: (7, 0)

所以,整件事似乎都有效。

问题是,每次计算打印警告大约需要30-60秒,即它超级慢!

我检查了相关的源代码(statsmodels / base / model.py)以及如何跳过打印CovergenceWarning:

   #TODO: hardcode scale?
        if isinstance(retvals, dict):
            mlefit.mle_retvals = retvals
            if warn_convergence and not retvals['converged']:
                from warnings import warn
                from statsmodels.tools.sm_exceptions import ConvergenceWarning
                warn("Maximum Likelihood optimization failed to converge. "
                     "Check mle_retvals", ConvergenceWarning)

        mlefit.mle_settings = optim_settings
        return mlefit

所以我试图删除链接到ConvergenceWarning的if部分,但它不会工作。

这部分来自相同的源代码:

mle_retvals : dict
    Contains the values returned from the chosen optimization method if
    full_output is True during the fit.  Available only if the model
    is fit by maximum likelihood.  See notes below for the output from
    the different methods. 

没有告诉我在哪里以及如何更改mle_retvals

如何检查mle_retvals以及要更改的内容?

有没有办法让ConvergenceWarning消失,让计算运行得更快?

2 个答案:

答案 0 :(得分:3)

Notes部分中的文档明确说明了如何加快速度......请参阅fit_kw的docstring以更改ARMA.fit方法的参数。对于大量模型来说,这将是缓慢的。这是一个天真的实现,只是成对适合他们所有。尝试method='css'以获得更快的结果。

我不知道你为什么要改变mle_retvals。它在返回部分。这不是你直接改变的。您不必删除任何源代码即可运行。那张支票是为了警告你事情出了问题。也就是说,导致这些警告的模型可能是您数据的模型很差。

答案 1 :(得分:0)

您可以尝试隐藏警告。

import warnings

with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=ConvergenceWarning)
    y = indexed_df
    res = arma_order_select_ic(y, max_ar=7, max_ma=7, ic=['aic', 'bic', 'hqic'], trend='c', fit_kw=dict(method='css'))
    print res
    print ('AIC-order: {}' .format(res.aic_min_order))
    print ('BIC-order: {}' .format(res.bic_min_order))
    print ('HQIC-order: {}' .format(res.hqic_min_order))