function [fit, vaf, addcon] = linfitac_missing(prox,inperm,proxmiss)

%  LINFITAC_MISSING does a confirmatory fitting of a given unidimensional order
%  using the Dykstra--Kaczmarz iterative projection 
%  least-squares method, but differing from linfit.m in 
%  including the estimation of an additive constant;also, missing entries
%  in the input proximity matrix PROX are given values of zero.
%
%  syntax: [fit, vaf, addcon] = ...
%                linfitac_missing(prox,inperm,proxmiss)
%
%  INPERM is the given order;
%  FIT is an $n \times n$ matrix that is fitted to 
%  PROX(INPERM,INPERM) with variance-accounted-for VAF;
%  ADDCON is the estimated additive constant that can be interpreted
%  as being added to PROX.  PROXMISS is the same size as PROX (with main
%  diagonal entries all zero); an off-diagonal entry of 1.0 denotes an
%  entry in PROX that is present and 0.0 if it is absent.


n=size(prox,1);
acondiff = 1.0;
addcon = 0.0;
proxave = zeros(n,n);
fit = zeros(n,n);
fmadd = zeros(n,n);

fitmiss = proxmiss(inperm,inperm);


while (acondiff >= 1.0e-005)
    
    npres = 0;
   
   for i =1:n
      for j=1:n
         
         if(i ~= j)
             
             if(proxmiss(i,j) == 1)
                 npres = npres + 1;
             end
            fit(i,j) = prox(inperm(i),inperm(j)) + addcon;
         else
            fit(i,j) = 0.0;
         end
      end
   end
   
      
      addconpv = addcon;
      
      work = zeros(n*(n-1)*(n-2),1);
      cr = 1.0;

while (cr >= 1.0e-006)
   
   cr = 0.0;
   index = 0;
   
   for jone = 1:(n-2)
      for jtwo = (jone+1):(n-1)
         for jthree = (jtwo+1):n
             
             if(((fitmiss(jone,jtwo) == 1) & (fitmiss(jone,jthree) == 1)) ...
                     & (fitmiss(jtwo,jthree) == 1))
            
            p1 = fit(jone,jtwo);
            p2 = fit(jone,jthree);
            p3 = fit(jtwo,jthree);
            
            fit(jone,jtwo) = fit(jone,jtwo) - work(index+1);
            fit(jone,jthree) = fit(jone,jthree) - work(index+2);
            fit(jtwo,jthree) = fit(jtwo,jthree) - work(index+3);
            
            del = (fit(jone,jthree) - fit(jone,jtwo) - ...
               fit(jtwo,jthree))/3.0;
            
            fit(jone,jthree) = fit(jone,jthree) - del;
            fit(jone,jtwo) = fit(jone,jtwo) + del;
            fit(jtwo,jthree) = fit(jtwo,jthree) + del;
            
            work(index+1) = del;
            work(index+2) = -del;
            work(index+3) = del;
            
            index = index + 3;
            
            cr = cr + abs(p1-fit(jone,jtwo)) + abs(p2-fit(jone,jthree)) ...
               + abs(p3 - fit(jtwo,jthree));
             end
                         
         end
      end
   end
   
   for jone = 1:(n-1)
      for jtwo = (jone+1):n
          
          if(fitmiss(jone,jtwo) == 1)
         
         p1 = fit(jone,jtwo);
         
         fit(jone,jtwo) = fit(jone,jtwo) - work(index+1);
         
         if(fit(jone,jtwo) < 0.0)
            
            work(index+1) = -fit(jone,jtwo);
            fit(jone,jtwo) = 0.0;
            
        else
            
            work(index+1) = 0.0;
            
         end
         
         index = index + 1;
         
         cr= cr + abs(p1-fit(jone,jtwo));
          end
      end
   end
end



for jone = 1:(n-1)
   for jtwo = (jone+1):n
                  
         fit(jtwo,jone) = fit(jone,jtwo);
         
   end
end

proxpermut = prox(inperm,inperm);


addcon = -sum(sum((proxpermut.*fitmiss) - (fit.*fitmiss)))/(npres);

acondiff = abs(addcon - addconpv);

end

   
  aveprox = sum(sum(prox))/(npres);

for i = 1:n
   for j = 1:n
      if( i ~= j)
         proxave(i,j) = aveprox;
         fmadd(i,j) = fit(i,j) - addcon;
      else
         proxave(i,j) = 0;
         fmadd(i,j) = 0.0;
      end
   end
end

diff = sum(sum(((proxpermut.*fitmiss) - (fmadd.*fitmiss)).^2));

denom = sum(sum(((proxpermut.*fitmiss) - (proxave.*fitmiss)).^2));

vaf = 1 - (diff/denom);


