# Simple macroeconomic model in DynamicalSystems.jl

**URL:** <https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221>\
**Category:** Finance and Economics\
**Tags:** question\
**Created:** [June 4, 2022, 1:24am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221 "2022-06-04T01:24:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 4, 2022, 1:24am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/1 "2022-06-04T01:24:30Z")

</div>

Hi. I am very new to Julia.  
I have a very simple macroeconomic model, which I managed to simulate and plot with numpy for Julia.  
I tried to simulate it in DynamicalSystems.jl, but not all the equations are of the type: x[t] = f(x[t-1]). This is the model:  
“”"  
(1) y[t] = m \* (a + G[t])  
(2) c[t] = a + alpha \* y[t-1]  
(3) I[t] = beta \* (y[t] - y[t-1])  
“”"

Parameters: m = 1/(1-alpha), a = 50, alpha = 0.75, beta = 4, G = 100.  
Steady state is y = 600.

My question: Is there a way to run such a model in DynamicalSystems? My problem is with equations 1 & 3.

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [June 4, 2022, 1:36am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/2 "2022-06-04T01:36:08Z")

</div>

I think you will need to give the boundary/initial values to make the problem well formed.

If this is a macroeconomic/control problem then it probably has a transversality condition, which is a very particular type of boundary condition that probably isn’t handled in that package. But I know that @Albert_Zevelev was trying to collect packages that could handle those

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 4, 2022, 1:54am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/3 "2022-06-04T01:54:32Z")

</div>

Thanks very much for the reply.  
It is not a control problem or a rbc kind of model; it is an old fashioned keynesian model, with no microfoundations.  
You are right I am sorry that I forgot the initial conditions: zeros for y, c, I, while G=100.  
But my problem is that DynamicalSystems.jl requires the equations to have the form y[t+1] = f(x[t]), but equations 1 and 3 have variables indexed [t+1] on both sides of the “equal” sign.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [June 4, 2022, 3:16am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/4 "2022-06-04T03:16:00Z")

</div>

Cool, if it is an initial value problem it is a lot easier.

I am still having trouble seeing this as a dynamical system though. Unless G[t] is an exogenously given function? But if so then I think that you can substitute to get almost everything in closed form to simulate on one dimension and this package is overkill?

If you misspecified this a little but still can’t substitute to get equations withiut the “differential” (ie, if some equations have terms all at the same time) then my suggestion is to convert this to continuous time. With that, you can write things as a differential algebraic equation with a mass matrix and solve it that way with differentialequations.jl

But as I said, I think either something is missing in your setup or else you can solve it with simpler methods

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [June 4, 2022, 3:19am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/5 "2022-06-04T03:19:07Z")

</div>

I can look at this tomorrow.

In this example gov spending, G[t] is an exogenously given sequence, which happens to be equal to 100 for all t.

I think it’s very easy to solve w/o any package.  
First simulate y[t] given y0  
Then: together c[t], I[t] given c0, I0

If you wanna use DynamicalSystems.jl for this, ask @Datseris

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [June 4, 2022, 9:14am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/6 "2022-06-04T09:14:43Z")

</div>

I don’t understand the problem. Make a `DiscreteDynamicalSystem` with function

```julia
function f(u, p, t)
y, c, I = u
α, β, ... = p
yn = m*(a + G[t])
cn = a + α * y
In = β * (yn - y)
return SVector(yn, cn, In)
end

```

I don’t know what G is. You say its 100, but then how can you access it with an index…?

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 4, 2022, 3:58pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/8 "2022-06-04T15:58:20Z")

</div>

Thank you so much for the input.  
In fact G[t] is an exogenous variable: it is zero at the beginning, and becomes 100 in period 15.  
y[t] is national income, c[t] is consumption, I[t] is investment, G[t] is government spending, an exogenous variable.  
I apologize for the omission of the details, I guess I was just trying to avoid too much detail for the people to read.  
Thanks, and again I am sorry for the omissions.

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 4, 2022, 4:20pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/9 "2022-06-04T16:20:52Z")

</div>

> [@Datseris](#):
>
> ```julia
> function f(u, p, t)
> y, c, I = u
> α, β, ... = p
> yn = m*(a + G[t])
> cn = a + α * y
> In = β * (yn - y)
> return SVector(yn, cn, In)
> end
> 
> ```

EDIT:  
The first equation y[t] = m\*(a+G[t]) is actually the steady state solution (I thought I should put that one). But the original model should have been:  
y[t] = c[t] + I[t] + G[t]  
c[t] = a + alpha\*(y[t-1])  
I[t] = beta\*(y[t] - y[t-1])

Initial value for y, c, I is zero.  
G[t] is zero up until period 14. In period 15 it becomes 100.  
Parameters: a = 50, alpha = 0.75, beta = 4.

I guess this should have been my first message. I solved this in Python, and in Julia with numpy, but thought perhaps there was a more direct way of doing it with DynamicalSystems (but the indexing in equations 1 and 3 got me confused).

As [jlperla] indicated previously this could easily be transformed into a continous system that can be analyzed with DifferentialEquations.jl, but I just wanted to get a better understanding of discrete systems in Julia.

I really appreciate your attention and help.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [June 4, 2022, 5:42pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/10 "2022-06-04T17:42:18Z")

</div>

@Datseris can dynamical systems handle “difference algebraic equations” or whatever they are called? I think that is what he has written down? Maybe this exact case can be transformed by substitution into a standard first order difference equation, I am not sure if the “algebraic” things are standard algorithms or even should be.

@josec when I think of the dynamical systems package, I think of thinks like chaos, cycles, bifurcation analysis, etc. If those are the things you expect to emerge out of your setup then cool. Not an expert here, but I think that because you are specifying a linear problem, it is impossible to have chaos/cycles/etc. If those can’t occur, this package may be a sledgehammer in this specific case since you can manually iterate forward.

---

<div class="post-metadata">

**Author:** ![RJDennis](https://avatars.discourse-cdn.com/v4/letter/r/90db22/32.png) [@RJDennis](https://discourse.julialang.org/u/RJDennis)\
**Post date:** [June 4, 2022, 5:51pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/11 "2022-06-04T17:51:13Z")

</div>

I don’t understand why this system cannot be simulated using a for loop.

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [June 4, 2022, 6:39pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/12 "2022-06-04T18:39:26Z")

</div>

This one clearly can.  
I think he wants to know how solve more general systems of difference equations in Julia, using this simple system as an example

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [June 4, 2022, 7:10pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/13 "2022-06-04T19:10:16Z")

</div>

I think the problem here is that it has an algebraic parts of it. Without those, there wouldn’t be a problem. In continuous time it would be a DAE - of which the SciML ecosystem has plenty of algorithms.

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 4, 2022, 8:51pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/14 "2022-06-04T20:51:32Z")

</div>

Thanks for your help. I tried to reorganize the code you sent in the following way. I would like to plot y,c, and print their values.

function f(u, p, t)  
u = y, c, I, G  
p = 50, 0.75, 4 # vector with the parameters.  
u0 = 0, 0, 0, 0 # the initial conditions.

# for loop for the exogenous government spending G (it is zero initially and becomes 100 in period 15).

# Total number of periods is 200.

for n = 1:14  
G[n] = 0  
end  
for n = 15:26  
G[n] = 100  
end  
yn = cn + In + Gn  
cn = 50 + 0.75 \* y  
In = 4 \* (yn - y)  
return SVector(yn, cn, In, Gn)  
end

Thanks again for your advice.

Jose A. Cordero

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 4, 2022, 8:54pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/15 "2022-06-04T20:54:15Z")

</div>

Yes, exactly, that’s what I would like to do.  
On this path, I would eventually like to solve a system like the one in Samuelson multiplier-accelerator model.

Thank you very much.

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 4, 2022, 9:00pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/16 "2022-06-04T21:00:45Z")

</div>

@jlperla you are right. This is a linear system and definitely cannot generate cycles or chaotic behavior. I am probably trying to to this with the wrong package. I didn’t think about that.

Thanks!!!

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 4, 2022, 9:04pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/17 "2022-06-04T21:04:13Z")

</div>

@Datseris I apologize for the careless use of the pound sign within markdown (didn`t realize this was in markdown format).

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [June 4, 2022, 10:51pm UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/18 "2022-06-04T22:51:06Z")

</div>

System:  
Y\_t = C\_{t} + I\_t + G\_t  
C\_t = a + \alpha Y\_{t-1}  
I\_t = \beta (Y\_{t} - Y\_{t-1})  
G\_t = 0\times 1\{t\leq 14\} + 100\times 1\{t\geq 15\}  
Parameters: a=50, \alpha =.75, \beta =4  
Initial Condition: Y\_{0}

System of 4 equations in 4 variables Y\_{t}, C\_{t}, I\_{t}, G\_{t}  
A solution gives each variable as a function of `t`.  
G\_{t} already given (exogenously).

**Solution 1 (Closed form)**  
Plug the 2nd/3rd equation into the 1st:  
Y\_t = (a + \alpha Y\_{t-1}) + (\beta Y\_{t} - \beta Y\_{t-1}) + G\_t   
\Leftrightarrow (1-\beta)Y\_t = a + (\alpha -\beta) Y\_{t-1} + G\_t   
\Leftrightarrow Y\_t = \frac{a}{(1-\beta)} + \frac{(\alpha -\beta)}{(1-\beta)} Y\_{t-1} + \frac{1}{(1-\beta)}G\_t , Y\_0 given is a 1st order difference equation, solve for Y\_{t}.  
Next, plug Y\_{t} into the 2nd/3rd equations to get C\_{t}, I\_{t}  
Hopefully `Symbolics.jl` will one day solve systems of difference equations @ChrisRackauckas.  
For now Mathematica:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/8/d814e5ea7972b8ba50f7ae9e795fb45a313ac3d9.png)

**Solution 2 (numerical in Julia)**

```julia
using Plots;
#
a=50.0; α=.75; β=4.0; Y0=0.0;
G(t)= (t <= 14) ? 0.0 : 100.0 
Y_rec(Ym, t; α=α, β=β, a=a) = (a/(1-β)) + ((α-β)/(1-β))*Ym + (1/(1-β))*G(t)
I_rec(Y, Ym; β=β) = β*(Y-Ym)
C_rec(Ym; a=a, α=α) = a + α*Ym
# Simulate
Δt = 1.0; T = 250; time = 0.0:Δt:T
G_sim, Y_sim, I_sim, C_sim = [zeros(length(time),1) for i in 1:4]
Y_sim[1] = Y0 
#
for i in 1:(length(time)-1)
    G_sim[i+1] = G(i)
    Y_sim[i+1] = Y_rec(Y_sim[i], i)
    I_sim[i+1] = I_rec(Y_sim[i+1], Y_sim[i])
    C_sim[i+1] = C_rec(Y_sim[i])
end
#
ix = 2:21 #(100) # time=0, to time=23
p1 = plot(size=(400,400),legend=:bottomleft)
plot!(time[ix], G_sim[ix], lab = "G")
plot!(time[ix], Y_sim[ix], lab = "Y")
plot!(time[ix], I_sim[ix], lab = "I")
plot!(time[ix], C_sim[ix], lab = "C")

```

![image](https://global.discourse-cdn.com/julialang/original/3X/4/9/491a2461db0ed9d9810f3c2baf5dd961dca993f7.png)

If we change one parameter \beta =0.4 instead of \beta=4, things look more as expected:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/1/6155ec433f304b8f83ab3537b909e1438f29d13c.png)  
As we lengthen the time horizon, variables approach Steady State:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/c/8cb9d420d20d47f9999cee2de8cdc566d424cac6.png)

@josec Based on the simulation, I believe there was a typo somewhere in your description of the problem (or parameter value, or initial value). Can you please post the original source?

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 5, 2022, 12:38am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/19 "2022-06-05T00:38:33Z")

</div>

@Albert_Zevelev: I appreciate very much your help; extremely useful.  
Just three comments:

- This example, I made it up, so I am sorry that I can`t provide the source because there isn`t one.
- The steady state solution (Y\_rec = Ym, in your notation) is:  
Y = (1/1-alpha) \* (a + G). This means that the value of beta should not affect the steady state solution. I thus wonder why, with the code, changing the value of beta changes the results.
- In the simulation on the first graph, Y, C, and I seem to move down to zero, while the closed solution indicates that, in steady state, Y=600, C=500 and I should be zero from the investment equation.

Thanks again for your help, and I apologize for asking so many questions.

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [June 5, 2022, 1:12am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/20 "2022-06-05T01:12:01Z")

</div>

@Albert_Zevelev: Hi again. I made some slight changes and got the correct steady state results, even with beta =4. This is what I did:

1. Eliminated the equation with Y\_rec(Ym, t; α=α, β=β, a=a) = …
2. After the equations for I\_rec and C\_rec I included this equation: Y\_rec(t)= C\_rec + I\_rec + G(t)
3. The new equation for Y\_rec is an equilibrium condition. The original equation for Y\_rec provided the “short-run” equilibrium for Y.
4. After the above change, the plot provided the correct steady state values for Y, C, I, G; approaching the steady state with more periods.

Thanks again for your help!!!

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [June 5, 2022, 1:17am UTC](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221/21 "2022-06-05T01:17:05Z")

</div>

Before I forget.

We start w/ a system of 3 DAEs in 3 variables Y\_{t}, C\_{t}, I\_{t}. G\_{t}=100\times 1\{t\geq 15\} is exogenous.  
Y\_t = C\_{t} + I\_t + G\_t  
C\_t = a + \alpha Y\_{t-1}  
I\_t = \beta (Y\_{t} - Y\_{t-1})  
Parameters: (a, \alpha, \beta)  
Initial Condition: Y\_{0}

We reduce this system of 3 DAEs into 3 1^{st} order non-autonomous difference equations:  
Y\_t =F\_{1}(Y\_{t-1}, t)= \frac{a}{(1-\beta)} + \frac{(\alpha -\beta)}{(1-\beta)} Y\_{t-1} + \frac{1}{(1-\beta)}G\_t , Y\_0 given  
C\_t =F\_{2}(Y\_{t-1}, t) = a + \alpha Y\_{t-1}  
I\_t =F\_{3}(Y\_{t-1}, t)= \beta (F\_{1}(Y\_{t-1}, t) - Y\_{t-1})  
**Note:** the reduced system only depends on Y\_{t-1} so we only need an initial condition for Y\_t.

SS:  
Y\_{ss} = \frac{a}{(1-\beta)} + \frac{(\alpha -\beta)}{(1-\beta)} Y\_{ss} + \frac{100}{(1-\beta)} \Leftrightarrow \frac{(1-\alpha)}{(1-\beta)} Y\_{ss} = \frac{a}{(1-\beta)} + \frac{100}{(1-\beta)} \Leftrightarrow Y\_{ss} = \frac{a}{(1-\alpha)} + \frac{100}{(1-\alpha)}  
C\_{ss} = a + \alpha Y\_{ss} = a + \frac{a \alpha}{(1-\alpha)} + \frac{100 \alpha}{(1-\alpha)} =\frac{a}{(1-\alpha)} + \frac{100 \alpha}{(1-\alpha)}  
I\_{ss} = \beta (Y\_{ss} - Y\_{ss}) = 0  
G\_{ss} = 100  
Note: the system acts weird for \beta \> \alpha.

[Next page](https://discourse.julialang.org/t/simple-macroeconomic-model-in-dynamicalsystems-jl/82221.md?page=2)
