"""F5-conf-04: VFI vs closed form, optimal growth with log utility. Run from workspace root: ~/wp29/venv/bin/python out/analysis.py Deterministic; no random numbers used (random_seed = null).""" 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"]); spacing = str(p["grid_spacing"]).strip() evals = [float(p["eval_y_1"]), float(p["eval_y_2"]), float(p["eval_y_3"])] data_issues = [] assert spacing == "log" # ---------- 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) savings_rate = ab # (y - c)/y # ---------- VFI on prescribed grid ---------- grid = np.exp(np.linspace(np.log(y_min), np.log(y_max), N)) # choice = next-period output y' on grid node j; requires capital k = y'^(1/alpha) < y k_needed = grid ** (1.0 / alpha) # (N,) C = grid[:, None] - k_needed[None, :] # c for (current i, next j) feasible = C > 0 U = np.full_like(C, -np.inf) U[feasible] = np.log(C[feasible]) if not feasible.any(axis=1).all(): data_issues.append({"issue": "some grid states have no feasible choice", "handling": "none"}) V = np.zeros(N) n_iter = 0 sup_change = np.inf max_iter = 100000 while sup_change >= tol and n_iter < max_iter: V_new = np.max(U + beta * V[None, :], axis=1) sup_change = float(np.max(np.abs(V_new - V))) V = V_new n_iter += 1 pol_j = np.argmax(U + beta * V[None, :], axis=1) c_grid = grid - k_needed[pol_j] converged = sup_change < tol # corner checks: does optimal y' hit grid bounds? hits_lower = bool(np.any(pol_j == 0)); hits_upper = bool(np.any(pol_j == N - 1)) if hits_lower or hits_upper: data_issues.append({"issue": f"optimal next-period output hits grid bound (lower={hits_lower}, upper={hits_upper}) for some states", "handling": "reported; policy at evaluation points checked to be interior"}) # interpolation at evaluation points: linear in ln y (grid is uniform in ln y) lg = np.log(grid) interp = lambda f, y: float(np.interp(np.log(y), lg, f)) c_num = [interp(c_grid, y) for y in evals] c_cl = [float(c_closed(y)) for y in evals] max_err = float(max(abs(a - b) for a, b in zip(c_num, c_cl))) V_num1 = interp(V, 1.0) # diagnostics (not required keys) grid_err_c = float(np.max(np.abs(c_grid - c_closed(grid)))) grid_err_V = float(np.max(np.abs(V - V_closed(grid)))) on_node = [bool(np.min(np.abs(lg - np.log(y))) < 1e-12) for y in evals] 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": float(V_closed(1.0)), "value_num_y1": V_num1, "savings_rate_closed_form": float(savings_rate), "contraction_modulus": beta, "grid_points_used": int(N), "n_iterations": int(n_iter), "final_sup_change": sup_change, "feasibility": "complete" if converged else "partial", "feasibility_reason": None if converged else "VFI did not reach tolerance within max_iter", "data_issues": data_issues, "n_input_rows": {"model.csv": int(len(df))}, "n_analysed": int(N), "random_seed": None, "diagnostics": { "eval_points_are_grid_nodes": on_node, "max_abs_policy_error_on_grid": grid_err_c, "max_abs_value_error_on_grid": grid_err_V, "value_error_bound_from_tolerance": beta * tol / (1 - beta), "log_grid_step": float(lg[1] - lg[0]), "closed_form_A": float(A), "closed_form_B": float(B), "policy_hits_lower_bound": hits_lower, "policy_hits_upper_bound": hits_upper, "interpolation": "linear in ln(y) on grid nodes", }, } os.makedirs(os.path.join(ROOT, "out"), exist_ok=True) with open(os.path.join(ROOT, "out", "results.json"), "w") as f: json.dump(res, f, indent=2) print(json.dumps(res, indent=2))