Skip to content

Commit

Permalink
Merge pull request #836 from rickecon/mactest
Browse files Browse the repository at this point in the history
Update build_and_test.yml GitHub Action
  • Loading branch information
jdebacker authored Jan 3, 2023
2 parents 60079d3 + c267b86 commit 86b8a3c
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 83 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and test [Python 3.8, 3.9, 3.10]
name: Build and test [Python 3.9, 3.10]

on: [push, pull_request]
# paths:
Expand All @@ -11,20 +11,22 @@ on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [3.8, 3.9, '3.10']
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ["3.9", "3.10"]

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
persist-credentials: false

- name: Setup Miniconda using Python ${{ matrix.python-version }}
uses: conda-incubator/setup-miniconda@v2
with:
auto-update-conda: true
activate-environment: ogcore-dev
environment-file: environment.yml
python-version: ${{ matrix.python-version }}
Expand All @@ -42,7 +44,7 @@ jobs:
run: |
pytest -m 'not local' --cov=./ --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v3
with:
files: ./coverage.xml
flags: unittests
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[![PSL cataloged](https://img.shields.io/badge/PSL-cataloged-a0a0a0.svg)](https://www.PSLmodels.org)
[![Python 3.7+](https://img.shields.io/badge/python-3.7%2B-blue.svg)](https://www.python.org/downloads/release/python-377/)
[![Python 3.9](https://img.shields.io/badge/python-3.9-blue.svg)](https://www.python.org/downloads/release/python-3916/)
[![Python 3.10](https://img.shields.io/badge/python-3.10-blue.svg)](https://www.python.org/downloads/release/python-3108/)
![example event parameter](https://github.com/PSLmodels/OG-Core/actions/workflows/build_and_test.yml/badge.svg?branch=master)
![example event parameter](https://github.com/PSLmodels/OG-Core/actions/workflows/deploy_docs.yml/badge.svg?branch=master)
![example event parameter](https://github.com/PSLmodels/OG-Core/actions/workflows/check_black.yml/badge.svg?branch=master)
[![Codecov](https://codecov.io/gh/PSLmodels/OG-Core/branch/master/graph/badge.svg)](https://codecov.io/gh/PSLmodels/OG-Core)

# OG-Core
Expand Down
137 changes: 98 additions & 39 deletions ogcore/SS.py
Original file line number Diff line number Diff line change
Expand Up @@ -1103,46 +1103,105 @@ def run_SS(p, client=None):
results
"""
# Create list of deviation factors for initial guesses of r and TR
dev_factor_list = [
[1.00, 1.0],
[0.95, 1.0],
[1.05, 1.0],
[0.90, 1.0],
[1.10, 1.0],
[0.85, 1.0],
[1.15, 1.0],
[0.80, 1.0],
[1.20, 1.0],
[0.75, 1.0],
[1.25, 1.0],
[0.70, 1.0],
[1.30, 1.0],
[1.00, 0.2],
[0.95, 0.2],
[1.05, 0.2],
[0.90, 0.2],
[1.10, 0.2],
[0.85, 0.2],
[1.15, 0.2],
[0.80, 0.2],
[1.20, 0.2],
[0.75, 0.2],
[1.25, 0.2],
[0.70, 0.2],
[1.30, 0.2],
[1.00, 0.6],
[0.95, 0.6],
[1.05, 0.6],
[0.90, 0.6],
[1.10, 0.6],
[0.85, 0.6],
[1.15, 0.6],
[0.80, 0.6],
[1.20, 0.6],
[0.75, 0.6],
[1.25, 0.6],
[0.70, 0.6],
[1.30, 0.6],
]

# For initial guesses of w, r, TR, and factor, we use values that
# are close to some steady state values.
if p.baseline:
r_p_guess = p.initial_guess_r_SS
rguess = p.initial_guess_r_SS
if p.use_zeta:
b_guess = np.ones((p.S, p.J)) * 0.0055
n_guess = np.ones((p.S, p.J)) * 0.4 * p.ltilde
else:
b_guess = np.ones((p.S, p.J)) * 0.07
n_guess = np.ones((p.S, p.J)) * 0.35 * p.ltilde
wguess = firm.get_w_from_r(rguess, p, "SS")
p_m_guess = np.ones(p.M)
TRguess = p.initial_guess_TR_SS
Yguess = TRguess / p.alpha_T[-1]
factorguess = p.initial_guess_factor_SS
BQguess = aggr.get_BQ(rguess, b_guess, None, p, "SS", False)
ss_params_baseline = (b_guess, n_guess, None, None, p, client)
if p.use_zeta:
BQguess = 0.12231465279007188
guesses = (
[r_p_guess, rguess, wguess]
+ list(p_m_guess)
+ [Yguess, BQguess, TRguess, factorguess]
)
else:
guesses = (
[r_p_guess, rguess, wguess]
+ list(p_m_guess)
+ [Yguess]
+ list(BQguess)
+ [TRguess, factorguess]
)
sol = opt.root(
SS_fsolve,
guesses,
args=ss_params_baseline,
method=p.SS_root_method,
tol=p.mindist_SS,
)
# Loop over initial guesses of r and TR until find a solution or until have
# gone through all guesses. This should usually solve in the first guess
SS_solved = False
k = 0
while not SS_solved and k < len(dev_factor_list) - 1:
for k, v in enumerate(dev_factor_list):
print(
"SS using initial guess factors for r and TR of",
v[0],
"and",
v[1],
", respectively.",
)
r_p_guess = v[0] * p.initial_guess_r_SS
rguess = v[0] * p.initial_guess_r_SS
if p.use_zeta:
b_guess = np.ones((p.S, p.J)) * 0.0055
n_guess = np.ones((p.S, p.J)) * 0.4 * p.ltilde
else:
b_guess = np.ones((p.S, p.J)) * 0.07
n_guess = np.ones((p.S, p.J)) * 0.35 * p.ltilde
wguess = firm.get_w_from_r(rguess, p, "SS")
p_m_guess = np.ones(p.M)
TRguess = v[1] * p.initial_guess_TR_SS
Yguess = TRguess / p.alpha_T[-1]
factorguess = p.initial_guess_factor_SS
BQguess = aggr.get_BQ(rguess, b_guess, None, p, "SS", False)
ss_params_baseline = (b_guess, n_guess, None, None, p, client)
if p.use_zeta:
BQguess = 0.12231465279007188
guesses = (
[r_p_guess, rguess, wguess]
+ list(p_m_guess)
+ [Yguess, BQguess, TRguess, factorguess]
)
else:
guesses = (
[r_p_guess, rguess, wguess]
+ list(p_m_guess)
+ [Yguess]
+ list(BQguess)
+ [TRguess, factorguess]
)
sol = opt.root(
SS_fsolve,
guesses,
args=ss_params_baseline,
method=p.SS_root_method,
tol=p.mindist_SS,
)
if sol.success:
SS_solved = True
break
if ENFORCE_SOLUTION_CHECKS and not sol.success:
raise RuntimeError("Steady state equilibrium not found")
r_p_ss = sol.x[0]
Expand Down Expand Up @@ -1173,8 +1232,8 @@ def run_SS(p, client=None):
)
else:
# Use the baseline solution to get starting values for the reform
baseline_ss_dir = os.path.join(p.baseline_dir, "SS", "SS_vars.pkl")
ss_solutions = utils.safe_read_pickle(baseline_ss_dir)
baseline_ss_path = os.path.join(p.baseline_dir, "SS", "SS_vars.pkl")
ss_solutions = utils.safe_read_pickle(baseline_ss_path)
if p.reform_use_baseline_solution:
# use baseline solution as starting values if dimensions match
try:
Expand Down
42 changes: 21 additions & 21 deletions tests/test_SS.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,23 +199,23 @@ def dask_client():
args7 = (bssmat, nssmat, None, None, p7, None)
expected7 = np.array(
[
-0.07223217833445712,
-0.07826441903781375,
3.0910147752085537,
3.507819685350171,
2.441318389339966,
3.013613722097677,
-0.07223291631036555,
-0.07826548741334771,
3.0911509552880334,
3.5079477421902254,
2.4414161493191413,
3.0137277396774236,
0.0,
1.5448561866177926,
-0.0005707287990899482,
-0.018804693852322394,
-0.018630470642212238,
0.007811498563349251,
0.011911389464209691,
0.008984373971821467,
-0.0029658076829370155,
0.1390370567956013,
0.034621510533743675,
1.5448512748680931,
-0.0005707403143014808,
-0.01880468553296961,
-0.018630471665521498,
0.007811485269807893,
0.011911373110717808,
0.00898435233878905,
-0.0029658077084825795,
0.13903661473812837,
0.03462075279248166,
]
)

Expand Down Expand Up @@ -279,7 +279,7 @@ def test_SS_fsolve(tmpdir, guesses, args, expected):
print("Test list = ", test_list)

assert np.allclose(
np.hstack(np.array(test_list)), np.array(expected), atol=1e-5
np.hstack(np.array(test_list)), np.array(expected), atol=5e-4
)


Expand Down Expand Up @@ -1011,7 +1011,7 @@ def test_euler_equation_solver(input_tuple, ubi_j, p, expected):
filename5 = "run_SS_reform.pkl"
param_updates6 = {
"use_zeta": True,
"initial_guess_r_SS": 0.065,
"initial_guess_r_SS": 0.06,
"initial_guess_TR_SS": 0.06,
}
filename6 = "run_SS_reform_use_zeta.pkl"
Expand Down Expand Up @@ -1145,8 +1145,8 @@ def test_run_SS(tmpdir, baseline, param_updates, filename, dask_client):
p_base.update_specifications(param_updates_base)
base_ss_outputs = SS.run_SS(p_base, client=dask_client)
utils.mkdirs(os.path.join(baseline_dir, "SS"))
ss_dir = os.path.join(baseline_dir, "SS", "SS_vars.pkl")
with open(ss_dir, "wb") as f:
ss_path = os.path.join(baseline_dir, "SS", "SS_vars.pkl")
with open(ss_path, "wb") as f:
pickle.dump(base_ss_outputs, f)
# now run specification for test
p = Specifications(
Expand All @@ -1163,4 +1163,4 @@ def test_run_SS(tmpdir, baseline, param_updates, filename, dask_client):
pass
for k, v in expected_dict.items():
print("Checking item = ", k)
assert np.allclose(test_dict[k], v, atol=1e-06)
assert np.allclose(test_dict[k], v, atol=5e-04)
14 changes: 7 additions & 7 deletions tests/test_TPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_twist_doughnut(file_inputs, file_outputs):
)
test_list = TPI.twist_doughnut(*input_tuple)
expected_list = utils.safe_read_pickle(file_outputs)
assert np.allclose(np.array(test_list), np.array(expected_list))
assert np.allclose(np.array(test_list), np.array(expected_list), atol=1e-5)


def test_inner_loop():
Expand Down Expand Up @@ -620,13 +620,13 @@ def test_run_TPI_extra(baseline, param_updates, filename, tmpdir, dask_client):

if p.baseline:
utils.mkdirs(os.path.join(p.baseline_dir, "SS"))
ss_dir = os.path.join(p.baseline_dir, "SS", "SS_vars.pkl")
with open(ss_dir, "wb") as f:
ss_path = os.path.join(p.baseline_dir, "SS", "SS_vars.pkl")
with open(ss_path, "wb") as f:
pickle.dump(ss_outputs, f)
else:
utils.mkdirs(os.path.join(p.output_base, "SS"))
ss_dir = os.path.join(p.output_base, "SS", "SS_vars.pkl")
with open(ss_dir, "wb") as f:
ss_path = os.path.join(p.output_base, "SS", "SS_vars.pkl")
with open(ss_path, "wb") as f:
pickle.dump(ss_outputs, f)

TPI.ENFORCE_SOLUTION_CHECKS = False
Expand All @@ -636,14 +636,14 @@ def test_run_TPI_extra(baseline, param_updates, filename, tmpdir, dask_client):
for k, v in expected_dict.items():
print("Checking ", k)
try:
print("Diff = ", np.abs(test_dict[k][: p.T] - v[: p.T]).max())
print("Diff = ", np.absolute(test_dict[k][: p.T] - v[: p.T]).max())
assert np.allclose(
test_dict[k][: p.T], v[: p.T], rtol=1e-04, atol=1e-04
)
except ValueError:
print(
"Diff = ",
np.abs(test_dict[k][: p.T, :, :] - v[: p.T, :, :]).max(),
np.absolute(test_dict[k][: p.T, :, :] - v[: p.T, :, :]).max(),
)
assert np.allclose(
test_dict[k][: p.T, :, :],
Expand Down
1 change: 0 additions & 1 deletion tests/test_firm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from curses.ascii import SP
from math import exp
import pytest
from ogcore import firm
Expand Down
Binary file modified tests/test_io_data/run_TPI_outputs_baseline_small_open_2.pkl
Binary file not shown.
Binary file modified tests/test_io_data/tax_func_estimate_outputs.pkl
Binary file not shown.
Binary file modified tests/test_io_data/tax_func_loop_outputs.pkl
Binary file not shown.
18 changes: 9 additions & 9 deletions tests/test_txfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,16 @@ def test_replace_outliers():
np.array(
[
2.27262567e-23,
6.52118581e-05,
2.58989255e-13,
5.79350547e-09,
3.37733466e-01,
7.99995110e-01,
9.14366888e-01,
6.52115408e-05,
2.58988624e-13,
5.79701075e-09,
3.39354147e-01,
7.60132613e-01,
9.14365331e-01,
9.02760087e-06,
9.02760087e-06,
3.37724438e-03,
7.99986083e-03,
3.39345119e-03,
7.60123585e-03,
9.02760087e-06,
]
),
Expand All @@ -191,7 +191,7 @@ def test_replace_outliers():
9.02760087e-06,
]
),
256983.45682508417,
256983.45682508394,
152900,
)
expected_tuple_linear = (0.26135747, 0.0, 152900)
Expand Down

0 comments on commit 86b8a3c

Please sign in to comment.