"""F5-conf-04: VFI vs closed form for Brock-Mirman growth with log utility. Run from workspace root: ~/wp29/venv/bin/python out/analysis.py Reads only data/model.csv; deterministic (no randomness).""" 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"])) spacing = str(p["grid_spacing"]).strip(); tol = float(p["vfi_tolerance"]) evals = [float(p["eval_y_1"]), float(p["eval_y_2"]), float(p["eval_y_3"])] data_issues = [] assert spacing == "log" assert 0 < alpha < 1 and 0 < beta < 1 # ---------------- 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) * y V_closed = lambda y: A + B * np.log(y) savings_rate = ab # (y - c)/y # ---------------- VFI on the prescribed grid (discrete choice of next-period y on the grid) ---- grid = np.exp(np.linspace(np.log(y_min), np.log(y_max), N)) k_needed = grid ** (1.0 / alpha) # investment needed to reach y' = grid[j] C = grid[:, None] - k_needed[None, :] # consumption for (y_i, y'_j) feasible = C > 0 U = np.full(C.shape, -np.inf) U[feasible] = np.log(C[feasible]) n_infeasible_pairs = int((~feasible).sum()) 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 converged = sup_change < tol pol_idx = np.argmax(U + beta * V[None, :], axis=1) c_pol = grid - k_needed[pol_idx] bound_hits = int(np.sum((pol_idx == 0) | (pol_idx == N - 1))) if bound_hits: data_issues.append({"issue": f"policy hits grid bound at {bound_hits} nodes", "handling": "reported; results at such nodes may be distorted by truncation"}) # interpolation at evaluation points: linear in ln y (grid is uniform in ln y) lg = np.log(grid) def interp(vals, y): assert y_min <= y <= y_max return float(np.interp(np.log(y), lg, vals)) on_node = [bool(np.min(np.abs(lg - np.log(y))) < 1e-12) for y in evals] c_num = [interp(c_pol, y) for y in evals] c_cf = [float(c_closed(y)) for y in evals] max_err = float(max(abs(a - b) for a, b in zip(c_num, c_cf))) V_num_1 = interp(V, 1.0) # diagnostics (not required keys; printed only) err_grid_c = float(np.max(np.abs(c_pol - c_closed(grid)))) err_grid_V = float(np.max(np.abs(V - V_closed(grid)))) print(f"grid step in ln y = {lg[1]-lg[0]:.6f}; eval points on nodes: {on_node}") print(f"VFI iterations={n_iter}, final sup change={sup_change:.3e}, converged={converged}") print(f"policy bound hits={bound_hits}; infeasible (y,y') pairs={n_infeasible_pairs}") print(f"max |c_vfi - c_closed| over all grid nodes = {err_grid_c:.3e}; max |V_vfi - V_closed| = {err_grid_V:.3e}") if not all(on_node): pass # interpolation is allowed by TASK.md; documented in report res = { "task_id": "F5-conf-04", "c_closed_y05": c_cf[0], "c_closed_y1": c_cf[1], "c_closed_y2": c_cf[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_num_1, "savings_rate_closed_form": float(savings_rate), "contraction_modulus": beta, "grid_points_used": int(N), "n_iterations": int(n_iter), "final_sup_change": float(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": { "grid_step_log_y": float(lg[1] - lg[0]), "eval_points_on_grid_nodes": on_node, "policy_grid_bound_hits": bound_hits, "max_abs_policy_error_all_nodes": err_grid_c, "max_abs_value_error_all_nodes": err_grid_V, "initial_guess": "V0 = 0", "choice_variable": "next-period output y' restricted to grid", "interpolation": "linear in ln y", }, } 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({k: v for k, v in res.items() if k != "diagnostics"}, indent=2))