示例#1
0
    fx = -sk**-alpha
    fxx = -alpha * sk**(-alpha - 1)
    return f, fx, fxx


def transition(s, k, i, j, in_, e):
    g = gamma * k + e * k**beta
    gx = gamma + beta * e * k**(beta - 1)
    gxx = (beta - 1) * beta * e * k**(beta - 2)
    return g, gx, gxx


growth = DPmodel(basis,
                 reward,
                 transition,
                 bounds,
                 x=['investment'],
                 discount=delta,
                 e=e,
                 w=w)

# Deterministic Steady-State
kstar = ((1 - delta * gamma) / (delta * beta))**(
    1 / (beta - 1))  # determistic steady-state capital investment
sstar = gamma * kstar + kstar**beta  # deterministic steady-state wealth

# Check Model Derivatives
# dpcheck(model,sstar,kstar)

## SOLUTION

# Solve Bellman Equation
示例#2
0
        (q - s)**2)
    fx = p[i] - beta[0] - beta[1] * q - alpha * (q - s)
    fxx = (-beta[1] - alpha) * np.ones_like(s)
    return f, fx, fxx


def transition(s, q, i, j, in_, e):
    return q.copy(), np.ones_like(q), np.zeros_like(q)


# Model Structure

model = DPmodel(basis,
                reward,
                transition,
                bounds,
                i=['Low price', 'Average price', 'High Price'],
                x=['Current production'],
                discount=delta,
                q=q)

# Check Model Derivatives
# dpcheck(model,sstar,sstar)

## SOLUTION

# Solve Bellman Equation
model.solve()
resid, s, v, x = model.solution()
"""

示例#3
0
# Model Structure

def profit(p, x, i, j):
    a = i + 1
    if j or a == A:
        return p * 50 - kappa
    else:
        return p * (alpha[0] + alpha[1] * a + alpha[2] * a ** 2 )

def transition(p, x, i, j, in_, e):
    return pbar + gamma * (p - pbar) + e

model = DPmodel(basis, profit, transition,
                # i=['a={}'.format(a+1) for a in range(A)],
                i=[a + 1 for a in range(A)],
                j=['keep', 'replace'],
                discount=delta, e=e, w=w, h=h)

# SOLUTION

S = model.solve()

pr = np.linspace(pmin, pmax, 10 * n)

# Plot Action-Contingent Value Functions

pp = demo.qplot('unit profit', 'value_j', 'i',
      data=S,
      main='Action-Contingent Value Functions',
      xlab='Net Unit Profit',
示例#4
0

def profit(p, x, i, j):
    return p * j - kappa * (1 - i) * j


def transition(p, x, i, j, in_, e):
    return pbar + gamma * (p - pbar) + e


# Model
model = DPmodel(BasisSpline(n, pmin, pmax, labels=['profit']),
                profit,
                transition,
                i=['idle', 'active'],
                j=['idle', 'active'],
                discount=delta,
                e=e,
                w=w,
                h=[[0, 0], [1, 1]])

## SOLUTION

# Solve Bellman Equation
S = model.solve(print=True)

# Plot Action-Contingent Value Functions

S['ij'] = pd.Categorical(
    S.i.astype(np.ndarray) + '--' + S.j.astype(np.ndarray))
S.ij.cat.categories = [
示例#5
0

def reward(s, x, i, j):
    return (price * s - kappa) * j


def transition(s, x, i, j, in_, e):
    if j:
        return np.full_like(s, gamma * smax)
    else:
        return s + gamma * (smax - s)


model = DPmodel(basis,
                reward,
                transition,
                discount=delta,
                j=['keep', 'clear cut'])

model.solve()
resid, sr, vr = model.residuals()

# Plot Action-Contingent Value Functions

demo.figure('Action-Contingent Value Functions', 'Biomass', 'Value of Stand')
plt.plot(sr, vr.T)
plt.legend(model.labels.j, loc='upper center')

# Compute and Plot Optimal Harvesting Stock Level
scrit = np.interp(0.0, vr[1] - vr[0], sr)
vcrit = np.interp(scrit, sr, vr[0])
示例#6
0
    f = (a0 - b0 + b1 * s) * q - (a1 + b1 / 2) * q**2
    fx = (a0 - b0 + b1 * s) - 2 * (a1 + b1 / 2) * q
    fxx = -2 * (a1 + b1 / 2) * np.ones_like(s)
    return f, fx, fxx


def transition(s, q, i, j, in_, e):
    g = s - q
    gx = -np.ones_like(s)
    gxx = np.zeros_like(s)
    return g, gx, gxx


model = DPmodel(basis,
                reward,
                transition,
                bounds,
                x=['extracted'],
                discount=delta)

# Check Model Derivatives
# dpcheck(model,smax,0)

## SOLUTION

# Solve Bellman Equation
model.solve()
resid, s, v, q = model.residuals()

# Compute and print abandonment point
sstar = (b[0] - a[0]) / b[1]
print('Abandonment Point = %5.2f' % sstar)
示例#7
0
n = 150
wmin = 0
wmax = 200
basis = BasisSpline(n, wmin, wmax, labels=['wage'])

# ## SOLUTION
#
# To represent the model, we create an instance of ```DPmodel```. Here, we assume a discout factor of $\delta=0.95$.

# In[7]:

model = DPmodel(basis,
                reward,
                transition,
                i=['unemployed', 'employed'],
                j=['idle', 'active'],
                discount=0.95,
                e=e,
                w=w,
                q=q)

# Then, we call the method ```solve``` to solve the Bellman equation

# In[8]:

S = model.solve(print=True)
S.head()

# ### Compute and Print Critical Action Wages

# In[9]:
    return f, fx, fxx


def transition(s, x, i, j, in_, e):
    g = s - x + e
    gx = -np.ones_like(s)
    gxx = np.zeros_like(s)
    return g, gx, gxx


# Model object

model = DPmodel(basis,
                reward,
                transition,
                bounds,
                x=['released'],
                discount=delta,
                e=e,
                w=w)

# Deterministic Steady-State
xstar = 1  # deterministic steady-state action
sstar = 1 + (a[0] * (1 - delta) / b[0])**(1 / b[1]
                                          )  # deterministic steady-state stock

# Check Model Derivatives
# dpcheck(model,sstar,xstar)

## SOLUTION

# Compute Linear-Quadratic Approximation at Collocation Nodes
def transition(s, k, i, j, in_, e):
    g = k ** beta
    gx = beta * k **(beta - 1)
    gxx = (beta - 1) * beta * k ** (beta - 2)
    return g, gx, gxx


sigma = 0.1
m=5
e,w = qnwnorm(m, -sigma**2 / 2, sigma**2)



growth_model = DPmodel(basis, reward, transition, bounds,
                       x=['Investment'],
                       discount=delta)


# ======== Steady-State
sstar = (beta * delta) ** (beta / (1 - beta))   # steady-state wealth
kstar = beta * delta * sstar                    # steady-state capital investment
vstar = np.log(sstar - kstar) / (1 - delta)     # steady-state value
pstar = 1 / (sstar * (1 - beta * delta))        # steady-state shadow price
b = 1 / (1 - delta * beta)


# ===========   Compute Analytic Solution at Collocation Nodes
vtrue = vstar + b * (np.log(snodes) - np.log(sstar))
ktrue = delta * beta * snodes
示例#10
0
dstates = ['idle', 'active']
dactions = ['close', 'open']

# The Bellman equation is represeted by a ```DPmodel``` object, where we assume a discount factor of $\delta=0.9$. Notice that the discrete state transition is deterministic, with transition matrix
# \begin{equation}
#     h=\begin{bmatrix}0&0\\1&1\end{bmatrix}
# \end{equation}

# In[7]:

model = DPmodel(basis,
                profit,
                transition,
                i=dstates,
                j=dactions,
                discount=0.9,
                e=e,
                w=w,
                h=[[0, 0], [1, 1]])

# ## SOLUTION
#
# To solve the model, we simply call the ```solve``` method on the ```model``` object.

# In[8]:

S = model.solve(print=True)
S.head()

# ### Plot Action-Contingent Value Functions
示例#11
0
    f = (q**(1 - gamma)) / (1 - gamma) - kappa * q
    fx = q**(-gamma) - kappa
    fxx = -gamma * q**(-gamma - 1)
    return f, fx, fxx


def transition(s, q, i, j, in_, e):
    g = alpha * (s - q) - 0.5 * beta * (s - q)**2
    gx = -alpha + beta * (s - q)
    gxx = -beta * np.zeros_like(s)
    return g, gx, gxx


model = DPmodel(basis,
                reward,
                transition,
                bounds,
                x=['harvest'],
                discount=delta)

# Steady-State

sstar = (alpha**2 - 1 / delta**2) / (2 * beta)  # steady-state stock
qstar = sstar - (delta * alpha - 1) / (delta * beta)  # steady-state action

# Print Steady-States
print('Steady States')
print('\tStock   = %5.2f' % sstar)
print('\tHarvest = %5.2f' % qstar)

# Check Model Derivatives
# dpcheck(model,sstar,qstar)
示例#12
0
# On the other hand, the age of the asset is deterministic: if it is replaced now it will be new ($a=0$) next period; otherwise its age will be $a+1$ next period if current age is $a$.

# In[9]:

h = np.zeros((2, A), int)
h[0, :-1] = np.arange(1, A)

# ## Model Structure

# In[10]:

model = DPmodel(basis,
                profit,
                transition,
                i=dstates,
                j=options,
                discount=delta,
                e=e,
                w=w,
                h=h)

# ### SOLUTION
# The ```solve``` method returns a pandas ```DataFrame```.

# In[11]:

S = model.solve()
S.head()

# ## Analysis
#
示例#13
0
    else:
        return s + gamma * (smax - s)


# ### Model Structure
# The value of the stand, given that it contains $s$ units of biomass at the beginning of the period, satisfies the Bellman equation
# 
# \begin{equation} V(s) = \max\left\{(ps-\kappa) + \delta V(\gamma s_{\max}),\quad \delta V[s+\gamma(s_{\max}-s)]  \right\}   \end{equation}
# 
# To solve and simulate this model, use the CompEcon class ```DPmodel```.

# In[7]:


model = DPmodel(basis, reward, transition,
                discount=delta,
                j=options)

S = model.solve()


# The ```solve``` method retuns a pandas ```DataFrame```, which can easily be used to make plots. To see the first 10 entries of `S`, we use the `head` method

# In[8]:


S.head()


# ## Analysis
示例#14
0
    return f, fx, fxx


def transition(s, x, i, j, in_, e):
    g = alpha + beta @ s + gamma @ x + e
    gx = np.tile(gamma, (1, x.size))
    gxx = np.zeros_like(s)
    return g, gx, gxx


# Model Structure

bank = DPmodel(basis,
               reward,
               transition,
               bounds,
               x=['interest'],
               discount=delta,
               e=e,
               w=w)

# Solve Bellman Equation

#sol = bank.solve()
# resid, s, v, x = bank.residuals(nr=5)  # fixme takes huge amount of memory to deal with vmax over the refined grid!

bank.check_derivatives()
"""
''' this is a temp fix '''