├── Life_Cycle_final.m ├── Life_cycle_2_periods.m ├── Life_cycle_2_periods_portfolio.m ├── README.md ├── Slides_Numerical_Methods_Final.pdf └── Solow.m /Life_Cycle_final.m: -------------------------------------------------------------------------------- 1 | %Life-cycle model of consumption and saving (T periods) 2 | %Written by Michael Hatcher (m.c.hatcher@soton.ac.uk) in 2017. 3 | %Fast and short version as per the slides 4 | 5 | clear; clc; 6 | 7 | %------------------------ 8 | %Housekeeping 9 | %------------------------ 10 | 11 | beta = 0.96; r = 0.035; T = 80; 12 | % Since r < 1/beta-1 optimal consumption path downward sloping 13 | a = -0.01; b=2/3; c =1; % Parameters of income process (quadratic) 14 | 15 | for t=1:T 16 | Y(t) = a*t^2 + b*t + c; 17 | 18 | if t >= 0.75*T 19 | Y(t) = 0; %Retired from t = 60 onwards if T = 80 20 | end 21 | 22 | end 23 | 24 | %---------------------------------------- 25 | %Simulating the model (guesses on A(1)) 26 | %---------------------------------------- 27 | 28 | Ainit = -15; %Initial value of wealth used in guess loop 29 | Nguess = 200000; 30 | A_vec = linspace(Ainit,-Ainit,Nguess); %Vector of guesses for A(1) 31 | 32 | %Outer loop i (guess loop) 33 | %Inner loop t (simulates consumption for each initial guess of A) 34 | 35 | for i=1:Nguess 36 | 37 | Aguess(i) = A_vec(i); 38 | A(1) = Aguess(i); 39 | C(1) = Y(1) - A(1); 40 | C(2) = beta*(1+r)*C(1); 41 | A(2) = Y(2) - C(2) + (1+r)*A(1); 42 | 43 | for t=3:T 44 | 45 | C(t) = beta*(1+r)*C(t-1); 46 | A(t) = Y(t)-C(t) + (1+r)*A(t-1); 47 | 48 | end 49 | 50 | CCheck = C(T); 51 | 52 | C(T) = Y(T) + (1+r)*A(T-1); 53 | %Equals CCheck only if A(T) = 0; 54 | 55 | gap(i) = abs(CCheck - C(T)); 56 | %Equal to abs(-A(T)) 57 | 58 | end 59 | 60 | 61 | %Find optimal path 62 | [gap_min, IndexU] = min(gap); 63 | 64 | disp('Terminal wealth at our solution is') 65 | gap_min 66 | 67 | %---------------------------------------------------------- 68 | %Simulate optimal path of consumption, saving and wealth 69 | %---------------------------------------------------------- 70 | 71 | Astar(1) = Aguess(IndexU); 72 | Astar(2) = Y(2) - beta*(1+r)*Y(1) + (1+r)*(1+beta)*Astar(1); 73 | Cstar(1) = Y(1) - Astar(1); 74 | Cstar(2) = beta*(1+r)*Cstar(1); 75 | Sstar(1) = 0; Sstar(2) = Astar(2); 76 | 77 | 78 | %Simulate consumption path in periods 3 to T 79 | for t=3:T 80 | 81 | Cstar(t) = beta*(1+r)*Cstar(t-1); 82 | 83 | Astar(t) = Y(t)-Cstar(t) + (1+r)*Astar(t-1); 84 | 85 | Sstar(t) = Astar(t) - Astar(t-1); 86 | %Alternative def: Sstar(t) = Y(t)-Cstar(t); 87 | 88 | end 89 | 90 | %------------------- 91 | %Plot the results 92 | %------------------- 93 | 94 | subplot(2,1,1) 95 | hold on, plot(Y), plot(Cstar, 'r') 96 | %ylabel('Consumption, Income') 97 | xlabel('Time') 98 | legend('Income','Consumption') 99 | subplot(2,1,2) 100 | hold on, plot(Astar), plot(Sstar, '--r') 101 | %ylabel('Wealth, Saving') 102 | xlabel('Time') 103 | legend('Wealth','Saving') 104 | 105 | -------------------------------------------------------------------------------- /Life_cycle_2_periods.m: -------------------------------------------------------------------------------- 1 | % Two-period life-cycle model with income risk (saving in riskless bond only) 2 | % Written by Michael Hatcher (m.c.hatcher@soton.ac.uk) in 2017. 3 | 4 | clear; clc; 5 | 6 | % Parameter values 7 | beta = 0.96; r = 0.05; Y1 = 1; p = 0.5; 8 | eps1 = 0.5; eps2 = 1.05; 9 | Nguess = 8000; 10 | Sguess = NaN(Nguess,1); U = Sguess; 11 | 12 | S_vec = linspace(0,Y1,Nguess); %Vector of guesses for S 13 | 14 | for i=1:Nguess 15 | 16 | Sguess(i) = S_vec(i); 17 | S = Sguess(i); 18 | C1 = Y1 - S; 19 | C2_1 = eps1 + (1+r)*S; 20 | C2_2 = eps2 + (1+r)*S; 21 | 22 | U(i) = log(C1) + beta*( p*log(C2_1) + (1-p)*log(C2_2) ); 23 | 24 | if ~isreal(U(i)) || C1 <= 0 || C2_1 <= 0 || C2_2 <= 0 25 | U(i) = -realmax; %Not sensible economic solutions 26 | end 27 | 28 | end 29 | 30 | %Find maximum of utility w.r.t. S guesses 31 | [Umax, IndexU] = max(U); %IndexU is the location of the optimal value 32 | 33 | disp('Optimal holding of shares is') 34 | Sguess(IndexU) 35 | 36 | Sstar = Sguess(IndexU); 37 | C1star = Y1 - Sstar; 38 | C2_1star = eps1 + (1+r)*Sstar; 39 | C2_2star = eps2 + (1+r)*Sstar; 40 | 41 | %Check that FOC holds 42 | Resid = abs(1/C1star - beta*(1+r)*(p/C2_1star + (1-p)/C2_2star) ) 43 | 44 | %Plot results 45 | T = 1000; %Window around optimum (see below) 46 | plot(Sguess(IndexU-T:IndexU+T),U(IndexU-T:IndexU+T)) 47 | ylabel('Expected Utility'), xlabel('S') 48 | -------------------------------------------------------------------------------- /Life_cycle_2_periods_portfolio.m: -------------------------------------------------------------------------------- 1 | % Two-period life-cycle porfolio model 2 | % Written by Michael Hatcher (m.c.hatcher@soton.ac.uk) in 2017. 3 | 4 | clc; clear; 5 | 6 | % Parameter values 7 | beta = 0.96; r = 0.05; Y1 = 1; p = 0.99; 8 | eps1 = 0.0107; eps2 = -0.7; 9 | Nguess = 2000; 10 | Nguess2 = 2000; 11 | 12 | for i=1:Nguess 13 | 14 | for j=1:Nguess2 15 | 16 | Sguess(i) = 0.5*(i-1)/Nguess; 17 | Bguess(j) = 0.5*(j-1)/Nguess2; 18 | 19 | S = Sguess(i); 20 | B = Bguess(j); 21 | C1 = Y1 - S - B; 22 | C2_1 = (1+r)*B + (1+r+eps1)*S ; 23 | C2_2 = (1+r)*B + (1+r+eps2)*S; 24 | 25 | U(i,j) = log(C1) + beta*( p*log(C2_1) + (1-p)*log(C2_2) ); 26 | 27 | end 28 | 29 | end 30 | 31 | %Find maximum of utility w.r.t. S guesses 32 | MN = max(U'); %row vector containing max for each column (note: transposed) 33 | [MN1, Index_S] = max(MN); 34 | Index_S 35 | 36 | %Find maximum of utility w.r.t. B guesses 37 | MN3 = max(U); %row vector containing max for each column (note: not transposed) 38 | [MN4, Index_B] = max(MN3); 39 | Index_B 40 | 41 | disp('Optimal holding of shares is') 42 | Sguess(Index_S) 43 | disp('Optimal holding of bonds is') 44 | Bguess(Index_B) 45 | 46 | Bstar = Bguess(Index_B); 47 | Sstar = Sguess(Index_S); 48 | C1star = Y1 - Sstar - Bstar; 49 | C2_1star = (1+r)*Bstar + (1+r+eps1)*Sstar; 50 | C2_2star = (1+r)*Bstar + (1+r+eps2)*Sstar; 51 | 52 | %Check that FOCs hold 53 | Resid = abs( 1/C1star - beta*(1+r)*(p/C2_1star + (1-p)/C2_2star) ) 54 | Resid2 = abs( 1/C1star - beta*( p*(1+r+eps1)/C2_1star + (1-p)*(1+r+eps2)/C2_2star ) ) 55 | 56 | %Plot results 57 | T = 40; %Window around optimum (see below) 58 | surf( Sguess(Index_S-T:Index_S+T), Bguess(Index_B-T:Index_B+T),... 59 | U(Index_S-T:Index_S+T, Index_B-T:Index_B+T) ) 60 | xlabel('S'), ylabel('B'), zlabel('Expected Utility, U') 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Solving_models_with_numerical_methods 2 | Solving models with numerical methods (economics). 3 | Introductory coding of numerical methods aimed at undergraduate / MSc students. There are several examples from economics and finance, and the codes are also provided. 4 | The examples are: 5 | 1. Solow model transitional dynamics 6 | 2. Life-cycle model (log utility, T periods) 7 | 3. Two-period stochastic model - optimal stock choice 8 | 4. Two-period stochastic model - optimal portfolio of stocks and bonds 9 | -------------------------------------------------------------------------------- /Slides_Numerical_Methods_Final.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MCHatcher/Solving_models_with_numerical_methods/bbafbe3b847786c6cec6562d1d600de701b1ac74/Slides_Numerical_Methods_Final.pdf -------------------------------------------------------------------------------- /Solow.m: -------------------------------------------------------------------------------- 1 | %Solow model 2 | % Written by Michael Hatcher (m.c.hatcher@soton.ac.uk) in 2017. 3 | 4 | clear; clc; 5 | 6 | %Parameters 7 | alpha = 1/3; delta = 0.05; n = 0.05; s = 0.20; 8 | 9 | %Initial capital and output 10 | k(1) = 1; 11 | y(1) = k(1)^alpha; 12 | 13 | %Number of simulated periods 14 | T = 150; 15 | 16 | %Simulate economy using a loop 17 | for t=2:T 18 | 19 | k(t) = (1/(1+n))*(s*k(t-1)^alpha - (n+delta)*k(t-1) ) + k(t-1); 20 | y(t) = k(t)^alpha; 21 | Growth(t) = 100*(y(t) - y(t-1))/y(t-1); 22 | 23 | end 24 | 25 | %Plot graphs 26 | hold on, 27 | subplot(1,2,1), plot(k) 28 | title('Capital per person, k'), xlabel('Time') 29 | subplot(1,2,2), plot(Growth(2:T),'r') 30 | title('Growth rate (output per person)'), xlabel('Time') 31 | 32 | --------------------------------------------------------------------------------