Skip to content

Commit ef3dc54

Browse files
authored
Fix CI: apply ruff formatting and cleanup
1 parent 8b6fd7a commit ef3dc54

26 files changed

Lines changed: 95 additions & 34 deletions

notebooks/demo.ipynb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
"metadata": {},
2626
"outputs": [],
2727
"source": [
28-
"import math\n",
29-
"import numpy as np\n",
3028
"import matplotlib.pyplot as plt\n",
29+
"import numpy as np\n",
3130
"\n",
31+
"from econ_math_portfolio.models.contract_stochastic_income import (\n",
32+
" ContractParams,\n",
33+
" lifetime_utility,\n",
34+
" solve_c_high,\n",
35+
")\n",
3236
"from econ_math_portfolio.models.cpi_target_discount import CpiParams, cpi, solve_t\n",
33-
"from econ_math_portfolio.models.contract_stochastic_income import ContractParams, lifetime_utility, solve_c_high\n",
34-
"from econ_math_portfolio.models.credit_var_quantile import CreditParams, var_analytic, var_mc\n"
37+
"from econ_math_portfolio.models.credit_var_quantile import CreditParams, var_analytic, var_mc"
3538
]
3639
},
3740
{
@@ -56,7 +59,7 @@
5659
"plt.ylabel(\"CPI(t)\")\n",
5760
"plt.show()\n",
5861
"\n",
59-
"t_star\n"
62+
"t_star"
6063
]
6164
},
6265
{
@@ -81,7 +84,7 @@
8184
"plt.ylabel(\"V(c_high)\")\n",
8285
"plt.show()\n",
8386
"\n",
84-
"c_star\n"
87+
"c_star"
8588
]
8689
},
8790
{
@@ -106,7 +109,7 @@
106109
"plt.ylabel(\"count\")\n",
107110
"plt.show()\n",
108111
"\n",
109-
"analytic, mc_q\n"
112+
"analytic, mc_q"
110113
]
111114
}
112115
],
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .cli import main
22

3-
if __name__ == '__main__':
3+
if __name__ == "__main__":
44
raise SystemExit(main())
0 Bytes
Binary file not shown.
5 Bytes
Binary file not shown.

src/econ_math_portfolio/cli.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,26 @@
66
from pathlib import Path
77
from typing import Any
88

9-
from econ_math_portfolio.scoring import load_submission_json, score_submission, to_json_dict, load_rubric
9+
from econ_math_portfolio.scoring import (
10+
load_rubric,
11+
load_submission_json,
12+
score_submission,
13+
to_json_dict,
14+
)
15+
1016

1117
def _repo_root() -> Path:
1218
return Path(__file__).resolve().parents[2]
1319

20+
1421
def _validators_dir() -> Path:
1522
return _repo_root() / "validators"
1623

24+
1725
def _load_validator(task_id: str):
1826
return import_module(f"validators.{task_id}")
1927

28+
2029
def _emit(obj: Any, *, as_json: bool) -> None:
2130
if as_json:
2231
print(json.dumps(obj, indent=2, sort_keys=True))
@@ -26,23 +35,27 @@ def _emit(obj: Any, *, as_json: bool) -> None:
2635
else:
2736
print(obj)
2837

38+
2939
def cmd_list(*, as_json: bool) -> int:
3040
tasks = sorted([p.stem for p in _validators_dir().glob("*.py") if p.name != "__init__.py"])
3141
_emit({"tasks": tasks}, as_json=as_json)
3242
return 0
3343

44+
3445
def cmd_reference(task_id: str, *, as_json: bool) -> int:
3546
v = _load_validator(task_id)
3647
value = float(v.reference_compute())
3748
_emit({"task_id": task_id, "reference": value}, as_json=as_json)
3849
return 0
3950

51+
4052
def cmd_validate(task_id: str, answer: float, *, as_json: bool) -> int:
4153
v = _load_validator(task_id)
4254
res = v.validate(answer)
4355
_emit(res, as_json=as_json)
4456
return 0 if res["ok"] else 2
4557

58+
4659
def cmd_score(submission_path: str, *, as_json: bool) -> int:
4760
sub_path = Path(submission_path)
4861
payload = load_submission_json(sub_path)
@@ -67,8 +80,11 @@ def cmd_score(submission_path: str, *, as_json: bool) -> int:
6780
_emit(out, as_json=as_json)
6881
return 0 if sb.total >= 0.8 else 2
6982

83+
7084
def main(argv: list[str] | None = None) -> int:
71-
p = argparse.ArgumentParser(prog="econ-math-portfolio", description="Math/Econ reasoning tasks + validators.")
85+
p = argparse.ArgumentParser(
86+
prog="econ-math-portfolio", description="Math/Econ reasoning tasks + validators."
87+
)
7288
p.add_argument("--json", action="store_true", help="Output machine-readable JSON.")
7389
sub = p.add_subparsers(dest="cmd", required=True)
7490

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/econ_math_portfolio/models/contract_stochastic_income.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
import math
44
from dataclasses import dataclass
55

6+
67
@dataclass(frozen=True)
78
class ContractParams:
89
delta: float = 0.95
910
V0: float = 3.0
1011
c_low: float = 0.95
1112
autarky_high: float = 1.1
1213

14+
1315
def lifetime_utility(delta: float, c_low: float, c_high: float) -> float:
1416
return (0.5 * math.log(c_low) + 0.5 * math.log(c_high)) / (1.0 - delta)
1517

16-
def solve_c_high(params: ContractParams, *, lo: float = 1e-8, hi: float = 10.0, iters: int = 200) -> float:
18+
19+
def solve_c_high(
20+
params: ContractParams, *, lo: float = 1e-8, hi: float = 10.0, iters: int = 200
21+
) -> float:
1722
def f(c_high: float) -> float:
1823
return lifetime_utility(params.delta, params.c_low, c_high) - params.V0
1924

0 commit comments

Comments
 (0)