function [MMRE, predict] = fwpsabe(Pdata, Tdata, Cases, Weightbin, mean, ARE, kval)

Nanalog = kval;
Neighbors = ones(1,kval);

% select appropriate cases 
Pdata = Pdata(find(Cases == 1), :);

% if the number of cases is less than the number of neighbors
ncase = sum(Cases);  

if Nanalog > ncase
    
    Nanalog = ncase;
    Neighbors = Neighbors(1:Nanalog);
    
end

% obtain the size of projects data
[npro, natt] = size(Pdata);
[tnpro, tnatt] = size(Tdata);

% convert weight from bin digits to dec digits
[junk, leng_weight] = size(Weightbin);

Weight = [];

for w = 1 : 2 : leng_weight  % Weights are 0-3 and give us a nifty function!
if (binvec2dec(Weightbin(w : w+1)) == 0)
Weight( (w-1)./2 +1 ) = 0;
end

if (binvec2dec(Weightbin(w : w+1)) == 1)
Weight( (w-1)./2 +1 ) = 1;
end

if (binvec2dec(Weightbin(w : w+1)) == 2)
Weight( (w-1)./2 +1 ) = 2;
end

if (binvec2dec(Weightbin(w : w+1)) == 3)
Weight( (w-1)./2 +1 ) = 4;
end
end

predict = []; % the vector contains the prediction value of each project's cost
Metrics = []; % the matrix contains the metrics of cost estimation
avek = 0; % the scalar contians the average number of analogous.

% jackknifing approach
for i = 1: tnpro
    
    distance = [];% the distances between the new project and the exsited projects
    near_neighbor = []; % the nearest neighbor 
    n = 0; % the index of vector distance

    % calculate the Eucilid distances between the new project and existed
    % projects
    
    for j = 1:ncase
        % calculate the weighted Eu distances    
        distance(1,j) = euc_distance(Pdata(j,1:natt-1), Tdata(i,1:tnatt-1), ones(1, natt-1), Weight);
        distance(2,j) = j;
           
        % find the nearest projects and npro must be greater than Nanalog
          if j <= Nanalog 
          % initiate the neighbors, Nanalog number of neighbors
             near_neighbor(:,j) = distance(:,j);
            
            % farthest neighbor is expelled
            elseif max(near_neighbor(1,:)) > distance(1,j)
                % find the indices of the maximum element in the neighbor
                % array and replace it with the new element
                    temp = find(near_neighbor(1,:)==max(near_neighbor(1,:)));
                    near_neighbor(:, temp(1)) = distance(:, j);  
          
         end
    end   

    % sort the neighbors
    for k = 1 : Nanalog
        for p = k+1 : Nanalog        
            if near_neighbor(1,k) > near_neighbor(1,p)
               temp = near_neighbor(:,p);
               near_neighbor(:,p) = near_neighbor(:,k);
               near_neighbor(:,k) = temp;
            end
        end           
    end

    
   %find the analogues 
    analogue = Pdata(near_neighbor(2,1:Nanalog)',natt);
	predict(i) = median(analogue);   

end
if (ARE == 1)
		metric = measure2(predict, Tdata(:,tnatt)');
else
						  metric = measure(predict, Tdata(:,tnatt)');
end

if (mean == 1)
	MMRE = mean(metric);
else if (mean == 2)
	MMRE = median(metric);
	else 
	MMRE = pred(metric, .3);
	end
end