Skip to content

Erroe: fcast = mu + np.dot(arparams, endog[i:i+p])  #5258

Closed

Description

@Martin1937

from statsmodels.tsa.ar_model import AR
import numpy as np

sample = np.array([0.46341460943222046, 0.46341460943222046, 0.39024388790130615, 0.4146341383457184, 0.4146341383457184, 0.4146341383457184, 0.3414634168148041, 0.4390243887901306, 0.46341460943222046, 0.4390243887901306])
model = AR(sample)
model_fit = model.fit(disp=0)
output = model_fit.predict(start=10,end=12)
print(output)

The python code is as above, When I run this code, I got following error,
File "AR_test.py", line 8, in
output = model_fit.predict(start=10,end=12)
File "D:\anaconda\envs\tensorflow\lib\site-packages\statsmodels\base\wrapper.py", line 95, in wrapper
obj = data.wrap_output(func(results, *args, **kwargs), how)
File "D:\anaconda\envs\tensorflow\lib\site-packages\statsmodels\tsa\ar_model.py", line 795, in predict
predictedvalues = self.model.predict(params, start, end, dynamic)
File "D:\anaconda\envs\tensorflow\lib\site-packages\statsmodels\tsa\ar_model.py", line 237, in predict
out_of_sample)
File "D:\anaconda\envs\tensorflow\lib\site-packages\statsmodels\tsa\ar_model.py", line 49, in _ar_predict_out_of_sample
fcast = mu + np.dot(arparams, endog[i:i+p])
ValueError: shapes (6,) and (7,) not aligned: 6 (dim 0) != 7 (dim 0)

Could anyone suggest how to fix this issue? Thanks in advance.

Activity

  1. jtweeder commented on Oct 12, 2018

    @jtweeder
    Contributor

    My initial hypothesis about the maxlag default value was incorrect so I edited it with my new findings, although you can return the sample model prediction if you set maxlag to 6 or lower in the .fit() statement:

    I believe this has to do with the add_trend that is getting called during _xstackX of the .fit() call. It may have to do with the repetitive nature of the sample data (the three repeating 0.414...) as it seems to think a constant is already added, and does not add one. Therefore the number of parameters is misshapen.

    from statsmodels.tsa.ar_model import AR
    import numpy as np
    
    sample = np.array([0.46341460943222046, 0.46341460943222046, 0.39024388790130615, 
                       0.4146341383457184, 0.4146341383457184, 0.4146341383457184, 
                       0.3414634168148041, 0.4390243887901306, 0.46341460943222046, 
                       0.4390243887901306])
    model1 = AR(sample)
    model_fit1 = model1.fit()
    len(model_fit1.params) #returns a length of 7
    
    
    #Changing the data slightly
    sample = np.array([0.461460943222046, 0.46341460943222046, 0.39024388790130615, 
                     0.5146341383457184, 0.2146341383457184, 0.4146341383457184, 
                     0.3414634168148041, 0.4390243887901306, 0.46341460943222046, 
                     0.4390243887901306])
    model2 = AR(sample)
    model_fit2 = model2.fit()
    len(model_fit2.params) #returns a length of 8

    This is the piece in statsmodels.tsa.tsatools.add_trend that is evaluating for the already existing constant column and by default skips if it is present.

    np.logical_and(np.any(np.ptp(np.asanyarray(x), axis=0) == 0, axis=0),
                                           np.all(x != 0.0, axis=0))

    Where x is the array of lags. For model 1 it returns true, and skips the adding constant value which leaves it one short when it does the OLS fitting which produces the model that is one value short.

    from statsmodels.tsa.tsatools import lagmat
    x = lagmat(model1.endog, model1.k_ar, trim='both')
    np.logical_and(np.any(np.ptp(np.asanyarray(x), axis=0) == 0, axis=0),
                                           np.all(x != 0.0, axis=0))

    image

    Not sure if that is a bug or not, or if this is just a unique circumstance with the sample values, I also tried replacing those repeating 0.41463... with other repeating numbers and the error occurred.

    Hope that helps to tease out the issue a little more clearly, and what I have found so far.

  2. josef-pkt commented on Dec 11, 2018

    @josef-pkt
    Member

    This got lost without label.
    temporarily prio-high to figure out what's going on.

  3. added this to the 0.10 milestone on Dec 11, 2018
  4. jbrockmendel commented on May 28, 2019

    @jbrockmendel
    Contributor

    @jtweeder is right. Before the call to add_trend column 3 is constant, so add_trend silently changes trend from "c" to "nc", but that information doesn't make it back to the AR object, which still has k_trend set to 1.

    If instead of calling res = model.fit() we do:

    res = model.fit(trend="nc")
    output = res.predict(start=10, end=12)
    
    >>> print(output)
    [0.44687422 0.45608137 0.47046381]
    

    Note that res.params here matches model_fit.params the OP would have gotten. The only difference is that model.k_trend (the fact that this is a model attribute is a different can of worms) attribute is correctly set to 0.

    Two options for how to fix this:

    • in _stackX pass has_constant="raise" to add_trend, and if it raises then tell the user to try passing trend="nc"
    • have add_trend return an updated k_trend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions