This is a work-in-progress (*updated: 2019-07-27*) website for solving several infinite-horizon exogenously-incomplete dynamic assets models in discrete-time. Section **(1)** solves the savings problem. Section **(2)** solves the borrowing and savings problem. Section **(3)** solve the risky and safe asset problem (risky entrepreneur). Section **(4)** solves the same problem as (3) with percentage choice grids and interpolation. Section **(5) and (6)** is an application/extension of the earlier models: models from (1) through (4) are augmented following *A Choice Amongst Many: Household Borrowing in a Setting with Multiple Providers* (**Robert M. Townsend** and **Fan Wang** 2019).

Key elements for solution algorithms from (1) through (5) are described in **Wang (2019)**. Generally, looped, vectorized, and optimized-vectorized implementations of the same solution algorithm with tabular, graphical and profiling results are shown. Separate subsections show files that solve policy functions and derive asset distributions. Looped codes are shown for clarity, vectorized codes are shown for speed. Codes are designed to not require special hardware or explicit parallelization. Codes are tested on Windows 10 with Matlab 2019a for replicability. The algorithms here are platform agnostic and could be implemented in alternative languages, but speed variations across languages is generally small for the algorithms described here. Please contact FanWangEcon for problems.

Functions are written with default parameters and are directly callable. See **Set-Up Instructions** for instructions and folder descriptions. On this webpage, there are three types of files:

**m**: matlab m file**publish html**: html files generated by matlab publish**profile**: html files generated by profiling the m file with timing results

For an introduction to savings and borrowing problems, see Fan’s Intro Math for Econ.

# 1. The Savings Problem (AZ)

Files for this section are in the /m_az/ folder in the CodeDynaAsset repository. links: (1) savings problem (2) borrowing and savings problem (3) risky and safe asset problem (risky entrepreneur) (4) percentage choice grids and interpolation (5) Townsend and Wang 2019 Single Asset (6) Townsend and Wang 2019 Safe + Risky Asset.

This section solves the standard household-side problem of the Bewley/Aiyagari type heterogeneous agent model. The supply of credit in Aiygari (1994) can be derived by invoking *1.1* and *1.2* with different interest rates.

## 1.1 Dynamic Programming Files (AZ)

Price (wage and interest rate) taking households arrive in a period with accumulated savings and a possibly persistent income shock. Households decide on optimal savings to carry forward to the next period in order to smooth consumption given rational expectation of the income process. Parameters can be adjusted here, for the benchmark simulation:

- savings only with minimum income
**750**grid points for asset states/choices**15**grid points for the AR1 shock- other parameters

Algorithms below provide identical solutions:

*az*model looped solution:**m**|**publish html**|**profile**- speed:
**8634.5**seconds, small grid demonstration in HTML - loops: 1 for VFI, 1 for shocks, 1 for asset state, 1 for asset choice, 1 for future shocks

- speed:
*az*model vectorized solution:**m**|**publish html**|**profile**- speed:
**32.3**seconds - loops: 1 for VFI, 1 for shocks, vectorize remaining 3 loops

- speed:
*az*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**1.3**seconds - loops: 1 for VFI, 1 for shocks, vectorize remaining 3 loops, reuse u(c)
- reuse u(c) in cells, speed improvements described here

- speed:

## 1.2 Asset Distributions (AZ)

### 1.2.a Deriving Asset Distributions

Solve for the stationary probability mass function over states using *non-simulation* methods. The first two methods below involve *iteration*, the third is *semi-analytical and iteration-free* (see appendix in Wang 2019 for details). The semi-analytical approach has three implementations, **eigenvector** or **projection** or **nth-power**. The **eigenvector** approach, when implemented with sparse matrix, is potentially faster (but not always). The codes compute, with benchmark parameters and after invoking the *optimized-vectorized* code from above, for each outcome *y*:

- joint and marginal probability mass functions:
*P(y,z)*,*P(y,a)*and*P(y)* *E(Y)*,*var(Y)*,*P(Y=0)*,*P(Y=max(Y))*, etc- percentiles and fraction of
*y*held by households up to percentiles

Algorithms 1 and 2 approximate 3 (speed does not include *1.1* speed), the vectorized and semi-analytical programs work for the risky and safe asset problems in section *3.1 and 3.2* as well:

*az*asset distribution looped:**m**|**publish html**|**profile**- speed:
**5.0**seconds - loops: 1 for pmf iteration, 1 for shocks, 1 for asset state, 1 for future shocks

- speed:
*az+akz+wkz*asset distribution vectorized:**m**|**publish html**|**profile**- speed:
**0.8**seconds - loops: 1 for pmf iteration, 1 for shocks, policy index match

- speed:
*az+akz+wkz*asset distribution semi-analytical:**m**|**publish html**|**profile**- speed:
**0.2**seconds**sparse eigenvector approach** - iteration free, construct full-states markov, sparse matrix, eigenvector/projection/Nth-power

- speed:

### 1.2.b Statistics Support Functions

Deriving asset distributions relies on two functions from the /tools/ folder for computing pmf for choice/outcome as well as distributional statistics based on pmf which apply to all models in later sections as well:

- all model f(y) from f(a,z) and y(a,z):
**m**|**publish html**- from pmf
*f(a,z)*and outcome/policy*y(a,z)*, derive*f(y,z)*,*f(y,a)*and*f(y)*

- from pmf
- all model covariance and correlations:
**m**|**publish html**- given
*E(y1), E(y2)*,*sd(y1), sd(y2)*,*f(a,z)*,*y1(a,z), y2(a,z)*, compute*cor(y1,y2)*

- given
- all model distributional statistics:
**m**|**publish html**- table: mean, sd, percentiles, fraction of outcome/asset held by household up to x percentile

- all model store statistics to container:
**m**|**publish html**- This is a manager file

## 1.3 Solution Support (AZ)

**Parameters and Function Definitions**:

*az*model set default parameters:**m**|**publish html**- param_map: container map for carrying parameters across functions
- support_map: container map for carrying programming instructions etc across functions

*az*model set functions:**m**|**publish html**- functions: centrally define functions as function handles

*az*model generate states, choices, and shocks grids:**m**|**publish html**- func_map: container map containing all function handles
- armt_map: container map containing matrixes for states and choices.

**Output Analysis**:

*az*model solution results processing:**m**|**publish html**- table: value and policy function by states and shocks
- table: iteration convergence and percentage policy function change by shock
- graph: value + policy functions with levels, logged levels and percentages
- mat: store all workspace matrixes, arrays, scalar values to matrixes

*az*model solution results graphing:**m**|**publish html**- graph: value function by asset and shock
- graph: consumption and asset choice levels
- graph: consumption and asset logged levels
- graph: consumption and asset as percentages of coh and assets

## 1.4 Parameter Testing (AZ)

Test model outcomes as parameters given parameter adjustments.

*az*model**value and policy**functions precision*az*model overall speed test for**asset distribution**simulation- 1.9 seconds for benchmark simulation:
**m**|**publish html**|**profile**

- 1.9 seconds for benchmark simulation:
*az*preference parameters and**asset distributions***az*model shock process and**asset distributions**

## 1.5 Simulations (AZ)

Simulate and graph effects of changing parameters on outcomes. Compare against simulation in *2.5* for the effects of allowing for borrowing on distributional outcomes. And compare against simulations in *3.7* as well as *4.8* for the effects of including a risky capital investment on distributional outcomes.

**Simulation Functions**:

*az*model simulate model:**m**|**publish html***cross*vs*full grid*vs*random*simulation- outputs to table where columns are statistics, rows are variables, groups of rows are different simulations

*az*model graph simulation results:**m**|**publish html**- graph various aggregate and distributional outcomes

**Simulation Results**:

*az*the effects**preference**parameters on distributional outcomes- cross test, adjust discount and risk aversion:
**m**|**publish html** - grid test, adjust discount and risk aversion:
**m**|**publish html**

- cross test, adjust discount and risk aversion:
*az*the effects**shock**parameters on distributional outcomes- cross test, adjust shock persistence and variance:
**m**|**publish html** - grid test, adjust shock persistence and variance:
**m**|**publish html**

- cross test, adjust shock persistence and variance:
*az*the effects**prices**on distributional outcomes- cross test, adjust wage and savings rate:
**m**|**publish html**

- cross test, adjust wage and savings rate:
*az*the effects**preference + shocks + prices**on distributional outcomes- random test, adjust preference shocks prices:
**m**|**publish html**

- random test, adjust preference shocks prices:

# 2. The Savings + Borrowing Problem (ABZ)

Files for this section are in the /m_abz/ folder in the CodeDynaAsset repository. links: (1) savings problem (2) borrowing and savings problem (3) risky and safe asset problem (risky entrepreneur) (4) percentage choice grids and interpolation (5) Townsend and Wang 2019 Single Asset (6) Townsend and Wang 2019 Safe + Risky Asset.

Codes from *1.1-1.3* are adjusted slightly to deal with both savings as well as borrowing with default. Additionally, there is an additional shock for interest rate.

## 2.1 Dynamic Programming Files (ABZ)

Parameters can be adjusted here, for the benchmark simulation:

- savings and borrowing (with default) with minimum income
**750**grid points for asset states/choices**55**grid points for shocks:**11**wage,**5**borrow interest- other parameters

Using three algorithm that provide identical solutions:

*abz*model looped solution:**m**|**publish html**|**profile**- small grid demonstration

*abz*model vectorized solution:**m**|**publish html**|**profile**- speed:
**281.0**seconds

- speed:
*abz*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**14.0**seconds

- speed:

## 2.2 Asset Distributions (ABZ)

This is the wrapper file for *abz* that invokes the looped, vectorized, and semi-analytical distributional programs from *1.2*:

*abz*distributional wrapper:**m**|**publish html**|**profile**- speed (dynamic programming + distribution):
**17.5**seconds

- speed (dynamic programming + distribution):

## 2.3 Solution Support (ABZ)

Add parameters *bl_default* and *fl_default_aprime*, which control if default is allowed and next period asset level for defaulters.

**Parameters and Function Definitions**:

*abz*model set default parameters:**m**|**publish html***abz*model set functions:**m**|**publish html***abz*model borrowing and savings grid:**m**|**publish html***abz*model generate states, choices, and shocks grids:**m**|**publish html**

**Output Analysis**: shared files with *az*.

## 2.4 Parameter Testing (ABZ)

We solved the exogenously incomplete borrowing and savings problem in *1.4*. Now we analyze model features by adjusting parameters.

**Policy Function Testing**

*abz*borrowing and savings choice grid with default*abz*model solution precision*abz*model compare saving vs borrowing with and without default

**Asset Distribution Testing**

*abz*model overall speed test- 11.3 seconds for benchmark simulation:
**m**|**publish html**|**profile**

- 11.3 seconds for benchmark simulation:
*abz*borrowing interest rates, bounds, and minimum c*abz*preference parameters*abz*model shock process

## 2.5 Simulations (ABZ)

Simulate and graph effects of changing parameters on outcomes. Compare against simulation in *1.5* for the effects of allowing for borrowing on distributional outcomes. And compare against simulations in *3.7* as well as *4.8* for the effects of including a risky capital investment on distributional outcomes.

**Simulation Functions**:

Share functions with *az* from *1.5* above, model (1) simulate model (2) graph simulation results

**Simulation Results**:

*abz*the effects**borrowing**parameters on distributional outcomes- cross test,
**no default**, adjust borrow rate, bounds, etc:**m**|**publish html** - grid test,
**no default**, adjust borrow rate, bounds, etc:**m**|**publish html** - cross test,
**default**, adjust borrow rate, bounds, etc:**m**|**publish html** - grid test,
**default**, adjust borrow rate, bounds, etc:**m**|**publish html**

- cross test,
*abz*the effects**preference**parameters on distributional outcomes- cross test,
**no default**, adjust discount and risk aversion:**m**|**publish html** - cross test,
**default**, adjust discount and risk aversion:**m**|**publish html**

- cross test,
*abz*the effects**shock**parameters on distributional outcomes- cross test,
**no default**, adjust shock persistence and variance:**m**|**publish html** - cross test,
**default**, adjust shock persistence and variance:**m**|**publish html**

- cross test,
*abz*the effects**min inc and save r**on outcomes- cross test,
**no default**, min income and savings r:**m**|**publish html** - cross test,
**default**, min income and savings r:**m**|**publish html**

- cross test,
*abz*the effects**preference + shocks + prices**on distributional outcomes- random test,
**default**, adjust preference shocks prices:**m**|**publish html**

- random test,

# 3. The Risky + Safe Asset Problem (Part 1)

Files for this section are in the /m_akz/ folder in the CodeDynaAsset repository. links: (1) savings problem (2) borrowing and savings problem (3) risky and safe asset problem (risky entrepreneur) (4) percentage choice grids and interpolation (5) Townsend and Wang 2019 Single Asset (6) Townsend and Wang 2019 Safe + Risky Asset.

Two endogenous assets, one safe one risky. Risky asset could be stocks with constant return to scale, or physical capital investment with depreciation and decreasing return to scale (risky entrepreneur). The risky asset choice is made ex-ante the realization of the shock for that risky asset, hence risky. This contrasts with a firm’s within period optimal physical capital choice problem, which is made after the realization of shocks. Note that the utility function is CRRA, however, households do not have constant share of risky investment for any wealth (cash-on-hand) levels when risky asset has decreasing return to scale and/or shock is persistent and/or there is minimum income.

There are more analytical ways of solving the basic version of this problem. Here we stick to using this grid based solution algorithm which allows for flexibly solving non-differentiable and non-continuous problems. The grid based solution algorithm now with 2 endogenous choices and states requires exponentially more computation time than the *az* model. Here I provide three sets of solution algorithms at increasing speeds:

- In
**3.1**, solve the problem with the two asset choice concurrently - In
**3.2**, solve the problem in two stages - In
**3.3**, two stage solution with interpolation

## 3.1 Concurrent Dynamic Programming (AKZ)

Files for this section have theakzin file names and are in the /m_akz/ folder in the CodeDynaAsset repository.

The *akz* problem. Parameters can be adjusted here, for the benchmark simulation:

- savings problem with alternative safe and risky assets and minimum income
**50**aggregate savings grid points,**50**risky investment grid points,**1274**valid choice combinations in upper triangle. Note that the benchmark parameters for the*az*model has*750*grid points of choices/states, the benchmark problem here is larger and hence takes more time.**15**grid points for the AR1 shock- other parameters

*akz*model looped solution:**m**|**publish html**|**profile**- speed:
**32764.7**seconds - loops: 1 for VFI, 1 for shocks, 1 for coh(b,k,z), 1 for (b’,k’) choices, 1 for future shocks

- speed:
*akz*model vectorized solution:**m**|**publish html**|**profile**- speed:
**93.2**seconds - loops: 1 for VFI, 1 for shocks, vectorize remaining

- speed:
*akz*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**2.7**seconds - loops: 1 for VFI, 1 for shocks, vectorize remaining
- reuse u(c) in cells, speed improvements described here

- speed:

## 3.2 Two-Stage Dynamic Programming (WKZ)

Files for this section have thewkzin file names and are in the /m_akz/ folder in the CodeDynaAsset repository.

The *wkz* problem, w=k’+b’. Takes significantly less time than *3.1*, produces identical results. Rather than solving the two asset problem in one shot. We can separate the problem into two stages. In the second stage, we find the optimal k’ choice given w=k’+b’: max_{k’}(E(V(coh(k’,b’=w-k’),z’)) given w and z. The second stage optimal risky asset allocation problem is not a function of cash-on-hand in the current period conditional on the aggregate savings choice w=k’+b’. In the first stage, households optimized only over aggregate savings. This significantly reduces the dimensionality of the problem.

Parameters can be adjusted here, for the benchmark simulation, same grid sizes as *3.1*.

*wkz*model 2nd stage solution:**m**|**publish html**- solving for k(w,z) = argmax_{k’}(E(V(coh(k’,b’=w-k’),z’)) given z and w.

*wkz*model looped solution:**m**|**publish html**|**profile**- speed:
**581.0**seconds - loops: 1 for VFI, 1 for shocks, 1 for coh(b,k,z), 1 for w(z)=k’+b’

- speed:
*wkz*model vectorized solution:**m**|**publish html**|**profile**- speed:
**5.6**seconds - loops: 1 for VFI, 1 for shocks, vectorize remaining

- speed:
*wkz*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**0.9**seconds - Step One solve k(w,z); Step Two solve w(z,coh(b,k,z)) given k(w,z)
- loops: 1 for VFI, 1 for shocks, vectorize remaining
- store u(c) in cells, update when k*(w,z) changes, speed improvements described here

- speed:

## 3.3 Two-Stage DP with Interpolation (iWKZ)

Files for this section have theiwkzin file names and are in the /m_akz/ folder in the CodeDynaAsset repository.

**Algorithm Description**

The *iwkz* problem, interpolated version of 2.2. Takes significantly less time than *3.2* at larger choice grids, produces approximately identical results as *3.2*. Simulations below are at the same grid points as *3.2* and *3.1* for comparison, but at these low accuracy grid points, *iwkz* is not necessarily faster than *3.2*.

The reason that *iWKZ* is faster then *3.2* is that when we increase aggregate savings grid points, we do not need to reduce the two interpolation grid gaps. COH is the endogenous state, and its size is determined by the *cash-on-hand interpolation grid gap*. The choice grid increases in size as we increase aggregate savings grid points, but since we are solving in two stages, the dimensionality of the problem increases proportionally (but not exponentially). The total number of safe and risky asset choice is determined by assuming equi-distance grid gap for aggregate savings as well as both risky and safe investments. Speed up achieved via interpolation as described here.

In section *3.6* below, I show how solution results change for *iwkz* as we increase aggregate savings grid points, shock grid points, reduce the interpolation gaps, or change production function parameters from decreasing to constant return to scale.

**Algorithm Implementation**

Parameters can be adjusted here, for the benchmark simulation, same grid size parameters as *3.1* and *3.2*, but we introduce two additional measures of precision:

**0.0001**consumption interpolation grid gap**0.1**value function cash-on-hand interpolation grid gap- other parameters

- Use same
*wkz*2nd stage solution as in*3.2* *iwkz*model looped solution:**m**|**publish html**|**profile**- speed:
**577.8**seconds - loops: 1 for VFI, 1 for shocks, 1 for coh(b,k,z), 1 for w(z)=k’+b’

- speed:
*iwkz*model vectorized solution:**m**|**publish html**|**profile**- speed:
**2.3**seconds - loops: 1 for VFI, 1 for shocks, vectorize remaining

- speed:
*iwkz*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**0.8**seconds - Step One solve k(w,z); Step Two solve w(z,coh(b,k,z)) given k(w,z)
- loops: 1 for VFI, 1 for shocks, vectorize remaining
- interpolate u(c), interpolate v(coh,z)
- store u(c) in cells, update when k*(w,z) changes

- speed:

## 3.4 Asset Distributions

### 3.4.a Distributions for AKZ and WKZ models

Solve for the stationary probability mass function over states using *non-simulation* methods. The algorithms in this section works for situations where (1) state-space grid is identical to the choice-space grid, and (2) the endogenous element of the state-space are last period choices. The second point means that we are interested in f(y,z), where y’(y,z), but not y’(y,z,z’).

The looped program is new but very similar to the looped code from *1.2.a* earlier. The vectorized and semi-analytical programs invoke the corresponding programs from *1.2.a*. The codes compute, with benchmark parameters and after invoking the *optimized-vecotrized* dynamic programming code from above for *akz* or *wkz*, for each outcome y various distributional statistics of interest, including: *P(y,z)*, *P(y)*, etc.

Algorithms 1 and 2 approximate 3 (speed does not include 3.1 and 3.2 speed):

*akz+wkz*asset distribution looped:**m**|**publish html**|**profile**- speed:
**18.8**seconds

- speed:
*akz+wkz*asset distribution vectorized:**m**|**publish html**|**profile**- speed:
**1.9**seconds

- speed:
*akz+wkz*asset distribution semi-analytical:**m**|**publish html**|**profile**- speed:
**0.9**seconds**sparse eigenvector approach**

- speed:

### 3.4.b Distributions for iWKZ model

Solve for the stationary probability mass function over states using *non-simulation* methods. The algorithms in this section works for *iwkz* from *3.3*. For this problem, we are interested in f(y,z), where y’(y,z,z’). Unlike for *3.4.a*, here, *z’* also matters.

The programs here are significantly different from programs from *1.2* and *3.4.a* above. The codes compute, with benchmark parameters and after invoking the *optimized-vecotrized* dynamic programming code from above for *iwkz*, for each outcome y various distributional statistics of interest, including: *P(y,z)*, *P(y)*, etc.

Algorithms 1 and 2 approximate 3 (speed does not include 3.3 speed):

*iwkz*asset distribution looped:**m**|**publish html**|**profile**- speed:
**104.4**seconds

- speed:
*iwkz*asset distribution vectorized:**m**|**publish html**|**profile**- speed:
**0.8**seconds

- speed:
*iwkz*asset distribution semi-analytical:**m**|**publish html**|**profile**- speed:
**0.4**seconds**sparse eigenvector approach**

- speed:

## 3.5 Solution Support

All solution algorithms share the same support files.

**Parameters and Function Definitions**:

*akz+wkz+iwkz*model set default parameters:**m**|**publish html***akz+wkz+iwkz*model set functions:**m**|**publish html***akz+wkz+iwkz*model generate states, choices, and shocks grids:**m**|**publish html**

**Output Analysis**:

*akz+wkz+iwkz*model solution results processing:**m**|**publish html***akz+wkz+iwkz*model solution results graphing:**m**|**publish html**

## 3.6 Parameter Testing (iWKZ)

We solve the joint asset choice problem using the *optimized-vectorized* method for *iwkz* algorithm from *3.3*. Now we analyze model features by adjusting parameters.

*iwkz*model solution precision*iwkz*model shift minimum income*iwkz*model shift shock persistence

## 3.7 Simulations (iWKZ)

Simulate and graph effects of changing parameters on outcomes. The results here should be compared against results in section *4.8*. In *4.8*, by adopting percentage grid rather than level grid, there is a significant improvement in the precision of simulations for the same number of grid points. Results in *4.8* and *3.7* should be compared about simulations from *az* and *abz* models in *1.5* and *2.5*.

**Simulation Functions**:

Share functions with *az* from *1.5* above, model (1) simulate model (2) graph simulation results

**Simulation Results**:

*iwkz*the effects*preference parameters*on distributional outcomes- sample simulate, adjust discount and risk aversion:
**m**|**publish html** - cross test, adjust discount and risk aversion:
**m**|**publish html**

- sample simulate, adjust discount and risk aversion:
*iwkz*the effects production function*shock parameters*on distributional outcomes- sample simulate, adjust shock persistence and variance:
**m**|**publish html** - cross test, adjust shock persistence and variance:
**m**|**publish html**

- sample simulate, adjust shock persistence and variance:
*iwkz*the effects production function*elasticity and depreciation parameters*on distributional outcomes- sample simulate, adjust elasticity and depreciation:
**m**|**publish html** - cross test, adjust elasticity and depreciation:
**m**|**publish html**

- sample simulate, adjust elasticity and depreciation:
*iwkz*the effects*price parameters*on distributional outcomes- cross test, adjust min income and savings rate:
**m**|**publish html**

- cross test, adjust min income and savings rate:
*iwkz*the effects*preference + shocks + prices*on distributional outcomes- cross test, adjust preference shocks prices:
**m**|**publish html**

- cross test, adjust preference shocks prices:

# 4. The Risky + Safe Asset Problem (Part 2)

Files for this section are in the /m_ipwkz/ folder in the CodeDynaAsset repository. links: (1) savings problem (2) borrowing and savings problem (3) risky and safe asset problem (risky entrepreneur) (4) percentage choice grids and interpolation (5) Townsend and Wang 2019 Single Asset (6) Townsend and Wang 2019 Safe + Risky Asset.

In Section 3, we solved the two asset problem in levels. Here, the same model is solved, but using an alternative algorithm. Now the model is solved in percentages. Households choose the percentage of cash-on-hand to allocate to aggregate savings, and then given that the percentage of total savings to allocate to safe vs risky asset.

### 4.1 Two Stage Percentage Interpolation Algorithm Description

In the levels solution above, households at different state space points face the same choice set, but almost half of the choice points are invalid because they would lead to negative consumption today. Solving the problem in percentages allow all households to have the same choice grid in terms of percentage levels. This potentially offers much more precise solutions under some model parameters combinations. One could compare testing results from *3.6* above and *4.4* below. Additionally, the levels based grid solution might lead to degenerate steady state asset distributions. This is because at low levels of cash-on-hand, if the risky asset’s lowest level of choice is invalid, then an individual could be stuck in that state. (see appendix in Wang 2019 for details)

This is the *ipWKZ* problem: *i* stands for interpolation, *p* stands for percentage, *wk* stand for two stage w=k’+b’ first then k’ given w. For the *ipWKZ* model, while there is still very substantial gains from the looped to the vectorized algorithm implementation, the speed improvements from moving to the efficient-vectorized implementation is much less substantial. For the small benchmark grids, the speed is actually slower when we use the efficient-vectorized implementation.

We have three sets of interpolations now:

**interpolate 1**: As before, we interpolate over u(c).**interpolate 2**: As before, interpolation of the value function over cash-on-hand and shocks is needed because the solution grid for cash-on-hand in the first stage is not the same as the reachable cash-on-hand levels given percentage second stage choices.**interpolate 3**: In addition to before, interpolation of the optimal 2nd stage risky investment choice over aggregate savings and shocks is needed because the 2nd stage solution grid for aggregate-savings in the second stage is the not the same as the percentage based first stage aggregate-savings choices. The second interpolation makes the efficiency gain from reusing u(c) through cells when k*(w,z) does not change much less substantial.

In section 3.4 below, I show how solution results change for *ipwkz* as we increase aggregate savings grid points, shock grid points, reduce the interpolation gaps, or change production function parameters from decreasing to constant return to scale.

## 4.2 Perc. Interp. Savings DP (IPWKZ)

Parameters can be adjusted here, for the benchmark simulation, same as *3.3*, but we introduce several additional measures of precision:

- savings problem with alternative safe and risky assets
**50**aggregate savings*percentage*grid points,**50**risky investment*percentage*grid points,**2500**valid choice combinations in triangular choice matrix**15**grid points for the AR1 shock- interpolate 1:
**0.0001**consumption interpolation grid gap - interpolate 2:
**0.1**value function cash-on-hand interpolation grid gap - interpolate 3:
**0.1**expected value function aggregate savings interpolation grid gap - other parameters

- Use again
*ipwkz*2nd stage solution:**m**|**publish html**- solving for kperc(w,z) = argmax_{kperc’}(E(V(coh(kperc’,b’=w-w*kperc’),z’)) given z and w.

*ipwkz*model loop:**m**|**publish html**|**profile**- small grid demonstration

*ipwkz*model vectorized:**m**|**publish html**|**profile**- speed:
**4.2**seconds

- speed:
*ipwkz*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**1.6**seconds - Step One solve kperc(w,z); Step Two solve wperc(z,coh(b,k,z)) given kperc(w,z)
- loops: 1 for VFI, 1 for shocks, vectorize remaining
- interpolate u(c), interpolate v(coh,z), interpolate EV(w,z)
- store u(c) in cells, update when k*(w,z) changes

- speed:

## 4.3 Perc. Interp. Save + Borrow DP (IPWKBZ)

The code from *4.1* are modified to allow for default. Set up for default is the same as in the *abz* model discussed in section *2*. Parameters can be adjusted here, for the benchmark simulation, same parameters as *4.1*.

- Second Stage with borrowing
*ipwkbz*2nd stage solution:**m**|**publish html** *ipwkbz*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**2.5**seconds

- speed:

## 4.4 Perc. Interp. Borrow R Shock DP (IPWKBZR)

The code from *4.2* are modified to allow for interest rate shock for borrowing. Set up for default and interest rate shock is the same as in the *abz* model discussed in section *2*. Parameters can be adjusted here, for the benchmark simulation, same parameters as *4.1*.

**55**grid points for shocks:**11**wage,**5**borrow interest

- Second Stage with borrowing
*ipwkbzr*2nd stage solution:**m**|**publish html** *ipwkbzr*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**19.6**seconds

- speed:

## 4.5 Asset Distributions

This is the wrapper file for *abz* that invokes the looped, vectorized, and semi-analytical distributional programs from *3.4.b*:

*ipWKZ*distributional wrapper:**m**|**publish html**|**profile**- speed (dynamic programming + distribution):
**2.5**seconds

- speed (dynamic programming + distribution):
*ipWKBZ*distributional wrapper:**m**|**publish html**|**profile**- speed (dynamic programming + distribution):
**4.2**seconds

- speed (dynamic programming + distribution):
*ipWKBZR*distributional wrapper:**m**|**publish html**|**profile**- speed (dynamic programming + distribution):
**51.1**seconds

- speed (dynamic programming + distribution):

## 4.6 Solution Support (ipWKZ + ipWKBZ + ipWKBZR)

All solution algorithms share the same support files.

**Parameters and Function Definitions (ipwkz)**:

*ipwkz*model set default parameters:**m**|**publish html***ipwkz*model set functions:**m**|**publish html***ipwkz*model generate states, choices, and shocks grids:**m**|**publish html**

**Parameters and Function Definitions (ipwkbz)**:

*ipwkbz*model set default parameters:**m**|**publish html***ipwkbz*model set functions:**m**|**publish html***ipwkbz*model generate states, choices, and shocks grids:**m**|**publish html**

**Parameters and Function Definitions (ipwkbzr)**:

*ipwkbzr*model set default parameters:**m**|**publish html***ipwkbzr*model set functions:**m**|**publish html***ipwkbzr*model generate states, choices, and shocks grids:**m**|**publish html**

**Output Analysis**:

*ipwkz+ipwkbz+ipwkbzr*shares with*akz+wkz+iwkz*models solution results processing codes.*ipwkz+ipwkbz+ipwkbzr*shares with*akz+wkz+iwkz*models solution results graphing codes.

## 4.7 Parameter Testing (ipWKZ + ipWKBZ)

**Savings Testing (ipWKZ)**

We solve the joint asset choice problem using the *optimized-vectorized* method for *ipwkz* algorithm from *3.3*. Now we analyze model features by adjusting parameters.

*ipwkz*2nd stage optimal k given w percentage vs level*ipwkz*model solution precision*ipwkz*model shift minimum income*ipwkz*model shift shock persistence

**Savings + Borrowing Testing (ipWKBZ)**

We solve the joint asset choice problem using the *optimized-vectorized* method for *ipwkbz* algorithm from *3.3*. Now we analyze model features by adjusting parameters.

*ipwkbz*borrowing and savings grid*ipwkbz*model solution precision*ipwkbz*model shift minimum income*ipwkbz*model shift shock persistence

## 4.8 Simulations (ipWKZ + ipWKBZR)

Simulate and graph effects of changing parameters on outcomes. The results here should be compared against results in section *3.7*. In *4.8*, by adopting percentage grid rather than level grid, there is a significant improvement in the precision of simulations for the same number of grid points. Results in *4.8* and *3.7* should be compared about simulations from *az* and *abz* models in *1.5* and *2.5*.

**Simulation Functions**:

Share functions with *az* from *1.5* above, model (1) simulate model (2) graph simulation results

**Savings Simulation Results (ipWKZ)**:

*ipwkz*the effects*preference parameters*on distributional outcomes- sample simulate, adjust discount and risk aversion:
**m**|**publish html** - cross test, adjust discount and risk aversion:
**m**|**publish html**

- sample simulate, adjust discount and risk aversion:
*ipwkz*the effects production function*shock parameters*on distributional outcomes- sample simulate, adjust shock persistence and variance:
**m**|**publish html** - cross test, adjust shock persistence and variance:
**m**|**publish html**

- sample simulate, adjust shock persistence and variance:
*ipwkz*the effects production function*elasticity and depreciation parameters*- sample simulate, adjust elasticity and depreciation:
**m**|**publish html** - cross test, adjust elasticity and depreciation:
**m**|**publish html**

- sample simulate, adjust elasticity and depreciation:
*ipwkz*the effects*price parameters*on distributional outcomes- cross test, adjust min income and savings rate:
**m**|**publish html**

- cross test, adjust min income and savings rate:
*ipwkz*the effects*preference + shocks + prices*on distributional outcomes- cross test, adjust preference shocks prices:
**m**|**publish html**

- cross test, adjust preference shocks prices:

**Savings + Borrowing Simulation Results (ipWKBZR)**:

*ipwkbzr*the effects**preference**parameters on distributional outcomes- sample simulate, adjust discount and risk aversion:
**m**|**publish html** - cross test,
**no default**adjust discount and risk aversion:**m**|**publish html** - cross test,
**default**adjust discount and risk aversion:**m**|**publish html**

- sample simulate, adjust discount and risk aversion:
*ipwkbzr*the effects**production function shock**parameters on distributional outcomes- sample simulate, adjust shock persistence and variance:
**m**|**publish html** - cross test,
**no default**adjust shock persistence and variance:**m**|**publish html** - cross test,
**default**adjust shock persistence and variance:**m**|**publish html**

- sample simulate, adjust shock persistence and variance:
*ipwkbzr*the effects**production function elasticity and depreciation**parameters- sample simulate, adjust elasticity and depreciation:
**m**|**publish html** - cross test,
**no default**adjust elasticity and depreciation:**m**|**publish html** - cross test,
**default**adjust elasticity and depreciation:**m**|**publish html**

- sample simulate, adjust elasticity and depreciation:
*ipwkbzr*the effects**price**parameters on distributional outcomes- cross test,
**no default**adjust min income and savings rate:**m**|**publish html** - cross test,
**default**adjust min income and savings rate:**m**|**publish html**

- cross test,
*ipwkbzr*the effects**preference + shocks + prices**on distributional outcomes- cross test,
**default**adjust preference shocks prices:**m**|**publish html**

- cross test,
*ipwkbzr*the effects**borrowing parameters**on distributional outcomes- sample simulate, adjust borrow rate, bounds, etc:
**m**|**publish html** - cross test,
**no default**, adjust borrow rate, bounds, etc:**m**|**publish html** - cross test,
**default**, adjust borrow rate, bounds, etc:**m**|**publish html** - random test,
**default**, adjust borrow rate, bounds, etc:**m**|**publish html**

- sample simulate, adjust borrow rate, bounds, etc:

# 5. One Asset Formal + Informal

Files for this section are in the /m_fibs/ folder in the CodeDynaAsset repository. links: (1) savings problem (2) borrowing and savings problem (3) risky and safe asset problem (risky entrepreneur) (4) percentage choice grids and interpolation (5) Townsend and Wang 2019 Single Asset (6) Townsend and Wang 2019 Safe + Risky Asset.

An application of the codes developed in sections (1) through (5) is the paper: *A Choice Amongst Many: Household Borrowing in a Setting with Multiple Providers* (**Robert M. Townsend** and **Fan Wang** 2019). Below, earlier models are augmented to allow for (a) formal borrowing choice menu, (b) bridge loans, and (c) defaults. This section solves the model in the single asset setting.

## 5.1 Formal and Informal Choices (ABZ+FIBS)

We have a sequence of files that are specific to solving the joint formal and informal problem.

**Choice Generation**

- Formal Borrowing Grid:
**m**|**publish html**- A menu of formal borrowing choices

- Informal Interest Rates:
**m**|**publish html**- Append onto shock Z interest rate shock representing informal interest rate shocks

**Formal and Informal Optimization**

- Match Borrowing to Formal Grid:
**m**|**publish html**- Given a level of borrowing, what items on the formal borrowing menu are the closest to it

- Optimize Formal and Informal, Borrowing and Savings Joint Choices:
**m**|**publish html**- Given interest rates, choose among: formal + informal borrowing, formal borrowing + savings, informal borrowing only, formal borrowing only.

- Bridge Loan:
**m**|**publish html**- When cash-on-hand is insufficient for loan repayment, households resort to informal lenders for bridge loans

- Overall Optimization:
**m**|**publish html**- Overall optimization over joint options, combines
*ffs_fibs_inf_bridge.m*and*ffs_fibs_min_c_cost.m*.

- Overall optimization over joint options, combines

**Results Processing**

- Discrete Choices:
**m**|**publish html**- Discrete choice categories from continuous choices: (1) informal only (ignore bridge loan) (2) formal only (ignore bridge loan) (3) formal + informal borrowing (ignore bridge loan) (4) formal borrowing + savings (ignore bridge loan) (5) informal only (with bridge loan) (6) formal + informal (include bridge loan) (7) formal borrowing + informal borrowing + savings (include bridge loan) (8) No borrowing or savings

## 5.2 Dynamic Programming (ABZ+FIBS)

Parameters can be adjusted here, for the benchmark simulation, similar to the *abz* model:

- savings and borrowing (with default) with minimum income
**750**grid points for asset states/choices**15**grid points for the AR1 shock- benchmark simulation with formal menu
- other parameters

Using three algorithm that provide identical solutions:

*abz fibs*model looped solution:**m**|**publish html**|**profile**- small grid demonstration

*abz fibs*model vectorized solution:**m**|**publish html**|**profile**- speed:
**50.2**seconds

- speed:
*abz fibs*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**2.5**seconds

- speed:

## 5.3 For/Inf + R Shock DP (ABZR+FIBS)

Same as *5.2*, but now include interest rate shock. Parameters can be adjusted here, for the benchmark simulation, similar to the *abz+fibs* model, except:

**55**grid points for shocks:**11**wage,**5**borrow interest

Using three algorithm that provide identical solutions:

*abzr fibs*model looped solution:**m**|**publish html**|**profile**- small grid demonstration

*abzr fibs*model vectorized solution:**m**|**publish html**|**profile**- speed:
**272.5**seconds

- speed:
*abzr fibs*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**11.2**seconds

- speed:

## 5.4 Asset Distributions

This are the wrapper files for *abz+fibs* and *abzr+fibs* that invokes the looped, vectorized, and semi-analytical distributional programs from *1.2*:

*abz+fibs*distributional wrapper:**m**|**publish html**|**profile**- speed (dynamic programming + distribution):
**17.5**seconds

- speed (dynamic programming + distribution):
*abzr+fibs*distributional wrapper:**m**|**publish html**|**profile**- speed (dynamic programming + distribution):
**28.7**seconds

- speed (dynamic programming + distribution):

## 5.5 Solution Support (ABZ+FIBS)

**Parameters and Function Definition (abz+fibs)**:

*abz fibs*model set default parameters:**m**|**publish html***abz fibs*model set functions:**m**|**publish html***abz fibs*model generate states, choices, and shocks grids:**m**|**publish html**

**Parameters and Function Definitions (abzr+fibs)**:

*abzr fibs*model set default parameters:**m**|**publish html***abzr fibs*model set functions:**m**|**publish html***abzr fibs*model generate states, choices, and shocks grids:**m**|**publish html**

**Output Analysis**:

*abz fibs*model solution results processing:**m**|**publish html**- formal and informal choices policy functions

*abz fibs*model solution results graphing:**m**|**publish html**- formal and informal choices levels, logged levels, and percentages

## 5.6 Parameter Testing (ABZ+FIBS)

We solved the exogenously incomplete borrowing and savings problem in *1.4*. Now we analyze model features by adjusting parameters.

**Policy Function Testing**

*abz fibs*model solution precision*abz fibs*model compare saving vs borrowing with and without default

**Asset Distribution Testing**

*abz fibs*borrowing interest rates, bounds, and minimum c- no default:
**m**|**publish html** - default:
**m**|**publish html**

- no default:
*abz fibs*preference parameters and**asset distributions**- no default:
**m**|**publish html** - default:
**m**|**publish html**

- no default:
*abz fibs*model shock process and**asset distributions**- no default:
**m**|**publish html** - default:
**m**|**publish html**

- no default:

# 6. Two Assets Formal + Informal

Files for this section are in the /m_fibs/ folder in the CodeDynaAsset repository. links: (1) savings problem (2) borrowing and savings problem (3) risky and safe asset problem (risky entrepreneur) (4) percentage choice grids and interpolation (5) Townsend and Wang 2019 Single Asset (6) Townsend and Wang 2019 Safe + Risky Asset.

An application of the codes developed in sections (1) through (5) is the paper: *A Choice Amongst Many: Household Borrowing in a Setting with Multiple Providers* (**Robert M. Townsend** and **Fan Wang** 2019). Below, earlier models are augmented to allow for (a) formal borrowing choice menu, (b) bridge loans, and (c) defaults. We do this in the context of the safe and risky asset setting. We use the formal and informal choice codes developed in section *5* above here again.

## 6.1 Two Assets Formal Informal (ipWKBZ+FIBS)

- Use
*ipwkbz+fibs*2nd stage solution with Formal Informal and Bridge:**m**|**publish html** *ipwkbz*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**8.7**seconds

- speed:

## 6.2 Two Assets For+Inf+R Shock (ipWKBZR+FIBS)

**55**grid points for shocks:**11**wage,**5**borrow interest

- Use
*ipwkbzr+fibs*2nd stage solution with Formal Informal and Bridge:**m**|**publish html** *ipwkbzr*model optimized vectorized solution:**m**|**publish html**|**profile**- speed:
**102.1**seconds

- speed:

## 6.2 Asset Distributions

This are the wrapper files for *ipwkbz+fibs* and *ipwkbzr+fibs* that invokes the looped, vectorized, and semi-analytical distributional programs from *3.4.b*:

*ipwkbz+fibs*distributional wrapper:**m**|**publish html**|**profile**- speed (dynamic programming + distribution):
**11.8**seconds

- speed (dynamic programming + distribution):
*ipwkbzr+fibs*distributional wrapper:**m**|**publish html**|**profile**- speed (dynamic programming + distribution):
**137.0**seconds

- speed (dynamic programming + distribution):

## 6.4 Solution Support

All solution algorithms share the same support files.

**Parameters and Function Definitions (ipwkbz+fibs)**:

*ipwkbz+fibs*model set default parameters:**m**|**publish html***ipwkbz+fibs*model set functions:**m**|**publish html***ipwkbz+fibs*model generate states, choices, and shocks grids:**m**|**publish html**

**Parameters and Function Definitions (ipwkbzr+fibs)**:

*ipwkbzr+fibs*model set default parameters:**m**|**publish html***ipwkbzr+fibs*model set functions:**m**|**publish html***ipwkbzr+fibs*model generate states, choices, and shocks grids:**m**|**publish html**

**Output Analysis**:

*ipwkbz+fibs*shares with*akz+wkz+iwkz*models solution results processing codes.*ipwkbz+fibs*shares with*akz+wkz+iwkz*models solution results graphing codes.