예제 #1
0
    def K_to_F(self, K):
        """
        Compute agent 1's best value-maximizing response F, given K.

        Parameters
        ----------
        K : array_like(float, ndim=2)
            A j x n array

        Returns
        -------
        F : array_like(float, ndim=2)
            The policy function for a given K
        P : array_like(float, ndim=2)
            The value function for a given K

        """
        A1 = self.A + dot(self.C, K)
        B1 = self.B
        Q1 = self.Q
        R1 = self.R - self.beta * self.theta * dot(K.T, K)
        lq = LQ(Q1, R1, A1, B1, beta=self.beta)
        P, F, d = lq.stationary_values()

        return F, P
예제 #2
0
    def F_to_K(self, F):
        """
        Compute agent 2's best cost-minimizing response K, given F.

        Parameters
        ----------
        F : array_like(float, ndim=2)
            A k x n array

        Returns
        -------
        K : array_like(float, ndim=2)
            Agent's best cost minimizing response for a given F
        P : array_like(float, ndim=2)
            The value function for a given F

        """
        Q2 = self.beta * self.theta
        R2 = - self.R - dot(F.T, dot(self.Q, F))
        A2 = self.A - dot(self.B, F)
        B2 = self.C
        lq = LQ(Q2, R2, A2, B2, beta=self.beta)
        P, neg_K, d = lq.stationary_values()

        return - neg_K, P
예제 #3
0
    def robust_rule(self):
        """
        This method solves the robust control problem by tricking it
        into a stacked LQ problem, as described in chapter 2 of Hansen-
        Sargent's text "Robustness."  The optimal control with observed
        state is

        .. math::

            u_t = - F x_t

        And the value function is -x'Px

        Returns
        -------
        F : array_like(float, ndim=2)
            The optimal control matrix from above
        P : array_like(float, ndim=2)
            The psoitive semi-definite matrix defining the value
            function
        K : array_like(float, ndim=2)
            the worst-case shock matrix K, where
            :math:`w_{t+1} = K x_t` is the worst case shock

        """
        # == Simplify names == #
        A, B, C, Q, R = self.A, self.B, self.C, self.Q, self.R
        beta, theta = self.beta, self.theta
        k, j = self.k, self.j
        # == Set up LQ version == #
        I = identity(j)
        Z = np.zeros((k, j))
        Ba = hstack([B, C])
        Qa = vstack([hstack([Q, Z]), hstack([Z.T, -beta*I*theta])])
        lq = LQ(Qa, R, A, Ba, beta=beta)

        # == Solve and convert back to robust problem == #
        P, f, d = lq.stationary_values()
        F = f[:k, :]
        K = -f[k:f.shape[0], :]

        return F, K, P
예제 #4
0
    def K_to_F(self, K):
        """
        Compute agent 1's best value-maximizing response F, given K.

        Parameters
        ==========
        K : array_like
            A self.j x self.n array

        Returns
        =======
        F : array_like, dtype = float
        P : array_like, dtype = float

        """
        A1 = self.A + dot(self.C, K)
        B1 = self.B
        Q1 = self.Q
        R1 = self.R - self.beta * self.theta * dot(K.T, K)
        lq = LQ(Q1, R1, A1, B1, beta=self.beta)
        P, F, d = lq.stationary_values()
        return F, P
예제 #5
0
    def F_to_K(self, F):
        """
        Compute agent 2's best cost-minimizing response K, given F.

        Parameters
        ==========
        F : array_like
            A self.k x self.n array

        Returns
        =======
        K : array_like, dtype = float
        P : array_like, dtype = float

        """
        Q2 = self.beta * self.theta
        R2 = - self.R - dot(F.T, dot(self.Q, F))
        A2 = self.A - dot(self.B, F)
        B2 = self.C
        lq = LQ(Q2, R2, A2, B2, beta=self.beta)
        P, neg_K, d = lq.stationary_values()
        return - neg_K, P
예제 #6
0
    def K_to_F(self, K):
        """
        Compute agent 1's best value-maximizing response F, given K.

        Parameters
        ==========
        K : array_like
            A self.j x self.n array

        Returns
        =======
        F : array_like, dtype = float
        P : array_like, dtype = float

        """
        A1 = self.A + dot(self.C, K)
        B1 = self.B
        Q1 = self.Q
        R1 = self.R - self.beta * self.theta * dot(K.T, K)
        lq = LQ(Q1, R1, A1, B1, beta=self.beta)
        P, F, d = lq.stationary_values()
        return F, P
예제 #7
0
    def F_to_K(self, F):
        """
        Compute agent 2's best cost-minimizing response K, given F.

        Parameters
        ==========
        F : array_like
            A self.k x self.n array

        Returns
        =======
        K : array_like, dtype = float
        P : array_like, dtype = float

        """
        Q2 = self.beta * self.theta
        R2 = -self.R - dot(F.T, dot(self.Q, F))
        A2 = self.A - dot(self.B, F)
        B2 = self.C
        lq = LQ(Q2, R2, A2, B2, beta=self.beta)
        P, neg_K, d = lq.stationary_values()
        return -neg_K, P
예제 #8
0
    def robust_rule(self):
        """
        This method solves the robust control problem by tricking it into a
        stacked LQ problem, as described in chapter 2 of Hansen-Sargent's
        text "Robustness."  The optimal control with observed state is

            u_t = - F x_t

        And the value function is -x'Px

        Returns
        =======
        F : array_like, dtype = float
            The optimal control matrix from above above

        P : array_like, dtype = float
            The psoitive semi-definite matrix defining the value function

        K : array_like, dtype = float
            the worst-case shock matrix K, where :math:`w_{t+1} = K x_t` is
            the worst case shock

        """
        # == Simplify names == #
        A, B, C, Q, R = self.A, self.B, self.C, self.Q, self.R
        beta, theta = self.beta, self.theta
        k, j = self.k, self.j
        # == Set up LQ version == #
        I = identity(j)
        Z = np.zeros((k, j))
        Ba = hstack([B, C])
        Qa = vstack([hstack([Q, Z]), hstack([Z.T, -beta * I * theta])])
        lq = LQ(Qa, R, A, Ba, beta=beta)
        # == Solve and convert back to robust problem == #
        P, f, d = lq.stationary_values()
        F = f[:k, :]
        K = -f[k:f.shape[0], :]
        return F, K, P
예제 #9
0
"""
Origin: QE by John Stachurski and Thomas J. Sargent
Filename: solution_ree_ex2.py
Authors: Chase Coleman, Spencer Lyon, Thomas Sargent, John Stachurski
Solves an exercise from the rational expectations module
"""
from __future__ import print_function
import numpy as np
from lqcontrol import LQ
from solution_ree_ex1 import beta, R, Q, B

candidates = ((94.0886298678, 0.923409232937), (93.2119845412, 0.984323478873),
              (95.0818452486, 0.952459076301))

for kappa0, kappa1 in candidates:

    # == Form the associated law of motion == #
    A = np.array([[1, 0, 0], [0, kappa1, kappa0], [0, 0, 1]])

    # == Solve the LQ problem for the firm == #
    lq = LQ(Q, R, A, B, beta=beta)
    P, F, d = lq.stationary_values()
    F = F.flatten()
    h0, h1, h2 = -F[2], 1 - F[0], -F[1]

    # == Test the equilibrium condition == #
    if np.allclose((kappa0, kappa1), (h0, h1 + h2)):
        print('Equilibrium pair =', kappa0, kappa1)
        print('(h0, h1, h2) = ', h0, h1, h2)
        break
예제 #10
0
    for theta in thetas:
        df.ix[theta] = evaluate_policy(theta, F)
        if df.ix[theta, 'entropy'] >= emax:
            break

    df = df.dropna(how='any')
    return df


#-----------------------------------------------------------------------------#
#                                    Main
#-----------------------------------------------------------------------------#


# == Compute the optimal rule == #
optimal_lq = LQ(Q, R, A, B, C, beta)
Po, Fo, do = optimal_lq.stationary_values()

# == Compute a robust rule given theta == #
baseline_robust = RBLQ(Q, R, A, B, C, beta, theta)
Fb, Kb, Pb = baseline_robust.robust_rule()

# == Check the positive definiteness of worst-case covariance matrix to == #
# == ensure that theta exceeds the breakdown point == #
test_matrix = np.identity(Pb.shape[0]) - np.dot(C.T, Pb.dot(C)) / theta
eigenvals, eigenvecs = eig(test_matrix)
assert (eigenvals >= 0).all(), 'theta below breakdown point.'


emax = 1.6e6
예제 #11
0
"""
from __future__ import print_function
import numpy as np
from lqcontrol import LQ
from solution_ree_ex1 import beta, R, Q, B

candidates = (
          (94.0886298678, 0.923409232937),
          (93.2119845412, 0.984323478873),
          (95.0818452486, 0.952459076301)
             )

for kappa0, kappa1 in candidates:

    # == Form the associated law of motion == #
    A = np.array([[1, 0, 0], [0, kappa1, kappa0], [0, 0, 1]])

    # == Solve the LQ problem for the firm == #
    lq = LQ(Q, R, A, B, beta=beta)
    P, F, d = lq.stationary_values()
    F = F.flatten()
    h0, h1, h2 = -F[2], 1 - F[0], -F[1]

    # == Test the equilibrium condition == #
    if np.allclose((kappa0, kappa1), (h0, h1 + h2)):
        print('Equilibrium pair =', kappa0, kappa1)
        print('(h0, h1, h2) = ', h0, h1, h2)
        break


예제 #12
0
theta_bar = 2.0

# == Useful constants == #
m0 = (a0 - c) / (2 * a1)
m1 = b / (2 * a1)

# == Formulate LQ problem == #
Q = gamma
R = [[a1, -a1, 0], [-a1, a1, 0], [0, 0, 0]]
A = [[0, 0, m0], [0, 1, 0], [0, 0, 1]]
B = [[0], [1], [0]]
C = [[m1], [0], [0]]

rlq = RBLQ(A, B, C, Q, R, beta, theta_bar)
lq = LQ(Q, R, A, B, beta=beta)

f, k, p = rlq.robust_rule()

print f
print rlq.K_to_F(k)

if 0:
    F_opt, K_opt, P_opt = rlq.robust_rule_simple()
    x0 = np.asarray((1, 0, 1)).reshape(3, 1)

    num_thetas = 20
    thetas = np.linspace(1, 5, num_thetas)
    vals = np.empty((2, num_thetas))
    ent = np.empty(num_thetas)