示例#1
0
    def test_smolyak(self):

        import numpy

        f = lambda x: numpy.row_stack([
            x[0,:] * x[1,:]**0.5,
            x[1,:] * x[1,:] - x[0,:] * x[0,:]
        ])

        bounds = numpy.row_stack([
            [0.5,0.1],
            [2,3]
        ])

        from dolo.numeric.smolyak import SmolyakGrid
        sg = SmolyakGrid(bounds,3)

        values = f(sg.grid)
        sg.fit_values(values)
        theta_0 = sg.theta.copy()

        def fobj(theta):
            sg.theta = theta
            return sg(sg.grid)

        fobj(theta_0)
示例#2
0
    def test_smolyak_2(self):

        import numpy
        from dolo.numeric.smolyak import SmolyakGrid
        d = 8
        l = 4

        bounds = numpy.row_stack([[-0.5]*6, [0.7]*6])
        sg = SmolyakGrid(bounds,l)
        f = lambda x: numpy.row_stack([
                    x[0,:] * x[1,:],
                    x[1,:] * x[1,:] - x[0,:] * x[0,:]
                ])
        values = f(sg.grid)

        import time
        t = time.time()
        for i in range(5):
            sg.fit_values(sg.grid)

            val = sg(sg.grid)
        s = time.time()
        print(s-t)
示例#3
0
    def test_2d_interpolation(self):
        import numpy
        from dolo.numeric.interpolation import RectangularDomain, SplineInterpolation, LinearTriangulation, TriangulatedDomain
        from dolo.numeric.smolyak import SmolyakGrid
        smin = [-2, -2]
        smax = [2, 2]
        orders = [5, 5]
        orders_ref = [100, 100]
        l = 6
        bounds = numpy.row_stack([smin, smax])
        recdomain = RectangularDomain(smin, smax, orders)
        tridomain = TriangulatedDomain(recdomain.grid)
        recdomain_ref = RectangularDomain(smin, smax, orders_ref)
        smolyakgrid = SmolyakGrid(bounds, l)

        fun = lambda x: 1.0 / numpy.sqrt(2 * numpy.pi) * numpy.exp(-(
            numpy.square(x[0:1, :]) + numpy.square(x[1:2, :])) / 2.0)

        values_rec = fun(recdomain.grid)
        values_smol = fun(smolyakgrid.grid)

        interp_rec = SplineInterpolation(smin, smax, orders)
        interp_smol = smolyakgrid
        interp_simplex = LinearTriangulation(tridomain)

        interp_rec.fit_values(values_rec)
        interp_smol.fit_values(values_smol)
        interp_simplex.fit_values(values_rec)

        true_values = fun(recdomain_ref.grid).reshape(orders_ref)
        interpolated_values_spline = interp_rec(
            recdomain_ref.grid).reshape(orders_ref)
        interpolated_values_simplex = interp_simplex(
            recdomain_ref.grid).reshape(orders_ref)
        interpolated_values_smolyak = interp_smol(
            recdomain_ref.grid).reshape(orders_ref)
示例#4
0
def global_solve(model, bounds=None, initial_dr=None, interp_type='smolyak', pert_order=2, T=200, n_s=2, N_e=40, maxit=500, polish=True, memory_hungry=True, smolyak_order=3, interp_orders=None):
    [y,x,parms] = model.read_calibration()
    sigma = model.read_covariances()
    
    if initial_dr == None:
        initial_dr = approximate_controls(model, order=pert_order, substitute_auxiliary=True, solve_systems=True)
        
    if bounds == None:
        from dolo.numeric.timeseries import asymptotic_variance
        # this will work only if initial_dr is a Taylor expansion
        Q = asymptotic_variance(initial_dr.A.real, initial_dr.B.real, initial_dr.sigma, T=T)
        
        devs = numpy.sqrt( numpy.diag(Q) )
        bounds  = numpy.row_stack([
                                   initial_dr.S_bar - devs * n_s,
                                   initial_dr.S_bar + devs * n_s,
                                   ])

    if interp_orders == None:
            interp_orders = [5]*bounds.shape[1]
    if interp_type == 'smolyak':
        from dolo.numeric.smolyak import SmolyakGrid
        sg = SmolyakGrid( bounds, smolyak_order )
    elif interp_type == 'spline':
        polish = False
        from dolo.numeric.interpolation import SplineInterpolation
        sg = SplineInterpolation( bounds[0,:], bounds[1,:], interp_orders )
    elif interp_type == 'linear':
        from dolo.numeric.interpolation import MLinInterpolation
        sg = MLinInterpolation( bounds[0,:], bounds[1,:], interp_orders )


    
    xinit = initial_dr(sg.grid)
    xinit = xinit.real  # just in case...

    from dolo.compiler.compiler_global import GlobalCompiler, time_iteration, stochastic_residuals_2, stochastic_residuals_3
    gc = GlobalCompiler(model, substitute_auxiliary=True, solve_systems=True)
    
    from dolo.numeric.quantization import quantization_weights
    # number of shocks
    [weights,epsilons] = quantization_weights(N_e, sigma)
    
    dr = time_iteration(sg.grid, sg, xinit, gc.f, gc.g, parms, epsilons, weights, maxit=maxit, nmaxit=50 )
    
    if polish: # this will only work with smolyak
        from dolo.compiler.compiler_global import GlobalCompiler, time_iteration, stochastic_residuals_2, stochastic_residuals_3
        
        from dolo.numeric.solver import solver
        xinit = dr(dr.grid)
        dr.fit_values(xinit)
        shape = dr.theta.shape
        theta_0 = dr.theta.copy().flatten()
        if memory_hungry:
            fobj = lambda t: stochastic_residuals_3(dr.grid, t, dr, gc.f, gc.g, parms, epsilons, weights, shape, no_deriv=True)[0]
            dfobj = lambda t: stochastic_residuals_3(dr.grid, t, dr, gc.f, gc.g, parms, epsilons, weights, shape)[1]
        else :
            fobj = lambda t: stochastic_residuals_2(dr.grid, t, dr, gc.f, gc.g, parms, epsilons, weights, shape, no_deriv=True)
            dfobj = lambda t: stochastic_residuals_2(dr.grid, t, dr, gc.f, gc.g, parms, epsilons, weights, shape)[1]
        
        theta = solver(fobj, theta_0, jac=dfobj, verbose=True)
        dr.theta = theta.reshape(shape)
    
    return dr
示例#5
0
    true_values_T = np.array([T4(i) for i in points])
    true_values = np.array([U4(i) for i in points])
    #    #pyplot.plot(points, cheb[4,:])
    #    pyplot.plot(points, cheb[4,:])
    #    pyplot.plot(points, true_values_T)
    #    pyplot.figure()
    #    pyplot.plot(points, cheb2[4,:])
    #    pyplot.plot(points, true_values)
    #    pyplot.show()

    import pyximport
    pyximport.install()

    from dolo.numeric.smolyak import SmolyakGrid
    sg = SmolyakGrid(np.row_stack([[0] * 6, [1] * 6]), 6)
    points = sg.grid

    from chebychev_pyx import chebychev as cheby_pyx
    import time
    t = time.time()
    for i in range(100):
        cheb = chebychev(points, 10)
    s = time.time()
    for i in range(100):
        ch_x = cheby_pyx(points, 10)

    u = time.time()
    print(s - t)
    print(u - s)
    print(abs(ch_x - cheb).max())
示例#6
0
n_S = 2

dev = eigs[0]
std  = dev**0.5 * n_S


s0 = dr.S_bar
bounds = numpy.row_stack([-std,std])
bounds[:,0] += s0[0]
bounds[:,1] += s0[1]
print('bounds')
print bounds

P = eigs[1]

sg = SmolyakGrid( bounds, 5, P)

[y,x,parms] = model.read_calibration()

xinit = dr( sg.grid)
sg.fit_values(xinit)


from dolo.compiler.compiler_global import CModel, time_iteration, deterministic_residuals

gc = CModel(model)




res = deterministic_residuals(sg.grid, xinit, sg, gc.f, gc.g, dr.sigma, parms)
示例#7
0
n_S = 2

dev = eigs[0]
std = dev**0.5 * n_S

s0 = dr.S_bar
bounds = numpy.row_stack([-std, std])
bounds[:, 0] += s0[0]
bounds[:, 1] += s0[1]
print('bounds')
print bounds

P = eigs[1]

sg = SmolyakGrid(bounds, 5, P)

[y, x, parms] = model.read_calibration()

xinit = dr(sg.grid)
sg.fit_values(xinit)

from dolo.compiler.compiler_global import CModel, time_iteration, deterministic_residuals

gc = CModel(model)

res = deterministic_residuals(sg.grid, xinit, sg, gc.f, gc.g, dr.sigma, parms)

n_e = 5
epsilons = np.zeros((1, n_e))
weights = np.ones(n_e) / n_e
示例#8
0
            sg.fit_values(sg.grid)

            val = sg(sg.grid)
        s = time.time()
        print(s-t)

        
if __name__ == '__main__':

    import numpy
    from dolo.numeric.smolyak import SmolyakGrid
    d = 8
    l = 4

    bounds = numpy.row_stack([[-0.5]*6, [0.7]*6])
    sg = SmolyakGrid(bounds,l)
    f = lambda x: numpy.row_stack([
                x[0,:] * x[1,:],
                x[1,:] * x[1,:] - x[0,:] * x[0,:]
            ])
    values = f(sg.grid)
    tt = numpy.repeat(sg.grid,40,axis=1)

    print tt.shape
    import time
    t = time.time()
    sg.fit_values(sg.grid)

    for i in range(5):

        val = sg(tt)