function [F,G] = FwdFut(S, R, N)
% Octave/MATLAB function to compute Forward prices F
% and Futures prices G from recombining binomial trees
% S,R of underlying asset prices and riskless returns.
% INPUTS:                                  (Example)
%   S = asset price matrix               (N+1 x N+1)
%   R = riskless returns matrix              (N x N)
%   N = binomial tree height, must be >1         (3)
% OUTPUTS:
%   F =  (N+1) row matrix of Forward prices
%   G =  (N+1) row matrix of Futures prices
% EXAMPLE:
%  S=[100,0,0,0;87,115,0,0;75,100,133,0;65,87,115,152]
%  R=[1.025,0,0;1.03,1.02,0;1.035,1.025,1.01]
%  [F,G]= FwdFut(S,R,3); F(1,1),G(1,1)
%
  p = RiskNeut(S,R,N); % Risk neutral up probabilities
  D = ZCB(p,R,N);      % Zero coupon bond discounts
  F = zeros(N+1,N+1);  % Forward fair prices
  for(n=0:N)
    for(j=0:n)
      F(n+1,j+1) = S(n+1,j+1)/D(n+1,j+1);
    end
  end
  G = zeros(N+1,N+1);  % Futures fair prices
  for(j=0:N)
    G(N+1,j+1) = S(N+1,j+1);
  end
  for(n=N-1:-1:0)
    for(j=0:n)
      G(n+1,j+1) = ( p(n+1,j+1)*G(n+2,j+2)
                    + (1-p(n+1,j+1))*G(n+2,j+1) );
    end
  end
  return
end
