Example #1
0
    def compute_dEV_scalar(self, istate, kp):
        # Unpack parameters
        A, alpha, beta, delta, gamma, rho, sigma = self._unpack_params()

        # All possible exogenous states tomorrow
        z1 = self.ncgm.z1[istate, :]
        phi = complete_polynomial_der(
            np.vstack([np.ones(self.ncgm.Qn) * kp, z1]), self.degree, 0).T
        val = self.ncgm.weights @ (phi @ self.v_coeffs)

        return val
def test_complete_vec_vs_mat():
    # Matrix for allocation
    temp = np.ones(n_complete(2, 3)) * 5.0
    temp_mat = np.ones((n_complete(2, 3), 3))

    # Point at which to evaluate
    z = np.array([0.9, 1.05])
    z_mat = np.array([[0.9, 0.95, 1.0], [1.05, 1.0, 0.95]])

    foo = complete_polynomial(z, 2)
    bar = complete_polynomial(z_mat, 2)[:, 0]
    assert np.allclose(foo, bar)

    foo = complete_polynomial_der(z, 2, 0)
    bar = complete_polynomial_der(z_mat, 2, 0)[:, 0]
    assert np.allclose(foo, bar)

    foo = complete_polynomial_der(z, 4, 0)
    bar = complete_polynomial_der(z_mat, 4, 0)[:, 0]
    assert np.allclose(foo, bar)
def test_complete_derivative():

    # Test derivative vector
    z = np.array([1, 2, 3])
    sol_vec = np.array([0.0, 1.0, 0.0, 0.0, 2.0, 2.0, 3.0, 0.0, 0.0, 0.0])
    out_vec = np.empty(n_complete(3, 2))
    _complete_poly_der_impl_vec(z, 2, 0, out_vec)
    assert (abs(out_vec - sol_vec).max() < 1e-10)

    # Test derivative matrix
    z = np.arange(1, 7).reshape(3, 2)
    out_mat = complete_polynomial_der(z, 2, 1)
    assert (abs(out_mat[0, :]).max() < 1e-10)
    assert (abs(out_mat[2, :] - np.ones(2)).max() < 1e-10)
    assert (abs(out_mat[-2, :] - np.array([5.0, 6.0])).max() < 1e-10)
def test_complete_derivative():

    # TODO: Currently if z has a 0 value then it breaks because occasionally
    #       tries to raise 0 to a negative power -- This can be fixed by
    #       checking whether coefficient is 0 before trying to do anything...

    # Test derivative vector
    z = np.array([1, 2, 3])
    sol_vec = np.array([0.0, 1.0, 0.0, 0.0, 2.0, 2.0, 3.0, 0.0, 0.0, 0.0])
    out_vec = np.empty(n_complete(3, 2))
    _complete_poly_der_impl_vec(z, 2, 0, out_vec)
    assert (abs(out_vec - sol_vec).max() < 1e-10)

    # Test derivative matrix
    z = np.arange(1, 7).reshape(3, 2)
    out_mat = complete_polynomial_der(z, 2, 1)
    assert (abs(out_mat[0, :]).max() < 1e-10)
    assert (abs(out_mat[2, :] - np.ones(2)).max() < 1e-10)
    assert (abs(out_mat[-2, :] - np.array([5.0, 6.0])).max() < 1e-10)
Example #5
0
    def __init__(self, ncgm, degree, prev_sol=None):
        # Save model and approximation degree
        self.ncgm, self.degree = ncgm, degree

        # Unpack some info from ncgm
        A, alpha, beta, delta, gamma, rho, sigma = self._unpack_params()
        grid = self.ncgm.grid
        k = grid[:, 0]
        z = grid[:, 1]

        # Use parameter values from model to create a namedtuple with
        # parameters saved inside
        self.params = Params(A, alpha, beta, delta, gamma, rho, sigma)

        self.Phi = complete_polynomial(grid.T, degree).T
        self.dPhi = complete_polynomial_der(grid.T, degree, 0).T

        # Update to fill initial value and policy matrices
        # If we give it another solution type then use it to
        # generate values and policies
        if issubclass(type(prev_sol), GeneralSolution):
            oldPhi = complete_polynomial(ncgm.grid.T, prev_sol.degree).T
            self.VF = oldPhi @ prev_sol.v_coeffs
            self.KP = oldPhi @ prev_sol.k_coeffs
        # If we give it a tuple then assume it is (policy, value) pair
        elif type(prev_sol) is tuple:
            self.KP = prev_sol[0]
            self.VF = prev_sol[1]
        # Otherwise guess a constant value function and a policy
        # of roughly steady state
        else:
            # VF is 0 everywhere
            self.VF = np.zeros(ncgm.ns)

            # Roughly ss policy
            c_pol = f(k, z, A, alpha) * (A - delta) / A
            self.KP = expendables_t(k, z, A, alpha, delta) - c_pol

        # Coefficients based on guesses
        self.v_coeffs = la.lstsq(self.Phi, self.VF)[0]
        self.k_coeffs = la.lstsq(self.Phi, self.KP)[0]
Example #6
0
    def compute_dEV(self, kp=None):
        """
        Compute the derivative of the expected value
        """
        # Unpack parameters
        A, alpha, beta, delta, gamma, rho, sigma = self._unpack_params()
        grid = self.ncgm.grid
        ns, Qn = self.ncgm.ns, self.ncgm.Qn

        # Use policy to compute kp and c
        if kp is None:
            kp = self.Phi @ self.k_coeffs

        # Evaluate E[V_{t+1}]
        dVtp1 = np.empty((Qn, grid.shape[0]))
        for iztp1 in range(Qn):
            grid_tp1 = np.vstack([kp, self.ncgm.z1[:, iztp1]])
            dPhi_tp1 = complete_polynomial_der(grid_tp1, self.degree, 0).T
            dVtp1[iztp1, :] = dPhi_tp1 @ self.v_coeffs

        dEV = self.ncgm.weights @ dVtp1

        return dEV