function [ML,MS,Pr] = CRRmargin(T,S0,ML0,MS0,r,v,N)
% Octave/MATLAB function to find Margin balances for
% the Long and Short side of a Futures contract in
% the Cox-Ross-Rubinstein (CRR) model.
% INPUTS:                                  (Example)
%   T = time to expiry, in years                 (1)
%   S0 = Underlying asset spot price           (100)
%   ML0 = Long-side initial Margin             (200)
%   MS0 = Short-side initial Margin            (200)
%   r = riskless annual interest rate         (0.02)
%   v = volatility                            (0.15)
%   N = binomial tree height                    (10)
% OUTPUTS:
%   ML = Long margin, non-recombining tree array 
%   MS = Short margin, non-recombining tree array
%   Pr = path probs., non-recombining tree array
% EXAMPLE:
%   [ML,MS,Pr]=CRRmargin(1,100,200,200,0.02,0.15,10);
%
  [pu,up,R1] = CRRparams(T,r,v,N); % key parameters
  S = StreeCRR(S0,up,N);  % CRR model S matrix 
  R = R1*ones(N,N); % const. riskless returns matrix
  [F,G] = FwdFut(S, R, N); % Futures prices matrix G
  [ML,MS] = MarFut(ML0, MS0, G, R, N); % accounts
  Pr = PathPr(pu*ones(N,N), N); % path probabilities
  return
end
