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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- `demographics.get_pop_objs` now starts the population projection from the last year of population data. The population data have one more year than the fertility, mortality, and immigration rates (T0 + 1 years with user-provided data, T0 + 2 with UN data, whose rates run from `start_data_year` to `final_data_year`), but the projection started from year T0 - 1. With UN data it replaced data year T0 with a step that applied the extrapolated rates (`mort_rates[-1]`, `imm_rates_orig[-1]`) where the rate arrays the model uses (`rho`, `imm_rates`) list the year T0 - 1 rates, so when mortality changed in the last data year the population path broke its own law of motion in that one period. In a Japan calibration (UN data, `initial_data_year = start_year - 1`) this caused a resource-constraint error of 1.5e-3 of GDP in period 2 of every transition; with the fix the law of motion holds to 1e-17 in every period before `fixper`, the period-2 error is gone (below 1e-6), and aggregates move by under 0.06 percent. User-provided data, whose rates cover T0 years, give the same path as before. Adds `test_pop_path_follows_its_law_of_motion`.

## [0.25.1] - 2026-11-22

### Changed
Expand Down
16 changes: 13 additions & 3 deletions ogcore/demographics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1775,10 +1775,20 @@ def get_pop_objs(

# Generate time path of the population distribution after final
# year of data
# pop_2D has one more year than the rates, which map each year into
# the next: T0 + 1 years with user-provided data, T0 + 2 with UN data
# (whose rates run from start_data_year to final_data_year). The
# projection starts from the last data year. Starting it from
# pop_2D[T0 - 1] would, with UN data, replace data year T0 with a step
# that applies the extrapolated rates (mort_rates[-1],
# imm_rates_orig[-1]) where the rate arrays the model uses list the
# period-(T0 - 1) rates. When mortality changes in the last data year,
# the population then breaks its own law of motion in that period,
# which shows up as a resource-constraint error in the transition.
omega_path_lev = np.zeros((T + S, E + S))
pop_curr = pop_2D[T0 - 1, :]
omega_path_lev[:T0, :] = pop_2D[:T0, :]
for per in range(T0, T + S):
pop_curr = pop_2D[len_pop_dist - 1, :]
omega_path_lev[:len_pop_dist, :] = pop_2D
for per in range(len_pop_dist, T + S):
pop_next = np.dot(OMEGA_orig, pop_curr)
omega_path_lev[per, :] = pop_next.copy()
pop_curr = pop_next.copy()
Expand Down
45 changes: 45 additions & 0 deletions tests/test_demographics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,3 +1247,48 @@ def test_cli_show_reports_expiry(
out = capsys.readouterr().out
assert expected in out
assert token not in out


def test_pop_path_follows_its_law_of_motion(monkeypatch):
"""
The population path must satisfy the law of motion implied by the rate
arrays the model uses in every period before the fixper adjustment,
including the first projected period. With UN data the rates cover
T0 + 1 years and the population T0 + 2, so mortality can change in the
last data year; the projection must start from the last data year
rather than replace it with a step that uses the extrapolated rates.
Mimics the UN path with T0 = 2 (rates for 3 years, population for 4),
mortality falling in the last year.
"""
E, S = 20, 80
T = int(round(4.0 * S))
start_year = 2024
fert_3 = np.vstack((fert_rates, fert_rates[-1:, :]))
mort_3 = np.vstack((mort_rates, mort_rates[-1:, :] * 0.95))
infmort_3 = np.append(infmort_rates, infmort_rates[-1])
pop_4 = np.vstack((pop_dist, pop_dist[-1:, :] * 0.997))

monkeypatch.setattr(demographics, "get_fert", lambda *a, **k: fert_3)
monkeypatch.setattr(
demographics, "get_mort", lambda *a, **k: (mort_3, infmort_3)
)
monkeypatch.setattr(demographics, "get_pop", lambda *a, **k: pop_4)
pop_dict = demographics.get_pop_objs(
E,
S,
T,
0,
99,
initial_data_year=start_year - 1,
final_data_year=start_year,
GraphDiag=False,
)
omega = pop_dict["omega"].sum(axis=-1)
rho = pop_dict["rho"][:, :, 0]
imm = pop_dict["imm_rates"][:, :, 0]
g_n = pop_dict["g_n"]
fixper = int(1.5 * S)
for t in range(fixper - 2):
lhs = omega[t + 1, 1:] * (1 + g_n[t])
rhs = omega[t, :-1] * (1 - rho[t, :-1]) + imm[t, 1:] * omega[t, 1:]
assert np.allclose(lhs, rhs, rtol=0, atol=1e-12), t
Loading