├── .gitignore ├── Auxiliary Functions ├── computeChebyshev.m ├── computeGaussLegendreQuadrature.m ├── computeGrids.m ├── computeLinearWeights.m ├── computeMCResidualHistogram.m ├── computeMCResidualPolynomials.m ├── computePolynomials.m ├── coreSteadyState.m ├── equations_polynomials.mod ├── equations_splines.mod ├── firstOrderDynamics_polynomials.mod ├── firstOrderDynamics_polynomials_steadystate.m ├── firstOrderDynamics_splines.mod ├── firstOrderDynamics_splines_steadystate.m ├── parametersResidual.m ├── parameters_polynomials.mod ├── parameters_splines.mod ├── scaleDown.m ├── scaleUp.m ├── setParameters.m ├── updateCoefficients_polynomials.m ├── updateCoefficients_splines.m ├── variables_polynomials.mod └── variables_splines.mod ├── README.txt ├── dynamics.m ├── steadyState.m ├── userGuide.pdf └── winberryAlgorithm.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | /Auxiliary Functions/approximationParameters.mat 2 | /Auxiliary Functions/economicParameters.mat 3 | /Auxiliary Functions/firstOrderDynamics_polynomials/Output/firstOrderDynamics_polynomials_results.mat 4 | /Auxiliary Functions/firstOrderDynamics_polynomials/checksum 5 | /Auxiliary Functions/firstOrderDynamics_polynomials/graphs/firstOrderDynamics_polynomials_IRF_aggregateTFPShock.eps 6 | /Auxiliary Functions/grids.mat 7 | /Auxiliary Functions/polynomials.mat 8 | -------------------------------------------------------------------------------- /Auxiliary Functions/computeChebyshev.m: -------------------------------------------------------------------------------- 1 | function mPoly = computeChebyshev(nPower,vGrid); 2 | 3 | % Computes Chebyshev polynomials up to order "nPower," evaluated along "vGrid" 4 | % 5 | % Inputs 6 | % (1) nPower: order of polynomial 7 | % (2) vGrid: points along which to compute polynomials (must be column vector) 8 | % 9 | % Outputs 10 | % (1) mPoly: nGrid x nPower matrix of polynomials; entry (i,j) is the j_th order 11 | % Chebyshev polynomial evaluated at vGrid(i) 12 | % 13 | % Thomas Winberry, January 19, 2016 14 | 15 | % Compute grid size 16 | [nGrid,~] = size(vGrid); 17 | 18 | % Create polynomial 19 | mPoly = ones(nGrid,nPower); 20 | mPoly(:,2) = vGrid; 21 | for iPower = 3:nPower 22 | mPoly(:,iPower) = 2 * vGrid .* mPoly(:,iPower-1) - mPoly(:,iPower-2); 23 | end -------------------------------------------------------------------------------- /Auxiliary Functions/computeGaussLegendreQuadrature.m: -------------------------------------------------------------------------------- 1 | function [grid,weight] = computeGaussLegendreQuadrature(order) 2 | 3 | % This function computes the notes and weights for the Gauss - Legendre quadrature 4 | % of order "order." 5 | % 6 | % Inputs 7 | % (1) order: order of the quadrature 8 | % 9 | % Outputs 10 | % (1) grid: nodes to evaluate the function on 11 | % (2) weights: weight in the approximation 12 | % 13 | % Jung Sakong, February 10, 2016 14 | 15 | % Compute polynomial recursively 16 | temp_poly = zeros(order+1,order+1); 17 | temp_poly(1,1) = 1; 18 | temp_poly(2,2) = 1; 19 | for ii = 3:(order+1) 20 | temp_poly(ii,:) = (2*ii-3)/(ii-1)*[0,temp_poly(ii-1,1:end-1)]... 21 | - (ii-2)/(ii-1)*temp_poly(ii-2,:); 22 | end 23 | the_poly = fliplr(temp_poly(end,:)); % higher order coefficients first 24 | 25 | % Solve for roots of the polynomial 26 | grid = roots(the_poly); 27 | 28 | % Compute weights 29 | temp_powers = zeros(order,order); 30 | for ii = 1:order 31 | temp_powers(:,ii) = (order+1-ii)*grid.^(order-ii); 32 | end 33 | poly_prime = repmat(the_poly(1:end-1),[order 1]) ... 34 | .* temp_powers; 35 | weight = 2 ./ ((1-grid.^2).*(sum(poly_prime,2)).^2); 36 | -------------------------------------------------------------------------------- /Auxiliary Functions/computeGrids.m: -------------------------------------------------------------------------------- 1 | % Creates grids to use in various approximations 2 | % 3 | % Thomas Winberry, July 26th, 2016 4 | 5 | %--------------------------------------------------------------- 6 | % Grids for approximating individual decisions 7 | %--------------------------------------------------------------- 8 | 9 | if splineOpt == 0 % if using polynomials and approximating conditional expectation 10 | 11 | global vAssetsGridZeros vAssetsGrid mEpsilonGrid mAssetsGrid mEpsilonPrimeGrid 12 | 13 | % Zeros of chebyshev polynomial 14 | vAssetsGridZeros = -cos(((2 * (1:nAssets)-1)' * pi) / (2 * nAssets)); 15 | 16 | % Scale up to state space 17 | vAssetsGrid = scaleUp(vAssetsGridZeros,assetsMin,assetsMax); 18 | 19 | else % if using splines and approximating asset accumulation 20 | 21 | global vAssetsGrid mEpsilonGrid mAssetsGrid mEpsilonPrimeGrid 22 | 23 | % Grid over assets 24 | vAssetsGrid = exp(linspace(log(assetsMin + .01),log(assetsMax + .01),nAssets)'); 25 | vAssetsGrid = vAssetsGrid - .01; 26 | 27 | end 28 | 29 | % Make matrix versions of the grids 30 | mEpsilonGrid = repmat(vEpsilonGrid,[1 nAssets]); 31 | mAssetsGrid = repmat(vAssetsGrid',[nEpsilon 1]); 32 | mEpsilonPrimeGrid = repmat(vEpsilonGrid,[1 nEpsilon*nAssets]); 33 | 34 | %--------------------------------------------------------------- 35 | % Fine grid, for histogram and plotting functions 36 | %--------------------------------------------------------------- 37 | 38 | global vAssetsGridFine vAssetsGridFineZeros mEpsilonGridFine mAssetsGridFine mEpsilonPrimeGridFine 39 | 40 | % Assets grid 41 | vAssetsGridFine = linspace(assetsMin,assetsMax,nAssetsFine)'; 42 | 43 | % Scale down to [-1,1] 44 | vAssetsGridFineZeros = scaleDown(vAssetsGridFine,assetsMin,assetsMax); 45 | 46 | % Make matrix versions of grids 47 | mEpsilonGridFine = repmat(vEpsilonGrid,[1 nAssetsFine]); 48 | mAssetsGridFine = repmat(vAssetsGridFine',[nEpsilon 1]); 49 | mEpsilonPrimeGridFine = repmat(vEpsilonGrid,[1 nStateFine]); 50 | 51 | %--------------------------------------------------------------- 52 | % Quadrature grid, to integrate density (away from borrowing constraint) 53 | %--------------------------------------------------------------- 54 | 55 | global vQuadratureWeights vAssetsGridQuadratureZeros vAssetsGridQuadrature mEpsilonGridQuadrature ... 56 | mAssetsGridQuadrature 57 | 58 | % Compute grid in the interval [-1, 1] 59 | [vAssetsGridQuadratureZeros,vQuadratureWeights] = computeGaussLegendreQuadrature(nAssetsQuadrature); 60 | 61 | % Scale up grid 62 | vAssetsGridQuadrature = scaleUp(vAssetsGridQuadratureZeros,assetsMin+1e-1,assetsMax); 63 | 64 | % Make matrix versions of the grids 65 | mEpsilonGridQuadrature = repmat(vEpsilonGrid,[1 nAssetsQuadrature]); 66 | mAssetsGridQuadrature = repmat(vAssetsGridQuadrature', [nEpsilon 1]); 67 | -------------------------------------------------------------------------------- /Auxiliary Functions/computeLinearWeights.m: -------------------------------------------------------------------------------- 1 | function [vIndicesBelow,vIndicesAbove,vWeightBelow,vWeightAbove] = ... 2 | computeLinearWeights(vGrid,vValues); 3 | 4 | % Given a grid and values off the grid, finds (1) points above and below on the grid and (2) distance 5 | % from these points. Useful for linear interpolation and constructing histogram transition. 6 | % 7 | % Inputs 8 | % (1) vGrid: grid of points find distances between (column vector) 9 | % (2) vValues: vector of values off the grid, to compute weights to closest grid points (column vector) 10 | % 11 | % Outputs 12 | % (1) vIndicesBelow: for each point in vValues, index of gridpoint immediately below 13 | % (2) vIndicesAbove: for each point in vValues, index of gridpoint immediately above 14 | % (3) vWeightBelow: for each point in vValues, computes distance between value and grid point 15 | % above, and then expresses as fraction of total size of bracketing interval (used 16 | % in constructing linear interpolation and histogram transition). So if value very close to 17 | % lower gridpoint, gets more weight. 18 | % (3) vWeightAbove: for each point in vValues, computes distance between value and grid point 19 | % below, and then expresses as fraction of total size of bracketing interval (used 20 | % in constructing linear interpolation and histogram transition) 21 | % 22 | % Thomas Winberry, July 26th, 2016 23 | 24 | % Find nearest point to vValues on the asset grid 25 | vIndices = knnsearch(vGrid,vValues); % find index of nearest neighbor on grid 26 | vGridIndices = vGrid(vIndices); % find value of nearest neighbor on grid 27 | 28 | % Find indices above and below choice 29 | vIndicesBelow = vIndices; vIndicesAbove = vIndices; [nGrid,~] = size(vGrid); 30 | 31 | vIndicesBelow(vGridIndices > vValues) = vIndicesBelow(vGridIndices > vValues) - 1; 32 | vIndicesBelow(vIndicesBelow <= 1) = 1; 33 | vGridIndicesBelow = vGrid(vIndicesBelow); 34 | 35 | vIndicesAbove(vGridIndices <= vValues) = vIndicesAbove(vGridIndices <= vValues) + 1; 36 | vIndicesAbove(vIndicesAbove >= nGrid) = nGrid; 37 | vGridIndicesAbove = vGrid(vIndicesAbove); 38 | 39 | % Compute weights in the weighted sum 40 | vWeightBelow = (vGridIndicesAbove - vValues) ./ (vGridIndicesAbove - vGridIndicesBelow); 41 | vWeightBelow(vValues <= vGridIndicesBelow) = 1; 42 | vWeightBelow(vValues >= vGridIndicesAbove) = 0; 43 | 44 | vWeightAbove = (vValues - vGridIndicesBelow) ./ (vGridIndicesAbove - vGridIndicesBelow); 45 | vWeightAbove(vValues <= vGridIndicesBelow) = 0; 46 | vWeightAbove(vValues >= vGridIndicesAbove) = 1; 47 | -------------------------------------------------------------------------------- /Auxiliary Functions/computeMCResidualHistogram.m: -------------------------------------------------------------------------------- 1 | function [residual,mHistogramOptional,mAssetsPrimeOptional,mConsumptionOptional] = computeMCResidualHistogram(capital) 2 | 3 | % Computes residual of market-clearing condition, using histogram approximation of distribution 4 | % as in Young (2010); used to compute initial guess for parametric family 5 | % 6 | % Inputs 7 | % (1) capital: candidate aggregate capital stock 8 | % 9 | % Outputs 10 | % (1) residual: residual of market clearing condition (to use in root finder) 11 | % (2) (optional) mHistogramOptional: mHistogram - histogram distribution 12 | % (3) (optional) mAssetsPrimeOptional: mAssetsPrime 13 | % (4) (optional) mConsumptionOptional: mConsumption 14 | % 15 | % Thomas Winberry, July 26th, 2016 16 | 17 | % Declare global variables 18 | global bbeta ssigma aalpha ddelta eepsilonBar rrhoEpsilon ssigmaEpsilon aaBar aggEmployment mmu ttau mEpsilonTransition vEpsilonGrid ... 19 | nEpsilon nAssets nState epsilonMin epsilonMax assetsMin assetsMax ... 20 | vAssetsGridZeros vAssetsGrid vAssetsGridHistogram mEpsilonGrid mAssetsGrid ... 21 | vAssetsPoly vAssetsPolySquared mAssetsPolyHistogram w r mEpsilonPrimeGrid maxIterations tolerance dampening vAssetsPolyFine vAssetsGridFine ... 22 | mEpsilonGridFine mAssetsGridFine nAssetsFine nStateFine splineOpt 23 | 24 | % Compute prices 25 | r = aalpha * (capital ^ (aalpha - 1)) * (aggEmployment ^ (1 - aalpha)) - ddelta; 26 | w = (capital ^ aalpha) * (1 - aalpha) * (aggEmployment ^ (-aalpha)); 27 | 28 | %---------------------------------------------------------------- 29 | % Compute individual decisions 30 | %---------------------------------------------------------------- 31 | 32 | if splineOpt == 0 % approximate conditional expectation function using polynomials 33 | 34 | % Initialize coefficients using rule of thumb savings rule 35 | mGridInit = log(bbeta * (1 + r) * ((w * (mmu * (1 - mEpsilonGrid) + (1 - ttau) * mEpsilonGrid) + ... 36 | r * mAssetsGrid) .^ (-ssigma))); 37 | mCoefficients = zeros(nEpsilon,nAssets); 38 | for iEpsilon = 1:nEpsilon % interpolate 39 | vCoefficients = sum(vAssetsPoly' .* (ones(nAssets,1) * mGridInit(iEpsilon,:)),2); 40 | mCoefficients(iEpsilon,:) = (vCoefficients ./ vAssetsPolySquared)'; 41 | end 42 | 43 | % Iterate 44 | err = 100; iteration = 1; 45 | while err > tolerance && iteration <= maxIterations 46 | 47 | mCoefficientsNew = updateCoefficients_polynomials(mCoefficients); 48 | err = max(abs(mCoefficientsNew(:) - mCoefficients(:))); 49 | iteration = iteration + 1; 50 | mCoefficients = dampening * mCoefficients + (1 - dampening) * mCoefficientsNew; 51 | 52 | end 53 | 54 | else % approximate savings decision using linear splines 55 | 56 | % Initialize coefficients 57 | mAssetsPrime = mAssetsGrid; 58 | 59 | % Iterate 60 | err = 100; iteration = 1; 61 | while err > tolerance && iteration <= maxIterations 62 | mAssetsPrimeNew = updateCoefficients_splines(mAssetsPrime); 63 | err = max(abs(mAssetsPrimeNew(:) - mAssetsPrime(:))); 64 | iteration = iteration + 1; 65 | mAssetsPrime = dampening * mAssetsPrime + (1 - dampening) * mAssetsPrimeNew; 66 | end 67 | 68 | end 69 | 70 | %---------------------------------------------------------------- 71 | % Compute histogram approximation of stationary distribution 72 | %---------------------------------------------------------------- 73 | 74 | %%% 75 | % Compute policies over histogram grid 76 | %%% 77 | 78 | if splineOpt == 0 79 | 80 | % Compute decision rules along fine grid 81 | mConditionalExpectation = exp(mCoefficients * vAssetsPolyFine'); 82 | 83 | % Compute savings policy 84 | mAssetsPrimeStar = w * (mmu * (1 - mEpsilonGridFine) + (1 - ttau) * mEpsilonGridFine) + ... 85 | (1 + r) * mAssetsGridFine - (mConditionalExpectation .^ (-1 / ssigma)); 86 | mAssetsPrimeFine = max(mAssetsPrimeStar,aaBar * ones(nEpsilon,nAssetsFine)); 87 | 88 | else % linearly interpolate savings rule over fine grid 89 | 90 | % Compute weights 91 | [vIndicesBelow,vIndicesAbove,vWeightBelow,vWeightAbove] = computeLinearWeights(vAssetsGrid,vAssetsGridFine); 92 | 93 | % Linear interpolation 94 | mAssetsPrimeFine = mAssetsPrime(:,vIndicesBelow) .* repmat(vWeightBelow',nEpsilon,1) + ... 95 | mAssetsPrime(:,vIndicesAbove) .* repmat(vWeightAbove',nEpsilon,1); 96 | 97 | end 98 | 99 | % Compute consumption 100 | mConsumptionFine = w * (mmu * (1 - mEpsilonGridFine) + (1 - ttau) * mEpsilonGridFine) + ... 101 | (1 + r) * mAssetsGridFine - mAssetsPrimeFine; 102 | 103 | %%% 104 | % Compute transition matrix associated with policy rules 105 | %%% 106 | 107 | % Compute weighting matrices 108 | [vIndicesBelow,vIndicesAbove,vWeightBelow,vWeightAbove] = computeLinearWeights(vAssetsGridFine,mAssetsPrimeFine(:)); 109 | 110 | % Compute transition matrix for assets over full grid 111 | mTransitionAbove = zeros(nStateFine,nAssetsFine); 112 | mTransitionBelow = zeros(nStateFine,nAssetsFine); 113 | for a = 1:nAssetsFine 114 | mTransitionBelow(vIndicesBelow == a,a) = vWeightBelow(vIndicesBelow == a); 115 | mTransitionAbove(vIndicesAbove == a,a) = vWeightAbove(vIndicesAbove == a); 116 | end 117 | mAssetsTransition = kron(mTransitionBelow + mTransitionAbove,ones(1,nEpsilon)); 118 | 119 | % Compute transition matrix for idiosyncratic shocks over full grid 120 | mEpsilonTransitionHistogram = repmat(mEpsilonTransition,nAssetsFine); 121 | 122 | % Compute full transition matrix 123 | mTransition = sparse(mAssetsTransition .* mEpsilonTransitionHistogram); 124 | 125 | % Compute invariant histogram by iteration 126 | errHistogram = 100; iterationHistogram = 0; 127 | vHistogram = ones(nStateFine,1) ./ nStateFine; 128 | while errHistogram > 1e-12 && iterationHistogram < 1e4 129 | 130 | vHistogramNew = mTransition' * vHistogram; 131 | errHistogram = max(abs(vHistogramNew - vHistogram)); iterationHistogram = iterationHistogram + 1; 132 | vHistogram = vHistogramNew; 133 | 134 | end 135 | 136 | % Expand histogram matrix 137 | mHistogram = reshape(full(vHistogramNew),nEpsilon,nAssetsFine); 138 | 139 | %---------------------------------------------------------------- 140 | % Return market clearing residual 141 | %---------------------------------------------------------------- 142 | 143 | residual = capital - sum(vAssetsGridFine' .* (mHistogram(1,:) + mHistogram(2,:))); 144 | 145 | if nargout > 1 146 | 147 | mHistogramOptional = mHistogram; 148 | 149 | if nargout > 2 150 | mAssetsPrimeOptional = mAssetsPrimeFine; 151 | mConsumptionOptional = mConsumptionFine; 152 | end 153 | 154 | end -------------------------------------------------------------------------------- /Auxiliary Functions/computeMCResidualPolynomials.m: -------------------------------------------------------------------------------- 1 | function [residual,mCoefficientsOptional,mParametersOptional,mMomentsOptional,mHatOptional] = ... 2 | computeMCResidualPolynomials(capital,mMoments,aGridMoments,mHat) 3 | 4 | % Computes residual of market-clearing condition, parametric family to approximate distribution 5 | % 6 | % Inputs 7 | % (1) capital: candidate aggregate capital stock 8 | % (2) mMoments: intial guess of moments of distribution (nEpsilon x nMeasure) 9 | % (3) aGridMoments: grid of centralized moments for computing PDF, corresponding to mMoments 10 | % (nEpsilon x nAssetsQuadrature x nMoments) 11 | % (4) mHat: initial guess of mass at borrowing constraint 12 | % 13 | % Outputs 14 | % (1) residual: residual of market clearing condition 15 | % (2) (optional) mCoefficientsOptional: coefficients on individual decisions (splines or polynomials) 16 | % (3) (optional) mParametersOptional: parameters of density away from borrowing constraint 17 | % (4) (optional) mMomentsOptional: moments of density away from borrowing constraint 18 | % (5) (optional) mHatOptional: mass at borrowing constraint 19 | % 20 | % Thomas Winberry, July 26th, 2016 21 | 22 | % Declare global variables 23 | global bbeta ssigma aalpha ddelta eepsilonBar rrhoEpsilon ssigmaEpsilon aaBar aggEmployment mmu ttau mEpsilonTransition vEpsilonGrid ... 24 | nEpsilon nAssets nState epsilonMin epsilonMax assetsMin assetsMax ... 25 | vAssetsGridZeros vAssetsGrid vAssetsGridHistogram mEpsilonGrid mAssetsGrid ... 26 | vAssetsPoly vAssetsPolySquared mAssetsPolyHistogram w r mEpsilonPrimeGrid maxIterations tolerance dampening vAssetsPolyFine vAssetsGridFine ... 27 | mEpsilonGridFine mAssetsGridFine nAssetsFine nStateFine ... 28 | vAssetsPolyQuadrature vAssetsGridQuadrature mEpsilonGridQuadrature mAssetsGridQuadrature nAssetsQuadrature ... 29 | nStateQuadrature vQuadratureWeights vEpsilonInvariant nMeasure splineOpt vAssetsPolyBC 30 | 31 | % Compute prices 32 | r = aalpha * (capital ^ (aalpha - 1)) * (aggEmployment ^ (1 - aalpha)) - ddelta; 33 | w = (capital ^ aalpha) * (1 - aalpha) * (aggEmployment ^ (-aalpha)); 34 | 35 | %---------------------------------------------------------------- 36 | % Set error tolerance & max iteration depending on use 37 | %---------------------------------------------------------------- 38 | 39 | if nargout == 1 40 | err1 = tolerance; 41 | err2 = 1e-4; 42 | tol2 = 200; 43 | elseif nargout > 1 44 | err1 = 1e-8; 45 | err2 = 1e-6; 46 | tol2 = 500; 47 | else 48 | error('Check # of inputs & outputs for computeMCResidualPolynomials'); 49 | end 50 | 51 | %---------------------------------------------------------------- 52 | % Compute individual decisions 53 | %---------------------------------------------------------------- 54 | 55 | if splineOpt == 0 % approximate conditional expectation function using polynomials 56 | 57 | % Initialize coefficients using rule of thumb savings rule 58 | mGridInit = log(bbeta * (1 + r) * ((w * (mmu * (1 - mEpsilonGrid) + (1 - ttau) * mEpsilonGrid) + ... 59 | r * mAssetsGrid) .^ (-ssigma))); 60 | mCoefficients = zeros(nEpsilon,nAssets); 61 | for iEpsilon = 1:nEpsilon % interpolate 62 | vCoefficients = sum(vAssetsPoly' .* (ones(nAssets,1) * mGridInit(iEpsilon,:)),2); 63 | mCoefficients(iEpsilon,:) = (vCoefficients ./ vAssetsPolySquared)'; 64 | end 65 | 66 | % Iterate 67 | err = 100; iteration = 1; 68 | while err > tolerance && iteration <= maxIterations 69 | 70 | mCoefficientsNew = updateCoefficients_polynomials(mCoefficients); 71 | err = max(abs(mCoefficientsNew(:) - mCoefficients(:))); 72 | iteration = iteration + 1; 73 | mCoefficients = dampening * mCoefficients + (1 - dampening) * mCoefficientsNew; 74 | 75 | end 76 | 77 | mCoefficientsOptional = mCoefficients; 78 | 79 | else % approximate savings decision using linear splines 80 | 81 | % Initialize coefficients 82 | mAssetsPrime = mAssetsGrid; 83 | 84 | % Iterate 85 | err = 100; iteration = 1; 86 | while err > tolerance && iteration <= maxIterations 87 | mAssetsPrimeNew = updateCoefficients_splines(mAssetsPrime); 88 | err = max(abs(mAssetsPrimeNew(:) - mAssetsPrime(:))); 89 | iteration = iteration + 1; 90 | mAssetsPrime = dampening * mAssetsPrime + (1 - dampening) * mAssetsPrimeNew; 91 | end 92 | 93 | mCoefficientsOptional = mAssetsPrime; 94 | 95 | end 96 | 97 | %---------------------------------------------------------------- 98 | % Compute policies over quadrature grid for integration 99 | %---------------------------------------------------------------- 100 | 101 | if splineOpt == 0 102 | 103 | % Compute conditional expectation 104 | mConditionalExpectation = exp(mCoefficients * vAssetsPolyQuadrature'); 105 | 106 | % Compute savings policy 107 | mAssetsPrimeStar = w * (mmu * (1 - mEpsilonGridQuadrature) + (1 - ttau) * mEpsilonGridQuadrature) + ... 108 | (1 + r) * mAssetsGridQuadrature - (mConditionalExpectation .^ (-1 / ssigma)); 109 | mAssetsPrimeQuadrature = max(mAssetsPrimeStar,aaBar * ones(nEpsilon,nAssetsQuadrature)); 110 | 111 | else 112 | 113 | % Compute weights 114 | [vIndicesBelow,vIndicesAbove,vWeightBelow,vWeightAbove] = computeLinearWeights(vAssetsGrid,vAssetsGridQuadrature); 115 | 116 | % Linear interpolation 117 | mAssetsPrimeQuadrature = mAssetsPrime(:,vIndicesBelow) .* repmat(vWeightBelow',nEpsilon,1) + ... 118 | mAssetsPrime(:,vIndicesAbove) .* repmat(vWeightAbove',nEpsilon,1); 119 | 120 | end 121 | 122 | %---------------------------------------------------------------- 123 | % Compute policies at borrowing constraint for integration 124 | %---------------------------------------------------------------- 125 | 126 | if splineOpt == 0 127 | 128 | % Compute conditional expectation 129 | mConditionalExpectation = exp(mCoefficients * vAssetsPolyBC'); 130 | 131 | % Compute savings policy 132 | mAssetsPrimeStar = w * (mmu * (1 - vEpsilonGrid) + (1 - ttau) * vEpsilonGrid) + ... 133 | (1 + r) * assetsMin - (mConditionalExpectation .^ (-1 / ssigma)); 134 | mAssetsPrimeBC = max(mAssetsPrimeStar,aaBar * ones(nEpsilon,1)); 135 | 136 | else 137 | 138 | % Compute weights 139 | [vIndicesBelow,vIndicesAbove,vWeightBelow,vWeightAbove] = computeLinearWeights(vAssetsGrid,assetsMin); 140 | 141 | % Linear interpolation 142 | mAssetsPrimeBC = mAssetsPrime(:,vIndicesBelow) .* repmat(vWeightBelow',nEpsilon,1) + ... 143 | mAssetsPrime(:,vIndicesAbove) .* repmat(vWeightAbove',nEpsilon,1); 144 | 145 | end 146 | 147 | %---------------------------------------------------------------- 148 | % Compute stationary distribution from these decision rules 149 | %---------------------------------------------------------------- 150 | 151 | % Initialize iteration 152 | err = 100; iteration = 1; 153 | options = optimoptions(@fminunc,'Algorithm','quasi-newton','Display','notify-detailed',... 154 | 'MaxFunEvals',50000,'TolFun',1e-12,'GradObj','on','MaxIter',1000); 155 | %{ For older versions of MATLAB: 156 | options = optimset('LargeScale','off','Display','notify-detailed',... 157 | 'MaxFunEvals',50000,'TolFun',1e-12,'GradObj','on','MaxIter',1000); 158 | %} 159 | 160 | % Iteration 161 | while err > err2 && iteration <= tol2 162 | 163 | %%% 164 | % Update density away from borrowing constraint 165 | %%% 166 | 167 | % Compute parameters of the distribution by minimization 168 | mParameters = zeros(nEpsilon,nMeasure+1); 169 | for iEpsilon = 1 : nEpsilon 170 | objectiveFunction = @(vParametersTilde) parametersResidual(vParametersTilde,squeeze(aGridMoments(iEpsilon,:,:))); 171 | [vParameters,normalization] = fminunc(objectiveFunction,zeros(nMeasure,1),options); 172 | mParameters(iEpsilon,:) = [1 / normalization; vParameters]; 173 | end 174 | 175 | % Compute new moments and centered moments grid 176 | mMomentsNew = zeros(nEpsilon,nMeasure); 177 | aGridMomentsNew = zeros(nEpsilon,nAssetsQuadrature,nMeasure); 178 | 179 | for iEpsilon = 1 : nEpsilon 180 | 181 | % Compute first moment (uncentered) 182 | mMomentsNew(iEpsilon,1) = 0; 183 | for iEpsilonTilde = 1 : nEpsilon 184 | 185 | mMomentsNew(iEpsilon,1) = mMomentsNew(iEpsilon,1) + (1 - mHat(iEpsilonTilde,1)) * vEpsilonInvariant(iEpsilonTilde) * mEpsilonTransition(... 186 | iEpsilonTilde,iEpsilon) * mParameters(iEpsilonTilde,1) * vQuadratureWeights' * (mAssetsPrimeQuadrature(iEpsilonTilde,:)' .* ... 187 | exp(squeeze(aGridMoments(iEpsilonTilde,:,:)) * mParameters(iEpsilonTilde,2:nMeasure+1)')) + mHat(iEpsilonTilde,1) * ... 188 | vEpsilonInvariant(iEpsilonTilde) * mEpsilonTransition(iEpsilonTilde,iEpsilon) * mAssetsPrimeBC(iEpsilonTilde,1); 189 | 190 | end 191 | 192 | mMomentsNew(iEpsilon,1) = mMomentsNew(iEpsilon,1) / vEpsilonInvariant(iEpsilon); 193 | aGridMomentsNew(iEpsilon,:,1) = vAssetsGridQuadrature - mMomentsNew(iEpsilon,1); 194 | 195 | % Compute higher order moments (centered) 196 | for iMoment = 2 : nMeasure 197 | 198 | mMomentsNew(iEpsilon,iMoment) = 0; 199 | 200 | for iEpsilonTilde = 1 : nEpsilon 201 | 202 | mMomentsNew(iEpsilon,iMoment) = mMomentsNew(iEpsilon,iMoment) + (1 - mHat(iEpsilonTilde,1)) * vEpsilonInvariant(iEpsilonTilde) * mEpsilonTransition(... 203 | iEpsilonTilde,iEpsilon) * mParameters(iEpsilonTilde,1) * vQuadratureWeights' * (((mAssetsPrimeQuadrature(iEpsilonTilde,:)' - ... 204 | mMomentsNew(iEpsilon,1)) .^ iMoment) .* exp(squeeze(aGridMoments(iEpsilonTilde,:,:)) * ... 205 | mParameters(iEpsilonTilde,2:nMeasure+1)')) + mHat(iEpsilonTilde,1) * vEpsilonInvariant(iEpsilonTilde) * ... 206 | mEpsilonTransition(iEpsilonTilde,iEpsilon) * ((mAssetsPrimeBC(iEpsilonTilde,1) - mMomentsNew(iEpsilon,1)) .^ iMoment); 207 | 208 | end 209 | 210 | mMomentsNew(iEpsilon,iMoment) = mMomentsNew(iEpsilon,iMoment) / vEpsilonInvariant(iEpsilon); 211 | aGridMomentsNew(iEpsilon,:,iMoment) = (vAssetsGridQuadrature' - mMomentsNew(iEpsilon,1)) .^ iMoment - ... 212 | mMomentsNew(iEpsilon,iMoment); 213 | 214 | end 215 | 216 | end 217 | 218 | %%% 219 | % Update mass at borrowing constraint 220 | %%% 221 | 222 | mHatNew = zeros(nEpsilon,1); 223 | 224 | for iEpsilon = 1 : nEpsilon; 225 | 226 | for iEpsilonTilde = 1 : nEpsilon 227 | 228 | mHatNew(iEpsilon,1) = mHatNew(iEpsilon,1) + (1 - mHat(iEpsilonTilde,1)) * vEpsilonInvariant(iEpsilonTilde) * ... 229 | mEpsilonTransition(iEpsilonTilde,iEpsilon) * mParameters(iEpsilonTilde,1) * vQuadratureWeights' * ... 230 | ((mAssetsPrimeQuadrature(iEpsilonTilde,:)' <= aaBar + 1e-8) .* exp(squeeze(aGridMoments(iEpsilonTilde,:,:)) * mParameters(iEpsilonTilde,2:nMeasure+1)')) + ... 231 | mHat(iEpsilonTilde,1) * vEpsilonInvariant(iEpsilonTilde) * mEpsilonTransition(iEpsilonTilde,iEpsilon) * ... 232 | (mAssetsPrimeBC(iEpsilonTilde,1) <= aaBar + 1e-8); 233 | 234 | end 235 | 236 | mHatNew(iEpsilon,1) = mHatNew(iEpsilon,1) / vEpsilonInvariant(iEpsilon); 237 | 238 | end 239 | 240 | %%% 241 | % Update iteration 242 | %%% 243 | 244 | err = max([max(abs(mMomentsNew(:) - mMoments(:))),max(abs(mHatNew(:) - mHat(:)))]); 245 | iteration = iteration + 1; 246 | mMoments = mMomentsNew; 247 | aGridMoments = aGridMomentsNew; 248 | mHat = mHatNew; 249 | 250 | end 251 | 252 | %---------------------------------------------------------------- 253 | % Return market clearing residual 254 | %---------------------------------------------------------------- 255 | 256 | capitalNew = (vEpsilonInvariant .* (1 - mHat))' * mMoments(:,1) + aaBar * (vEpsilonInvariant .* mHat)' * ones(nEpsilon,1); 257 | residual = capital - capitalNew; 258 | 259 | % Also return optional outputs if requested 260 | if nargout > 2 261 | 262 | mParametersOptional = mParameters; 263 | mMomentsOptional = mMoments; 264 | mHatOptional = mHat; 265 | 266 | end 267 | -------------------------------------------------------------------------------- /Auxiliary Functions/computePolynomials.m: -------------------------------------------------------------------------------- 1 | % Compute polynomials for approximations (only relevant if approximating conditional expectation) 2 | % 3 | % Thomas Winberry, July 26th, 2016 4 | 5 | %--------------------------------------------------------------- 6 | % Grid for approximating conditional expectation 7 | %--------------------------------------------------------------- 8 | 9 | global vAssetsPoly vAssetsPolySquared 10 | 11 | % Create polynomials 12 | vAssetsPoly = computeChebyshev(nAssets,vAssetsGridZeros); 13 | 14 | % Create squared terms for interpolation 15 | vAssetsPolySquared = sum(vAssetsPoly .^ 2)'; 16 | 17 | %--------------------------------------------------------------- 18 | % Fine grid, for histogram and plots 19 | %--------------------------------------------------------------- 20 | 21 | global vAssetsPolyFine 22 | 23 | % Create polynomials 24 | vAssetsPolyFine = computeChebyshev(nAssets,vAssetsGridFineZeros); 25 | 26 | %--------------------------------------------------------------- 27 | % Quadrature grid, for integrating distribution 28 | %--------------------------------------------------------------- 29 | 30 | global vAssetsPolyQuadrature 31 | 32 | % Create polynomials 33 | vAssetsPolyQuadrature = computeChebyshev(nAssets,vAssetsGridQuadratureZeros); 34 | 35 | %--------------------------------------------------------------- 36 | % At borrowing constraint, for integrating mass point 37 | %--------------------------------------------------------------- 38 | 39 | global vAssetsPolyBC 40 | 41 | % Create polynomials 42 | vAssetsPolyBC = computeChebyshev(nAssets,-1); % at edge of state space -------------------------------------------------------------------------------- /Auxiliary Functions/coreSteadyState.m: -------------------------------------------------------------------------------- 1 | % Computes market clearing capital stock and associated distribution and 2 | % decision rules in steady state 3 | % 4 | % Thomas Winberry, July 26th, 2016 5 | 6 | %---------------------------------------------------------------- 7 | % Compute approximation tools 8 | %---------------------------------------------------------------- 9 | 10 | % Grids 11 | computeGrids; 12 | 13 | % Polynomials over grids (only if using polynomials to approximate conditional expectation) 14 | if splineOpt == 0 15 | computePolynomials; 16 | end 17 | 18 | %---------------------------------------------------------------- 19 | % Compute initial guess of market-clearing capital stock using 20 | % histogram approximation of distribution, from Young (2010) 21 | %---------------------------------------------------------------- 22 | 23 | t0 = tic; 24 | fprintf('Computing initial guess from histogram...\n') 25 | 26 | % Solve for market clearing capital stock 27 | f = @(capital) computeMCResidualHistogram(capital); 28 | options = optimoptions('fsolve','Display',displayOpt,'TolFun',1e-2); % In older versions of MATLAB, use: options = optimset('Display',displayOpt); 29 | [aggregateCapitalInit,err,exitflag] = fsolve(f,1.01*kRepSS,options); 30 | 31 | % Return exitflag if market clearing not solved 32 | if exitflag < 1 33 | check = 1; 34 | return; 35 | end 36 | 37 | aggregateCapital = aggregateCapitalInit; 38 | if strcmp(displayOpt,'iter-detailed') == 1 39 | fprintf('Done! Time to compute: %2.2f seconds \n\n',toc(t0)) 40 | end 41 | 42 | %---------------------------------------------------------------- 43 | % Compute moments of histogram to use as initial guess for parametric family 44 | %---------------------------------------------------------------- 45 | 46 | % Compute histogram 47 | [~, mHistogram] = computeMCResidualHistogram(aggregateCapital); 48 | 49 | % Compute moments from histogram 50 | mMomentsHistogram = zeros(nEpsilon,nMeasure); 51 | aGridMoments = zeros(nEpsilon,nAssetsQuadrature,nMeasure); % grid for computing PDF 52 | 53 | for iEpsilon = 1 : nEpsilon 54 | 55 | % First moment (uncentered) 56 | mMomentsHistogram(iEpsilon,1) = sum(vAssetsGridFine' .* (mHistogram(iEpsilon,:) ./ ... 57 | sum(mHistogram(iEpsilon,:)))); 58 | aGridMoments(iEpsilon,:,1) = vAssetsGridQuadrature - mMomentsHistogram(iEpsilon,1); 59 | 60 | % Higher order moments (centered) 61 | for iMoment = 2 : nMeasure 62 | mMomentsHistogram(iEpsilon,iMoment) = sum(((vAssetsGridFine' - mMomentsHistogram(iEpsilon,1)) .^ iMoment) .* ... 63 | (mHistogram(iEpsilon,:) ./ sum(mHistogram(iEpsilon,:)))); 64 | aGridMoments(iEpsilon,:,iMoment) = (vAssetsGridQuadrature' - mMomentsHistogram(iEpsilon,1)) .^ ... 65 | iMoment - mMomentsHistogram(iEpsilon,iMoment); 66 | end 67 | 68 | end 69 | 70 | % Mass at borrowing constraint 71 | mHatHistogram = [mHistogram(1,1) / sum(mHistogram(1,:));mHistogram(2,1) / sum(mHistogram(2,:))]; 72 | 73 | %---------------------------------------------------------------- 74 | % Compute market-clearing capital stock from parametric family 75 | %---------------------------------------------------------------- 76 | 77 | t0 = tic; 78 | fprintf('Compute steady state from parametric family...\n') 79 | 80 | % Solve for market clearing capital stock 81 | f = @(capital) computeMCResidualPolynomials(capital,mMomentsHistogram,aGridMoments,mHatHistogram); 82 | options = optimoptions('fsolve','Display',displayOpt,'TolFun',1e-2); 83 | if abs(f(aggregateCapitalInit)) > 1e-4 84 | [aggregateCapital,err,exitflag] = fsolve(f,aggregateCapitalInit,options); 85 | end 86 | 87 | % Return error if market clearing not solved 88 | if exitflag < 1 89 | check = 1; 90 | return; 91 | end 92 | 93 | if strcmp(displayOpt,'iter-detailed') == 1 94 | fprintf('Done! Time to compute: %2.2f seconds \n\n',toc(t0)) 95 | end 96 | 97 | %---------------------------------------------------------------- 98 | % Compute other objects from steady state 99 | %---------------------------------------------------------------- 100 | 101 | [~,mCoefficients,mParameters,mMoments,mHat] = ... 102 | computeMCResidualPolynomials(aggregateCapital,mMomentsHistogram,aGridMoments,mHatHistogram); -------------------------------------------------------------------------------- /Auxiliary Functions/equations_polynomials.mod: -------------------------------------------------------------------------------- 1 | // Specifies equations for firstOrderDynamics.mod 2 | // 3 | // Thomas Winberry, July 26th, 2016 4 | 5 | //---------------------------------------------------------------- 6 | // Conditional expectation (#equations = nEpsilon * nAssets) 7 | //---------------------------------------------------------------- 8 | 9 | @#for iEpsilon in 1 : nEpsilon 10 | 11 | @#for iAssets in 1 : nAssets 12 | 13 | // Compute conditional expectation 14 | # expectation_@{iEpsilon}_@{iAssets} = exp(0 15 | @#for iPower in 1 : nAssets 16 | + expectationCoefficient_@{iEpsilon}_@{iPower} * expectationPoly_@{iAssets}_@{iPower} 17 | @#endfor 18 | ); 19 | 20 | // Compute savings policy 21 | # assetsPrime_@{iEpsilon}_@{iAssets} = max(w * (mmu * (1 - epsilonGrid_@{iEpsilon}) + (1 - ttau) * 22 | epsilonGrid_@{iEpsilon}) + (1 + r) * assetsGrid_@{iAssets} - (expectation_@{iEpsilon}_@{iAssets} ^ 23 | (-1 / ssigma)),aaBar); 24 | 25 | // Compute next period's consumption 26 | @#for iEpsilonPrime in 1 : nEpsilon 27 | 28 | # expectationPrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets} = exp(0 29 | @#for iPower in 1 : nAssets 30 | + expectationCoefficient_@{iEpsilonPrime}_@{iPower}(+1) * cos((@{iPower} - 1) * acos(min(max( 31 | 2 * ((assetsPrime_@{iEpsilon}_@{iAssets} - assetsMin) / (assetsMax - assetsMin)) - 1,-1),1))) 32 | @#endfor 33 | ); 34 | 35 | # assetsPrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets} = max(w(+1) * (mmu * (1 - epsilonGrid_@{iEpsilonPrime}) + 36 | (1 - ttau) * epsilonGrid_@{iEpsilonPrime}) + (1 + r(+1)) * assetsPrime_@{iEpsilon}_@{iAssets} - 37 | (expectationPrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets} ^ (-1 / ssigma)),aaBar); 38 | 39 | # consumptionPrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets} = w(+1) * (mmu * (1 - epsilonGrid_@{iEpsilonPrime}) + 40 | (1 - ttau) * epsilonGrid_@{iEpsilonPrime}) + (1 + r(+1)) * assetsPrime_@{iEpsilon}_@{iAssets} - 41 | assetsPrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets}; 42 | 43 | @#endfor 44 | 45 | // Functional equation 46 | log(expectation_@{iEpsilon}_@{iAssets}) = log(bbeta * (1 + r(+1)) * (0 47 | @#for iEpsilonPrime in 1 : nEpsilon 48 | + epsilonTransition_@{iEpsilon}_@{iEpsilonPrime} * (consumptionPrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets} ^ 49 | (-ssigma)) 50 | @#endfor 51 | )); 52 | 53 | @#endfor 54 | 55 | @#endfor 56 | 57 | //---------------------------------------------------------------- 58 | // Compute various objects over quadrature grid for integrating distribution 59 | //---------------------------------------------------------------- 60 | 61 | @#for iEpsilon in 1 : nEpsilon 62 | 63 | @#for iAssets in 1 : nAssetsQuadrature 64 | 65 | // Compute conditional expectation 66 | # expectationQuadrature_@{iEpsilon}_@{iAssets} = exp(0 67 | @#for iPower in 1 : nAssets 68 | + expectationCoefficient_@{iEpsilon}_@{iPower} * quadraturePoly_@{iAssets}_@{iPower} 69 | @#endfor 70 | ); 71 | 72 | // Compute savings policy 73 | # assetsPrimeQuadrature_@{iEpsilon}_@{iAssets} = max(w * (mmu * (1 - epsilonGrid_@{iEpsilon}) + (1 - ttau) * 74 | epsilonGrid_@{iEpsilon}) + (1 + r) * quadratureGrid_@{iAssets} - (expectationQuadrature_@{iEpsilon}_@{iAssets} ^ 75 | (-1 / ssigma)),aaBar); 76 | 77 | // PDF of distribution 78 | # measurePDF_@{iEpsilon}_@{iAssets} = exp(0 + measureCoefficient_@{iEpsilon}_1 * (quadratureGrid_@{iAssets} - 79 | moment_@{iEpsilon}_1(-1)) 80 | @#for iMoment in 2 : nMeasure 81 | + measureCoefficient_@{iEpsilon}_@{iMoment} * ((quadratureGrid_@{iAssets} - moment_@{iEpsilon}_1(-1)) ^ @{iMoment} - 82 | moment_@{iEpsilon}_@{iMoment}(-1)) 83 | @#endfor 84 | ); 85 | 86 | @#endfor 87 | 88 | // Total mass of distribution 89 | # totalMass_@{iEpsilon} = 0 90 | @#for iAssets in 1 : nAssetsQuadrature 91 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilon}_@{iAssets} 92 | @#endfor 93 | ; 94 | 95 | @#endfor 96 | 97 | //---------------------------------------------------------------- 98 | // Compute various objects at borrowing constraint for integrating distribution 99 | //---------------------------------------------------------------- 100 | 101 | @#for iEpsilon in 1 : nEpsilon 102 | 103 | // Compute conditional expectation 104 | # expectationBC_@{iEpsilon} = exp(0 105 | @#for iPower in 1 : nAssets 106 | + expectationCoefficient_@{iEpsilon}_@{iPower} * bcPoly_@{iPower} 107 | @#endfor 108 | ); 109 | 110 | // Compute savings policy 111 | # assetsPrimeBC_@{iEpsilon} = max(w * (mmu * (1 - epsilonGrid_@{iEpsilon}) + (1 - ttau) * 112 | epsilonGrid_@{iEpsilon}) + (1 + r) * aaBar - (expectationBC_@{iEpsilon} ^ 113 | (-1 / ssigma)),aaBar); 114 | 115 | @#endfor 116 | 117 | //---------------------------------------------------------------- 118 | // Relationship between moments of distribution and parameters 119 | // (#equations = nEpsilon * nMeasure) 120 | //---------------------------------------------------------------- 121 | 122 | @#for iEpsilon in 1 : nEpsilon 123 | 124 | // First moments (uncentered) 125 | moment_@{iEpsilon}_1(-1) = (0 126 | @#for iAssets in 1 : nAssetsQuadrature 127 | + quadratureWeights_@{iAssets} * quadratureGrid_@{iAssets} * measurePDF_@{iEpsilon}_@{iAssets} 128 | @#endfor 129 | ) / totalMass_@{iEpsilon}; 130 | 131 | // Higher order moments (centered) 132 | @#for iMoment in 2 : nMeasure 133 | moment_@{iEpsilon}_@{iMoment}(-1) = (0 134 | @#for iAssets in 1 : nAssetsQuadrature 135 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilon}_@{iAssets} * ((quadratureGrid_@{iAssets} - 136 | moment_@{iEpsilon}_1(-1)) ^ @{iMoment}) 137 | @#endfor 138 | ) / totalMass_@{iEpsilon}; 139 | 140 | @#endfor 141 | 142 | @#endfor 143 | 144 | //---------------------------------------------------------------- 145 | // Law of motion for density away from borrowing constraint 146 | // (#equations = nEpsilon * nMeasure) 147 | //---------------------------------------------------------------- 148 | 149 | @#for iEpsilon in 1 : nEpsilon 150 | 151 | // First moment (uncentered) 152 | moment_@{iEpsilon}_1 = (0 153 | @#for iEpsilonTilde in 1 : nEpsilon 154 | + ((1 - mHat_@{iEpsilonTilde}(-1)) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} * (0 155 | @#for iAssets in 1 : nAssetsQuadrature 156 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilonTilde}_@{iAssets} * 157 | assetsPrimeQuadrature_@{iEpsilonTilde}_@{iAssets} 158 | @#endfor 159 | ) / totalMass_@{iEpsilonTilde}) + mHat_@{iEpsilonTilde}(-1) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} 160 | * assetsPrimeBC_@{iEpsilonTilde} 161 | @#endfor 162 | ) / epsilonMass_@{iEpsilon}; 163 | 164 | // Higher order moments (uncentered) 165 | @#for iMoment in 2 : nMeasure 166 | moment_@{iEpsilon}_@{iMoment} = (0 167 | @#for iEpsilonTilde in 1 : nEpsilon 168 | + ((1 - mHat_@{iEpsilonTilde}(-1)) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} * (0 169 | @#for iAssets in 1 : nAssetsQuadrature 170 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilonTilde}_@{iAssets} * 171 | (assetsPrimeQuadrature_@{iEpsilonTilde}_@{iAssets} - moment_@{iEpsilon}_1) ^ @{iMoment} 172 | @#endfor 173 | ) / totalMass_@{iEpsilonTilde}) + mHat_@{iEpsilonTilde}(-1) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} 174 | * (assetsPrimeBC_@{iEpsilonTilde} - moment_@{iEpsilon}_1) ^ @{iMoment} 175 | @#endfor 176 | ) / epsilonMass_@{iEpsilon}; 177 | @#endfor 178 | 179 | @#endfor 180 | 181 | //---------------------------------------------------------------- 182 | // Law of motion for mass at borrowing constraint 183 | // (#equations = nEpsilon) 184 | //---------------------------------------------------------------- 185 | 186 | @#for iEpsilon in 1 : nEpsilon 187 | 188 | mHat_@{iEpsilon} = (0 189 | @#for iEpsilonTilde in 1 : nEpsilon 190 | + ((1 - mHat_@{iEpsilonTilde}(-1)) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} * (0 191 | @#for iAssets in 1 : nAssetsQuadrature 192 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilonTilde}_@{iAssets} * 193 | (assetsPrimeQuadrature_@{iEpsilonTilde}_@{iAssets} <= aaBar + 1e-8) 194 | @#endfor 195 | ) / totalMass_@{iEpsilonTilde}) + mHat_@{iEpsilonTilde}(-1) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} 196 | * (assetsPrimeBC_@{iEpsilonTilde} <= aaBar + 1e-8) 197 | @#endfor 198 | ) / epsilonMass_@{iEpsilon}; 199 | 200 | @#endfor 201 | 202 | //---------------------------------------------------------------- 203 | // Factor prices (# equations = 2) 204 | //---------------------------------------------------------------- 205 | 206 | # aggregateCapital = (1 - aggEmployment) * moment_1_1(-1) + aggEmployment * moment_2_1(-1); 207 | 208 | r = exp(aggregateTFP) * aalpha * (aggregateCapital ^ (aalpha - 1)) * (aggEmployment ^ (1 - aalpha)) - ddelta; 209 | w = exp(aggregateTFP) * (aggregateCapital ^ aalpha) * (1 - aalpha) * (aggEmployment ^ (-aalpha)); 210 | 211 | //---------------------------------------------------------------- 212 | // Law of motion for aggregate TFP (# equations = 1) 213 | //---------------------------------------------------------------- 214 | 215 | aggregateTFP = rrhoTFP * aggregateTFP(-1) + ssigmaTFP * aggregateTFPShock; 216 | 217 | //---------------------------------------------------------------- 218 | // Auxiliary variables of interest (# equations = 4) 219 | //---------------------------------------------------------------- 220 | 221 | // Output 222 | logAggregateOutput = log(exp(aggregateTFP) * (aggregateCapital ^ aalpha) * (aggEmployment ^ (1 - aalpha))); 223 | 224 | // Investment 225 | logAggregateInvestment = log((1 - aggEmployment) * moment_1_1 + aggEmployment * moment_2_1 - (1 - ddelta) * aggregateCapital); 226 | 227 | // Consumption 228 | logAggregateConsumption = log(exp(logAggregateOutput) - exp(logAggregateInvestment)); 229 | 230 | // Wage 231 | logWage = log(w); 232 | -------------------------------------------------------------------------------- /Auxiliary Functions/equations_splines.mod: -------------------------------------------------------------------------------- 1 | // Specifies equations for firstOrderDynamics.mod 2 | // 3 | // Thomas Winberry and Alp Tuncay, July 26th, 2016 4 | 5 | //---------------------------------------------------------------- 6 | // Individual decisions (#equations = nEpsilon * nAssets) 7 | //---------------------------------------------------------------- 8 | 9 | @#for iEpsilon in 1 : nEpsilon 10 | 11 | @#for iAssets in 1 : nAssets 12 | 13 | // Compute basis representation of spline approximation 14 | # basis_1_@{iEpsilon}_@{iAssets} = ((assetsGrid_2 - coefficient_@{iEpsilon}_@{iAssets}) / 15 | (assetsGrid_2 - assetsGrid_1)) * (coefficient_@{iEpsilon}_@{iAssets} < assetsGrid_2); 16 | 17 | @#for iAssetsTilde in 2 : nAssets - 1 18 | # basis_@{iAssetsTilde}_@{iEpsilon}_@{iAssets} = ((coefficient_@{iEpsilon}_@{iAssets} - 19 | assetsGrid_@{iAssetsTilde-1}) / (assetsGrid_@{iAssetsTilde} - assetsGrid_@{iAssetsTilde-1})) * 20 | (assetsGrid_@{iAssetsTilde-1} <= coefficient_@{iEpsilon}_@{iAssets}) * (assetsGrid_@{iAssetsTilde} > 21 | coefficient_@{iEpsilon}_@{iAssets}) + ((assetsGrid_@{iAssetsTilde+1} - coefficient_@{iEpsilon}_@{iAssets}) / 22 | (assetsGrid_@{iAssetsTilde+1} - assetsGrid_@{iAssetsTilde})) * (assetsGrid_@{iAssetsTilde} <= 23 | coefficient_@{iEpsilon}_@{iAssets}) * (assetsGrid_@{iAssetsTilde+1} > coefficient_@{iEpsilon}_@{iAssets}); 24 | @#endfor 25 | 26 | # basis_@{nAssets}_@{iEpsilon}_@{iAssets} = ((coefficient_@{iEpsilon}_@{iAssets} - assetsGrid_@{nAssets-1}) / 27 | (assetsGrid_@{nAssets} - assetsGrid_@{nAssets-1})) * (coefficient_@{iEpsilon}_@{iAssets} >= assetsGrid_@{nAssets-1}); 28 | 29 | // Interpolate asset choices over assetPrime 30 | @#for iEpsilonPrime in 1 : nEpsilon 31 | 32 | # assetsPrimePrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets} = (0 33 | @#for iCoefficient in 1 : nAssets 34 | + coefficient_@{iEpsilonPrime}_@{iCoefficient}(+1) * basis_@{iCoefficient}_@{iEpsilon}_@{iAssets} 35 | @#endfor 36 | ); 37 | 38 | # consumptionPrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets} = w(+1) * (mmu * (1 - epsilonGrid_@{iEpsilonPrime}) + 39 | (1 - ttau) * epsilonGrid_@{iEpsilonPrime}) + (1 + r(+1)) * coefficient_@{iEpsilon}_@{iAssets} - 40 | assetsPrimePrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets}; 41 | 42 | @#endfor 43 | 44 | # consumption_@{iEpsilon}_@{iAssets} = (bbeta * (1 + r(+1)) * (0 45 | @#for iEpsilonPrime in 1 : nEpsilon 46 | + epsilonTransition_@{iEpsilon}_@{iEpsilonPrime} * (consumptionPrime_@{iEpsilonPrime}_@{iEpsilon}_@{iAssets} ^ 47 | (-ssigma)) 48 | @#endfor 49 | )) ^ (-1 / ssigma); 50 | 51 | coefficient_@{iEpsilon}_@{iAssets} = max(aaBar,w * (mmu * (1 - epsilonGrid_@{iEpsilon}) + 52 | (1 - ttau) * epsilonGrid_@{iEpsilon}) + (1 + r) * assetsGrid_@{iAssets} - consumption_@{iEpsilon}_@{iAssets}); 53 | 54 | @#endfor 55 | 56 | @#endfor 57 | 58 | //---------------------------------------------------------------- 59 | // Compute various objects over quadrature grid for future use 60 | //---------------------------------------------------------------- 61 | 62 | // Interpolate savings choice over quadrature grid 63 | 64 | @#for iAssets in 1 : nAssetsQuadrature 65 | 66 | // Compute basis representation of spline representation 67 | # basisQuadrature_1_@{iAssets} = ((assetsGrid_2 - quadratureGrid_@{iAssets}) / 68 | (assetsGrid_2 - assetsGrid_1)) * (quadratureGrid_@{iAssets} < assetsGrid_2); 69 | 70 | @#for iAssetsTilde in 2 : nAssets - 1 71 | # basisQuadrature_@{iAssetsTilde}_@{iAssets} = ((quadratureGrid_@{iAssets} - 72 | assetsGrid_@{iAssetsTilde-1}) / (assetsGrid_@{iAssetsTilde} - assetsGrid_@{iAssetsTilde-1})) * 73 | (assetsGrid_@{iAssetsTilde-1} <= quadratureGrid_@{iAssets}) * (assetsGrid_@{iAssetsTilde} > 74 | quadratureGrid_@{iAssets}) + ((assetsGrid_@{iAssetsTilde+1} - quadratureGrid_@{iAssets}) / 75 | (assetsGrid_@{iAssetsTilde+1} - assetsGrid_@{iAssetsTilde})) * (assetsGrid_@{iAssetsTilde} <= 76 | quadratureGrid_@{iAssets}) * (assetsGrid_@{iAssetsTilde+1} > quadratureGrid_@{iAssets}); 77 | @#endfor 78 | 79 | # basisQuadrature_@{nAssets}_@{iAssets} = ((quadratureGrid_@{iAssets} - assetsGrid_@{nAssets-1}) / 80 | (assetsGrid_@{nAssets} - assetsGrid_@{nAssets-1})) * (quadratureGrid_@{iAssets} >= assetsGrid_@{nAssets-1}); 81 | 82 | // Interpolate asset choices over this grid 83 | @#for iEpsilon in 1 : nEpsilon 84 | 85 | # assetsPrimeQuadrature_@{iEpsilon}_@{iAssets} = (0 86 | @#for iCoefficient in 1 : nAssets 87 | + coefficient_@{iEpsilon}_@{iCoefficient} * basisQuadrature_@{iCoefficient}_@{iAssets} 88 | @#endfor 89 | ); 90 | 91 | @#endfor 92 | 93 | @#endfor 94 | 95 | // Compute density away from borrowing constraint 96 | 97 | @#for iEpsilon in 1 : nEpsilon 98 | 99 | @#for iAssets in 1 : nAssetsQuadrature 100 | 101 | # measurePDF_@{iEpsilon}_@{iAssets} = exp(0 + measureCoefficient_@{iEpsilon}_1 * (quadratureGrid_@{iAssets} - 102 | moment_@{iEpsilon}_1(-1)) 103 | @#for iMoment in 2 : nMeasure 104 | + measureCoefficient_@{iEpsilon}_@{iMoment} * ((quadratureGrid_@{iAssets} - moment_@{iEpsilon}_1(-1)) ^ @{iMoment} - 105 | moment_@{iEpsilon}_@{iMoment}(-1)) 106 | @#endfor 107 | ); 108 | 109 | @#endfor 110 | 111 | // Total mass of distribution 112 | # totalMass_@{iEpsilon} = 0 113 | @#for iAssets in 1 : nAssetsQuadrature 114 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilon}_@{iAssets} 115 | @#endfor 116 | ; 117 | 118 | @#endfor 119 | 120 | //---------------------------------------------------------------- 121 | // Compute various objects at borrowing constraint for integrating distribution 122 | //---------------------------------------------------------------- 123 | 124 | @#for iEpsilon in 1 : nEpsilon 125 | 126 | # assetsPrimeBC_@{iEpsilon} = coefficient_@{iEpsilon}_1; 127 | 128 | @#endfor 129 | 130 | //---------------------------------------------------------------- 131 | // Relationship between moments of distribution and parameters 132 | // (#equations = nEpsilon * nMeasure) 133 | //---------------------------------------------------------------- 134 | 135 | @#for iEpsilon in 1 : nEpsilon 136 | 137 | // First moments (uncentered) 138 | moment_@{iEpsilon}_1(-1) = (0 139 | @#for iAssets in 1 : nAssetsQuadrature 140 | + quadratureWeights_@{iAssets} * quadratureGrid_@{iAssets} * measurePDF_@{iEpsilon}_@{iAssets} 141 | @#endfor 142 | ) / totalMass_@{iEpsilon}; 143 | 144 | // Higher order moments (centered) 145 | @#for iMoment in 2 : nMeasure 146 | moment_@{iEpsilon}_@{iMoment}(-1) = (0 147 | @#for iAssets in 1 : nAssetsQuadrature 148 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilon}_@{iAssets} * ((quadratureGrid_@{iAssets} - 149 | moment_@{iEpsilon}_1(-1)) ^ @{iMoment}) 150 | @#endfor 151 | ) / totalMass_@{iEpsilon}; 152 | 153 | @#endfor 154 | 155 | @#endfor 156 | 157 | //---------------------------------------------------------------- 158 | // Law of motion for density away from borrowing constraint 159 | // (#equations = nEpsilon * nMeasure) 160 | //---------------------------------------------------------------- 161 | 162 | @#for iEpsilon in 1 : nEpsilon 163 | 164 | // First moment (uncentered) 165 | moment_@{iEpsilon}_1 = (0 166 | @#for iEpsilonTilde in 1 : nEpsilon 167 | + ((1 - mHat_@{iEpsilonTilde}(-1)) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} * (0 168 | @#for iAssets in 1 : nAssetsQuadrature 169 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilonTilde}_@{iAssets} * 170 | assetsPrimeQuadrature_@{iEpsilonTilde}_@{iAssets} 171 | @#endfor 172 | ) / totalMass_@{iEpsilonTilde}) + mHat_@{iEpsilonTilde}(-1) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} 173 | * assetsPrimeBC_@{iEpsilonTilde} 174 | @#endfor 175 | ) / epsilonMass_@{iEpsilon}; 176 | 177 | // Higher order moments (uncentered) 178 | @#for iMoment in 2 : nMeasure 179 | moment_@{iEpsilon}_@{iMoment} = (0 180 | @#for iEpsilonTilde in 1 : nEpsilon 181 | + ((1 - mHat_@{iEpsilonTilde}(-1)) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} * (0 182 | @#for iAssets in 1 : nAssetsQuadrature 183 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilonTilde}_@{iAssets} * 184 | (assetsPrimeQuadrature_@{iEpsilonTilde}_@{iAssets} - moment_@{iEpsilon}_1) ^ @{iMoment} 185 | @#endfor 186 | ) / totalMass_@{iEpsilonTilde}) + mHat_@{iEpsilonTilde}(-1) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} 187 | * (assetsPrimeBC_@{iEpsilonTilde} - moment_@{iEpsilon}_1) ^ @{iMoment} 188 | @#endfor 189 | ) / epsilonMass_@{iEpsilon}; 190 | @#endfor 191 | 192 | @#endfor 193 | 194 | //---------------------------------------------------------------- 195 | // Law of motion for mass at borrowing constraint 196 | // (#equations = nEpsilon) 197 | //---------------------------------------------------------------- 198 | 199 | @#for iEpsilon in 1 : nEpsilon 200 | 201 | mHat_@{iEpsilon} = (0 202 | @#for iEpsilonTilde in 1 : nEpsilon 203 | + ((1 - mHat_@{iEpsilonTilde}(-1)) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} * (0 204 | @#for iAssets in 1 : nAssetsQuadrature 205 | + quadratureWeights_@{iAssets} * measurePDF_@{iEpsilonTilde}_@{iAssets} * 206 | (assetsPrimeQuadrature_@{iEpsilonTilde}_@{iAssets} <= aaBar + 1e-8) 207 | @#endfor 208 | ) / totalMass_@{iEpsilonTilde}) + mHat_@{iEpsilonTilde}(-1) * epsilonMass_@{iEpsilonTilde} * epsilonTransition_@{iEpsilonTilde}_@{iEpsilon} 209 | * (assetsPrimeBC_@{iEpsilonTilde} <= aaBar + 1e-8) 210 | @#endfor 211 | ) / epsilonMass_@{iEpsilon}; 212 | 213 | @#endfor 214 | 215 | //---------------------------------------------------------------- 216 | // Factor prices (# equations = 2) 217 | //---------------------------------------------------------------- 218 | 219 | # aggregateCapital = (1 - aggEmployment) * moment_1_1(-1) + aggEmployment * moment_2_1(-1); 220 | 221 | r = exp(aggregateTFP) * aalpha * (aggregateCapital ^ (aalpha - 1)) * (aggEmployment ^ (1 - aalpha)) - ddelta; 222 | w = exp(aggregateTFP) * (aggregateCapital ^ aalpha) * (1 - aalpha) * (aggEmployment ^ (-aalpha)); 223 | 224 | //---------------------------------------------------------------- 225 | // Law of motion for aggregate TFP (# equations = 1) 226 | //---------------------------------------------------------------- 227 | 228 | aggregateTFP = rrhoTFP * aggregateTFP(-1) + ssigmaTFP * aggregateTFPShock; 229 | 230 | //---------------------------------------------------------------- 231 | // Auxiliary variables of interest (# equations = 4) 232 | //---------------------------------------------------------------- 233 | 234 | // Output 235 | logAggregateOutput = log(exp(aggregateTFP) * (aggregateCapital ^ aalpha) * (aggEmployment ^ (1 - aalpha))); 236 | 237 | // Investment 238 | logAggregateInvestment = log((1 - aggEmployment) * moment_1_1 + aggEmployment * moment_2_1 - (1 - ddelta) * aggregateCapital); 239 | 240 | // Consumption 241 | logAggregateConsumption = log(exp(logAggregateOutput) - exp(logAggregateInvestment)); 242 | 243 | // Wage 244 | logWage = log(w); 245 | -------------------------------------------------------------------------------- /Auxiliary Functions/firstOrderDynamics_polynomials.mod: -------------------------------------------------------------------------------- 1 | // Dynare shell which declares model and solves for aggregate dynamics using 2 | // first order approximation (when approximating conditional expectation with 3 | // polynomials) 4 | // 5 | // Thomas Winberry, July 26th, 2016 6 | 7 | //---------------------------------------------------------------- 8 | // Load parameters 9 | //---------------------------------------------------------------- 10 | 11 | @#include "parameters_polynomials.mod" 12 | 13 | //---------------------------------------------------------------- 14 | // Define variables 15 | //---------------------------------------------------------------- 16 | 17 | @#include "variables_polynomials.mod" 18 | 19 | //---------------------------------------------------------------- 20 | // Model equations 21 | //---------------------------------------------------------------- 22 | 23 | model; 24 | 25 | @#include "equations_polynomials.mod" 26 | 27 | end; 28 | 29 | //---------------------------------------------------------------- 30 | // 4. Computation 31 | //---------------------------------------------------------------- 32 | 33 | // Specify shock process 34 | 35 | shocks; 36 | var aggregateTFPShock = 1; 37 | end; 38 | 39 | options_.steadystate.nocheck = 1; 40 | 41 | // Compute steady state (nocheck option ensures that Dynare runs even if steady 42 | // state only computed approximately, i.e., with small numerical error) 43 | //steady(nocheck); 44 | 45 | // Check regularity conditions (turn on to check) 46 | //check; 47 | //model_diagnostics; 48 | //model_info; 49 | 50 | // Simulate 51 | stoch_simul(order=1,hp_filter=100,irf=40) aggregateTFP logAggregateOutput 52 | logAggregateConsumption logAggregateInvestment logWage r; 53 | -------------------------------------------------------------------------------- /Auxiliary Functions/firstOrderDynamics_polynomials_steadystate.m: -------------------------------------------------------------------------------- 1 | function [ys,params,check] = firstOrderDynamics_polynomials_steadyState(ys,exo,M_,options_) 2 | 3 | % Computes stationary equilibrium of the model for Dynare; format is required 4 | % to be called by Dynare (follows example of NK_baseline.mod in Dynare examples) 5 | % 6 | % Thomas Winberry, July 26th, 2016 7 | 8 | tStart = tic; 9 | fprintf('\nComputing steady state...\n') 10 | 11 | %---------------------------------------------------------------- 12 | % Call parameters (the next set of commands will overwrite some) 13 | %---------------------------------------------------------------- 14 | setParameters; 15 | 16 | %---------------------------------------------------------------- 17 | % Read in parameters from Dynare declaration 18 | %---------------------------------------------------------------- 19 | 20 | % Initialize indicator 21 | check = 0; 22 | 23 | % Read parameters from Dynare 24 | 25 | % Read out parameters to access them with their name 26 | for iParameter = 1:M_.param_nbr 27 | paramname = M_.param_names{iParameter,:}; 28 | eval(['global ' paramname]); 29 | eval([ paramname ' = M_.params(' int2str(iParameter) ');']); 30 | end 31 | 32 | %---------------------------------------------------------------- 33 | % Steady State 34 | %---------------------------------------------------------------- 35 | displayOpt = 'off'; % 'iter-detailed' or 'off' 36 | coreSteadyState; 37 | 38 | % Prices 39 | r = aalpha * (aggregateCapital ^ (aalpha - 1)) * (aggEmployment ^ (1 - aalpha)) - ddelta; 40 | w = (aggregateCapital ^ aalpha) * (1 - aalpha) * (aggEmployment ^ (-aalpha)); 41 | 42 | %---------------------------------------------------------------- 43 | % Save values of steady state variables for Dynare (must be exactly 44 | % as declared in Dynare) 45 | %---------------------------------------------------------------- 46 | 47 | % Coefficients on conditional expectation function 48 | for iEpsilon = 1 : nEpsilon 49 | for iAsset = 1 : nAssets 50 | eval(sprintf('expectationCoefficient_%d_%d = mCoefficients(iEpsilon,iAsset);',... 51 | iEpsilon,iAsset)); 52 | end 53 | end 54 | 55 | % Moments and parameters of density away from borrowing constraint 56 | for iEpsilon = 1 : nEpsilon 57 | for iMoment = 1 : nMeasure 58 | eval(sprintf('moment_%d_%d = mMoments(iEpsilon,iMoment);',iEpsilon,iMoment)); 59 | eval(sprintf('measureCoefficient_%d_%d = mParameters(iEpsilon,iMoment+1);',iEpsilon,iMoment)); 60 | end 61 | end 62 | 63 | % Mass at borrowing constraint 64 | for iEpsilon = 1 : nEpsilon 65 | eval(sprintf('mHat_%d = mHat(iEpsilon);',iEpsilon)); 66 | end 67 | 68 | % Other variables 69 | aggregateCapital = (1 - mHat(1,1)) * (1 - aggEmployment) * mMoments(1,1) + (1 - mHat(2,1)) * aggEmployment * mMoments(2,1); 70 | aggregateTFP = 0; 71 | logAggregateOutput = log(exp(aggregateTFP) * (aggregateCapital ^ aalpha) * (aggEmployment ^ (1 - aalpha))); 72 | logAggregateInvestment = log(ddelta * aggregateCapital); 73 | logAggregateConsumption = log(exp(logAggregateOutput) - exp(logAggregateInvestment)); 74 | logWage = log(w); 75 | 76 | params=NaN(M_.param_nbr,1); 77 | for iter = 1:length(M_.params) %update parameters set in the file 78 | eval([ 'params(' num2str(iter) ',1) = ' M_.param_names{iter} ';' ]) 79 | end 80 | % Save endogenous variables back into ys 81 | for ii = 1 : M_.orig_endo_nbr 82 | varname = M_.endo_names{ii,:}; 83 | eval(['ys(' int2str(ii) ',1) = ' varname ';']); 84 | end 85 | 86 | fprintf('... Done! Elapsed time: %2.2f seconds \n\n',toc(tStart)) 87 | -------------------------------------------------------------------------------- /Auxiliary Functions/firstOrderDynamics_splines.mod: -------------------------------------------------------------------------------- 1 | // Dynare shell which declares model and solves for aggregate dynamics using 2 | // first order approximation (when approximating savings decisions with splines) 3 | // 4 | // Thomas Winberry, July 26th, 2016 5 | 6 | //---------------------------------------------------------------- 7 | // Load parameters 8 | //---------------------------------------------------------------- 9 | 10 | @#include "parameters_splines.mod" 11 | 12 | //---------------------------------------------------------------- 13 | // Define variables 14 | //---------------------------------------------------------------- 15 | 16 | @#include "variables_splines.mod" 17 | 18 | //---------------------------------------------------------------- 19 | // Model equations 20 | //---------------------------------------------------------------- 21 | 22 | model; 23 | 24 | @#include "equations_splines.mod" 25 | 26 | end; 27 | 28 | //---------------------------------------------------------------- 29 | // 4. Computation 30 | //---------------------------------------------------------------- 31 | 32 | // Specify shock process 33 | 34 | shocks; 35 | var aggregateTFPShock = 1; 36 | end; 37 | 38 | options.steadystate.nocheck = 1; 39 | 40 | // Compute steady state (nocheck option ensures that Dynare runs even if steady 41 | // state only computed approximately, i.e., with small numerical error) 42 | //steady(nocheck); 43 | 44 | // Check regularity conditions (turn on to check) 45 | //check; 46 | //model_diagnostics; 47 | //model_info; 48 | 49 | // Simulate 50 | stoch_simul(order=1,hp_filter=100,irf=40) aggregateTFP logAggregateOutput 51 | logAggregateConsumption logAggregateInvestment logWage r; 52 | -------------------------------------------------------------------------------- /Auxiliary Functions/firstOrderDynamics_splines_steadystate.m: -------------------------------------------------------------------------------- 1 | function [ys,params,check] = firstOrderDynamics_polynomials_steadyState(ys,exo,M_,options) 2 | 3 | % Computes stationary equilibrium of the model for Dynare; format is required 4 | % to be called by Dynare (follows example of NK_baseline.mod in Dynare examples) 5 | % 6 | % Thomas Winberry, July 26th, 2016 7 | 8 | tStart = tic; 9 | fprintf('\nComputing steady state...\n') 10 | 11 | %---------------------------------------------------------------- 12 | % Call parameters (the next set of commands will overwrite some) 13 | %---------------------------------------------------------------- 14 | setParameters; 15 | 16 | %---------------------------------------------------------------- 17 | % Read in parameters from Dynare declaration 18 | %---------------------------------------------------------------- 19 | 20 | % Initialize indicator 21 | check = 0; 22 | 23 | % Read out parameters to access them with their name 24 | for iParameter = 1:M_.param_nbr 25 | paramname = M_.param_names{iParameter,:}; 26 | eval(['global ' paramname]); 27 | eval([ paramname ' = M_.params(' int2str(iParameter) ');']); 28 | end 29 | 30 | %---------------------------------------------------------------- 31 | % Steady State 32 | %---------------------------------------------------------------- 33 | displayOpt = 'off'; % 'iter-detailed' or 'off' 34 | coreSteadyState; 35 | 36 | % Prices 37 | r = aalpha * (aggregateCapital ^ (aalpha - 1)) * (aggEmployment ^ (1 - aalpha)) - ddelta; 38 | w = (aggregateCapital ^ aalpha) * (1 - aalpha) * (aggEmployment ^ (-aalpha)); 39 | 40 | %---------------------------------------------------------------- 41 | % Save values of steady state variables for Dynare (must be exactly 42 | % as declared in Dynare) 43 | %---------------------------------------------------------------- 44 | 45 | % Coefficients on conditional expectation function 46 | for iEpsilon = 1 : nEpsilon 47 | for iAsset = 1 : nAssets 48 | eval(sprintf('coefficient_%d_%d = mCoefficients(iEpsilon,iAsset);',... 49 | iEpsilon,iAsset)); 50 | end 51 | end 52 | 53 | % Moments and parameters of density away from borrowing constraint 54 | for iEpsilon = 1 : nEpsilon 55 | for iMoment = 1 : nMeasure 56 | eval(sprintf('moment_%d_%d = mMoments(iEpsilon,iMoment);',iEpsilon,iMoment)); 57 | eval(sprintf('measureCoefficient_%d_%d = mParameters(iEpsilon,iMoment+1);',iEpsilon,iMoment)); 58 | end 59 | end 60 | 61 | % Mass at borrowing constraint 62 | for iEpsilon = 1 : nEpsilon 63 | eval(sprintf('mHat_%d = mHat(iEpsilon);',iEpsilon)); 64 | end 65 | 66 | % Other variables 67 | aggregateCapital = (1 - mHat(1,1)) * (1 - aggEmployment) * mMoments(1,1) + (1 - mHat(2,1)) * aggEmployment * mMoments(2,1); 68 | aggregateTFP = 0; 69 | logAggregateOutput = log(exp(aggregateTFP) * (aggregateCapital ^ aalpha) * (aggEmployment ^ (1 - aalpha))); 70 | logAggregateInvestment = log(ddelta * aggregateCapital); 71 | logAggregateConsumption = log(exp(logAggregateOutput) - exp(logAggregateInvestment)); 72 | logWage = log(w); 73 | 74 | params=NaN(M_.param_nbr,1); 75 | for iter = 1:length(M_.params) %update parameters set in the file 76 | eval([ 'params(' num2str(iter) ',1) = ' M_.param_names{iter} ';' ]) 77 | end 78 | 79 | % Save endogenous variables back into ys 80 | for ii = 1 : M_.orig_endo_nbr; 81 | varname = M_.endo_names{ii,:}; 82 | eval(['ys(' int2str(ii) ') = ' varname ';']); 83 | end 84 | 85 | fprintf('... Done! Elapsed time: %2.2f seconds \n\n',toc(tStart)) 86 | -------------------------------------------------------------------------------- /Auxiliary Functions/parametersResidual.m: -------------------------------------------------------------------------------- 1 | function [value vDerivs] = parametersResidual(vParameters,mGridMoments); 2 | 3 | % Computes the objective function and Jacobian for computing the parameters of the distribution, given 4 | % moments to match 5 | % 6 | % Inputs 7 | % (1) vParameters: candidate parameters of distribution (to be optimized over) 8 | % (2) mGridMoments: grid of centralized moments 9 | % 10 | % Outputs 11 | % (1) value: value of objective function 12 | % (2) vDerivs: Jacobian 13 | % 14 | % Thomas Winberry, October 12, 2015 15 | 16 | global assetsMax assetsMin vQuadratureWeights nMeasure 17 | 18 | % Value of function 19 | value = vQuadratureWeights' * exp(mGridMoments * vParameters); 20 | 21 | % Derivatives of function 22 | if nargout > 1 23 | 24 | vDerivs = sum(repmat(vQuadratureWeights,[1 nMeasure]) .* mGridMoments .* repmat(exp(mGridMoments * vParameters),... 25 | [1 nMeasure]),1)'; 26 | 27 | end 28 | 29 | -------------------------------------------------------------------------------- /Auxiliary Functions/parameters_polynomials.mod: -------------------------------------------------------------------------------- 1 | // Declare parameters and load in their values for firstOrderDynamics_polynomials.mod 2 | // 3 | // Thomas Winberry, July 26th, 2016 4 | 5 | //---------------------------------------------------------------- 6 | // Preliminaries 7 | //---------------------------------------------------------------- 8 | 9 | // Load in files containing parameters 10 | economicParameters = load('economicParameters'); 11 | approximationParameters = load('approximationParameters'); 12 | grids = load('grids'); 13 | polynomials = load('polynomials'); 14 | 15 | // Define economic parameters 16 | parameters bbeta ssigma aaBar aalpha ddelta aggEmployment 17 | mmu ttau rrhoTFP ssigmaTFP; 18 | //Load in their values 19 | @#define nEconomicParameters = 10 20 | for iParam = 1 : @{nEconomicParameters} 21 | parameterName = M_.param_names{iParam,:}; 22 | if isfield(economicParameters,parameterName) 23 | M_.params(iParam) = eval(['economicParameters.' parameterName]); 24 | end 25 | end 26 | 27 | // Epsilon transition matrix 28 | @#for iEpsilon in 1 : 2 29 | @#for iEpsilonPrime in 1 : 2 30 | parameters epsilonTransition_@{iEpsilon}_@{iEpsilonPrime}; 31 | @#endfor 32 | @#endfor 33 | for iEpsilon = 1 : 2 34 | for iEpsilonPrime = 1 : 2 35 | M_.params(@{nEconomicParameters} + 2 * (iEpsilon - 1) + iEpsilonPrime) = ... 36 | economicParameters.mEpsilonTransition(iEpsilon,iEpsilonPrime); 37 | end 38 | end 39 | 40 | // Mass of invariant distrbution of idiosyncratic shocks 41 | parameters epsilonMass_1 epsilonMass_2; 42 | epsilonMass_1 = 1 - aggEmployment; 43 | epsilonMass_2 = aggEmployment; 44 | 45 | // Define some of the approximation parameters 46 | parameters nEpsilon nAssets nState assetsMin assetsMax nAssetsFine nStateFine nAssetsQuadrature nStateQuadrature 47 | nMeasure nMeasureCoefficients kRepSS maxIterations tolerance dampening; 48 | // Load in their values 49 | @#define nApproximationParameters = 15 50 | for iParam = 1 : @{nApproximationParameters} 51 | parameterName = M_.param_names{@{nEconomicParameters} + 6 + iParam,:}; 52 | if isfield(approximationParameters,parameterName) 53 | M_.params(@{nEconomicParameters} + 6 + iParam) = eval(['approximationParameters.' parameterName]); 54 | end 55 | end 56 | 57 | @#define nCounter = nEconomicParameters + 6 + nApproximationParameters 58 | 59 | // Have to impose parameters used as counters directly 60 | @#define nEpsilon = 2 61 | @#define nAssets = 25 62 | @#define nMeasure = 3 63 | @#define nAssetsQuadrature = 8 64 | 65 | @#define nState = nEpsilon * nAssets 66 | @#define nStateQuadrature = nEpsilon * nAssetsQuadrature 67 | 68 | // 69 | //---------------------------------------------------------------- 70 | // Grids for approximating conditional expectation 71 | //---------------------------------------------------------------- 72 | 73 | // 74 | // Employment 75 | // 76 | 77 | // Define the grids 78 | parameters epsilonGrid_1 epsilonGrid_2; 79 | 80 | // Assign values 81 | epsilonGrid_1 = 0; 82 | epsilonGrid_2 = 1; 83 | 84 | @#define nCounter = nCounter + 2 85 | 86 | // 87 | // Assets 88 | // 89 | 90 | // Define the grids 91 | @#for iAssets in 1 : nAssets 92 | parameters assetsGrid_@{iAssets}; 93 | @#endfor 94 | 95 | // Assign values (must be in the same order that the parameters were declared) 96 | for iAssets = 1 : @{nAssets} 97 | M_.params(@{nCounter} + iAssets) = grids.vAssetsGrid(iAssets); 98 | end 99 | 100 | // Update counter for future parameter assignments 101 | @#define nCounter = nCounter + nAssets 102 | 103 | //---------------------------------------------------------------- 104 | // Quadrature grid and weights 105 | //---------------------------------------------------------------- 106 | 107 | // Define the parameters 108 | @#for iAssets in 1 : nAssetsQuadrature 109 | parameters quadratureGrid_@{iAssets}; 110 | parameters quadratureWeights_@{iAssets}; 111 | @#endfor 112 | 113 | // Assign values 114 | for iAssets = 1 : @{nAssetsQuadrature} 115 | M_.params(@{nCounter} + 2 * (iAssets - 1) + 1) = grids.vAssetsGridQuadrature(iAssets); 116 | M_.params(@{nCounter} + 2 * (iAssets - 1) + 2) = grids.vQuadratureWeights(iAssets); 117 | end 118 | 119 | @#define nCounter = nCounter + 2 * nAssetsQuadrature 120 | 121 | //---------------------------------------------------------------- 122 | // Conditional expectation polynomials 123 | //---------------------------------------------------------------- 124 | 125 | // 126 | // Chebyshev polynomials 127 | // 128 | 129 | // Define the parameters 130 | @#for iAssets in 1 : nAssets 131 | @#for iPower in 1 : nAssets 132 | parameters expectationPoly_@{iAssets}_@{iPower}; 133 | @#endfor 134 | @#endfor 135 | 136 | // Assign values 137 | for iAssets = 1 : @{nAssets} 138 | for iPower = 1 : @{nAssets} 139 | M_.params(@{nCounter} + @{nAssets} * (iAssets - 1) + iPower) = polynomials.vAssetsPoly(iAssets,iPower); 140 | end 141 | end 142 | 143 | @#define nCounter = nCounter + nAssets * nAssets 144 | 145 | // 146 | // Squared terms in Chebyshev interpolation 147 | // 148 | 149 | // Define the parameters 150 | @#for iAssets in 1 : nAssets 151 | parameters expectationPolySquared_@{iAssets}; 152 | @#endfor 153 | 154 | // Assign the values 155 | for iAssets = 1 : @{nAssets} 156 | M_.params(@{nCounter} + iAssets) = polynomials.vAssetsPolySquared(iAssets); 157 | end 158 | 159 | @#define nCounter = nCounter + nAssets 160 | 161 | //---------------------------------------------------------------- 162 | // Quadrature grid polynomials 163 | //---------------------------------------------------------------- 164 | 165 | // Define the parameters 166 | @#for iAssets in 1 : nAssetsQuadrature 167 | @#for iPower in 1 : nAssets 168 | parameters quadraturePoly_@{iAssets}_@{iPower}; 169 | @#endfor 170 | @#endfor 171 | 172 | // Assign values 173 | for iAssets = 1 : @{nAssetsQuadrature} 174 | for iPower = 1 : @{nAssets} 175 | M_.params(@{nCounter} + @{nAssets} * (iAssets - 1) + iPower) = polynomials.vAssetsPolyQuadrature(iAssets,iPower); 176 | end 177 | end 178 | 179 | @#define nCounter = nCounter + nAssetsQuadrature * nAssets 180 | 181 | //---------------------------------------------------------------- 182 | // Borrowing constraint polynomials 183 | //---------------------------------------------------------------- 184 | 185 | // Define the parameters 186 | @#for iPower in 1 : nAssets 187 | parameters bcPoly_@{iPower}; 188 | @#endfor 189 | 190 | // Assign values 191 | for iPower = 1 : @{nAssets} 192 | M_.params(@{nCounter} + iPower) = polynomials.vAssetsPolyBC(iPower); 193 | end -------------------------------------------------------------------------------- /Auxiliary Functions/parameters_splines.mod: -------------------------------------------------------------------------------- 1 | // Declare parameters and load in their values for firstOrderDynamics.mod 2 | // 3 | // Thomas Winberry, July 26th, 2016 4 | 5 | //---------------------------------------------------------------- 6 | // Preliminaries 7 | //---------------------------------------------------------------- 8 | 9 | // Load in files containing parameters 10 | economicParameters = load('economicParameters'); 11 | approximationParameters = load('approximationParameters'); 12 | grids = load('grids'); 13 | // polynomials = load('polynomials'); 14 | 15 | // Define economic parameters 16 | parameters bbeta ssigma aaBar aalpha ddelta aggEmployment 17 | mmu ttau rrhoTFP ssigmaTFP; 18 | //Load in their values 19 | @#define nEconomicParameters = 10 20 | for iParam = 1 : @{nEconomicParameters} 21 | parameterName = M_.param_names{iParam,:}; 22 | if isfield(economicParameters,parameterName) 23 | M_.params(iParam) = eval(['economicParameters.' parameterName]); 24 | end 25 | end 26 | 27 | // Epsilon transition matrix 28 | @#for iEpsilon in 1 : 2 29 | @#for iEpsilonPrime in 1 : 2 30 | parameters epsilonTransition_@{iEpsilon}_@{iEpsilonPrime}; 31 | @#endfor 32 | @#endfor 33 | for iEpsilon = 1 : 2 34 | for iEpsilonPrime = 1 : 2 35 | M_.params(@{nEconomicParameters} + 2 * (iEpsilon - 1) + iEpsilonPrime) = ... 36 | economicParameters.mEpsilonTransition(iEpsilon,iEpsilonPrime); 37 | end 38 | end 39 | 40 | // Mass of invariant distrbution of idiosyncratic shocks 41 | parameters epsilonMass_1 epsilonMass_2; 42 | epsilonMass_1 = 1 - aggEmployment; 43 | epsilonMass_2 = aggEmployment; 44 | 45 | // Define some of the approximation parameters 46 | parameters nEpsilon nAssets nState assetsMin assetsMax nAssetsFine nStateFine nAssetsQuadrature nStateQuadrature 47 | nMeasure nMeasureCoefficients kRepSS maxIterations tolerance dampening; 48 | // Load in their values 49 | @#define nApproximationParameters = 15 50 | for iParam = 1 : @{nApproximationParameters} 51 | parameterName = M_.param_names{@{nEconomicParameters} + 6 + iParam,:}; 52 | if isfield(approximationParameters,parameterName) 53 | M_.params(@{nEconomicParameters} + 6 + iParam) = eval(['approximationParameters.' parameterName]); 54 | end 55 | end 56 | 57 | @#define nCounter = nEconomicParameters + 6 + nApproximationParameters 58 | 59 | // Have to impose parameters used as counters directly 60 | @#define nEpsilon = 2 61 | @#define nAssets = 25 62 | @#define nMeasure = 3 63 | @#define nAssetsQuadrature = 8 64 | 65 | @#define nState = nEpsilon * nAssets 66 | @#define nStateQuadrature = nEpsilon * nAssetsQuadrature 67 | 68 | // 69 | //---------------------------------------------------------------- 70 | // Grids for approximating conditional expectation 71 | //---------------------------------------------------------------- 72 | 73 | // 74 | // Employment 75 | // 76 | 77 | // Define the grids 78 | parameters epsilonGrid_1 epsilonGrid_2; 79 | 80 | // Assign values 81 | epsilonGrid_1 = 0; 82 | epsilonGrid_2 = 1; 83 | 84 | @#define nCounter = nCounter + 2 85 | 86 | // 87 | // Assets 88 | // 89 | 90 | // Define the grids 91 | @#for iAssets in 1 : nAssets 92 | parameters assetsGrid_@{iAssets}; 93 | @#endfor 94 | 95 | // Assign values (must be in the same order that the parameters were declared) 96 | for iAssets = 1 : @{nAssets} 97 | M_.params(@{nCounter} + iAssets) = grids.vAssetsGrid(iAssets); 98 | end 99 | 100 | // Update counter for future parameter assignments 101 | @#define nCounter = nCounter + nAssets 102 | 103 | //---------------------------------------------------------------- 104 | // Quadrature grid and weights 105 | //---------------------------------------------------------------- 106 | 107 | // Define the parameters 108 | @#for iAssets in 1 : nAssetsQuadrature 109 | parameters quadratureGrid_@{iAssets}; 110 | parameters quadratureWeights_@{iAssets}; 111 | @#endfor 112 | 113 | // Assign values 114 | for iAssets = 1 : @{nAssetsQuadrature} 115 | M_.params(@{nCounter} + 2 * (iAssets - 1) + 1) = grids.vAssetsGridQuadrature(iAssets); 116 | M_.params(@{nCounter} + 2 * (iAssets - 1) + 2) = grids.vQuadratureWeights(iAssets); 117 | end 118 | 119 | @#define nCounter = nCounter + 2 * nAssetsQuadrature 120 | -------------------------------------------------------------------------------- /Auxiliary Functions/scaleDown.m: -------------------------------------------------------------------------------- 1 | function z = scaleDown(x,xmin,xmax) 2 | 3 | % This function maps x in [xmin,xmax] into z in [-1,1]; inverse of scale_up 4 | % 5 | % Inputs 6 | % (1) x: array with elements in [xmin,xmax]; if outside range, will force 7 | % (2) xmin: scalar lower bound of domain 8 | % (3) xmax: scalar upper bound of domain 9 | % 10 | % Outputs 11 | % (1) z: array with elements in [-1,1] 12 | % 13 | % Thomas Winberry, January 19, 2016 14 | 15 | z = min(max(2 * ((x - xmin) / (xmax - xmin)) - 1,-1 * ones(size(x))),ones(size(x))); -------------------------------------------------------------------------------- /Auxiliary Functions/scaleUp.m: -------------------------------------------------------------------------------- 1 | function x = scaleUp(z,xmin,xmax) 2 | 3 | % This function maps z in [-1,1] into x in [xmin,xmax]; inverse of scale_down 4 | % 5 | % Inputs 6 | % (1) z: array with elements in [-1,1]; if outside range, will force 7 | % (2) xmin: scalar lower bound of range 8 | % (3) xmax: scalar upper bound of range 9 | % 10 | % Outputs 11 | % (1) x: array with elements in [xmin,xmax] 12 | % 13 | % Thomas Winberry, January 19, 2016 14 | 15 | x = min(max((.5 * (z + 1) * (xmax - xmin)) + xmin,xmin * ones(size(z))),... 16 | xmax * ones(size(z))); 17 | -------------------------------------------------------------------------------- /Auxiliary Functions/setParameters.m: -------------------------------------------------------------------------------- 1 | % Sets parameter values 2 | % 3 | % Thomas Winberry, July 26th, 2016 4 | 5 | %---------------------------------------------------------------- 6 | % Set economic parameters 7 | %---------------------------------------------------------------- 8 | 9 | global bbeta ssigma aaBar aalpha ddelta vEpsilonGrid mEpsilonTransition vEpsilonInvariant aggEmployment ... 10 | mmu ttau rrhoTFP ssigmaTFP 11 | 12 | % Preferences 13 | bbeta = .96; % discount factor (annual calibration) 14 | ssigma = 1; % coefficient of relative risk aversion 15 | aaBar = 0; % borrowing constraint 16 | 17 | % Technology 18 | aalpha = .36; % capital share 19 | ddelta = .1; % depreciation rate (annual calibration) 20 | 21 | % Idioynscratic Shocks 22 | vEpsilonGrid = [0;1]; 23 | aggEmployment = .93; uDuration = 1; 24 | mEpsilonTransition = [uDuration / (1 + uDuration), 1 - (uDuration / (1 + uDuration));... 25 | ((1 - aggEmployment) / aggEmployment) * (1 - (uDuration / (1 + uDuration))),... 26 | 1 - ((1 - aggEmployment) / aggEmployment) * (1 - (uDuration / (1 + uDuration)))]; 27 | vEpsilonInvariant = [1 - aggEmployment;aggEmployment]; 28 | 29 | % Unemployment benefits 30 | mmu = .15; 31 | ttau = mmu * (1 - aggEmployment) / aggEmployment; 32 | 33 | % Aggregate Shocks 34 | rrhoTFP = .859; 35 | ssigmaTFP = .014; 36 | 37 | %---------------------------------------------------------------- 38 | % Set approximation parameters 39 | %---------------------------------------------------------------- 40 | 41 | global nEpsilon nAssets nState assetsMin assetsMax nAssetsFine nStateFine nAssetsQuadrature nStateQuadrature ... 42 | nMeasure nMeasureCoefficients kRepSS maxIterations tolerance dampening splineOpt displayOpt 43 | 44 | % Whether approximating decision rule with splines or polynomials 45 | splineOpt = 0; % if splineOpt = 1, use splines to approximate savings policy; if splineOpt = 0, use polynomials 46 | % to approximate conditional expectation function 47 | 48 | % Whether to print out results from steady state computation 49 | displayOpt = 'iter-detailed'; % 'iter-detailed' or 'off' 50 | 51 | % Order of approximation 52 | nEpsilon = 2; 53 | nAssets = 25; % number of gridpoints in spline approximation or polynomials in polynomial approximation 54 | nState = nEpsilon * nAssets; 55 | 56 | % Bounds on grid space 57 | kRepSS = ((aalpha * (aggEmployment ^ (1 - aalpha))) / ((1 / bbeta) - (1 - ddelta))) ^ (1 / (1 - aalpha)); 58 | assetsMin = aaBar; assetsMax = 3 * kRepSS; 59 | 60 | % Finer grid for analyzing policy functions 61 | nAssetsFine = 100; 62 | nStateFine = nEpsilon * nAssetsFine; 63 | 64 | % Approximation of distribution 65 | nMeasure = 3; 66 | nAssetsQuadrature = 8; 67 | nStateQuadrature = nEpsilon * nAssetsQuadrature; 68 | nMeasureCoefficients = nEpsilon * nMeasure; 69 | 70 | % Iteration on individual decisions 71 | maxIterations = 2e4; 72 | tolerance = 1e-5; 73 | dampening = .95; -------------------------------------------------------------------------------- /Auxiliary Functions/updateCoefficients_polynomials.m: -------------------------------------------------------------------------------- 1 | function mCoefficientsNew = updateCoefficients_polynomials(mCoefficients) 2 | 3 | % Updates polynomial coefficients approximating the conditional expectation function in steady state 4 | % 5 | % Inputs 6 | % (1) mCoefficients: nEpsilon x nAssets matrix, storing previous iteration's coefficients 7 | % 8 | % Outputs 9 | % (1) mCoefficientsNew: nEpsilon x nAssets matrix, storing updated coefficients 10 | % 11 | % Thomas Winberry, January 19, 2016 12 | 13 | % Declare global variables 14 | global bbeta ssigma aalpha ddelta eepsilonBar rrhoEpsilon ssigmaEpsilon aaBar aggEmployment mmu ttau mEpsilonTransition vEpsilonGrid ... 15 | nEpsilon nAssets nState epsilonMin epsilonMax assetsMin assetsMax ... 16 | vAssetsGridZeros vAssetsGrid vAssetsGridHistogram mEpsilonGrid mAssetsGrid ... 17 | vAssetsGridHistogramZeros vAssetsPoly vAssetsPolySquared mAssetsPolyHistogram w r mEpsilonPrimeGrid 18 | 19 | %--------------------------------------------------------------- 20 | % Compute current period's savings policy function 21 | %--------------------------------------------------------------- 22 | 23 | % Compute conditional expectation 24 | mConditionalExpectation = exp(mCoefficients * vAssetsPoly'); 25 | 26 | % Compute target saving 27 | mAssetsPrimeStar = w * (mmu * (1 - mEpsilonGrid) + (1 - ttau) * mEpsilonGrid) + (1 + r) * mAssetsGrid - ... 28 | (mConditionalExpectation .^ (-1 / ssigma)); 29 | 30 | % Compute actual saving 31 | mAssetsPrime = max(mAssetsPrimeStar,aaBar * ones(nEpsilon,nAssets)); 32 | mAssetsPrimeGrid = repmat(reshape(mAssetsPrime,1,nState),[nEpsilon 1]); 33 | 34 | % Compute next period's polynomials 35 | mAssetsPrimeZeros = scaleDown(mAssetsPrime,assetsMin,assetsMax); 36 | mPolyAssetsPrime = computeChebyshev(nAssets,reshape(mAssetsPrimeZeros,nState,1)); 37 | 38 | %--------------------------------------------------------------- 39 | % Compute next period's savings policy function 40 | %--------------------------------------------------------------- 41 | 42 | % Compute conditional expectation 43 | mConditionalExpectationPrime = exp(mCoefficients * mPolyAssetsPrime'); 44 | 45 | % Compute target saving 46 | mAssetsPrimePrimeStar = w * (mmu * (1 - mEpsilonPrimeGrid) + (1 - ttau) * mEpsilonPrimeGrid) + (1 + r) * mAssetsPrimeGrid - ... 47 | (mConditionalExpectationPrime .^ (-1 / ssigma)); 48 | 49 | % Compute actual savings 50 | mAssetsPrimePrimeGrid = max(mAssetsPrimePrimeStar,aaBar*ones(nEpsilon,nEpsilon*nAssets)); 51 | 52 | %--------------------------------------------------------------- 53 | % Update conditional expectation function 54 | %--------------------------------------------------------------- 55 | 56 | % Compute new conditional expectation function 57 | mConsumptionPrime = w * (mmu * (1 - mEpsilonPrimeGrid) + (1 - ttau) * mEpsilonPrimeGrid) + (1 + r) * ... 58 | mAssetsPrimeGrid - mAssetsPrimePrimeGrid; 59 | aConditionalExpectationTilde = reshape(bbeta * mEpsilonTransition * ((1 + r) * (mConsumptionPrime .^ (-ssigma))),... 60 | nEpsilon,nEpsilon,nAssets); 61 | 62 | % Extract the relevant entries 63 | mConditionalExpectation = zeros(nEpsilon,nAssets); 64 | for iEpsilon = 1:nEpsilon 65 | mConditionalExpectation(iEpsilon,:) = aConditionalExpectationTilde(iEpsilon,iEpsilon,:); 66 | end 67 | 68 | % Update the coefficients 69 | mCoefficientsNew = zeros(nEpsilon,nAssets); 70 | for iEpsilon = 1:nEpsilon 71 | vCoefficients = sum(vAssetsPoly' .* (ones(nAssets,1) * log(mConditionalExpectation(iEpsilon,:))),2); 72 | mCoefficientsNew(iEpsilon,:) = (vCoefficients ./ vAssetsPolySquared)'; 73 | end 74 | 75 | -------------------------------------------------------------------------------- /Auxiliary Functions/updateCoefficients_splines.m: -------------------------------------------------------------------------------- 1 | function mAssetsPrimeNew = updateCoefficients_splines(mAssetsPrime) 2 | 3 | % Updates linear splines approximating savings decision in steady state 4 | % 5 | % Inputs 6 | % (1) mAssetsPrime: nEpsilon x nAssets matrix, storing previous iteration's decision along grid 7 | % 8 | % Outputs 9 | % (1) mAssetsPrimeNew: nEpsilon x nAssets matrix, storing updated decisions 10 | % 11 | % Thomas Winberry and Alp Tuncay, July 26th, 2016 12 | 13 | % Declare global variables 14 | global bbeta ssigma aalpha ddelta eepsilonBar rrhoEpsilon ssigmaEpsilon aaBar aggEmployment mmu ttau mEpsilonTransition vEpsilonGrid ... 15 | nEpsilon nAssets nState epsilonMin epsilonMax assetsMin assetsMax nAssetsFine mEpsilonPrimeGridFine mEpsilonGridFine mConsumption ... 16 | vAssetsGrid vAssetsGridHistogram mEpsilonGrid mAssetsGrid vAssetsGridFine mAssetsGridFine... 17 | w r mEpsilonPrimeGrid vIndicesAbove vIndicesBelow vWeightAbove vWeightBelow 18 | 19 | %--------------------------------------------------------------- 20 | % Compute savings policy next period (mAssetsPrimePrime) 21 | %--------------------------------------------------------------- 22 | 23 | % Compute weights and indices for linear interpolation 24 | [vIndicesBelow,vIndicesAbove,vWeightBelow,vWeightAbove] = computeLinearWeights(vAssetsGrid,mAssetsPrime(:)); 25 | 26 | % Compute interpolation for each realization of epsilon 27 | mAssetsPrimePrime = mAssetsPrime(:,vIndicesBelow) .* repmat(vWeightBelow',nEpsilon,1) + ... 28 | mAssetsPrime(:,vIndicesAbove) .* repmat(vWeightAbove',nEpsilon,1); 29 | 30 | %--------------------------------------------------------------- 31 | % Compute updated savings policy 32 | %--------------------------------------------------------------- 33 | 34 | % Compute consumption in next period using mAssetsPrimePrime and budget constraint 35 | mConsumptionPrime = w * (mmu * (1 - mEpsilonPrimeGrid) + (1 - ttau) * mEpsilonPrimeGrid) + (1 + r) * ... 36 | repmat(mAssetsPrime(:)',nEpsilon,1) - mAssetsPrimePrime; 37 | 38 | % Compute savings policy from Euler Equation(i.e. assuming not borrowing constrained) 39 | mConsumption = reshape((sum(repmat(mEpsilonTransition',1,nAssets) .* mConsumptionPrime .^ (-ssigma),1) * ... 40 | bbeta * (1 + r)) .^ (-1 / ssigma),nEpsilon,nAssets); 41 | mAssetsPrimeNewStar = (w * (mmu * (1 - mEpsilonGrid) + (1 - ttau) * mEpsilonGrid) + ... 42 | (1 + r) * mAssetsGrid) - mConsumption; 43 | 44 | % Enforce borrowing constraint 45 | mAssetsPrimeNew = max(mAssetsPrimeNewStar,aaBar * ones(nEpsilon,nAssets)); -------------------------------------------------------------------------------- /Auxiliary Functions/variables_polynomials.mod: -------------------------------------------------------------------------------- 1 | // Declare variables for firstOrderDynamics.mod 2 | // 3 | // Thomas Winberry, July 26th, 2016 4 | 5 | //---------------------------------------------------------------- 6 | // Conditional expectation coefficients 7 | //---------------------------------------------------------------- 8 | 9 | @#for iPower in 1 : nAssets 10 | var expectationCoefficient_1_@{iPower} expectationCoefficient_2_@{iPower}; 11 | @#endfor 12 | 13 | //---------------------------------------------------------------- 14 | // Density of households away from borrowing constraint 15 | //---------------------------------------------------------------- 16 | 17 | // Moments of the distribution 18 | @#for iMoment in 1 : nMeasure 19 | var moment_1_@{iMoment} moment_2_@{iMoment}; 20 | @#endfor 21 | 22 | // Parameters of the distribution 23 | @#for iParameter in 1 : nMeasure 24 | var measureCoefficient_1_@{iParameter} measureCoefficient_2_@{iParameter}; 25 | @#endfor 26 | 27 | //---------------------------------------------------------------- 28 | // Mass at borrowing constraint 29 | //---------------------------------------------------------------- 30 | 31 | var mHat_1 mHat_2; 32 | 33 | //---------------------------------------------------------------- 34 | // Prices 35 | //---------------------------------------------------------------- 36 | 37 | var r w; 38 | 39 | //---------------------------------------------------------------- 40 | // Aggregate TFP 41 | //---------------------------------------------------------------- 42 | 43 | var aggregateTFP; 44 | 45 | //---------------------------------------------------------------- 46 | // Auxiliary variables we're interested in 47 | //---------------------------------------------------------------- 48 | 49 | var logAggregateOutput logAggregateInvestment logAggregateConsumption logWage; 50 | 51 | //---------------------------------------------------------------- 52 | // Shocks 53 | //---------------------------------------------------------------- 54 | 55 | varexo aggregateTFPShock; -------------------------------------------------------------------------------- /Auxiliary Functions/variables_splines.mod: -------------------------------------------------------------------------------- 1 | // Declare variables for firstOrderDynamics.mod 2 | // 3 | // Thomas Winberry, July 26th, 2016 4 | 5 | //---------------------------------------------------------------- 6 | // Coefficients on linear spline approximating savings policy 7 | //---------------------------------------------------------------- 8 | 9 | @#for iCoefficient in 1 : nAssets 10 | var coefficient_1_@{iCoefficient} coefficient_2_@{iCoefficient}; 11 | @#endfor 12 | 13 | //---------------------------------------------------------------- 14 | // Density of households away from borrowing constraint 15 | //---------------------------------------------------------------- 16 | 17 | // Moments of the distribution 18 | @#for iMoment in 1 : nMeasure 19 | var moment_1_@{iMoment} moment_2_@{iMoment}; 20 | @#endfor 21 | 22 | // Parameters of the distribution 23 | @#for iParameter in 1 : nMeasure 24 | var measureCoefficient_1_@{iParameter} measureCoefficient_2_@{iParameter}; 25 | @#endfor 26 | 27 | //---------------------------------------------------------------- 28 | // Mass at borrowing constraint 29 | //---------------------------------------------------------------- 30 | 31 | var mHat_1 mHat_2; 32 | 33 | //---------------------------------------------------------------- 34 | // Prices 35 | //---------------------------------------------------------------- 36 | 37 | var r w; 38 | 39 | //---------------------------------------------------------------- 40 | // Aggregate TFP 41 | //---------------------------------------------------------------- 42 | 43 | var aggregateTFP; 44 | 45 | //---------------------------------------------------------------- 46 | // Auxiliary variables we're interested in 47 | //---------------------------------------------------------------- 48 | 49 | var logAggregateOutput logAggregateInvestment logAggregateConsumption logWage; 50 | 51 | //---------------------------------------------------------------- 52 | // Shocks 53 | //---------------------------------------------------------------- 54 | 55 | varexo aggregateTFPShock; -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | This repository contains an updated version of the original toolkit to be compatible with Dynare 5.x. The 2 | original readme follows below. 3 | 4 | This archive contains MATLAB and DYNARE software for solving the Krusell and Smith (1998) model, using 5 | the method developed in "A Toolbox for Solving and Estimating Heterogeneous Agent Macro Models" by Thomas 6 | Winberry. It also contains the PDF file "dynareUserGruide.pdf" explaining how the codes work, and how to 7 | apply them to other heterogeneous agent models. 8 | 9 | Thomas Winberry, July 27th, 2016. Please email me at Thomas.Winberry@chicagobooth.edu with any feedback, 10 | questions, or bug reports. If you use these codes, please cite: Winberry (2016), "A Toolbox for Solving 11 | and Estimating Heterogeneous Agent Macro Models." 12 | 13 | 14 | The following items are included: 15 | 16 | -------------------------------------------------------------------------------------------------------- 17 | 1. MATLAB software for computing and analyzing the stationary equilibrium of the model with no aggregate 18 | shocks 19 | 20 | "steadyState.m" 21 | 22 | This script will solve for the steady state of the model and plot decision rules and the stationary 23 | distribution. It has two options for computing individual decisions: (1) approximate the savings 24 | policy rule a'(epsilon,a) using linear splines or (2) approximate the conditional expectation of 25 | next period's consumption using Chebyshev polynomials (see user guide for details). The polynomial 26 | approximation is faster in this particular model, but I include them both as a template for other 27 | models as well (in some cases, splines may be necessary due to strong kinks). 28 | 29 | The script calls subroutines: 30 | 31 | a. "set Parameters.m": sets parameters. This includes both parameters of the economic model, and 32 | parameters controlling the approximation of the model. Importantly, set splineOpt = 1 to 33 | approximate the savings decision using linear splines or set splineOpt = 0 to approximate 34 | the conditional expectation using polynomials. 35 | b. "coreSteadyState.m": solves for the market-clearing aggregate capital stock by solving the 36 | market clearing condition. 37 | c. "scaleUp.m": transforms [-1,1] to [a,b]; used for computing Chebyshev polynomials 38 | d. "scaleDown.m": inverse of scaleUp.m 39 | e. "computeChebyshev.m": compute Chebyshev polynomials over a pre-defined grid 40 | f. "computeGaussLegendreQuadrature": compute weights and nodes for Gauss-Legendre 41 | quadrature; used to integrate the parametric family 42 | g. "computeGrids.m": script which computes various grids used in approximation 43 | h. "computePolynomials.m": script which computes various polynomials used in approximation (only used 44 | if splineOpt = 0) 45 | i. "updateCoefficients_polynomials.m": updates polynomial coefficients of conditional expectation function, 46 | which is used to approximate individual decisions (only used if splineOpt = 0) 47 | j. "updateCoefficients_splines.m": updates coefficients of linear splines of savings function, which 48 | is used to approximate individual decisions (only used if splineOpt = 1) 49 | k. "computeMCResidualHistogram.m": computes the residual of the market clearing condition that capital 50 | supply (from integrating household savings decisions) equals capital demand (from the representative 51 | firm's first order condition). Approximates the distribution using a nonparametric histogram and 52 | serves as initial guess for later approximating the distribution using the parametric family of 53 | exponential polynomials. 54 | l. "computeMCResidualPolynomials.m": does the same as computeMCResidualHistogram.m, but approximates the 55 | distribution using the parametric family of exponential polynomials. 56 | m. "parametersResidual.m": evaluates the objective function in the minimization problem defining the 57 | parameters of the distribution, given moments. Used in computeMCResidualPolynomials.m. 58 | 59 | 60 | -------------------------------------------------------------------------------------------------------- 61 | 2. MATLAB and DYNARE software for computing and analyzing the dynamics of the model with aggregate 62 | shocks 63 | 64 | "dynamics.m" 65 | 66 | This script solves for the first order approximation of the model's dynamics. You should be able to run only 67 | this script and everything else is done automatically. As with the steady state 68 | codes, it has two options for computing individual decisions: (1) approximate the savings 69 | policy rule a'(epsilon,a) using linear splines or (2) approximate the conditional expectation of 70 | next period's consumption using Chebyshev polynomials. 71 | 72 | The files are meant to provide a template for how to solve other models using the method in Dynare as well. 73 | They are liberally commented, and I also provide some additional information in the descriptions of the sub- 74 | routines below. 75 | 76 | In addition to the subroutines described above, the script calls: 77 | 78 | a. "firstOrderDynamics_polynomials.mod" or "firstOrderDynamics_splines.mod": Dynare .mod shell which calls other 79 | files and computes the solution (_polynomials is called if splineOpt = 0 and _splines is called if splineOpt = 1). 80 | @# signs indicate a macro-processor command (see Dynare documentation). NB: Dynare will not work unless 81 | steady(nocheck) is specified; otherwise, the steady state equilibrium conditions are only approximately satisfied, 82 | which causes Dynare to shut down. It is good practice in debugging to first check that these residuals are 83 | approximately zero. 84 | b. "parameters_polynomials.mod" or "parameters_splines.mod": declares and loads in parameter values. NB: this includes 85 | the grids and if, applicable, polynomials for approximation. The method treats these as parameters in Dynare because 86 | they are not endogeneous variables in the model. "solveDynamicsFirstOrder.m" solves the grids and polynomials in 87 | a .mat structure which is read into Dynare in this file. 88 | !!!NB: some of the approximation parameters (e.g., the size of the grids) must also be set here, in addition to in 89 | set_parameters.m!!!! 90 | c. "variables_polynomials.mod" or "variables_splines.mod": declares the variables in the model. This includes the coefficients 91 | on individual decisions (either polynomials or splines, depending on splineOpt), the parameters and moments of the 92 | distribution, the mass of households at the borrowing constraint, prices, aggregate TFP, and aggregate output, 93 | investment, and consumption. Dynare solves for the dynamics of all of these variables. 94 | d. "equations_polynomials.mod" or "equations_splines.mod:" defines the equilibrium conditions of the model. NB: compared to 95 | a typical Dynare file for a representative agent model, this file libearlly uses the macro-processor (macro-processor 96 | commands are preceeded by @#; see the Dynare documentation for details). This allows the user to write loops in Dynare, 97 | which are useful in characterizing individual decisions and the distribution because one can loop over the gridpoints 98 | in the individual state space. 99 | e. "firstOrderDynamics_polynomials_steadystate.m" or "firstOrderDynamics_splines_steadystate.m": an external Matlab .m file 100 | which computes the model's steady state. It performs essentially the same commands as steadyState.m, but is 101 | in the form required to be called by Dynare. 102 | -------------------------------------------------------------------------------- /dynamics.m: -------------------------------------------------------------------------------- 1 | % Shell which declares parameters and calls Dynare to solve model using 2 | % first order approximation of aggregate dynamics 3 | % 4 | % Thomas Winberry, July 26th, 2016 5 | 6 | clear all 7 | close all 8 | clc 9 | 10 | cd('./Auxiliary Functions'); 11 | 12 | %---------------------------------------------------------------- 13 | % Set parameters 14 | %---------------------------------------------------------------- 15 | setParameters; 16 | 17 | %---------------------------------------------------------------- 18 | % Compute approximation tools 19 | %---------------------------------------------------------------- 20 | 21 | % Grids 22 | computeGrids; 23 | 24 | % Polynomials over grids (only if using polynomials to approximate conditional expectation) 25 | if splineOpt == 0 26 | computePolynomials; 27 | end 28 | 29 | %---------------------------------------------------------------- 30 | % Save parameters in .mat files to import into Dynare 31 | %---------------------------------------------------------------- 32 | 33 | if splineOpt == 0 % if using polynomials to approximate individual decisions 34 | 35 | % Economic parameters 36 | save economicParameters.mat bbeta ssigma aaBar aalpha ddelta vEpsilonGrid aggEmployment ... 37 | mmu ttau rrhoTFP ssigmaTFP mEpsilonTransition 38 | 39 | % Approximation parameters 40 | save approximationParameters.mat nEpsilon nAssets nState assetsMin assetsMax nAssetsFine nStateFine nAssetsQuadrature nStateQuadrature ... 41 | nMeasure nMeasureCoefficients kRepSS maxIterations tolerance dampening 42 | 43 | % Grids 44 | save grids.mat vAssetsGridZeros vAssetsGrid mEpsilonGrid mAssetsGrid mEpsilonPrimeGrid vAssetsGridFine ... 45 | vAssetsGridFineZeros mEpsilonGridFine mAssetsGridFine mEpsilonPrimeGridFine vQuadratureWeights ... 46 | vAssetsGridQuadratureZeros vAssetsGridQuadrature mEpsilonGridQuadrature mAssetsGridQuadrature 47 | 48 | % Polynomials 49 | save polynomials.mat vAssetsPoly vAssetsPolySquared vAssetsPolyFine vAssetsPolyQuadrature vAssetsPolyBC 50 | 51 | else % if using splines to approximate individual decisions 52 | 53 | % Economic parameters 54 | save economicParameters.mat bbeta ssigma aaBar aalpha ddelta vEpsilonGrid aggEmployment ... 55 | mmu ttau rrhoTFP ssigmaTFP mEpsilonTransition 56 | 57 | % Approximation parameters 58 | save approximationParameters.mat nEpsilon nAssets nState assetsMin assetsMax nAssetsFine nStateFine nAssetsQuadrature nStateQuadrature ... 59 | nMeasure nMeasureCoefficients kRepSS maxIterations tolerance dampening 60 | 61 | % Grids 62 | save grids.mat vAssetsGrid mEpsilonGrid mAssetsGrid mEpsilonPrimeGrid vAssetsGridFine ... 63 | mEpsilonGridFine mAssetsGridFine mEpsilonPrimeGridFine vQuadratureWeights ... 64 | vAssetsGridQuadrature mEpsilonGridQuadrature mAssetsGridQuadrature 65 | 66 | end 67 | 68 | %---------------------------------------------------------------- 69 | % Run Dynare 70 | %---------------------------------------------------------------- 71 | 72 | if splineOpt == 0 % if using polynomials to approximate individual decisions 73 | 74 | dynare firstOrderDynamics_polynomials 75 | 76 | else % if using splines to approximate individual decisions 77 | 78 | dynare firstOrderDynamics_splines 79 | 80 | end 81 | 82 | cd('../') -------------------------------------------------------------------------------- /steadyState.m: -------------------------------------------------------------------------------- 1 | % Computes and analyzes steady state with no aggregate shocks 2 | % 3 | % Thomas Winberry, July 26th, 2016 4 | 5 | clear all 6 | close all 7 | clc 8 | 9 | oldFolder = cd('./Auxiliary Functions'); 10 | 11 | %---------------------------------------------------------------- 12 | % Set parameters 13 | %---------------------------------------------------------------- 14 | setParameters; 15 | 16 | %---------------------------------------------------------------- 17 | % Compute Steady State 18 | %---------------------------------------------------------------- 19 | 20 | % Solve for steady state capital stock, distribution, and decision rules 21 | coreSteadyState; 22 | 23 | % Compute decision rules along fine grid for analysis 24 | [~,mHistogram,mAssetsPrime,mConsumption] = computeMCResidualHistogram(aggregateCapital); 25 | 26 | % Compute density along fine grid 27 | mDistributionFine = zeros(nEpsilon,nAssetsFine); 28 | for iEpsilon = 1 : nEpsilon 29 | 30 | % First moment (uncentered) 31 | mGridMoments = zeros(nAssetsFine,nMeasure); 32 | mGridMoments(:,1) = (vAssetsGridFine - mMoments(iEpsilon,1)); 33 | 34 | % Higher order moments (centered) 35 | for iMoment = 2 : nMeasure 36 | mGridMoments(:,iMoment) = (vAssetsGridFine - mMoments(iEpsilon,1)) .^ iMoment - ... 37 | mMoments(iEpsilon,iMoment); 38 | end 39 | 40 | % Compute density away from borrowing constraint 41 | mDistributionFine(iEpsilon,:) = mParameters(iEpsilon,1) * exp(mGridMoments * ... 42 | mParameters(iEpsilon,2:nMeasure+1)'); 43 | 44 | % Mass at borrowing constraint 45 | %mDistributionFine(iEpsilon,1) = mHat(iEpsilon,1); % Commented out for now; need fine quadrature grid to capture correctly 46 | 47 | end 48 | 49 | %---------------------------------------------------------------- 50 | % Plot results 51 | %---------------------------------------------------------------- 52 | 53 | % Savings function 54 | figure 55 | hold on 56 | plot(vAssetsGridFine,mAssetsPrime(1,:),'linewidth',1.5,'color',[178/255,34/255,34/255]) 57 | plot(vAssetsGridFine,mAssetsPrime(2,:),'linewidth',1.5,'color',[8/255,62/255,118/255]) 58 | plot(vAssetsGridFine,vAssetsGridFine,'k--','linewidth',1) 59 | xlabel('Assets, $a$','interpreter','latex') 60 | ylabel('Savings, $s(\varepsilon,a)$','interpreter','latex') 61 | xlim([aaBar .9*assetsMax]) 62 | title('Savings Decision Rule') 63 | legend('Unemployed','Employed','location','southeast') 64 | grid on 65 | set(gcf,'color','w') 66 | hold off 67 | 68 | % Consumption function 69 | figure 70 | hold on 71 | plot(vAssetsGridFine,mConsumption(1,:),'linewidth',1.5,'color',[178/255,34/255,34/255]) 72 | plot(vAssetsGridFine,mConsumption(2,:),'linewidth',1.5,'color',[8/255,62/255,118/255]) 73 | xlabel('Assets, $a$','interpreter','latex') 74 | ylabel('Consumption, $c(\varepsilon,a)$','interpreter','latex') 75 | xlim([aaBar .9*assetsMax]) 76 | title('Consumption Decision rule') 77 | legend('Unemployed','Employed','location','southeast') 78 | grid on 79 | set(gcf,'color','w') 80 | hold off 81 | 82 | % Distribution of unemployed households 83 | figure 84 | hold on 85 | plot(vAssetsGridFine,mHistogram(1,:) / sum(mHistogram(1,:)),... 86 | 'linewidth',1.5,'color',[8/255,62/255,118/255]) 87 | plot(vAssetsGridFine,mDistributionFine(1,:) ./ sum(mDistributionFine(1,:)),... 88 | 'linewidth',1.5,'color',[178/255,34/255,34/255],'linestyle','--') 89 | xlabel('Assets, $a$','interpreter','latex') 90 | ylabel('Mass of households, $g(\varepsilon,a)$','interpreter','latex') 91 | xlim([aaBar .9*assetsMax]) 92 | title('Invariant Distribution of Households (Unemployed)') 93 | legend('Histogram','Parametric Family','location','northeast') 94 | grid on 95 | set(gcf,'color','w') 96 | hold off 97 | 98 | % Distribution of employed households 99 | figure 100 | hold on 101 | plot(vAssetsGridFine,mHistogram(2,:) / sum(mHistogram(2,:)),... 102 | 'linewidth',1.5,'color',[8/255,62/255,118/255]) 103 | plot(vAssetsGridFine,mDistributionFine(2,:) ./ sum(mDistributionFine(2,:)),... 104 | 'linewidth',1.5,'color',[178/255,34/255,34/255],'linestyle','--') 105 | xlabel('Assets, $a$','interpreter','latex') 106 | ylabel('Mass of households, $g(\varepsilon,a)$','interpreter','latex') 107 | xlim([aaBar .9*assetsMax]) 108 | title('Invariant Distribution of Households (Employed)') 109 | legend('Histogram','Parametric Family','location','northeast') 110 | grid on 111 | set(gcf,'color','w') 112 | hold off 113 | 114 | cd(oldFolder) -------------------------------------------------------------------------------- /userGuide.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohannesPfeifer/winberryAlgorithmCodes/cccb309722a1b03759bb38698f5ab65f67cd794b/userGuide.pdf -------------------------------------------------------------------------------- /winberryAlgorithm.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JohannesPfeifer/winberryAlgorithmCodes/cccb309722a1b03759bb38698f5ab65f67cd794b/winberryAlgorithm.pdf --------------------------------------------------------------------------------