示例#1
0
文件: accuracy.py 项目: sglyon/dolo
def denhaanerrors( model, dr, s0=None, horizon=100, n_sims=10, seed=0, integration_orders=None):

    assert(model.model_type in ('fg', 'fga'))

    from dolo.numeric.discretization.quadrature import gauss_hermite_nodes
    from dolo.algos.simulations import simulate

    n_x = len(model.symbols['controls'])
    n_s = len(model.symbols['states'])

    sigma = model.covariances
    mean = sigma[0,:]*0

    if integration_orders is None:
        integration_orders = [5]*len(mean)
    [nodes, weights] = gauss_hermite_nodes(integration_orders, sigma)

    if s0 is None:
        s0 = model.calibration['states']

    # standard simulation
    simul = simulate(model, dr, s0, horizon=horizon, n_exp=n_sims, seed=seed, solve_expectations=False)
    simul_se = simulate(model, dr, s0, horizon=horizon, n_exp=n_sims, seed=seed, solve_expectations=True, nodes=nodes, weights=weights)

    x_simul = simul[:,n_s:n_s+n_x,:]
    x_simul_se = simul_se[:,n_s:n_s+n_x,:]

    diff = abs( x_simul_se - x_simul )
    error_1 = (diff).max(axis=0).mean(axis=1)
    error_2 = (diff).mean(axis=0).mean(axis=1)

    d = dict(
        max_errors=error_1,
        mean_errors=error_2,
        horizon=horizon,
        n_sims=n_sims
    )

    return DenHaanErrors(d)
示例#2
0
exogenous = UNormal(σ=0.001)

###
### Discretized grid for endogenous states
###

from dolo.numeric.grids import UniformCartesianGrid

grid = UniformCartesianGrid(min=[-0.01, 5], max=[0.01, 15], n=[20, 20])

###
### Solve model
###

# now we should be able to solve the model using
# any of the availables methods

model = PureModel(symbols, calibration, functions, exogenous, grid)

from dolo.algos.time_iteration import time_iteration
from dolo.algos.perturbation import perturb
from dolo.algos.simulations import simulate
from dolo.algos.improved_time_iteration import improved_time_iteration

dr0 = perturb(model)

time_iteration(model)
improved_time_iteration(model)

simulate(model, dr0)
示例#3
0
文件: accuracy.py 项目: sglyon/dolo
def omega(model, dr, n_exp=10000, orders=None, bounds=None,
          n_draws=100, seed=0, horizon=50, s0=None,
          solve_expectations=False, time_discount=None):

    assert(model.model_type =='fga')

    [f,g] = get_fg_functions(model)

    sigma = model.covariances
    parms = model.calibration['parameters']

    mean = numpy.zeros(sigma.shape[0])

    numpy.random.seed(seed)
    epsilons = numpy.random.multivariate_normal(mean, sigma, n_draws)
    weights = np.ones(epsilons.shape[0])/n_draws

    if bounds is None:
        approx = model.options['approximation_space']
        a = approx['a']
        b = approx['b']
        bounds = numpy.row_stack([a,b])
    else:
        a,b =numpy.row_stack(bounds)

    if orders is None:
        orders = [100]*len(a)

    domain = RectangularDomain(a, b, orders)

    grid = domain.grid

    n_s = len(model.symbols['states'])

    errors = test_residuals( grid, dr, f, g, parms, epsilons, weights )
    errors = abs(errors)

    if s0 is None:
        s0 = model.calibration['states']

    from dolo.algos.simulations import simulate
    simul = simulate(model, dr, s0, n_exp=n_exp, horizon=horizon+1,
                     discard=True, solve_expectations=solve_expectations)

    s_simul = simul[:,:,:n_s]

    densities = [domain.compute_density(s_simul[t,:,:]) for t in range(horizon)]
    ergo_dens = densities[-1]

    max_error = numpy.max(errors,axis=0)        # maximum errors
    ergo_error = numpy.dot(ergo_dens,errors)    # weighted by ergodic distribution

    d = dict(
            errors = errors,
            densities = densities,
            bounds = bounds,
            max_errors = max_error,
            ergodic = ergo_error,
            domain = domain
        )

    if time_discount is not None:
        beta = time_discount
        time_weighted_errors = max_error*0
        for i in range(horizon):
            err = numpy.dot(densities[i], errors)
            time_weighted_errors += beta**i * err
        time_weighted_errors /= (1-beta**(horizon-1))/(1-beta)
        d['time_weighted'] = time_weighted_errors


    return EulerErrors(d)