Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor example models #56

Merged
merged 28 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
39a271d
Refactor example models
timmens Feb 28, 2024
7ee660c
Do not make unnecessary ignores
timmens Feb 28, 2024
b6c9eb1
Fix tests
timmens Feb 28, 2024
2053bce
Change no. of grid points back to previous level
timmens Feb 28, 2024
9a09376
Fix typo in examples/README.md
timmens Feb 28, 2024
1102de4
Update examples/README.md
timmens Feb 28, 2024
d721fcb
Update examples/long_running.py
timmens Feb 28, 2024
20cdd61
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 28, 2024
fc4812d
Update examples/long_running.py
timmens Feb 28, 2024
53411d9
Rename sport -> exericse
timmens Feb 28, 2024
7f6acef
Merge branch 'cleanup-models' of https://github.com/OpenSourceEconomi…
timmens Feb 28, 2024
dcaf4a6
Implement suggestions from review
timmens Feb 28, 2024
8769ad0
Reference issue #30
timmens Feb 28, 2024
249c75c
Add reference to DC-EGM paper
timmens Feb 28, 2024
a61c5dc
Rename delta -> disutility_of_work
timmens Feb 28, 2024
0a94247
Rename delta -> disutility_of_work throughout
timmens Feb 28, 2024
456bb79
Remove dependency of get_model from test_regression_test.py
timmens Feb 28, 2024
a3005c4
Remove get_models.py module
timmens Feb 28, 2024
4f75885
Update example model
timmens Feb 28, 2024
e4ebed3
Rename phelps_deaton -> deterministic
timmens Feb 28, 2024
21a05f9
Implement requested changes and correct typo
timmens Feb 29, 2024
bd8e90f
Improve todos
timmens Feb 29, 2024
fb4a1e4
Use labor_income in stochastic models
timmens Feb 29, 2024
a797bf3
Fix test_process_model.py
timmens Feb 29, 2024
435636d
Fix test_analytical_solution.py
timmens Feb 29, 2024
36b383f
Rename utility -> base_model_utility in tests
timmens Feb 29, 2024
efe11cb
Remove MODELS dictionary in test_entry_point.py
timmens Feb 29, 2024
48e6070
Update examples README
timmens Feb 29, 2024
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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ repos:
- id: name-tests-test
args:
- --pytest-test-first
exclude: ^tests/test_models/
- id: no-commit-to-branch
args:
- --branch
Expand Down
5 changes: 3 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ exclude *.yml
exclude *.pickle
exclude pytask.ini

prune src/lcm/sandbox
prune .envs
prune examples
prune docs
prune src/lcm/sandbox
prune tests
prune .envs

global-exclude __pycache__
global-exclude *.py[co]
Expand Down
22 changes: 22 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Example model specifications

## Choosing an example

| Example name | Description | Runtime |
| -------------- | ------------------------------------------------- | ------------- |
| `long_running` | Consumption-savings model with health and leisure | a few minutes |

## Running an example

Say you want to solve the [`long_running`](./long_running.py) example locally. In a
Python shell, execute:

```python
from lcm.entry_point import get_lcm_function

from long_running import MODEL_CONFIG, PARAMS


solve_model, _ = get_lcm_function(model=MODEL_CONFIG)
vf_arr = solve_model(PARAMS)
```
117 changes: 117 additions & 0 deletions examples/long_running.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""Example specification for a consumption-savings model with health and leisure."""

import jax.numpy as jnp

# ======================================================================================
# Numerical parameters and constants
# ======================================================================================
N_GRID_POINTS = {
"wealth": 100,
"health": 100,
"consumption": 100,
"exercise": 200,
}

RETIREMENT_AGE = 65

# ======================================================================================
# Model functions
# ======================================================================================


# --------------------------------------------------------------------------------------
# Utility function
# --------------------------------------------------------------------------------------
def utility(consumption, working, health, exercise, disutility_of_work):
return jnp.log(consumption) - (disutility_of_work - health) * working - exercise


# --------------------------------------------------------------------------------------
# Auxiliary variables
# --------------------------------------------------------------------------------------
def labor_income(wage, working):
return wage * working


def working(leisure):
return 1 - leisure


def wage(age):
return 1 + 0.1 * age


def age(_period):
return _period + 18


# --------------------------------------------------------------------------------------
# State transitions
# --------------------------------------------------------------------------------------
def next_wealth(wealth, consumption, labor_income, interest_rate):
return (1 + interest_rate) * (wealth + labor_income - consumption)


def next_health(health, exercise, working):
return health * (1 + exercise - working / 2)


# --------------------------------------------------------------------------------------
# Constraints
# --------------------------------------------------------------------------------------
def consumption_constraint(consumption, wealth, labor_income):
return consumption <= wealth + labor_income


# ======================================================================================
# Model specification and parameters
# ======================================================================================

MODEL_CONFIG = {
"functions": {
"utility": utility,
"next_wealth": next_wealth,
"next_health": next_health,
"consumption_constraint": consumption_constraint,
"labor_income": labor_income,
"working": working,
"wage": wage,
"age": age,
},
"choices": {
"leisure": {"options": [0, 1]},
"consumption": {
"grid_type": "linspace",
"start": 1,
"stop": 100,
"n_points": N_GRID_POINTS["consumption"],
},
"exercise": {
"grid_type": "linspace",
"start": 0,
"stop": 1,
"n_points": N_GRID_POINTS["exercise"],
},
},
"states": {
"wealth": {
"grid_type": "linspace",
"start": 1,
"stop": 100,
"n_points": N_GRID_POINTS["wealth"],
},
"health": {
"grid_type": "linspace",
"start": 0,
"stop": 1,
"n_points": N_GRID_POINTS["health"],
},
},
"n_periods": RETIREMENT_AGE - 18,
}

PARAMS = {
"beta": 0.95,
"utility": {"disutility_of_work": 0.05},
"next_wealth": {"interest_rate": 0.05},
}
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ write_to = "src/lcm/_version.py"
[tool.ruff]
target-version = "py311"
fix = true

[tool.ruff.lint]
select = ["ALL"]
extend-ignore = [
# missing type annotation
Expand Down Expand Up @@ -47,6 +49,9 @@ extend-ignore = [
# use of `assert` detected
"S101",

# `pickle` module is unsafe
"S301",

# Private member accessed: `_stochastic_info`
"SLF001",

Expand All @@ -57,6 +62,7 @@ extend-ignore = [
[tool.ruff.lint.per-file-ignores]
"docs/source/conf.py" = ["E501", "ERA001", "DTZ005"]
"tests/test_*.py" = ["PLR2004"]
"examples/*" = ["INP001"]

[tool.ruff.lint.pydocstyle]
convention = "google"
Expand Down
2 changes: 1 addition & 1 deletion src/lcm/_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from pathlib import Path

TEST_DATA_PATH = Path(__file__).parent.parent.parent.resolve().joinpath("tests", "data")
TEST_DATA = Path(__file__).parent.parent.parent.resolve().joinpath("tests", "data")
169 changes: 0 additions & 169 deletions src/lcm/example_models/basic_example_models.py

This file was deleted.

Loading
Loading