Solvers#
solvers.py classes and functions
ogcore.solvers#
- class ogcore.solvers.AndersonAccelerator(m=5, beta=1.0)[source]#
Anderson acceleration (type-II) with limited memory for the TPI outer loop.
Given the residual \(f_k = G(x_k) - x_k\) and the differences \(\Delta X, \Delta F\) of the last
miterates and residuals, the update is\[x_{k+1} = x_k + \beta f_k - (\Delta X + \beta\Delta F)\gamma,\]where \(\gamma\) solves the least squares problem \(\min_{\gamma}\ \lVert f_k - \Delta F\gamma\rVert\).
beta = 1is undamped;beta < 1adds damping for robustness far from the solution.The macro/price blocks differ in magnitude by orders (r ~ 0.05, BQ/TR large), which would swamp the least squares in raw units, so each element is scaled by a fixed reference captured on the first step (floored well away from zero) to put the whole vector in an O(1), dimensionless space.
- Parameters:
m (int) – number of previous iterates kept in the acceleration memory
beta (float) – mixing (relaxation) parameter applied to the residual
- reset()[source]#
Clear the stored iterate and residual history so the next step restarts the acceleration from scratch (used by
run_TPI’s safety net when an accelerated step diverges). The fixed per-element scale is kept.- Returns:
None
- update(x, gx)[source]#
Propose the next iterate from the current iterate and map value.
- Parameters:
x (array_like) – current iterate of the flattened outer-loop variables
gx (array_like) – value of the fixed-point map G(x) implied by the model at the current iterate
- Returns:
- proposed next iterate, in the same
(unscaled) units as
x
- Return type:
x_next (Numpy array)
Pluggable outer-loop update rules for the TPI fixed-point solve.
run_TPI’s outer loop computes an implied path G(x) from the current
guess x of the macro/price series {r_p, r, w, p_m, BQ[, TR]}; the
update rule maps (x, G(x), history) -> x_next. The default "picard"
rule is the damped step x_next = (1 - nu) x + nu G(x) – the model’s
historical functional iteration (see the nu parameter) – and run_TPI
keeps its original convex_combo path for "picard", so the default
behavior (and golden outputs) are unchanged.
The "anderson" rule instead uses the recent residual history
f = G(x) - x to take larger, better-directed (superlinear) steps, selected
via p.TPI_outer_method. On its own Anderson can overshoot a strongly
nonlinear map into infeasible regions; run_TPI guards it with a trust
region anchored to the always-feasible damped point (see run_TPI).
- ogcore.solvers.make_outer_updater(method, p)[source]#
Create the outer-loop updater selected by
p.TPI_outer_method.- Parameters:
method (str or None) – outer-loop update rule, either “picard” or “anderson” (None defaults to “picard”)
p (OG-Core Specifications object) – model parameters
- Returns:
- accelerator instance for
”anderson”, or None for “picard” – the model’s historical damped functional iteration, which
run_TPIhandles with its native update
- Return type:
updater (AndersonAccelerator or None)
- Raises:
ValueError – if
methodis not a recognized update rule
- ogcore.solvers.pack_outer_vars(blocks, T)[source]#
Stack the first
Tperiods of each (current, implied) pair of outer-loop arrays into the flat vectors the update rule works on.- Parameters:
blocks (list) – (current, implied) pairs of Numpy arrays for the outer-loop variables, e.g. [(r_p, r_p_new), (r, rnew), …]
T (int) – number of transition-path periods to include
- Returns:
stacked outer-loop vectors:
x (Numpy array): current iterate
gx (Numpy array): implied fixed-point map value G(x)
- Return type:
(tuple)
- ogcore.solvers.unpack_outer_vars(x_next, blocks, T)[source]#
Write a stacked next iterate back into the first
Tperiods of each current outer-loop array, in place (the inverse ofpack_outer_vars).- Parameters:
x_next (Numpy array) – stacked next iterate from the update rule
blocks (list) – (current, implied) pairs of Numpy arrays, in the same order passed to
pack_outer_varsT (int) – number of transition-path periods in the stack
- Returns:
None