This is an infinite horizon, deterministic model with time $t$ measured in years.

In [1]:

```
from demos.setup import np, plt, demo
from compecon import DDPmodel
%matplotlib inline
```

In [2]:

```
maxage = 5
repcost = 75
delta = 0.9
```

The state variable $a \in \{1, 2, 3, \dots, n\}$ is the age of the asset in years.

In [3]:

```
S = np.arange(1, 1 + maxage) # machine age
n = S.size # number of states
```

The action variable $x \in \{\text{keep, replace}\}$ is the hold-replacement decision.

In [4]:

```
X = ['keep', 'replace'] # list of actions
m = len(X) # number of actions
```

The reward function is \begin{equation} f(a, x) = \begin{cases} p(a), &x = \text{keep} \ p(0) - c, &x = \text{replace} \end{cases} \end{equation}

Assuming a profit contribution $p(a) = 50 − 2.5a − 2.5a^2$ that is a function of the asset age $a$ in years:

In [5]:

```
f = np.zeros((m, n))
f[0] = 50 - 2.5 * S - 2.5 * S ** 2
f[1] = 50 - repcost
f[0, -1] = -np.inf
```

In [6]:

```
g = np.zeros_like(f)
g[0] = np.arange(1, n + 1)
g[0, -1] = n - 1 # adjust last state so it doesn't go out of bounds
```

The value of an asset of age a satisfies the Bellman equation \begin{equation} V(a) = \max{p(a) + \delta V(a + 1),\quad p(0) − c + \delta V(1)} \end{equation}

where we set $p(n) = -\infty$ to enforce replacement of an asset of age $n$. The Bellman equation asserts that if the manufacturer keeps an asset of age $a$, he earns $p(a)$ over the coming year and begins the subsequent year with an asset that is one year older and worth $V(a+1)$; if he replaces the asset, however, he starts the year with a new asset, earns $p(0)−c$ over the year, and begins the subsequent year with an asset that is one year old and worth $V(1)$. Actually, our language is a little loose here. The value $V(a)$ measures not only the current and future net earnings of an asset of age $a$, but also the net earnings of all future assets that replace it.

To solve and simulate this model, use the CompEcon class `DDPmodel`

.

In [7]:

```
model = DDPmodel(f, g, delta)
model.solve()
```

Out[7]:

This Figure gives the value of the firm at the beginning of the period as a function of the asset’s age.

In [8]:

```
demo.figure('Optimal Value Function', 'Age of Machine', 'Value')
plt.plot(S, model.value)
plt.show()
```

The path was computed by performing a deterministic simulation of 12 years in duration using the `simulate()`

method.

In [9]:

```
sinit, nyrs = S.min() - 1, 12
t = np.arange(1 + nyrs)
spath, xpath = model.simulate(sinit, nyrs)
```

Next Figure gives the age of the asset along the optimal path. As can be seen in this figure, the asset is replaced every four years.

In [10]:

```
demo.figure('Optimal State Path', 'Year', 'Age of Machine', [0, 12])
plt.plot(t, S[spath])
plt.show()
```