function Zero = CostofCapitalGLS(Pr,B0,FB,FROE,FPeriod,capd);

% This function computes the price of a stock based on the discounted residual income model in Gebhardt, Lee, and
% Swminathan (2001). 
% There are 4 types of inputs in this function. 
%   Pr is a firm's current market price
%   B0 is a firm's book value at some time t
%   FROE is a firm's vector of forecasted ROE's that are forecasted
%        "FPeriod" Periods out, with perpetuity growth occuring in the last
%        period's forecasted growth. 
%   Note: The length of FROE must be the same as FPeriod
%   capd is the cost of capital faced by the firm at time t. 
%    
%   Following Gebhardt, Lee, and Swminathan (2001), 
%   Pr(t) = B0(t) + sum over i = 1 to FPeriod-1 {(FROE(t+i)-capd(t))/(1+capd(t))^i}  * FB(t+i-1)  +
%   (FROE(t+FPeriod)-capd(t))/[capd(t)*(1+capd(t))^FPeriod] * FB(t+FPeriod-1)
%
%   Or, 0 = B0 + sum over i = 1 to FPeriod-1{(FROE-capd)/(1+capd)^i} +
%   (FROE-capd)/[capd*(1+capd)^FPeriod] - Pr

    clear scale
    for i = 1:FPeriod
        scale(1,i) = 1/((1+capd)^i);
    end
   
    Zero = B0(1,:)+((FROE(1,1:FPeriod)-capd).*scale.*[B0 FB(1:FPeriod-1)])* [ones(1,FPeriod-1) (1/capd)*(1+capd)]' - Pr;