gEconpy.model.perfect_foresight.solve_perfect_foresight#
- gEconpy.model.perfect_foresight.solve_perfect_foresight(model, simulation_length, x0=None, initial_conditions=None, terminal_conditions=None, shocks=None, param_paths=None, compile_kwargs=None, solver=None, steady_state_kwargs=None)#
Solve the model under perfect foresight.
- Parameters:
- model
Model The DSGE model to solve.
- simulation_length
int Number of time periods for the simulation.
- x0
DataFrame,ndarray,dict, orNone, optional Initial guess for the Newton solver. Accepts four forms:
None(default): compute the steady state and tile it across all periods.dict: a steady-state dictionary (as returned bymodel.steady_state()). Used in place of callingmodel.steady_state()internally, and also used for default initial/terminal conditions.DataFrame: columns are variable names, rows are periods. Columns are reindexed to match the model’s canonical variable order. This is the output format ofmake_piecewise_x0().ndarrayof shape(simulation_length, n_vars): a full initial trajectory. Columns must be in the model’s canonical variable order.
- initial_conditions
dictofstrtofloat, optional Initial values for state variables at t=-1. Keys are variable base names (e.g.,
"K"). Keys with an_sssuffix (e.g.,"K_ss") are also accepted. If not provided, steady-state values are used.- terminal_conditions
dictofstrtofloat, optional Terminal values for state variables at t=T. Keys are variable base names (e.g.,
"K"). Keys with an_sssuffix are also accepted. If not provided, steady-state values are used.- shocks
dictofstrtondarray, optional Shock paths over the simulation horizon. Keys are shock base names, values are arrays of length T. If None, all shocks are set to zero.
- param_paths
dictofstrtofloatorndarray, optional Parameter paths over the simulation horizon, analogous to
shocks. Keys are parameter names. Values are either a scalar (constant across all periods) or an array of length T (time-varying). Parameters not listed use their current model values. When time-varying paths are provided and boundary conditions are not fully specified, the steady state for initial/terminal conditions is computed using the parameter values at t=0 and t=T-1 respectively.- compile_kwargs
dict, optional Additional arguments passed to
pytensor.functionwhen compiling.- solver
RootSolver, optional Root-finding solver instance. Default uses Newton’s method with Armijo backtracking (
NewtonArmijo()). Seeline_searchfor available solvers, or pass a custom solver.- steady_state_kwargs
dict, optional Additional arguments passed to
model.steady_state(). If'verbose'is not explicitly set, it defaults toFalse.
- model
- Returns:
- trajectory
DataFrame Solution trajectory with time as index and variables as columns.
- result
OptimizeResult Optimization result with convergence info.
- trajectory
Examples
Temporary shock (transition from perturbed initial condition back to steady state):
import gEconpy as ge model = ge.model_from_gcn("rbc.gcn") ss_dict = model.steady_state() # Simulate transition from 90% of steady state capital trajectory, result = solve_perfect_foresight( model, simulation_length=100, initial_conditions={"K": ss_dict["K_ss"] * 0.9}, )
Permanent shock (transition between two steady states):
from gEconpy.model.perfect_foresight import make_piecewise_x0 import numpy as np init_ss = model.steady_state(tax_rate=0.08, how="root") final_ss = model.steady_state(tax_rate=0.18, how="root") x0 = make_piecewise_x0(init_ss, final_ss, simulation_length=200, transition_periods=50) tax_path = np.where(np.arange(200) < 100, 0.08, 0.18) trajectory, result = solve_perfect_foresight( model, simulation_length=200, x0=x0, initial_conditions=init_ss, terminal_conditions=final_ss, param_paths={"tax_rate": tax_path}, )