#NEWLINE# def plot_components( self , which = None , alpha = 0.05 , observed = True , level = True , trend = True , seasonal = True , cycle = True , autoregressive = True , legend_loc = 'upper right' , fig = None , figsize = None ) : #NEWLINE# #INDENT# "\n        Plot the estimated components of the model.\n\n        Parameters\n        ----------\n        which : {'filtered', 'smoothed'}, or None, optional\n            Type of state estimate to plot. Default is 'smoothed' if smoothed\n            results are available otherwise 'filtered'.\n        alpha : float, optional\n            The confidence intervals for the components are (1 - alpha) %\n        level : boolean, optional\n            Whether or not to plot the level component, if applicable.\n            Default is True.\n        trend : boolean, optional\n            Whether or not to plot the trend component, if applicable.\n            Default is True.\n        seasonal : boolean, optional\n            Whether or not to plot the seasonal component, if applicable.\n            Default is True.\n        cycle : boolean, optional\n            Whether or not to plot the cyclical component, if applicable.\n            Default is True.\n        autoregressive : boolean, optional\n            Whether or not to plot the autoregressive state, if applicable.\n            Default is True.\n        fig : Matplotlib Figure instance, optional\n            If given, subplots are created in this figure instead of in a new\n            figure. Note that the grid will be created in the provided\n            figure using `fig.add_subplot()`.\n        figsize : tuple, optional\n            If a figure is created, this argument allows specifying a size.\n            The tuple is (width, height).\n\n        Notes\n        -----\n        If all options are included in the model and selected, this produces\n        a 6x1 plot grid with the following plots (ordered top-to-bottom):\n\n        0. Observed series against predicted series\n        1. Level\n        2. Trend\n        3. Seasonal\n        4. Cycle\n        5. Autoregressive\n\n        Specific subplots will be removed if the component is not present in\n        the estimated model or if the corresponding keywork argument is set to\n        False.\n\n        All plots contain (1 - `alpha`) %  confidence intervals.\n        " #NEWLINE# from scipy.stats import norm #NEWLINE# from statsmodels.graphics.utils import _import_mpl , create_mpl_fig #NEWLINE# plt = _import_mpl ( ) #NEWLINE# fig = create_mpl_fig ( fig , figsize ) #NEWLINE# if ( which is None ) : #NEWLINE# #INDENT# which = ( 'filtered' if ( self . smoothed_state is None ) else 'smoothed' ) #NEWLINE# #UNINDENT# spec = self . specification #NEWLINE# components = OrderedDict ( [ ( 'level' , ( level and spec . level ) ) , ( 'trend' , ( trend and spec . trend ) ) , ( 'seasonal' , ( seasonal and spec . seasonal ) ) , ( 'cycle' , ( cycle and spec . cycle ) ) , ( 'autoregressive' , ( autoregressive and spec . autoregressive ) ) ] ) #NEWLINE# llb = self . filter_results . loglikelihood_burn #NEWLINE# k_plots = ( observed + np . sum ( list ( components . values ( ) ) ) ) #NEWLINE# if ( hasattr ( self . data , 'dates' ) and ( self . data . dates is not None ) ) : #NEWLINE# #INDENT# dates = self . data . dates . _mpl_repr ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# dates = np . arange ( len ( self . data . endog ) ) #NEWLINE# #UNINDENT# critical_value = norm . ppf ( ( 1 - ( alpha / 2.0 ) ) ) #NEWLINE# plot_idx = 1 #NEWLINE# if observed : #NEWLINE# #INDENT# ax = fig . add_subplot ( k_plots , 1 , plot_idx ) #NEWLINE# plot_idx += 1 #NEWLINE# ax . plot ( dates [ llb : ] , self . model . endog [ llb : ] , color = 'k' , label = 'Observed' ) #NEWLINE# predict = self . filter_results . forecasts [ 0 ] #NEWLINE# std_errors = np . sqrt ( self . filter_results . forecasts_error_cov [ ( 0 , 0 ) ] ) #NEWLINE# ci_lower = ( predict - ( critical_value * std_errors ) ) #NEWLINE# ci_upper = ( predict + ( critical_value * std_errors ) ) #NEWLINE# ax . plot ( dates [ llb : ] , predict [ llb : ] , label = 'One-step-ahead predictions' ) #NEWLINE# ci_poly = ax . fill_between ( dates [ llb : ] , ci_lower [ llb : ] , ci_upper [ llb : ] , alpha = 0.2 ) #NEWLINE# ci_label = ( '$%.3g \\%%$ confidence interval' % ( ( 1 - alpha ) * 100 ) ) #NEWLINE# p = plt . Rectangle ( ( 0 , 0 ) , 1 , 1 , fc = ci_poly . get_facecolor ( ) [ 0 ] ) #NEWLINE# ( handles , labels ) = ax . get_legend_handles_labels ( ) #NEWLINE# handles . append ( p ) #NEWLINE# labels . append ( ci_label ) #NEWLINE# ax . legend ( handles , labels , loc = legend_loc ) #NEWLINE# ax . set_title ( 'Predicted vs observed' ) #NEWLINE# #UNINDENT# for ( component , is_plotted ) in components . items ( ) : #NEWLINE# #INDENT# if ( not is_plotted ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# ax = fig . add_subplot ( k_plots , 1 , plot_idx ) #NEWLINE# plot_idx += 1 #NEWLINE# component_bunch = getattr ( self , component ) #NEWLINE# if ( which not in component_bunch ) : #NEWLINE# #INDENT# raise ValueError ( 'Invalid type of state estimate.' ) #NEWLINE# #UNINDENT# which_cov = ( '%s_cov' % which ) #NEWLINE# value = component_bunch [ which ] #NEWLINE# state_label = ( '%s (%s)' % ( component . title ( ) , which ) ) #NEWLINE# ax . plot ( dates [ llb : ] , value [ llb : ] , label = state_label ) #NEWLINE# if ( which_cov in component_bunch ) : #NEWLINE# #INDENT# std_errors = np . sqrt ( component_bunch [ ( '%s_cov' % which ) ] ) #NEWLINE# ci_lower = ( value - ( critical_value * std_errors ) ) #NEWLINE# ci_upper = ( value + ( critical_value * std_errors ) ) #NEWLINE# ci_poly = ax . fill_between ( dates [ llb : ] , ci_lower [ llb : ] , ci_upper [ llb : ] , alpha = 0.2 ) #NEWLINE# ci_label = ( '$%.3g \\%%$ confidence interval' % ( ( 1 - alpha ) * 100 ) ) #NEWLINE# #UNINDENT# ax . legend ( loc = legend_loc ) #NEWLINE# ax . set_title ( ( '%s component' % component . title ( ) ) ) #NEWLINE# #UNINDENT# if ( llb > 0 ) : #NEWLINE# #INDENT# text = 'Note: The first %d observations are not shown, due to approximate diffuse initialization.' #NEWLINE# fig . text ( 0.1 , 0.01 , ( text % llb ) , fontsize = 'large' ) #NEWLINE# #UNINDENT# return fig