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:
modelModel

The DSGE model to solve.

simulation_lengthint

Number of time periods for the simulation.

x0DataFrame, ndarray, dict, or None, 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 by model.steady_state()). Used in place of calling model.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 of make_piecewise_x0().

  • ndarray of shape (simulation_length, n_vars): a full initial trajectory. Columns must be in the model’s canonical variable order.

initial_conditionsdict of str to float, optional

Initial values for state variables at t=-1. Keys are variable base names (e.g., "K"). Keys with an _ss suffix (e.g., "K_ss") are also accepted. If not provided, steady-state values are used.

terminal_conditionsdict of str to float, optional

Terminal values for state variables at t=T. Keys are variable base names (e.g., "K"). Keys with an _ss suffix are also accepted. If not provided, steady-state values are used.

shocksdict of str to ndarray, 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_pathsdict of str to float or ndarray, 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_kwargsdict, optional

Additional arguments passed to pytensor.function when compiling.

solverRootSolver, optional

Root-finding solver instance. Default uses Newton’s method with Armijo backtracking (NewtonArmijo()). See line_search for available solvers, or pass a custom solver.

steady_state_kwargsdict, optional

Additional arguments passed to model.steady_state(). If 'verbose' is not explicitly set, it defaults to False.

Returns:
trajectoryDataFrame

Solution trajectory with time as index and variables as columns.

resultOptimizeResult

Optimization result with convergence info.

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},
)