In [112]:
from dolo import *

In [113]:
model = yaml_import("../models/consumption_savings.yaml")

In [114]:
dr = time_iteration(model)


Solving WITH complementarities.
------------------------------------------------
| N   |  Error     | Gain     | Time     | nit |
------------------------------------------------
|   1 |  8.000e-01 |      nan |    0.024 |   5 |
|   2 |  8.027e-02 |    0.100 |    0.031 |   6 |
|   3 |  4.881e-02 |    0.608 |    0.019 |   4 |
|   4 |  3.369e-02 |    0.690 |    0.023 |   5 |
|   5 |  2.470e-02 |    0.733 |    0.019 |   4 |
|   6 |  1.938e-02 |    0.785 |    0.023 |   5 |
|   7 |  1.541e-02 |    0.795 |    0.023 |   5 |
|   8 |  1.223e-02 |    0.794 |    0.020 |   4 |
|   9 |  9.744e-03 |    0.797 |    0.021 |   4 |
|  10 |  7.958e-03 |    0.817 |    0.020 |   4 |
|  11 |  6.347e-03 |    0.798 |    0.020 |   4 |
|  12 |  5.251e-03 |    0.827 |    0.019 |   4 |
|  13 |  5.288e-03 |    1.007 |    0.023 |   5 |
|  14 |  5.270e-03 |    0.997 |    0.023 |   5 |
|  15 |  5.295e-03 |    1.005 |    0.023 |   5 |
|  16 |  5.280e-03 |    0.997 |    0.024 |   5 |
|  17 |  5.154e-03 |    0.976 |    0.024 |   5 |
|  18 |  5.190e-03 |    1.007 |    0.024 |   5 |
|  19 |  5.256e-03 |    1.013 |    0.016 |   3 |
|  20 |  5.356e-03 |    1.019 |    0.016 |   3 |
|  21 |  5.478e-03 |    1.023 |    0.016 |   3 |
|  22 |  5.613e-03 |    1.025 |    0.016 |   3 |
|  23 |  5.752e-03 |    1.025 |    0.016 |   3 |
|  24 |  5.891e-03 |    1.024 |    0.016 |   3 |
|  25 |  6.022e-03 |    1.022 |    0.019 |   4 |
|  26 |  6.143e-03 |    1.020 |    0.019 |   4 |
|  27 |  6.251e-03 |    1.018 |    0.019 |   4 |
|  28 |  6.343e-03 |    1.015 |    0.019 |   4 |
|  29 |  6.420e-03 |    1.012 |    0.019 |   4 |
|  30 |  6.479e-03 |    1.009 |    0.020 |   4 |
|  31 |  6.520e-03 |    1.006 |    0.019 |   4 |
|  32 |  6.542e-03 |    1.003 |    0.019 |   4 |
|  33 |  6.545e-03 |    1.001 |    0.020 |   4 |
|  34 |  6.530e-03 |    0.998 |    0.019 |   4 |
|  35 |  6.495e-03 |    0.995 |    0.020 |   4 |
|  36 |  6.442e-03 |    0.992 |    0.019 |   4 |
|  37 |  6.371e-03 |    0.989 |    0.019 |   4 |
|  38 |  6.283e-03 |    0.986 |    0.024 |   5 |
|  39 |  6.180e-03 |    0.984 |    0.025 |   5 |
|  40 |  6.062e-03 |    0.981 |    0.023 |   5 |
|  41 |  5.930e-03 |    0.978 |    0.024 |   5 |
|  42 |  5.786e-03 |    0.976 |    0.023 |   5 |
|  43 |  5.631e-03 |    0.973 |    0.023 |   5 |
|  44 |  5.465e-03 |    0.971 |    0.016 |   3 |
|  45 |  5.289e-03 |    0.968 |    0.016 |   3 |
|  46 |  5.105e-03 |    0.965 |    0.015 |   3 |
|  47 |  4.915e-03 |    0.963 |    0.015 |   3 |
|  48 |  4.719e-03 |    0.960 |    0.015 |   3 |
|  49 |  4.519e-03 |    0.958 |    0.015 |   3 |
|  50 |  4.317e-03 |    0.955 |    0.015 |   3 |
|  51 |  4.114e-03 |    0.953 |    0.015 |   3 |
|  52 |  3.911e-03 |    0.951 |    0.016 |   3 |
|  53 |  3.710e-03 |    0.949 |    0.016 |   3 |
|  54 |  3.512e-03 |    0.947 |    0.015 |   3 |
|  55 |  3.318e-03 |    0.945 |    0.015 |   3 |
|  56 |  3.128e-03 |    0.943 |    0.015 |   3 |
|  57 |  2.944e-03 |    0.941 |    0.015 |   3 |
|  58 |  2.765e-03 |    0.939 |    0.015 |   3 |
|  59 |  2.593e-03 |    0.938 |    0.015 |   3 |
|  60 |  2.429e-03 |    0.936 |    0.015 |   3 |
|  61 |  2.271e-03 |    0.935 |    0.014 |   3 |
|  62 |  2.121e-03 |    0.934 |    0.015 |   3 |
|  63 |  1.978e-03 |    0.933 |    0.015 |   3 |
|  64 |  1.843e-03 |    0.932 |    0.015 |   3 |
|  65 |  1.715e-03 |    0.931 |    0.015 |   3 |
|  66 |  1.595e-03 |    0.930 |    0.016 |   3 |
|  67 |  1.481e-03 |    0.929 |    0.016 |   3 |
|  68 |  1.375e-03 |    0.928 |    0.015 |   3 |
|  69 |  1.275e-03 |    0.928 |    0.015 |   3 |
|  70 |  1.182e-03 |    0.927 |    0.015 |   3 |
|  71 |  1.095e-03 |    0.926 |    0.014 |   3 |
|  72 |  1.014e-03 |    0.926 |    0.010 |   2 |
|  73 |  9.379e-04 |    0.925 |    0.010 |   2 |
|  74 |  8.674e-04 |    0.925 |    0.011 |   2 |
|  75 |  8.019e-04 |    0.924 |    0.011 |   2 |
|  76 |  7.410e-04 |    0.924 |    0.011 |   2 |
|  77 |  6.845e-04 |    0.924 |    0.011 |   2 |
|  78 |  6.321e-04 |    0.923 |    0.011 |   2 |
|  79 |  5.835e-04 |    0.923 |    0.011 |   2 |
|  80 |  5.386e-04 |    0.923 |    0.011 |   2 |
|  81 |  4.969e-04 |    0.923 |    0.011 |   2 |
|  82 |  4.584e-04 |    0.922 |    0.011 |   2 |
|  83 |  4.228e-04 |    0.922 |    0.011 |   2 |
|  84 |  3.899e-04 |    0.922 |    0.011 |   2 |
|  85 |  3.595e-04 |    0.922 |    0.011 |   2 |
|  86 |  3.314e-04 |    0.922 |    0.011 |   2 |
|  87 |  3.054e-04 |    0.922 |    0.011 |   2 |
|  88 |  2.815e-04 |    0.922 |    0.010 |   2 |
|  89 |  2.594e-04 |    0.921 |    0.011 |   2 |
|  90 |  2.390e-04 |    0.921 |    0.011 |   2 |
|  91 |  2.202e-04 |    0.921 |    0.011 |   2 |
|  92 |  2.029e-04 |    0.921 |    0.011 |   2 |
|  93 |  1.869e-04 |    0.921 |    0.011 |   2 |
|  94 |  1.721e-04 |    0.921 |    0.011 |   2 |
|  95 |  1.585e-04 |    0.921 |    0.011 |   2 |
|  96 |  1.460e-04 |    0.921 |    0.011 |   2 |
|  97 |  1.345e-04 |    0.921 |    0.011 |   2 |
|  98 |  1.238e-04 |    0.921 |    0.011 |   2 |
|  99 |  1.140e-04 |    0.921 |    0.011 |   2 |
| 100 |  1.050e-04 |    0.921 |    0.011 |   2 |
| 101 |  9.666e-05 |    0.921 |    0.011 |   2 |
| 102 |  8.900e-05 |    0.921 |    0.012 |   2 |
| 103 |  8.194e-05 |    0.921 |    0.011 |   2 |
| 104 |  7.544e-05 |    0.921 |    0.011 |   2 |
| 105 |  6.945e-05 |    0.921 |    0.011 |   2 |
| 106 |  6.394e-05 |    0.921 |    0.012 |   2 |
| 107 |  5.886e-05 |    0.921 |    0.011 |   2 |
| 108 |  5.419e-05 |    0.921 |    0.011 |   2 |
| 109 |  4.988e-05 |    0.921 |    0.012 |   2 |
| 110 |  4.592e-05 |    0.921 |    0.011 |   2 |
| 111 |  4.227e-05 |    0.921 |    0.011 |   2 |
| 112 |  3.891e-05 |    0.920 |    0.011 |   2 |
| 113 |  3.581e-05 |    0.920 |    0.010 |   2 |
| 114 |  3.296e-05 |    0.920 |    0.010 |   2 |
| 115 |  3.034e-05 |    0.920 |    0.010 |   2 |
| 116 |  2.793e-05 |    0.920 |    0.011 |   2 |
| 117 |  2.571e-05 |    0.920 |    0.011 |   2 |
| 118 |  2.366e-05 |    0.920 |    0.011 |   2 |
| 119 |  2.178e-05 |    0.920 |    0.011 |   2 |
| 120 |  2.004e-05 |    0.920 |    0.012 |   2 |
| 121 |  1.845e-05 |    0.920 |    0.008 |   1 |
| 122 |  1.698e-05 |    0.920 |    0.007 |   1 |
| 123 |  1.563e-05 |    0.920 |    0.007 |   1 |
| 124 |  1.438e-05 |    0.920 |    0.007 |   1 |
| 125 |  1.324e-05 |    0.920 |    0.006 |   1 |
| 126 |  1.218e-05 |    0.920 |    0.007 |   1 |
| 127 |  1.121e-05 |    0.920 |    0.007 |   1 |
| 128 |  1.032e-05 |    0.920 |    0.007 |   1 |
| 129 |  9.499e-06 |    0.920 |    0.006 |   1 |
| 130 |  8.742e-06 |    0.920 |    0.006 |   1 |
| 131 |  8.046e-06 |    0.920 |    0.006 |   1 |
| 132 |  7.405e-06 |    0.920 |    0.006 |   1 |
| 133 |  6.815e-06 |    0.920 |    0.007 |   1 |
| 134 |  6.272e-06 |    0.920 |    0.007 |   1 |
| 135 |  5.772e-06 |    0.920 |    0.007 |   1 |
| 136 |  5.313e-06 |    0.920 |    0.006 |   1 |
| 137 |  4.889e-06 |    0.920 |    0.006 |   1 |
| 138 |  4.500e-06 |    0.920 |    0.006 |   1 |
| 139 |  4.141e-06 |    0.920 |    0.007 |   1 |
| 140 |  3.811e-06 |    0.920 |    0.006 |   1 |
| 141 |  3.508e-06 |    0.920 |    0.006 |   1 |
| 142 |  3.228e-06 |    0.920 |    0.006 |   1 |
| 143 |  2.971e-06 |    0.920 |    0.007 |   1 |
| 144 |  2.734e-06 |    0.920 |    0.006 |   1 |
| 145 |  2.516e-06 |    0.920 |    0.007 |   1 |
| 146 |  2.316e-06 |    0.920 |    0.006 |   1 |
| 147 |  2.131e-06 |    0.920 |    0.006 |   1 |
| 148 |  1.961e-06 |    0.920 |    0.006 |   1 |
| 149 |  1.805e-06 |    0.920 |    0.006 |   1 |
| 150 |  1.661e-06 |    0.920 |    0.007 |   1 |
| 151 |  1.529e-06 |    0.920 |    0.007 |   1 |
| 152 |  1.407e-06 |    0.920 |    0.007 |   1 |
| 153 |  1.295e-06 |    0.920 |    0.014 |   1 |
| 154 |  1.192e-06 |    0.920 |    0.014 |   1 |
| 155 |  1.097e-06 |    0.920 |    0.007 |   1 |
| 156 |  1.009e-06 |    0.920 |    0.007 |   1 |
| 157 |  9.288e-07 |    0.920 |    0.007 |   1 |
------------------------------------------------
Elapsed: 2.1186208724975586 seconds.
------------------------------------------------

One can also try the faster version

Stochastic Simulations


In [115]:
# Shocks are discretized as a markov chain by default:
dp = model.exogenous.discretize()
sim_shock = dp.simulate(10, 100, i0=1)
for i in range(10):
    plt.plot(sim_shock[:,i,0], color='red', alpha=0.5)



In [116]:
sim = simulate(model, dr, i0=1, N=100)

In [117]:
plt.subplot(121)
for i in range(10):
    plt.plot(sim.sel(N=i,V='c'), color='red', alpha=0.5)
plt.ylabel("$c_t$")
plt.xlabel("$t$")
plt.subplot(122)
for i in range(10):
    plt.plot(sim.sel(N=i,V='w'), color='red', alpha=0.5)
plt.xlabel("$t$")
plt.ylabel("$w_t$")

plt.tight_layout()


Ergodic distribution


In [118]:
sim_long = simulate(model, dr, i0=1, N=1000, T=200)

In [119]:
import seaborn
seaborn.distplot(sim_long.sel(T=199, V='w'))
plt.xlabel("$w$")


FutureWarning:/opt/pablo/anaconda3/lib/python3.6/site-packages/scipy/stats/stats.py:1713
    Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
Out[119]:
Text(0.5, 0, '$w$')

Plotting Decision Rule


In [120]:
tab = tabulate(model, dr,'w')

In [121]:
from matplotlib import pyplot as plt

In [122]:
stable_wealth = model.eval_formula('1/r+(1-1/r)*w(0)', tab)
plt.plot(tab['w'], tab['w'],color='black', linestyle='--')
plt.plot(tab['w'], stable_wealth,color='black', linestyle='--')
plt.plot(tab['w'], tab['c'])
plt.xlabel("w_t")
plt.ylabel("c_t")
plt.grid()



In [ ]:


In [ ]: