% VF_Bellman_L_PREC.m is a routine for computing value function on the left side
% of the Bellman equation in the neoclassical growth model with elastic labor 
% supply, considered in the article "How to Solve Dynamic Stochastic Models
% Computing Expectations Just Once" by Kenneth L. Judd, Lilia Maliar, Serguei 
% Maliar and Inna Tsener, (2016), Quantitative Economics, forthcoming, 
% (henceforth, JMMT, 2016).
%
% -------------------------------------------------------------------------
% -------------------------------------------------------------------------
% Inputs:    "c0" is current consumption;
%            "n0" is current labor;
%            "k1" is next-period capital;
%            "z0" is current productivity;
%            "gam, nu, B" are the utility-function parameters;
%            "beta" is the discount factor; 
%            "vcv" is the variance of the shock;
%            "rho" is the autocorrelation coefficient in the process for
%                shock;
%            "vf_coef" is the vector of polynomial coefficients in the 
%               approximated value function;  
%            "D" is the degree of polynomial approximation
%
% Output:    "V_new" is value function on the left side of the Bellman
%            equation
% -------------------------------------------------------------------------
% Copyright © 2016 by Lilia Maliar, Serguei Maliar and Inna Tsener. All 
% rights reserved. The code may be used, modified and redistributed under 
% the terms provided in the file "License_Agreement.txt".
% -------------------------------------------------------------------------

function [V_new] = VF_Bellman_L_PREC(n0,c0,k1,z0,gam,nu,B,beta,vcv,rho,vf_coef,D)

% Precomputation of integrals in the Bellman equation
%-----------------------------------------------------
intV = Integrals_PREC_2d(D,vcv)'; % Precompute integrals for constructing 
                                  % expectation functions
vf1_coef = vf_coef.*intV;         % A new vector of coefficients
                                  % with precomputed integrals

X1 = Polynomial_2d([k1 z0.^rho],D);% Construct polynomial   
EV = X1*vf1_coef;                 % Evaluate value function

if gam==1; uc0=log(c0); else uc0=(c0.^(1-gam)-1)/(1-gam); end
            % If gam=1, the consumption utility subfunction is
            % logarithmic; otherwise, it is power
if nu==1; un0=log(1-n0); else un0=((1-n0).^(1-nu)-1)/(1-nu); end
            % If nu=1, the leisure utility subfunction is
            % logarithmic; otherwise, it is power
V_new = uc0+B*un0+beta*EV; % Bellman equation        
