#NEWLINE# def impulse_responses( self , steps = 10 , impulse = 0 , orthogonalized = False , cumulative = False , **kwargs ) : #NEWLINE# #INDENT# '\n        Impulse response function\n\n        Parameters\n        ----------\n        steps : int, optional\n            The number of steps for which impulse responses are calculated.\n            Default is 10. Note that the initial impulse is not counted as a\n            step, so if `steps=1`, the output will have 2 entries.\n        impulse : int or array_like\n            If an integer, the state innovation to pulse; must be between 0\n            and `k_posdef-1` where `k_posdef` is the same as in the state\n            space model. Alternatively, a custom impulse vector may be\n            provided; must be a column vector with shape `(k_posdef, 1)`.\n        orthogonalized : boolean, optional\n            Whether or not to perform impulse using orthogonalized innovations.\n            Note that this will also affect custum `impulse` vectors. Default\n            is False.\n        cumulative : boolean, optional\n            Whether or not to return cumulative impulse responses. Default is\n            False.\n        **kwargs\n            If the model is time-varying and `steps` is greater than the number\n            of observations, any of the state space representation matrices\n            that are time-varying must have updated values provided for the\n            out-of-sample steps.\n            For example, if `design` is a time-varying component, `nobs` is 10,\n            and `steps` is 15, a (`k_endog` x `k_states` x 5) matrix must be\n            provided with the new design matrix values.\n\n        Returns\n        -------\n        impulse_responses : array\n            Responses for each endogenous variable due to the impulse\n            given by the `impulse` argument. A (steps + 1 x k_endog) array.\n\n        Notes\n        -----\n        Intercepts in the measurement and state equation are ignored when\n        calculating impulse responses.\n\n        ' #NEWLINE# steps += 1 #NEWLINE# if ( type ( impulse ) == int ) : #NEWLINE# #INDENT# if ( ( impulse >= self . k_posdef ) or ( impulse < 0 ) ) : #NEWLINE# #INDENT# raise ValueError ( 'Invalid value for `impulse`. Must be the of one of the state innovations.' ) #NEWLINE# #UNINDENT# idx = impulse #NEWLINE# impulse = np . zeros ( self . k_posdef ) #NEWLINE# impulse [ idx ] = 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# impulse = np . array ( impulse ) #NEWLINE# if ( impulse . ndim > 1 ) : #NEWLINE# #INDENT# impulse = np . squeeze ( impulse ) #NEWLINE# #UNINDENT# if ( not ( impulse . shape == ( self . k_posdef , ) ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Invalid impulse vector. Must be shaped (%d,)' % self . k_posdef ) ) #NEWLINE# #UNINDENT# #UNINDENT# if orthogonalized : #NEWLINE# #INDENT# state_chol = np . linalg . cholesky ( self . state_cov [ : , : , 0 ] ) #NEWLINE# impulse = np . dot ( state_chol , impulse ) #NEWLINE# #UNINDENT# if self . time_invariant : #NEWLINE# #INDENT# design = self . design [ : , : , 0 ] #NEWLINE# transition = self . transition [ : , : , 0 ] #NEWLINE# selection = self . selection [ : , : , 0 ] #NEWLINE# irf = np . zeros ( ( steps , self . k_endog ) , dtype = self . dtype ) #NEWLINE# states = np . zeros ( ( steps , self . k_states ) , dtype = self . dtype ) #NEWLINE# states [ 0 ] = np . dot ( selection , impulse ) #NEWLINE# irf [ 0 ] = np . dot ( design , states [ 0 ] ) #NEWLINE# for t in range ( 1 , steps ) : #NEWLINE# #INDENT# states [ t ] = np . dot ( transition , states [ ( t - 1 ) ] ) #NEWLINE# irf [ t ] = np . dot ( design , states [ t ] ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# representation = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( name , shape ) in self . shapes . items ( ) : #NEWLINE# #INDENT# if ( name in [ 'obs' , 'obs_intercept' , 'state_intercept' ] ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# representation [ name ] = getattr ( self , name ) #NEWLINE# #UNINDENT# warning = 'Model has time-invariant %s matrix, so the %s argument to `irf` has been ignored.' #NEWLINE# exception = 'Impulse response functions for models with time-varying %s matrix requires an updated time-varying matrix for any periods beyond those in the original model.' #NEWLINE# for ( name , shape ) in self . shapes . items ( ) : #NEWLINE# #INDENT# if ( name in [ 'obs' , 'obs_intercept' , 'state_intercept' ] ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( representation [ name ] . shape [ ( - 1 ) ] == 1 ) : #NEWLINE# #INDENT# if ( name in kwargs ) : #NEWLINE# #INDENT# warn ( ( warning % ( name , name ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( name not in kwargs ) : #NEWLINE# #INDENT# raise ValueError ( ( exception % name ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# mat = np . asarray ( kwargs [ name ] ) #NEWLINE# validate_matrix_shape ( name , mat . shape , shape [ 0 ] , shape [ 1 ] , nforecast ) #NEWLINE# if ( ( mat . ndim < 3 ) or ( not ( mat . shape [ 2 ] == nforecast ) ) ) : #NEWLINE# #INDENT# raise ValueError ( ( exception % name ) ) #NEWLINE# #UNINDENT# representation [ name ] = np . c_ [ ( representation [ name ] , mat ) ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# model_kwargs = { #NEWLINE# #INDENT# 'filter_method' : self . filter_method , #NEWLINE# 'inversion_method' : self . inversion_method , #NEWLINE# 'stability_method' : self . stability_method , #NEWLINE# 'conserve_memory' : self . conserve_memory , #NEWLINE# 'tolerance' : self . tolerance , #NEWLINE# 'loglikelihood_burn' : self . loglikelihood_burn , #NEWLINE# #UNINDENT# } #NEWLINE# model_kwargs . update ( representation ) #NEWLINE# model = KalmanFilter ( np . zeros ( self . endog . T . shape ) , self . k_states , self . k_posdef , ** model_kwargs ) #NEWLINE# model . initialize_approximate_diffuse ( ) #NEWLINE# model . _initialize_filter ( ) #NEWLINE# model . _initialize_state ( ) #NEWLINE# measurement_shocks = np . zeros ( ( steps , self . k_endog ) ) #NEWLINE# state_shocks = np . zeros ( ( steps , self . k_posdef ) ) #NEWLINE# state_shocks [ 0 ] = impulse #NEWLINE# ( irf , _ ) = model . simulate ( steps , measurement_shocks = measurement_shocks , state_shocks = state_shocks ) #NEWLINE# #UNINDENT# if cumulative : #NEWLINE# #INDENT# irf = np . cumsum ( irf , axis = 0 ) #NEWLINE# #UNINDENT# return irf