```python
import sympy as sm
import numpy as np
from scipy import optimize
import ModelProject as fct
import matplotlib.pyplot as plt
%load_ext autoreload
%autoreload 2
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from IPython.display import display
```

# 1. Introduction

We make use of an OLG model where people live in two periods $(1t)$ and $(2t+1)$. We have homogenous firms that maximise their profits in the following way:
$$
max_{K_{t},\space L_{t}}\space AK_{t}^{\alpha}L_{t}^{1-\alpha}-r_{t}K_{t}-w_{t}L_{t}
$$
where $\alpha \in (0,1)$, and the total factory productivty $A$ is strictly positive. Also, $r_{t}$ denotes the rental rate of capital while $w_{t}$ refers to wage rate.

To continue, we define the following utility of young people born in period $1t$:
$$
U_{t}=ln(c_{1t})+\frac{1}{1+\rho}ln(c_{2t+1})
$$
where $\rho>-1$ and $c_{1t}$ refers to the consumption of the young generation while $c_{2t+1}$ refers to the consumption of the old generation.

We also assume that a government provides a 'pay-as-you-go' scenario where the young generation contributes an amount $\tau$ of their wage where $\tau \in (0,1)$ that is paid in the same period to the old. As such, the budget constraints are:

$$
c_{1t}= (1-\tau)w_{t}-s_{t}
$$

$$
c_{2t+1}=R_{t+1}s_{t}+\tau w_{t+1}
$$
where $R_{t+1}=1+r_{t+1}-\delta$.

Finally, we assume that population growth is constant and that the old generation does not work. As such, the old generation depends financially on income from their savings as well as on the contributions given by the young generation.

# 2. Analytical analysis


```python
A=sm.symbols('A')
K=sm.symbols('K_t')
L=sm.symbols('L_t')
alpha=sm.symbols('alpha')
rho=sm.symbols('rho')
c1 = sm.symbols('c_1t')
c2=sm.symbols('c_2t+1')
u=sm.symbols('u_t')
U=sm.symbols('U_t')
uc1=sm.ln(c1)
uc2=sm.ln(c2)
s=sm.symbols('s_t')
tau=sm.symbols('tau')
r=sm.symbols('r_t')
rt=sm.symbols('R_t+1')
w=sm.symbols('w_t')
wt=sm.symbols('w_t+1')
k = sm.symbols('k_t')
kt=sm.symbols('k_t+1')
lamb=sm.symbols('lambda_t')
tau=sm.symbols('tau')
kstar=sm.symbols('k_{ss}^{*}')
sstar=sm.symbols('s_{t}^{*}')
```

**2.1. Firm maximisation problem**

We begin by maximising the firm profit problem with respect to $K_t$ and $L_t$ in order to find the rental rate of capital and the wage rate: $$
max_{K_{t}, \space {L_t}} \space AK_{t}^{\alpha}L_{t}^{1-\alpha}-r_{t}K_{t}-w_{t}L_{t}
$$



```python
#Define the firm maximisation problem
firmmax=A*K**(alpha)*L**(1-alpha)-(r*K)-(w*L)
```

FOC w.r.t $K_{t}$ and $L_{t}$ respectively:


```python
#We compute the first order conditions of our firm maximisation problem w.r.t to K_{t} and L_{t}
FOCK = sm.Eq(0,sm.diff(firmmax, K))
FOCL = sm.Eq(0,sm.diff(firmmax, L))
display(FOCK)
display(FOCL)
```


$\displaystyle 0 = \frac{A K_{t}^{\alpha} L_{t}^{1 - \alpha} \alpha}{K_{t}} - r_{t}$



$\displaystyle 0 = \frac{A K_{t}^{\alpha} L_{t}^{1 - \alpha} \left(1 - \alpha\right)}{L_{t}} - w_{t}$


Using the fact that $k_{t}=\frac{K_t}{L_t}$, our FOCs above become:


```python
rentalrateK =sm.Eq(r,A*alpha* k**(alpha-1))
wagerate= sm.Eq(w,A*k**alpha * (1-alpha))
display(rentalrateK)
display(wagerate)
```


$\displaystyle r_{t} = A \alpha k_{t}^{\alpha - 1}$



$\displaystyle w_{t} = A k_{t}^{\alpha} \left(1 - \alpha\right)$


**2.2. Household maximisation problem**

We maxmimise the household problem of intertemporal allocation of resources with respect to the budget constraints mentioned above:
$$
max_{c_{1t}, \space c_{2t+1}}\space U_{t}=ln(c_{1t})+\frac{1}{1+\rho}ln(c_{2t+1})+\lambda_{t}((1-\tau)w_{t}-c_{1t}-(\frac{c_{2t+1}-\tau w_{t+1}}{R_{t+1}})
$$


```python
#We define the household maximisation problem
maxU =uc1 + (1/(1+rho))*uc2 + lamb*((1-tau)*w-c1 -((c2-tau*wt)/rt))
display(sm.Eq(U,maxU))
```


$\displaystyle U_{t} = \lambda_{t} \left(- c_{1t} + w_{t} \left(1 - \tau\right) - \frac{c_{2t+1} - \tau w_{t+1}}{R_{t+1}}\right) + \log{\left(c_{1t} \right)} + \frac{\log{\left(c_{2t+1} \right)}}{\rho + 1}$


FOC of $U_{t}$ w.r.t $c_{1t}$ and $c_{2t+1}$ are respectively:


```python
FOCc1=sm.Eq(0,sm.diff(maxU,c1))
FOCc2=sm.Eq(0,sm.diff(maxU,c2))
display(FOCc1)
display(FOCc2)
```


$\displaystyle 0 = - \lambda_{t} + \frac{1}{c_{1t}}$



$\displaystyle 0 = \frac{1}{c_{2t+1} \left(\rho + 1\right)} - \frac{\lambda_{t}}{R_{t+1}}$


Isolating $\lambda_{t}$ in the first FOC and plugging it into the second FOC yields the Euler equation:


```python
#We isolate lambda_{t} in the first FOC:
lambisol=sm.solve(FOCc1, lamb)
#Then we plug in the isolated lambda_{t} into the second FOC:
Eulereq=FOCc2.subs(lamb, lambisol[0])
display(Eulereq)
```


$\displaystyle 0 = \frac{1}{c_{2t+1} \left(\rho + 1\right)} - \frac{1}{R_{t+1} c_{1t}}$


Then we isolate for $c_1$ so that our Euler equation is:


```python
#We isolate for c1:
Eulereqiso=sm.solve(Eulereq,c1)
display(sm.Eq(c1,Eulereqiso[0]))
```


$\displaystyle c_{1t} = \frac{c_{2t+1} \left(\rho + 1\right)}{R_{t+1}}$


The Euler equation gives us households' optimal consumption path. Therefore, the marginal utility of consumption today is equal to the future value of discounted marginal
utility of consumption tomorrow, discounted by $\rho$, when the above condition holds.

**2.3. Optimal savings $s_{t}^{*}$**

To continue, we plug in the budget constraints in our maximisation problem to find the optimal individual savings behaviour $s_t$:
$$
max_{s_t}\space U_{t}=ln((1-\tau)w_{t}-s_{t})+\frac{1}{1+\rho}ln((R_{t+1})s_{t}+\tau (w_{t+1}))+\lambda_{t}((1-\tau)w_{t}-c_{1t}-(\frac{c_{2t+1}-\tau w_{t+1}}{R_{t+1}})
$$



```python
#We define our budget contraints
bc1=(1-tau)*w-s
bc2=(rt*s)+tau*(wt)
#We define the logs of our budget contraints
lnbc1=sm.ln(bc1)
lnbc2=sm.ln(bc2)
```


```python
#We plug in our budget constraints into our household max problem:
maxUsavings=lnbc1+(1/(1+rho))*lnbc2+lamb*((1-tau)*w-c1 -((c2-tau*wt)/rt))
display(sm.Eq(U,maxUsavings))
```


$\displaystyle U_{t} = \lambda_{t} \left(- c_{1t} + w_{t} \left(1 - \tau\right) - \frac{c_{2t+1} - \tau w_{t+1}}{R_{t+1}}\right) + \log{\left(- s_{t} + w_{t} \left(1 - \tau\right) \right)} + \frac{\log{\left(R_{t+1} s_{t} + \tau w_{t+1} \right)}}{\rho + 1}$


We then find the FOC of our household maximisation problem w.r.t $s_t$:


```python
FOCsavings=sm.Eq(0,sm.diff(maxUsavings,s))
display(FOCsavings)
```


$\displaystyle 0 = \frac{R_{t+1}}{\left(\rho + 1\right) \left(R_{t+1} s_{t} + \tau w_{t+1}\right)} - \frac{1}{- s_{t} + w_{t} \left(1 - \tau\right)}$


We isolate for $s_t$ so that our optimal savings is:


```python
#isolate for savings:
savings=(sm.solve(FOCsavings,s))
optsavings=sm.Eq(sstar,savings[0])
display(optsavings)
```


$\displaystyle s_{t}^{*} = \frac{- R_{t+1} \tau w_{t} + R_{t+1} w_{t} - \rho \tau w_{t+1} - \tau w_{t+1}}{R_{t+1} \left(\rho + 2\right)}$



```python
#Defining optimal savings into a python function:
optsavingsfct=sm.lambdify((rt,tau,w,wt,rho),savings)
```


```python
optsavingsfct(0.8,0.1,0.6,0.9,0.1)
print('Analytical optimal savings =', optsavingsfct(0.8,0.1,0.6,0.9,0.1))
```

    Analytical optimal savings = [0.19821428571428568]


We can see that the higher the gross return on savings $R_{t+1}$ is, the higher the optimal savings $s_{t}^{*}$ is. Also, we notice that the higher the contribution $\tau$ from young to old is, the lower the optimal savings $s_{t}^{*}$ are. Finally, we notice that the wage rate in period 1 ($w_t)$ is positively correlated to $s_{t}^{*}$ while the wage rate in period 2 $(w_{t+1})$ is negatively correlated to $s_{t}^{*}$. As a result, the optimal savings $s_{t}^{*}$ decrease the more $w_{t+1}$ increases.

**2.4. Capital accumulation**

In order to find the the capital accumulation equation, we use the optimal savings equation $s_t^{*}$ derived above and replace $s_t^{*}$ with $k_{t+1}$, and we plug in the values for $w_t$, $w_{t+1}$, $r_t$ and $r_{t+1}$. Also, we now assume that capital full depreciates so that $\delta=1$. Therefore, $R_{t+1}=r_{t+1}$ since $R_{t+1}=1+r_{t+1}-1$.

Note that our sympy commands provide a different capital accumulation and steady-state. As such, we perform parameter substitution ourselves in order to compute capital accumulation and the steady state. Our capital accumulation equation $k_{t+1}=s_{t}^{*}$ looks as followed: 


```python
#We define $w_{t+1}$ and $r_{t+1}$:
rentalrateK1=rentalrateK.subs(k,kt)
wagerate1=wagerate.subs(k,kt)
display(rentalrateK1)
display(wagerate1)
```


$\displaystyle r_{t} = A \alpha k_{t+1}^{\alpha - 1}$



$\displaystyle w_{t} = A k_{t+1}^{\alpha} \left(1 - \alpha\right)$



```python
#We set $s_t^{*}=k_{t+1}$ and plug in the values for $w_t$, $w_{t+1}$, $r_t$ and $r_{t+1}$:
capitalacc=(((1-alpha)*(1-tau)*alpha)/((2+rho)*alpha+(1+rho)*(1-alpha)*tau))*A*(k**alpha)
capitalacc=sm.Eq(kt,capitalacc)
display(capitalacc)
```


$\displaystyle k_{t+1} = \frac{A \alpha k_{t}^{\alpha} \left(1 - \alpha\right) \left(1 - \tau\right)}{\alpha \left(\rho + 2\right) + \tau \left(1 - \alpha\right) \left(\rho + 1\right)}$


**2.5. Steady state**

To find our steady state, we assume that $k_{ss}^{*}=k_{t+1}=k_{t}$. Thus, we plug in $k_{ss}^{*}=k_{t+1}=k_{t}$ in our equation avove so that:


```python
#We substitute $k_{ss}^{*}=k_{t+1}=k_{t}$ into our capital accumulation equation:
sstate=(((1-alpha)*(1-tau)*alpha*A)/((2+rho)*alpha+(1+rho)*(1-alpha)*tau))**(1/(1-alpha))
SS=sm.Eq(kstar,sstate)
display(SS)
```


$\displaystyle k_{ss}^{*} = \left(\frac{A \alpha \left(1 - \alpha\right) \left(1 - \tau\right)}{\alpha \left(\rho + 2\right) + \tau \left(1 - \alpha\right) \left(\rho + 1\right)}\right)^{\frac{1}{1 - \alpha}}$



```python
#Defining steady state into a python function:
optSSfct=sm.lambdify((A,alpha,tau,rho),sstate)
```


```python
optSSfct(100,0.1,0.003,0.1)
print('Analytical steady state =', optSSfct(100,0.1,0.003,0.1))
```

    Analytical steady state = 63.84582238491508


We can see that $\alpha$, $\tau$ and $\rho$ are all negatively correlated to the steady state $k_{ss}^{*}$ as an increase in either parameter decreases the steady state. In contrast, the total factor productivity $A$ is positively correlated, meaning that an increase in $A$ increases the steady state.

# 3. Numerical analysis

We will now use functions that have been created in the `ModelProject.py` file in order to solde this model numericaly.

We first set the model parameters :


```python
alpha=0.7
rho=0.1
tau=0.05
A=20
w=10
s=2
r=0.3
L=50
delta=1
K=100
```


```python
alpha1=0.33
rho1=0.05
tau1=0.2
A1=2
w1=10
s1=2
r1=0.3
L1=50
delta1=1
K1=100
```

**3.1. Firm maximization problem**

We have the following results:


```python
#profit maximisation:
MaxProfit = fct.profit(K,L,w,r,alpha,A)
print('Firm profits =', MaxProfit)
#Optimal wage:
optw=fct.optiWage(A,K,alpha)
print('Optimal wage =',optw)
#Optimal rental rate of capital:
optr=fct.optirr(A,K,alpha)
print('Optimal rental rate of capital =',optr)
#Optimal level of capital:
optK=fct.MaxProf(K,L,w,r,alpha,A,Kopt=True)
print('Optimal level of capital =',optK)
#Optimal level of labour:
optL=fct.MaxProf(K,L,w,r,alpha,A,Kopt=False)
print('Optimal level of labour =',optL)
```

    Firm profits = 1094.5047927124708
    Optimal wage = 150.7131858905748
    Optimal rental rate of capital = 3.5166410041134113
    Optimal level of capital = 9999.99970046681
    Optimal level of labour = 48.202904223331565


**3.2. Household maximization problem**

We have the following results:


```python
#Young generation consumption:
c1=fct.consoY(s,tau,w)
print('Young generation consumption =',c1)
#Old generation consumption:we first calculate the gross return on savings and then old generation consumption:
R=fct.GrossRet(r,delta)
c2=fct.consoO(R,s,tau,w)
c2
print('Old generation consumption =',R)
#Euler equation:
c1E=fct.eulerC1(c2,rho,R)
print('Euler equation =',c1E)
#Utility of young generation:
u=fct.utilityY(c1,c2,rho)
print('Utility =',u)
#savings:
s=fct.savings(w,rho)
opts=fct.optiS(tau,rho,w,R)
print('Savings = ',s)
print('Optimal savings = ',opts)
```

    Young generation consumption = 7.5
    Old generation consumption = 0.30000000000000004
    Euler equation = 4.033333333333333
    Utility = 2.1015486385461966
    Savings =  4.761904761904762
    Optimal savings =  3.6507936507936507


**3.3. Capital steady state**


```python
#We define the parameters:
n=50
k=100
l=[k]
```

We create a list that contains the different levels of capital accross a period of time *n*:


```python
for t in range(n):
    k=((1-alpha)*(1-tau)*alpha)/((2+rho)*alpha+(1+rho)*(1-alpha)*tau)*A*k**alpha
    l.append(k)
```

Numerical steady state:


```python
K_ss=(((1-alpha)*(1-tau)*alpha)/((2+rho)*alpha+(1+rho)*(1-alpha)*tau)*A)**(1/(1-alpha))
print('Numerical steady state =',K_ss)
```

    Numerical steady state = 26.875709644766083


We now plot the list created earlier and the numerical steady state:


```python
fig = plt.figure(figsize=(6,6/1.5))
ax = fig.add_subplot(1,1,1)
ax.plot(l,label=r'$K_{t+1}$')
ax.axhline(K_ss,ls='--',color='red',label='Analytical steady state')
ax.legend(frameon=True)
fig.tight_layout()
plt.grid()
```

We now make our results interactive :


```python
A_inter=interactive(fct.f,x=20)
print('Choose A:')
display(A_inter)
```

    Choose A:



    interactive(children=(IntSlider(value=20, description='x', max=60, min=-20), Output()), _dom_classes=('widget-…



```python
alpha_inter=interactive(fct.f,x=0.7)
print('Choose alpha:')
display(alpha_inter)
```

    Choose alpha:



    interactive(children=(FloatSlider(value=0.7, description='x', max=2.0999999999999996, min=-0.7), Output()), _d…



```python
rho_inter=interactive(fct.f,x=0.1)
print('Choose rho:')
display(rho_inter)
```

    Choose rho:



    interactive(children=(FloatSlider(value=0.1, description='x', max=0.30000000000000004, min=-0.1), Output()), _…



```python
tau_inter=interactive(fct.f,x=0.05)
print('Choose tau:')
display(tau_inter)
```

    Choose tau:



    interactive(children=(FloatSlider(value=0.05, description='x', max=0.15000000000000002, min=-0.05), Output()),…



```python
#We create a list of the level of capital from t to t+n: 
n=50
k=100
l_inter=[k]
```


```python
def plotfct(A,alpha,rho,tau,k=100):
    l_inter=[]
    for t in range(50):
        k=((1-alpha)*(1-tau)*alpha)/((2+rho)*alpha+(1+rho)*(1-alpha)*tau)*(A)*k**alpha
        l_inter.append(k)
        
    K_ss_inter=(((1-alpha)*(1-tau)*alpha)/((2+rho)*alpha+(1+rho)*(1-alpha)*tau)*(A))**(1/(1-alpha))

    #We plot the analytical steady state and the level of capital generated by the list above:
    fig = plt.figure(figsize=(6,6/1.5))
    ax = fig.add_subplot(1,1,1)
    ax.plot(l_inter,label=r'$K_{t+1}$')
    ax.axhline(K_ss_inter,ls='--',color='red',label='Analytical steady state')
    ax.legend(frameon=True)
    fig.tight_layout()
    plt.grid()
```


```python
import ipywidgets as widgets
```


```python
widgets.interact(plotfct,
                 A=widgets.FloatSlider(description="A",min=1,max=100,step=5,value=20),
                 alpha=widgets.FloatSlider(description="alpha",min=0.01,max=0.99,step=0.05,value=0.5),
                 rho=widgets.FloatSlider(description="rho",min=0.01,max=0.99,step=0.05,value=0.1),
                 tau=widgets.FloatSlider(description="tau",min=0.01,max=0.99,step=0.05,value=0.2),
                 k=widgets.fixed(100))
```


    interactive(children=(FloatSlider(value=20.0, description='A', min=1.0, step=5.0), FloatSlider(value=0.5, desc…





    <function __main__.plotfct(A, alpha, rho, tau, k=100)>




```python
for t in range(n):
    k=((1-alpha_inter.result)*(1-tau_inter.result)*alpha_inter.result)/((2+rho_inter.result)*alpha_inter.result+(1+rho_inter.result)*(1-alpha_inter.result)*tau_inter.result)*(A_inter.result)*k**alpha_inter.result
    l_inter.append(k)
```


```python
K_ss_inter=(((1-alpha_inter.result)*(1-tau_inter.result)*alpha_inter.result)/((2+rho_inter.result)*alpha_inter.result+(1+rho_inter.result)*(1-alpha_inter.result)*tau_inter.result)*(A_inter.result))**(1/(1-alpha_inter.result))
print('The analytical steady state is :',K_ss_inter)
```

    The analytical steady state is : 31.622116722161557



```python
#We plot the analytical steady state and the level of capital generated by the list above:
fig = plt.figure(figsize=(6,6/1.5))
ax = fig.add_subplot(1,1,1)
ax.plot(l_inter,label=r'$K_{t+1}$')
ax.axhline(K_ss_inter,ls='--',color='red',label='Analytical steady state')
ax.legend(frameon=True)
fig.tight_layout()
plt.grid()
```

# 4. Extension: Dynamics of capital accumulation

**4.1. We first use the basic level of savings $s_{t}$**


```python
# Define parameters
capital_1,capital_2 = fct.transition_curve(A,alpha,rho)

# Plot
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(capital_1,capital_2)
ax.plot(capital_1,capital_1, '--', color='grey')
plt.text(45,32,'$k_t+1$')
plt.text(35,45,'$k_t=k_t+1$')
plt.text(32,30,'$k^*$')
ax.set_xlabel('$k_t$')
ax.set_ylabel('$k_t+1$')
ax.set_title('Transition curve')
ax.set_xlim([-0.001,50])
ax.set_ylim([0,50]);
plt.grid()
```

We can observe that there is an intersection between transition curve (in blue) and the 45° line (in grey) at around $k_{t+1}=k_{t}=20$. This intersection represent the capital steady state $k_{ss}^{*}$. 

**4.2. we use the optimal level of savings $s_{t}^{*}$:**


```python
# Define parameters
capital_1,capital_2 = fct.transition_curve2(A,alpha,tau,rho,R)

# Plot
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(capital_1,capital_2)
ax.plot(capital_1,capital_1, '--', color='grey')
plt.text(27,17,'$k_t+1$')
plt.text(22,27,'$k_t=k_t+1$')
plt.text(13,10,'$k^*$')
ax.set_xlabel('$k_t$')
ax.set_ylabel('$k_t+1$')
ax.set_title('Transition curve')
ax.set_xlim([-0.001,30])
ax.set_ylim([0,30]);
plt.grid()
```

We observe that by using the optimal savings $s_{t}^{*}$, we obtain a smaller capital steady state level $k_{ss}^{*}$.


```python
kvec=np.linspace(1e-8,35,500)
for i in range(499):
    kt1=fct.transition3(kvec[i+1],kvec[i],A,alpha,tau,rho,delta)
    kvec[i]=kt1
print(kvec)
```

    [ 0.42250596  0.68556991  0.91017317  1.11289324  1.30074739  1.47754239
      1.64563879  1.80662929  1.96165339  2.11156233  2.25701345  2.39852792
      2.53652794  2.67136161  2.80332032  2.93265115  3.05956589  3.18424788
      3.30685723  3.42753475  3.54640518  3.66357968  3.77915785  3.89322938
      4.00587544  4.11716972  4.22717949  4.33596625  4.44358652  4.55009234
      4.65553177  4.75994935  4.86338639  4.96588137  5.06747016  5.16818627
      5.2680611   5.3671241   5.46540293  5.56292365  5.65971079  5.75578755
      5.85117583  5.94589638  6.03996887  6.13341195  6.22624337  6.31847998
      6.41013785  6.50123229  6.59177791  6.68178865  6.77127785  6.86025827
      6.94874213  7.03674112  7.12426648  7.21132899  7.29793899  7.38410642
      7.46984086  7.5551515   7.64004722  7.72453656  7.80862775  7.89232873
      7.97564719  8.05859051  8.14116587  8.22338017  8.30524011  8.38675217
      8.46792261  8.5487575   8.62926273  8.709444    8.78930683  8.86885661
      8.94809853  9.02703765  9.10567889  9.18402701  9.26208666  9.33986233
      9.41735843  9.49457922  9.57152886  9.64821138  9.72463074  9.80079076
      9.87669518  9.95234766 10.02775174 10.1029109  10.17782851 10.25250788
     10.32695223 10.40116469 10.47514835 10.5489062  10.62244118 10.69575615
     10.7688539  10.84173718 10.91440867 10.98687098 11.05912667 11.13117825
     11.20302817 11.27467885 11.34613262 11.41739179 11.48845862 11.55933531
     11.63002404 11.70052692 11.77084603 11.84098342 11.91094108 11.98072098
     12.05032503 12.11975513 12.18901312 12.25810083 12.32702003 12.39577248
     12.46435989 12.53278396 12.60104633 12.66914864 12.73709249 12.80487944
     12.87251104 12.93998881 13.00731424 13.07448879 13.1415139  13.20839099
     13.27512145 13.34170666 13.40814796 13.47444667 13.5406041  13.60662154
     13.67250024 13.73824144 13.80384638 13.86931625 13.93465224 13.99985552
     14.06492724 14.12986852 14.19468048 14.25936422 14.32392082 14.38835135
     14.45265686 14.51683837 14.58089692 14.6448335  14.7086491  14.7723447
     14.83592126 14.89937973 14.96272105 15.02594614 15.0890559  15.15205124
     15.21493303 15.27770216 15.34035948 15.40290584 15.46534208 15.52766903
     15.5898875  15.65199829 15.71400221 15.77590004 15.83769254 15.89938049
     15.96096464 16.02244573 16.0838245  16.14510168 16.20627798 16.26735411
     16.32833077 16.38920866 16.44998844 16.51067081 16.57125642 16.63174594
     16.69214    16.75243927 16.81264436 16.87275591 16.93277454 16.99270085
     17.05253547 17.11227897 17.17193196 17.23149503 17.29096874 17.35035368
     17.4096504  17.46885946 17.52798143 17.58701684 17.64596623 17.70483014
     17.76360911 17.82230364 17.88091427 17.93944149 17.99788582 18.05624777
     18.11452781 18.17272645 18.23084417 18.28888144 18.34683875 18.40471657
     18.46251535 18.52023557 18.57787766 18.6354421  18.69292932 18.75033976
     18.80767387 18.86493207 18.9221148  18.97922247 19.03625551 19.09321434
     19.15009937 19.206911   19.26364964 19.32031569 19.37690954 19.43343159
     19.48988223 19.54626184 19.60257081 19.6588095  19.7149783  19.77107758
     19.82710769 19.88306902 19.93896191 19.99478672 20.05054382 20.10623354
     20.16185623 20.21741225 20.27290192 20.3283256  20.38368361 20.43897629
     20.49420396 20.54936695 20.60446558 20.65950018 20.71447106 20.76937854
     20.82422293 20.87900453 20.93372366 20.98838062 21.04297571 21.09750923
     21.15198147 21.20639274 21.26074331 21.31503348 21.36926354 21.42343377
     21.47754445 21.53159586 21.58558828 21.63952198 21.69339723 21.74721431
     21.80097349 21.85467502 21.90831917 21.96190621 22.0154364  22.06890998
     22.12232722 22.17568838 22.22899369 22.28224341 22.33543779 22.38857708
     22.44166151 22.49469133 22.54766678 22.60058809 22.6534555  22.70626926
     22.75902958 22.8117367  22.86439085 22.91699225 22.96954114 23.02203772
     23.07448224 23.1268749  23.17921593 23.23150554 23.28374395 23.33593138
     23.38806802 23.44015411 23.49218984 23.54417542 23.59611105 23.64799696
     23.69983333 23.75162036 23.80335827 23.85504724 23.90668748 23.95827918
     24.00982254 24.06131774 24.11276499 24.16416446 24.21551636 24.26682087
     24.31807817 24.36928845 24.4204519  24.4715687  24.52263902 24.57366305
     24.62464097 24.67557296 24.72645919 24.77729984 24.82809508 24.87884508
     24.92955003 24.98021008 25.0308254  25.08139618 25.13192256 25.18240473
     25.23284284 25.28323706 25.33358755 25.38389447 25.43415799 25.48437826
     25.53455545 25.5846897  25.63478119 25.68483005 25.73483646 25.78480055
     25.83472249 25.88460242 25.9344405  25.98423688 26.0339917  26.08370512
     26.13337727 26.18300831 26.23259839 26.28214764 26.3316562  26.38112423
     26.43055186 26.47993924 26.5292865  26.57859379 26.62786123 26.67708897
     26.72627715 26.7754259  26.82453535 26.87360564 26.92263691 26.97162928
     27.02058288 27.06949786 27.11837433 27.16721243 27.21601228 27.26477401
     27.31349776 27.36218364 27.41083179 27.45944232 27.50801536 27.55655104
     27.60504947 27.65351078 27.70193509 27.75032252 27.79867319 27.84698722
     27.89526473 27.94350583 27.99171065 28.03987929 28.08801188 28.13610853
     28.18416936 28.23219447 28.28018398 28.32813801 28.37605666 28.42394005
     28.47178829 28.51960148 28.56737975 28.61512318 28.66283191 28.71050602
     28.75814564 28.80575086 28.85332179 28.90085854 28.94836121 28.99582991
     29.04326474 29.0906658  29.13803319 29.18536702 29.23266739 29.2799344
     29.32716815 29.37436874 29.42153627 29.46867084 29.51577254 29.56284148
     29.60987774 29.65688144 29.70385266 29.75079149 29.79769805 29.84457241
     29.89141468 29.93822494 29.98500329 30.03174983 30.07846465 30.12514783
     30.17179947 30.21841966 30.26500849 30.31156605 30.35809243 30.40458772
     30.451052   30.49748537 30.54388792 30.59025972 30.63660088 30.68291146
     30.72919157 30.77544128 30.82166069 30.86784986 30.9140089  30.96013788
     31.00623689 31.052306   31.09834531 31.1443549  31.19033484 31.23628522
     31.28220612 31.32809761 31.37395979 31.41979273 31.46559651 31.5113712
     31.5571169  31.60283367 31.64852159 31.69418074 31.7398112  31.78541305
     31.83098636 31.87653121 31.92204767 31.96753582 32.01299573 32.05842749
     32.10383115 32.14920681 32.19455452 32.23987438 32.28516643 32.33043077
     32.37566746 35.        ]



```python

```


```python
obj2 = lambda k: k - fct.transition3(k,k1,A,alpha,tau,rho,delta)
result = optimize.root_scalar(obj2,bracket=[1e-8,1],method='brentq')
result.root
```


```python
# Define parameters
capital_5,capital_6 = fct.transition_curve3(A1,alpha1,tau1,rho1,delta1)

# Plot
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(capital_5,capital_6)
ax.plot(capital_5,capital_5, '--', color='grey')
#plt.text(27,17,'$k_t+1$')
#plt.text(22,27,'$k_t=k_t+1$')
#plt.text(13,10,'$k^*$')
ax.set_xlabel('$k_t$')
ax.set_ylabel('$k_t+1$')
ax.set_title('Transition curve')
ax.set_xlim([-0.001,1])
ax.set_ylim([0,1]);
plt.grid()
```


```python
K_ss_1=(((1-alpha1)*(1-tau1)*alpha1)/((2+rho1)*alpha1+(1+rho1)*(1-alpha1)*tau1)*(A1))**(1/(1-alpha1))
K_ss_1
```




    0.28660524259061815



**4.3. Finally, we study the capital accumulation $k_{t+1}$ for different levels of the discount parameter $\rho$:**


```python
# Make a plot
fig = plt.figure()
ax = fig.add_subplot(1,1,1)

#transition curves
for rho in [0.01,0.05,0.1,0.2,0.4]:
    capital_3,capital_4 = fct.transition_curve2(A,alpha,tau,rho,R)
    ax.plot(capital_3,capital_4,label=f'rho={rho}')
  
    
    
# Plot transition curves
ax.legend(loc='lower right',facecolor='white',frameon=True)
ax.plot(capital_3,capital_3, '--', color='grey')
ax.set_xlabel('$k_t$')
ax.set_ylabel('$k_t+1$')
ax.set_title('Transition curve')
ax.set_xlim([-0.001,20])
ax.set_ylim([0,20]);  
fig.set_figwidth(9)
fig.set_figheight(5)
plt.grid()
```

We can conclude from this graph that the higher the discount parameter $\rho$ is, the lower the capital steady state $k_{ss}^{*}$ is. Thus, $\rho$ is negatively correlated to the capital steady state $k_{ss}^{*}$.

# 5. Conclusion

We solved a two-period overlapping generations model (OLG) in a 'pay-as-you-go' setting in order to find the optimal savings, capital accumulation and steady state of our model. The model was solved analytically with a solver. Then, the numerical part was solved with help of `Model.Project.py`, and different dynamics of capital accumulation were shown. 

Overall, we found that our analytical and numerical steady converge to the same value in the long-run. Also, we found that the contribution that the young generation provide to the old, i.e. $\tau$, and the discouting parameter $\rho$ and optimal savings $s_{t}^{*}$ are all negatively correlated to capital accumulation.

94863e70696c15c7d2301f0a152dc524c756a6d3
ipynb
Jupyter Notebook
modelproject/Model_ProjectFinal_change.ipynb
NumEconCopenhagen/projects-2022-anna-thomas-oscar
6f82722a7a1a40cf1b0893a72095a2f2034f33e6
MIT
modelproject/Model_ProjectFinal_change.ipynb
NumEconCopenhagen/projects-2022-anna-thomas-oscar
6f82722a7a1a40cf1b0893a72095a2f2034f33e6
MIT
2022-03-31T10:23:24.000Z
2022-03-31T10:23:24.000Z
modelproject/Model_ProjectFinal_change.ipynb
NumEconCopenhagen/projects-2022-anna-thomas-oscar
6f82722a7a1a40cf1b0893a72095a2f2034f33e6
MIT
Qwen/Qwen-72B
1. YES
2. YES