load risk_rate.dat

tic;

opts = statset('Maxiter',1000);

best_vaf = 0.0;
store_vaf = zeros(100,1);
store_stress = zeros(100,1);

for k = 1:100

[coords,stress,disparities] = mdscale(risk_rate,2,'Criterion','stress', 'Start',...
    'random','Replicates',1,'Options',opts);

n = size(coords,1);

distance_matrix = zeros(n,n);

for i = 1:n
    for j = 1:n
        
        distance_matrix(i,j) = sqrt(((coords(i,1) - coords(j,1))^2) + ...
            ((coords(i,2) - coords(j,2))^2));
    end
end

ave_diff = (sum(sum(disparities - distance_matrix)))/(n*(n-1));

ave_proximity = (sum(sum(disparities)))/(n*(n-1));

disparities_vec = squareform(disparities);

distance_vec = squareform(distance_matrix);

r = corrcoef(disparities_vec',distance_vec');
vaf = r(1,2)^2;

store_vaf(k) = vaf;
store_stress(k) = stress;

if(vaf > best_vaf)
    
    best_vaf = vaf;
    best_coords = coords;
    best_disparities = disparities;
    
end
end

store_vaf;

sorted_vafs = sort(store_vaf');
sorted_stress = sort(store_stress');

sorted_vafs
sorted_stress;
best_vaf
best_coords
best_disparities

lossvalues = [store_vaf,store_stress];

corrcoef(lossvalues);

axis equal

plot(best_coords(:,1),best_coords(:,2),'ko')

hold on

for i = 1:18
    
    objectlabels{i,1} = int2str(i);
    
end

text(best_coords(:,1),best_coords(:,2),objectlabels,'fontsize',10,'verticalalignment','bottom')

toc;


