Skip to content

Fix nonconformable matrices in TPI.py#1058

Merged
rickecon merged 4 commits intoPSLmodels:masterfrom
rickecon:fixtpi
Sep 11, 2025
Merged

Fix nonconformable matrices in TPI.py#1058
rickecon merged 4 commits intoPSLmodels:masterfrom
rickecon:fixtpi

Conversation

@rickecon
Copy link
Copy Markdown
Member

This PR:

cc: @jdebacker

@rickecon
Copy link
Copy Markdown
Member Author

@jdebacker. I was running the baseline simulation in our (currently) private repository /SS_MaxBen on a new ss-maxben-dev conda environment that loaded ogcore==0.14.9. The steady-state ran beautifully and quickly, but I got the following error when the computation got into the time path iteration.

JUST SAVED SS output to  /Users/richardevans/Docs/Economics/OSE/Federal/CRFB2025/SS_MaxBen/code/OUTPUT_BASELINE_POSTOBBBA_tg1_40/SS/SS_vars.pkl
Government spending breakpoints are tG1: 40; and tG2: 256
Traceback (most recent call last):
  File "/Users/richardevans/Docs/Economics/OSE/Federal/CRFB2025/SS_MaxBen/./code/ss_maxben_rick.py", line 369, in <module>
    main()
  File "/Users/richardevans/Docs/Economics/OSE/Federal/CRFB2025/SS_MaxBen/./code/ss_maxben_rick.py", line 189, in main
    runner(p, time_path=True, client=client)
  File "/opt/anaconda3/envs/ss-maxben-dev/lib/python3.12/site-packages/ogcore/execute.py", line 69, in runner
    tpi_output = TPI.run_TPI(p, client=client)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/anaconda3/envs/ss-maxben-dev/lib/python3.12/site-packages/ogcore/TPI.py", line 886, in run_TPI
    sales_tax_mat = (p.tau_c[: p.T, :] * p_i).reshape(
                     ~~~~~~~~~~~~~~~~~~^~~~~
ValueError: operands could not be broadcast together with shapes (320,1) (400,1)

So next I wanted to see if this was specific to the SS_MaxBen repository runs or if it was also happening in the OG-Core run scripts. SoAfter the creation of OG-Core version 0.14.9, I built a new ogcore-dev conda environment and ran the example script run_ogusa_example.py. Right after the steady-state computation in the baseline, I got the following identical error.

JUST SAVED SS output to  /Users/richardevans/Docs/Economics/OSE/OG/OG-Core/examples/OG-Core-Example/OUTPUT_BASELINE/SS/SS_vars.pkl
Government spending breakpoints are tG1: 20; and tG2: 256
Traceback (most recent call last):
  File "/Users/richardevans/Docs/Economics/OSE/OG/OG-Core/./examples/run_ogcore_example.py", line 138, in <module>
    main()
    ~~~~^^
  File "/Users/richardevans/Docs/Economics/OSE/OG/OG-Core/./examples/run_ogcore_example.py", line 80, in main
    runner(p, time_path=True, client=client)
    ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/richardevans/Docs/Economics/OSE/OG/OG-Core/ogcore/execute.py", line 69, in runner
    tpi_output = TPI.run_TPI(p, client=client)
  File "/Users/richardevans/Docs/Economics/OSE/OG/OG-Core/ogcore/TPI.py", line 886, in run_TPI
    sales_tax_mat = (p.tau_c[: p.T, :] * p_i).reshape(
                     ~~~~~~~~~~~~~~~~~~^~~~~
ValueError: operands could not be broadcast together with shapes (320,1) (400,1) 

The change to TPI.py in this PR fixes this error.

@rickecon
Copy link
Copy Markdown
Member Author

rickecon commented Sep 11, 2025

@jdebacker. I just ran the run_ogcore_example.py run script on my local machine, and the baseline and reform steady-states and transition paths both ran perfectly.

BASELINE STEADY-STATE SOLUTION (very quick)

GE loop errors = ['2.322e-14', '2.135e-14', '1.237e-12', '0.000e+00', '2.329e-13', '1.238e-14', '-3.966e-15', '1.405e-14', '2.897e-13', '1.206e-14', '7.043e-16', '5.205e-15', '2.096e-14', '5.955e-14']
Iteration: 1  Distance: 1.395507834151389e-11
SS debt = 0.6252962397934748, 0.00794992146845252
IO: (1, 1), C: (1,)
Foreign debt holdings = 0.25011849591738994
Foreign capital holdings = 0.05620836283593522
resource constraint: [-1.15879528e-14]
Checking constraints on capital, labor, and consumption.
There were no violations of the constraints on labor supply.
There were no violations of the constraints on consumption.
Maximum error in labor FOC = 1.0658141036401503e-13
Maximum error in savings FOC = 1.176836406102666e-13
JUST SAVED SS output to  /Users/richardevans/Docs/Economics/OSE/OG/OG-Core/examples/OG-Core-Example/OUTPUT_BASELINE/SS/SS_vars.pkl

BASELINE TRANSITION PATH SOLUTION (24 min, 30.0 sec)

Maximum debt ratio:  1.623089202706317
w diff: 7.562077934952782e-07, -3.7450276324513254e-07
r diff: 5.8892526050291494e-08, -1.2326341539831143e-07
r_p diff: 8.935178777425001e-10, -1.3025323768178776e-07
p_m diff: 0.0, 0.0
BQ diff: 1.2743259007047536e-07, -1.727531616195488e-08
TR diff: 5.555568013959622e-08, -3.6277384272975777e-07
Iteration: 21
Distance: 7.501911761051849e-06
Max absolute value resource constraint error: 8.459743881805704e-07
Checking time path for violations of constraints.
Max Euler error, savings: 3.4737768217496523e-12
Max Euler error labor supply: 1.3353762540191383e-12
Time path iteration complete.
It took 1470.0193099975586 seconds to get that part done.
run time =  1470.0288062095642

REFORM STEADY-STATE SOLUTION (very quick)

GE loop errors = ['4.857e-17', '4.857e-17', '-4.441e-16', '0.000e+00', '-3.331e-16', '-1.041e-17', '-6.939e-18', '-2.082e-17', '-1.561e-17', '-1.735e-17', '-2.082e-17', '-3.469e-18', '-2.776e-17']
Iteration: 1  Distance: 8.460555395304066e-16
SS debt = 0.6228816990786425, 0.007919223364348076
IO: (1, 1), C: (1,)
Foreign debt holdings = 0.24915267963145704
Foreign capital holdings = 0.05260193167620883
resource constraint: [-2.3349378e-14]
Checking constraints on capital, labor, and consumption.
There were no violations of the constraints on labor supply.
There were no violations of the constraints on consumption.
Maximum error in labor FOC = 1.0702549957386509e-13
Maximum error in savings FOC = 9.303668946358812e-14
JUST SAVED SS output to  /Users/richardevans/Docs/Economics/OSE/OG/OG-Core/examples/OG-Core-Example/OUTPUT_REFORM/SS/SS_vars.pkl

REFORM TRANSITION PATH SOLUTION (27 min, 56.1 sec)

Maximum debt ratio:  1.4790841176512344
w diff: 7.448194998538327e-07, -4.7274221026682994e-07
r diff: 7.45354062151038e-08, -1.2010352863633944e-07
r_p diff: 1.715385472556008e-10, -1.2534476614284307e-07
p_m diff: 0.0, 0.0
BQ diff: 1.196840882580763e-07, -1.6932682098472207e-08
TR diff: 6.15183639862682e-08, -3.318398965890257e-07
Iteration: 21
Distance: 6.942822334127482e-06
Max absolute value resource constraint error: 8.089560778780369e-07
Checking time path for violations of constraints.
Max Euler error, savings: 4.681366405634435e-12
Max Euler error labor supply: 1.1830536550405668e-12
Time path iteration complete.
It took 1676.1279680728912 seconds to get that part done.
run time =  1676.1369531154633
total time was  3153.40030002594
Percentage changes in aggregates: Year                    Variable  2021  2022  2023  2024  2025  2026  2027  2028  2029  2030  2021-2030    SS
0                    GDP ($Y_t$) -0.17 -0.14 -0.22 -0.22 -0.21 -0.19 -0.16 -0.12 -0.10 -0.07      -0.16 -0.39
1            Consumption ($C_t$) -0.08  1.05  0.13 -0.13 -0.24 -0.32 -0.37 -0.42 -0.43 -0.44      -0.12 -0.72
2          Capital Stock ($K_t$) -0.37 -0.41 -0.66 -0.73 -0.75 -0.74 -0.71 -0.64 -0.60 -0.54      -0.62 -1.51
3                  Labor ($L_t$) -0.05  0.00  0.02  0.05  0.08  0.11  0.14  0.16  0.17  0.18       0.08  0.22
4     Real interest rate ($r_t$) -3.05 -2.94 -2.64 -2.52 -2.47 -2.45 -2.45 -2.58 -2.61 -2.65      -2.64 -1.40
5            Wage rate ($w_{t}$) -0.11 -0.14 -0.24 -0.27 -0.29 -0.30 -0.30 -0.28 -0.27 -0.25      -0.25 -0.61

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Sep 11, 2025

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 72.62%. Comparing base (e42d4e5) to head (e45819a).
⚠️ Report is 215 commits behind head on master.

Files with missing lines Patch % Lines
ogcore/TPI.py 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1058   +/-   ##
=======================================
  Coverage   72.62%   72.62%           
=======================================
  Files          21       21           
  Lines        5096     5096           
=======================================
  Hits         3701     3701           
  Misses       1395     1395           
Flag Coverage Δ
unittests 72.62% <50.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
ogcore/__init__.py 100.00% <100.00%> (ø)
ogcore/TPI.py 35.66% <0.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rickecon
Copy link
Copy Markdown
Member Author

rickecon commented Sep 11, 2025

My run of the SS_MaxBen baseline finished with ogcore==0.14.10, and the steady-state and time path solved beautifully.

BASELINE STEADY-STATE EQUILIBRIUM (solved quickly)

GE loop errors = ['1.173e-12', '2.778e-12', '-1.848e-11', '0.000e+00', '-3.306e-11', '8.101e-13', '7.857e-13', '6.215e-13', '6.991e-13', '6.423e-13', '9.589e-13', '-2.872e-14', '-1.729e-13', '-1.098e-13', '-6.988e-14', '-3.312e-12', '-4.394e-13']
Iteration: 1  Distance: 5.010880207628338e-11
SS debt = 1.8402980772041115, 0.012007511874253998
IO: (1, 1), C: (1,)
Foreign debt holdings = 0.7361192308816447
Foreign capital holdings = 0.8824829331658246
resource constraint: [-2.22111218e-12]
Checking constraints on capital, labor, and consumption.
There were no violations of the constraints on labor supply.
There were no violations of the constraints on consumption.
Maximum error in labor FOC = 3.0597746558669314e-13
Maximum error in savings FOC = 3.5743630277806915e-13
JUST SAVED SS output to  /Users/richardevans/Docs/Economics/OSE/Federal/CRFB2025/SS_MaxBen/code/OUTPUT_BASELINE_POSTOBBBA_tg1_40/SS/SS_vars.pkl

BASELINE TRANSITION PATH EQUILIBRIUM (55 min, 55.5 sec)

Maximum debt ratio:  2.550107637760425
w diff: 1.3985328355392568e-06, 2.3372076185523838e-07
r diff: -3.1319692367359764e-08, -1.8754644375701401e-07
r_p diff: -3.4049976796457493e-09, -8.762866073735998e-08
p_m diff: 0.0, 0.0
BQ diff: 1.1012975701524264e-07, -1.1367595775281514e-09
TR diff: 1.4320898161779283e-08, -9.533299638597725e-08
Iteration: 24
Distance: 6.767469040284342e-06
Max absolute value resource constraint error: 1.0644063151432626e-06
Checking time path for violations of constraints.
Max Euler error, savings: 9.921286014957786e-12
Max Euler error labor supply: 2.539302101922658e-12
Time path iteration complete.
It took 3351.974048614502 seconds to get that part done.
run time =  3355.515179157257
DY_tg1_40

@rickecon rickecon merged commit ab0497d into PSLmodels:master Sep 11, 2025
8 checks passed
@rickecon rickecon deleted the fixtpi branch September 11, 2025 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants