Skip to content

Commit 0e413ad

Browse files
authored
Format originals and validators to satisfy ruff
1 parent ef3dc54 commit 0e413ad

6 files changed

Lines changed: 81 additions & 81 deletions

originals/b7_contract_stochastic_income.py

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,48 @@
44

55

66
def solve_contract() -> float:
7-
# --- Inputs ---
7+
"""Solve for high-state consumption in a two-state stochastic contract.
8+
9+
Uses bisection to match a target lifetime utility under log preferences
10+
with a participation (exit) constraint in the high-income state.
11+
"""
812
delta = 0.95
9-
prob = 0.5 # equal probability for the two income states
13+
prob = 0.5
1014
c_low = 0.95
11-
V0_target = 3.0
15+
v0_target = 3.0
1216

13-
# Autarky utilities (if consumption equals income)
14-
V_aut_high = math.log(1.1) / (1 - delta)
17+
v_aut_high = math.log(1.1) / (1.0 - delta)
1518

16-
# Expected lifetime utility under the contract (given c_high)
1719
def expected_value(c_high: float) -> float:
1820
if c_high <= 0:
1921
return -math.inf
20-
exp_u = prob * math.log(c_low) + prob * math.log(c_high)
21-
return exp_u / (1 - delta)
22+
eu = prob * math.log(c_low) + prob * math.log(c_high)
23+
return eu / (1.0 - delta)
2224

23-
# Participation / exit constraint in the high-income state:
24-
# log(c_high)/(1-delta) >= log(1.1)/(1-delta) => c_high >= 1.1
2525
def exit_constraint(c_high: float) -> bool:
2626
return c_high >= 1.1
2727

28-
# Solve expected_value(c_high) = V0_target via bisection on (0, 10)
2928
tol = 1e-8
30-
low, high = 1e-8, 10.0
29+
lo, hi = 1e-8, 10.0
3130

32-
# Bracketing check (monotone in c_high)
33-
if expected_value(low) > V0_target:
34-
raise ValueError("Target too low for the chosen lower bound.")
35-
if expected_value(high) < V0_target:
36-
raise ValueError("Target too high for the chosen upper bound.")
31+
if expected_value(lo) > v0_target:
32+
raise ValueError("Target utility too low for lower bound.")
33+
if expected_value(hi) < v0_target:
34+
raise ValueError("Target utility too high for upper bound.")
3735

38-
while high - low > tol:
39-
mid = (low + high) / 2
40-
val = expected_value(mid)
41-
if val < V0_target:
42-
low = mid
36+
while hi - lo > tol:
37+
mid = 0.5 * (lo + hi)
38+
if expected_value(mid) < v0_target:
39+
lo = mid
4340
else:
44-
high = mid
45-
46-
solution = (low + high) / 2
41+
hi = mid
4742

48-
# Enforce exit constraint if needed
49-
if not exit_constraint(solution):
50-
print("Exit constraint not satisfied by the utility-matching solution.")
51-
print("Raising c_high to the minimum feasible value: 1.100000")
52-
solution = max(solution, 1.1)
53-
else:
54-
print("Solution satisfies both the target utility and the exit constraint.")
43+
sol = 0.5 * (lo + hi)
44+
if not exit_constraint(sol):
45+
sol = 1.1
5546

56-
print(f"c_high (income=1.1): {solution:.6f}")
57-
print(f"Contract lifetime utility: {expected_value(solution):.6f}")
58-
print(
59-
f"High-state lifetime utility: {math.log(solution)/(1 - delta):.6f} "
60-
f"(autarky minimum: {V_aut_high:.6f})"
61-
)
62-
return float(solution)
47+
return float(sol)
6348

6449

6550
if __name__ == "__main__":
66-
solve_contract()
51+
print(solve_contract())

originals/check_HJB_condition.py

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
from __future__ import annotations
22

33
# Original-style script: HJB inequality check at a test state.
4-
#
5-
# Notes:
6-
# - This script is kept in `originals/` for transparency.
7-
# - The repo's main implementation lives in:
8-
# `src/econ_math_portfolio/models/hjb_discount_threshold.py`
9-
#
10-
# We check the inequality in the form used in the repo:
11-
# F(rho) = rho*w^gamma + b*p*gamma*y*w^(gamma-1) + 0.5*sigma^2*p^2*gamma*(gamma-1)*y^2*w^(gamma-2)
12-
# and compute rho_critical (smallest rho with F(rho) >= 0) at x=0,y=1,p=1.
13-
14-
# Model parameters
4+
# Kept for transparency; main implementation lives in models/.
5+
156
b = 1.0
167
sigma = 0.2
178
gamma = 0.7
@@ -21,41 +12,39 @@ def F(rho: float, x: float, y: float, p: float) -> float:
2112
w = x + p * y
2213
if w <= 0:
2314
raise ValueError("Wealth must be positive.")
15+
2416
return (
2517
rho * (w**gamma)
2618
+ b * p * gamma * y * (w ** (gamma - 1))
27-
+ 0.5 * (sigma**2) * (p**2) * gamma * (gamma - 1) * (y**2) * (w ** (gamma - 2))
19+
+ 0.5
20+
* sigma**2
21+
* p**2
22+
* gamma
23+
* (gamma - 1)
24+
* y**2
25+
* (w ** (gamma - 2))
2826
)
2927

3028

3129
def rho_critical(x: float, y: float, p: float) -> float:
3230
w = x + p * y
3331
if w <= 0:
3432
raise ValueError("Wealth must be positive.")
33+
3534
const = (
3635
b * p * gamma * y * (w ** (gamma - 1))
37-
+ 0.5 * (sigma**2) * (p**2) * gamma * (gamma - 1) * (y**2) * (w ** (gamma - 2))
36+
+ 0.5
37+
* sigma**2
38+
* p**2
39+
* gamma
40+
* (gamma - 1)
41+
* y**2
42+
* (w ** (gamma - 2))
3843
)
39-
return (-const) / (w**gamma)
44+
return -const / (w**gamma)
4045

4146

4247
if __name__ == "__main__":
43-
p_test = 1.0
44-
x_test = 0.0
45-
y_test = 1.0
46-
47-
rho_star = rho_critical(x_test, y_test, p_test)
48-
test_rhos = [rho_star - 0.01, rho_star, rho_star + 0.01]
49-
50-
print(f"Critical rho (rho*): {rho_star:.6f}")
51-
print(f"Test state: x={x_test}, y={y_test}, p={p_test}")
52-
print("=" * 60)
53-
54-
for rho in test_rhos:
55-
value = F(rho, x_test, y_test, p_test)
56-
status = "SATISFIED" if value >= 0 else "VIOLATED"
57-
print(f"rho = {rho:.6f}")
58-
print(f"delta vs critical: {rho - rho_star:+.6f}")
59-
print(f"F(rho) = {value:.6f}")
60-
print(f"Inequality status: {status}")
61-
print("-" * 60)
48+
x0, y0, p0 = 0.0, 1.0, 1.0
49+
rho_star = rho_critical(x0, y0, p0)
50+
print(f"Critical rho: {rho_star:.6f}")
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
from __future__ import annotations
22

3-
from econ_math_portfolio.models.contract_stochastic_income import ContractParams, solve_c_high
3+
from econ_math_portfolio.models.contract_stochastic_income import (
4+
ContractParams,
5+
solve_c_high,
6+
)
47
from econ_math_portfolio.utils.validate import result
58

69
TASK_ID = "contract_stochastic_income"
710
TOL = 1e-6
811

12+
913
def reference_compute() -> float:
1014
return solve_c_high(ContractParams())
1115

16+
1217
EXPECTED = reference_compute()
1318

19+
1420
def validate(answer: float) -> dict:
15-
return result(TASK_ID, EXPECTED, TOL, float(answer))
21+
return result(TASK_ID, EXPECTED, TOL, float(answer))

validators/cpi_target_discount.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
TASK_ID = "cpi_target_discount"
77
TOL = 1e-5
88

9+
910
def reference_compute() -> float:
1011
return solve_t(CpiParams())
1112

13+
1214
EXPECTED = reference_compute()
1315

16+
1417
def validate(answer: float) -> dict:
15-
return result(TASK_ID, EXPECTED, TOL, float(answer))
18+
return result(TASK_ID, EXPECTED, TOL, float(answer))

validators/credit_var_quantile.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
from __future__ import annotations
22

3-
from econ_math_portfolio.models.credit_var_quantile import CreditParams, var_with_sanity_check
3+
from econ_math_portfolio.models.credit_var_quantile import (
4+
CreditParams,
5+
var_with_sanity_check,
6+
)
47
from econ_math_portfolio.utils.validate import result
58

69
TASK_ID = "credit_var_quantile"
710
TOL = 1e-6
811

12+
913
def reference_compute() -> float:
10-
return var_with_sanity_check(CreditParams(), mc_paths=50_000, seed=7, max_gap=5.0)
14+
return var_with_sanity_check(
15+
CreditParams(),
16+
mc_paths=50_000,
17+
seed=7,
18+
max_gap=5.0,
19+
)
20+
1121

1222
EXPECTED = reference_compute()
1323

24+
1425
def validate(answer: float) -> dict:
15-
return result(TASK_ID, EXPECTED, TOL, float(answer))
26+
return result(TASK_ID, EXPECTED, TOL, float(answer))
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
from __future__ import annotations
22

3-
from econ_math_portfolio.models.hjb_discount_threshold import HjbParams, rho_critical
3+
from econ_math_portfolio.models.hjb_discount_threshold import (
4+
HjbParams,
5+
rho_critical,
6+
)
47
from econ_math_portfolio.utils.validate import result
58

69
TASK_ID = "hjb_discount_threshold"
710
TOL = 1e-6
811

12+
913
def reference_compute() -> float:
1014
return rho_critical(HjbParams())
1115

16+
1217
EXPECTED = reference_compute()
1318

19+
1420
def validate(answer: float) -> dict:
15-
return result(TASK_ID, EXPECTED, TOL, float(answer))
21+
return result(TASK_ID, EXPECTED, TOL, float(answer))

0 commit comments

Comments
 (0)