SPB Git

spb/wp29_uqo

Public
4.0 KB · 89 lines python
1"""Walrasian equilibrium of a 2-agent, 2-good Cobb-Douglas exchange economy (task F5-conf-01).2Reads only data/; writes out/results.json deterministically. No randomness is used."""3import json, os4import pandas as pd5from scipy.optimize import brentq67ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))8end = pd.read_csv(os.path.join(ROOT, "data", "endowments.csv"))9par = pd.read_csv(os.path.join(ROOT, "data", "parameters.csv"))10P = dict(zip(par["parameter"], par["value"].astype(float)))1112issues = []13# ---- validation ------------------------------------------------------------14assert end["agent"].is_unique and set(end["agent"]) == {"A", "B"}, "expected exactly agents A and B"15assert not end.isna().any().any() and not par["value"].isna().any()16e = {r.agent: (float(r.good1), float(r.good2)) for r in end.itertuples()}17alpha = {"A": P["alpha_A"], "B": P["alpha_B"]}18for i in "AB":19    assert 0 < alpha[i] < 1, f"alpha_{i} outside (0,1)"20    assert e[i][0] > 0 and e[i][1] > 0, f"non-positive endowment for {i}"2122E1 = sum(e[i][0] for i in "AB")23E2 = sum(e[i][1] for i in "AB")24E1_sheet, E2_sheet = P["aggregate_endowment_good1"], P["aggregate_endowment_good2"]252627def demand(i, p):28    w = p * e[i][0] + e[i][1]29    return alpha[i] * w / p, (1 - alpha[i]) * w303132def z1(p, E1_used=E1):33    return sum(demand(i, p)[0] for i in "AB") - E1_used343536def z2(p, E2_used=E2):37    return sum(demand(i, p)[1] for i in "AB") - E2_used383940# diagnostic for the summary-sheet discrepancy (not used for estimates)41if abs(E1_sheet - E1) > 1e-12:42    p_sheet = brentq(lambda p: z1(p, E1_sheet), 1e-9, 1e9, xtol=1e-15, rtol=1e-15, maxiter=500)43    issues.append({44        "issue": f"parameters.csv aggregate_endowment_good1={E1_sheet} differs from the sum of individual endowments "45                 f"in endowments.csv ({E1}).",46        "handling": f"Used the sum of individual endowments ({E1}) since aggregate endowment in a pure exchange economy is "47                    f"by definition the sum of individual endowments and endowments.csv is the primary data. Diagnostic: "48                    f"clearing good 1 against {E1_sheet} would give p={p_sheet:.6f} with good-2 excess demand "49                    f"{z2(p_sheet):.6f} != 0, i.e. no consistent equilibrium; the summary value is treated as a recording error."})50if abs(E2_sheet - E2) > 1e-12:51    issues.append({"issue": f"aggregate_endowment_good2={E2_sheet} differs from sum {E2}",52                   "handling": "Used sum of individual endowments."})5354# ---- baseline / independent numerical check: root of z1(p) -----------------55p_num = brentq(z1, 1e-9, 1e9, xtol=1e-15, rtol=1e-15, maxiter=500)5657# ---- closed form -------------------------------------------------------------58# sum_i alpha_i (p e1_i + e2_i) = p E1  =>  p = sum alpha_i e2_i / sum (1-alpha_i) e1_i59p = sum(alpha[i] * e[i][1] for i in "AB") / sum((1 - alpha[i]) * e[i][0] for i in "AB")60x = {i: demand(i, p) for i in "AB"}61ed1 = z1(p)62walras = p * z1(p) + z2(p)6364res = {65    "task_id": "F5-conf-01",66    "price_ratio_p1_over_p2": p,67    "x1_A": x["A"][0], "x2_A": x["A"][1], "x1_B": x["B"][0], "x2_B": x["B"][1],68    "excess_demand_good1": ed1,69    "excess_demand_good2": z2(p),70    "walras_law_residual": walras,71    "aggregate_endowment_good1": E1,72    "aggregate_endowment_good2": E2,73    "aggregate_endowment_good1_summary_sheet": E1_sheet,74    "aggregate_endowment_good2_summary_sheet": E2_sheet,75    "price_ratio_numerical": p_num,76    "price_abs_diff_closed_vs_numerical": abs(p - p_num),77    "feasibility": "complete",78    "feasibility_reason": None,79    "data_issues": issues,80    "n_input_rows": {"endowments.csv": int(len(end)), "parameters.csv": int(len(par))},81    "n_analysed": 2,82    "random_seed": None,83}84os.makedirs(os.path.join(ROOT, "out"), exist_ok=True)85with open(os.path.join(ROOT, "out", "results.json"), "w") as f:86    json.dump(res, f, indent=2)87print(json.dumps({k: res[k] for k in ("price_ratio_p1_over_p2", "price_ratio_numerical", "excess_demand_good1",88                                      "walras_law_residual")}, indent=1))89
Files · main
Menu

Theme