예제 #1
0
def test_ps_roots():
    weightf = orth.sh_legendre(5).weight_func
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, weightf, 0., 1., 5)
    verify_gauss_quad(orth.ps_roots,
                      orth.eval_sh_legendre,
                      weightf,
                      0.,
                      1.,
                      25,
                      atol=1e-13)
    verify_gauss_quad(orth.ps_roots,
                      orth.eval_sh_legendre,
                      weightf,
                      0.,
                      1.,
                      100,
                      atol=1e-12)

    x, w = orth.ps_roots(5, False)
    y, v, m = orth.ps_roots(5, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    muI, muI_err = integrate.quad(weightf, 0, 1)
    assert_allclose(m, muI, rtol=muI_err)

    assert_raises(ValueError, orth.ps_roots, 0)
    assert_raises(ValueError, orth.ps_roots, 3.3)
예제 #2
0
파일: solve.py 프로젝트: fagan2888/dc-egm
def solve_retirement_model(
    num_grid,
    n_quad_points,
    r,
    coeffs_age_poly,
    theta,
    cost_work,
    beta,
    lambda_,
    sigma,
    mmax,
    num_periods,
    cfloor=0.001,
):
    # Initialize grids
    quadstnorm = scps.norm.ppf(ps_roots(n_quad_points)[0])
    quadw = ps_roots(n_quad_points)[1]
    # define savingsgrid
    savingsgrid = np.linspace(0, mmax, num_grid)
    # Set up list containers
    policy, value = create_container(num_grid, num_periods, savingsgrid, theta,
                                     cost_work)
    # state = 0 retirement
    # state = 1 worker
    for period in range(num_periods - 2, -1, -1):
        # TODO: For state = 0, no need to solve egm.
        for state in [1, 0]:
            value, policy, ev = egm_step(
                value,
                policy,
                state,
                savingsgrid,
                quadstnorm,
                period,
                num_periods,
                num_grid,
                cfloor,
                n_quad_points,
                r,
                coeffs_age_poly,
                theta,
                cost_work,
                beta,
                lambda_,
                sigma,
                quadw,
            )
            if state == 1:
                value_, policy_ = secondary_envelope_wrapper(
                    value, policy, period, theta, cost_work, beta, ev,
                    num_grid)
                value[period][state] = value_
                policy[period][state] = policy_

    return value, policy
예제 #3
0
def test_ps_roots():
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, 5)
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, 25, atol=1e-13)
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, 100, atol=1e-12)

    x, w = orth.ps_roots(5, False)
    y, v, m = orth.ps_roots(5, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    assert_raises(ValueError, orth.ps_roots, 0)
    assert_raises(ValueError, orth.ps_roots, 3.3)
예제 #4
0
def test_ps_roots():
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, 5)
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, 25, atol=1e-13)
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, 100, atol=1e-12)

    x, w = orth.ps_roots(5, False)
    y, v, m = orth.ps_roots(5, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    assert_raises(ValueError, orth.ps_roots, 0)
    assert_raises(ValueError, orth.ps_roots, 3.3)
예제 #5
0
def test_ps_roots():
    weightf = orth.sh_legendre(5).weight_func
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, weightf, 0.0, 1.0, 5)
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, weightf, 0.0, 1.0, 25, atol=1e-13)
    verify_gauss_quad(orth.ps_roots, orth.eval_sh_legendre, weightf, 0.0, 1.0, 100, atol=1e-12)

    x, w = orth.ps_roots(5, False)
    y, v, m = orth.ps_roots(5, True)
    assert_allclose(x, y, 1e-14, 1e-14)
    assert_allclose(w, v, 1e-14, 1e-14)

    muI, muI_err = integrate.quad(weightf, 0, 1)
    assert_allclose(m, muI, rtol=muI_err)

    assert_raises(ValueError, orth.ps_roots, 0)
    assert_raises(ValueError, orth.ps_roots, 3.3)
예제 #6
0
 def set_ngauss(self):
     k, w = ps_roots(self.ng)
     self.knots, self.weights = k * pi, w * pi
     self.thetas = self.knots
     self.sint = sin(self.thetas)
     self.cost = cos(self.thetas)
     self.tgt = tan(self.thetas)
     self.ctgt = 1 / self.tgt
예제 #7
0
 def set_ngauss(self):
     k, w = ps_roots(self.ng)
     self.knots, self.weights = k * pi, w * pi
     self.thetas = self.knots
     self.sint = sin(self.thetas)
     self.cost = cos(self.thetas)
     self.tgt = tan(self.thetas)
     self.ctgt = 1 / self.tgt
예제 #8
0
    def __init__(self, order):
        """
        Parameters:
        ----------
        order:
            Quadrature Order

        """
        self._order = order

        # The integration rules for the unit square can be constructed
        # by taking tensor products of the standard one-dimensional
        # Gauss-Legendre quadrature o the domain [0, 1]
        x, w = ps_roots(self._order)
        self._size = x.size * x.size
        self._points = (numpy.array(numpy.meshgrid(
            x, x, indexing="ij")).transpose().reshape(self._size, 2))
        self._weights = numpy.outer(w, w).reshape((self._size, 1))
예제 #9
0
def GaussLegendre(degree):
    return sso.ps_roots(degree)