def predict(self, h=5, intervals=False, oos_data=None, **kwargs):        
        """ Makes forecast with the estimated model

        Parameters
        ----------
        h : int (default : 5)
            How many steps ahead would you like to forecast?

        intervals : boolean (default: False)
            Whether to return prediction intervals

        oos_data : pd.DataFrame
            Data for the variables to be used out of sample (ys can be NaNs)

        Returns
        ----------
        - pd.DataFrame with predictions
        """     

        nsims = kwargs.get('nsims', 200)

        if self.latent_variables.estimated is False:
            raise Exception("No latent variables estimated!")
        else:

            _, X_oos = dmatrices(self.formula, oos_data)
            X_oos = np.array([X_oos])[0]
            full_X = self.X.copy()
            full_X = np.append(full_X,X_oos,axis=0)
            Z = full_X
            date_index = self.shift_dates(h)

            # Retrieve data, dates and (transformed) latent variables   
            if self.latent_variables.estimation_method in ['M-H']:
                lower_1_final = 0
                upper_99_final = 0
                lower_5_final = 0
                upper_95_final = 0
                forecasted_values_final = 0

                for i in range(nsims):
                    t_params = self.draw_latent_variables(nsims=1).T[0]
                    a, P = self._forecast_model(t_params, Z, h)

                    smoothed_series = np.zeros(h)
                    series_variance = np.zeros(h)

                    for t in range(h):
                        smoothed_series[t] = np.dot(Z[self.y.shape[0]+t],a[:,self.y.shape[0]+t])
                        series_variance[t] = np.dot(np.dot(Z[self.y.shape[0]+t],P[:,:,self.y.shape[0]+t]),Z[self.y.shape[0]+t].T)

                    forecasted_values = smoothed_series
                    lower_5 = smoothed_series - 1.96*np.power(P[0][0][-h:] + self.latent_variables.z_list[0].prior.transform(t_params[0]),0.5)
                    upper_95 = smoothed_series + 1.96*np.power(P[0][0][-h:] + self.latent_variables.z_list[0].prior.transform(t_params[0]),0.5)
                    lower_5_final += lower_5
                    upper_95_final += upper_95
                    lower_1 = smoothed_series - 2.575*np.power(P[0][0][-h:] + self.latent_variables.z_list[0].prior.transform(t_params[0]),0.5)
                    upper_99 = smoothed_series + 2.575*np.power(P[0][0][-h:] + self.latent_variables.z_list[0].prior.transform(t_params[0]),0.5)
                    lower_1_final += lower_1
                    upper_99_final += upper_99
                    forecasted_values_final += forecasted_values

                forecasted_values_final = forecasted_values_final / nsims
                lower_1_final = lower_1_final / nsims
                lower_5_final = lower_5_final / nsims
                upper_95_final = upper_95_final / nsims
                upper_99_final = upper_99_final / nsims

                if intervals is False:
                    result = pd.DataFrame(forecasted_values_final)
                    result.rename(columns={0:self.data_name}, inplace=True)
                else:
                    prediction_05 = lower_5_final
                    prediction_95 = upper_95_final
                    prediction_01 = lower_1_final
                    prediction_99 = upper_99_final

                    result = pd.DataFrame([forecasted_values_final, prediction_01, prediction_05, 
                        prediction_95, prediction_99]).T
                    result.rename(columns={0:self.data_name, 1: "1% Prediction Interval", 
                        2: "5% Prediction Interval", 3: "95% Prediction Interval", 4: "99% Prediction Interval"}, 
                        inplace=True)

                result.index = date_index[-h:]

                return result
     
            else:
                t_params = self.latent_variables.get_z_values()
                a, P = self._forecast_model(t_params, Z, h)
                smoothed_series = np.zeros(h)

                for t in range(h):
                    smoothed_series[t] = np.dot(Z[self.y.shape[0]+t],a[:,self.y.shape[0]+t])

                # Retrieve data, dates and (transformed) latent variables         
                forecasted_values = smoothed_series

                if intervals is False:
                    result = pd.DataFrame(forecasted_values)
                    result.rename(columns={0:self.data_name}, inplace=True)
                else:

                    series_variance = np.zeros(h)

                    for t in range(h):
                        series_variance[t] = np.dot(np.dot(Z[self.y.shape[0]+t],P[:,:,self.y.shape[0]+t]),Z[self.y.shape[0]+t].T)

                    prediction_05 = forecasted_values - 1.96*np.power(P[0][0][-h:] + self.latent_variables.z_list[0].prior.transform(self.latent_variables.get_z_values()[0]),0.5)
                    prediction_95 = forecasted_values + 1.96*np.power(P[0][0][-h:] + self.latent_variables.z_list[0].prior.transform(self.latent_variables.get_z_values()[0]),0.5)
                    prediction_01 = forecasted_values - 2.575*np.power(P[0][0][-h:] + self.latent_variables.z_list[0].prior.transform(self.latent_variables.get_z_values()[0]),0.5)
                    prediction_99 = forecasted_values + 2.575*np.power(P[0][0][-h:] + self.latent_variables.z_list[0].prior.transform(self.latent_variables.get_z_values()[0]),0.5)

                    result = pd.DataFrame([forecasted_values, prediction_01, prediction_05, 
                        prediction_95, prediction_99]).T
                    result.rename(columns={0:self.data_name, 1: "1% Prediction Interval", 
                        2: "5% Prediction Interval", 3: "95% Prediction Interval", 4: "99% Prediction Interval"}, 
                        inplace=True)
     
                result.index = date_index[-h:]

                return result
def predict ( self , h = 5 , intervals = False , oos_data = None , ** kwargs ) : nsims = kwargs . get ( 'nsims' , 200 ) if self . latent_variables . estimated is False : raise Exception ( "No latent variables estimated!" ) else : _ , X_oos = dmatrices ( self . formula , oos_data ) X_oos = np . array ( [ X_oos ] ) [ 0 ] full_X = self . X . copy ( ) full_X = np . append ( full_X , X_oos , axis = 0 ) Z = full_X date_index = self . shift_dates ( h ) if self . latent_variables . estimation_method in [ 'M-H' ] : lower_1_final = 0 upper_99_final = 0 lower_5_final = 0 upper_95_final = 0 forecasted_values_final = 0 for i in range ( nsims ) : t_params = self . draw_latent_variables ( nsims = 1 ) . T [ 0 ] a , P = self . _forecast_model ( t_params , Z , h ) smoothed_series = np . zeros ( h ) series_variance = np . zeros ( h ) for t in range ( h ) : smoothed_series [ t ] = np . dot ( Z [ self . y . shape [ 0 ] + t ] , a [ : , self . y . shape [ 0 ] + t ] ) series_variance [ t ] = np . dot ( np . dot ( Z [ self . y . shape [ 0 ] + t ] , P [ : , : , self . y . shape [ 0 ] + t ] ) , Z [ self . y . shape [ 0 ] + t ] . T ) forecasted_values = smoothed_series lower_5 = smoothed_series - 1.96 * np . power ( P [ 0 ] [ 0 ] [ - h : ] + self . latent_variables . z_list [ 0 ] . prior . transform ( t_params [ 0 ] ) , 0.5 ) upper_95 = smoothed_series + 1.96 * np . power ( P [ 0 ] [ 0 ] [ - h : ] + self . latent_variables . z_list [ 0 ] . prior . transform ( t_params [ 0 ] ) , 0.5 ) lower_5_final += lower_5 upper_95_final += upper_95 lower_1 = smoothed_series - 2.575 * np . power ( P [ 0 ] [ 0 ] [ - h : ] + self . latent_variables . z_list [ 0 ] . prior . transform ( t_params [ 0 ] ) , 0.5 ) upper_99 = smoothed_series + 2.575 * np . power ( P [ 0 ] [ 0 ] [ - h : ] + self . latent_variables . z_list [ 0 ] . prior . transform ( t_params [ 0 ] ) , 0.5 ) lower_1_final += lower_1 upper_99_final += upper_99 forecasted_values_final += forecasted_values forecasted_values_final = forecasted_values_final / nsims lower_1_final = lower_1_final / nsims lower_5_final = lower_5_final / nsims upper_95_final = upper_95_final / nsims upper_99_final = upper_99_final / nsims if intervals is False : result = pd . DataFrame ( forecasted_values_final ) result . rename ( columns = { 0 : self . data_name } , inplace = True ) else : prediction_05 = lower_5_final prediction_95 = upper_95_final prediction_01 = lower_1_final prediction_99 = upper_99_final result = pd . DataFrame ( [ forecasted_values_final , prediction_01 , prediction_05 , prediction_95 , prediction_99 ] ) . T result . rename ( columns = { 0 : self . data_name , 1 : "1% Prediction Interval" , 2 : "5% Prediction Interval" , 3 : "95% Prediction Interval" , 4 : "99% Prediction Interval" } , inplace = True ) result . index = date_index [ - h : ] return result else : t_params = self . latent_variables . get_z_values ( ) a , P = self . _forecast_model ( t_params , Z , h ) smoothed_series = np . zeros ( h ) for t in range ( h ) : smoothed_series [ t ] = np . dot ( Z [ self . y . shape [ 0 ] + t ] , a [ : , self . y . shape [ 0 ] + t ] ) forecasted_values = smoothed_series if intervals is False : result = pd . DataFrame ( forecasted_values ) result . rename ( columns = { 0 : self . data_name } , inplace = True ) else : series_variance = np . zeros ( h ) for t in range ( h ) : series_variance [ t ] = np . dot ( np . dot ( Z [ self . y . shape [ 0 ] + t ] , P [ : , : , self . y . shape [ 0 ] + t ] ) , Z [ self . y . shape [ 0 ] + t ] . T ) prediction_05 = forecasted_values - 1.96 * np . power ( P [ 0 ] [ 0 ] [ - h : ] + self . latent_variables . z_list [ 0 ] . prior . transform ( self . latent_variables . get_z_values ( ) [ 0 ] ) , 0.5 ) prediction_95 = forecasted_values + 1.96 * np . power ( P [ 0 ] [ 0 ] [ - h : ] + self . latent_variables . z_list [ 0 ] . prior . transform ( self . latent_variables . get_z_values ( ) [ 0 ] ) , 0.5 ) prediction_01 = forecasted_values - 2.575 * np . power ( P [ 0 ] [ 0 ] [ - h : ] + self . latent_variables . z_list [ 0 ] . prior . transform ( self . latent_variables . get_z_values ( ) [ 0 ] ) , 0.5 ) prediction_99 = forecasted_values + 2.575 * np . power ( P [ 0 ] [ 0 ] [ - h : ] + self . latent_variables . z_list [ 0 ] . prior . transform ( self . latent_variables . get_z_values ( ) [ 0 ] ) , 0.5 ) result = pd . DataFrame ( [ forecasted_values , prediction_01 , prediction_05 , prediction_95 , prediction_99 ] ) . T result . rename ( columns = { 0 : self . data_name , 1 : "1% Prediction Interval" , 2 : "5% Prediction Interval" , 3 : "95% Prediction Interval" , 4 : "99% Prediction Interval" } , inplace = True ) result . index = date_index [ - h : ] return result
Makes forecast with the estimated model

        Parameters
        ----------
        h : int (default : 5)
            How many steps ahead would you like to forecast?

        intervals : boolean (default: False)
            Whether to return prediction intervals

        oos_data : pd.DataFrame
            Data for the variables to be used out of sample (ys can be NaNs)

        Returns
        ----------
        - pd.DataFrame with predictions