#NEWLINE# def simulate( self , nsimulations , measurement_shocks = None , state_shocks = None , initial_state = None ) : #NEWLINE# #INDENT# '\n        Simulate a new time series following the state space model\n\n        Parameters\n        ----------\n        nsimulations : int\n            The number of observations to simulate. If the model is\n            time-invariant this can be any number. If the model is\n            time-varying, then this number must be less than or equal to the\n            number\n        measurement_shocks : array_like, optional\n            If specified, these are the shocks to the measurement equation,\n            :math:`\x0barepsilon_t`. If unspecified, these are automatically\n            generated using a pseudo-random number generator. If specified,\n            must be shaped `nsimulations` x `k_endog`, where `k_endog` is the\n            same as in the state space model.\n        state_shocks : array_like, optional\n            If specified, these are the shocks to the state equation,\n            :math:`\\eta_t`. If unspecified, these are automatically\n            generated using a pseudo-random number generator. If specified,\n            must be shaped `nsimulations` x `k_posdef` where `k_posdef` is the\n            same as in the state space model.\n        initial_state : array_like, optional\n            If specified, this is the state vector at time zero, which should\n            be shaped (`k_states` x 1), where `k_states` is the same as in the\n            state space model. If unspecified, but the model has been\n            initialized, then that initialization is used. If unspecified and\n            the model has not been initialized, then a vector of zeros is used.\n            Note that this is not included in the returned `simulated_states`\n            array.\n\n        Returns\n        -------\n        simulated_obs : array\n            An (nsimulations x k_endog) array of simulated observations.\n        simulated_states : array\n            An (nsimulations x k_states) array of simulated states.\n        ' #NEWLINE# time_invariant = self . time_invariant #NEWLINE# if ( ( not time_invariant ) and ( nsimulations > self . nobs ) ) : #NEWLINE# #INDENT# raise ValueError ( 'In a time-varying model, cannot create more simulations than there are observations.' ) #NEWLINE# #UNINDENT# if ( measurement_shocks is not None ) : #NEWLINE# #INDENT# measurement_shocks = np . array ( measurement_shocks ) #NEWLINE# if ( measurement_shocks . ndim == 0 ) : #NEWLINE# #INDENT# measurement_shocks = measurement_shocks [ ( np . newaxis , np . newaxis ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( measurement_shocks . ndim == 1 ) : #NEWLINE# #INDENT# measurement_shocks = measurement_shocks [ : , np . newaxis ] #NEWLINE# #UNINDENT# #UNINDENT# if ( not ( measurement_shocks . shape == ( nsimulations , self . k_endog ) ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Invalid shape of provided measurement shocks. Required (%d, %d)' % ( nsimulations , self . k_endog ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . shapes [ 'obs_cov' ] [ ( - 1 ) ] == 1 ) : #NEWLINE# #INDENT# measurement_shocks = np . random . multivariate_normal ( mean = np . zeros ( self . k_endog ) , cov = self [ 'obs_cov' ] , size = nsimulations ) #NEWLINE# #UNINDENT# #UNINDENT# if ( state_shocks is not None ) : #NEWLINE# #INDENT# state_shocks = np . array ( state_shocks ) #NEWLINE# if ( state_shocks . ndim == 0 ) : #NEWLINE# #INDENT# state_shocks = state_shocks [ ( np . newaxis , np . newaxis ) ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( state_shocks . ndim == 1 ) : #NEWLINE# #INDENT# state_shocks = state_shocks [ : , np . newaxis ] #NEWLINE# #UNINDENT# #UNINDENT# if ( not ( state_shocks . shape == ( nsimulations , self . k_posdef ) ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Invalid shape of provided state shocks. Required (%d, %d).' % ( nsimulations , self . k_posdef ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . shapes [ 'state_cov' ] [ ( - 1 ) ] == 1 ) : #NEWLINE# #INDENT# state_shocks = np . random . multivariate_normal ( mean = np . zeros ( self . k_posdef ) , cov = self [ 'state_cov' ] , size = nsimulations ) #NEWLINE# #UNINDENT# #UNINDENT# if ( initial_state is not None ) : #NEWLINE# #INDENT# initial_state = np . array ( initial_state ) #NEWLINE# if ( initial_state . ndim == 0 ) : #NEWLINE# #INDENT# initial_state = initial_state [ np . newaxis ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( initial_state . ndim > 1 ) and ( not ( initial_state . shape == ( self . k_states , 1 ) ) ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Invalid shape of provided initial state vector. Required (%d, 1)' % self . k_states ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . initialization == 'known' ) : #NEWLINE# #INDENT# initial_state = self . _initial_state #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( self . initialization in [ 'approximate_diffuse' , 'stationary' ] ) : #NEWLINE# #INDENT# initial_state = np . zeros ( self . k_states ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# initial_state = np . zeros ( self . k_states ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return self . _simulate ( nsimulations , measurement_shocks , state_shocks , initial_state )