Solutions at the perches¶
Summary
A stage holds its model and no numerical solution. A solver keeps what it computes in plain dictionaries, one for each stage occurrence: keyed by perch as the stage file marks it, arvl, dcsn or cntn, then by the name the file gives each field, with arrays on the perch grid and whole values for constant fields. A trellis's solutions are one such dictionary for each period and stage.
Relationship with other documents
Builds on — Parameters and settings and A piece §2: the parameters, settings and kernels a stage answers; Trellises: the periods of a model and their index.
See also — API: every path and verb of the stage object; the lesson tutorials/topics/kernels.ipynb of the repository, which computes the example of §2.
1 One dictionary for each stage occurrence¶
The package constructs and binds a stage; it computes and stores no value function. A solver keeps its results in one dictionary for each stage occurrence, a stage file elaborated at one position of a period. Its keys are the perches as the stage file marks them: arvl for arrival, dcsn for decision, cntn for continuation. Under each perch is a dictionary keyed by the name the file gives each field there, such as m, m_d, c, a and V, holding NumPy arrays. A solver that stores the derivative of the value keeps it under dV: the keys V and dV of a perch hold the value and its derivative in the perch's coordinate, the function that the derivative term D_{a}V[>] reads at the continuation. The grid of a perch stands under the name of its coordinate, m_d at the decision of the consumption stage and a at its continuation, and each grid-based array holds one entry for each grid point. The grid is the one the settings define: at the decision of the consumption stage, n_m points from m_min to m_max.
Values sent backward include every value-type field the backward header gives back. The solver keeps a separate entry for each such field: the array of V on the perch grid, and one whole value for a constant, a scalar for age or a whole table or function. The receiving stage takes a constant under its declared input spelling as it is kept. For a field that depends on a state it takes a representing function, such as an interpolant of the stored array, since stage.check_inputs refuses an array there. See Supplied continuation fields.
2 An example on the consumption stage¶
The lesson tutorials/topics/kernels.ipynb calibrates the consumption stage applications/portfolio-choice/declaration/stages/cons/cons.bl at \(\rho = 6\), takes at the continuation ten levels of savings and the value of consuming all of them, and computes with the stage's kernels one step of the decision builder at the consumption rule \(c(\mathrm m_d) = \mathrm m_d/2\). It keeps the results in this form:
solution = {
"cntn": {"a": a, "V": v, "dV": dv},
"dcsn": {"m_d": m_d, "c": m_d / 2, "V": V, "dV": dV},
}
a holds the savings levels and v and dv the continuation value and its derivative at them; m_d holds the levels of resources, c the consumption the rule gives, and V and dV the value and its derivative at the decision. The value comes from the decision builder's two compiled kernels, the law of motion \(g\) and the aggregator \(G\), with \(\rho\) passed at the call; the aggregator returns the value alone. The derivative is what the stated derivative line D_{m_d}V = 𝑑𝑢(c) of the decision builder gives, marginal utility \(c^{-\rho}\) at the rule, with \(\rho\) the one entry of values:
g = cons.dcsn.builder.bwd["Pullback"].compile_kernel()
G = cons.dcsn.builder.bwd["Aggregation"].compile_kernel()
values = cons.dcsn.builder.bwd["Aggregation"].kernel().parameter_values
V = G(np.interp(g(m_d, m_d / 2), a, v), m_d, m_d / 2, *values)
dV = (m_d / 2) ** -values[0]
The lesson's ten points are its own, checkable by hand; a solver takes its grids from the settings.
3 A trellis¶
A trellis holds its periods in a list indexed by h, the number of periods before the last. Its solutions are held in the same way, one dictionary of perches for each period and stage, solutions[h]["cons"], which a backward solver fills in the order \(h = 0, 1, 2, \dots\). A stage's continuation arrays are the arrival arrays of the stage its continuation fields join, in the same period or, through the inter-period connector, in solutions[h - 1].
4 What the stage supplies¶
The stage supplies the numbers and the functions. stage.par["ρ"] answers a parameter and its value, and stage.settings["n_m"] a setting and the places that read it. A builder's kernels compute the arrays of one perch from those of the next: compile_kernel() answers one kernel as a plain function, and a builder's kernels() answers all of them in the order they are applied, with the parameters' values, in a record that refers to no stage (A builder §5). A solver needs the records, the grids and the dictionaries, not the stage.