def simulate(self, nsimulations, measurement_shocks=None,
                 state_shocks=None, initial_state=None):
        time_invariant = self.time_invariant
        # Check for valid number of simulations
        if not time_invariant and nsimulations > self.nobs:
            raise ValueError('In a time-varying model, cannot create more'
                             ' simulations than there are observations.')

        # Check / generate measurement shocks
        if measurement_shocks is not None:
            measurement_shocks = np.array(measurement_shocks)
            if measurement_shocks.ndim == 0:
                measurement_shocks = measurement_shocks[np.newaxis, np.newaxis]
            elif measurement_shocks.ndim == 1:
                measurement_shocks = measurement_shocks[:, np.newaxis]
            if not measurement_shocks.shape == (nsimulations, self.k_endog):
                raise ValueError('Invalid shape of provided measurement shocks.'
                                 ' Required (%d, %d)'
                                 % (nsimulations, self.k_endog))
        elif self.shapes['obs_cov'][-1] == 1:
            measurement_shocks = np.random.multivariate_normal(
                mean=np.zeros(self.k_endog), cov=self['obs_cov'],
                size=nsimulations)

        # Check / generate state shocks
        if state_shocks is not None:
            state_shocks = np.array(state_shocks)
            if state_shocks.ndim == 0:
                state_shocks = state_shocks[np.newaxis, np.newaxis]
            elif state_shocks.ndim == 1:
                state_shocks = state_shocks[:, np.newaxis]
            if not state_shocks.shape == (nsimulations, self.k_posdef):
                raise ValueError('Invalid shape of provided state shocks.'
                                 ' Required (%d, %d).'
                                 % (nsimulations, self.k_posdef))
        elif self.shapes['state_cov'][-1] == 1:
            state_shocks = np.random.multivariate_normal(
                mean=np.zeros(self.k_posdef), cov=self['state_cov'],
                size=nsimulations)

        # Get the initial states
        if initial_state is not None:
            initial_state = np.array(initial_state)
            if initial_state.ndim == 0:
                initial_state = initial_state[np.newaxis]
            elif (initial_state.ndim > 1 and
                  not initial_state.shape == (self.k_states, 1)):
                raise ValueError('Invalid shape of provided initial state'
                                 ' vector. Required (%d, 1)' % self.k_states)
        elif self.initialization == 'known':
            initial_state = self._initial_state
        elif self.initialization in ['approximate_diffuse', 'stationary']:
            initial_state = np.zeros(self.k_states)
        else:
            initial_state = np.zeros(self.k_states)

        return self._simulate(nsimulations, measurement_shocks, state_shocks,
                              initial_state)
Simulate a new time series following the state space model
dataset/ETHPy150Open statsmodels/statsmodels/statsmodels/tsa/statespace/kalman_filter.py/KalmanFilter.simulate