clear
% read in data and format
data = textread('data.csv','','delimiter',',','headerlines',1);
for j = 1:8
    skater(j).name = ' ';
    skater(j).raw.ee = ones(8,13);
    skater(j).raw.pc = ones(5,12);
    skater(j).score.tot = ones(1,220);
    skater(j).score.ee = ones(1,220);
    skater(j).score.pc = ones(1,220);
    k = 13*j-12; %indexing variables
    l = 13*j-5;
    m = 13*j-4;
    n = 13*j;
    skater(j).raw.ee = data(k:l,:);
    skater(j).raw.pc = data(m:n,:);
end
clear data;

% generate all permutations of eliminated judges
elim = ones(220,3);
count = 1;
for i = 1:10
    for j = i+1:11
        for k = j+1:12
            elim(count,:) = [i,j,k];
            count = count+1;
        end
    end
end

% calculate score for each permutation
for i = 1:220
    d = elim(i,:);
    d = setxor(2:13,d+1);
    
    for j = 1:8 %loop through each skater
        
        % calculate execution element scores
        clear temp score;
        temp = skater(j).raw.ee(:,d);
        score = (sum(temp')' - max(temp')' - min(temp')')/7;
        score = score + skater(j).raw.ee(:,1);
        skater(j).score.ee(1,i) = sum(score);

        % calculate program component scores
        clear temp score;
        temp = skater(j).raw.pc(:,d);
        score = (sum(temp')' - max(temp')' - min(temp')')/7;
        score = score.*.8;
        skater(j).score.pc(1,i) = sum(score);

        % calculate combined news
        skater(j).score.tot(1,i) = skater(j).score.ee(1,i) + skater(j).score.pc(1,i);
    end
end

% generate histograms of scores for each skater
x = 0:.2:100;
figure
hold on
for j = 1:8
    plot(x, histc(skater(j).score.tot,x));
end

% simulating new method
new_0bias = normrnd(0,1,12,10^4);
new_1bias = new_0bias;
new_1bias(1,:) = normrnd(1,1,1,10^4);
new_2bias = new_1bias;
new_2bias(2,:) = normrnd(1,1,1,10^4);
new_3bias = new_2bias;
new_3bias(3,:) = normrnd(1,1,1,10^4);
new_4bias = new_3bias;
new_4bias(4,:) = normrnd(1,1,1,10^4);
new_5bias = new_4bias;
new_5bias(5,:) = normrnd(1,1,1,10^4);
new_6bias = new_5bias;
new_6bias(6,:) = normrnd(1,1,1,10^4);

new_0bias_trimmed = ones(220,10^4);
new_1bias_trimmed = ones(220,10^4);
new_2bias_trimmed = ones(220,10^4);
new_3bias_trimmed = ones(220,10^4);
new_4bias_trimmed = ones(220,10^4);
new_5bias_trimmed = ones(220,10^4);
new_6bias_trimmed = ones(220,10^4);
new_0bias_notrim = ones(220,10^4);
new_1bias_notrim = ones(220,10^4);
new_2bias_notrim = ones(220,10^4);
new_3bias_notrim = ones(220,10^4);
new_4bias_notrim = ones(220,10^4);
new_5bias_notrim = ones(220,10^4);
new_6bias_notrim = ones(220,10^4);





for i = 1:220
    d = elim(i,:);
    d = setxor(1:12,d);
    new_0bias_trimmed(i,:) = (sum(new_0bias(d,:)) - max(new_0bias(d,:)) - min(new_0bias(d,:)))/7;
    new_1bias_trimmed(i,:) = (sum(new_1bias(d,:)) - max(new_1bias(d,:)) - min(new_1bias(d,:)))/7;
    new_2bias_trimmed(i,:) = (sum(new_2bias(d,:)) - max(new_2bias(d,:)) - min(new_2bias(d,:)))/7;
    new_3bias_trimmed(i,:) = (sum(new_3bias(d,:)) - max(new_3bias(d,:)) - min(new_3bias(d,:)))/7;
    new_4bias_trimmed(i,:) = (sum(new_4bias(d,:)) - max(new_4bias(d,:)) - min(new_4bias(d,:)))/7;
    new_5bias_trimmed(i,:) = (sum(new_5bias(d,:)) - max(new_5bias(d,:)) - min(new_5bias(d,:)))/7;
    new_6bias_trimmed(i,:) = (sum(new_6bias(d,:)) - max(new_6bias(d,:)) - min(new_6bias(d,:)))/7;
    new_0bias_notrim(i,:) = sum(new_0bias(d,:))/9;
    new_1bias_notrim(i,:) = sum(new_1bias(d,:))/9;
    new_2bias_notrim(i,:) = sum(new_2bias(d,:))/9;
    new_3bias_notrim(i,:) = sum(new_3bias(d,:))/9;
    new_4bias_notrim(i,:) = sum(new_4bias(d,:))/9;
    new_5bias_notrim(i,:) = sum(new_5bias(d,:))/9;
    new_6bias_notrim(i,:) = sum(new_6bias(d,:))/9;
end


mean(mean(new_0bias_trimmed))
mean(mean(new_1bias_trimmed))
mean(mean(new_2bias_trimmed))
mean(mean(new_3bias_trimmed))
mean(mean(new_4bias_trimmed))
mean(mean(new_5bias_trimmed))
mean(mean(new_6bias_trimmed))
mean(mean(new_0bias_notrim))
mean(mean(new_1bias_notrim))
mean(mean(new_2bias_notrim))
mean(mean(new_3bias_notrim))
mean(mean(new_4bias_notrim))
mean(mean(new_5bias_notrim))
mean(mean(new_6bias_notrim))

var(mean(new_0bias_trimmed))
var(mean(new_1bias_trimmed))
var(mean(new_2bias_trimmed))
var(mean(new_3bias_trimmed))
var(mean(new_4bias_trimmed))
var(mean(new_5bias_trimmed))
var(mean(new_6bias_trimmed))
var(mean(new_0bias_notrim))
var(mean(new_1bias_notrim))
var(mean(new_2bias_notrim))
var(mean(new_3bias_notrim))
var(mean(new_4bias_notrim))
var(mean(new_5bias_notrim))
var(mean(new_6bias_notrim))
