Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ repos:
- id: python-use-type-annotations
- id: text-unicode-replacement-char
- repo: https://github.com/pycqa/isort
rev: 8.0.1
rev: 9.0.0b1
hooks:
- id: isort
name: isort
Expand All @@ -55,13 +55,13 @@ repos:
# args:
# - --py37-plus
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 26.1.0
rev: 26.5.1
hooks:
- id: black
language_version: python3.12
exclude: tests/utils/fast_upper_envelope_org.py
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.4
rev: v0.16.1
hooks:
- id: ruff
# exclude: |
Expand All @@ -70,7 +70,7 @@ repos:
# docs/source/conf.py|
# )$
- repo: https://github.com/PyCQA/docformatter
rev: v1.7.7
rev: v1.7.8
hooks:
- id: docformatter
args:
Expand Down
4 changes: 2 additions & 2 deletions docs/time_period2_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def _parse_args() -> argparse.Namespace:


def utility_crra_jax(
consumption: jnp.ndarray, choice: int, params: Dict[str, float]
consumption: jnp.ndarray, choice: int, params: dict[str, float]
) -> jnp.ndarray:
utility_consumption = (consumption ** (1 - params["rho"]) - 1) / (1 - params["rho"])
utility = utility_consumption - (1 - choice) * params["delta"]
Expand All @@ -50,7 +50,7 @@ def value_func_numba(
test_resources / f"pol{period}.csv", delimiter=",", dtype=float
)

params: Dict[str, float] = {"beta": 0.95, "rho": 1.95, "delta": 0.35}
params: dict[str, float] = {"beta": 0.95, "rho": 1.95, "delta": 0.35}
state_choice = {"lagged_choice": 0, "choice": 0}


Expand Down
107 changes: 54 additions & 53 deletions docs/tutorials/getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,35 @@
"cells": [
{
"cell_type": "code",
"outputs": [],
"source": [
"import numpy as np\n",
"from upper_envelope.fues_numba.fues_numba import fast_upper_envelope_wrapper\n",
"import numba as nb\n",
"from collections import namedtuple\n"
],
"execution_count": 1,
"id": "c71bb4b54fd1da58",
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-05-24T06:42:04.589484405Z",
"start_time": "2024-05-24T06:42:04.167227740Z"
}
},
"collapsed": false
},
"id": "c71bb4b54fd1da58",
"execution_count": 1
"outputs": [],
"source": [
"from collections import namedtuple\n",
"\n",
"import numba as nb\n",
"import numpy as np\n",
"from upper_envelope.fues_numba.fues_numba import fast_upper_envelope_wrapper\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ab4b0dcb970dac41",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-24T06:42:04.611236211Z",
"start_time": "2024-05-24T06:42:04.609614665Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"@nb.njit\n",
Expand Down Expand Up @@ -50,27 +60,18 @@
" utility = utility_consumption - (1 - params_ntuple.choice) * params_ntuple.delta\n",
"\n",
" return utility"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-05-24T06:42:04.611236211Z",
"start_time": "2024-05-24T06:42:04.609614665Z"
}
},
"id": "ab4b0dcb970dac41",
"execution_count": 2
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-05-24T06:42:04.613370376Z",
"start_time": "2024-05-24T06:42:04.609954700Z"
}
},
"collapsed": true
},
"outputs": [],
"source": [
Expand All @@ -89,6 +90,15 @@
},
{
"cell_type": "code",
"execution_count": 4,
"id": "61b7b971c6f81237",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-24T06:42:04.614201145Z",
"start_time": "2024-05-24T06:42:04.610074325Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"resource_dir = \"../../tests/resources/\"\n",
Expand All @@ -108,19 +118,19 @@
" \"n_constrained_points_to_add\": n_constrained_points_to_add,\n",
" \"n_points_to_scan\": 10,\n",
"}"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-05-24T06:42:04.614201145Z",
"start_time": "2024-05-24T06:42:04.610074325Z"
}
},
"id": "61b7b971c6f81237",
"execution_count": 4
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "415a872b98d65631",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-24T06:42:04.614607376Z",
"start_time": "2024-05-24T06:42:04.610206856Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"\n",
Expand All @@ -130,19 +140,19 @@
"tuning_params_tuple = namedtuple(\"tunings\", tuning_params.keys())(\n",
" *tuning_params.values()\n",
")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-05-24T06:42:04.614607376Z",
"start_time": "2024-05-24T06:42:04.610206856Z"
}
},
"id": "415a872b98d65631",
"execution_count": 5
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "df76ba014c57934d",
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-24T06:42:14.783923576Z",
"start_time": "2024-05-24T06:42:04.611563430Z"
},
"collapsed": false
},
"outputs": [],
"source": [
"endog_grid_refined, policy_refined, value_refined = fast_upper_envelope_wrapper(\n",
Expand All @@ -156,16 +166,7 @@
" discount_factor=beta,\n",
" tuning_params=tuning_params_tuple,\n",
")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2024-05-24T06:42:14.783923576Z",
"start_time": "2024-05-24T06:42:04.611563430Z"
}
},
"id": "df76ba014c57934d",
"execution_count": 6
]
}
],
"metadata": {
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorials/ue_drued_jorg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import jax\n",
"import jax.numpy as jnp\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"import upper_envelope.jax as upenv\n",
"\n",
Expand Down
5 changes: 3 additions & 2 deletions src/upper_envelope/jax/drued_jorg_jax.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

from collections.abc import Callable
from functools import partial
from typing import Callable, Dict, Optional
from typing import Dict, Optional

import jax
import jax.numpy as jnp
Expand All @@ -16,7 +17,7 @@ def drued_jorg_jax(
expected_value_zero_savings: jnp.ndarray | float,
value_function: Callable,
value_function_args=(),
value_function_kwargs: Optional[Dict] = None,
value_function_kwargs: dict | None = None,
):
"""Compute a simple 1D upper envelope on a given common grid.

Expand Down
4 changes: 2 additions & 2 deletions src/upper_envelope/jax/fues_jax/check_and_scan_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def forward_scan(
n_points_to_scan: int,
is_scan_needed,
jump_thresh: float,
) -> Tuple[float, int]:
) -> tuple[float, int]:
"""Find next point on same value function as most recent point on upper envelope.

We use the forward scan to find the next point that lies on the same value
Expand Down Expand Up @@ -322,7 +322,7 @@ def backward_scan(
n_points_to_scan: int,
is_scan_needed,
jump_thresh: float,
) -> Tuple[float, int]:
) -> tuple[float, int]:
"""Find previous point on same value function as idx_to_scan_from.

We use the backward scan to find the preceding point that lies on the same value
Expand Down
15 changes: 8 additions & 7 deletions src/upper_envelope/jax/fues_jax/fues_jax.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

"""

from collections.abc import Callable
from functools import partial
from typing import Callable, Dict, Optional, Tuple
from typing import Dict, Optional, Tuple

import jax
import jax.numpy as jnp
Expand Down Expand Up @@ -35,13 +36,13 @@ def fues_jax(
value: jnp.ndarray,
expected_value_zero_savings: jnp.ndarray | float,
value_function: Callable,
value_function_args: Optional[Tuple] = (),
value_function_kwargs: Optional[Dict] = None,
value_function_args: tuple | None = (),
value_function_kwargs: dict | None = None,
n_constrained_points_to_add=None,
n_final_wealth_grid=None,
jump_thresh=2,
n_points_to_scan=10,
) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
) -> tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
"""Drop suboptimal points and refines the endogenous grid, policy, and value.

Computes the upper envelope over the overlapping segments of the
Expand Down Expand Up @@ -185,7 +186,7 @@ def fues_jax_unconstrained(
n_final_wealth_grid=None,
jump_thresh=2,
n_points_to_scan=10,
) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
) -> tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
"""Remove suboptimal points from the endogenous grid, policy, and value function.

Args:
Expand Down Expand Up @@ -249,8 +250,8 @@ def scan_value_function(
expected_value_zero_savings,
n_final_wealth_grid: int,
jump_thresh: float,
n_points_to_scan: Optional[int] = 0,
) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
n_points_to_scan: int | None = 0,
) -> tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
"""Scan the value function to remove suboptimal points and add intersection points.

Args:
Expand Down
3 changes: 1 addition & 2 deletions src/upper_envelope/math_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def _find_linear_intersection(
y3: float | jnp.ndarray,
x4: float | jnp.ndarray,
y4: float | jnp.ndarray,
) -> Tuple[float | jnp.ndarray, float | jnp.ndarray]:
) -> tuple[float | jnp.ndarray, float | jnp.ndarray]:
"""Find the linear intersection of two lines.

Args:
Expand All @@ -176,7 +176,6 @@ def _find_linear_intersection(
tuple: x and y coordinates of the intersection point.

"""

slope1 = calc_gradient(x1, y1, x2, y2)
slope2 = calc_gradient(x3, y3, x4, y4)

Expand Down
2 changes: 1 addition & 1 deletion src/upper_envelope/numba/drued_jorg_numba.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable
from collections.abc import Callable

import numpy as np
from numba import njit
Expand Down
Loading
Loading