ai4energy / Ai4EComponentLib.jl

A component library based on ModelingToolkit
MIT License
26 stars 14 forks source link

Extending the Ai4Energy ecosystem to Economics #55

Open finmod opened 1 year ago

finmod commented 1 year ago

@jake484 As suggested, here is the MTK code of ModelA1 crresponding to the diagram in the text I sent you.

using DifferentialEquations
using OrdinaryDiffEq
using ModelingToolkit
using Plots; gr()

ps = @parameters begin
       α  = 0.5, [tunable = true, bounds = (0, Inf)]
       β  = 1.0 
       dp̂ = 0.1
       γ  = 0.25
       k̂  = 0.4
       K₀ = 1.5 
       l₀ = 1.0 
       l̂  = 1.0 
       Mᶠ₀= 1.00 
       Mʰ₀= 1.0 
       M̂ʰ = 1.0 
       p₀ = 1.0 
       S₀ = 1.0 
       Ŝ  = 1.0 
       w₀ = 1.0 
       μ₁ = 1.0 
       μ₂ = 1.0 
       μ₃ = 1.0 
       μ₄ = 1.0 
       μ₅ = 1.0 
       μ₆ = 1.0 
       μ₇ = 0.05 
       μ₈ = 1.0 
       μ₉ = 0.0
end

@variables t 
sts = @variables begin 
       c(t) = β*K₀^(1 - α) * l₀^(α)
       K(t) = 2.0
       l(t) = 0.8 
       Mᶠ(t) = 1.2 
       Mʰ(t) = 1.2
       p(t) = 0.8 
       S(t) = 0.8 
       w(t) = 1.2 
       k(t) = 0.1 
       mᶠ(t) = 0.1 
       mʰ(t) = 0.0
       s(t) = 0.0
       𝛌₁(t) = -0.1 
       𝛌₂(t) = -0.3
       𝛌₃(t) = 0.4
end
# Algebraic variables
@variables k̂(t) dep(t) y(t) Uᶠ(t) Uʰ(t) 

Dₜ = Differential(t)

# Algebraically defined variables 
# Warning the listing of equation is not indifferent . 

k̂   = (1 - α)*y/K                        # Marginal product of capital
dep = dp̂*K                               # Depreciation function
y   = β*K^(1 - α)*l^(α)                  # Production function

Uᶠ  = p*y - w*l - (k̂ - k)^2 - (Ŝ - S)^2  # Profit function of firms
Uʰ  = c^(γ) - (l̂ - l)^2 - (M̂ʰ - Mʰ)^2    # Utility function of households

# Constraints

Zₕ  = w*l - p*c - mʰ                     # Budget constraint household
Zₚ  = p*c - w*l - mᶠ                     # Budget constraint firms            
Zᵣ  = y - c - k - s - dep                # Goods market identity

# Constrained dynamics D'Alembert principle

DLP = [Uᶠ, Uʰ, 𝛌₁*Zₕ, 𝛌₂*Zₚ, 𝛌₃*Zᵣ]

Jᵀ = transpose(Symbolics.jacobian(DLP, sts))

eqs = [Dₜ(c)  ~              μ₁*Jᵀ[1,2] + Jᵀ[1,3] + Jᵀ[1,4] + Jᵀ[1,5],
       Dₜ(K)  ~ μ₂*Jᵀ[2,1]              + Jᵀ[2,3] + Jᵀ[2,4] + Jᵀ[9,5],
       Dₜ(l)  ~ μ₃*Jᵀ[3,1] + μ₄*Jᵀ[3,2] + Jᵀ[3,3] + Jᵀ[3,4] + Jᵀ[3,5],
       Dₜ(Mᶠ) ~ μ₅*Jᵀ[4,1]              + Jᵀ[4,3] + Jᵀ[10,4]+ Jᵀ[4,5],
       Dₜ(Mʰ) ~              μ₆*Jᵀ[5,2] + Jᵀ[11,3]+ Jᵀ[5,4] + Jᵀ[5,5],
       Dₜ(p)  ~ μ₇*Jᵀ[6,1]              + Jᵀ[6,3] + Jᵀ[6,4] + Jᵀ[6,5],
       Dₜ(S)  ~ μ₈*Jᵀ[7,1]              + Jᵀ[7,3] + Jᵀ[7,4] + Jᵀ[12,5],
       Dₜ(w)  ~ μ₉*Jᵀ[8,1]              + Jᵀ[8,3] + Jᵀ[8,4] + Jᵀ[8,5],
       Dₜ(K)  ~ k,                                
       Dₜ(Mᶠ) ~ mᶠ,
       Dₜ(Mʰ) ~ mʰ,
       Dₜ(S)  ~ s,
       0.     ~ Zₕ,
       0.     ~ Zₚ,
       0.     ~ Zᵣ]

@named A1_dae = ModelingToolkit.ODESystem(eqs, t, sts, ps)
#A1_dae = structural_simplify(A1_dae)
Daiyunxi commented 1 year ago

componentization.pdf In this document I put forward two methods of componentization, next step is to translate to programming. In fact I don't really understand economics and may have some misunderstanding about the model. If you find something incorrect or just strange, tell me and I will find a way to improve it.

finmod commented 1 year ago

Please find my reply to your suggestions.

From: Daiyunxi @.> Sent: Thursday, March 30, 2023 4:10 AM To: ai4energy/Ai4EComponentLib.jl @.> Cc: finmod @.>; Author @.> Subject: Re: [ai4energy/Ai4EComponentLib.jl] Extending the Ai4Energy ecosystem to Economics (Issue #55)

componentization.pdf https://github.com/ai4energy/Ai4EComponentLib.jl/files/11106994/componentization.pdf In this document I put forward two methods of componentization, next step is to translate to programming. In fact I don't really understand economics and may have some misunderstanding about the model. If you find something incorrect or just strange, tell me and I will find a way to improve it.

— Reply to this email directly, view it on GitHub https://github.com/ai4energy/Ai4EComponentLib.jl/issues/55#issuecomment-1489582776 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ADA7EIV5AB2RR2IM267DAQTW6TTPBANCNFSM6AAAAAAV2O75P4 . You are receiving this because you authored the thread. https://github.com/notifications/beacon/ADA7EIVKUXLXYIS6DYI23R3W6TTPBA5CNFSM6AAAAAAV2O75P6WGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTSYZE5LQ.gif Message ID: @. @.> >

jake484 commented 1 year ago

New version released, hope it's helpful.

finmod commented 1 year ago

Fantastic. Let me play with this with a view to provide better docs for building the function of an agent.

Yes, it is the right approach to define a function for firms, for household and later for banks, gouvernment, central bank and rest of the world.

Within a function, one has to make a distinction between stocks (balance sheet account), flows (transactions account) and the accounting constraints that apply to each: assets = liabilities and supply = demand.

@ChrisRackauckas can we get some guidance on this appplication of Acausal modeling to General Constrained Dynamic models in economics?

ChrisRackauckas commented 1 year ago

DSGEs are generally causal IIRC? So you'd just make it a component.