Exemple #1
0
    def compute_sequence(self, x0, ts_length=None):
        """ 
        This Function simulates x,u,w.
        It is based on the compute_sequence function from the LQ class, but is amended to recognise
        the fact that the A,B,C matrices can depend on the state of the world
        """
        x0 = np.asarray(x0)
        x0 = x0.reshape(self.nx, 1)

        T = ts_length if ts_length else 100

        chain = MarkovChain(self.Pi)
        state = chain.simulate_indices(ts_length=T + 1)

        x_path = np.empty((self.nx, T + 1))
        u_path = np.empty((self.nu, T))
        shocks = np.random.randn(self.nw, T + 1)
        w_path = np.empty((self.nx, T + 1))
        for i in range(T + 1):
            w_path[:, i] = self.C[state[i] + 1].dot(shocks[:, i])

        x_path[:, 0] = x0.flatten()
        u_path[:, 0] = -dot(self.F[state[0] + 1], x0).flatten()
        for t in range(1, T):
            Ax, Bu = dot(self.A[state[t] + 1],
                         x_path[:, t - 1]), dot(self.B[state[t] + 1],
                                                u_path[:, t - 1])
            x_path[:, t] = Ax + Bu + w_path[:, t]
            u_path[:, t] = -dot(self.F[state[t] + 1], x_path[:, t])
        Ax, Bu = dot(self.A[state[T] + 1],
                     x_path[:, T - 1]), dot(self.B[state[T] + 1],
                                            u_path[:, T - 1])
        x_path[:, T] = Ax + Bu + w_path[:, T]

        return x_path, u_path, w_path, state