def find_steady_state(model, m=None): n_s = len(model.calibration["states"]) n_x = len(model.calibration["controls"]) if m is None: m = model.calibration["exogenous"] p = model.calibration["parameters"] def fobj(v): s = v[:n_s] x = v[n_s:] d = dict(states=s, controls=x, exogenous=m, parameters=p) res = residuals(model, d) return np.concatenate([res["transition"], res["arbitrage"]]) calib = model.calibration x0 = np.concatenate([calib["states"], calib["controls"]]) import scipy.optimize sol = scipy.optimize.root(fobj, x0) res = sol.x d = dict(exogenous=m, states=res[:n_s], controls=res[n_s:], parameters=p) return CalibrationDict(model.symbols, d)
def __update_from_symbolic__(self): import numpy # updates calibration according to the symbolic definitions system = self.symbolic.calibration_dict from dolo.compiler.triangular_solver import solve_triangular_system self.calibration_dict = solve_triangular_system( system ) from dolo.compiler.misc import CalibrationDict, calibration_to_vector calib = calibration_to_vector(self.symbols, self.calibration_dict) self.calibration = CalibrationDict(self.symbols, calib) from .symbolic_eval import NumericEval evaluator = NumericEval(self.calibration_dict) # read symbolic structure self.options = evaluator.eval(self.symbolic.options) distribution = evaluator.eval(self.symbolic.distribution) discrete_transition = evaluator.eval(self.symbolic.discrete_transition) covariances = distribution if distribution is None: self.covariances = None else: self.covariances = numpy.atleast_2d(numpy.array(covariances, dtype=float)) markov_chain = discrete_transition if markov_chain is None: self.markov_chain = None else: self.markov_chain = [numpy.atleast_2d(numpy.array(tab, dtype=float)) for tab in markov_chain]
def calibration(self): if self.__calibration__ is None: calibration_dict = super(self.__class__, self).get_calibration() from dolo.compiler.misc import CalibrationDict, calibration_to_vector calib = calibration_to_vector(self.symbols, calibration_dict) self.__calibration__ = CalibrationDict(self.symbols, calib) # return self.__calibration__
def __update_from_symbolic__(self): import numpy # updates calibration according to the symbolic definitions system = self.symbolic.calibration_dict from dolo.compiler.triangular_solver import solve_triangular_system self.calibration_dict = solve_triangular_system(system) from dolo.compiler.misc import CalibrationDict, calibration_to_vector calib = calibration_to_vector(self.symbols, self.calibration_dict) self.calibration = CalibrationDict(self.symbols, calib) from .symbolic_eval import NumericEval evaluator = NumericEval(self.calibration_dict) # read symbolic structure self.options = evaluator.eval(self.symbolic.options) self.exogenous = self.get_exogenous() self.domain = self.get_domain()
def find_steady_state(model): n_s = len(model.calibration['states']) n_x = len(model.calibration['controls']) m = model.calibration['exogenous'] p = model.calibration['parameters'] def fobj(v): s = v[:n_s] x = v[n_s:] d = dict(states=s, controls=x, exogenous=m, parameters=p) res = residuals(model,d) return np.concatenate([res['transition'],res['arbitrage']]) calib = model.calibration x0 = np.concatenate([calib['states'],calib['controls']]) import scipy.optimize sol = scipy.optimize.root(fobj, x0) res = sol.x d = dict(exogenous=m, states=res[:n_s],controls=res[n_s:],parameters=p) return CalibrationDict(model.symbols, d)
def calibration(self): if self.__calibration__ is None: calibration_dict = self.__get_calibration__() calib = calibration_to_vector(self.symbols, calibration_dict) self.__calibration__ = CalibrationDict(self.symbols, calib) # return self.__calibration__
rk="1/β-1+δ", w="(1-α)*exp(z)*(k/n)**(α)", k="n/(rk/α)**(1/(1-α))", y="exp(z)*k**α*n**(1-α)", i="δ*k", c="y - i", V="log(c)/(1-β)", u="c**(1-σ)/(1-σ) - χ*n**(1+η)/(1+η)", m="β/c**σ*(1-δ+rk)") from dolang.triangular_solver import solve_triangular_system calibration_dict = solve_triangular_system(calibration_strings) calibration_vector = calibration_to_vector(symbols, calibration_dict) calibration = CalibrationDict(symbols, calibration_vector) calibration ### ### Define functions ### # the basic formulation of the functions (aka kernel) # take and return "tuples" of floats because tuples are supposed # to be easy to be optimzed away by the compiler # after the definition of the function, there is some boilerplate # to translate these functions into broadcastable ones, with # optional numerical differentiation capabilities from numba import jit
def find_deterministic_equilibrium(model, constraints=None, return_jacobian=False): ''' Finds the steady state calibration. Taking the value of parameters as given, finds the values for endogenous variables consistent with the deterministic steady-state. This function requires the specification of the first order equations. Parameters ---------- model: NumericModel an `(f,g)` compliant model constraints: dict a dictionaries with forced values. Use it to set shocks to non-zero values or to add additional constraints in order to avoid unit roots. Returns: -------- OrderedDict: calibration dictionary (i.e. endogenous variables and parameters by type) ''' f = model.functions['arbitrage'] g = model.functions['transition'] s0 = model.calibration['states'] x0 = model.calibration['controls'] p = model.calibration['parameters'] if 'shocks' in model.calibration: e0 = model.calibration['shocks'].copy() else: e0 = numpy.zeros(len(model.symbols['shocks'])) n_e = len(e0) z = numpy.concatenate([s0, x0, e0]) symbs = model.symbols['states'] + model.symbols['controls'] addcons_ind = [] addcons_val = [] if constraints is None: constraints = dict() for k in constraints: if k in symbs: i = symbs.index(k) addcons_ind.append(i) addcons_val.append(constraints[k]) elif k in model.symbols['shocks']: i = model.symbols['shocks'].index(k) e0[i] = constraints[k] else: raise Exception( "Invalid symbol '{}' for steady_state constraint".format(k)) def fobj(z): s = z[:len(s0)] x = z[len(s0):-n_e] e = z[-n_e:] S = g(s, x, e, p) r = f(s, x, e, s, x, p) d_e = e - e0 d_sx = z[addcons_ind] - addcons_val res = numpy.concatenate([S - s, r, d_e, d_sx]) return res jac = MyJacobian(fobj)(z) if return_jacobian: return jac rank = numpy.linalg.matrix_rank(jac) if rank < len(z): msg = """\ There are {} equilibrium variables to find, but the jacobian \ matrix is only of rank {}. The solution is indeterminate.""" warnings.warn(msg.format(len(z), rank)) sol = root(fobj, z, method='lm') steady_state = sol.x s = steady_state[:len(s0)] x = steady_state[len(s0):-n_e] e = steady_state[-n_e:] calib = OrderedDict(states=s, controls=x, shocks=e, parameters=p.copy()) if 'auxiliary' in model.functions: a = model.functions['auxiliary'](s, x, p) calib['auxiliaries'] = a from dolo.compiler.misc import CalibrationDict return CalibrationDict(model.symbols, calib)