Calibration & Input-Output
GRAS and cross-entropy matrix balancing, and SAM multipliers.
SpatialEconometrics.StructuralMacroCalibration.CalibrationResult — Type
CalibrationResultOutput of a matrix-balancing problem, from balance_gras_full or balance_generalized_ce.
Fields
X_balanced: the balanced matrix, the result itself.X_prior: the input matrix it started from.tau,sigma: the row and column scaling multipliers.W_sens: sensitivity matrix, for propagating uncertainty in the targets through to the solution.kl_divergence: how farX_balancedmoved fromX_prior. Large values mean the targets were hard to reconcile with the prior.max_discrepancy: largest remaining violation of a target constraint.iterations,converged: solver state. Checkconverged, and readmax_discrepancyeven when it istrue.
SpatialEconometrics.StructuralMacroCalibration.balance_generalized_ce — Function
balance_generalized_ce(X0, G, b, sigma_b; tol = 1e-10, maxiter = 40) -> CalibrationResultBalance a matrix under general linear constraints with uncertain targets, by cross-entropy minimization.
More flexible than balance_gras_full in two ways. Constraints are arbitrary linear combinations G*vec(X) = b rather than just row and column sums, so aggregates spanning several cells can be targeted. And each target carries a standard deviation sigma_b, so constraints are matched as closely as their reliability warrants rather than exactly — which is what makes an over-determined or mutually inconsistent set of targets solvable at all.
Arguments
X0: prior matrix.G: constraint matrix acting onvec(X).b: target values.sigma_b: standard deviation per target. Smaller means a harder constraint; set one very small to make a target effectively exact.
Returns
References
Golan, A., Judge, G. & Robinson, S. (1994). Recovering information from incomplete or partial multisectoral economic data. Review of Economics and Statistics, 76(3).
SpatialEconometrics.StructuralMacroCalibration.balance_gras — Function
balance_gras(X0, u, v; tol = 1e-11) -> MatrixBalance a matrix to target row sums u and column sums v by GRAS, returning just the balanced matrix.
A convenience wrapper over balance_gras_full, which returns the multipliers, sensitivities and convergence diagnostics as well. Prefer the full version when the result matters, since this one discards the information that would tell you the solve went badly.
Examples
X = balance_gras(X_prior, row_targets, col_targets)SpatialEconometrics.StructuralMacroCalibration.balance_gras_full — Function
balance_gras_full(X0, u, v; tol = 1e-11) -> CalibrationResultBalance a matrix to given row and column totals by GRAS, returning the full diagnostics.
GRAS (Generalized RAS) extends biproportional scaling to matrices containing negative entries, which ordinary RAS cannot handle — the reason it is the standard tool for social accounting matrices and input-output tables, where negatives legitimately occur.
The solution is the matrix closest to X0 in a cross-entropy sense that satisfies the targets, so the prior's structure is preserved as far as the constraints allow. Sign patterns and zeros in X0 are respected: a zero cell stays zero.
Arguments
X0: prior matrix.u: target row sums.v: target column sums.
Totals must be consistent (sum(u) == sum(v)) for a solution to exist.
Returns
A CalibrationResult. Use balance_gras when you want the balanced matrix alone.
Differentiable: the solution can be propagated through automatic differentiation, so a balanced matrix can sit inside a larger calibration or estimation problem.
References
Junius, T. & Oosterhaven, J. (2003). Economic Systems Research, 15(1).
SpatialEconometrics.StructuralMacroCalibration.sam_leontief_multipliers — Function
sam_leontief_multipliers(X_sam, endogenous_idx)Leontief multipliers from a social accounting matrix.
Forms the column-coefficient matrix over the accounts named by endogenous_idx, then inverts (I - A) to give the total effect of a unit of exogenous injection on each endogenous account — direct plus all induced rounds.
Arguments
X_sam: a balanced SAM. Balance it first withbalance_gras; multipliers from an unbalanced matrix are meaningless.endogenous_idx: indices of the accounts treated as endogenous. The rest are the exogenous injections, and this split determines the multipliers entirely.