From a200bcb4bf924f2333a0382eaab461b7cd7e7b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Sun, 29 Oct 2023 18:02:02 +0100 Subject: [PATCH 01/35] Add tests --- test/runtests.jl | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 293c774..54088c3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -52,15 +52,28 @@ using Test # test unused variables let eqs = @equations begin - Y = C + Y = C + end + @test_throws ErrorException model( + endos = @variables(Y, C, G), + exos = @variables(), + params = @variables(), + eqs = eqs + ) end + + # test future indices @test_throws ErrorException model( - endos = @variables(Y, C, G), - exos = @variables(), - params = @variables(), - eqs = eqs + eqs = @equations begin + Y = Y[1] + end + ) + @test_throws ErrorException model( + exos = @variables(G), + eqs = @equations begin + Y = G[1] + end ) - end end @testset "Verbose" begin From 1c3d128cc2914453d0cbebeb18611128d4172cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Sun, 29 Oct 2023 18:02:18 +0100 Subject: [PATCH 02/35] New model --- src/models/GROWTH.jl | 138 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 src/models/GROWTH.jl diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl new file mode 100644 index 0000000..21c1e02 --- /dev/null +++ b/src/models/GROWTH.jl @@ -0,0 +1,138 @@ +@equations begin + Yk = Ske + INke - INk[-1], # 11.1 : Real output + Ske = β * Sk + (1 - β) * Sk[-1] * (1 + GRpr + RA), # 11.2 : Expenditure + INke = INk[-1] + γ * (INkt - INk[-1]), # 11.3 : Investment expenditure + INkt = σₜ * Ske, # 11.4 : Intended investment expenditure + INk = INk[-1] + Yk - Sk - NPL / UC, # 11.5 : Capital goods demand + Kk = Kk[-1] * (1 + GRk), # 11.6 : Capital stock + GRk = γ₀ + γᵤ * U[-1] - γᵣ * RRl, # 11.7 : Growth rate of capital stock + U = Yk / Kk[-1], # 11.8 : Capacity utilization + RRl = ((1 + Rl) / (1 + PI)) - 1, # 11.9 : Real rate of interest + PI = (P - P[-1]) / P[-1], # 11.10 : Inflation rate + Sk = Ck + Gk + Ik, # 11.11 : Aggregate supply + S = Sk * P, # 11.12 : Nominal supply + IN = INk * UC, # 11.13 : Nominal investment + INV = Ik * P, # 11.14 : Nominal inventory investment + K = Kk * P, # 11.15 : Nominal capital stock + Y = Sk * P + (INk - INk[-1]) * UC, # 11.16 : Nominal output + ωₜ = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z3 * (1 - ER) - z4 * BANDₜ + z5 * BANDᵦ)), # 11.17 : Real exchange rate + ER = N[-1] / Nfe[-1], # 11.18 : Exchange rate + z3a = ER > (1 - BANDᵦ) ? 1 : 0, # 11.19a : Upper band activation + z3b = ER <= (1 + BANDₜ) ? 1 : 0, # 11.19b : Lower band activation + z3 = z3a * z3b, # 11.20 : Band activation + z4 = ER > (1 + BANDₜ) ? 1 : 0, # 11.21 : Upper band overshoot + z5 = ER < (1 - BANDᵦ) ? 1 : 0, # 11.22 : Lower band undershoot + W = W[-1] + ω₃ * (ωₜ * P[-1] - W[-1]), # 11.23 : Wage adjustment + PR = PR[-1] * (1 + GRpr), # 11.24 : Productivity + Nt = Yk / PR, # 11.25 : Employment target + N = N[-1] + ηₙ * (Nt - N[-1]), # 11.26 : Employment + WB = N * W, # 11.27 : Wage bill + UC = WB / Yk, # 11.28 : Unit labor costs + NUC = W / PR, # 11.29 : Normal unit costs + NHUC = (1 - σₙ) * NUC + σₙ * (1 + Rln[-1]) * NUC[-1], # 11.30 : Historical unit costs + P = (1 + ϕ) * NHUC, # 11.31 : Price level + ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]), # 11.32 : Markup adjustment + ϕₜ = (FUft + FDf + Rl[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * S) # 11.33 : Target markup + Lfd = Lfd[-1] + FDf - P * INV / (1 + Rl), # 11.34 : Funding need for firm debt + FDf = ϵ₃ * (P * INV - Lfd[-1] * (1 + Rl)), # 11.35 : Change in funding need + Lft = Lf - FUft, # 11.36 : Target for liquid assets of firms + FUf = Lft - FUft, # 11.37 : Funding need for liquid assets + Lf = ϵ₄ * (P * S - W * N - T), # 11.38 : Liquid assets of firms + T = τ * Y, # 11.39 : Taxes + Ck = θ * (W * N + DIV - T + TR), # 11.40 : Consumption out of income + Ik = (1 - θ) * (W * N + DIV - T + TR), # 11.41 : Investment out of income + TR = TRt * Y, # 11.42 : Transfers + DIV = πt * Y, # 11.43 : Dividends + Gk = Gt * Y, # 11.44 : Government consumption + Rl = max(0, Rb + πt - θₑ * (πt - πtₑ)), # 11.45 : Lending rate + πtₑ = ϵ₅ * πt + (1 - ϵ₅) * πtₑ, # 11.46 : Expected inflation + Rb = Rbₜ * (1 + ψ * (D / Y - Dₜ / Y)), # 11.47 : Base rate + D = Lfd + Lh, # 11.48 : Total debt + Lh = Lh[-1] + Ch - Ph * IH / (1 + Rh), # 11.49 : Funding need for household debt + Ch = ϵ₆ * (Ph * IH - Lh[-1] * (1 + Rh)), # 11.50 : Change in funding need for households + Ht = H - FUht, # 11.51 : Target for liquid assets of households + FUh = Ht - FUht, # 11.52 : Funding need for liquid assets of households + H = ϵ₇ * (Ph * Ck + Ph * Gk - WB - T), # 11.53 : Liquid assets of households + Rh = max(0, Rb + πt - θₘ * (πt - πtₘ)), # 11.54 : Household lending rate + πtₘ = ϵ₈ * πt + (1 - ϵ₈) * πtₘ, # 11.55 : Expected inflation for households + IH = ι * H, # 11.56 : Housing investment + Ph = max(0, Ph[-1] + ϵ₉ * (UC * Yk - Ph[-1] * IH)), # 11.57 : House price + UC = α * (Ck + Gk) + β * IH + (1 - α - β) * Yk, # 11.58 : Utilized capacity + Yk = Ske + INke - INk[-1], # 11.59 : Real output + INk = δ * K, # 11.60 : Capital depreciation + Ske = min(Sk, Dk / P), # 11.61 : Effective supply + Sk = ξ * Yk, # 11.62 : Supply + Dk = Lfd + Ch + FUf + FUh + Ph * IH, # 11.63 : Demand for credit + K = K[-1] + Ik - INk, # 11.64 : Capital stock + WB = W * N, # 11.65 : Wage bill + W = Wp * P, # 11.66 : Nominal wage + Wp = Wp[-1] * (1 + λ * (πt - πtₑ)), # 11.67 : Real wage target + πt = (P - P[-1]) / P[-1], # 11.68 : Inflation + P = P[-1] * (1 + μ * (UC - 1)), # 11.69 : Price level + N = Ns, # 11.70 : Employment + Ns = Yk / A, # 11.71 : Supply of labor + A = A[-1] * (1 + g), # 11.72 : Labor productivity + g = gₜ * (1 + χ * (UC - 1)), # 11.73 : Growth rate of productivity + # Box 11.9 : Governments equations + # --------------------------------- + G = Gk * P, # 11.71 : Pure government expenditures + Gk = Gk[-1] * (1 + GRg), # 11.72 : Real government expenditures + PSBR = G + BLs[-1] + Rb[-1] * (Bbs[-1] + Bhs[-1]) - TX, # 11.73 : Government deficit --> BLs[-1] missing in the book + # 11.74 : New issues of bills + Bs = Bs[-1] + G - TX - (BLs - BLs[-1]) * Pbl + Rb[-1] * (Bhs[-1] + Bbs[-1]) + BLs[-1], + GD = Bbs + Bhs + BLs * Pbl + Hs, # 11.75 : Government debt + + # Box 11.10 : The Central banks equations + # ---------------------------------------- + Fcb = Rb[-1] * Bcbd[-1], # 11.76 : Central bank profits + BLs = BLd, # 11.77 : Bonds are supplied on demand + Bhs = Bhd, # 11.78 : Household bills supplied on demand + Hhs = Hhd, # 11.79 : Cash supplied on demand + Hbs = Hbd, # 11.80 : Reserves supplied on demand + Hs = Hbs + Hhs, # 11.81 : Total supply of cash + Bcbd = Hs, # 11.82 : Central bank + Bcbs = Bcbd, # 11.83 : Supply of bills to Central bank + Rb = Rb̄, # 11.84 : Interest rate on bills set exogenously + Rbl = Rb + ADDbl, # 11.85 : Long term interest rate + Pbl = 1 / Rbl, # 11.86 : Price of long-term bonds + + # Box 11.11 : Commercial Banks equations + # --------------------------------------- + Ms = Mh, # 11.87 : Bank deposits supplied on demand + Lfs = Lfd, # 11.88 : Loans to firms supplied on demand + Lhs = Lhd, # 11.89 : Personal loans supplied on demand + Hbd = ρ * Ms, # 11.90 : Reserve requirements of banks + # 11.91 : Bills supplied to banks + Bbs = Bbs[-1] + (Bs - Bs[-1]) - (Bhs - Bhs[-1]) - (Bcbs - Bcbs[-1]), + # 11.92 : Balance sheet constraint of banks + Bbd = Ms + OFb - Lfs - Lhs - Hbd, + BLR = Bbd / Ms, # 11.93 : Bank liquidity ratio + # 11.94 : Deposit interest rate + Rm = Rm[-1] + z1a * xīm1 + z1b * xīm2 - z2a * xīm1 - z2b * xīm2, + # 11.95-97 : Mechanism for determining changes to the interest rate on deposits + z2a = if BLR[-1] > (top + .05) 1 else 0 end, + z2b = if BLR[-1] > top 1 else 0 end, + z1a = if BLR[-1] ≤ bot 1 else 0 end, + z1b = if BLR[-1] ≤ (bot - .05) 1 else 0 end, + + # Box 11.12 : Commercial banks equations + # --------------------------------------- + Rl = Rm + ADDl, # 11.98 : Loan interest rate + OFbt = NCAR * (Lfs[-1] + Lhs[-1]), # 11.99 : Long-run own funds target + OFbe = OFb[-1] + β * (OFbt - OFb[-1]), # 11.100 : Short-run own funds target + FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1], # 11.101 : Target retained earnings of banks + NPLke = εb * NPLke[-1] + (1 - εb) * NPLk[-1], # 11.102 : Expected proportion of non-performing loans + FDb = Fb - FUb, # 11.103 : Dividends of banks + Fbt = λb * Y[-1] + (OFbe - OFb[-1] + NPLke * Lfs[-1]), # 11.104 : Target profits of banks + # 11.105 : Actual profits of banks + Fb = Rl[-1] * (Lfs[-1] + Lhs[-1] - NPL) + Rb[-1] * Bbd[-1] - Rm[-1] * Ms[-1], + # 11.106 : Lending mark-up over deposit rate + ADDl = (Fbt - Rb[-1] * Bbd[-1] + Rm[-1] * (Ms[-1] - (1 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1 - NPLke) * Lfs[-1] + Lhs[-1]), + FUb = Fb - λb * Y[-1], # 11.107 : Actual retained earnings + OFb = OFb[-1] + FUb - NPL, # 11.108 : Own funds of banks + CAR = OFb / (Lfs + Lhs), + + Vf = IN + K - Lfd - Ekd * Pe, # Firm's wealth (memo for matrices) + # Vg = -Bs - BLs * Pbl, # Government's wealth (memo for matrices) + Ls = Lfs + Lhs, # Loans supply (memo for matrices) +end From 82828b3edd0a0d10dc8b754a02a24cc127cbd2b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Sun, 29 Oct 2023 20:38:40 +0100 Subject: [PATCH 03/35] Extended GROWTH --- src/models/GROWTH.jl | 494 +++++++++++++++++++++++++++++++------------ 1 file changed, 363 insertions(+), 131 deletions(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 21c1e02..eb04e89 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -1,138 +1,370 @@ -@equations begin - Yk = Ske + INke - INk[-1], # 11.1 : Real output - Ske = β * Sk + (1 - β) * Sk[-1] * (1 + GRpr + RA), # 11.2 : Expenditure - INke = INk[-1] + γ * (INkt - INk[-1]), # 11.3 : Investment expenditure - INkt = σₜ * Ske, # 11.4 : Intended investment expenditure - INk = INk[-1] + Yk - Sk - NPL / UC, # 11.5 : Capital goods demand - Kk = Kk[-1] * (1 + GRk), # 11.6 : Capital stock - GRk = γ₀ + γᵤ * U[-1] - γᵣ * RRl, # 11.7 : Growth rate of capital stock - U = Yk / Kk[-1], # 11.8 : Capacity utilization - RRl = ((1 + Rl) / (1 + PI)) - 1, # 11.9 : Real rate of interest - PI = (P - P[-1]) / P[-1], # 11.10 : Inflation rate - Sk = Ck + Gk + Ik, # 11.11 : Aggregate supply - S = Sk * P, # 11.12 : Nominal supply - IN = INk * UC, # 11.13 : Nominal investment - INV = Ik * P, # 11.14 : Nominal inventory investment - K = Kk * P, # 11.15 : Nominal capital stock - Y = Sk * P + (INk - INk[-1]) * UC, # 11.16 : Nominal output - ωₜ = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z3 * (1 - ER) - z4 * BANDₜ + z5 * BANDᵦ)), # 11.17 : Real exchange rate - ER = N[-1] / Nfe[-1], # 11.18 : Exchange rate - z3a = ER > (1 - BANDᵦ) ? 1 : 0, # 11.19a : Upper band activation - z3b = ER <= (1 + BANDₜ) ? 1 : 0, # 11.19b : Lower band activation - z3 = z3a * z3b, # 11.20 : Band activation - z4 = ER > (1 + BANDₜ) ? 1 : 0, # 11.21 : Upper band overshoot - z5 = ER < (1 - BANDᵦ) ? 1 : 0, # 11.22 : Lower band undershoot - W = W[-1] + ω₃ * (ωₜ * P[-1] - W[-1]), # 11.23 : Wage adjustment - PR = PR[-1] * (1 + GRpr), # 11.24 : Productivity - Nt = Yk / PR, # 11.25 : Employment target - N = N[-1] + ηₙ * (Nt - N[-1]), # 11.26 : Employment - WB = N * W, # 11.27 : Wage bill - UC = WB / Yk, # 11.28 : Unit labor costs - NUC = W / PR, # 11.29 : Normal unit costs - NHUC = (1 - σₙ) * NUC + σₙ * (1 + Rln[-1]) * NUC[-1], # 11.30 : Historical unit costs - P = (1 + ϕ) * NHUC, # 11.31 : Price level - ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]), # 11.32 : Markup adjustment - ϕₜ = (FUft + FDf + Rl[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * S) # 11.33 : Target markup - Lfd = Lfd[-1] + FDf - P * INV / (1 + Rl), # 11.34 : Funding need for firm debt - FDf = ϵ₃ * (P * INV - Lfd[-1] * (1 + Rl)), # 11.35 : Change in funding need - Lft = Lf - FUft, # 11.36 : Target for liquid assets of firms - FUf = Lft - FUft, # 11.37 : Funding need for liquid assets - Lf = ϵ₄ * (P * S - W * N - T), # 11.38 : Liquid assets of firms - T = τ * Y, # 11.39 : Taxes - Ck = θ * (W * N + DIV - T + TR), # 11.40 : Consumption out of income - Ik = (1 - θ) * (W * N + DIV - T + TR), # 11.41 : Investment out of income - TR = TRt * Y, # 11.42 : Transfers - DIV = πt * Y, # 11.43 : Dividends - Gk = Gt * Y, # 11.44 : Government consumption - Rl = max(0, Rb + πt - θₑ * (πt - πtₑ)), # 11.45 : Lending rate - πtₑ = ϵ₅ * πt + (1 - ϵ₅) * πtₑ, # 11.46 : Expected inflation - Rb = Rbₜ * (1 + ψ * (D / Y - Dₜ / Y)), # 11.47 : Base rate - D = Lfd + Lh, # 11.48 : Total debt - Lh = Lh[-1] + Ch - Ph * IH / (1 + Rh), # 11.49 : Funding need for household debt - Ch = ϵ₆ * (Ph * IH - Lh[-1] * (1 + Rh)), # 11.50 : Change in funding need for households - Ht = H - FUht, # 11.51 : Target for liquid assets of households - FUh = Ht - FUht, # 11.52 : Funding need for liquid assets of households - H = ϵ₇ * (Ph * Ck + Ph * Gk - WB - T), # 11.53 : Liquid assets of households - Rh = max(0, Rb + πt - θₘ * (πt - πtₘ)), # 11.54 : Household lending rate - πtₘ = ϵ₈ * πt + (1 - ϵ₈) * πtₘ, # 11.55 : Expected inflation for households - IH = ι * H, # 11.56 : Housing investment - Ph = max(0, Ph[-1] + ϵ₉ * (UC * Yk - Ph[-1] * IH)), # 11.57 : House price - UC = α * (Ck + Gk) + β * IH + (1 - α - β) * Yk, # 11.58 : Utilized capacity - Yk = Ske + INke - INk[-1], # 11.59 : Real output - INk = δ * K, # 11.60 : Capital depreciation - Ske = min(Sk, Dk / P), # 11.61 : Effective supply - Sk = ξ * Yk, # 11.62 : Supply - Dk = Lfd + Ch + FUf + FUh + Ph * IH, # 11.63 : Demand for credit - K = K[-1] + Ik - INk, # 11.64 : Capital stock - WB = W * N, # 11.65 : Wage bill - W = Wp * P, # 11.66 : Nominal wage - Wp = Wp[-1] * (1 + λ * (πt - πtₑ)), # 11.67 : Real wage target - πt = (P - P[-1]) / P[-1], # 11.68 : Inflation - P = P[-1] * (1 + μ * (UC - 1)), # 11.69 : Price level - N = Ns, # 11.70 : Employment - Ns = Yk / A, # 11.71 : Supply of labor - A = A[-1] * (1 + g), # 11.72 : Labor productivity - g = gₜ * (1 + χ * (UC - 1)), # 11.73 : Growth rate of productivity - # Box 11.9 : Governments equations - # --------------------------------- - G = Gk * P, # 11.71 : Pure government expenditures - Gk = Gk[-1] * (1 + GRg), # 11.72 : Real government expenditures - PSBR = G + BLs[-1] + Rb[-1] * (Bbs[-1] + Bhs[-1]) - TX, # 11.73 : Government deficit --> BLs[-1] missing in the book - # 11.74 : New issues of bills - Bs = Bs[-1] + G - TX - (BLs - BLs[-1]) * Pbl + Rb[-1] * (Bhs[-1] + Bbs[-1]) + BLs[-1], - GD = Bbs + Bhs + BLs * Pbl + Hs, # 11.75 : Government debt +# Box 11.1: Aggregation equations +aggr = @equations begin + Yk = Ske + INke - INk[-1] # 11.1: Real output + Ske = β * Sk + (1 - β) * Sk[-1] * (1 + GRpr + RA) # 11.2: Expected real sales + INke = INk[-1] + γ * (INkt - INk[-1]) # 11.3: Long-run inventory target + INkt = σₜ * Ske # 11.4: Short-run inventory target + INk = INk[-1] + Yk - Sk - NPL / UC # 11.5: Actual real inventories + Kk = Kk[-1] * (1 + GRk) # 11.6: Real capital stock + GRk = γ₀ + γᵤ * U[-1] - γᵣ * RRl # 11.7: Growth of real capital stock + U = Yk / Kk[-1] # 11.8: Capital utilization proxy + RRl = ((1 + Rl) / (1 + PI)) - 1 # 11.9: Real interest rate on loans + PI = (P - P[-1]) / P[-1] # 11.10: Rate of price inflation + Ik = (Kk - Kk[-1]) + δ * Kk[-1] # 11.11 : Real gross investment +end + +# Box 11.2: Firms equations +firms1 = @equations begin + Sk = Ck + Gk + Ik # 11.12: Actual real sales + S = Sk * P # 11.13: Value of realized sales + IN = INk * UC # 11.14: Inventories valued at current cost + INV = Ik * P # 11.15: Nominal gross investment + K = Kk * P # 11.16: Nominal value of fixed capital + Y = Sk * P + (INk - INk[-1]) * UC # 11.17 : Nominal GDP +end + +# Box 11.3: Firms equations +firms2 = @equations begin + ωₜ = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z3 * (1 - ER) - z4 * BANDₜ + z5 * BANDᵦ)) # 11.17: Real exchange rate + ER = N[-1] / Nfe[-1] # 11.18: Exchange rate + z3a = ER > (1 - BANDᵦ) ? 1: 0 # 11.19a: Upper band activation + z3b = ER <= (1 + BANDₜ) ? 1: 0 # 11.19b: Lower band activation + z3 = z3a * z3b # 11.20: Band activation + z4 = ER > (1 + BANDₜ) ? 1: 0 # 11.21: Upper band overshoot + z5 = ER < (1 - BANDᵦ) ? 1: 0 # 11.22: Lower band undershoot + W = W[-1] + ω₃ * (ωₜ * P[-1] - W[-1]) # 11.23: Wage adjustment + PR = PR[-1] * (1 + GRpr) # 11.24: Productivity + Nt = Yk / PR # 11.25: Employment target + N = N[-1] + ηₙ * (Nt - N[-1]) # 11.26: Employment + WB = N * W # 11.27: Wage bill + UC = WB / Yk # 11.28: Unit labor costs + NUC = W / PR # 11.29: Normal unit costs + NHUC = (1 - σₙ) * NUC + σₙ * (1 + Rln[-1]) * NUC[-1] # 11.30: Historical unit costs + P = (1 + ϕ) * NHUC # 11.31: Price level + ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]) # 11.32: Markup adjustment + ϕₜ = (FUft + FDf + Rl[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * S) # 11.33: Target markup + Lfd = Lfd[-1] + FDf - P * INV / (1 + Rl) # 11.34: Funding need for firm debt + FDf = ϵ₃ * (P * INV - Lfd[-1] * (1 + Rl)) # 11.35: Change in funding need + Lft = Lf - FUft # 11.36: Target for liquid assets of firms + FUf = Lft - FUft # 11.37: Funding need for liquid assets + Lf = ϵ₄ * (P * S - W * N - T) # 11.38: Liquid assets of firms + T = τ * Y # 11.39: Taxes + Ck = θ * (W * N + DIV - T + TR) # 11.40: Consumption out of income + Ik = (1 - θ) * (W * N + DIV - T + TR) # 11.41: Investment out of income + TR = TRt * Y # 11.42: Transfers + DIV = πt * Y # 11.43: Dividends + Gk = Gt * Y # 11.44: Government consumption + Rl = max(0, Rb + πt - θₑ * (πt - πtₑ)) # 11.45: Lending rate + πtₑ = ϵ₅ * πt + (1 - ϵ₅) * πtₑ # 11.46: Expected inflation + Rb = Rbₜ * (1 + ψ * (D / Y - Dₜ / Y)) # 11.47: Base rate + D = Lfd + Lh # 11.48: Total debt + Lh = Lh[-1] + Ch - Ph * IH / (1 + Rh) # 11.49: Funding need for household debt + Ch = ϵ₆ * (Ph * IH - Lh[-1] * (1 + Rh)) # 11.50: Change in funding need for households + Ht = H - FUht # 11.51: Target for liquid assets of households + FUh = Ht - FUht # 11.52: Funding need for liquid assets of households + H = ϵ₇ * (Ph * Ck + Ph * Gk - WB - T) # 11.53: Liquid assets of households + Rh = max(0, Rb + πt - θₘ * (πt - πtₘ)) # 11.54: Household lending rate + πtₘ = ϵ₈ * πt + (1 - ϵ₈) * πtₘ # 11.55: Expected inflation for households + IH = ι * H # 11.56: Housing investment + Ph = max(0, Ph[-1] + ϵ₉ * (UC * Yk - Ph[-1] * IH)) # 11.57: House price + UC = α * (Ck + Gk) + β * IH + (1 - α - β) * Yk # 11.58: Utilized capacity + Yk = Ske + INke - INk[-1] # 11.59: Real output + INk = δ * K # 11.60: Capital depreciation + Ske = min(Sk, Dk / P) # 11.61: Effective supply + Sk = ξ * Yk # 11.62: Supply + Dk = Lfd + Ch + FUf + FUh + Ph * IH # 11.63: Demand for credit + K = K[-1] + Ik - INk # 11.64: Capital stock + WB = W * N # 11.65: Wage bill + W = Wp * P # 11.66: Nominal wage + Wp = Wp[-1] * (1 + λ * (πt - πtₑ)) # 11.67: Real wage target + πt = (P - P[-1]) / P[-1] # 11.68: Inflation + P = P[-1] * (1 + μ * (UC - 1)) # 11.69: Price level + N = Ns # 11.70: Employment + Ns = Yk / A # 11.71: Supply of labor + A = A[-1] * (1 + g) # 11.72: Labor productivity + g = gₜ * (1 + χ * (UC - 1)) # 11.73: Growth rate of productivity +end - # Box 11.10 : The Central banks equations +# Box 11.9: Governments equations +gov = @equations begin + G = Gk * P # 11.71: Pure government expenditures + Gk = Gk[-1] * (1 + GRg) # 11.72: Real government expenditures + PSBR = G + BLs[-1] + Rb[-1] * (Bbs[-1] + Bhs[-1]) - TX # 11.73: Government deficit --> BLs[-1] missing in the book + # 11.74: New issues of bills + Bs = Bs[-1] + G - TX - (BLs - BLs[-1]) * Pbl + Rb[-1] * (Bhs[-1] + Bbs[-1]) + BLs[-1] + GD = Bbs + Bhs + BLs * Pbl + Hs # 11.75: Government debt +end + +# Box 11.10: The Central banks equations +cb = @equations begin # ---------------------------------------- - Fcb = Rb[-1] * Bcbd[-1], # 11.76 : Central bank profits - BLs = BLd, # 11.77 : Bonds are supplied on demand - Bhs = Bhd, # 11.78 : Household bills supplied on demand - Hhs = Hhd, # 11.79 : Cash supplied on demand - Hbs = Hbd, # 11.80 : Reserves supplied on demand - Hs = Hbs + Hhs, # 11.81 : Total supply of cash - Bcbd = Hs, # 11.82 : Central bank - Bcbs = Bcbd, # 11.83 : Supply of bills to Central bank - Rb = Rb̄, # 11.84 : Interest rate on bills set exogenously - Rbl = Rb + ADDbl, # 11.85 : Long term interest rate - Pbl = 1 / Rbl, # 11.86 : Price of long-term bonds + Fcb = Rb[-1] * Bcbd[-1] # 11.76: Central bank profits + BLs = BLd # 11.77: Bonds are supplied on demand + Bhs = Bhd # 11.78: Household bills supplied on demand + Hhs = Hhd # 11.79: Cash supplied on demand + Hbs = Hbd # 11.80: Reserves supplied on demand + Hs = Hbs + Hhs # 11.81: Total supply of cash + Bcbd = Hs # 11.82: Central bank + Bcbs = Bcbd # 11.83: Supply of bills to Central bank + Rb = Rb̄ # 11.84: Interest rate on bills set exogenously + Rbl = Rb + ADDbl # 11.85: Long term interest rate + Pbl = 1 / Rbl # 11.86: Price of long-term bonds +end - # Box 11.11 : Commercial Banks equations +# Box 11.11: Commercial Banks equations +banks1 = @equations begin # --------------------------------------- - Ms = Mh, # 11.87 : Bank deposits supplied on demand - Lfs = Lfd, # 11.88 : Loans to firms supplied on demand - Lhs = Lhd, # 11.89 : Personal loans supplied on demand - Hbd = ρ * Ms, # 11.90 : Reserve requirements of banks - # 11.91 : Bills supplied to banks - Bbs = Bbs[-1] + (Bs - Bs[-1]) - (Bhs - Bhs[-1]) - (Bcbs - Bcbs[-1]), - # 11.92 : Balance sheet constraint of banks - Bbd = Ms + OFb - Lfs - Lhs - Hbd, - BLR = Bbd / Ms, # 11.93 : Bank liquidity ratio - # 11.94 : Deposit interest rate - Rm = Rm[-1] + z1a * xīm1 + z1b * xīm2 - z2a * xīm1 - z2b * xīm2, - # 11.95-97 : Mechanism for determining changes to the interest rate on deposits - z2a = if BLR[-1] > (top + .05) 1 else 0 end, - z2b = if BLR[-1] > top 1 else 0 end, - z1a = if BLR[-1] ≤ bot 1 else 0 end, - z1b = if BLR[-1] ≤ (bot - .05) 1 else 0 end, + Ms = Mh # 11.87: Bank deposits supplied on demand + Lfs = Lfd # 11.88: Loans to firms supplied on demand + Lhs = Lhd # 11.89: Personal loans supplied on demand + Hbd = ρ * Ms # 11.90: Reserve requirements of banks + # 11.91: Bills supplied to banks + Bbs = Bbs[-1] + (Bs - Bs[-1]) - (Bhs - Bhs[-1]) - (Bcbs - Bcbs[-1]) + # 11.92: Balance sheet constraint of banks + Bbd = Ms + OFb - Lfs - Lhs - Hbd + BLR = Bbd / Ms # 11.93: Bank liquidity ratio + # 11.94: Deposit interest rate + Rm = Rm[-1] + z1a * xīm1 + z1b * xīm2 - z2a * xīm1 - z2b * xīm2 + # 11.95-97: Mechanism for determining changes to the interest rate on deposits + z2a = if BLR[-1] > (top + 0.05) + 1 + else + 0 + end + z2b = if BLR[-1] > top + 1 + else + 0 + end + z1a = if BLR[-1] ≤ bot + 1 + else + 0 + end + z1b = if BLR[-1] ≤ (bot - 0.05) + 1 + else + 0 + end +end - # Box 11.12 : Commercial banks equations +# Box 11.12: Commercial banks equations +banks2 = @equations begin # --------------------------------------- - Rl = Rm + ADDl, # 11.98 : Loan interest rate - OFbt = NCAR * (Lfs[-1] + Lhs[-1]), # 11.99 : Long-run own funds target - OFbe = OFb[-1] + β * (OFbt - OFb[-1]), # 11.100 : Short-run own funds target - FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1], # 11.101 : Target retained earnings of banks - NPLke = εb * NPLke[-1] + (1 - εb) * NPLk[-1], # 11.102 : Expected proportion of non-performing loans - FDb = Fb - FUb, # 11.103 : Dividends of banks - Fbt = λb * Y[-1] + (OFbe - OFb[-1] + NPLke * Lfs[-1]), # 11.104 : Target profits of banks - # 11.105 : Actual profits of banks - Fb = Rl[-1] * (Lfs[-1] + Lhs[-1] - NPL) + Rb[-1] * Bbd[-1] - Rm[-1] * Ms[-1], - # 11.106 : Lending mark-up over deposit rate - ADDl = (Fbt - Rb[-1] * Bbd[-1] + Rm[-1] * (Ms[-1] - (1 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1 - NPLke) * Lfs[-1] + Lhs[-1]), - FUb = Fb - λb * Y[-1], # 11.107 : Actual retained earnings - OFb = OFb[-1] + FUb - NPL, # 11.108 : Own funds of banks - CAR = OFb / (Lfs + Lhs), - - Vf = IN + K - Lfd - Ekd * Pe, # Firm's wealth (memo for matrices) - # Vg = -Bs - BLs * Pbl, # Government's wealth (memo for matrices) - Ls = Lfs + Lhs, # Loans supply (memo for matrices) + Rl = Rm + ADDl # 11.98: Loan interest rate + OFbt = NCAR * (Lfs[-1] + Lhs[-1]) # 11.99: Long-run own funds target + OFbe = OFb[-1] + β * (OFbt - OFb[-1]) # 11.100: Short-run own funds target + FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1] # 11.101: Target retained earnings of banks + NPLke = εb * NPLke[-1] + (1 - εb) * NPLk[-1] # 11.102: Expected proportion of non-performing loans + FDb = Fb - FUb # 11.103: Dividends of banks + Fbt = λb * Y[-1] + (OFbe - OFb[-1] + NPLke * Lfs[-1]) # 11.104: Target profits of banks + # 11.105: Actual profits of banks + Fb = Rl[-1] * (Lfs[-1] + Lhs[-1] - NPL) + Rb[-1] * Bbd[-1] - Rm[-1] * Ms[-1] + # 11.106: Lending mark-up over deposit rate + ADDl = (Fbt - Rb[-1] * Bbd[-1] + Rm[-1] * (Ms[-1] - (1 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1 - NPLke) * Lfs[-1] + Lhs[-1]) + FUb = Fb - λb * Y[-1] # 11.107: Actual retained earnings + OFb = OFb[-1] + FUb - NPL # 11.108: Own funds of banks + CAR = OFb / (Lfs + Lhs) + + Vf = IN + K - Lfd - Ekd * Pe # Firm's wealth (memo for matrices) + # Vg = -Bs - BLs * Pbl # Government's wealth (memo for matrices) + Ls = Lfs + Lhs # Loans supply (memo for matrices) +end + +param_dict = @parameters begin + alpha1 = 0.75 + alpha2 = 0.064 + beta = 0.5 + betab = 0.4 + gamma = 0.15 + gamma0 = 0.00122 + gammar = 0.1 + gammau = 0.05 + delta = 0.10667 + deltarep = 0.1 + eps = 0.5 + eps2 = 0.8 + epsb = 0.25 + epsrb = 0.9 + eta0 = 0.07416 + etan = 0.6 + etar = 0.4 + theta = 0.22844 + # lambda10 = -0.17071 + # lambda11 = 0 + # lambda12 = 0 + # lambda13 = 0 + # lambda14 = 0 + # lambda15 = 0.18 + lambda20 = 0.25 + lambda21 = 2.2 + lambda22 = 6.6 + lambda23 = 2.2 + lambda24 = 2.2 + lambda25 = 0.1 + lambda30 = -0.04341 + lambda31 = 2.2 + lambda32 = 2.2 + lambda33 = 6.6 + lambda34 = 2.2 + lambda35 = 0.1 + lambda40 = 0.67132 + lambda41 = 2.2 + lambda42 = 2.2 + lambda43 = 2.2 + lambda44 = 6.6 + lambda45 = 0.1 + lambdab = 0.0153 + lambdac = 0.05 + xim1 = 0.0008 + xim2 = 0.0007 + ro = 0.05 + sigman = 0.1666 + sigmat = 0.2 + psid = 0.15255 + psiu = 0.92 + omega0 = -0.20594 + omega1 = 1 + omega2 = 2 + omega3 = 0.45621 end + +initial = @equations begin + sigmase = 0.16667 + eta = 0.04918 + phi = 0.26417 + phit = 0.26417 + + ADDbl = 0.02 + BANDt = 0.01 + BANDb = 0.01 + bot = 0.05 + GRg = 0.03 + GRpr = 0.03 + Nfe = 87.181 + NCAR = 0.1 + NPLk = 0.02 + Rbbar = 0.035 + Rln = 0.07 + RA = 0 + top = 0.12 + + + ADDl = 0.04592 + BLR = 0.1091 + BUR = 0.06324 + Ck = 7334240 + CAR = 0.09245 + CONS = 52603100 + ER = 1 + Fb = 1744130 + Fbt = 1744140 + Ff = 18081100 + Fft = 18013600 + FDb = 1325090 + FDf = 2670970 + FUb = 419039 + FUf = 15153800 + FUft = 15066200 + G = 16755600 + Gk = 2336160 + GL = 2775900 + GRk = 0.03001 + INV = 16911600 + Ik = 2357910 + N = 87.181 + Nt = 87.181 + NHUC = 5.6735 + NL = 683593 + NLk = 95311 + NPL = 309158 + NPLke = 0.02 + NUC = 5.6106 + omegat = 112852 + P = 7.1723 + Pbl = 18.182 + Pe = 17937 + PE = 5.07185 + PI = 0.0026 + PR = 138659 + PSBR = 1894780 + Q = 0.77443 + Rb = 0.035 + Rbl = 0.055 + Rk = 0.03008 + Rl = 0.06522 + Rm = 0.0193 + REP = 2092310 + #RRb = 0.03232 + RRl = 0.06246 + S = 86270300 + Sk = 12028300 + Ske = 12028300 + TX = 17024100 + U = 0.70073 + UC = 5.6106 + W = 777968 + WB = 67824000 + Y = 86607700 + Yk = 12088400 + YDr = 56446400 + YDkr = 7813270 + YDkre = 7813290 + YP = 73158700 + z1a = 0 + z1b = 0 + z2a = 0 + z2b = 0 + + ## + + #Bbd = 4388930 + #Bbs = 4388930 + Bbd = 4389790 + Bbs = 4389790 + Bcbd = 4655690 + Bcbs = 4655690 + Bhd = 33439320 + Bhs = 33439320 + #Bhd = 33396900 + #Bhs = 33396900 + Bs = 42484800 + #Bs = 42441520 + BLd = 840742 + BLs = 840742 + GD = 57728700 + Ekd = 5112.6001 + Eks = 5112.6001 + Hbd = 2025540 + Hbs = 2025540 + Hhd = 2630150 + Hhs = 2630150 + Hs = 4655690 + IN = 11585400 + INk = 2064890 + INke = 2405660 + INkt = 2064890 + #K = 127444000 + K = 127486471 + #Kk = 17768900 + Kk = 17774838 + Lfd = 15962900 + Lfs = 15962900 + Lhd = 21606600 + Lhs = 21606600 + Ls = 37569500 + #Md = 40510800 + Mh = 40510800 + Ms = 40510800 + OFb = 3474030 + OFbe = 3474030 + #OFb = 3473280 + #OFbe = 3782430 + OFbt = 3638100 + #V = 165395000 + V = 165438779 + #Vfma = 159291000 + Vfma = 159334599 + Vk = 23066350 + Vf = 31361792 +end \ No newline at end of file From ed47b51512c0e4b30b65fadf3a9fee56183c201a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Sun, 29 Oct 2023 22:17:04 +0100 Subject: [PATCH 04/35] Extended GROWTH --- src/models/GROWTH.jl | 226 ++++++++++++++++++++----------------------- 1 file changed, 107 insertions(+), 119 deletions(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index eb04e89..8d3bf39 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -10,7 +10,7 @@ aggr = @equations begin U = Yk / Kk[-1] # 11.8: Capital utilization proxy RRl = ((1 + Rl) / (1 + PI)) - 1 # 11.9: Real interest rate on loans PI = (P - P[-1]) / P[-1] # 11.10: Rate of price inflation - Ik = (Kk - Kk[-1]) + δ * Kk[-1] # 11.11 : Real gross investment + Ik = (Kk - Kk[-1]) + δ * Kk[-1] # 11.11: Real gross investment end # Box 11.2: Firms equations @@ -20,69 +20,60 @@ firms1 = @equations begin IN = INk * UC # 11.14: Inventories valued at current cost INV = Ik * P # 11.15: Nominal gross investment K = Kk * P # 11.16: Nominal value of fixed capital - Y = Sk * P + (INk - INk[-1]) * UC # 11.17 : Nominal GDP + Y = Sk * P + (INk - INk[-1]) * UC # 11.17: Nominal GDP end # Box 11.3: Firms equations firms2 = @equations begin - ωₜ = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z3 * (1 - ER) - z4 * BANDₜ + z5 * BANDᵦ)) # 11.17: Real exchange rate - ER = N[-1] / Nfe[-1] # 11.18: Exchange rate - z3a = ER > (1 - BANDᵦ) ? 1: 0 # 11.19a: Upper band activation - z3b = ER <= (1 + BANDₜ) ? 1: 0 # 11.19b: Lower band activation - z3 = z3a * z3b # 11.20: Band activation - z4 = ER > (1 + BANDₜ) ? 1: 0 # 11.21: Upper band overshoot - z5 = ER < (1 - BANDᵦ) ? 1: 0 # 11.22: Lower band undershoot - W = W[-1] + ω₃ * (ωₜ * P[-1] - W[-1]) # 11.23: Wage adjustment - PR = PR[-1] * (1 + GRpr) # 11.24: Productivity - Nt = Yk / PR # 11.25: Employment target - N = N[-1] + ηₙ * (Nt - N[-1]) # 11.26: Employment - WB = N * W # 11.27: Wage bill - UC = WB / Yk # 11.28: Unit labor costs - NUC = W / PR # 11.29: Normal unit costs - NHUC = (1 - σₙ) * NUC + σₙ * (1 + Rln[-1]) * NUC[-1] # 11.30: Historical unit costs - P = (1 + ϕ) * NHUC # 11.31: Price level - ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]) # 11.32: Markup adjustment - ϕₜ = (FUft + FDf + Rl[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * S) # 11.33: Target markup - Lfd = Lfd[-1] + FDf - P * INV / (1 + Rl) # 11.34: Funding need for firm debt - FDf = ϵ₃ * (P * INV - Lfd[-1] * (1 + Rl)) # 11.35: Change in funding need - Lft = Lf - FUft # 11.36: Target for liquid assets of firms - FUf = Lft - FUft # 11.37: Funding need for liquid assets - Lf = ϵ₄ * (P * S - W * N - T) # 11.38: Liquid assets of firms - T = τ * Y # 11.39: Taxes - Ck = θ * (W * N + DIV - T + TR) # 11.40: Consumption out of income - Ik = (1 - θ) * (W * N + DIV - T + TR) # 11.41: Investment out of income - TR = TRt * Y # 11.42: Transfers - DIV = πt * Y # 11.43: Dividends - Gk = Gt * Y # 11.44: Government consumption - Rl = max(0, Rb + πt - θₑ * (πt - πtₑ)) # 11.45: Lending rate - πtₑ = ϵ₅ * πt + (1 - ϵ₅) * πtₑ # 11.46: Expected inflation - Rb = Rbₜ * (1 + ψ * (D / Y - Dₜ / Y)) # 11.47: Base rate - D = Lfd + Lh # 11.48: Total debt - Lh = Lh[-1] + Ch - Ph * IH / (1 + Rh) # 11.49: Funding need for household debt - Ch = ϵ₆ * (Ph * IH - Lh[-1] * (1 + Rh)) # 11.50: Change in funding need for households - Ht = H - FUht # 11.51: Target for liquid assets of households - FUh = Ht - FUht # 11.52: Funding need for liquid assets of households - H = ϵ₇ * (Ph * Ck + Ph * Gk - WB - T) # 11.53: Liquid assets of households - Rh = max(0, Rb + πt - θₘ * (πt - πtₘ)) # 11.54: Household lending rate - πtₘ = ϵ₈ * πt + (1 - ϵ₈) * πtₘ # 11.55: Expected inflation for households - IH = ι * H # 11.56: Housing investment - Ph = max(0, Ph[-1] + ϵ₉ * (UC * Yk - Ph[-1] * IH)) # 11.57: House price - UC = α * (Ck + Gk) + β * IH + (1 - α - β) * Yk # 11.58: Utilized capacity - Yk = Ske + INke - INk[-1] # 11.59: Real output - INk = δ * K # 11.60: Capital depreciation - Ske = min(Sk, Dk / P) # 11.61: Effective supply - Sk = ξ * Yk # 11.62: Supply - Dk = Lfd + Ch + FUf + FUh + Ph * IH # 11.63: Demand for credit - K = K[-1] + Ik - INk # 11.64: Capital stock - WB = W * N # 11.65: Wage bill - W = Wp * P # 11.66: Nominal wage - Wp = Wp[-1] * (1 + λ * (πt - πtₑ)) # 11.67: Real wage target - πt = (P - P[-1]) / P[-1] # 11.68: Inflation - P = P[-1] * (1 + μ * (UC - 1)) # 11.69: Price level - N = Ns # 11.70: Employment - Ns = Yk / A # 11.71: Supply of labor - A = A[-1] * (1 + g) # 11.72: Labor productivity - g = gₜ * (1 + χ * (UC - 1)) # 11.73: Growth rate of productivity + ωₜ = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z3 * (1 - ER) - z4 * BANDₜ + z5 * BANDᵦ)) # 11.18: Real wage aspirations + ER = N[-1] / Nfe[-1] # 11.19: Employment rate + # 11.20: Switch variables + z3 = ((1 + BANDᵦ) < ER <= (1 + BANDₜ)) ? 1: 0 # Band activation + z4 = ER > (1 + BANDₜ) ? 1: 0 # Upper band overshoot + z5 = ER < (1 - BANDᵦ) ? 1: 0 # Lower band undershoot + W = W[-1] + ω₃ * (ωₜ * P[-1] - W[-1]) # 11.21: Nominal wage + PR = PR[-1] * (1 + GRpr) # 11.22: Labor productivity + Nt = Yk / PR # 11.23: Desired employment + N = N[-1] + ηₙ * (Nt - N[-1]) # 11.24: Actual employment --> etan not in the book + WB = N * W # 11.25: Nominal wage bill + UC = WB / Yk # 11.26: Actual unit cost + NUC = W / PR # 11.27: Normal unit cost + NHUC = (1 - σₙ) * NUC + σₙ * (1 + Rln[-1]) * NUC[-1] # 11.28: Normal historic unit cost +end + +# Box 11.4: Firms equations +firms3 = @equations begin + P = (1 + ϕ) * NHUC # 11.29: Normal-cost pricing + ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]) # 11.30: Actual mark-up --> ε₂ not in the book + ϕₜ = (FUft + FDf + Rl[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * S) # 11.32: Expected historical costs + HCe = (1 - σₛₑ) * Ske * UC + (1 + Rl[-1]) * σₛₑ * Ske * UC[-1] # 11.33: Opening inventories to expected sales ratio + σₛₑ = INk[-1] / Ske # 11.34: Planned entrepeneurial profits of firms + Fft = FUft + FDf + Rl[-1] * (Lfd[-1] - IN[-1]) # 11.34: Planned entrepeneurial profits of firms + FUft = ψᵤ * INV[-1] # 11.35: Planned retained earnings of firms + FDf = ψd * Ff[-1] # 11.36: Dividends of firms +end + +# Box 11.5: Firms equations +firms4 = @equations begin + Ff = S - WB + (IN - IN[-1]) - Rl[-1]*IN[-1] # 11.37: Realized entrepeneurial profits + FUf = Ff - FDf - Rl[-1]*(Lfd[-1] - IN[-1]) + Rl[-1]*NPL # 11.38: Retained earnings of firms + Lfd = Lfd[-1] + INV + (IN - IN[-1]) - FUf - (Eks - Eks[-1])*Pe - NPL # 11.39: Demand for loans by firms + NPL = NPLk * Lfs[-1] # 11.40: Defaulted loans + +end + +@equations begin + # 11.64: Demand for bills + Bhd = Vfma[-1] * (λ20 + λ22 * Rb[-1] - λ21 * Rm[-1] - λ24 * Rk[-1] - λ23 * Rbl[-1] - λ25 * (YDr / V)) + # 11.65: Demand for bonds + BLd = Vfma[-1] * (λ30 - λ32 * Rb[-1] - λ31 * Rm[-1] - λ34 * Rk[-1] + λ33 * Rbl[-1] - λ35 * (YDr / V)) / Pbl + # 11.66: Demand for equities - normalized to get the price of equitities + Pe = Vfma[-1] * (λ40 - λ42 * Rb[-1] - λ41 * Rm[-1] + λ44 * Rk[-1] - λ43 * Rbl[-1] - λ45 * (YDr / V)) / Ekd + Mh = Vfma - Bhd - Pe * Ekd - Pbl * BLd + Lhd # 11.67: Money deposits - as a residual + Vfma = V - Hhd - OFb # 11.68: Investible wealth + VfmaA = Mh + Bhd + Pbl * BLd + Pe * Ekd + Hhd = λc * CONS # 11.69: Households demand for cash + Ekd = Eks # 11.70: Stock market equilibrium end # Box 11.9: Governments equations @@ -172,65 +163,65 @@ banks2 = @equations begin end param_dict = @parameters begin - alpha1 = 0.75 - alpha2 = 0.064 - beta = 0.5 - betab = 0.4 - gamma = 0.15 - gamma0 = 0.00122 - gammar = 0.1 - gammau = 0.05 - delta = 0.10667 - deltarep = 0.1 - eps = 0.5 - eps2 = 0.8 - epsb = 0.25 - epsrb = 0.9 - eta0 = 0.07416 - etan = 0.6 - etar = 0.4 - theta = 0.22844 - # lambda10 = -0.17071 - # lambda11 = 0 - # lambda12 = 0 - # lambda13 = 0 - # lambda14 = 0 - # lambda15 = 0.18 - lambda20 = 0.25 - lambda21 = 2.2 - lambda22 = 6.6 - lambda23 = 2.2 - lambda24 = 2.2 - lambda25 = 0.1 - lambda30 = -0.04341 - lambda31 = 2.2 - lambda32 = 2.2 - lambda33 = 6.6 - lambda34 = 2.2 - lambda35 = 0.1 - lambda40 = 0.67132 - lambda41 = 2.2 - lambda42 = 2.2 - lambda43 = 2.2 - lambda44 = 6.6 - lambda45 = 0.1 - lambdab = 0.0153 - lambdac = 0.05 - xim1 = 0.0008 - xim2 = 0.0007 - ro = 0.05 - sigman = 0.1666 - sigmat = 0.2 - psid = 0.15255 - psiu = 0.92 - omega0 = -0.20594 - omega1 = 1 - omega2 = 2 - omega3 = 0.45621 + alpha1 = 0.75 + alpha2 = 0.064 + beta = 0.5 + betab = 0.4 + gamma = 0.15 + gamma0 = 0.00122 + gammar = 0.1 + gammau = 0.05 + delta = 0.10667 + deltarep = 0.1 + eps = 0.5 + eps2 = 0.8 + epsb = 0.25 + epsrb = 0.9 + eta0 = 0.07416 + etan = 0.6 + etar = 0.4 + theta = 0.22844 + # λ10 = -0.17071 + # λ11 = 0 + # λ12 = 0 + # λ13 = 0 + # λ14 = 0 + # λ15 = 0.18 + λ20 = 0.25 + λ21 = 2.2 + λ22 = 6.6 + λ23 = 2.2 + λ24 = 2.2 + λ25 = 0.1 + λ30 = -0.04341 + λ31 = 2.2 + λ32 = 2.2 + λ33 = 6.6 + λ34 = 2.2 + λ35 = 0.1 + λ40 = 0.67132 + λ41 = 2.2 + λ42 = 2.2 + λ43 = 2.2 + λ44 = 6.6 + λ45 = 0.1 + λb = 0.0153 + λc = 0.05 + xim1 = 0.0008 + xim2 = 0.0007 + ro = 0.05 + σn = 0.1666 + σt = 0.2 + psid = 0.15255 + psiu = 0.92 + omega0 = -0.20594 + omega1 = 1 + omega2 = 2 + omega3 = 0.45621 end initial = @equations begin - sigmase = 0.16667 + σse = 0.16667 eta = 0.04918 phi = 0.26417 phit = 0.26417 @@ -249,7 +240,6 @@ initial = @equations begin RA = 0 top = 0.12 - ADDl = 0.04592 BLR = 0.1091 BUR = 0.06324 @@ -316,8 +306,6 @@ initial = @equations begin z2a = 0 z2b = 0 - ## - #Bbd = 4388930 #Bbs = 4388930 Bbd = 4389790 From 2fe04b12f83f9adc7bb95ab2dfbd220ec029eef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Sun, 29 Oct 2023 22:26:36 +0100 Subject: [PATCH 05/35] Extended GROWTH --- src/models/GROWTH.jl | 78 ++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 8d3bf39..12656b5 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -28,13 +28,13 @@ firms2 = @equations begin ωₜ = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z3 * (1 - ER) - z4 * BANDₜ + z5 * BANDᵦ)) # 11.18: Real wage aspirations ER = N[-1] / Nfe[-1] # 11.19: Employment rate # 11.20: Switch variables - z3 = ((1 + BANDᵦ) < ER <= (1 + BANDₜ)) ? 1: 0 # Band activation - z4 = ER > (1 + BANDₜ) ? 1: 0 # Upper band overshoot - z5 = ER < (1 - BANDᵦ) ? 1: 0 # Lower band undershoot + z3 = ((1 + BANDᵦ) < ER <= (1 + BANDₜ)) ? 1 : 0 # Band activation + z4 = ER > (1 + BANDₜ) ? 1 : 0 # Upper band overshoot + z5 = ER < (1 - BANDᵦ) ? 1 : 0 # Lower band undershoot W = W[-1] + ω₃ * (ωₜ * P[-1] - W[-1]) # 11.21: Nominal wage PR = PR[-1] * (1 + GRpr) # 11.22: Labor productivity Nt = Yk / PR # 11.23: Desired employment - N = N[-1] + ηₙ * (Nt - N[-1]) # 11.24: Actual employment --> etan not in the book + N = N[-1] + ηₙ * (Nt - N[-1]) # 11.24: Actual employment --> ηn not in the book WB = N * W # 11.25: Nominal wage bill UC = WB / Yk # 11.26: Actual unit cost NUC = W / PR # 11.27: Normal unit cost @@ -49,20 +49,56 @@ firms3 = @equations begin HCe = (1 - σₛₑ) * Ske * UC + (1 + Rl[-1]) * σₛₑ * Ske * UC[-1] # 11.33: Opening inventories to expected sales ratio σₛₑ = INk[-1] / Ske # 11.34: Planned entrepeneurial profits of firms Fft = FUft + FDf + Rl[-1] * (Lfd[-1] - IN[-1]) # 11.34: Planned entrepeneurial profits of firms - FUft = ψᵤ * INV[-1] # 11.35: Planned retained earnings of firms + FUft = ψᵤ * INV[-1] # 11.35: Planned rηined earnings of firms FDf = ψd * Ff[-1] # 11.36: Dividends of firms end # Box 11.5: Firms equations firms4 = @equations begin - Ff = S - WB + (IN - IN[-1]) - Rl[-1]*IN[-1] # 11.37: Realized entrepeneurial profits - FUf = Ff - FDf - Rl[-1]*(Lfd[-1] - IN[-1]) + Rl[-1]*NPL # 11.38: Retained earnings of firms - Lfd = Lfd[-1] + INV + (IN - IN[-1]) - FUf - (Eks - Eks[-1])*Pe - NPL # 11.39: Demand for loans by firms + Ff = S - WB + (IN - IN[-1]) - Rl[-1] * IN[-1] # 11.37: Realized entrepeneurial profits + FUf = Ff - FDf - Rl[-1] * (Lfd[-1] - IN[-1]) + Rl[-1] * NPL # 11.38: Rηined earnings of firms + Lfd = Lfd[-1] + INV + (IN - IN[-1]) - FUf - (Eks - Eks[-1]) * Pe - NPL # 11.39: Demand for loans by firms NPL = NPLk * Lfs[-1] # 11.40: Defaulted loans + Eks = Eks[-1] + ((1 - ψᵤ) * INV[-1]) / Pe # 11.41: Supply of equities issued by firms + # Rk = FDf/(Pe[-1]*Ekd[-1]) # 11.42: Dividend yield of firms + Rk = FDf / (Pe[-1] * Ekd[-1]) + PE = Pe / (Ff / Eks[-1]) # 11.43: Price earnings ratio + Q = (Eks * Pe + Lfd) / (K + IN) # 11.44: Tobins Q ratio +end + +# Box 11.6: Households equations +hh1 = @equations begin + # YP = WB + FDf + FDb + Rm[-1]*Md[-1] + Rb[-1]*Bhd[-1] + BLs[-1] # 11.45: Personal income + YP = WB + FDf + FDb + Rm[-1] * Mh[-1] + Rb[-1] * Bhd[-1] + BLs[-1] + #YP = WB + FDf + FDb + Rm[-1]*Mh[-1] + Rb[-1]*Bhd[-1] + BLs[-1] + NL + TX = theta * YP # 11.46: Income taxes + YDr = YP - TX - Rl[-1] * Lhd[-1] # 11.47: Regular disposable income + YDhs = YDr + CG # 11.48: Haig-Simons disposable income + # 11.49: Capital gains + CG = (Pbl - Pbl[-1]) * BLd[-1] + (Pe - Pe[-1]) * Ekd[-1] + (OFb - OFb[-1]) + # 11.50: Wealth + V = V[-1] + YDr - CONS + (Pbl - Pbl[-1]) * BLd[-1] + (Pe - Pe[-1]) * Ekd[-1] + (OFb - OFb[-1]) + Vk = V / P # 11.51: Real staock of wealth + CONS = Ck * P # 11.52: Consumption + Ck = alpha1 * (YDkre + NLk) + alpha2 * Vk[-1] # 11.53: Real consumption + YDkre = eps * YDkr + (1 - eps) * (YDkr[-1] * (1 + GRpr)) # 11.54: Expected real regular disposable income + # YDkr = YDr/P - (P - P[-1])*Vk[-1]/P # 11.55: Real regular disposable income + YDkr = YDr / P - ((P - P[-1]) * Vk[-1]) / P +end +# Box 11.7: Households equations +hh2 = @equations begin + GL = η * YDr # 11.56: Gross amount of new personal loans ---> new η here + η = η₀ - ηr * RRl # 11.57: New loans to personal income ratio + NL = GL - REP # 11.58: Net amount of new personal loans + REP = deltarep * Lhd[-1] # 11.59: Personal loans repayments + Lhd = Lhd[-1] + GL - REP # 11.60: Demand for personal loans + NLk = NL / P # 11.61: Real amount of new personal loans + BUR = (REP + Rl[-1] * Lhd[-1]) / YDr[-1] # 11.62: Burden of personal debt end -@equations begin +# Box 11.8: Households equations - portfolio decisions +hh2 = @equations begin # 11.64: Demand for bills Bhd = Vfma[-1] * (λ20 + λ22 * Rb[-1] - λ21 * Rm[-1] - λ24 * Rk[-1] - λ23 * Rbl[-1] - λ25 * (YDr / V)) # 11.65: Demand for bonds @@ -71,7 +107,7 @@ end Pe = Vfma[-1] * (λ40 - λ42 * Rb[-1] - λ41 * Rm[-1] + λ44 * Rk[-1] - λ43 * Rbl[-1] - λ45 * (YDr / V)) / Ekd Mh = Vfma - Bhd - Pe * Ekd - Pbl * BLd + Lhd # 11.67: Money deposits - as a residual Vfma = V - Hhd - OFb # 11.68: Investible wealth - VfmaA = Mh + Bhd + Pbl * BLd + Pe * Ekd + VfmaA = Mh + Bhd + Pbl * BLd + Pe * Ekd Hhd = λc * CONS # 11.69: Households demand for cash Ekd = Eks # 11.70: Stock market equilibrium end @@ -145,7 +181,7 @@ banks2 = @equations begin Rl = Rm + ADDl # 11.98: Loan interest rate OFbt = NCAR * (Lfs[-1] + Lhs[-1]) # 11.99: Long-run own funds target OFbe = OFb[-1] + β * (OFbt - OFb[-1]) # 11.100: Short-run own funds target - FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1] # 11.101: Target retained earnings of banks + FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1] # 11.101: Target rηined earnings of banks NPLke = εb * NPLke[-1] + (1 - εb) * NPLk[-1] # 11.102: Expected proportion of non-performing loans FDb = Fb - FUb # 11.103: Dividends of banks Fbt = λb * Y[-1] + (OFbe - OFb[-1] + NPLke * Lfs[-1]) # 11.104: Target profits of banks @@ -153,7 +189,7 @@ banks2 = @equations begin Fb = Rl[-1] * (Lfs[-1] + Lhs[-1] - NPL) + Rb[-1] * Bbd[-1] - Rm[-1] * Ms[-1] # 11.106: Lending mark-up over deposit rate ADDl = (Fbt - Rb[-1] * Bbd[-1] + Rm[-1] * (Ms[-1] - (1 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1 - NPLke) * Lfs[-1] + Lhs[-1]) - FUb = Fb - λb * Y[-1] # 11.107: Actual retained earnings + FUb = Fb - λb * Y[-1] # 11.107: Actual rηined earnings OFb = OFb[-1] + FUb - NPL # 11.108: Own funds of banks CAR = OFb / (Lfs + Lhs) @@ -165,8 +201,8 @@ end param_dict = @parameters begin alpha1 = 0.75 alpha2 = 0.064 - beta = 0.5 - betab = 0.4 + bη = 0.5 + bηb = 0.4 gamma = 0.15 gamma0 = 0.00122 gammar = 0.1 @@ -177,10 +213,10 @@ param_dict = @parameters begin eps2 = 0.8 epsb = 0.25 epsrb = 0.9 - eta0 = 0.07416 - etan = 0.6 - etar = 0.4 - theta = 0.22844 + η0 = 0.07416 + ηn = 0.6 + ηr = 0.4 + thη = 0.22844 # λ10 = -0.17071 # λ11 = 0 # λ12 = 0 @@ -212,8 +248,8 @@ param_dict = @parameters begin ro = 0.05 σn = 0.1666 σt = 0.2 - psid = 0.15255 - psiu = 0.92 + ψd = 0.15255 + ψᵤ = 0.92 omega0 = -0.20594 omega1 = 1 omega2 = 2 @@ -222,7 +258,7 @@ end initial = @equations begin σse = 0.16667 - eta = 0.04918 + η = 0.04918 phi = 0.26417 phit = 0.26417 From be9a84ec70373f347df0c441e13e7960e9a2e9eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Mon, 30 Oct 2023 12:31:12 +0100 Subject: [PATCH 06/35] Extended GROWTH --- src/models/GROWTH.jl | 503 ++++++++++++++++++++++--------------------- 1 file changed, 261 insertions(+), 242 deletions(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 12656b5..8cf1091 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -1,266 +1,285 @@ +operators!([:<, :>, :≤, :<=, :≥, :>=]) + # Box 11.1: Aggregation equations -aggr = @equations begin - Yk = Ske + INke - INk[-1] # 11.1: Real output - Ske = β * Sk + (1 - β) * Sk[-1] * (1 + GRpr + RA) # 11.2: Expected real sales - INke = INk[-1] + γ * (INkt - INk[-1]) # 11.3: Long-run inventory target - INkt = σₜ * Ske # 11.4: Short-run inventory target - INk = INk[-1] + Yk - Sk - NPL / UC # 11.5: Actual real inventories - Kk = Kk[-1] * (1 + GRk) # 11.6: Real capital stock - GRk = γ₀ + γᵤ * U[-1] - γᵣ * RRl # 11.7: Growth of real capital stock - U = Yk / Kk[-1] # 11.8: Capital utilization proxy - RRl = ((1 + Rl) / (1 + PI)) - 1 # 11.9: Real interest rate on loans - PI = (P - P[-1]) / P[-1] # 11.10: Rate of price inflation - Ik = (Kk - Kk[-1]) + δ * Kk[-1] # 11.11: Real gross investment -end +aggr = model( + exos=@variables(Sk, p), + eqs=@equations begin + Yk = Skₑ + INkₑ - INk[-1] # 11.1: Real output + Skₑ = β * Sk + (1 - β) * Sk[-1] * (1 + GRpr + RA) # 11.2: Expected real sales ??? RA + INkₜ = σₜ * Skₑ # 11.3: Long-run inventory target + INkₑ = INk[-1] + γ * (INKₜ - INk[-1]) # 11.4: Short-run inventory target + INk = INk[-1] + Yk - Sk - NPL / UC # 11.5: Actual real inventories ??? - NPL / UC + Kk = Kk[-1] * (1 + GRk) # 11.6: Real capital stock + GRk = γ₀ + γᵤ * U[-1] - γᵣ * RRₗ # 11.7: Growth of real capital stock ??? [-1] + U = Yk / Kk[-1] # 11.8: Capital utilization proxy + RRₗ = ((1 + rₗ) / (1 + π)) - 1 # 11.9: Real interest rate on loans + π = (p - p[-1]) / p[-1] # 11.10: Rate of price inflation + Ik = (Kk - Kk[-1]) + δ * Kk[-1] # 11.11: Real gross investment ??? + end +) # Box 11.2: Firms equations -firms1 = @equations begin - Sk = Ck + Gk + Ik # 11.12: Actual real sales - S = Sk * P # 11.13: Value of realized sales - IN = INk * UC # 11.14: Inventories valued at current cost - INV = Ik * P # 11.15: Nominal gross investment - K = Kk * P # 11.16: Nominal value of fixed capital - Y = Sk * P + (INk - INk[-1]) * UC # 11.17: Nominal GDP -end +firms₁ = model( + exos=@variables(INk), + eqs=@equations begin + Sk = Ck + Gk + Ik # 11.12: Actual real sales + S = Sk * p # 11.13: Value of realized sales + IN = INk * UC # 11.14: Inventories valued at current cost + INV = Ik * p # 11.15: Nominal gross investment + K = Kk * p # 11.16: Nominal value of fixed capital + Y = Sk * p + (INk - INk[-1]) * UC # 11.17: Nominal GDP + end +) # Box 11.3: Firms equations -firms2 = @equations begin - ωₜ = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z3 * (1 - ER) - z4 * BANDₜ + z5 * BANDᵦ)) # 11.18: Real wage aspirations - ER = N[-1] / Nfe[-1] # 11.19: Employment rate - # 11.20: Switch variables - z3 = ((1 + BANDᵦ) < ER <= (1 + BANDₜ)) ? 1 : 0 # Band activation - z4 = ER > (1 + BANDₜ) ? 1 : 0 # Upper band overshoot - z5 = ER < (1 - BANDᵦ) ? 1 : 0 # Lower band undershoot - W = W[-1] + ω₃ * (ωₜ * P[-1] - W[-1]) # 11.21: Nominal wage - PR = PR[-1] * (1 + GRpr) # 11.22: Labor productivity - Nt = Yk / PR # 11.23: Desired employment - N = N[-1] + ηₙ * (Nt - N[-1]) # 11.24: Actual employment --> ηn not in the book - WB = N * W # 11.25: Nominal wage bill - UC = WB / Yk # 11.26: Actual unit cost - NUC = W / PR # 11.27: Normal unit cost - NHUC = (1 - σₙ) * NUC + σₙ * (1 + Rln[-1]) * NUC[-1] # 11.28: Normal historic unit cost -end +firms₂ = model( + exos=@variables(Nfe, p, Rln), + eqs=@equations begin + ωT = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z₃ * (1 - ER) - z₄ * BANDₜ + z₅ * BANDᵦ)) # 11.18: Real wage aspirations ??? + ER = N[-1] / Nfe[-1] # 11.19: Employment rate + # 11.20: Switch variables + z₃ = ((1 + BANDᵦ) < ER <= (1 + BANDₜ)) ? 1 : 0 # Band activation + z₄ = ER > (1 + BANDₜ) ? 1 : 0 # Upper band overshoot + z₅ = ER < (1 - BANDᵦ) ? 1 : 0 # Lower band undershoot + W = W[-1] + Ω₃ * (ωT * p[-1] - W[-1]) # 11.21: Nominal wage + PR = PR[-1] * (1 + GRpr) # 11.22: Labor productivity + NT = Yk / PR # 11.23: Desired employment + N = N[-1] + ηₙ * (NT - N[-1]) # 11.24: Actual employment --> ηₙ not in the book + WB = N * W # 11.25: Nominal wage bill + UC = WB / Yk # 11.26: Actual unit cost + NUC = W / PR # 11.27: Normal unit cost + NHUC = (1 - σₙ) * NUC + σₙ * (1 + Rln[-1]) * NUC[-1] # 11.28: Normal historic unit cost + end +) # Box 11.4: Firms equations -firms3 = @equations begin - P = (1 + ϕ) * NHUC # 11.29: Normal-cost pricing - ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]) # 11.30: Actual mark-up --> ε₂ not in the book - ϕₜ = (FUft + FDf + Rl[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * S) # 11.32: Expected historical costs - HCe = (1 - σₛₑ) * Ske * UC + (1 + Rl[-1]) * σₛₑ * Ske * UC[-1] # 11.33: Opening inventories to expected sales ratio - σₛₑ = INk[-1] / Ske # 11.34: Planned entrepeneurial profits of firms - Fft = FUft + FDf + Rl[-1] * (Lfd[-1] - IN[-1]) # 11.34: Planned entrepeneurial profits of firms - FUft = ψᵤ * INV[-1] # 11.35: Planned rηined earnings of firms - FDf = ψd * Ff[-1] # 11.36: Dividends of firms -end +firms₃ = model( + exos=@variables(p, rₗ, Lfd, IN, UC, INk, INV, Ff), + eqs=@equations begin + p = (1 + ϕ) * NHUC # 11.29: Normal-cost pricing + ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]) # 11.30: Actual mark-up --> ε₂ not in the book + ϕₜ = (FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * S) # 11.32: Expected historical costs ??? + HCe = (1 - σₛₑ) * Skₑ * UC + (1 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1] # 11.33: Opening inventories to expected sales ratio + σₛₑ = INk[-1] / Skₑ # 11.34: Planned entrepeneurial profits of firms + Fft = FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1]) # 11.34: Planned entrepeneurial profits of firms + FUft = ψᵤ * INV[-1] # 11.35: Planned rηined earnings of firms + FDf = ψd * Ff[-1] # 11.36: Dividends of firms + end +) # Box 11.5: Firms equations -firms4 = @equations begin - Ff = S - WB + (IN - IN[-1]) - Rl[-1] * IN[-1] # 11.37: Realized entrepeneurial profits - FUf = Ff - FDf - Rl[-1] * (Lfd[-1] - IN[-1]) + Rl[-1] * NPL # 11.38: Rηined earnings of firms - Lfd = Lfd[-1] + INV + (IN - IN[-1]) - FUf - (Eks - Eks[-1]) * Pe - NPL # 11.39: Demand for loans by firms - NPL = NPLk * Lfs[-1] # 11.40: Defaulted loans - Eks = Eks[-1] + ((1 - ψᵤ) * INV[-1]) / Pe # 11.41: Supply of equities issued by firms - # Rk = FDf/(Pe[-1]*Ekd[-1]) # 11.42: Dividend yield of firms - Rk = FDf / (Pe[-1] * Ekd[-1]) - PE = Pe / (Ff / Eks[-1]) # 11.43: Price earnings ratio - Q = (Eks * Pe + Lfd) / (K + IN) # 11.44: Tobins Q ratio -end +firms₄ = model( + exos=@variables(rₗ, Lfd, IN, INV, Ff, Lfs, Pₑ, ed), + eqs=@equations begin + Ff = S - WB + (IN - IN[-1]) - rₗ[-1] * IN[-1] # 11.37: Realized entrepeneurial profits + FUf = Ff - FDf - rₗ[-1] * (Lfd[-1] - IN[-1]) + rₗ[-1] * NPL # 11.38: Retained earnings of firms + Lfd = Lfd[-1] + INV + (IN - IN[-1]) - FUf - (Eks - Eks[-1]) * Pₑ - NPL # 11.39: Demand for loans by firms + NPL = NPLk * Lfs[-1] # 11.40: Defaulted loans ??? Lfd + Eks = Eks[-1] + ((1 - ψᵤ) * INV[-1]) / Pₑ # 11.41: Supply of equities issued by firms + Rk = FDf / (Pₑ[-1] * ed[-1]) # 11.42: Dividend yield of firms + PE = Pₑ / (Ff / Eks[-1]) # 11.43: Price earnings ratio + Q = (Eks * Pₑ + Lfd) / (K + IN) # 11.44: Tobins Q ratio + end +) +# TODO: Δ operator # Box 11.6: Households equations -hh1 = @equations begin - # YP = WB + FDf + FDb + Rm[-1]*Md[-1] + Rb[-1]*Bhd[-1] + BLs[-1] # 11.45: Personal income - YP = WB + FDf + FDb + Rm[-1] * Mh[-1] + Rb[-1] * Bhd[-1] + BLs[-1] - #YP = WB + FDf + FDb + Rm[-1]*Mh[-1] + Rb[-1]*Bhd[-1] + BLs[-1] + NL - TX = theta * YP # 11.46: Income taxes - YDr = YP - TX - Rl[-1] * Lhd[-1] # 11.47: Regular disposable income - YDhs = YDr + CG # 11.48: Haig-Simons disposable income - # 11.49: Capital gains - CG = (Pbl - Pbl[-1]) * BLd[-1] + (Pe - Pe[-1]) * Ekd[-1] + (OFb - OFb[-1]) - # 11.50: Wealth - V = V[-1] + YDr - CONS + (Pbl - Pbl[-1]) * BLd[-1] + (Pe - Pe[-1]) * Ekd[-1] + (OFb - OFb[-1]) - Vk = V / P # 11.51: Real staock of wealth - CONS = Ck * P # 11.52: Consumption - Ck = alpha1 * (YDkre + NLk) + alpha2 * Vk[-1] # 11.53: Real consumption - YDkre = eps * YDkr + (1 - eps) * (YDkr[-1] * (1 + GRpr)) # 11.54: Expected real regular disposable income - # YDkr = YDr/P - (P - P[-1])*Vk[-1]/P # 11.55: Real regular disposable income - YDkr = YDr / P - ((P - P[-1]) * Vk[-1]) / P -end +hh₁ = model( + exos=@variables(rₗ, Pₑ, ed, rₘ, Mₕ, rb, Bhd, BLs, Lhd, Pbl, BLd, OFb, p), + eqs=@equations begin + YP = WB + FDf + FDb + rₘ[-1] * Mₕ[-1] + rb[-1] * Bhd[-1] + BLs[-1] # 11.45: Personal income ??? + TX = θ * YP # 11.46: Income taxes + YDᵣ = YP - TX - rₗ[-1] * Lhd[-1] # 11.47: Regular disposable income + YDhs = YDᵣ + CG # 11.48: Haig-Simons disposable income + CG = (Pbl - Pbl[-1]) * BLd[-1] + (Pₑ - Pₑ[-1]) * ed[-1] + (OFb - OFb[-1]) # 11.49: Capital gains + # 11.50: Wealth + V = V[-1] + YDᵣ - CONS # ??? fixed + Vk = V / p # 11.51: Real stock of wealth + CONS = Ck * p # 11.52: Consumption + Ck = α₁ * (YDkre + NLk) + α₂ * Vk[-1] # 11.53: Real consumption + YDkre = ε * YDkr + (1 - ε) * (YDkr[-1] * (1 + GRpr)) # 11.54: Expected real regular disposable income + YDkr = YDᵣ / p - ((p - p[-1]) * Vk[-1]) / p # 11.55: Real regular disposable income ??? + end +) # Box 11.7: Households equations -hh2 = @equations begin - GL = η * YDr # 11.56: Gross amount of new personal loans ---> new η here - η = η₀ - ηr * RRl # 11.57: New loans to personal income ratio - NL = GL - REP # 11.58: Net amount of new personal loans - REP = deltarep * Lhd[-1] # 11.59: Personal loans repayments - Lhd = Lhd[-1] + GL - REP # 11.60: Demand for personal loans - NLk = NL / P # 11.61: Real amount of new personal loans - BUR = (REP + Rl[-1] * Lhd[-1]) / YDr[-1] # 11.62: Burden of personal debt -end +hh₂ = model( + exos=@variables(rₗ, YDᵣ), + eqs=@equations begin + GL = η * YDᵣ # 11.56: Gross amount of new personal loans ---> new η here + η = η₀ - ηr * RRₗ # 11.57: New loans to personal income ratio + NL = GL - REP # 11.58: Net amount of new personal loans + REP = δrep * Lhd[-1] # 11.59: Personal loans repayments + Lhd = Lhd[-1] + NL # 11.60: Demand for personal loans + NLk = NL / p # 11.61: Real amount of new personal loans + BUR = (REP + rₗ[-1] * Lhd[-1]) / YDᵣ[-1] # 11.62: Burden of personal debt ??? [-1] + end +) # Box 11.8: Households equations - portfolio decisions -hh2 = @equations begin - # 11.64: Demand for bills - Bhd = Vfma[-1] * (λ20 + λ22 * Rb[-1] - λ21 * Rm[-1] - λ24 * Rk[-1] - λ23 * Rbl[-1] - λ25 * (YDr / V)) - # 11.65: Demand for bonds - BLd = Vfma[-1] * (λ30 - λ32 * Rb[-1] - λ31 * Rm[-1] - λ34 * Rk[-1] + λ33 * Rbl[-1] - λ35 * (YDr / V)) / Pbl - # 11.66: Demand for equities - normalized to get the price of equitities - Pe = Vfma[-1] * (λ40 - λ42 * Rb[-1] - λ41 * Rm[-1] + λ44 * Rk[-1] - λ43 * Rbl[-1] - λ45 * (YDr / V)) / Ekd - Mh = Vfma - Bhd - Pe * Ekd - Pbl * BLd + Lhd # 11.67: Money deposits - as a residual - Vfma = V - Hhd - OFb # 11.68: Investible wealth - VfmaA = Mh + Bhd + Pbl * BLd + Pe * Ekd - Hhd = λc * CONS # 11.69: Households demand for cash - Ekd = Eks # 11.70: Stock market equilibrium -end +hh₃ = model( + exos=@variables(Pₑ, ed, rₘ, Mₕ, rb, Bhd, Lhd, Pbl, BLd, OFb, Rk, Rbl), + eqs=@equations begin + # 11.64: Demand for bills + Bhd = Vfma[-1] * (λ₂₀ + λ₂₂ * rb[-1] - λ₂₁ * rₘ[-1] - λ₂₄ * Rk[-1] - λ₂₃ * Rbl[-1] - λ₂₅ * (YDᵣ / V)) # ??? Vfma = V + # 11.65: Demand for bonds + BLd = Vfma[-1] * (λ₃₀ - λ₃₂ * rb[-1] - λ₃₁ * rₘ[-1] - λ₃₄ * Rk[-1] + λ₃₃ * Rbl[-1] - λ₃₅ * (YDᵣ / V)) / Pbl + # 11.66: Demand for equities - normalized to get the price of equitities + Pₑ = Vfma[-1] * (λ₄₀ - λ₄₂ * rb[-1] - λ₄₁ * rₘ[-1] + λ₄₄ * Rk[-1] - λ₄₃ * Rbl[-1] - λ₄₅ * (YDᵣ / V)) / ed + Mₕ = Vfma - Bhd - Pₑ * ed - Pbl * BLd + Lhd # 11.67: Money deposits - as a residual + Vfma = V - Hhd - OFb # 11.68: Investible wealth + VfmaA = Mₕ + Bhd + Pbl * BLd + Pₑ * ed + Hhd = λc * CONS # 11.69: Households demand for cash + ed = Eks # 11.70: Stock market equilibrium + end +) # Box 11.9: Governments equations -gov = @equations begin - G = Gk * P # 11.71: Pure government expenditures - Gk = Gk[-1] * (1 + GRg) # 11.72: Real government expenditures - PSBR = G + BLs[-1] + Rb[-1] * (Bbs[-1] + Bhs[-1]) - TX # 11.73: Government deficit --> BLs[-1] missing in the book - # 11.74: New issues of bills - Bs = Bs[-1] + G - TX - (BLs - BLs[-1]) * Pbl + Rb[-1] * (Bhs[-1] + Bbs[-1]) + BLs[-1] - GD = Bbs + Bhs + BLs * Pbl + Hs # 11.75: Government debt -end +gov = model( + exos=@variables(BLs, rb, Bbs, Bhs), + eqs=@equations begin + G = Gk * p # 11.71: Pure government expenditures + Gk = Gk[-1] * (1 + GRg) # 11.72: Real government expenditures + PSBR = G + BLs[-1] + rb[-1] * (Bbs[-1] + Bhs[-1]) - TX # 11.73: Government deficit --> BLs[-1] missing in the book + # 11.74: New issues of bills + Bs = Bs[-1] + G - TX - (BLs - BLs[-1]) * Pbl + rb[-1] * (Bhs[-1] + Bbs[-1]) + BLs[-1] + GD = Bbs + Bhs + BLs * Pbl + Hs # 11.75: Government debt + end +) # Box 11.10: The Central banks equations -cb = @equations begin - # ---------------------------------------- - Fcb = Rb[-1] * Bcbd[-1] # 11.76: Central bank profits - BLs = BLd # 11.77: Bonds are supplied on demand - Bhs = Bhd # 11.78: Household bills supplied on demand - Hhs = Hhd # 11.79: Cash supplied on demand - Hbs = Hbd # 11.80: Reserves supplied on demand - Hs = Hbs + Hhs # 11.81: Total supply of cash - Bcbd = Hs # 11.82: Central bank - Bcbs = Bcbd # 11.83: Supply of bills to Central bank - Rb = Rb̄ # 11.84: Interest rate on bills set exogenously - Rbl = Rb + ADDbl # 11.85: Long term interest rate - Pbl = 1 / Rbl # 11.86: Price of long-term bonds -end +cb = model( + exos=@variables(BLs, rb, Bhs), + eqs=@equations begin + Fcb = rb[-1] * Bcbd[-1] # 11.76: Central bank profits + BLs = BLd # 11.77: Bonds are supplied on demand + Bhs = Bhd # 11.78: Household bills supplied on demand + Hhs = Hhd # 11.79: Cash supplied on demand + Hbs = Hbd # 11.80: Reserves supplied on demand + Hs = Hbs + Hhs # 11.81: Total supply of cash + Bcbd = Hs # 11.82: Central bank + Bcbs = Bcbd # 11.83: Supply of bills to Central bank + rb = Rb̄ # 11.84: Interest rate on bills set exogenously + Rbl = rb + ADDbl # 11.85: Long term interest rate + Pbl = 1 / Rbl # 11.86: Price of long-term bonds + end +) # Box 11.11: Commercial Banks equations -banks1 = @equations begin - # --------------------------------------- - Ms = Mh # 11.87: Bank deposits supplied on demand - Lfs = Lfd # 11.88: Loans to firms supplied on demand - Lhs = Lhd # 11.89: Personal loans supplied on demand - Hbd = ρ * Ms # 11.90: Reserve requirements of banks - # 11.91: Bills supplied to banks - Bbs = Bbs[-1] + (Bs - Bs[-1]) - (Bhs - Bhs[-1]) - (Bcbs - Bcbs[-1]) - # 11.92: Balance sheet constraint of banks - Bbd = Ms + OFb - Lfs - Lhs - Hbd - BLR = Bbd / Ms # 11.93: Bank liquidity ratio - # 11.94: Deposit interest rate - Rm = Rm[-1] + z1a * xīm1 + z1b * xīm2 - z2a * xīm1 - z2b * xīm2 - # 11.95-97: Mechanism for determining changes to the interest rate on deposits - z2a = if BLR[-1] > (top + 0.05) - 1 - else - 0 +banks₁ = model( + exos=@variables(Bhs, Bs, Bcbs), + eqs=@equations begin + Ms = Mₕ # 11.87: Bank deposits supplied on demand + Lfs = Lfd # 11.88: Loans to firms supplied on demand + Lhs = Lhd # 11.89: Personal loans supplied on demand + Hbd = ρ * Ms # 11.90: Reserve requirements of banks + # 11.91: Bills supplied to banks + Bbs = Bbs[-1] + (Bs - Bs[-1]) - (Bhs - Bhs[-1]) - (Bcbs - Bcbs[-1]) + # 11.92: Balance sheet constraint of banks + Bbd = Ms + OFb - Lfs - Lhs - Hbd + BLR = Bbd / Ms # 11.93: Bank liquidity ratio + # 11.94: Deposit interest rate + rₘ = rₘ[-1] + z1a * ξ₁ + z1b * ξ₂ - z2a * ξ₁ - z2b * ξ₂ + # 11.95-97: Mechanism for determining changes to the interest rate on deposits + z2a = (BLR[-1] > (top + 0.05)) ? 1 : 0 + z2b = (BLR[-1] > top) ? 1 : 0 + z1a = (BLR[-1] ≤ bot) ? 1 : 0 + z1b = (BLR[-1] ≤ (bot - 0.05)) ? 1 : 0 end - z2b = if BLR[-1] > top - 1 - else - 0 - end - z1a = if BLR[-1] ≤ bot - 1 - else - 0 - end - z1b = if BLR[-1] ≤ (bot - 0.05) - 1 - else - 0 - end -end +) # Box 11.12: Commercial banks equations -banks2 = @equations begin - # --------------------------------------- - Rl = Rm + ADDl # 11.98: Loan interest rate - OFbt = NCAR * (Lfs[-1] + Lhs[-1]) # 11.99: Long-run own funds target - OFbe = OFb[-1] + β * (OFbt - OFb[-1]) # 11.100: Short-run own funds target - FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1] # 11.101: Target rηined earnings of banks - NPLke = εb * NPLke[-1] + (1 - εb) * NPLk[-1] # 11.102: Expected proportion of non-performing loans - FDb = Fb - FUb # 11.103: Dividends of banks - Fbt = λb * Y[-1] + (OFbe - OFb[-1] + NPLke * Lfs[-1]) # 11.104: Target profits of banks - # 11.105: Actual profits of banks - Fb = Rl[-1] * (Lfs[-1] + Lhs[-1] - NPL) + Rb[-1] * Bbd[-1] - Rm[-1] * Ms[-1] - # 11.106: Lending mark-up over deposit rate - ADDl = (Fbt - Rb[-1] * Bbd[-1] + Rm[-1] * (Ms[-1] - (1 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1 - NPLke) * Lfs[-1] + Lhs[-1]) - FUb = Fb - λb * Y[-1] # 11.107: Actual rηined earnings - OFb = OFb[-1] + FUb - NPL # 11.108: Own funds of banks - CAR = OFb / (Lfs + Lhs) +banks₂ = model( + exos=@variables(Lfs, Lhs, NPLk, Y, rb, Bbd, rₘ, Ms), + eqs=@equations begin + rₗ = rₘ + ADDl # 11.98: Loan interest rate + OFbt = NCAR * (Lfs[-1] + Lhs[-1]) # 11.99: Long-run own funds target + OFbe = OFb[-1] + βb * (OFbt - OFb[-1]) # 11.100: Short-run own funds target + FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1] # 11.101: Target rηined earnings of banks + NPLke = εb * NPLke[-1] + (1 - εb) * NPLk[-1] # 11.102: Expected proportion of non-performing loans + FDb = Fb - FUb # 11.103: Dividends of banks + Fbt = λb * Y[-1] + (OFbe - OFb[-1] + NPLke * Lfs[-1]) # 11.104: Target profits of banks + # 11.105: Actual profits of banks + Fb = rₗ[-1] * (Lfs[-1] + Lhs[-1] - NPL) + rb[-1] * Bbd[-1] - rₘ[-1] * Ms[-1] + # 11.106: Lending mark-up over deposit rate + ADDl = (Fbt - rb[-1] * Bbd[-1] + rₘ[-1] * (Ms[-1] - (1 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1 - NPLke) * Lfs[-1] + Lhs[-1]) + FUb = Fb - λb * Y[-1] # 11.107: Actual rηined earnings + OFb = OFb[-1] + FUb - NPL # 11.108: Own funds of banks + CAR = OFb / (Lfs + Lhs) + Vf = IN + K - Lfd - ed * Pₑ # Firm's wealth (memo for matrices) + # Vg = -Bs - BLs * Pbl # Government's wealth (memo for matrices) + Ls = Lfs + Lhs # Loans supply (memo for matrices) + end +) - Vf = IN + K - Lfd - Ekd * Pe # Firm's wealth (memo for matrices) - # Vg = -Bs - BLs * Pbl # Government's wealth (memo for matrices) - Ls = Lfs + Lhs # Loans supply (memo for matrices) -end +growth = aggr + firms₁ + firms₂ + firms₃ + firms₄ + hh₁ + hh₂ + hh₃ + gov + cb + banks₁ + banks₂ +growth.endogenous_variables +growth + Consistent.Variables(param_dict) param_dict = @parameters begin - alpha1 = 0.75 - alpha2 = 0.064 - bη = 0.5 - bηb = 0.4 - gamma = 0.15 - gamma0 = 0.00122 - gammar = 0.1 - gammau = 0.05 - delta = 0.10667 - deltarep = 0.1 - eps = 0.5 - eps2 = 0.8 - epsb = 0.25 - epsrb = 0.9 - η0 = 0.07416 - ηn = 0.6 + α₁ = 0.75 + α₂ = 0.064 + β = 0.5 + βb = 0.4 + γ = 0.15 + γ₀ = 0.00122 + γᵣ = 0.1 + γᵤ = 0.05 + δ = 0.10667 + δrep = 0.1 + ε = 0.5 + ε₂ = 0.8 + εb = 0.25 + # εrb = 0.9 + η₀ = 0.07416 + ηₙ = 0.6 ηr = 0.4 - thη = 0.22844 - # λ10 = -0.17071 - # λ11 = 0 - # λ12 = 0 - # λ13 = 0 - # λ14 = 0 - # λ15 = 0.18 - λ20 = 0.25 - λ21 = 2.2 - λ22 = 6.6 - λ23 = 2.2 - λ24 = 2.2 - λ25 = 0.1 - λ30 = -0.04341 - λ31 = 2.2 - λ32 = 2.2 - λ33 = 6.6 - λ34 = 2.2 - λ35 = 0.1 - λ40 = 0.67132 - λ41 = 2.2 - λ42 = 2.2 - λ43 = 2.2 - λ44 = 6.6 - λ45 = 0.1 + θ = 0.22844 + # λ1₀= -0.17071 + # λ1₁= 0 + # λ1₂= 0 + # λ1₃= 0 + # λ1₄ = 0 + # λ1₅ = 0.18 + λ₂₀ = 0.25 + λ₂₁ = 2.2 + λ₂₂ = 6.6 + λ₂₃ = 2.2 + λ₂₄ = 2.2 + λ₂₅ = 0.1 + λ₃₀ = -0.04341 + λ₃₁ = 2.2 + λ₃₂ = 2.2 + λ₃₃ = 6.6 + λ₃₄ = 2.2 + λ₃₅ = 0.1 + λ₄₀ = 0.67132 + λ₄₁ = 2.2 + λ₄₂ = 2.2 + λ₄₃ = 2.2 + λ₄₄ = 6.6 + λ₄₅ = 0.1 λb = 0.0153 λc = 0.05 - xim1 = 0.0008 - xim2 = 0.0007 - ro = 0.05 - σn = 0.1666 - σt = 0.2 + ξ + ξ₁ = 0.0008 + ξ₂ = 0.0007 + # ro = 0.05 + σₙ = 0.1666 + σₜ = 0.2 ψd = 0.15255 ψᵤ = 0.92 - omega0 = -0.20594 - omega1 = 1 - omega2 = 2 - omega3 = 0.45621 + ω₀ = -0.20594 + ω₁ = 1 + ω₂ = 2 + Ω₃ = 0.45621 end -initial = @equations begin +initial = @parameters begin σse = 0.16667 η = 0.04918 - phi = 0.26417 - phit = 0.26417 + ϕ = 0.26417 + ϕₜ = 0.26417 ADDbl = 0.02 BANDt = 0.01 @@ -299,33 +318,33 @@ initial = @equations begin INV = 16911600 Ik = 2357910 N = 87.181 - Nt = 87.181 + NT = 87.181 NHUC = 5.6735 NL = 683593 NLk = 95311 NPL = 309158 NPLke = 0.02 NUC = 5.6106 - omegat = 112852 - P = 7.1723 + ωt = 112852 + p = 7.1723 Pbl = 18.182 - Pe = 17937 + Pₑ = 17937 PE = 5.07185 - PI = 0.0026 + π = 0.0026 PR = 138659 PSBR = 1894780 Q = 0.77443 - Rb = 0.035 + rb = 0.035 Rbl = 0.055 Rk = 0.03008 - Rl = 0.06522 - Rm = 0.0193 + rₗ = 0.06522 + rₘ = 0.0193 REP = 2092310 #RRb = 0.03232 - RRl = 0.06246 + RRₗ = 0.06246 S = 86270300 Sk = 12028300 - Ske = 12028300 + Skₑ = 12028300 TX = 17024100 U = 0.70073 UC = 5.6106 @@ -333,7 +352,7 @@ initial = @equations begin WB = 67824000 Y = 86607700 Yk = 12088400 - YDr = 56446400 + YDᵣ = 56446400 YDkr = 7813270 YDkre = 7813290 YP = 73158700 @@ -357,7 +376,7 @@ initial = @equations begin BLd = 840742 BLs = 840742 GD = 57728700 - Ekd = 5112.6001 + ed = 5112.6001 Eks = 5112.6001 Hbd = 2025540 Hbs = 2025540 @@ -366,8 +385,8 @@ initial = @equations begin Hs = 4655690 IN = 11585400 INk = 2064890 - INke = 2405660 - INkt = 2064890 + INkₑ = 2405660 + INKₜ = 2064890 #K = 127444000 K = 127486471 #Kk = 17768900 @@ -378,7 +397,7 @@ initial = @equations begin Lhs = 21606600 Ls = 37569500 #Md = 40510800 - Mh = 40510800 + Mₕ = 40510800 Ms = 40510800 OFb = 3474030 OFbe = 3474030 From aa517cee2a5332869c4de311d33fb2aaf6aa92ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Mon, 30 Oct 2023 16:43:55 +0100 Subject: [PATCH 07/35] New convenience functions for params/exos --- src/CombineModels.jl | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/CombineModels.jl b/src/CombineModels.jl index 71e32f0..43978b9 100644 --- a/src/CombineModels.jl +++ b/src/CombineModels.jl @@ -2,7 +2,7 @@ import Base: + function +(model1::Model, model2::Model) undetermined = findall(in(model2.endogenous_variables), model1.endogenous_variables) - @assert isempty(undetermined) "The endogenous variables $(model1[undetermined]) appear twice" + @assert isempty(undetermined) "The endogenous variables $(model1.endogenous_variables[undetermined]) appear twice" exos1 = filter(x -> !(x in model2.endogenous_variables), model1.exogenous_variables) exos2 = filter( x -> !((x in model1.endogenous_variables) || (x in exos1)), model2.exogenous_variables @@ -10,7 +10,27 @@ function +(model1::Model, model2::Model) return model( endos=Variables(vcat(model1.endogenous_variables, model2.endogenous_variables)), exos=Variables(vcat(exos1, exos2)), - params=Variables(vcat(model1.parameters, model2.parameters)), + params=Variables(unique(vcat(model1.parameters, model2.parameters))), eqs=Equations(vcat(model1.equations, model2.equations)) ) +end + +function add_params(model1::Model, params::Variables, verbose=false) + return model( + endos=model1.endogenous_variables, + exos=model1.exogenous_variables, + params=Variables(unique(vcat(model1.parameters, params))), + eqs=model1.equations, + verbose=verbose + ) +end + +function add_exos(model1::Model, exos::Variables, verbose=false) + return model( + endos=model1.endogenous_variables, + exos=Variables(unique(vcat(model1.exogenous_variables, exos))), + params=model1.parameters, + eqs=model1.equations, + verbose=verbose + ) end \ No newline at end of file From 8daf5a5b375e9f0c00408c69015b97424c5baca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Mon, 30 Oct 2023 16:44:07 +0100 Subject: [PATCH 08/35] Continuation last commit --- src/Consistent.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Consistent.jl b/src/Consistent.jl index d758400..0652831 100644 --- a/src/Consistent.jl +++ b/src/Consistent.jl @@ -1,7 +1,7 @@ module Consistent export @parameters, @equations, @variables -export model, solve +export model, solve, operators!, add_params, add_exos include("Helpers.jl") include("ModelComponents.jl") From e5515bd103fb53aa9f46a9dd485b994ac990fa0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Mon, 30 Oct 2023 16:44:27 +0100 Subject: [PATCH 09/35] User interface to math_operators --- src/Macros.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Macros.jl b/src/Macros.jl index 350c99f..a99c3f3 100644 --- a/src/Macros.jl +++ b/src/Macros.jl @@ -22,7 +22,7 @@ function build_f!(endos, exos, params, args) if issubset(variables, found) function_body = replace_vars(function_body, endos, exos, params, name[2:5]) else - error("$(setdiff(variables, found)) unused!") + error("$(setdiff(variables, found)) unused!\n\nUsed: $found") end # construct function for residuals of model variables @@ -91,4 +91,6 @@ function model(; eqs, deepcopy(Consistent.f!) ) -end \ No newline at end of file +end + +operators!(x) = union!(math_operators, x) \ No newline at end of file From fb5e9be0b724f81d363bcf8f14ca04ba98237bf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Mon, 30 Oct 2023 16:46:05 +0100 Subject: [PATCH 10/35] Fixed stupid output bug --- src/Model.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model.jl b/src/Model.jl index 39073d5..cea5296 100644 --- a/src/Model.jl +++ b/src/Model.jl @@ -30,7 +30,7 @@ function Base.show(io::IO, m::Model) print(io, Crayon(foreground = :blue), descriptors[3]); println(io, Crayon(reset=true), m.parameters) print(io, Crayon(foreground = :red), descriptors[4]); print(io, Crayon(reset=true)) for i in eachindex(m.equations) - additional_space = div(length(m.equations), 10) - div(i, 10) + additional_space = Int(floor(log10(length(m.equations)))) - Int(floor(log10(i))) print(io, "\n", ' '^(max_width + max(additional_space, 0)), "($i) ", m.equations[i]) end end \ No newline at end of file From 31f39e430ceb5c631fb59a85176b0e58f82738a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Mon, 30 Oct 2023 16:50:23 +0100 Subject: [PATCH 11/35] Extended growth model --- src/models/GROWTH.jl | 93 +++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 36 deletions(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 8cf1091..33a4b81 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -1,4 +1,4 @@ -operators!([:<, :>, :≤, :<=, :≥, :>=]) +operators!([:<, :>, :≤, :<=, :≥, :>=, :exp, :log]) # Box 11.1: Aggregation equations aggr = model( @@ -7,7 +7,7 @@ aggr = model( Yk = Skₑ + INkₑ - INk[-1] # 11.1: Real output Skₑ = β * Sk + (1 - β) * Sk[-1] * (1 + GRpr + RA) # 11.2: Expected real sales ??? RA INkₜ = σₜ * Skₑ # 11.3: Long-run inventory target - INkₑ = INk[-1] + γ * (INKₜ - INk[-1]) # 11.4: Short-run inventory target + INkₑ = INk[-1] + γ * (INkₜ - INk[-1]) # 11.4: Short-run inventory target INk = INk[-1] + Yk - Sk - NPL / UC # 11.5: Actual real inventories ??? - NPL / UC Kk = Kk[-1] * (1 + GRk) # 11.6: Real capital stock GRk = γ₀ + γᵤ * U[-1] - γᵣ * RRₗ # 11.7: Growth of real capital stock ??? [-1] @@ -33,14 +33,15 @@ firms₁ = model( # Box 11.3: Firms equations firms₂ = model( - exos=@variables(Nfe, p, Rln), + exos=@variables(Nfe, p, Rln, ER, BANDb), + verbose=true, eqs=@equations begin - ωT = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z₃ * (1 - ER) - z₄ * BANDₜ + z₅ * BANDᵦ)) # 11.18: Real wage aspirations ??? + ωT = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z₃ * (1 - ER) - z₄ * BANDₜ + z₅ * BANDb)) # 11.18: Real wage aspirations ??? ER = N[-1] / Nfe[-1] # 11.19: Employment rate # 11.20: Switch variables - z₃ = ((1 + BANDᵦ) < ER <= (1 + BANDₜ)) ? 1 : 0 # Band activation + z₃ = ((1 + BANDb) < ER <= (1 + BANDₜ)) ? 1 : 0 # Band activation z₄ = ER > (1 + BANDₜ) ? 1 : 0 # Upper band overshoot - z₅ = ER < (1 - BANDᵦ) ? 1 : 0 # Lower band undershoot + z₅ = ER < (1 - BANDb) ? 1 : 0 # Lower band undershoot W = W[-1] + Ω₃ * (ωT * p[-1] - W[-1]) # 11.21: Nominal wage PR = PR[-1] * (1 + GRpr) # 11.22: Labor productivity NT = Yk / PR # 11.23: Desired employment @@ -159,7 +160,7 @@ cb = model( Hs = Hbs + Hhs # 11.81: Total supply of cash Bcbd = Hs # 11.82: Central bank Bcbs = Bcbd # 11.83: Supply of bills to Central bank - rb = Rb̄ # 11.84: Interest rate on bills set exogenously + rb = Rbbar # 11.84: Interest rate on bills set exogenously Rbl = rb + ADDbl # 11.85: Long term interest rate Pbl = 1 / Rbl # 11.86: Price of long-term bonds end @@ -212,11 +213,7 @@ banks₂ = model( end ) -growth = aggr + firms₁ + firms₂ + firms₃ + firms₄ + hh₁ + hh₂ + hh₃ + gov + cb + banks₁ + banks₂ -growth.endogenous_variables -growth + Consistent.Variables(param_dict) - -param_dict = @parameters begin +params_dict = @parameters begin α₁ = 0.75 α₂ = 0.064 β = 0.5 @@ -261,10 +258,9 @@ param_dict = @parameters begin λ₄₅ = 0.1 λb = 0.0153 λc = 0.05 - ξ ξ₁ = 0.0008 ξ₂ = 0.0007 - # ro = 0.05 + ρ = 0.05 σₙ = 0.1666 σₜ = 0.2 ψd = 0.15255 @@ -275,14 +271,15 @@ param_dict = @parameters begin Ω₃ = 0.45621 end -initial = @parameters begin - σse = 0.16667 +initial_dict = @parameters begin + Rbbar = 0.035 + + σₛₑ = 0.16667 η = 0.04918 ϕ = 0.26417 ϕₜ = 0.26417 - ADDbl = 0.02 - BANDt = 0.01 + BANDₜ = 0.01 BANDb = 0.01 bot = 0.05 GRg = 0.03 @@ -294,7 +291,6 @@ initial = @parameters begin Rln = 0.07 RA = 0 top = 0.12 - ADDl = 0.04592 BLR = 0.1091 BUR = 0.06324 @@ -325,7 +321,7 @@ initial = @parameters begin NPL = 309158 NPLke = 0.02 NUC = 5.6106 - ωt = 112852 + ωT = 112852 p = 7.1723 Pbl = 18.182 Pₑ = 17937 @@ -340,7 +336,6 @@ initial = @parameters begin rₗ = 0.06522 rₘ = 0.0193 REP = 2092310 - #RRb = 0.03232 RRₗ = 0.06246 S = 86270300 Sk = 12028300 @@ -360,19 +355,13 @@ initial = @parameters begin z1b = 0 z2a = 0 z2b = 0 - - #Bbd = 4388930 - #Bbs = 4388930 Bbd = 4389790 Bbs = 4389790 Bcbd = 4655690 Bcbs = 4655690 Bhd = 33439320 Bhs = 33439320 - #Bhd = 33396900 - #Bhs = 33396900 Bs = 42484800 - #Bs = 42441520 BLd = 840742 BLs = 840742 GD = 57728700 @@ -386,28 +375,60 @@ initial = @parameters begin IN = 11585400 INk = 2064890 INkₑ = 2405660 - INKₜ = 2064890 - #K = 127444000 + INkₜ = 2064890 K = 127486471 - #Kk = 17768900 Kk = 17774838 Lfd = 15962900 Lfs = 15962900 Lhd = 21606600 Lhs = 21606600 Ls = 37569500 - #Md = 40510800 Mₕ = 40510800 Ms = 40510800 OFb = 3474030 OFbe = 3474030 - #OFb = 3473280 - #OFbe = 3782430 OFbt = 3638100 - #V = 165395000 V = 165438779 - #Vfma = 159291000 Vfma = 159334599 Vk = 23066350 Vf = 31361792 -end \ No newline at end of file + + z₃ = missing + z₄ = missing + z₅ = missing + HCe = missing + YDhs = missing + CG = missing + VfmaA = missing + Fcb = missing + FUbt = missing +end + +growth = aggr + firms₁ + firms₂ + firms₃ + firms₄ + hh₁ + hh₂ + hh₃ + gov + cb + banks₁ + banks₂ +growth = add_params(growth, Consistent.Variables(params_dict)) +growth = add_exos(growth, @variables(RA, GRpr, GRg, BANDb, BANDₜ, ER, NCAR, Rbbar, ADDbl, bot, top), true) + +lags = map(x -> initial_dict[x] isa Number ? initial_dict[x] : 0.0, growth.endogenous_variables)[:,:] +exos_const = map(x -> initial_dict[x], growth.exogenous_variables) +exos = hcat(exos_const, exos_const) +param_values = map(x -> params_dict[x], growth.parameters) + +a = fill(1.0, length(growth.endogenous_variables)) +growth.f!(a, fill(1.0, length(growth.endogenous_variables)), lags, exos, param_values) + +solve(growth, lags, exos, param_values) + +# a = :(((1 + BANDb) < ER <= (1 + BANDₜ)) ? 1 : 0) +# Consistent.replace_vars( +# [a], +# Symbol[], +# Symbol[:BANDb, :ER], +# Symbol[], +# ) +# b = Consistent.create_vars( +# b, +# Set(Symbol[:BANDb]), +# Symbol[], +# Symbol[:BANDb], +# [:endos, :lags, :exos] +# ) \ No newline at end of file From 82f39ffef17e3afd2c2e06964a2bdf6d29110832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Mon, 30 Oct 2023 22:49:52 +0100 Subject: [PATCH 12/35] Param values should be correct --- src/models/GROWTH.jl | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 33a4b81..7338871 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -272,16 +272,12 @@ params_dict = @parameters begin end initial_dict = @parameters begin - Rbbar = 0.035 - - σₛₑ = 0.16667 - η = 0.04918 - ϕ = 0.26417 - ϕₜ = 0.26417 ADDbl = 0.02 BANDₜ = 0.01 BANDb = 0.01 + Rbbar = 0.035 bot = 0.05 + top = 0.12 GRg = 0.03 GRpr = 0.03 Nfe = 87.181 @@ -290,7 +286,12 @@ initial_dict = @parameters begin Rbbar = 0.035 Rln = 0.07 RA = 0 - top = 0.12 + + ϕ = 0.26417 + ϕₜ = 0.26417 + σₛₑ = 0.16667 + η = 0.04918 + ADDl = 0.04592 BLR = 0.1091 BUR = 0.06324 @@ -313,7 +314,7 @@ initial_dict = @parameters begin GRk = 0.03001 INV = 16911600 Ik = 2357910 - N = 87.181 + N = 87.181 # ??? NT = 87.181 NHUC = 5.6735 NL = 683593 @@ -355,6 +356,7 @@ initial_dict = @parameters begin z1b = 0 z2a = 0 z2b = 0 + Bbd = 4389790 Bbs = 4389790 Bcbd = 4655690 @@ -382,7 +384,7 @@ initial_dict = @parameters begin Lfs = 15962900 Lhd = 21606600 Lhs = 21606600 - Ls = 37569500 + Ls = 37569500 # ??? Mₕ = 40510800 Ms = 40510800 OFb = 3474030 @@ -391,7 +393,7 @@ initial_dict = @parameters begin V = 165438779 Vfma = 159334599 Vk = 23066350 - Vf = 31361792 + Vf = 31361792 # ??? z₃ = missing z₄ = missing @@ -414,9 +416,9 @@ exos = hcat(exos_const, exos_const) param_values = map(x -> params_dict[x], growth.parameters) a = fill(1.0, length(growth.endogenous_variables)) -growth.f!(a, fill(1.0, length(growth.endogenous_variables)), lags, exos, param_values) - -solve(growth, lags, exos, param_values) +growth.f!(a, lags[:, 1], lags, exos, param_values) +# growth.equations[findfirst(==(:G), growth.endogenous_variables)] +solve(growth, lags, exos, param_values, initial=lags[:, 1], solver=:newton) # a = :(((1 + BANDb) < ER <= (1 + BANDₜ)) ? 1 : 0) # Consistent.replace_vars( From 4dc9f449a655a65874d95b82388062e99790c220 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Mon, 30 Oct 2023 23:39:53 +0100 Subject: [PATCH 13/35] Works finally --- src/models/GROWTH.jl | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 7338871..51e1b3f 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -59,7 +59,8 @@ firms₃ = model( eqs=@equations begin p = (1 + ϕ) * NHUC # 11.29: Normal-cost pricing ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]) # 11.30: Actual mark-up --> ε₂ not in the book - ϕₜ = (FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * S) # 11.32: Expected historical costs ??? + # 11.32: Expected historical costs ??? + ϕₜ = (FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * Skₑ * UC + (1 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1]) HCe = (1 - σₛₑ) * Skₑ * UC + (1 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1] # 11.33: Opening inventories to expected sales ratio σₛₑ = INk[-1] / Skₑ # 11.34: Planned entrepeneurial profits of firms Fft = FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1]) # 11.34: Planned entrepeneurial profits of firms @@ -94,7 +95,7 @@ hh₁ = model( YDhs = YDᵣ + CG # 11.48: Haig-Simons disposable income CG = (Pbl - Pbl[-1]) * BLd[-1] + (Pₑ - Pₑ[-1]) * ed[-1] + (OFb - OFb[-1]) # 11.49: Capital gains # 11.50: Wealth - V = V[-1] + YDᵣ - CONS # ??? fixed + V = V[-1] + YDᵣ - CONS + (Pbl - Pbl[-1]) * BLd[-1] + (Pₑ - Pₑ[-1]) * ed[-1] + (OFb - OFb[-1]) # ??? Vk = V / p # 11.51: Real stock of wealth CONS = Ck * p # 11.52: Consumption Ck = α₁ * (YDkre + NLk) + α₂ * Vk[-1] # 11.53: Real consumption @@ -410,27 +411,19 @@ growth = aggr + firms₁ + firms₂ + firms₃ + firms₄ + hh₁ + hh₂ + hh growth = add_params(growth, Consistent.Variables(params_dict)) growth = add_exos(growth, @variables(RA, GRpr, GRg, BANDb, BANDₜ, ER, NCAR, Rbbar, ADDbl, bot, top), true) -lags = map(x -> initial_dict[x] isa Number ? initial_dict[x] : 0.0, growth.endogenous_variables)[:,:] -exos_const = map(x -> initial_dict[x], growth.exogenous_variables) +lags = map(x -> initial_dict[x] isa Number ? Float64(initial_dict[x]) : 0.0, growth.endogenous_variables)[:,:] +exos_const = map(x -> Float64(initial_dict[x]), growth.exogenous_variables) exos = hcat(exos_const, exos_const) -param_values = map(x -> params_dict[x], growth.parameters) +param_values = map(x -> Float64(params_dict[x]), growth.parameters) -a = fill(1.0, length(growth.endogenous_variables)) -growth.f!(a, lags[:, 1], lags, exos, param_values) +# a = fill(1.0, length(growth.endogenous_variables)) +# growth.f!(a, lags[:, 1], lags, exos, param_values) +# b = similar(lags) +# fill!(b, 0.0) +solve(growth, lags, exos, param_values, initial=lags[:, 1], method=:broyden) # growth.equations[findfirst(==(:G), growth.endogenous_variables)] -solve(growth, lags, exos, param_values, initial=lags[:, 1], solver=:newton) -# a = :(((1 + BANDb) < ER <= (1 + BANDₜ)) ? 1 : 0) -# Consistent.replace_vars( -# [a], -# Symbol[], -# Symbol[:BANDb, :ER], -# Symbol[], -# ) -# b = Consistent.create_vars( -# b, -# Set(Symbol[:BANDb]), -# Symbol[], -# Symbol[:BANDb], -# [:endos, :lags, :exos] +# NLsolve.nlsolve( +# (x, y) -> growth.f!(x, y, lags, exos, param_values), +# lags[:, 1] # ) \ No newline at end of file From 14c14247ad645886763243a73b49307766d4cb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Tue, 14 Nov 2023 11:28:34 +0100 Subject: [PATCH 14/35] GROWTH works --- src/models/GROWTH.jl | 128 ++++++++++++++++++++++++++++++------------- 1 file changed, 91 insertions(+), 37 deletions(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 51e1b3f..7b7dc4b 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -5,14 +5,14 @@ aggr = model( exos=@variables(Sk, p), eqs=@equations begin Yk = Skₑ + INkₑ - INk[-1] # 11.1: Real output - Skₑ = β * Sk + (1 - β) * Sk[-1] * (1 + GRpr + RA) # 11.2: Expected real sales ??? RA + Skₑ = β * Sk + (1.0 - β) * Sk[-1] * (1.0 + GRpr + RA) # 11.2: Expected real sales ??? RA INkₜ = σₜ * Skₑ # 11.3: Long-run inventory target INkₑ = INk[-1] + γ * (INkₜ - INk[-1]) # 11.4: Short-run inventory target INk = INk[-1] + Yk - Sk - NPL / UC # 11.5: Actual real inventories ??? - NPL / UC - Kk = Kk[-1] * (1 + GRk) # 11.6: Real capital stock + Kk = Kk[-1] * (1.0 + GRk) # 11.6: Real capital stock GRk = γ₀ + γᵤ * U[-1] - γᵣ * RRₗ # 11.7: Growth of real capital stock ??? [-1] U = Yk / Kk[-1] # 11.8: Capital utilization proxy - RRₗ = ((1 + rₗ) / (1 + π)) - 1 # 11.9: Real interest rate on loans + RRₗ = ((1.0 + rₗ) / (1.0 + π)) - 1.0 # 11.9: Real interest rate on loans π = (p - p[-1]) / p[-1] # 11.10: Rate of price inflation Ik = (Kk - Kk[-1]) + δ * Kk[-1] # 11.11: Real gross investment ??? end @@ -36,20 +36,20 @@ firms₂ = model( exos=@variables(Nfe, p, Rln, ER, BANDb), verbose=true, eqs=@equations begin - ωT = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z₃ * (1 - ER) - z₄ * BANDₜ + z₅ * BANDb)) # 11.18: Real wage aspirations ??? + ωT = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z₃ * (1.0 - ER) - z₄ * BANDₜ + z₅ * BANDb)) # 11.18: Real wage aspirations ??? ER = N[-1] / Nfe[-1] # 11.19: Employment rate # 11.20: Switch variables - z₃ = ((1 + BANDb) < ER <= (1 + BANDₜ)) ? 1 : 0 # Band activation - z₄ = ER > (1 + BANDₜ) ? 1 : 0 # Upper band overshoot - z₅ = ER < (1 - BANDb) ? 1 : 0 # Lower band undershoot + z₃ = ((1.0 - BANDb) < ER <= (1.0 + BANDₜ)) ? 1.0 : 0.0 # Band activation + z₄ = ER > (1.0 + BANDₜ) ? 1.0 : 0.0 # Upper band overshoot + z₅ = ER < (1.0 - BANDb) ? 1.0 : 0.0 # Lower band undershoot W = W[-1] + Ω₃ * (ωT * p[-1] - W[-1]) # 11.21: Nominal wage - PR = PR[-1] * (1 + GRpr) # 11.22: Labor productivity + PR = PR[-1] * (1.0 + GRpr) # 11.22: Labor productivity NT = Yk / PR # 11.23: Desired employment N = N[-1] + ηₙ * (NT - N[-1]) # 11.24: Actual employment --> ηₙ not in the book WB = N * W # 11.25: Nominal wage bill UC = WB / Yk # 11.26: Actual unit cost NUC = W / PR # 11.27: Normal unit cost - NHUC = (1 - σₙ) * NUC + σₙ * (1 + Rln[-1]) * NUC[-1] # 11.28: Normal historic unit cost + NHUC = (1.0 - σₙ) * NUC + σₙ * (1.0 + Rln[-1]) * NUC[-1] # 11.28: Normal historic unit cost end ) @@ -57,11 +57,11 @@ firms₂ = model( firms₃ = model( exos=@variables(p, rₗ, Lfd, IN, UC, INk, INV, Ff), eqs=@equations begin - p = (1 + ϕ) * NHUC # 11.29: Normal-cost pricing + p = (1.0 + ϕ) * NHUC # 11.29: Normal-cost pricing ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]) # 11.30: Actual mark-up --> ε₂ not in the book # 11.32: Expected historical costs ??? - ϕₜ = (FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1])) / ((1 - σₛₑ) * Skₑ * UC + (1 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1]) - HCe = (1 - σₛₑ) * Skₑ * UC + (1 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1] # 11.33: Opening inventories to expected sales ratio + ϕₜ = (FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1])) / ((1.0 - σₛₑ) * Skₑ * UC + (1.0 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1]) + HCe = (1.0 - σₛₑ) * Skₑ * UC + (1.0 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1] # 11.33: Opening inventories to expected sales ratio σₛₑ = INk[-1] / Skₑ # 11.34: Planned entrepeneurial profits of firms Fft = FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1]) # 11.34: Planned entrepeneurial profits of firms FUft = ψᵤ * INV[-1] # 11.35: Planned rηined earnings of firms @@ -77,7 +77,7 @@ firms₄ = model( FUf = Ff - FDf - rₗ[-1] * (Lfd[-1] - IN[-1]) + rₗ[-1] * NPL # 11.38: Retained earnings of firms Lfd = Lfd[-1] + INV + (IN - IN[-1]) - FUf - (Eks - Eks[-1]) * Pₑ - NPL # 11.39: Demand for loans by firms NPL = NPLk * Lfs[-1] # 11.40: Defaulted loans ??? Lfd - Eks = Eks[-1] + ((1 - ψᵤ) * INV[-1]) / Pₑ # 11.41: Supply of equities issued by firms + Eks = Eks[-1] + ((1.0 - ψᵤ) * INV[-1]) / Pₑ # 11.41: Supply of equities issued by firms Rk = FDf / (Pₑ[-1] * ed[-1]) # 11.42: Dividend yield of firms PE = Pₑ / (Ff / Eks[-1]) # 11.43: Price earnings ratio Q = (Eks * Pₑ + Lfd) / (K + IN) # 11.44: Tobins Q ratio @@ -99,7 +99,7 @@ hh₁ = model( Vk = V / p # 11.51: Real stock of wealth CONS = Ck * p # 11.52: Consumption Ck = α₁ * (YDkre + NLk) + α₂ * Vk[-1] # 11.53: Real consumption - YDkre = ε * YDkr + (1 - ε) * (YDkr[-1] * (1 + GRpr)) # 11.54: Expected real regular disposable income + YDkre = ε * YDkr + (1.0 - ε) * (YDkr[-1] * (1.0 + GRpr)) # 11.54: Expected real regular disposable income YDkr = YDᵣ / p - ((p - p[-1]) * Vk[-1]) / p # 11.55: Real regular disposable income ??? end ) @@ -141,7 +141,7 @@ gov = model( exos=@variables(BLs, rb, Bbs, Bhs), eqs=@equations begin G = Gk * p # 11.71: Pure government expenditures - Gk = Gk[-1] * (1 + GRg) # 11.72: Real government expenditures + Gk = Gk[-1] * (1.0 + GRg) # 11.72: Real government expenditures PSBR = G + BLs[-1] + rb[-1] * (Bbs[-1] + Bhs[-1]) - TX # 11.73: Government deficit --> BLs[-1] missing in the book # 11.74: New issues of bills Bs = Bs[-1] + G - TX - (BLs - BLs[-1]) * Pbl + rb[-1] * (Bhs[-1] + Bbs[-1]) + BLs[-1] @@ -183,10 +183,10 @@ banks₁ = model( # 11.94: Deposit interest rate rₘ = rₘ[-1] + z1a * ξ₁ + z1b * ξ₂ - z2a * ξ₁ - z2b * ξ₂ # 11.95-97: Mechanism for determining changes to the interest rate on deposits - z2a = (BLR[-1] > (top + 0.05)) ? 1 : 0 - z2b = (BLR[-1] > top) ? 1 : 0 - z1a = (BLR[-1] ≤ bot) ? 1 : 0 - z1b = (BLR[-1] ≤ (bot - 0.05)) ? 1 : 0 + z2a = (BLR[-1] > (top + 0.05)) ? 1.0 : 0.0 + z2b = (BLR[-1] > top) ? 1.0 : 0.0 + z1a = (BLR[-1] ≤ bot) ? 1.0 : 0.0 + z1b = (BLR[-1] ≤ (bot - 0.05)) ? 1.0 : 0.0 end ) @@ -198,13 +198,13 @@ banks₂ = model( OFbt = NCAR * (Lfs[-1] + Lhs[-1]) # 11.99: Long-run own funds target OFbe = OFb[-1] + βb * (OFbt - OFb[-1]) # 11.100: Short-run own funds target FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1] # 11.101: Target rηined earnings of banks - NPLke = εb * NPLke[-1] + (1 - εb) * NPLk[-1] # 11.102: Expected proportion of non-performing loans + NPLke = εb * NPLke[-1] + (1.0 - εb) * NPLk[-1] # 11.102: Expected proportion of non-performing loans FDb = Fb - FUb # 11.103: Dividends of banks Fbt = λb * Y[-1] + (OFbe - OFb[-1] + NPLke * Lfs[-1]) # 11.104: Target profits of banks # 11.105: Actual profits of banks Fb = rₗ[-1] * (Lfs[-1] + Lhs[-1] - NPL) + rb[-1] * Bbd[-1] - rₘ[-1] * Ms[-1] # 11.106: Lending mark-up over deposit rate - ADDl = (Fbt - rb[-1] * Bbd[-1] + rₘ[-1] * (Ms[-1] - (1 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1 - NPLke) * Lfs[-1] + Lhs[-1]) + ADDl = (Fbt - rb[-1] * Bbd[-1] + rₘ[-1] * (Ms[-1] - (1.0 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1.0 - NPLke) * Lfs[-1] + Lhs[-1]) FUb = Fb - λb * Y[-1] # 11.107: Actual rηined earnings OFb = OFb[-1] + FUb - NPL # 11.108: Own funds of banks CAR = OFb / (Lfs + Lhs) @@ -228,17 +228,10 @@ params_dict = @parameters begin ε = 0.5 ε₂ = 0.8 εb = 0.25 - # εrb = 0.9 η₀ = 0.07416 ηₙ = 0.6 ηr = 0.4 θ = 0.22844 - # λ1₀= -0.17071 - # λ1₁= 0 - # λ1₂= 0 - # λ1₃= 0 - # λ1₄ = 0 - # λ1₅ = 0.18 λ₂₀ = 0.25 λ₂₁ = 2.2 λ₂₂ = 6.6 @@ -411,19 +404,80 @@ growth = aggr + firms₁ + firms₂ + firms₃ + firms₄ + hh₁ + hh₂ + hh growth = add_params(growth, Consistent.Variables(params_dict)) growth = add_exos(growth, @variables(RA, GRpr, GRg, BANDb, BANDₜ, ER, NCAR, Rbbar, ADDbl, bot, top), true) +using BenchmarkTools +using DataFrames +using Pipe +using Gadfly + +const T = 350 lags = map(x -> initial_dict[x] isa Number ? Float64(initial_dict[x]) : 0.0, growth.endogenous_variables)[:,:] exos_const = map(x -> Float64(initial_dict[x]), growth.exogenous_variables) exos = hcat(exos_const, exos_const) param_values = map(x -> Float64(params_dict[x]), growth.parameters) -# a = fill(1.0, length(growth.endogenous_variables)) -# growth.f!(a, lags[:, 1], lags, exos, param_values) -# b = similar(lags) -# fill!(b, 0.0) -solve(growth, lags, exos, param_values, initial=lags[:, 1], method=:broyden) +# @report_opt solve(growth, lags, exos, param_values, initial=lags[:, 1], method=:broyden) +# a = similar(lags) +# @report_opt Consistent.f!(a, lags, lags, exos, param_values) # growth.equations[findfirst(==(:G), growth.endogenous_variables)] -# NLsolve.nlsolve( -# (x, y) -> growth.f!(x, y, lags, exos, param_values), -# lags[:, 1] -# ) \ No newline at end of file +# Solve model for 59 periods +function progn(model, lags, exos, param_values; method=:broyden) + results = zeros(length(model.endogenous_variables), T) + results[:, 1] = lags + for i in 1:(T-1) + solution = Consistent.solve(model, results[:, i], exos, param_values, initial=results[:, i], method=method) + results[:, i + 1] = solution + end + return results +end + +@time results = progn(growth, lags, exos, param_values, method=:broyden) +@profview progn(growth, lags, exos, param_values) +# res = @report_opt progn(growth, lags, exos, param_values) +# res_2 = @report_opt solve(growth, zeros(length(growth.endogenous_variables), T), exos, param_values, initial=zeros(length(growth.endogenous_variables), T), method=:broyden) +# res_3 = @report_call solve(growth, zeros(length(growth.endogenous_variables), T), exos, param_values, initial=zeros(length(growth.endogenous_variables), T), method=:broyden) + +# Convert results to DataFrame +df = DataFrame(results', growth.endogenous_variables) +# Add time column +df[!, :period] = 1:nrow(df) +# Select variables, convert to long format, and plot variables +@pipe df |> # Bs + transform(_, [:Bbd, :Bbs, :Bs, :V] .=> (x -> x./_.K) .=> [:Bbd, :Bbs, :Bs, :V]) |> + select(_, [:Bbs, :Bbd, :period]) |> + # select(_, :V, :period) |> + stack(_, Not(:period), variable_name=:variable) |> + # subset(_, :period => ByRow(<(100))) |> + plot( + _, + x=:period, + y=:value, + color=:variable, + Geom.line + ) + +# import Optimization +# import Optim +# using OptimizationOptimJL +# using ForwardDiff +# preacc = similar(lags) +# f = (x, y) -> (growth.f!(x, y, lags, exos, params); return x) +# fct = Optimization.OptimizationFunction( +# (y, p) -> (a = f(preacc, y); max(norm(a) + abs(a[1] - a[1]))), +# Optimization.AutoForwardDiff() +# ) +# prob = Optimization.OptimizationProblem( +# fct, +# lags, +# [0.0] +# ) +# sol = Optimization.solve(prob, BFGS()) + +trick = [12460224.9860725,12398017.9476432,2127097.0384293,2479603.58952865,2061456.84176233,18310339.3067494,0.0301269303691746,0.70100357517028,0.0612956963082538,0.00259172659092623,2431543.27620936,12406886.8952865,89216542.0408732,11592779.7909174,17484956.9240895,131667610.92695,89197235.3803454,116237.612987599,1,1,1,1,0,0,803389.404260357,142818.77,87.245009784586,87.2194058707516,70071146.5224455,5.62358597864547,5.62523682468598,5.6882291468933,7.1908886406281,0.26417,0.26405136720981,70450096.7049297,0.166550008938527,18602444.355,15558672,2758271.805,18397175.5213452,15374225.1731052,16408825.5419017,319258,5186.55622019386,0.0300777557155461,5.08383255337631,0.776836589582287,76947492.7774455,17577885.2500797,57960425.0753658,59940613.2083165,1980188.13295065,170950845.490423,23773257.2473117,54428546.7178933,7569098.81907713,8024149.99562342,8000631.89124685,2877255.27826236,0.0496417214766985,716595.278262361,2160660,22323195.2782624,99653.2298127439,0.0632430491935713,34648785.3532165,876772.054654456,18293.6584078994,41450511.0061155,164598498.971289,186921694.249551,2721427.33589466,5186.55622019386,17303038.3988904,2406244.8,1889913.99881079,43719622.0960025,59660932.180629,162949.15,876772.054654456,34648785.3532165,2721427.33589466,2072525.55030577,4793952.88620044,4793952.88620044,4793952.88620044,0.035,0.055,18.1818181818182,41450511.0061155,16408825.5419017,22323195.2782624,2072525.55030577,4276883.85658557,4276883.81888558,0.103180484753362,0.0193,0,0,0,0,0.0640462845852116,3756950,3587198,432426,0.02,1325097.81,1757523.81,1801244.99324,0.0447462845852116,476147.18324,3630919.18324,0.0937446357394738,31970477.3703734,38732020.8201641,0.75,0.064,0.5,0.4,0.15,0.00122,0.1,0.05,0.10667,0.1,0.5,0.8,0.25,0.9,0.07416,0.6,0.4,0.22844,0.25,2.2,6.6,2.2,2.2,0.1,-0.04341,2.2,2.2,6.6,2.2,0.1,0.67132,2.2,2.2,2.2,6.6,0.1,0.0153,0.05,8e-04,7e-04,0.05,0.1666,0.2,0.15255,0.92,-0.20594,1,2,0.45621,0.02,0.01,0.01,0.05,0.03,0.03,87.181,0.1,0.02,0.035,0.07,0,0.12] +# trick = [12847139.6056919,12773163.234909,2135433.21254525,2554632.6469818,2083143.88137755,18863017.845508,0.0301839594285881,0.701633071373853,0.060862193299259,0.0025224867867597,2505842.43260956,12767232.9676729,92039334.3570995,11742418.5207003,18064687.1632355,135984015.55471,92161581.4557822,119724.741377227,1.00044053028471,1,1,1,0,0,829638.759815988,147103.3331,87.334116331399,87.28823214714,72417700.6650833,5.63687348995558,5.63983658515748,5.70300577691242,7.20902756220914,0.264075093767848,0.26405681412294,72715788.0642565,0.161389688979185,19201099.3326865,16086160.3701624,2806489.12578121,19028797.9481763,15934877.4718595,16961300.8982954,328176.510838035,5261.64950550768,0.0295790150670657,5.07716390433231,0.778279285336612,79478381.8966188,18156041.5604636,59892622.6185124,61740286.2991206,1847663.68060818,176583570.232841,24494783.6180457,56107561.5567029,7782958.39106339,8244418.50426299,8248186.16054174,2983558.34338589,0.0498151226802964,751238.815559653,2232319.52782624,23074434.093822,104208.065384264,0.0631816837213899,35792190.4356194,905667.436927023,18627.4518165163,42835287.9184401,170030847.570879,193105281.664701,2805378.07783515,5261.64950550768,17867085.6371611,2478432.144,1950214.55369503,45144466.0629236,61611146.734324,167788.351017015,905667.436927023,35792190.4356194,2805378.07783515,2141764.39592201,4947142.47375715,4947142.47375715,4947142.47375715,0.035,0.055,18.1818181818182,42835287.9184401,16961300.8982954,23074434.093822,2141764.39592201,4405133.15354705,4405133.11452758,0.102838881879704,0.0193,0,0,0,0,0.0635382041644293,3873202.08201641,3727832.34275056,425089.6703486,0.02,1364717.70131928,1789807.37166788,1809319.61304422,0.0442382041644293,444601.911724938,3747344.5841269,0.0935999947263291,32754010.5378738,40035734.9921174,0.75,0.064,0.5,0.4,0.15,0.00122,0.1,0.05,0.10667,0.1,0.5,0.8,0.25,0.9,0.07416,0.6,0.4,0.22844,0.25,2.2,6.6,2.2,2.2,0.1,-0.04341,2.2,2.2,6.6,2.2,0.1,0.67132,2.2,2.2,2.2,6.6,0.1,0.0153,0.05,8e-04,7e-04,0.05,0.1666,0.2,0.15255,0.92,-0.20594,1,2,0.45621,0.02,0.01,0.01,0.05,0.03,0.03,87.181,0.1,0.02,0.035,0.07,0,0.12] +trick_neu = trick[vcat([1, 2, 4, 3], 5:19, 22:114+2)] +Consistent.solve(growth, results[:, i], exos, param_values, initial=results[:, i], method=method) +argmax(abs.(Vector(df[2, 1:20]) - trick[1:20])) +argmax(abs.(Vector(df[2, 1:114]) - trick_neu)) +(df[2, 22 .+ findall(x -> abs(x) > 0.00000000000001, (Vector(df[2, 23:end-1]) - trick[25:115+25-23-1]) ./ max.(0.00000000001, Vector(df[2, 23:end-1])))]) +(df[3, findall(x -> abs(x) > 10000, (Vector(df[3, :]) - trick[1:115]))]) From ab6ad1979965aa730282232ec62d38661c62cd23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Tue, 14 Nov 2023 11:32:18 +0100 Subject: [PATCH 15/35] Fix bug --- src/Variables.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Variables.jl b/src/Variables.jl index 25c3f66..b20c395 100644 --- a/src/Variables.jl +++ b/src/Variables.jl @@ -17,8 +17,8 @@ function create_missing_indices(line::Expr, vars::Set, symbs::Set) completed_line = deepcopy(line) head = completed_line.head args = completed_line.args - for i in length(args):-1:2 # FIXME: why? - if (typeof(args[i]) == Symbol) && (head == :call) && args[i] in vars # create index + for i in length(args):-1:1 # FIXME: why backwards? + if (typeof(args[i]) == Symbol) && !(head == :ref) && args[i] in vars # create index args[i] = :($(args[i])[0]) elseif typeof(args[i]) == Expr # recursion for nested expressions args[i] = create_missing_indices(args[i], vars, symbs) @@ -74,7 +74,7 @@ function create_vars( # FIXME: read properly position = findall(x -> x == args[1], exos)[1] if length(args) == 2 if args[2] <= 0 - completed_line.args = [name[3], :($position), Expr(:call, :-, :end, args[2])] + completed_line.args = [name[3], :($position), Expr(:call, :+, :end, args[2])] else error("future indices are not allowed!") end From 54817da31b0e9d28ae88dea24068d437d6c4c47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Tue, 14 Nov 2023 14:29:09 +0100 Subject: [PATCH 16/35] Refactor into new file --- src/models/GROWTH.jl | 414 +-------------------------------------- src/models/GROWTH_eqs.jl | 405 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 410 insertions(+), 409 deletions(-) create mode 100644 src/models/GROWTH_eqs.jl diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 7b7dc4b..5eb33ff 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -1,414 +1,10 @@ -operators!([:<, :>, :≤, :<=, :≥, :>=, :exp, :log]) - -# Box 11.1: Aggregation equations -aggr = model( - exos=@variables(Sk, p), - eqs=@equations begin - Yk = Skₑ + INkₑ - INk[-1] # 11.1: Real output - Skₑ = β * Sk + (1.0 - β) * Sk[-1] * (1.0 + GRpr + RA) # 11.2: Expected real sales ??? RA - INkₜ = σₜ * Skₑ # 11.3: Long-run inventory target - INkₑ = INk[-1] + γ * (INkₜ - INk[-1]) # 11.4: Short-run inventory target - INk = INk[-1] + Yk - Sk - NPL / UC # 11.5: Actual real inventories ??? - NPL / UC - Kk = Kk[-1] * (1.0 + GRk) # 11.6: Real capital stock - GRk = γ₀ + γᵤ * U[-1] - γᵣ * RRₗ # 11.7: Growth of real capital stock ??? [-1] - U = Yk / Kk[-1] # 11.8: Capital utilization proxy - RRₗ = ((1.0 + rₗ) / (1.0 + π)) - 1.0 # 11.9: Real interest rate on loans - π = (p - p[-1]) / p[-1] # 11.10: Rate of price inflation - Ik = (Kk - Kk[-1]) + δ * Kk[-1] # 11.11: Real gross investment ??? - end -) - -# Box 11.2: Firms equations -firms₁ = model( - exos=@variables(INk), - eqs=@equations begin - Sk = Ck + Gk + Ik # 11.12: Actual real sales - S = Sk * p # 11.13: Value of realized sales - IN = INk * UC # 11.14: Inventories valued at current cost - INV = Ik * p # 11.15: Nominal gross investment - K = Kk * p # 11.16: Nominal value of fixed capital - Y = Sk * p + (INk - INk[-1]) * UC # 11.17: Nominal GDP - end -) - -# Box 11.3: Firms equations -firms₂ = model( - exos=@variables(Nfe, p, Rln, ER, BANDb), - verbose=true, - eqs=@equations begin - ωT = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z₃ * (1.0 - ER) - z₄ * BANDₜ + z₅ * BANDb)) # 11.18: Real wage aspirations ??? - ER = N[-1] / Nfe[-1] # 11.19: Employment rate - # 11.20: Switch variables - z₃ = ((1.0 - BANDb) < ER <= (1.0 + BANDₜ)) ? 1.0 : 0.0 # Band activation - z₄ = ER > (1.0 + BANDₜ) ? 1.0 : 0.0 # Upper band overshoot - z₅ = ER < (1.0 - BANDb) ? 1.0 : 0.0 # Lower band undershoot - W = W[-1] + Ω₃ * (ωT * p[-1] - W[-1]) # 11.21: Nominal wage - PR = PR[-1] * (1.0 + GRpr) # 11.22: Labor productivity - NT = Yk / PR # 11.23: Desired employment - N = N[-1] + ηₙ * (NT - N[-1]) # 11.24: Actual employment --> ηₙ not in the book - WB = N * W # 11.25: Nominal wage bill - UC = WB / Yk # 11.26: Actual unit cost - NUC = W / PR # 11.27: Normal unit cost - NHUC = (1.0 - σₙ) * NUC + σₙ * (1.0 + Rln[-1]) * NUC[-1] # 11.28: Normal historic unit cost - end -) - -# Box 11.4: Firms equations -firms₃ = model( - exos=@variables(p, rₗ, Lfd, IN, UC, INk, INV, Ff), - eqs=@equations begin - p = (1.0 + ϕ) * NHUC # 11.29: Normal-cost pricing - ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]) # 11.30: Actual mark-up --> ε₂ not in the book - # 11.32: Expected historical costs ??? - ϕₜ = (FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1])) / ((1.0 - σₛₑ) * Skₑ * UC + (1.0 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1]) - HCe = (1.0 - σₛₑ) * Skₑ * UC + (1.0 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1] # 11.33: Opening inventories to expected sales ratio - σₛₑ = INk[-1] / Skₑ # 11.34: Planned entrepeneurial profits of firms - Fft = FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1]) # 11.34: Planned entrepeneurial profits of firms - FUft = ψᵤ * INV[-1] # 11.35: Planned rηined earnings of firms - FDf = ψd * Ff[-1] # 11.36: Dividends of firms - end -) - -# Box 11.5: Firms equations -firms₄ = model( - exos=@variables(rₗ, Lfd, IN, INV, Ff, Lfs, Pₑ, ed), - eqs=@equations begin - Ff = S - WB + (IN - IN[-1]) - rₗ[-1] * IN[-1] # 11.37: Realized entrepeneurial profits - FUf = Ff - FDf - rₗ[-1] * (Lfd[-1] - IN[-1]) + rₗ[-1] * NPL # 11.38: Retained earnings of firms - Lfd = Lfd[-1] + INV + (IN - IN[-1]) - FUf - (Eks - Eks[-1]) * Pₑ - NPL # 11.39: Demand for loans by firms - NPL = NPLk * Lfs[-1] # 11.40: Defaulted loans ??? Lfd - Eks = Eks[-1] + ((1.0 - ψᵤ) * INV[-1]) / Pₑ # 11.41: Supply of equities issued by firms - Rk = FDf / (Pₑ[-1] * ed[-1]) # 11.42: Dividend yield of firms - PE = Pₑ / (Ff / Eks[-1]) # 11.43: Price earnings ratio - Q = (Eks * Pₑ + Lfd) / (K + IN) # 11.44: Tobins Q ratio - end -) - -# TODO: Δ operator -# Box 11.6: Households equations -hh₁ = model( - exos=@variables(rₗ, Pₑ, ed, rₘ, Mₕ, rb, Bhd, BLs, Lhd, Pbl, BLd, OFb, p), - eqs=@equations begin - YP = WB + FDf + FDb + rₘ[-1] * Mₕ[-1] + rb[-1] * Bhd[-1] + BLs[-1] # 11.45: Personal income ??? - TX = θ * YP # 11.46: Income taxes - YDᵣ = YP - TX - rₗ[-1] * Lhd[-1] # 11.47: Regular disposable income - YDhs = YDᵣ + CG # 11.48: Haig-Simons disposable income - CG = (Pbl - Pbl[-1]) * BLd[-1] + (Pₑ - Pₑ[-1]) * ed[-1] + (OFb - OFb[-1]) # 11.49: Capital gains - # 11.50: Wealth - V = V[-1] + YDᵣ - CONS + (Pbl - Pbl[-1]) * BLd[-1] + (Pₑ - Pₑ[-1]) * ed[-1] + (OFb - OFb[-1]) # ??? - Vk = V / p # 11.51: Real stock of wealth - CONS = Ck * p # 11.52: Consumption - Ck = α₁ * (YDkre + NLk) + α₂ * Vk[-1] # 11.53: Real consumption - YDkre = ε * YDkr + (1.0 - ε) * (YDkr[-1] * (1.0 + GRpr)) # 11.54: Expected real regular disposable income - YDkr = YDᵣ / p - ((p - p[-1]) * Vk[-1]) / p # 11.55: Real regular disposable income ??? - end -) - -# Box 11.7: Households equations -hh₂ = model( - exos=@variables(rₗ, YDᵣ), - eqs=@equations begin - GL = η * YDᵣ # 11.56: Gross amount of new personal loans ---> new η here - η = η₀ - ηr * RRₗ # 11.57: New loans to personal income ratio - NL = GL - REP # 11.58: Net amount of new personal loans - REP = δrep * Lhd[-1] # 11.59: Personal loans repayments - Lhd = Lhd[-1] + NL # 11.60: Demand for personal loans - NLk = NL / p # 11.61: Real amount of new personal loans - BUR = (REP + rₗ[-1] * Lhd[-1]) / YDᵣ[-1] # 11.62: Burden of personal debt ??? [-1] - end -) - -# Box 11.8: Households equations - portfolio decisions -hh₃ = model( - exos=@variables(Pₑ, ed, rₘ, Mₕ, rb, Bhd, Lhd, Pbl, BLd, OFb, Rk, Rbl), - eqs=@equations begin - # 11.64: Demand for bills - Bhd = Vfma[-1] * (λ₂₀ + λ₂₂ * rb[-1] - λ₂₁ * rₘ[-1] - λ₂₄ * Rk[-1] - λ₂₃ * Rbl[-1] - λ₂₅ * (YDᵣ / V)) # ??? Vfma = V - # 11.65: Demand for bonds - BLd = Vfma[-1] * (λ₃₀ - λ₃₂ * rb[-1] - λ₃₁ * rₘ[-1] - λ₃₄ * Rk[-1] + λ₃₃ * Rbl[-1] - λ₃₅ * (YDᵣ / V)) / Pbl - # 11.66: Demand for equities - normalized to get the price of equitities - Pₑ = Vfma[-1] * (λ₄₀ - λ₄₂ * rb[-1] - λ₄₁ * rₘ[-1] + λ₄₄ * Rk[-1] - λ₄₃ * Rbl[-1] - λ₄₅ * (YDᵣ / V)) / ed - Mₕ = Vfma - Bhd - Pₑ * ed - Pbl * BLd + Lhd # 11.67: Money deposits - as a residual - Vfma = V - Hhd - OFb # 11.68: Investible wealth - VfmaA = Mₕ + Bhd + Pbl * BLd + Pₑ * ed - Hhd = λc * CONS # 11.69: Households demand for cash - ed = Eks # 11.70: Stock market equilibrium - end -) - -# Box 11.9: Governments equations -gov = model( - exos=@variables(BLs, rb, Bbs, Bhs), - eqs=@equations begin - G = Gk * p # 11.71: Pure government expenditures - Gk = Gk[-1] * (1.0 + GRg) # 11.72: Real government expenditures - PSBR = G + BLs[-1] + rb[-1] * (Bbs[-1] + Bhs[-1]) - TX # 11.73: Government deficit --> BLs[-1] missing in the book - # 11.74: New issues of bills - Bs = Bs[-1] + G - TX - (BLs - BLs[-1]) * Pbl + rb[-1] * (Bhs[-1] + Bbs[-1]) + BLs[-1] - GD = Bbs + Bhs + BLs * Pbl + Hs # 11.75: Government debt - end -) - -# Box 11.10: The Central banks equations -cb = model( - exos=@variables(BLs, rb, Bhs), - eqs=@equations begin - Fcb = rb[-1] * Bcbd[-1] # 11.76: Central bank profits - BLs = BLd # 11.77: Bonds are supplied on demand - Bhs = Bhd # 11.78: Household bills supplied on demand - Hhs = Hhd # 11.79: Cash supplied on demand - Hbs = Hbd # 11.80: Reserves supplied on demand - Hs = Hbs + Hhs # 11.81: Total supply of cash - Bcbd = Hs # 11.82: Central bank - Bcbs = Bcbd # 11.83: Supply of bills to Central bank - rb = Rbbar # 11.84: Interest rate on bills set exogenously - Rbl = rb + ADDbl # 11.85: Long term interest rate - Pbl = 1 / Rbl # 11.86: Price of long-term bonds - end -) - -# Box 11.11: Commercial Banks equations -banks₁ = model( - exos=@variables(Bhs, Bs, Bcbs), - eqs=@equations begin - Ms = Mₕ # 11.87: Bank deposits supplied on demand - Lfs = Lfd # 11.88: Loans to firms supplied on demand - Lhs = Lhd # 11.89: Personal loans supplied on demand - Hbd = ρ * Ms # 11.90: Reserve requirements of banks - # 11.91: Bills supplied to banks - Bbs = Bbs[-1] + (Bs - Bs[-1]) - (Bhs - Bhs[-1]) - (Bcbs - Bcbs[-1]) - # 11.92: Balance sheet constraint of banks - Bbd = Ms + OFb - Lfs - Lhs - Hbd - BLR = Bbd / Ms # 11.93: Bank liquidity ratio - # 11.94: Deposit interest rate - rₘ = rₘ[-1] + z1a * ξ₁ + z1b * ξ₂ - z2a * ξ₁ - z2b * ξ₂ - # 11.95-97: Mechanism for determining changes to the interest rate on deposits - z2a = (BLR[-1] > (top + 0.05)) ? 1.0 : 0.0 - z2b = (BLR[-1] > top) ? 1.0 : 0.0 - z1a = (BLR[-1] ≤ bot) ? 1.0 : 0.0 - z1b = (BLR[-1] ≤ (bot - 0.05)) ? 1.0 : 0.0 - end -) - -# Box 11.12: Commercial banks equations -banks₂ = model( - exos=@variables(Lfs, Lhs, NPLk, Y, rb, Bbd, rₘ, Ms), - eqs=@equations begin - rₗ = rₘ + ADDl # 11.98: Loan interest rate - OFbt = NCAR * (Lfs[-1] + Lhs[-1]) # 11.99: Long-run own funds target - OFbe = OFb[-1] + βb * (OFbt - OFb[-1]) # 11.100: Short-run own funds target - FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1] # 11.101: Target rηined earnings of banks - NPLke = εb * NPLke[-1] + (1.0 - εb) * NPLk[-1] # 11.102: Expected proportion of non-performing loans - FDb = Fb - FUb # 11.103: Dividends of banks - Fbt = λb * Y[-1] + (OFbe - OFb[-1] + NPLke * Lfs[-1]) # 11.104: Target profits of banks - # 11.105: Actual profits of banks - Fb = rₗ[-1] * (Lfs[-1] + Lhs[-1] - NPL) + rb[-1] * Bbd[-1] - rₘ[-1] * Ms[-1] - # 11.106: Lending mark-up over deposit rate - ADDl = (Fbt - rb[-1] * Bbd[-1] + rₘ[-1] * (Ms[-1] - (1.0 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1.0 - NPLke) * Lfs[-1] + Lhs[-1]) - FUb = Fb - λb * Y[-1] # 11.107: Actual rηined earnings - OFb = OFb[-1] + FUb - NPL # 11.108: Own funds of banks - CAR = OFb / (Lfs + Lhs) - Vf = IN + K - Lfd - ed * Pₑ # Firm's wealth (memo for matrices) - # Vg = -Bs - BLs * Pbl # Government's wealth (memo for matrices) - Ls = Lfs + Lhs # Loans supply (memo for matrices) - end -) - -params_dict = @parameters begin - α₁ = 0.75 - α₂ = 0.064 - β = 0.5 - βb = 0.4 - γ = 0.15 - γ₀ = 0.00122 - γᵣ = 0.1 - γᵤ = 0.05 - δ = 0.10667 - δrep = 0.1 - ε = 0.5 - ε₂ = 0.8 - εb = 0.25 - η₀ = 0.07416 - ηₙ = 0.6 - ηr = 0.4 - θ = 0.22844 - λ₂₀ = 0.25 - λ₂₁ = 2.2 - λ₂₂ = 6.6 - λ₂₃ = 2.2 - λ₂₄ = 2.2 - λ₂₅ = 0.1 - λ₃₀ = -0.04341 - λ₃₁ = 2.2 - λ₃₂ = 2.2 - λ₃₃ = 6.6 - λ₃₄ = 2.2 - λ₃₅ = 0.1 - λ₄₀ = 0.67132 - λ₄₁ = 2.2 - λ₄₂ = 2.2 - λ₄₃ = 2.2 - λ₄₄ = 6.6 - λ₄₅ = 0.1 - λb = 0.0153 - λc = 0.05 - ξ₁ = 0.0008 - ξ₂ = 0.0007 - ρ = 0.05 - σₙ = 0.1666 - σₜ = 0.2 - ψd = 0.15255 - ψᵤ = 0.92 - ω₀ = -0.20594 - ω₁ = 1 - ω₂ = 2 - Ω₃ = 0.45621 -end - -initial_dict = @parameters begin - ADDbl = 0.02 - BANDₜ = 0.01 - BANDb = 0.01 - Rbbar = 0.035 - bot = 0.05 - top = 0.12 - GRg = 0.03 - GRpr = 0.03 - Nfe = 87.181 - NCAR = 0.1 - NPLk = 0.02 - Rbbar = 0.035 - Rln = 0.07 - RA = 0 - - ϕ = 0.26417 - ϕₜ = 0.26417 - σₛₑ = 0.16667 - η = 0.04918 - - ADDl = 0.04592 - BLR = 0.1091 - BUR = 0.06324 - Ck = 7334240 - CAR = 0.09245 - CONS = 52603100 - ER = 1 - Fb = 1744130 - Fbt = 1744140 - Ff = 18081100 - Fft = 18013600 - FDb = 1325090 - FDf = 2670970 - FUb = 419039 - FUf = 15153800 - FUft = 15066200 - G = 16755600 - Gk = 2336160 - GL = 2775900 - GRk = 0.03001 - INV = 16911600 - Ik = 2357910 - N = 87.181 # ??? - NT = 87.181 - NHUC = 5.6735 - NL = 683593 - NLk = 95311 - NPL = 309158 - NPLke = 0.02 - NUC = 5.6106 - ωT = 112852 - p = 7.1723 - Pbl = 18.182 - Pₑ = 17937 - PE = 5.07185 - π = 0.0026 - PR = 138659 - PSBR = 1894780 - Q = 0.77443 - rb = 0.035 - Rbl = 0.055 - Rk = 0.03008 - rₗ = 0.06522 - rₘ = 0.0193 - REP = 2092310 - RRₗ = 0.06246 - S = 86270300 - Sk = 12028300 - Skₑ = 12028300 - TX = 17024100 - U = 0.70073 - UC = 5.6106 - W = 777968 - WB = 67824000 - Y = 86607700 - Yk = 12088400 - YDᵣ = 56446400 - YDkr = 7813270 - YDkre = 7813290 - YP = 73158700 - z1a = 0 - z1b = 0 - z2a = 0 - z2b = 0 - - Bbd = 4389790 - Bbs = 4389790 - Bcbd = 4655690 - Bcbs = 4655690 - Bhd = 33439320 - Bhs = 33439320 - Bs = 42484800 - BLd = 840742 - BLs = 840742 - GD = 57728700 - ed = 5112.6001 - Eks = 5112.6001 - Hbd = 2025540 - Hbs = 2025540 - Hhd = 2630150 - Hhs = 2630150 - Hs = 4655690 - IN = 11585400 - INk = 2064890 - INkₑ = 2405660 - INkₜ = 2064890 - K = 127486471 - Kk = 17774838 - Lfd = 15962900 - Lfs = 15962900 - Lhd = 21606600 - Lhs = 21606600 - Ls = 37569500 # ??? - Mₕ = 40510800 - Ms = 40510800 - OFb = 3474030 - OFbe = 3474030 - OFbt = 3638100 - V = 165438779 - Vfma = 159334599 - Vk = 23066350 - Vf = 31361792 # ??? - - z₃ = missing - z₄ = missing - z₅ = missing - HCe = missing - YDhs = missing - CG = missing - VfmaA = missing - Fcb = missing - FUbt = missing -end - -growth = aggr + firms₁ + firms₂ + firms₃ + firms₄ + hh₁ + hh₂ + hh₃ + gov + cb + banks₁ + banks₂ -growth = add_params(growth, Consistent.Variables(params_dict)) -growth = add_exos(growth, @variables(RA, GRpr, GRg, BANDb, BANDₜ, ER, NCAR, Rbbar, ADDbl, bot, top), true) - -using BenchmarkTools +using Consistent using DataFrames using Pipe using Gadfly +include("GROWTH_eqs.jl") + const T = 350 lags = map(x -> initial_dict[x] isa Number ? Float64(initial_dict[x]) : 0.0, growth.endogenous_variables)[:,:] exos_const = map(x -> Float64(initial_dict[x]), growth.exogenous_variables) @@ -425,14 +21,14 @@ function progn(model, lags, exos, param_values; method=:broyden) results = zeros(length(model.endogenous_variables), T) results[:, 1] = lags for i in 1:(T-1) - solution = Consistent.solve(model, results[:, i], exos, param_values, initial=results[:, i], method=method) + solution = solve(model, results[:, i], exos, param_values, initial=results[:, i], method=method) results[:, i + 1] = solution end return results end @time results = progn(growth, lags, exos, param_values, method=:broyden) -@profview progn(growth, lags, exos, param_values) +# @profview progn(growth, lags, exos, param_values) # res = @report_opt progn(growth, lags, exos, param_values) # res_2 = @report_opt solve(growth, zeros(length(growth.endogenous_variables), T), exos, param_values, initial=zeros(length(growth.endogenous_variables), T), method=:broyden) # res_3 = @report_call solve(growth, zeros(length(growth.endogenous_variables), T), exos, param_values, initial=zeros(length(growth.endogenous_variables), T), method=:broyden) diff --git a/src/models/GROWTH_eqs.jl b/src/models/GROWTH_eqs.jl new file mode 100644 index 0000000..477bf9f --- /dev/null +++ b/src/models/GROWTH_eqs.jl @@ -0,0 +1,405 @@ +operators!([:<, :>, :≤, :<=, :≥, :>=, :exp, :log]) + +# Box 11.1: Aggregation equations +aggr = model( + exos=@variables(Sk, p), + eqs=@equations begin + Yk = Skₑ + INkₑ - INk[-1] # 11.1: Real output + Skₑ = β * Sk + (1.0 - β) * Sk[-1] * (1.0 + GRpr + RA) # 11.2: Expected real sales ??? RA + INkₜ = σₜ * Skₑ # 11.3: Long-run inventory target + INkₑ = INk[-1] + γ * (INkₜ - INk[-1]) # 11.4: Short-run inventory target + INk = INk[-1] + Yk - Sk - NPL / UC # 11.5: Actual real inventories ??? - NPL / UC + Kk = Kk[-1] * (1.0 + GRk) # 11.6: Real capital stock + GRk = γ₀ + γᵤ * U[-1] - γᵣ * RRₗ # 11.7: Growth of real capital stock ??? [-1] + U = Yk / Kk[-1] # 11.8: Capital utilization proxy + RRₗ = ((1.0 + rₗ) / (1.0 + π)) - 1.0 # 11.9: Real interest rate on loans + π = (p - p[-1]) / p[-1] # 11.10: Rate of price inflation + Ik = (Kk - Kk[-1]) + δ * Kk[-1] # 11.11: Real gross investment ??? + end +) + +# Box 11.2: Firms equations +firms₁ = model( + exos=@variables(INk), + eqs=@equations begin + Sk = Ck + Gk + Ik # 11.12: Actual real sales + S = Sk * p # 11.13: Value of realized sales + IN = INk * UC # 11.14: Inventories valued at current cost + INV = Ik * p # 11.15: Nominal gross investment + K = Kk * p # 11.16: Nominal value of fixed capital + Y = Sk * p + (INk - INk[-1]) * UC # 11.17: Nominal GDP + end +) + +# Box 11.3: Firms equations +firms₂ = model( + exos=@variables(Nfe, p, Rln, ER, BANDb), + verbose=true, + eqs=@equations begin + ωT = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z₃ * (1.0 - ER) - z₄ * BANDₜ + z₅ * BANDb)) # 11.18: Real wage aspirations ??? + ER = N[-1] / Nfe[-1] # 11.19: Employment rate + # 11.20: Switch variables + z₃ = ((1.0 - BANDb) < ER <= (1.0 + BANDₜ)) ? 1.0 : 0.0 # Band activation + z₄ = ER > (1.0 + BANDₜ) ? 1.0 : 0.0 # Upper band overshoot + z₅ = ER < (1.0 - BANDb) ? 1.0 : 0.0 # Lower band undershoot + W = W[-1] + Ω₃ * (ωT * p[-1] - W[-1]) # 11.21: Nominal wage + PR = PR[-1] * (1.0 + GRpr) # 11.22: Labor productivity + NT = Yk / PR # 11.23: Desired employment + N = N[-1] + ηₙ * (NT - N[-1]) # 11.24: Actual employment --> ηₙ not in the book + WB = N * W # 11.25: Nominal wage bill + UC = WB / Yk # 11.26: Actual unit cost + NUC = W / PR # 11.27: Normal unit cost + NHUC = (1.0 - σₙ) * NUC + σₙ * (1.0 + Rln[-1]) * NUC[-1] # 11.28: Normal historic unit cost + end +) + +# Box 11.4: Firms equations +firms₃ = model( + exos=@variables(p, rₗ, Lfd, IN, UC, INk, INV, Ff), + eqs=@equations begin + p = (1.0 + ϕ) * NHUC # 11.29: Normal-cost pricing + ϕ = ϕ[-1] + ε₂ * (ϕₜ[-1] - ϕ[-1]) # 11.30: Actual mark-up --> ε₂ not in the book + # 11.32: Expected historical costs ??? + ϕₜ = (FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1])) / ((1.0 - σₛₑ) * Skₑ * UC + (1.0 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1]) + HCe = (1.0 - σₛₑ) * Skₑ * UC + (1.0 + rₗ[-1]) * σₛₑ * Skₑ * UC[-1] # 11.33: Opening inventories to expected sales ratio + σₛₑ = INk[-1] / Skₑ # 11.34: Planned entrepeneurial profits of firms + Fft = FUft + FDf + rₗ[-1] * (Lfd[-1] - IN[-1]) # 11.34: Planned entrepeneurial profits of firms + FUft = ψᵤ * INV[-1] # 11.35: Planned rηined earnings of firms + FDf = ψd * Ff[-1] # 11.36: Dividends of firms + end +) + +# Box 11.5: Firms equations +firms₄ = model( + exos=@variables(rₗ, Lfd, IN, INV, Ff, Lfs, Pₑ, ed), + eqs=@equations begin + Ff = S - WB + (IN - IN[-1]) - rₗ[-1] * IN[-1] # 11.37: Realized entrepeneurial profits + FUf = Ff - FDf - rₗ[-1] * (Lfd[-1] - IN[-1]) + rₗ[-1] * NPL # 11.38: Retained earnings of firms + Lfd = Lfd[-1] + INV + (IN - IN[-1]) - FUf - (Eks - Eks[-1]) * Pₑ - NPL # 11.39: Demand for loans by firms + NPL = NPLk * Lfs[-1] # 11.40: Defaulted loans ??? Lfd + Eks = Eks[-1] + ((1.0 - ψᵤ) * INV[-1]) / Pₑ # 11.41: Supply of equities issued by firms + Rk = FDf / (Pₑ[-1] * ed[-1]) # 11.42: Dividend yield of firms + PE = Pₑ / (Ff / Eks[-1]) # 11.43: Price earnings ratio + Q = (Eks * Pₑ + Lfd) / (K + IN) # 11.44: Tobins Q ratio + end +) + +# TODO: Δ operator +# Box 11.6: Households equations +hh₁ = model( + exos=@variables(rₗ, Pₑ, ed, rₘ, Mₕ, rb, Bhd, BLs, Lhd, Pbl, BLd, OFb, p), + eqs=@equations begin + YP = WB + FDf + FDb + rₘ[-1] * Mₕ[-1] + rb[-1] * Bhd[-1] + BLs[-1] # 11.45: Personal income ??? + TX = θ * YP # 11.46: Income taxes + YDᵣ = YP - TX - rₗ[-1] * Lhd[-1] # 11.47: Regular disposable income + YDhs = YDᵣ + CG # 11.48: Haig-Simons disposable income + CG = (Pbl - Pbl[-1]) * BLd[-1] + (Pₑ - Pₑ[-1]) * ed[-1] + (OFb - OFb[-1]) # 11.49: Capital gains + # 11.50: Wealth + V = V[-1] + YDᵣ - CONS + (Pbl - Pbl[-1]) * BLd[-1] + (Pₑ - Pₑ[-1]) * ed[-1] + (OFb - OFb[-1]) # ??? + Vk = V / p # 11.51: Real stock of wealth + CONS = Ck * p # 11.52: Consumption + Ck = α₁ * (YDkre + NLk) + α₂ * Vk[-1] # 11.53: Real consumption + YDkre = ε * YDkr + (1.0 - ε) * (YDkr[-1] * (1.0 + GRpr)) # 11.54: Expected real regular disposable income + YDkr = YDᵣ / p - ((p - p[-1]) * Vk[-1]) / p # 11.55: Real regular disposable income ??? + end +) + +# Box 11.7: Households equations +hh₂ = model( + exos=@variables(rₗ, YDᵣ), + eqs=@equations begin + GL = η * YDᵣ # 11.56: Gross amount of new personal loans ---> new η here + η = η₀ - ηr * RRₗ # 11.57: New loans to personal income ratio + NL = GL - REP # 11.58: Net amount of new personal loans + REP = δrep * Lhd[-1] # 11.59: Personal loans repayments + Lhd = Lhd[-1] + NL # 11.60: Demand for personal loans + NLk = NL / p # 11.61: Real amount of new personal loans + BUR = (REP + rₗ[-1] * Lhd[-1]) / YDᵣ[-1] # 11.62: Burden of personal debt ??? [-1] + end +) + +# Box 11.8: Households equations - portfolio decisions +hh₃ = model( + exos=@variables(Pₑ, ed, rₘ, Mₕ, rb, Bhd, Lhd, Pbl, BLd, OFb, Rk, Rbl), + eqs=@equations begin + # 11.64: Demand for bills + Bhd = Vfma[-1] * (λ₂₀ + λ₂₂ * rb[-1] - λ₂₁ * rₘ[-1] - λ₂₄ * Rk[-1] - λ₂₃ * Rbl[-1] - λ₂₅ * (YDᵣ / V)) # ??? Vfma = V + # 11.65: Demand for bonds + BLd = Vfma[-1] * (λ₃₀ - λ₃₂ * rb[-1] - λ₃₁ * rₘ[-1] - λ₃₄ * Rk[-1] + λ₃₃ * Rbl[-1] - λ₃₅ * (YDᵣ / V)) / Pbl + # 11.66: Demand for equities - normalized to get the price of equitities + Pₑ = Vfma[-1] * (λ₄₀ - λ₄₂ * rb[-1] - λ₄₁ * rₘ[-1] + λ₄₄ * Rk[-1] - λ₄₃ * Rbl[-1] - λ₄₅ * (YDᵣ / V)) / ed + Mₕ = Vfma - Bhd - Pₑ * ed - Pbl * BLd + Lhd # 11.67: Money deposits - as a residual + Vfma = V - Hhd - OFb # 11.68: Investible wealth + VfmaA = Mₕ + Bhd + Pbl * BLd + Pₑ * ed + Hhd = λc * CONS # 11.69: Households demand for cash + ed = Eks # 11.70: Stock market equilibrium + end +) + +# Box 11.9: Governments equations +gov = model( + exos=@variables(BLs, rb, Bbs, Bhs), + eqs=@equations begin + G = Gk * p # 11.71: Pure government expenditures + Gk = Gk[-1] * (1.0 + GRg) # 11.72: Real government expenditures + PSBR = G + BLs[-1] + rb[-1] * (Bbs[-1] + Bhs[-1]) - TX # 11.73: Government deficit --> BLs[-1] missing in the book + # 11.74: New issues of bills + Bs = Bs[-1] + G - TX - (BLs - BLs[-1]) * Pbl + rb[-1] * (Bhs[-1] + Bbs[-1]) + BLs[-1] + GD = Bbs + Bhs + BLs * Pbl + Hs # 11.75: Government debt + end +) + +# Box 11.10: The Central banks equations +cb = model( + exos=@variables(BLs, rb, Bhs), + eqs=@equations begin + Fcb = rb[-1] * Bcbd[-1] # 11.76: Central bank profits + BLs = BLd # 11.77: Bonds are supplied on demand + Bhs = Bhd # 11.78: Household bills supplied on demand + Hhs = Hhd # 11.79: Cash supplied on demand + Hbs = Hbd # 11.80: Reserves supplied on demand + Hs = Hbs + Hhs # 11.81: Total supply of cash + Bcbd = Hs # 11.82: Central bank + Bcbs = Bcbd # 11.83: Supply of bills to Central bank + rb = Rbbar # 11.84: Interest rate on bills set exogenously + Rbl = rb + ADDbl # 11.85: Long term interest rate + Pbl = 1 / Rbl # 11.86: Price of long-term bonds + end +) + +# Box 11.11: Commercial Banks equations +banks₁ = model( + exos=@variables(Bhs, Bs, Bcbs), + eqs=@equations begin + Ms = Mₕ # 11.87: Bank deposits supplied on demand + Lfs = Lfd # 11.88: Loans to firms supplied on demand + Lhs = Lhd # 11.89: Personal loans supplied on demand + Hbd = ρ * Ms # 11.90: Reserve requirements of banks + # 11.91: Bills supplied to banks + Bbs = Bbs[-1] + (Bs - Bs[-1]) - (Bhs - Bhs[-1]) - (Bcbs - Bcbs[-1]) + # 11.92: Balance sheet constraint of banks + Bbd = Ms + OFb - Lfs - Lhs - Hbd + BLR = Bbd / Ms # 11.93: Bank liquidity ratio + # 11.94: Deposit interest rate + rₘ = rₘ[-1] + z1a * ξ₁ + z1b * ξ₂ - z2a * ξ₁ - z2b * ξ₂ + # 11.95-97: Mechanism for determining changes to the interest rate on deposits + z2a = (BLR[-1] > (top + 0.05)) ? 1.0 : 0.0 + z2b = (BLR[-1] > top) ? 1.0 : 0.0 + z1a = (BLR[-1] ≤ bot) ? 1.0 : 0.0 + z1b = (BLR[-1] ≤ (bot - 0.05)) ? 1.0 : 0.0 + end +) + +# Box 11.12: Commercial banks equations +banks₂ = model( + exos=@variables(Lfs, Lhs, NPLk, Y, rb, Bbd, rₘ, Ms), + eqs=@equations begin + rₗ = rₘ + ADDl # 11.98: Loan interest rate + OFbt = NCAR * (Lfs[-1] + Lhs[-1]) # 11.99: Long-run own funds target + OFbe = OFb[-1] + βb * (OFbt - OFb[-1]) # 11.100: Short-run own funds target + FUbt = OFbe - OFb[-1] + NPLke * Lfs[-1] # 11.101: Target rηined earnings of banks + NPLke = εb * NPLke[-1] + (1.0 - εb) * NPLk[-1] # 11.102: Expected proportion of non-performing loans + FDb = Fb - FUb # 11.103: Dividends of banks + Fbt = λb * Y[-1] + (OFbe - OFb[-1] + NPLke * Lfs[-1]) # 11.104: Target profits of banks + # 11.105: Actual profits of banks + Fb = rₗ[-1] * (Lfs[-1] + Lhs[-1] - NPL) + rb[-1] * Bbd[-1] - rₘ[-1] * Ms[-1] + # 11.106: Lending mark-up over deposit rate + ADDl = (Fbt - rb[-1] * Bbd[-1] + rₘ[-1] * (Ms[-1] - (1.0 - NPLke) * Lfs[-1] - Lhs[-1])) / ((1.0 - NPLke) * Lfs[-1] + Lhs[-1]) + FUb = Fb - λb * Y[-1] # 11.107: Actual rηined earnings + OFb = OFb[-1] + FUb - NPL # 11.108: Own funds of banks + CAR = OFb / (Lfs + Lhs) + Vf = IN + K - Lfd - ed * Pₑ # Firm's wealth (memo for matrices) + # Vg = -Bs - BLs * Pbl # Government's wealth (memo for matrices) + Ls = Lfs + Lhs # Loans supply (memo for matrices) + end +) + +params_dict = @parameters begin + α₁ = 0.75 + α₂ = 0.064 + β = 0.5 + βb = 0.4 + γ = 0.15 + γ₀ = 0.00122 + γᵣ = 0.1 + γᵤ = 0.05 + δ = 0.10667 + δrep = 0.1 + ε = 0.5 + ε₂ = 0.8 + εb = 0.25 + η₀ = 0.07416 + ηₙ = 0.6 + ηr = 0.4 + θ = 0.22844 + λ₂₀ = 0.25 + λ₂₁ = 2.2 + λ₂₂ = 6.6 + λ₂₃ = 2.2 + λ₂₄ = 2.2 + λ₂₅ = 0.1 + λ₃₀ = -0.04341 + λ₃₁ = 2.2 + λ₃₂ = 2.2 + λ₃₃ = 6.6 + λ₃₄ = 2.2 + λ₃₅ = 0.1 + λ₄₀ = 0.67132 + λ₄₁ = 2.2 + λ₄₂ = 2.2 + λ₄₃ = 2.2 + λ₄₄ = 6.6 + λ₄₅ = 0.1 + λb = 0.0153 + λc = 0.05 + ξ₁ = 0.0008 + ξ₂ = 0.0007 + ρ = 0.05 + σₙ = 0.1666 + σₜ = 0.2 + ψd = 0.15255 + ψᵤ = 0.92 + ω₀ = -0.20594 + ω₁ = 1 + ω₂ = 2 + Ω₃ = 0.45621 +end + +initial_dict = @parameters begin + ADDbl = 0.02 + BANDₜ = 0.01 + BANDb = 0.01 + Rbbar = 0.035 + bot = 0.05 + top = 0.12 + GRg = 0.03 + GRpr = 0.03 + Nfe = 87.181 + NCAR = 0.1 + NPLk = 0.02 + Rbbar = 0.035 + Rln = 0.07 + RA = 0 + + ϕ = 0.26417 + ϕₜ = 0.26417 + σₛₑ = 0.16667 + η = 0.04918 + + ADDl = 0.04592 + BLR = 0.1091 + BUR = 0.06324 + Ck = 7334240 + CAR = 0.09245 + CONS = 52603100 + ER = 1 + Fb = 1744130 + Fbt = 1744140 + Ff = 18081100 + Fft = 18013600 + FDb = 1325090 + FDf = 2670970 + FUb = 419039 + FUf = 15153800 + FUft = 15066200 + G = 16755600 + Gk = 2336160 + GL = 2775900 + GRk = 0.03001 + INV = 16911600 + Ik = 2357910 + N = 87.181 # ??? + NT = 87.181 + NHUC = 5.6735 + NL = 683593 + NLk = 95311 + NPL = 309158 + NPLke = 0.02 + NUC = 5.6106 + ωT = 112852 + p = 7.1723 + Pbl = 18.182 + Pₑ = 17937 + PE = 5.07185 + π = 0.0026 + PR = 138659 + PSBR = 1894780 + Q = 0.77443 + rb = 0.035 + Rbl = 0.055 + Rk = 0.03008 + rₗ = 0.06522 + rₘ = 0.0193 + REP = 2092310 + RRₗ = 0.06246 + S = 86270300 + Sk = 12028300 + Skₑ = 12028300 + TX = 17024100 + U = 0.70073 + UC = 5.6106 + W = 777968 + WB = 67824000 + Y = 86607700 + Yk = 12088400 + YDᵣ = 56446400 + YDkr = 7813270 + YDkre = 7813290 + YP = 73158700 + z1a = 0 + z1b = 0 + z2a = 0 + z2b = 0 + + Bbd = 4389790 + Bbs = 4389790 + Bcbd = 4655690 + Bcbs = 4655690 + Bhd = 33439320 + Bhs = 33439320 + Bs = 42484800 + BLd = 840742 + BLs = 840742 + GD = 57728700 + ed = 5112.6001 + Eks = 5112.6001 + Hbd = 2025540 + Hbs = 2025540 + Hhd = 2630150 + Hhs = 2630150 + Hs = 4655690 + IN = 11585400 + INk = 2064890 + INkₑ = 2405660 + INkₜ = 2064890 + K = 127486471 + Kk = 17774838 + Lfd = 15962900 + Lfs = 15962900 + Lhd = 21606600 + Lhs = 21606600 + Ls = 37569500 # ??? + Mₕ = 40510800 + Ms = 40510800 + OFb = 3474030 + OFbe = 3474030 + OFbt = 3638100 + V = 165438779 + Vfma = 159334599 + Vk = 23066350 + Vf = 31361792 # ??? + + z₃ = missing + z₄ = missing + z₅ = missing + HCe = missing + YDhs = missing + CG = missing + VfmaA = missing + Fcb = missing + FUbt = missing +end + +growth = aggr + firms₁ + firms₂ + firms₃ + firms₄ + hh₁ + hh₂ + hh₃ + gov + cb + banks₁ + banks₂ +growth = add_params(growth, Consistent.Variables(params_dict)) +growth = add_exos(growth, @variables(RA, GRpr, GRg, BANDb, BANDₜ, ER, NCAR, Rbbar, ADDbl, bot, top), true) \ No newline at end of file From fafc1fa7401053d0aeac5677e92e8a2d1e8f1ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Wed, 15 Nov 2023 23:32:31 +0100 Subject: [PATCH 17/35] Extend ideas to calibrate --- examples/calibrate.jl | 44 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/examples/calibrate.jl b/examples/calibrate.jl index ea56f59..2498300 100644 --- a/examples/calibrate.jl +++ b/examples/calibrate.jl @@ -1,15 +1,41 @@ using Consistent using DataFrames -using ForwardDiff - -function calibrate!(data, model, parameters=Dict()) - x -> model.f!(y, x, ...) + x - function loss(data,) +using Zygote +using Optimization +using OptimizationOptimJL +function calibrate!(data, model, parameters=Dict(), initial_parameters=Dict()) + exos = data... + lags = ... + F = similar(...) + function g!(F, x) + return model.f!(F, x, lags, exos, params) + end + function loss(params) + sol = solve(model, lags, exos, params; initial=fill(1.0, length(model.endogenous_variables)), method=:newton) end - ForwardDiff.derivative(() -> loss, input) + deriv = x -> ForwardDiff.derivative(loss, x) + initial = ... + prob = OptimizationProblem(loss, deriv, initial, p) + solve(prob, BFGS()) +end + +# Given data for exogenous variables +df = DataFrame(:G => 20 .+ 10 * rand(60)) + +# Define model +sim = Consistent.SIM()[:model] +exos = permutedims(Matrix(df[!, sim.exogenous_variables])) +lags = Consistent.SIM()[:lags] +params_dict = Consistent.SIM()[:params] +param_values = map(x -> params_dict[x], sim.parameters) + +# Solve model for 59 periods +for i in 1:59 + # assume we have some randomness in our parameters + solution = Consistent.solve(sim, lags, exos[:, begin:i], param_values + 0.1 * rand(3)) + lags = hcat(lags, solution) end -model = SIM() -df = DataFrame(reverse(lags[:, end-20:end-1], dims=2)', model.endogenous_variables) -df \ No newline at end of file +# Convert results to DataFrame +df = hcat(df, DataFrame(lags', sim.endogenous_variables)) \ No newline at end of file From 48f6bd7f4cd2fb2105faf2ac29bc82b88e180e3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 16 Nov 2023 03:38:51 +0100 Subject: [PATCH 18/35] New file for prognosis --- src/Prognose.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/Prognose.jl diff --git a/src/Prognose.jl b/src/Prognose.jl new file mode 100644 index 0000000..e488961 --- /dev/null +++ b/src/Prognose.jl @@ -0,0 +1,10 @@ +function prognose!(results, horizon, model, exos, param_values; method=:broyden) + for i in horizon + sol = Consistent.solve_nonlinear(model, results[:, begin:i-1], exos, param_values, initial=results[:, i], method=method) + if sol.retcode == ReturnCode.Failure + return ReturnCode.Failure + end + results[:, i] = sol.u + end + return ReturnCode.Success +end \ No newline at end of file From a657c664330c5b939a2c960f4c2e73aaffa6c9e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 16 Nov 2023 03:39:02 +0100 Subject: [PATCH 19/35] Include prognosis --- src/Consistent.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Consistent.jl b/src/Consistent.jl index 0652831..133494c 100644 --- a/src/Consistent.jl +++ b/src/Consistent.jl @@ -1,7 +1,7 @@ module Consistent export @parameters, @equations, @variables -export model, solve, operators!, add_params, add_exos +export model, solve, operators!, add_params, add_exos, prognose! include("Helpers.jl") include("ModelComponents.jl") @@ -11,6 +11,7 @@ include("ConstructResiduals.jl") include("Macros.jl") include("CombineModels.jl") include("Solve.jl") +include("Prognose.jl") # Godley/Lavoie include("models/SIM.jl") From a8b3a1a9a4075f1f59d1a37bcb66e800a5537a83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 16 Nov 2023 03:39:22 +0100 Subject: [PATCH 20/35] New nonlinear solve variant --- src/Solve.jl | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Solve.jl b/src/Solve.jl index 9f26e8f..da0b91d 100644 --- a/src/Solve.jl +++ b/src/Solve.jl @@ -1,9 +1,38 @@ using NLsolve +using LinearAlgebra +import Optimization +import Optim +using NonlinearSolve +using SimpleNonlinearSolve -function solve(model, lags, exos, params, initial=fill(1.0, length(model.endogenous_variables))) +function solve(model, lags, exos, params; initial=fill(1.0, length(model.endogenous_variables)), method=:newton) nlsolve( - (x, y) -> model.f!(x, y, lags, exos, params), + (F, x) -> model.f!(F, x, lags, exos, params), initial, autodiff = :forward, + method = method, + # iterations = 500, + ftol = 1e-40, + xtol = 1e-40 ).zero -end \ No newline at end of file +end + +function solve_nonlinear(model, lags, exos, params; initial=fill(1.0, length(model.endogenous_variables)), method=:newton) + prob = NonlinearSolve.NonlinearProblem( + (F, x, p) -> model.f!(F, x, lags, exos, params), + initial,# + abstol = 1e-40, reltol = 1e-40 + ) + sol = NonlinearSolve.solve(prob, NonlinearSolve.TrustRegion()) + return sol +end + +# function solve_optim(model, lags, exos, params; initial=fill(1.0, length(model.endogenous_variables)), method=:newton) +# f = (x, y) -> model.f!(x, y, lags, exos, params) +# prob = Optimization.OptimizationProblem( +# x -> max(norm(f(x, y)) + abs(f(x, y)[1] - f(x, y)[1])), +# initial, +# Optimization.AutoForwardDiff() +# ) +# sol = Optimization.solve(prob, Optim.BFGS()) +# end \ No newline at end of file From 0d0d9b45aad6af2bfdb848c23b82db1d07251f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 16 Nov 2023 03:39:41 +0100 Subject: [PATCH 21/35] calibrate method almost ready --- examples/calibrate.jl | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/examples/calibrate.jl b/examples/calibrate.jl index 2498300..a2715a1 100644 --- a/examples/calibrate.jl +++ b/examples/calibrate.jl @@ -3,21 +3,26 @@ using DataFrames using Zygote using Optimization using OptimizationOptimJL +using SciMLSensitivity function calibrate!(data, model, parameters=Dict(), initial_parameters=Dict()) - exos = data... - lags = ... - F = similar(...) - function g!(F, x) - return model.f!(F, x, lags, exos, params) - end + exos = permutedims(Matrix(data[!, model.exogenous_variables])) + results = similar(Matrix(data[!, model.endogenous_variables])') # lags in + results[:, 1] = Matrix(data[1, model.exogenous_variables])' function loss(params) - sol = solve(model, lags, exos, params; initial=fill(1.0, length(model.endogenous_variables)), method=:newton) + sol = prognose!(results, horizon, model, exos, param_values; method=:broyden) + if sol == ReturnCode.Failure + return Inf + else + return ... + end end - deriv = x -> ForwardDiff.derivative(loss, x) initial = ... - prob = OptimizationProblem(loss, deriv, initial, p) - solve(prob, BFGS()) + adtype = Optimization.AutoZygote() + optf = Optimization.OptimizationFunction((x, p) -> loss(x), adtype) + optprob = Optimization.OptimizationProblem(optf, initial) + res = Optimization.solve(optprob, ADAM(), maxiters = 1000) + return res end # Given data for exogenous variables @@ -38,4 +43,5 @@ for i in 1:59 end # Convert results to DataFrame -df = hcat(df, DataFrame(lags', sim.endogenous_variables)) \ No newline at end of file +df = hcat(df, DataFrame(lags', sim.endogenous_variables)) +calibrate!(df, sim, Dict(:θ => 0.2), Dict(:α_1 => 0.5, :α_2 => 0.5)) \ No newline at end of file From bec3f64101716706e61ee17860d1e4bfd3c1ebcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 16 Nov 2023 12:10:15 +0100 Subject: [PATCH 22/35] Running calibration but not working --- examples/calibrate.jl | 49 +++++++++++++++++++++++++++++++++++-------- src/Consistent.jl | 1 + src/Loss.jl | 2 ++ src/Solve.jl | 5 +++-- 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/examples/calibrate.jl b/examples/calibrate.jl index a2715a1..134ee11 100644 --- a/examples/calibrate.jl +++ b/examples/calibrate.jl @@ -5,23 +5,52 @@ using Optimization using OptimizationOptimJL using SciMLSensitivity -function calibrate!(data, model, parameters=Dict(), initial_parameters=Dict()) +function calibrate(data, model, opt_vars, opt_params, init_params) + # Set up variables exos = permutedims(Matrix(data[!, model.exogenous_variables])) - results = similar(Matrix(data[!, model.endogenous_variables])') # lags in - results[:, 1] = Matrix(data[1, model.exogenous_variables])' - function loss(params) - sol = prognose!(results, horizon, model, exos, param_values; method=:broyden) + reference_results = permutedims(Matrix(data[!, model.endogenous_variables])) + results = Matrix(data[!, model.endogenous_variables])' # lags in + results[:, 1] = Vector(data[1, model.endogenous_variables])' + param_values = map(x -> init_params[x], model.parameters) + param_opt_inidices = Int[] + for (i, p) in enumerate(model.parameters) + if p in opt_params + push!(param_opt_inidices, i) + end + end + var_opt_indices = Int[] + for (i, var) in enumerate(model.endogenous_variables) + if var in opt_vars + push!(var_opt_indices, i) + end + end + initial = map(x -> init_params[x], opt_params) + + # println(exos) + # println(param_values) + # println(param_opt_inidices) + # println(var_opt_indices) + # println(initial) + horizon = 2:(ncol(data)) + + # Define loss function + function loss(p) + pvalues = Zygote.Buffer(param_values) + pvalues[param_opt_inidices] = p + bresults = Zygote.Buffer(results) + sol = prognose!(bresults, horizon, model, exos, copy(pvalues); method=:broyden) if sol == ReturnCode.Failure return Inf else - return ... + return Consistent.msrmse(reference_results, copy(bresults)) end end - initial = ... + + # Numerical optimization adtype = Optimization.AutoZygote() optf = Optimization.OptimizationFunction((x, p) -> loss(x), adtype) optprob = Optimization.OptimizationProblem(optf, initial) - res = Optimization.solve(optprob, ADAM(), maxiters = 1000) + res = Optimization.solve(optprob, BFGS(), maxiters = 1000) return res end @@ -44,4 +73,6 @@ end # Convert results to DataFrame df = hcat(df, DataFrame(lags', sim.endogenous_variables)) -calibrate!(df, sim, Dict(:θ => 0.2), Dict(:α_1 => 0.5, :α_2 => 0.5)) \ No newline at end of file +sol = calibrate(df, sim, [:Y, :T, :YD, :C], [:α_1, :α_2], Dict(:α_1 => 0.5, :α_2 => 0.5, :θ => 0.2)) + +# Consistent.solve_nonlinear(sim, lags, exos[:, 1], param_values + 0.1 * rand(3)) \ No newline at end of file diff --git a/src/Consistent.jl b/src/Consistent.jl index 133494c..ebb37b0 100644 --- a/src/Consistent.jl +++ b/src/Consistent.jl @@ -11,6 +11,7 @@ include("ConstructResiduals.jl") include("Macros.jl") include("CombineModels.jl") include("Solve.jl") +include("Loss.jl") include("Prognose.jl") # Godley/Lavoie diff --git a/src/Loss.jl b/src/Loss.jl index 66cc0c3..82ac487 100644 --- a/src/Loss.jl +++ b/src/Loss.jl @@ -1,3 +1,5 @@ +using Statistics + """ Root-mean-square error of a time series. """ diff --git a/src/Solve.jl b/src/Solve.jl index da0b91d..58c31c1 100644 --- a/src/Solve.jl +++ b/src/Solve.jl @@ -19,8 +19,9 @@ end function solve_nonlinear(model, lags, exos, params; initial=fill(1.0, length(model.endogenous_variables)), method=:newton) prob = NonlinearSolve.NonlinearProblem( - (F, x, p) -> model.f!(F, x, lags, exos, params), - initial,# + (F, x, p) -> model.f!(F, x, lags, exos, p), + initial, + params, abstol = 1e-40, reltol = 1e-40 ) sol = NonlinearSolve.solve(prob, NonlinearSolve.TrustRegion()) From ae469a70e33b990bcf4ec3c654ab80767436aa6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 16 Nov 2023 23:20:16 +0100 Subject: [PATCH 23/35] Both calibration methods working --- examples/calibrate.jl | 82 +++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/examples/calibrate.jl b/examples/calibrate.jl index 134ee11..a799450 100644 --- a/examples/calibrate.jl +++ b/examples/calibrate.jl @@ -1,10 +1,32 @@ using Consistent using DataFrames +using Pipe +using Gadfly using Zygote using Optimization using OptimizationOptimJL using SciMLSensitivity +# Given data for exogenous variables +df = DataFrame(:G => 20 .+ 10 * rand(60)) + +# Define model +sim = Consistent.SIM()[:model] +exos = permutedims(Matrix(df[!, sim.exogenous_variables])) +lags = Consistent.SIM()[:lags] +params_dict = Consistent.SIM()[:params] +param_values = map(x -> params_dict[x], sim.parameters) + +# Solve model for 59 periods +for i in 1:59 + # assume we have some randomness in our parameters + solution = Consistent.solve(sim, lags, exos[:, begin:i], param_values + 0.05 * rand(3)) + lags = hcat(lags, solution) +end + +# Convert results to DataFrame +df = hcat(df, DataFrame(lags', sim.endogenous_variables)) + function calibrate(data, model, opt_vars, opt_params, init_params) # Set up variables exos = permutedims(Matrix(data[!, model.exogenous_variables])) @@ -26,53 +48,53 @@ function calibrate(data, model, opt_vars, opt_params, init_params) end initial = map(x -> init_params[x], opt_params) - # println(exos) - # println(param_values) - # println(param_opt_inidices) - # println(var_opt_indices) - # println(initial) - horizon = 2:(ncol(data)) + horizon = 2:(nrow(data)) + # println(horizon) # Define loss function function loss(p) pvalues = Zygote.Buffer(param_values) + pvalues[1:length(param_values)] = param_values pvalues[param_opt_inidices] = p bresults = Zygote.Buffer(results) - sol = prognose!(bresults, horizon, model, exos, copy(pvalues); method=:broyden) + bresults[:] = results + # sol = prognose!(bresults, horizon, model, exos, copy(pvalues); method=:broyden) + sol = onestep_prognose!(bresults, results, horizon, model, exos, copy(pvalues); method=:broyden) + # println(copy(bresults)) + # println(copy(pvalues)) if sol == ReturnCode.Failure return Inf else - return Consistent.msrmse(reference_results, copy(bresults)) + # println(copy(bresults[:, 2:end])) + return Consistent.msrmse(reference_results[:, 2:end], copy(bresults[:, 2:end])) end end # Numerical optimization adtype = Optimization.AutoZygote() optf = Optimization.OptimizationFunction((x, p) -> loss(x), adtype) - optprob = Optimization.OptimizationProblem(optf, initial) - res = Optimization.solve(optprob, BFGS(), maxiters = 1000) + optprob = Optimization.OptimizationProblem(optf, initial, lb = [0.0, 0.0], ub = [1.0, 1.0]) + res = Optimization.solve(optprob, SAMIN(), maxiters = 50000) return res end -# Given data for exogenous variables -df = DataFrame(:G => 20 .+ 10 * rand(60)) - -# Define model -sim = Consistent.SIM()[:model] -exos = permutedims(Matrix(df[!, sim.exogenous_variables])) -lags = Consistent.SIM()[:lags] -params_dict = Consistent.SIM()[:params] -param_values = map(x -> params_dict[x], sim.parameters) - -# Solve model for 59 periods -for i in 1:59 - # assume we have some randomness in our parameters - solution = Consistent.solve(sim, lags, exos[:, begin:i], param_values + 0.1 * rand(3)) - lags = hcat(lags, solution) -end +sol = calibrate(df, sim, [:Y, :C], [:α_1, :α_2], Dict(:α_1 => 0.5, :α_2 => 0.5, :θ => 0.2)) -# Convert results to DataFrame -df = hcat(df, DataFrame(lags', sim.endogenous_variables)) -sol = calibrate(df, sim, [:Y, :T, :YD, :C], [:α_1, :α_2], Dict(:α_1 => 0.5, :α_2 => 0.5, :θ => 0.2)) +# Compare to fit +fitted = deepcopy(lags) +prognose!(fitted, 2:60, sim, exos, vcat(0.2, sol.u); method=:broyden) +# prognose!(fitted, 2:60, sim, exos, vcat(0.2, [0.6, 0.4]); method=:broyden) -# Consistent.solve_nonlinear(sim, lags, exos[:, 1], param_values + 0.1 * rand(3)) \ No newline at end of file +df_fitted = DataFrame(fitted', sim.endogenous_variables) +df_fitted[!, :period] = 1:nrow(df) +df_all = hcat(df, df_fitted, makeunique=true) +@pipe df_all |> + select(_, [:Y, :Y_1, :C, :C_1, :period]) |> + stack(_, Not(:period), variable_name=:variable) |> + plot( + _, + x=:period, + y=:value, + color=:variable, + Geom.line + ) \ No newline at end of file From 3aadbda6ba6e0b86b82ac070cefdf19cbfe0bf95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 16 Nov 2023 23:20:29 +0100 Subject: [PATCH 24/35] Add onestep prog --- src/Consistent.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Consistent.jl b/src/Consistent.jl index ebb37b0..f38f7b8 100644 --- a/src/Consistent.jl +++ b/src/Consistent.jl @@ -1,7 +1,7 @@ module Consistent export @parameters, @equations, @variables -export model, solve, operators!, add_params, add_exos, prognose! +export model, solve, operators!, add_params, add_exos, prognose!, onestep_prognose! include("Helpers.jl") include("ModelComponents.jl") From bf1b3725dd0fbdf2c1c78490dde04503e89bd976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 16 Nov 2023 23:20:45 +0100 Subject: [PATCH 25/35] Fixed bug and onestep prog --- src/Prognose.jl | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Prognose.jl b/src/Prognose.jl index e488961..1b519f7 100644 --- a/src/Prognose.jl +++ b/src/Prognose.jl @@ -1,6 +1,17 @@ function prognose!(results, horizon, model, exos, param_values; method=:broyden) for i in horizon - sol = Consistent.solve_nonlinear(model, results[:, begin:i-1], exos, param_values, initial=results[:, i], method=method) + sol = Consistent.solve_nonlinear(model, results[:, begin:i-1], exos, param_values, initial=results[:, i-1], method=method) + if sol.retcode == ReturnCode.Failure + return ReturnCode.Failure + end + results[:, i] = sol.u + end + return ReturnCode.Success +end + +function onestep_prognose!(results, reference_results, horizon, model, exos, param_values; method=:broyden) + for i in horizon + sol = Consistent.solve_nonlinear(model, reference_results[:, begin:i-1], exos, param_values, initial=reference_results[:, i-1], method=method) if sol.retcode == ReturnCode.Failure return ReturnCode.Failure end From e58e2209e10ca263ba113553f264b99f1246c3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 16 Nov 2023 23:22:03 +0100 Subject: [PATCH 26/35] Fixed imports --- src/Solve.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Solve.jl b/src/Solve.jl index 58c31c1..68e00d7 100644 --- a/src/Solve.jl +++ b/src/Solve.jl @@ -1,9 +1,7 @@ using NLsolve using LinearAlgebra import Optimization -import Optim using NonlinearSolve -using SimpleNonlinearSolve function solve(model, lags, exos, params; initial=fill(1.0, length(model.endogenous_variables)), method=:newton) nlsolve( From d1028cf46879b238a368945e2565b385bc3db345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Sat, 18 Nov 2023 23:49:05 +0100 Subject: [PATCH 27/35] New file for bayesian analysis --- examples/bayesian.jl | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 examples/bayesian.jl diff --git a/examples/bayesian.jl b/examples/bayesian.jl new file mode 100644 index 0000000..25c2013 --- /dev/null +++ b/examples/bayesian.jl @@ -0,0 +1,75 @@ +using Consistent +using Distributions +using DataFrames +using Pipe +using Gadfly +using Random +# using Zygote +# using Optimization +# using OptimizationOptimJL +# using SciMLSensitivity + +# Given data for exogenous variables +df = DataFrame(:G => fill(20, 60)) + +# Define model +sim = Consistent.SIM()[:model] +exos = permutedims(Matrix(df[!, sim.exogenous_variables])) +lags = Consistent.SIM()[:lags] +params_dict = @parameters begin + θ = 0.2 + α_1 = Normal(0.6, 0.01) + α_2 = Normal(0.4, 0.01) +end +priors_dict = @parameters begin + α_1 = Uniform() + α_2 = Uniform() +end + +# let's say we know that some kind of variables is prone to measurement error +# if it is just a constant, we can introduce some bias variable + +function solve( + model, lags, exos, params_dict::AbstractDict; + rng=Random.default_rng(), + initial=fill(1.0, length(model.endogenous_variables)) +) + function sample_param(x) + if x isa Number + return x + else + return rand(rng, x) + end + end +param_values = map(x -> sample_param(params_dict[x]), sim.parameters) + return Consistent.solve( + sim, lags, exos, param_values, initial=initial + ) +end + +# Solve model for 59 periods +for i in 1:59 + solution = solve( + sim, + lags, + exos[:, begin:i], + params_dict + ) + lags = hcat(lags, solution) +end + +# Convert results to DataFrame +df = DataFrame(lags', sim.endogenous_variables) +# Add time column +df[!, :period] = 1:nrow(df) +# Select variables, convert to long format, and plot variables +@pipe df |> + select(_, [:Y, :C, :YD, :period]) |> + stack(_, Not(:period), variable_name=:variable) |> + plot( + _, + x=:period, + y=:value, + color=:variable, + Geom.line + ) \ No newline at end of file From 0ea902895916562d3bcdf1404d4397deefa09cee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 23 Nov 2023 15:38:41 +0100 Subject: [PATCH 28/35] Add proper custom method option --- src/Solve.jl | 6 +++--- src/models/GROWTH.jl | 41 +++++------------------------------------ 2 files changed, 8 insertions(+), 39 deletions(-) diff --git a/src/Solve.jl b/src/Solve.jl index 68e00d7..87c0a68 100644 --- a/src/Solve.jl +++ b/src/Solve.jl @@ -15,14 +15,14 @@ function solve(model, lags, exos, params; initial=fill(1.0, length(model.endogen ).zero end -function solve_nonlinear(model, lags, exos, params; initial=fill(1.0, length(model.endogenous_variables)), method=:newton) - prob = NonlinearSolve.NonlinearProblem( +function solve_nonlinear(model, lags, exos, params; initial=fill(1.0, length(model.endogenous_variables)), method=TrustRegion()) + prob = NonlinearProblem( (F, x, p) -> model.f!(F, x, lags, exos, p), initial, params, abstol = 1e-40, reltol = 1e-40 ) - sol = NonlinearSolve.solve(prob, NonlinearSolve.TrustRegion()) + sol = NonlinearSolve.solve(prob, method) return sol end diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 5eb33ff..34d992d 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -2,6 +2,7 @@ using Consistent using DataFrames using Pipe using Gadfly +using NonlinearSolve include("GROWTH_eqs.jl") @@ -11,27 +12,18 @@ exos_const = map(x -> Float64(initial_dict[x]), growth.exogenous_variables) exos = hcat(exos_const, exos_const) param_values = map(x -> Float64(params_dict[x]), growth.parameters) -# @report_opt solve(growth, lags, exos, param_values, initial=lags[:, 1], method=:broyden) -# a = similar(lags) -# @report_opt Consistent.f!(a, lags, lags, exos, param_values) -# growth.equations[findfirst(==(:G), growth.endogenous_variables)] - # Solve model for 59 periods -function progn(model, lags, exos, param_values; method=:broyden) +function progn(model, lags, exos, param_values; method=NonlinearSolve.TrustRegion()) results = zeros(length(model.endogenous_variables), T) results[:, 1] = lags for i in 1:(T-1) - solution = solve(model, results[:, i], exos, param_values, initial=results[:, i], method=method) + solution = Consistent.solve_nonlinear(model, results[:, i], exos, param_values, initial=results[:, i], method=method) results[:, i + 1] = solution end return results end @time results = progn(growth, lags, exos, param_values, method=:broyden) -# @profview progn(growth, lags, exos, param_values) -# res = @report_opt progn(growth, lags, exos, param_values) -# res_2 = @report_opt solve(growth, zeros(length(growth.endogenous_variables), T), exos, param_values, initial=zeros(length(growth.endogenous_variables), T), method=:broyden) -# res_3 = @report_call solve(growth, zeros(length(growth.endogenous_variables), T), exos, param_values, initial=zeros(length(growth.endogenous_variables), T), method=:broyden) # Convert results to DataFrame df = DataFrame(results', growth.endogenous_variables) @@ -52,28 +44,5 @@ df[!, :period] = 1:nrow(df) Geom.line ) -# import Optimization -# import Optim -# using OptimizationOptimJL -# using ForwardDiff -# preacc = similar(lags) -# f = (x, y) -> (growth.f!(x, y, lags, exos, params); return x) -# fct = Optimization.OptimizationFunction( -# (y, p) -> (a = f(preacc, y); max(norm(a) + abs(a[1] - a[1]))), -# Optimization.AutoForwardDiff() -# ) -# prob = Optimization.OptimizationProblem( -# fct, -# lags, -# [0.0] -# ) -# sol = Optimization.solve(prob, BFGS()) - -trick = [12460224.9860725,12398017.9476432,2127097.0384293,2479603.58952865,2061456.84176233,18310339.3067494,0.0301269303691746,0.70100357517028,0.0612956963082538,0.00259172659092623,2431543.27620936,12406886.8952865,89216542.0408732,11592779.7909174,17484956.9240895,131667610.92695,89197235.3803454,116237.612987599,1,1,1,1,0,0,803389.404260357,142818.77,87.245009784586,87.2194058707516,70071146.5224455,5.62358597864547,5.62523682468598,5.6882291468933,7.1908886406281,0.26417,0.26405136720981,70450096.7049297,0.166550008938527,18602444.355,15558672,2758271.805,18397175.5213452,15374225.1731052,16408825.5419017,319258,5186.55622019386,0.0300777557155461,5.08383255337631,0.776836589582287,76947492.7774455,17577885.2500797,57960425.0753658,59940613.2083165,1980188.13295065,170950845.490423,23773257.2473117,54428546.7178933,7569098.81907713,8024149.99562342,8000631.89124685,2877255.27826236,0.0496417214766985,716595.278262361,2160660,22323195.2782624,99653.2298127439,0.0632430491935713,34648785.3532165,876772.054654456,18293.6584078994,41450511.0061155,164598498.971289,186921694.249551,2721427.33589466,5186.55622019386,17303038.3988904,2406244.8,1889913.99881079,43719622.0960025,59660932.180629,162949.15,876772.054654456,34648785.3532165,2721427.33589466,2072525.55030577,4793952.88620044,4793952.88620044,4793952.88620044,0.035,0.055,18.1818181818182,41450511.0061155,16408825.5419017,22323195.2782624,2072525.55030577,4276883.85658557,4276883.81888558,0.103180484753362,0.0193,0,0,0,0,0.0640462845852116,3756950,3587198,432426,0.02,1325097.81,1757523.81,1801244.99324,0.0447462845852116,476147.18324,3630919.18324,0.0937446357394738,31970477.3703734,38732020.8201641,0.75,0.064,0.5,0.4,0.15,0.00122,0.1,0.05,0.10667,0.1,0.5,0.8,0.25,0.9,0.07416,0.6,0.4,0.22844,0.25,2.2,6.6,2.2,2.2,0.1,-0.04341,2.2,2.2,6.6,2.2,0.1,0.67132,2.2,2.2,2.2,6.6,0.1,0.0153,0.05,8e-04,7e-04,0.05,0.1666,0.2,0.15255,0.92,-0.20594,1,2,0.45621,0.02,0.01,0.01,0.05,0.03,0.03,87.181,0.1,0.02,0.035,0.07,0,0.12] -# trick = [12847139.6056919,12773163.234909,2135433.21254525,2554632.6469818,2083143.88137755,18863017.845508,0.0301839594285881,0.701633071373853,0.060862193299259,0.0025224867867597,2505842.43260956,12767232.9676729,92039334.3570995,11742418.5207003,18064687.1632355,135984015.55471,92161581.4557822,119724.741377227,1.00044053028471,1,1,1,0,0,829638.759815988,147103.3331,87.334116331399,87.28823214714,72417700.6650833,5.63687348995558,5.63983658515748,5.70300577691242,7.20902756220914,0.264075093767848,0.26405681412294,72715788.0642565,0.161389688979185,19201099.3326865,16086160.3701624,2806489.12578121,19028797.9481763,15934877.4718595,16961300.8982954,328176.510838035,5261.64950550768,0.0295790150670657,5.07716390433231,0.778279285336612,79478381.8966188,18156041.5604636,59892622.6185124,61740286.2991206,1847663.68060818,176583570.232841,24494783.6180457,56107561.5567029,7782958.39106339,8244418.50426299,8248186.16054174,2983558.34338589,0.0498151226802964,751238.815559653,2232319.52782624,23074434.093822,104208.065384264,0.0631816837213899,35792190.4356194,905667.436927023,18627.4518165163,42835287.9184401,170030847.570879,193105281.664701,2805378.07783515,5261.64950550768,17867085.6371611,2478432.144,1950214.55369503,45144466.0629236,61611146.734324,167788.351017015,905667.436927023,35792190.4356194,2805378.07783515,2141764.39592201,4947142.47375715,4947142.47375715,4947142.47375715,0.035,0.055,18.1818181818182,42835287.9184401,16961300.8982954,23074434.093822,2141764.39592201,4405133.15354705,4405133.11452758,0.102838881879704,0.0193,0,0,0,0,0.0635382041644293,3873202.08201641,3727832.34275056,425089.6703486,0.02,1364717.70131928,1789807.37166788,1809319.61304422,0.0442382041644293,444601.911724938,3747344.5841269,0.0935999947263291,32754010.5378738,40035734.9921174,0.75,0.064,0.5,0.4,0.15,0.00122,0.1,0.05,0.10667,0.1,0.5,0.8,0.25,0.9,0.07416,0.6,0.4,0.22844,0.25,2.2,6.6,2.2,2.2,0.1,-0.04341,2.2,2.2,6.6,2.2,0.1,0.67132,2.2,2.2,2.2,6.6,0.1,0.0153,0.05,8e-04,7e-04,0.05,0.1666,0.2,0.15255,0.92,-0.20594,1,2,0.45621,0.02,0.01,0.01,0.05,0.03,0.03,87.181,0.1,0.02,0.035,0.07,0,0.12] -trick_neu = trick[vcat([1, 2, 4, 3], 5:19, 22:114+2)] -Consistent.solve(growth, results[:, i], exos, param_values, initial=results[:, i], method=method) -argmax(abs.(Vector(df[2, 1:20]) - trick[1:20])) -argmax(abs.(Vector(df[2, 1:114]) - trick_neu)) -(df[2, 22 .+ findall(x -> abs(x) > 0.00000000000001, (Vector(df[2, 23:end-1]) - trick[25:115+25-23-1]) ./ max.(0.00000000001, Vector(df[2, 23:end-1])))]) -(df[3, findall(x -> abs(x) > 10000, (Vector(df[3, :]) - trick[1:115]))]) +# @report_opt Consistent.f!(a, lags, lags, exos, param_values) +# growth.equations[findfirst(==(:G), growth.endogenous_variables)] \ No newline at end of file From 82502df9bdbdd94879fd27e71e4c48a995c6a158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 23 Nov 2023 15:39:56 +0100 Subject: [PATCH 29/35] New dependencies --- Project.toml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Project.toml b/Project.toml index 9afdbe9..d333877 100644 --- a/Project.toml +++ b/Project.toml @@ -8,10 +8,18 @@ Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f" DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" +Gadfly = "c91e804a-d5a3-530f-b6f0-dfbca275c004" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56" +NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec" +Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba" +OptimizationOptimJL = "36348300-93cb-4f02-beb5-3c3902f8871e" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" Pipe = "b98c9c47-44ae-5843-9183-064241ee97a0" +SciMLSensitivity = "1ed8b502-d754-442c-8d5d-10ac956f44a1" +Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" +Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" [compat] Crayons = "4" From dd48b00c1e77aa9c075bf1b1b0e3596da46cefce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 23 Nov 2023 15:40:29 +0100 Subject: [PATCH 30/35] Fix custom method --- src/Prognose.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Prognose.jl b/src/Prognose.jl index 1b519f7..75b944a 100644 --- a/src/Prognose.jl +++ b/src/Prognose.jl @@ -1,6 +1,6 @@ -function prognose!(results, horizon, model, exos, param_values; method=:broyden) +function prognose!(results, horizon, model, exos, param_values; method=TrustMethod()) for i in horizon - sol = Consistent.solve_nonlinear(model, results[:, begin:i-1], exos, param_values, initial=results[:, i-1], method=method) + sol = Consistent.solve_nonlinear(model, results[:, begin:i-1], exos, param_values, initial=results[:, i-1]) if sol.retcode == ReturnCode.Failure return ReturnCode.Failure end @@ -9,9 +9,9 @@ function prognose!(results, horizon, model, exos, param_values; method=:broyden) return ReturnCode.Success end -function onestep_prognose!(results, reference_results, horizon, model, exos, param_values; method=:broyden) +function onestep_prognose!(results, reference_results, horizon, model, exos, param_values; method=TrustMethod()) for i in horizon - sol = Consistent.solve_nonlinear(model, reference_results[:, begin:i-1], exos, param_values, initial=reference_results[:, i-1], method=method) + sol = Consistent.solve_nonlinear(model, reference_results[:, begin:i-1], exos, param_values, initial=reference_results[:, i-1]) if sol.retcode == ReturnCode.Failure return ReturnCode.Failure end From 558c056c737f229d8f0156b87b5791e7a2e0fea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 23 Nov 2023 16:08:39 +0100 Subject: [PATCH 31/35] Fixed tests --- test/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 54088c3..f5c9a8d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -21,8 +21,8 @@ using Test y = z[-2] * x * b end @test Consistent.replace_vars(test_eqs.args[[2, 4]], [:z, :y], Symbol[:x], [:θ]) == [ - :(z = endos[2] * (lags[2, end - 0] + 0.5 * endos[1]) * params[1] + exos[1, end - -1]), - :(y = lags[1, end - -1] * exos[1, end - 0] * b) + :(endos[1] = endos[2] * (lags[2, end - 0] + 0.5 * endos[1]) * params[1] + exos[1, end + -1]), + :(endos[2] = lags[1, end - -1] * exos[1, end + 0] * b) ] # test non-equation From 883c2528a621067dfa642b61a83579ed24185d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 23 Nov 2023 16:09:27 +0100 Subject: [PATCH 32/35] Remove verbose option --- src/models/GROWTH_eqs.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/models/GROWTH_eqs.jl b/src/models/GROWTH_eqs.jl index 477bf9f..bac6fd4 100644 --- a/src/models/GROWTH_eqs.jl +++ b/src/models/GROWTH_eqs.jl @@ -34,7 +34,6 @@ firms₁ = model( # Box 11.3: Firms equations firms₂ = model( exos=@variables(Nfe, p, Rln, ER, BANDb), - verbose=true, eqs=@equations begin ωT = exp(ω₀ + ω₁ * log(PR) + ω₂ * log(ER + z₃ * (1.0 - ER) - z₄ * BANDₜ + z₅ * BANDb)) # 11.18: Real wage aspirations ??? ER = N[-1] / Nfe[-1] # 11.19: Employment rate @@ -402,4 +401,4 @@ end growth = aggr + firms₁ + firms₂ + firms₃ + firms₄ + hh₁ + hh₂ + hh₃ + gov + cb + banks₁ + banks₂ growth = add_params(growth, Consistent.Variables(params_dict)) -growth = add_exos(growth, @variables(RA, GRpr, GRg, BANDb, BANDₜ, ER, NCAR, Rbbar, ADDbl, bot, top), true) \ No newline at end of file +growth = add_exos(growth, @variables(RA, GRpr, GRg, BANDb, BANDₜ, ER, NCAR, Rbbar, ADDbl, bot, top)) \ No newline at end of file From ee09bfb067cedf4905bcc6289d8dae4cd609219e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 23 Nov 2023 16:09:44 +0100 Subject: [PATCH 33/35] Fix use method --- src/models/GROWTH.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index 34d992d..ece2507 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -23,7 +23,7 @@ function progn(model, lags, exos, param_values; method=NonlinearSolve.TrustRegio return results end -@time results = progn(growth, lags, exos, param_values, method=:broyden) +@time results = progn(growth, lags, exos, param_values) # Convert results to DataFrame df = DataFrame(results', growth.endogenous_variables) From e03eee7ee9c3ee54c6fcedd1f04badf730370f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 23 Nov 2023 16:12:31 +0100 Subject: [PATCH 34/35] Fix format --- src/models/GROWTH.jl | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/models/GROWTH.jl b/src/models/GROWTH.jl index ece2507..23b7f51 100644 --- a/src/models/GROWTH.jl +++ b/src/models/GROWTH.jl @@ -7,23 +7,25 @@ using NonlinearSolve include("GROWTH_eqs.jl") const T = 350 -lags = map(x -> initial_dict[x] isa Number ? Float64(initial_dict[x]) : 0.0, growth.endogenous_variables)[:,:] +lags = map(x -> initial_dict[x] isa Number ? Float64(initial_dict[x]) : 0.0, growth.endogenous_variables)[:, :] exos_const = map(x -> Float64(initial_dict[x]), growth.exogenous_variables) exos = hcat(exos_const, exos_const) param_values = map(x -> Float64(params_dict[x]), growth.parameters) -# Solve model for 59 periods +# Solve model for T periods function progn(model, lags, exos, param_values; method=NonlinearSolve.TrustRegion()) results = zeros(length(model.endogenous_variables), T) results[:, 1] = lags for i in 1:(T-1) solution = Consistent.solve_nonlinear(model, results[:, i], exos, param_values, initial=results[:, i], method=method) - results[:, i + 1] = solution + results[:, i+1] = solution end return results end @time results = progn(growth, lags, exos, param_values) +# compare: +# @time results = progn(growth, lags, exos, param_values, method=NonlinearSolve.GeneralBroyden()) # Convert results to DataFrame df = DataFrame(results', growth.endogenous_variables) @@ -31,18 +33,18 @@ df = DataFrame(results', growth.endogenous_variables) df[!, :period] = 1:nrow(df) # Select variables, convert to long format, and plot variables @pipe df |> # Bs - transform(_, [:Bbd, :Bbs, :Bs, :V] .=> (x -> x./_.K) .=> [:Bbd, :Bbs, :Bs, :V]) |> - select(_, [:Bbs, :Bbd, :period]) |> - # select(_, :V, :period) |> - stack(_, Not(:period), variable_name=:variable) |> - # subset(_, :period => ByRow(<(100))) |> - plot( - _, - x=:period, - y=:value, - color=:variable, - Geom.line - ) + transform(_, [:Bbd, :Bbs, :Bs, :V] .=> (x -> x ./ _.K) .=> [:Bbd, :Bbs, :Bs, :V]) |> + select(_, [:Bbs, :Bbd, :period]) |> + # select(_, :V, :period) |> + stack(_, Not(:period), variable_name=:variable) |> + # subset(_, :period => ByRow(<(100))) |> + plot( + _, + x=:period, + y=:value, + color=:variable, + Geom.line + ) # @report_opt Consistent.f!(a, lags, lags, exos, param_values) # growth.equations[findfirst(==(:G), growth.endogenous_variables)] \ No newline at end of file From 6b8f2b5072995b296491cefc7240423de71ea027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20N=C3=A4gele?= Date: Thu, 23 Nov 2023 16:13:17 +0100 Subject: [PATCH 35/35] Fix format --- src/Solve.jl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Solve.jl b/src/Solve.jl index 87c0a68..08e768c 100644 --- a/src/Solve.jl +++ b/src/Solve.jl @@ -7,11 +7,11 @@ function solve(model, lags, exos, params; initial=fill(1.0, length(model.endogen nlsolve( (F, x) -> model.f!(F, x, lags, exos, params), initial, - autodiff = :forward, - method = method, + autodiff=:forward, + method=method, # iterations = 500, - ftol = 1e-40, - xtol = 1e-40 + ftol=1e-40, + xtol=1e-40 ).zero end @@ -20,7 +20,8 @@ function solve_nonlinear(model, lags, exos, params; initial=fill(1.0, length(mod (F, x, p) -> model.f!(F, x, lags, exos, p), initial, params, - abstol = 1e-40, reltol = 1e-40 + abstol=1e-40, + reltol=1e-40 ) sol = NonlinearSolve.solve(prob, method) return sol