pyperfectforesight.solve_perfect_foresight#
- pyperfectforesight.solve_perfect_foresight(T, params_dict, ss, model_funcs, vars_dyn, X0=None, exog_path=None, initial_state=None, ss_initial=None, stock_var_indices=None, method='sparse_newton', solver_options=None, *, endval=None, compiled_ss=None, homotopy_fallback=True, homotopy_options=None)[source]#
Solve the perfect foresight problem using an augmented-path BVP formulation.
The solver always uses the BVP (boundary value problem) formulation: a fixed
initvalrow (pre-period-0 boundary) and a fixedendvalrow (terminal steady state) are prepended/appended to theT-row unknown path, giving a squareT×nsystem solved by sparse Newton.Parameters:#
- Tint
Number of periods
- params_dictdict
Parameter values
- ssndarray
Default steady-state values used as fallbacks when
ss_initialandendvalare not provided. Specifically:ss_initialdefaults toss(pre-shock steady state) andendvaldefaults toss(terminal boundary). For permanent shocks where the initial and terminal steady states differ, pass the pre-shock values asss_initialand the post-shock values asendval(or assswithss_initialset explicitly).- model_funcsdict
Dictionary from process_model() containing compiled functions
- vars_dynlist
List of endogenous variable names
- X0ndarray or None, optional
Initial guess for endogenous state path (T x n_endo). If None (the default), the path is initialised to the terminal steady state (
endvalif provided, otherwisess) tiled over all T periods.- exog_pathndarray, optional
Exogenous variable path (T x n_exo). If None, no exogenous variables.
- initial_statendarray, optional
Pre-period-0 values of the stock variables (Dynare convention:
k_{-1}). The BVP formulation prepends this as theinitvalboundary row; k_0 and all jump variables at t=0 are determined simultaneously by the model equations at t=0. If None, stock variables default to their initial steady-state values (ss_initial[stock_var_indices]), i.e., the economy starts at the initial steady state. When provided, must have the same length asstock_var_indices.- ss_initialndarray, optional
Initial steady state (at exog[0]). If None, uses ss.
- stock_var_indiceslist of int, optional
Indices of stock (predetermined) variables in vars_dyn. Stock variables are those that appear at lag < 0 in the model equations; their pre-period-0 values form the left BVP boundary. Jump variables (no negative-lag appearances) are free to respond at t=0. If None, inferred automatically from the lead-lag incidence table in
model_funcs['incidence']. Example: vars_dyn=[“c”,”k”], stock_var_indices=[1] means k is stock, c is jump.- endvalarray-like (including SteadyState), optional
Terminal boundary values for all endogenous variables (the fixed right boundary row of the augmented path, appended at
t = T).If provided, it is used as-is and
compiled_ssis ignored forendvalresolution. Use this when you have already computed the terminal steady state (e.g. for repeated simulations where you want to avoid recomputing it).If None and
compiled_ssis provided andexog_pathis not None, the terminal steady state is automatically computed by solving the steady-state equations at the exogenous valuesexog_path[-1](the last row of the path, representing the long-run exogenous level). This guarantees that the terminal boundary is a true steady state consistent with the terminal exogenous values.If None and no
compiled_ssis given (orexog_pathis None), defaults toss.
Must match the effective dynamic variable vector used internally by the solver —
model_funcs['vars_dyn']when present (e.g. whenaux_method='dynamic'extends the variable list), falling back to thevars_dynargument otherwise. Constructendvalconsistently withssandX0using that same variable ordering.- compiled_ssdict, optional
Pre-compiled steady-state bundle returned by
compile_steady_state_funcs(equations, vars_dyn, vars_exo). Required for automaticendvalcomputation fromexog_path[-1](seeendvalabove).Compile the bundle once and reuse it across multiple solver calls:
>>> compiled_ss = compile_steady_state_funcs(equations, vars_dyn, vars_exo)
First call:
endvalis auto-computed fromexog_path[-1].>>> result = solve_perfect_foresight( ... T, params, ss_terminal, model_funcs, vars_dyn, ... exog_path=exog_path, ss_initial=ss_initial, ... compiled_ss=compiled_ss, ... )
Repeated call with the same terminal exogenous level: pass the pre-computed
endvaldirectly to skip the steady-state solve.>>> ss_terminal = solve_steady_state(compiled_ss, params, exog_ss=exog_path[-1]) >>> for shock in shocks: ... result = solve_perfect_foresight( ... T, params, ss_terminal, model_funcs, vars_dyn, ... exog_path=shock, ss_initial=ss_initial, ... endval=ss_terminal, ... )
- methodstr, default
'sparse_newton' Solution algorithm.
'sparse_newton'is the only fully supported value.'hybr'is accepted as a deprecated alias for backward compatibility and emits aDeprecationWarning. Any other value raises aValueError.- solver_optionsdict
Options forwarded to _sparse_newton. Supported keys: ‘maxiter’ (max Newton iterations), ‘ftol’ (f-norm tolerance), ‘xtol’ (x-step tolerance), ‘maxfev’ (max function evaluations budget).
- homotopy_fallbackbool, default True
If True and the sparse Newton solver fails to converge, automatically retries using solve_perfect_foresight_homotopy with the same inputs. A UserWarning is emitted when the fallback is triggered. Set to False to surface the Newton failure directly (original behaviour). Note: fallback is silently skipped when both
initial_stateandexog_pathare None, because homotopy has nothing to scale in that case; the Newton failure result is returned directly.- homotopy_optionsdict, optional
Options forwarded to
solve_perfect_foresight_homotopywhen the fallback is triggered. Supported keys:n_steps(int, default 10): number of homotopy continuation steps.verbose(bool, default False): if True, print homotopy progress.exog_ss(ndarray): steady-state exogenous path (lam=0 value).solver_options(dict): options for the Newton solver at each homotopy step (keys:'maxiter','ftol','xtol','maxfev').endval(ndarray, optional): terminal boundary override for the homotopy solver; if provided, it is interpolated fromss_initialat lam=0 to this value at lam=1.method(str, optional): solution algorithm forwarded to the homotopy solver.'sparse_newton'is the only fully supported value;'hybr'is accepted as a deprecated alias and emits aDeprecationWarning.
Ignored when
homotopy_fallback=Falseor when Newton succeeds.
Returns:#
OptimizeResult : Solution with full path including X[0]