Python Forum
Confidence intervals over time with polynomial regression - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Confidence intervals over time with polynomial regression (/thread-18415.html)



Confidence intervals over time with polynomial regression - jjameson - May-16-2019

I am having trouble replicating 95% confidence bands for a time-series polynomial regression model.

I have found the code below, however the resulting confidence bands do not become greater the further out the prediction is into the future - which is what I require. Am I missing something? I appreciate that an ARIMA model would be preferable for time-series forecasts, however I am stuck with replicating a polynomial regression for this task.

Thanks in advance,

    ## instantiate regression model.
    model = LinearRegression()
    ## train model on polynomial x data and y data.
    model.fit(x, y)

    y_poly_pred = model.predict(x)

    ## reshape predictions
    predictions = predictions.values.reshape((n_value,1))

    ## RMSE & R2
    rmse = np.sqrt(mean_squared_error(y, y_poly_pred_test))
    r2 = r2_score(y, y_poly_pred)
    ## calculate t_value
    t = stats.t.ppf(1-tail_value,n_value)
    ## predict y values of original data using the fit model.
    p_y = y_poly_pred_test

    # calculate the y-error (residuals)
    y_err = y - y_poly_pred

    # calculate confidence intervals for new test x-series
    mean_x = np.mean(x)
    n = len(x)
    s_err = np.sum(np.power(y_err,2))   # sum of the squares of the residuals
    confs = t * np.sqrt((s_err/(n-2))*(1.0/n + (np.power((predictions-mean_x),2)/
            ((np.sum(np.power(x,2)))-n*(np.power(mean_x,2))))))

    # get lower and upper confidence limits based on predicted y and confidence intervals
    lower = y_poly_pred - abs(confs)
    upper = y_poly_pred + abs(confs)