snowdrop.src.numeric.dp package

Submodules

snowdrop.src.numeric.dp.bellman module

Dynamic Programming.

Created on Mon Feb 15 16:48:37 2021 @author: A.Goumilevski

class snowdrop.src.numeric.dp.bellman.DynProg(functions, m_var, m_par, options, state_variables, control_variables, utilities, value_functions, eqs, be_eqs, lower_boundary, upper_boundary, ns=2, rho=0.8, shk_stddedv=0.3, Ngrid=1000, ngrid=100, n_order=20, R=None, Q=None)[source]

Bases: object

discrete()[source]

Solve Bellman equation by discrete dynamic programming method.

getBounds(m)[source]

Returns bound on variables.

Parameters:
mdict

Locals mapping.

Returns:

Array of 2-tuples.

parametric_value_iteration(n=None, debug=False)[source]

Solve the OGM by parametric value iteration.

Parameters:
nint, optional

Number of data points in the grid. The default is 20.

Returns:
iterationsint

Number of iterations.

critfloat

Convergence value.

ynumpy array

Solution of Bellman equation.

policy_iteration(debug=False)[source]

Solve the OGM by policy function iteration (Howard method).

Parameters:

Instance of tis class.

Returns:
iterationsint

Number of iterations.

critfloat

Convergence value.

ynumpy array

Solution of Bellman equation.

stochastic_policy_iteration(p=0.9, kmin=0.2, kmax=6, debug=False)[source]

Solve the stochastic OGM by policy iteration.

Parameters:
pfloat, optional

Probability value. The default is 0.9. It is used for discrete approximation of VAR(1) stochastic process.

kminfloat, optional

Lower bound on the grid. The default is 0.2

kmaxfloat, optional

Upper bound on the grid. The default is 6.

Returns:
iterationsint

Number of iterations.

critfloat

Convergence value.

ynumpy array

Solution of Bellman equation

stochastic_value_iteration(p=0.9, debug=False)[source]

Solve the stochastic OGM by value iteration.

Parameters:
pfloat, optional

Probability value. The default is 0.9. It is used for discrete approximation of VAR(1) stochastic process.

Returns:
iterationsint

Number of iterations.

critfloat

Convergence value.

ynumpy array

Solution of Bellman equation

transient_func(x, params)[source]

Compute function values.

Parameters:
xlist

Variables values.

paramslist

Parameters values.

Returns:

Function values.

updateVariables(d, j=None)[source]

Updates control variables.

Parameters:
ddict

Dictionary of variables and parameters names and values.

jint

Index of grid.

Returns:
numpy.array

Utility function.

utility_function(m)[source]

Evaluate utility function.

Parameters:
mdict

Dictionary of variables and parameters names and values.

Returns:
numpy array

Utility values.

value(expr, m)[source]
value_iteration(debug=False)[source]

Solve the OGM by value function iteration.

Parameters:

Instance of tis class.

Returns:
iterationsint

Number of iterations.

critfloat

Convergence value.

ynumpy array

Solution of Bellman equation

snowdrop.src.numeric.dp.bellman.Plot(path_to_dir, data, variable_names, var_labels={})[source]
snowdrop.src.numeric.dp.bellman.simulate(model)[source]

Solve Bellman equation.

Parameters:
modelModel object

Instance of model class.

Returns:

numpy.array.

snowdrop.src.numeric.dp.collard module

Created on Mon Feb 8 17:50:30 2021

Originally developed by Fabrice Collard.

Please see Dynamic Programming Notes #7 at: http://fabcol.free.fr/pdf/lectnotes7.pdf

Translated from Matlab code to Python by A.Goumilevski

snowdrop.src.numeric.dp.collard.Plot(path_to_dir, y, variable_names, var_labels={})[source]
snowdrop.src.numeric.dp.collard.parametric_value_iteration(n=10, nbk=20)[source]

Solve the OGM by parametric value iteration.

Parameters:
nint, optional

Order of polynomials. The default is 10.

nbkint, optional

Number of data points in the grid. The default is 20.

Returns:
cnumpy array

Consumption.

vnumpy array

Value function.

snowdrop.src.numeric.dp.collard.policy_iteration()[source]

Solve the OGM by policy function iteration (Howard method).

Returns:
cnumpy array

Consumption.

kpnumpy array

Capital.

unumpy array

Utility.

vnumpy array

Value function.

snowdrop.src.numeric.dp.collard.stochastic_constarined_policy_iteration(p=0.8, r=0.02, beta=0.95, gam=0.5, amin=0, amax=10)[source]

Solve and simulate the borrowing constraint problem (policy iteration).

Parameters:
pfloat, optional

Probability value. The default is 0.8. It is used for discrete approximation of VAR(1) stochastic process.

rfloat, optional

Interest rate. The default is 0.02.

betafloat, optional

Discount factor. The default is 0.95.

gamfloat, optional

Replacement ratio. The default is 0.5.

aminfloat, optional

Lower bound on the grid. The default is 0.

amaxfloat, optional

Upper bound on the grid. The default is 10.

Returns:
cnumpy array

Consumption.

knumpy array

Capital.

astnumpy array

Asset values.

vnumpy array

Value function.

snowdrop.src.numeric.dp.collard.stochastic_policy_iteration(p=0.9, kmin=0.2, kmax=6)[source]

Solve the stochastic OGM by policy iteration.

Parameters:
pfloat, optional

Probability value. The default is 0.9. It is used for discrete approximation of VAR(1) stochastic process.

kminfloat, optional

Lower bound on the grid. The default is 0.2

kmaxfloat, optional

Upper bound on the grid. The default is 6.

Returns:
cnumpy array

Consumption.

kpnumpy array

Capital.

vnumpy array

Value function.

snowdrop.src.numeric.dp.collard.stochastic_value_iteration(p=0.9, interpolate=False)[source]

Solve the stochastic OGM by value iteration.

Parameters:
pfloat, optional

Probability value. The default is 0.9. It is used for discrete approximation of VAR(1) stochastic process.

interpolatebool, optional

If True interpolate grid. The default is False.

Returns:
cnumpy array

Consumption.

unumpy array

Utility.

vnumpy array

Value function.

snowdrop.src.numeric.dp.collard.value_iteration(interpolate=False)[source]

Solve the OGM by value function iteration.

Parameters:
interpolatebool, optional

If True performs interpolation of value function to grid nodes. The default is False.

snowdrop.src.numeric.dp.discrete module

Discrete programming example: QuantEcon website: https://quantecon.org/quantecon-py/

Originally developed by Thomas J. Sargent and John Stachurski Please see lecture on Discrete State Dynamic Programming: https://python-advanced.quantecon.org/_downloads/pdf/discrete_dp.pdf

class snowdrop.src.numeric.dp.discrete.SimpleOG(B=10, M=5, α=0.5, β=0.9)[source]

Bases: object

populate_Q()[source]

Populate the Q matrix by setting

Q[s, a, s’] = 1 / (1 + B) if a <= s’ <= a + B

and zero otherwise.

populate_R()[source]

Populate the R matrix, with R[s, a] = -np.inf for infeasible state-action pairs.

u(c)[source]

snowdrop.src.numeric.dp.grids module

class snowdrop.src.numeric.dp.grids.CartesianGrid(min, max, n=[])[source]

Bases: Grid

CartesianGrid is a sub-class of Grid class.

type = 'cartesian'
class snowdrop.src.numeric.dp.grids.EmptyGrid[source]

Bases: Grid

EmptyGrid is a sub-class of Grid class.

n_nodes()[source]
node(i)[source]
nodes()[source]
type = 'empty'
class snowdrop.src.numeric.dp.grids.Grid[source]

Bases: object

Grid class is a parent class of EmptyGrid, PointGrid, UnstructuredGrid, CartesianGrid, NonUniformCartesianGrid, and SmolyakGrid sub-classes.

n_nodes()[source]
node(i)[source]
nodes()[source]
class snowdrop.src.numeric.dp.grids.NonUniformCartesianGrid(list_of_nodes)[source]

Bases: Grid

NonUniformCartesianGrid is a sub-class of Grid class.

type = 'NonUniformCartesian'
snowdrop.src.numeric.dp.grids.Plot(grid, title)[source]
class snowdrop.src.numeric.dp.grids.PointGrid(point)[source]

Bases: Grid

PointGrid is a sub-class of Grid class.

n_nodes()[source]
node(i)[source]
nodes()[source]
type = 'point'
class snowdrop.src.numeric.dp.grids.SmolyakGrid(min, max, mu=2)[source]

Bases: Grid

SmolyakGrid is a sub-class of Grid class.

type = 'Smolyak'
class snowdrop.src.numeric.dp.grids.UnstructuredGrid(nodes)[source]

Bases: Grid

UnstructuredGrid is a sub-class of Grid class.

type = 'unstructured'
snowdrop.src.numeric.dp.grids.cat_grids(grid_1, grid_2)[source]

Concatenate grids.

Parameters:
grid_1Grid

The first grid object.

grid_2Grid

The second grid object.

Raises:

Exception if grid is not Cartesian nor empty. is not Car

Returns:

Grid object.

snowdrop.src.numeric.dp.grids.mlinspace(a, b, orders, out=None)[source]

Return Cartesian grid.

snowdrop.src.numeric.dp.grids.n_nodes(grid)[source]
snowdrop.src.numeric.dp.grids.node(grid, i)[source]
snowdrop.src.numeric.dp.grids.nodes(grid)[source]
snowdrop.src.numeric.dp.grids.prod(l)[source]

snowdrop.src.numeric.dp.hjb module

snowdrop.src.numeric.dp.hjb.main()[source]

snowdrop.src.numeric.dp.olg module

Created on Sun Mar 21 08:40:21 2021

@author: A.Goumilevski

snowdrop.src.numeric.dp.olg.MSequilibrium(Labor, u)[source]
snowdrop.src.numeric.dp.olg.MSnext(Labor, Saving, u)[source]
snowdrop.src.numeric.dp.olg.Plot(path_to_dir, data, variable_names, var_labels={})[source]
snowdrop.src.numeric.dp.olg.Qtot(N, S)[source]
snowdrop.src.numeric.dp.olg.dQdL(N, S)[source]
snowdrop.src.numeric.dp.olg.dQdM(N, S, u)[source]
snowdrop.src.numeric.dp.olg.dQdS(N, S)[source]
snowdrop.src.numeric.dp.olg.value_iteration(Output=False)[source]

Solve Overlapping Generations Model by value function iteration.

Parameters:

Output: If True save results in excel file.

Returns:
iterationsint

Number of iterations.

critfloat

Convergence value.

ynumpy array

Solution of Bellman equation

snowdrop.src.numeric.dp.tauchen module

snowdrop.src.numeric.dp.tauchen.GaussHermite(n)[source]

Gauss Hermite nodes and weights following ‘Numerical Recipes for C’

snowdrop.src.numeric.dp.tauchen.GaussNorm(n, mu, s2)[source]

Find Gaussian nodes and weights for the normal distribution n = # nodes mu = mean s2 = variance

snowdrop.src.numeric.dp.tauchen.tauchen_hussey(N, mu, rho, sigma, baseSigma)[source]

Find a Markov chain whose sample paths approximate those of the AR(1) process

\[z_{t+1} = (1-rho)*mu + rho*z_{t} + eps_{t+1}\]

Here eps is the normal noise with stddev sigma

Format:

Z, Zprob = tauchen_hussey(N,mu,rho,sigma,m)

Input:

N: scalar, number of nodes for Z

mu: scalar, unconditional mean of process

rho: scalar

sigma: scalar, std. dev. of epsilons

baseSigma: scalar, std. dev. used to calculate Gaussian quadrature weights and nodes, i.e. to build the grid.

It is recommended to you use:
\[baseSigma = w*sigma +(1-w)*sigmaZ \]
where
\[sigmaZ = sigma/sqrt(1-rho^2), \]
and
\[w = 0.5 + rho/4\]

Tauchen & Hussey recommend baseSigma = sigma, and also mention baseSigma = sigmaZ.

Output:
Z: N*1 vector, nodes for Z

Zprob: N*N matrix, transition probabilities

Author:

Benjamin Tengelsen, Brigham Young University (python) Martin Floden, Stockholm School of Economics (original) January 2007 (updated August 2007)

This procedure is an implementation of Tauchen and Hussey’s algorithm, Econometrica (1991, Vol. 59(2), pp. 371-396)

snowdrop.src.numeric.dp.test module

Created on Mon Feb 8 17:50:30 2021

Originally developed by Fabrice Collard.

Please see Dynamic Programming Notes #7 at: http://fabcol.free.fr/pdf/lectnotes7.pdf

Translated from Matlab code to Python by A.Goumilevski

snowdrop.src.numeric.dp.test.Plot(header, titles, data, labels=[], save=True)[source]

Plot graphs.

Parameters:
headerstr.

Graphs header.

titlesstr,

Titles.

datanumpy array

Data.

labelslist, optional

Labels. The default is [].

saveTYPE, optional

If True saves plots in a directory. The default is True.

snowdrop.src.numeric.dp.test.parametric_value_iteration(n=10, nbk=20)[source]

Solve the OGM by parametric value iteration.

Parameters:
nint, optional

Order of polynomials. The default is 10.

nbkint, optional

Number of data points in the grid. The default is 20.

Returns:
cnumpy array

Consumption.

vnumpy array

Value function.

snowdrop.src.numeric.dp.test.policy_iteration()[source]

Solve the OGM by policy iteration.

Returns:
cnumpy array

Consumption.

kpnumpy array

Capital.

uTYPE

Utility.

vnumpy array

Value function.

snowdrop.src.numeric.dp.test.stochastic_constarined_policy_iteration(p=0.8, r=0.02, beta=0.95, gam=0.5, amin=0, amax=10)[source]

Solve and simulate the borrowing constraint problem (policy iteration).

Parameters:
pfloat, optional

Probability value. The default is 0.8. It is used for discrete approximation of VAR(1) stochastic process.

rfloat, optional

Interest rate. The default is 0.02.

betafloat, optional

Discount factor. The default is 0.95.

gamfloat, optional

Replacement ratio. The default is 0.5.

aminfloat, optional

Lower bound on the grid. The default is 0.

amaxfloat, optional

Upper bound on the grid. The default is 10.

Returns:
cnumpy array

Consumption.

knumpy array

Capital.

astnumpy array

Asset values.

vnumpy array

Value function.

snowdrop.src.numeric.dp.test.stochastic_policy_iteration(p=0.9, kmin=0.2, kmax=6)[source]

Solve the stochastic OGM by policy iteration.

Parameters:
pfloat, optional

Probability value. The default is 0.9. It is used for discrete approximation of VAR(1) stochastic process.

kminfloat, optional

Lower bound on the grid. The default is 0.2

kmaxfloat, optional

Upper bound on the grid. The default is 6.

Returns:
cnumpy array

Consumption.

kpnumpy array

Capital.

vnumpy array

Value function.

snowdrop.src.numeric.dp.test.stochastic_value_iteration(p=0.9, interpolate=False)[source]

Solve the stochastic OGM by value iteration.

Parameters:
pfloat, optional

Probability value. The default is 0.9. It is used for discrete approximation of VAR(1) stochastic process.

interpolatebool, optional

If True interpolate grid. The default is False.

Returns:
cnumpy array

Consumption.

unumpy array

Utility.

vnumpy array

Value function.

snowdrop.src.numeric.dp.test.value_iteration(interpolate=False)[source]

Solve the OGM by value function iteration.

Parameters:
interpolatebool, optional

If True performs interpolation of value function to grid nodes. The default is False.

snowdrop.src.numeric.dp.util module

Created on Mon Feb 8 15:30:10 2021

Originally developed by Fabrice Collard.

Please see Dynamic Programming Notes at: http://fabcol.free.fr/pdf/lectnotes7.pdf

Translated from Matlab code to Python by A. Goumilevski

snowdrop.src.numeric.dp.util.Chebychev_Polinomial(x, n)[source]

Compute Chebychev polinomials.

Parameters:
xnumpy array.

Nodes.

nint.

Degree of polinomial.

Returns:
Txnumpy array.

Chebyshev Polinomial of order n.

snowdrop.src.numeric.dp.util.gauss_herm(n)[source]

Compute the coefficients of Hermite polynomials using the recursion:

\[H_{n+1} = 2*H_{n} - 2*n*H_{n-1}\]
Parameters:
nint

Number of nodes.

Returns:
xnumpy array

Gauss Hermite nodes

wnumpy array

Gauss Hermite weights

snowdrop.src.numeric.dp.util.markov(PI, s, n, s0=1, seed=1)[source]

Simulate a Markov chain.

Parameters:
PI: numpy array

Transition matrix of Markov states.

slist

State vector.

nint

Length of simulation.

s0: float, optional

Initial state (index)

seedint, optional

Random seed. The default is 1.

Returns:
chainlist

Values for the simulated Markov chain

statenumpy array

Index of the state

snowdrop.src.numeric.dp.util.scalar_tv(kp, alpha, beta, delta, sigma, kmin, kmax, n, k, theta)[source]

Compute norm of a function.

snowdrop.src.numeric.dp.util.tausc_hussey(n=4, mx=0, rx=0.9, sigma=0.01)[source]

Implement Tauchen-Hussey algorithm.

It asymptotically reproduces AR(1) process

\[Z_{t+1} = (1-rx)*mx + rx*Z_{t} + eps_{t+1}\]

Here eps is a random process with normal distribution with standard deviation, sigma.

Parameters:
nint, optional

Number of nodes. The default is 4.

mxint, optional

Unconditional mean of process. The default is 0.

rxfloat, optional

Persistence coefficient. The default is 0.9.

sigmafloat, optional

Standard deviation. The default is 0.01.

Returns:
pnumpy array

Transition probabilities.

snowdrop.src.numeric.dp.util.tv(kp, alpha, beta, delta, sigma, kmin, kmax, n, k, theta)[source]

Compute value function.

Parameters:
kpfloat

Grid nodes.

Returns:
resfloat

Minus value function.

Module contents