"""F5-conf-04: VFI vs closed form for log-utility Cobb-Douglas growth (full depreciation). Run from workspace root: ~/wp29/venv/bin/python out/analysis.py """ import json, os import numpy as np import pandas as pd ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) df = pd.read_csv(os.path.join(ROOT, "data", "model.csv")) p = dict(zip(df["parameter"], df["value"])) alpha = float(p["alpha"]); beta = float(p["beta"]) y_min = float(p["y_min"]); y_max = float(p["y_max"]); N = int(float(p["grid_points"])) tol = float(p["vfi_tolerance"]) assert str(p["grid_spacing"]).strip() == "log" evals = [float(p["eval_y_1"]), float(p["eval_y_2"]), float(p["eval_y_3"])] # ---------- closed form (guess V = A + B ln y, verified in report) ---------- ab = alpha * beta B = 1.0 / (1.0 - ab) A = (np.log(1.0 - ab) + (ab / (1.0 - ab)) * np.log(ab)) / (1.0 - beta) c_closed = lambda y: (1.0 - ab) * np.asarray(y) V_closed = lambda y: A + B * np.log(y) # ---------- VFI: discrete choice of next-period output y' on the grid ---------- lny = np.linspace(np.log(y_min), np.log(y_max), N) y = np.exp(lny) k_needed = y ** (1.0 / alpha) # savings needed to reach y' = grid[j] C = y[:, None] - k_needed[None, :] # consumption for (state i, choice j) feasible = C > 0 U = np.full_like(C, -np.inf) U[feasible] = np.log(C[feasible]) V = np.zeros(N) n_it = 0 while True: RHS = U + beta * V[None, :] Vn = RHS.max(axis=1) sup = float(np.max(np.abs(Vn - V))) V = Vn n_it += 1 if sup < tol: break pol_j = (U + beta * V[None, :]).argmax(axis=1) c_grid = C[np.arange(N), pol_j] # check whether the choice ever hits grid bounds hits_lower = bool(np.any(pol_j == 0)); hits_upper = bool(np.any(pol_j == N - 1)) # interpolate (linear in ln y) at evaluation points interp = lambda f, x: float(np.interp(np.log(x), lny, f)) c_num = [interp(c_grid, e) for e in evals] c_cl = [float(c_closed(e)) for e in evals] max_err = float(max(abs(a - b) for a, b in zip(c_num, c_cl))) v_num_1 = interp(V, 1.0) v_cl_1 = float(V_closed(1.0)) # whole-grid diagnostics (not required keys) sup_policy_err_grid = float(np.max(np.abs(c_grid - c_closed(y)))) sup_value_err_grid = float(np.max(np.abs(V - V_closed(y)))) issues = [] for e in evals: if not np.any(np.isclose(y, e, rtol=0, atol=1e-12)): issues.append({"issue": f"evaluation point y={e} is not a grid node", "handling": "VFI policy/value linearly interpolated in ln y between adjacent grid nodes"}) issues.append({"issue": "VFI choice variable not specified in model.csv", "handling": "next-period output y' chosen from the same 400-point log grid (discrete choice, no off-grid interpolation of V); V initialised at 0"}) res = { "task_id": "F5-conf-04", "c_closed_y05": c_cl[0], "c_closed_y1": c_cl[1], "c_closed_y2": c_cl[2], "c_num_y05": c_num[0], "c_num_y1": c_num[1], "c_num_y2": c_num[2], "max_abs_error_policy": max_err, "value_closed_y1": v_cl_1, "value_num_y1": v_num_1, "savings_rate_closed_form": float(ab), "contraction_modulus": float(beta), "grid_points_used": int(N), "n_iterations": int(n_it), "final_sup_change": sup, "feasibility": "complete", "feasibility_reason": None, "data_issues": issues, "n_input_rows": {"model.csv": int(len(df))}, "n_analysed": int(N), "random_seed": None, "diagnostics": { "grid_log_step": float(lny[1] - lny[0]), "sup_abs_policy_error_on_grid": sup_policy_err_grid, "sup_abs_value_error_on_grid": sup_value_err_grid, "value_error_bound_from_tolerance": float(beta * tol / (1 - beta)), "policy_hits_lower_grid_bound": hits_lower, "policy_hits_upper_grid_bound": hits_upper, "closed_form_A": float(A), "closed_form_B": float(B), "steady_state_y": float(ab ** (alpha / (1 - alpha))), }, } with open(os.path.join(ROOT, "out", "results.json"), "w") as f: json.dump(res, f, indent=2) print(json.dumps(res, indent=2))