Prediction for time-series data gives constant values I tried generating an AR process and checked whether it is predictable. After generating 5000 values of AR process, I put the first 4000 values as in-sample in statsmodels.tsa.ar_model.ar_select_order() and predict the rest of 1000 values.
Although it predicts in-sample accurately, out-of-sample prediction gets constant and considerably different from the true values.
I wonder why it happened and could someone me give any advice?

My code is as follows.
import numpy as np
import statsmodels as sm
import matplotlib.pyplot as plt
import statsmodels as sm
import statsmodels.api as smp
%matplotlib inline


#----- generate AR process sample
np.random.seed(12345)
arparams = np.array([0.9])
maparams = np.array([0])
ar = np.r_[1, -arparams] # add zero-lag and negate
ma = np.r_[1, maparams] # add zero-lag
y = smp.tsa.arma_generate_sample(ar, ma, 5000)

#----- fit in-sample and predict out-of-sample
estimated_model = sm.tsa.ar_model.ar_select_order(y[:4000], maxlag=5, ic='aic', glob=True)
res = estimated_model.model.fit()
print(res.summary()) # checked if my AR process is correct

fig, ax = plt.subplots(1, 1, sharex=False)
ax.set_title("Pred")
ax.plot(y, label='truth')
ax.plot(res.predict(0,5000,), label='pred')


This is normal behavior for any ARIMA model. These models quickly converge to flat lines (if there is no differencing), or to the appropriate linear trend (if the order of differencing is 1), or to higher trends (for higher order differencing, which should rarely be used).
Note that the initial forecasts are not flat, and it only looks flat because you are forecasting a long way out:
res.predict(0,10,)
array([        nan,         nan,         nan, -0.19471936, -0.6925775 ,
        1.06673156,  2.27563109,  2.14843756,  2.14919931,  2.6125645 ,
        3.46204872])

You have a very simple AR(3) model with a nonzero intercept and should be able to calculate the forecasts by hand.
We have many earlier posts on flat ARIMA forecasts, take a look at those. As to why your predictions vary much less than your history, the two links here should be helpful.
