pyperfectforesight.solve_perfect_foresight_expectation_errors#
- pyperfectforesight.solve_perfect_foresight_expectation_errors(T, params_dict, ss, model_funcs, vars_dyn, news_shocks, X0=None, initial_state=None, ss_initial=None, stock_var_indices=None, constant_simulation_length=False, solver_options=None, sub_x0=None, compiled_ss=None)[source]#
Solve a perfect foresight model with multiple surprise (MIT) shocks.
Replicates Dynare’s
perfect_foresight_with_expectation_errors_setup/perfect_foresight_with_expectation_errors_solver. At eachlearnt_inperiod agents receive a surprise update to their information set and re-solve the perfect foresight problem from that point forward. The full simulation path is stitched from the sub-simulations.- Parameters:
T (int) – Total simulation length (number of periods in the stitched output).
params_dict (dict) – Parameter values.
ss (ndarray, shape (n_endo,)) – Default terminal steady state (initial right BVP boundary), used unless overridden by an
endvalprovided in anews_shocks3-tuple.model_funcs (dict) – Output of
process_model().vars_dyn (list of str) – Endogenous variable names (may be extended by process_model).
news_shocks (list of tuples) –
Each entry is either a 2-tuple
(learnt_in, exog_path)or a 3-tuple(learnt_in, exog_path, endval)wherelearnt_inis the period at which agents receive new information (1-indexed, must be in[1, T]).exog_pathis either an array with at leastT_subrows andn_exocolumns, representing the agents’ full belief about the shock path fromlearnt_inonward (row 0 is the shock at periodlearnt_in, row 1 at periodlearnt_in + 1, etc.), orNoneto pass an all-zero exogenous path to the sub-solver. Note:Noneis only correct when the exogenous steady state is zero; for level exogenous variables (e.g.z = 1) you must supply an explicit path including the steady-state level, otherwise the simulation will be incorrect. Only the firstT_subrows are used internally (T_sub = Twhenconstant_simulation_length=True; otherwiseT_sub = T - learnt_in + 1); extra rows are ignored.endval(optional) overrides the terminal BVP boundary (right-hand steady state) for this sub-solve and all subsequent ones. Use this to replicate Dynare’sendval(learnt_in=k)block, which signals a permanent shock that changes the terminal steady state. If omitted andcompiled_ssis provided, the terminal steady state is automatically computed from the last row of that segment’sexog_path(the long-run exogenous level). If neither is given, the currentendval(initialised toss) is reused.
The list must be sorted by
learnt_inand the first entry must havelearnt_in == 1.X0 (ndarray, shape (T, n_endo), or None, optional) –
Initial guess for the endogenous path, used as the warm-start for the first sub-solve. Subsequent sub-solves are warm-started from the previous sub-solve’s solution. If None (the default), the path is tiled from the effective terminal steady state for the first segment, determined by the following priority order:
The explicit
endvalin the firstnews_shocks3-tuple.Auto-computed from
compiled_ssat the last row of the first segment’sexog_path(whencompiled_ssis provided and the first segment has a non-Noneexog_path).ss(the steady state passed as the second positional argument).
initial_state (ndarray, optional) – Pre-period-0 stock variable values (
k_{-1}in Dynare notation). Defaults toss_initial[stock_var_indices].ss_initial (ndarray, optional) – Initial steady state. Defaults to
ss.stock_var_indices (list of int, optional) – Inferred from incidence if not provided.
constant_simulation_length (bool, default False) – If False (Dynare’s default), each sub-solve uses the shrinking remaining horizon
T - learnt_in + 1. If True (Dynare’sconstant_simulation_lengthoption), every sub-solve runs for the fullTperiods.solver_options (dict, optional) – Forwarded unchanged to each
solve_perfect_foresightsub-solve. Recognised keys:maxiter,maxfev,ftol,xtol.sub_x0 (list or tuple, optional) –
Per-sub-solve initial guesses, one entry per element of
news_shocks. Each entry is either:None— use the automatic warm-start (previous sub-solve’s tail, orX0for the first sub-solve).an ndarray of shape
(T_sub, n_endo)— use this array as the warm-start for that sub-solve.T_subisT - learnt_in + 1(orTwhenconstant_simulation_length=True); the array will be trimmed or padded toT_subrows if necessary, matching the behaviour applied to the automatic warm-start.
If
sub_x0isNone(the default) the automatic warm-start is used for every sub-solve.compiled_ss (dict, optional) –
Pre-compiled steady-state bundle returned by
compile_steady_state_funcs(equations, vars_dyn, vars_exo). When provided, the terminal steady state for each sub-solve is automatically computed from the last row of that segment’sexog_path(i.e.exog_path[-1]before trimming toT_sub), unless thenews_shocksentry already supplies an explicitendvalin a 3-tuple. This ensures every sub-solve’s terminal boundary is a true steady state consistent with the agents’ long-run exogenous beliefs.The computed
endvalpersists to subsequent segments (same semantics as an explicit 3-tupleendval): if segment k auto-computes anendvaland segment k+1 provides neither an explicitendvalnor anexog_path, segment k+1 reuses the value from segment k.Compile the bundle once and reuse across many simulations:
>>> compiled_ss = compile_steady_state_funcs(equations, vars_dyn, vars_exo) >>> result = solve_perfect_foresight_expectation_errors( ... T, params, ss_initial, model_funcs, vars_dyn, ... news_shocks=[(1, exog_path_1), (6, exog_path_2)], ... compiled_ss=compiled_ss, ... )
- Returns:
sol.xndarray, shape (T * n_endo,)Stitched endogenous path, reshape to
(T, n_endo).n_endoislen(model_funcs.get('vars_dyn', vars_dyn)), i.e. the effective variable count afterprocess_model(same ordering asvars_dynused internally).sol.successboolTrue if every sub-solve converged.
sol.statusint1 if all sub-solves converged, 0 otherwise.
sol.messagestrHuman-readable summary; lists failing segments on failure.
sol.sub_resultslist of OptimizeResultPer-sub-solve results (one per entry in
news_shocks).sol.x_auxndarray or NoneStitched auxiliary variable path, shape
(T, n_aux), or None.sol.vars_auxlist of strNames of auxiliary variables (empty list if none).
- Return type:
OptimizeResult