Exemplo n.º 1
0
def f(z):
    x, y = z
    fval = np.array([200 * x * (y - x**2) + 1 - x, 100 * (x**2 - y)])
    fjac = np.array([[200 * (y - x**2) - 400 * x**2 - 1, 200 * x],
                     [200 * x, -100]])

    return fval, fjac
Exemplo n.º 2
0
def f(z):
    x, y = z
    fval = np.array([200 * x * (y - x ** 2) + 1 - x,
                     100 * (x ** 2 - y)])
    fjac = np.array([[200 * (y - x ** 2) - 400 * x ** 2 - 1, 200 * x],
                         [200 * x, -100]])

    return fval, fjac
Exemplo n.º 3
0
def f(z):
    x, y = z
    fval = [x - x ** 2 - y ** 3,
            y - x * y + 0.5]
    fjac = [[1 - 2 * x, -3 * y **2],
            [-y, 1 - x]]

    return np.array(fval), np.array(fjac)
Exemplo n.º 4
0
#     delta   discount factor 

# Preliminary tasks

from compecon import BasisSpline, DPmodel
from compecon.quad import qnwnorm
from demos.setup import np, plt, demo
from warnings import simplefilter
simplefilter('ignore')


## FORMULATION
  
# Model Parameters
A       = 6                                # maximum asset age 
alpha   = np.array([50, -2.5, -2.5])       # production function coefficients
kappa   = 40                               # net replacement cost
pbar    = 1                                # long-run mean unit profit
gamma   = 0.5                              # unit profit autoregression coefficient
sigma   = 0.15                             # standard deviation of unit profit shock
delta   = 0.9                              # discount factor 

# Continuous State Shock Distribution
m = 5                                      # number of unit profit shocks
[e,w] = qnwnorm(m,0,sigma ** 2)               # unit profit shocks and probabilities

# Deterministic Discrete State Transitions
h = np.zeros((2, A))
h[0, :-1] = np.arange(1, A)

# Approximation Structure
Exemplo n.º 5
0
__author__ = 'Randall'

from demos.setup import np
from compecon import DDPmodel

## DEMDDP08 Job search model

# Model Parameters
u = 50  # weekly unemp. benefit
v = 60  # weekly value of leisure
pfind = 0.90  # prob. of finding job
pfire = 0.10  # prob. of being fired
delta = 0.99  # discount factor

# State Space
S = np.array([1, 2])  # vector of states
n = S.size  # number of states

# Action Space (idle=1, active=2)
X = ['idle', 'active']  # vector of actions
m = len(X)  # number of actions

# Reward Function
f = np.zeros((m, n))
f[0] = v  # gets leisure
f[1, 0] = u  # gets benefit

# State Transition Probability Matrix
P = np.zeros((m, n, n))
P[0, :, 0] = 1  # remains unemployed
P[1, 0, 0] = 1 - pfind  # finds no job
Exemplo n.º 6
0
__author__ = 'Randall'

from demos.setup import np
from compecon import DDPmodel

## DEMDDP08 Job search model

# Model Parameters
u     =  50                  # weekly unemp. benefit
v     =  60                  # weekly value of leisure
pfind = 0.90                 # prob. of finding job
pfire = 0.10                 # prob. of being fired
delta = 0.99                 # discount factor

# State Space
S = np.array([1, 2])         # vector of states
n = S.size                   # number of states

# Action Space (idle=1, active=2)
X = ['idle', 'active']          	# vector of actions
m = len(X)               	# number of actions

# Reward Function
f = np.zeros((m, n))
f[0] = v                   # gets leisure
f[1, 0] = u                   # gets benefit


# State Transition Probability Matrix
P = np.zeros((m, n, n))
P[0, :, 0] = 1                 # remains unemployed
Exemplo n.º 7
0
error = f2fit(x) - f2(x)
error.shape = nplot
x1.shape = nplot
x2.shape = nplot

fig = plt.figure(figsize=[15, 6])
ax = fig.gca(projection='3d',
             title='Chebyshev Approximation Error',
             xlabel='$x_1$',
             ylabel='$x_2$',
             zlabel='Error')
ax.plot_surface(x1,
                x2,
                error,
                rstride=1,
                cstride=1,
                cmap=cm.coolwarm,
                linewidth=0,
                antialiased=False)
plt.show()

# Compute partial Derivatives
x = np.array([[0.5], [0.5]])
order = [[1, 0, 2, 1, 0], [0, 1, 0, 1, 2]]

ff = f2fit(x, order)

print(
    'x   = [0.5, 0.5]\nf1  = {:7.4f}\nf2  = {:7.4f}\nf11 = {:7.4f}\nf12 = {:7.4f}\nf22 = {:7.4f}'
    .format(*ff))
Exemplo n.º 8
0
f2 = lambda x: np.cos(x[0]) / np.exp(x[1])

# Set degree and domain interpolation
n, a, b = 7, 0.0, 1.0
f2fit = BasisChebyshev([n, n], a, b, f=f2)

# Nice plot of function approximation error
nplot = [101, 101]
x = nodeunif(nplot, a, b)
x1, x2 = x
error = f2fit(x) - f2(x)
error.shape = nplot
x1.shape = nplot
x2.shape = nplot

fig = plt.figure(figsize=[15, 6])
ax = fig.gca(projection='3d', title='Chebyshev Approximation Error',
             xlabel='$x_1$', ylabel='$x_2$', zlabel='Error')
ax.plot_surface(x1, x2, error, rstride=1, cstride=1, cmap=cm.coolwarm,
                linewidth=0, antialiased=False)
plt.show()

# Compute partial Derivatives
x = np.array([[0.5], [0.5]])
order = [[1, 0, 2, 1, 0],
         [0, 1, 0, 1, 2]]

ff = f2fit(x, order)

print('x   = [0.5, 0.5]\nf1  = {:7.4f}\nf2  = {:7.4f}\nf11 = {:7.4f}\nf12 = {:7.4f}\nf22 = {:7.4f}'.format(*ff))
Exemplo n.º 9
0
__author__ = 'Randall'

from demos.setup import np, plt, demo
from compecon import DDPmodel
from compecon.tools import gridmake

## DEMDDP10 Stochastic cow replacement model

# Model Parameters
delta = 0.9  # discount factor
cost = 500  # replacement cost
price = 150  # milk price

# State Space
s1 = np.arange(10) + 1  # lactation states
s2 = np.array([0.8, 1.0, 1.2])  # productivity states
n1 = s1.size  # number of lactation states
n2 = s2.size  # number of productivity states
S1, S2 = gridmake(s1, s2)  # combined state grid
n = n1 * n2  # total number of states

# Action Space (keep='K', replace='R')
X = np.array(['Keep', 'Replace'])  # keep or replace
m = X.size  # number of actions

# Reward Function
f = np.empty((m, n))
y = (-0.2 * S1**2 + 2 * S1 + 8) * S2  # yield per lactation
f[0] = price * y
f[1] = f[0] - cost
f[0, S1 == 10] = -np.inf  # force replace at lactation 10
Exemplo n.º 10
0
from demos.setup import np, plt, demo
from compecon import DDPmodel
from compecon.tools import gridmake


## DEMDDP10 Stochastic cow replacement model

# Model Parameters
delta = 0.9                  # discount factor
cost = 500                  # replacement cost
price = 150                  # milk price

# State Space
s1 = np.arange(10) + 1     # lactation states
s2 = np.array([0.8, 1.0, 1.2])           # productivity states
n1 = s1.size              # number of lactation states
n2 = s2.size              # number of productivity states
S1, S2 = gridmake(s1,s2)    # combined state grid
n = n1 * n2                    # total number of states

# Action Space (keep='K', replace='R')
X = np.array(['Keep','Replace'])          # keep or replace
m = X.size                 	# number of actions

# Reward Function
f = np.empty((m, n))
y = (-0.2 * S1 ** 2 + 2 * S1 +8) * S2  # yield per lactation
f[0] = price * y
f[1] = f[0] - cost
f[0, S1 == 10] = -np.inf           # force replace at lactation 10
Exemplo n.º 11
0
__author__ = 'Randall'

from demos.setup import np, plt, demo
from compecon import DDPmodel
from compecon.tools import gridmake, getindex

# DEMDDP05 Water management model

# Model Parameters
alpha1 = 14  # producer benefit function parameter
beta1 = 0.8  # producer benefit function parameter
alpha2 = 10  # recreational user benefit function parameter
beta2 = 0.4  # recreational user benefit function parameter
maxcap = 30  # maximum dam capacity
r = np.array([0, 1, 2, 3, 4])  # rain levels
p = np.array([0.1, 0.2, 0.4, 0.2, 0.1])  # rain probabilities
delta = 0.9  # discount factor

# State Space
S = np.arange(1 + maxcap)  # vector of states
n = S.size  # number of states

# Action Space
X = np.arange(1 + maxcap)  # vector of actions
m = X.size  # number of actions

# Reward Function
f = np.full((m, n), -np.inf)
for k in range(m):
    f[k, k:] = alpha1 * X[k]**beta1 + alpha2 * (S[k:] - X[k])**beta2
Exemplo n.º 12
0
#     beta    transition function state coefficients
#     gamma   transition function action coefficients
#     omega   central banker's preference weights
#     sbar    equilibrium targets
#     delta   discount factor

# Preliminary tasks
from compecon import BasisChebyshev, DPmodel
from demos.setup import np, plt, demo
from compecon.quad import qnwnorm


## FORMULATION

# Model Parameters
alpha   = np.array([[0.9, -0.1]]).T             # transition function constant coefficients
beta    = np.array([[-0.5, 0.2], [0.3, -0.4]])  # transition function state coefficients
gamma   = np.array([[-0.1, 0.0]]).T             # transition function action coefficients
omega   = np.identity(2)                        # central banker's preference weights
sbar    = np.array([[1, 0]]).T                  # equilibrium targets
sigma   = 0.08 * np.identity(2),                # shock covariance matrix
delta   = 0.9                                   # discount factor


# Continuous State Shock Distribution
m   = [3, 3]                            # number of shocks
mu  = [0, 0]                            # means of shocks
[e,w] = qnwnorm(m,mu,sigma)            # shocks and probabilities

# Approximation Structure
n = 21                                 # number of collocation coordinates, per dimension
Exemplo n.º 13
0
def f(x):
    fval = [200 * x[0] * (x[1] - x[0] ** 2) + 1 - x[0], 100 * (x[0] ** 2 - x[1])]
    fjac = [[200 * (x[1] - x[0] ** 2) - 400 * x[0] ** 2 - 1, 200 * x[0]],
            [200 * x[0], -100]]
    return np.array(fval), np.array(fjac)
Exemplo n.º 14
0
def g(z):
    x, y = z
    return np.array([x **2 + y ** 3, x * y - 0.5])
Exemplo n.º 15
0
#     alpha   transition function constant coefficients
#     beta    transition function state coefficients
#     gamma   transition function action coefficients
#     omega   central banker's preference weights
#     sbar    equilibrium targets
#     delta   discount factor

# Preliminary tasks
from compecon import BasisChebyshev, DPmodel
from demos.setup import np, plt, demo
from compecon.quad import qnwnorm

## FORMULATION

# Model Parameters
alpha = np.array([[0.9, -0.1]]).T  # transition function constant coefficients
beta = np.array([[-0.5, 0.2],
                 [0.3, -0.4]])  # transition function state coefficients
gamma = np.array([[-0.1, 0.0]]).T  # transition function action coefficients
omega = np.identity(2)  # central banker's preference weights
sbar = np.array([[1, 0]]).T  # equilibrium targets
sigma = 0.08 * np.identity(2),  # shock covariance matrix
delta = 0.9  # discount factor

# Continuous State Shock Distribution
m = [3, 3]  # number of shocks
mu = [0, 0]  # means of shocks
[e, w] = qnwnorm(m, mu, sigma)  # shocks and probabilities

# Approximation Structure
n = 21  # number of collocation coordinates, per dimension