% Incremental Classifier Adpatation
clc;clear all;close all;
load ('batchs.mat');
labels={batch1_p(:,1),batch2_p(:,1),batch3_p(:,1),batch4_p(:,1),batch5_p(:,1),batch6_p(:,1),batch7_p(:,1),batch8_p(:,1),batch9_p(:,1),batch10_p(:,1)};
data={batch1_p(:,4:2:end),batch2_p(:,4:2:end),batch3_p(:,4:2:end),batch4_p(:,4:2:end),batch5_p(:,4:2:end),batch6_p(:,4:2:end),batch7_p(:,4:2:end),...
    batch8_p(:,4:2:end),batch9_p(:,4:2:end),batch10_p(:,4:2:end)};
for i=1:size(data,2)
    for j=1:size(data{i},2)
        data{i}(:,j)=data{i}(:,j)/max(abs(data{i}(:,j)));
    end
end

DAT{1}=data{1};
Lab{1}=labels{1};
for i=2:size(data,2)
    ind=randperm(size(data{i},1));
    indx=ind(1:20);
    DAT{i}=data{i}(indx,:);
    Lab{i}=labels{i}(indx,:);
end

r=50;%30;
for i=1:size(data,2)
    [coef{i},score]=princomp(data{i});
    COEF{i}=coef{i}(:,1:r);
    subplot(2,10,i)
    plot(COEF{i}(:,1),COEF{i}(:,2),'.');
end

for i=1:size(COEF,2)
    projData{i}=data{i}*COEF{i};
    subplot(2,10,i+10)
    plot(projData{i}(:,1),projData{i}(:,2),'.');
end

miu=3; % 0~10
for i=1:size(COEF,2) 
    W{i}=COEF{i}'*COEF{1}/(1+miu);
    COEF_New{i}=COEF{i}*W{i};
end
figure
for i=1:size(data,2)
    subplot(2,10,i)
    plot(COEF_New{i}(:,1),COEF_New{i}(:,2),'.');
end

for i=1:size(COEF,2)
    projData_New{i}=data{i}*COEF_New{i};
    subplot(2,10,i+10)
    plot(projData_New{i}(:,1),projData_New{i}(:,2),'.');
end

% batch 2, batch 7, batch 9

figure
subplot(2,4,1)
plot(COEF{1}(:,1),COEF{1}(:,2),'k.');
subplot(2,4,2)
plot(COEF{2}(:,1),COEF{2}(:,2),'.');
subplot(2,4,3)
plot(COEF{7}(:,1),COEF{7}(:,2),'r.');
subplot(2,4,4)
plot(COEF{9}(:,1),COEF{9}(:,2),'g.');

subplot(2,4,5)
plot(COEF_New{1}(:,1),COEF_New{1}(:,2),'k.');
subplot(2,4,6)
plot(COEF_New{2}(:,1),COEF_New{2}(:,2),'.');
subplot(2,4,7)
plot(COEF_New{7}(:,1),COEF_New{7}(:,2),'r.');
subplot(2,4,8)
plot(COEF_New{9}(:,1),COEF_New{9}(:,2),'g.');

figure
subplot(2,4,1)
plot(projData{1}(:,1),projData{1}(:,2),'k.');
subplot(2,4,2)
plot(projData{2}(:,1),projData{2}(:,2),'.');
subplot(2,4,3)
plot(projData{7}(:,1),projData{7}(:,2),'r.');
subplot(2,4,4)
plot(projData{9}(:,1),projData{9}(:,2),'g.');

subplot(2,4,5)
plot(projData_New{1}(:,1),projData_New{1}(:,2),'k.');
subplot(2,4,6)
plot(projData_New{2}(:,1),projData_New{2}(:,2),'.');
subplot(2,4,7)
plot(projData_New{7}(:,1),projData_New{7}(:,2),'r.');
subplot(2,4,8)
plot(projData_New{9}(:,1),projData_New{9}(:,2),'g.');

% nearest neighbor classification

for i=2:size(data,2)
    for j=1:size(data{i},1)
        for k=1:size(data{1},1)
            distan_b(k)=(data{i}(j,:)-data{1}(k,:))*(data{i}(j,:)-data{1}(k,:))';
        end
        for k=1:size(projData{1},1)
            distan_a(k)=(projData_New{i}(j,:)-projData_New{1}(k,:))*(projData_New{i}(j,:)-projData_New{1}(k,:))';
        end
        [v,ind_lab_b]=min(distan_b);
        [v,ind_lab_a]=min(distan_a);
        pred_label_b{i}(j)=labels{1}(ind_lab_b);
        pred_label_a{i}(j)=labels{1}(ind_lab_a);
    end
    nn_acc_b(i)=length(find(pred_label_b{i}==labels{i}'))/length(labels{i})*100;
    nn_acc_a(i)=length(find(pred_label_a{i}==labels{i}'))/length(labels{i})*100;
end

% support vector machine
for i=2:size(data,2)
    model_svm_b = svmtrain(labels{1}, data{1}, '-c 10 -g 0.1 -b 1 -t 2'); % -c:penalty coefficient;-t:kernel type;-g:gamma kernel parameter
    [ytest_b, test_accuracy_b, prob_estimates] = svmpredict(labels{i},data{i}, model_svm_b, '-b 1');
    model_svm_a = svmtrain(labels{1}, projData_New{1}, '-c 10 -g 0.1 -b 1 -t 2'); % -c:penalty coefficient;-t:kernel type;-g:gamma kernel parameter
    [ytest_a, test_accuracy_a, prob_estimates] = svmpredict(labels{i},projData_New{i}, model_svm_a, '-b 1');
    svm_acc_b(i)= test_accuracy_b(1);
    svm_acc_a(i)= test_accuracy_a(1);
end

% regularized least square
for i=1:size(labels,2)
    for j=1:size(labels{i},1)
        if labels{i}(j)==1
            labels_vectors{i}(j,:)=[1 0 0 0 0 0];
        elseif labels{i}(j)==2
            labels_vectors{i}(j,:)=[0 1 0 0 0 0];
        elseif labels{i}(j)==3
            labels_vectors{i}(j,:)=[0 0 1 0 0 0];
        elseif labels{i}(j)==4
            labels_vectors{i}(j,:)=[0 0 0 1 0 0];
        elseif labels{i}(j)==5
            labels_vectors{i}(j,:)=[0 0 0 0 1 0];
        elseif labels{i}(j)==6
            labels_vectors{i}(j,:)=[0 0 0 0 0 1];
        end
    end
end

lambda=1000;

for i=2:size(data,2)
    P_b=inv((data{1}'*data{1}+lambda*eye(128)))*data{1}'*labels_vectors{1};
    P_a=inv((projData_New{1}'*projData_New{1}+lambda*eye(r)))*projData_New{1}'*labels_vectors{1};
    y_b{i}=data{i}*P_b;
    y_a{i}=projData_New{i}*P_a;
    for j=1:size(y_b{i},1)
        [v,ind_ls_b]=max(y_b{i}(j,:));
        [v,ind_ls_a]=max(y_a{i}(j,:));
        ls_pred_b{i}(j,:)=ind_ls_b;
        ls_pred_a{i}(j,:)=ind_ls_a;
    end
    ls_acc_b(i)=length(find(ls_pred_b{i}==labels{i}))/length(labels{i})*100;
    ls_acc_a(i)=length(find(ls_pred_a{i}==labels{i}))/length(labels{i})*100;
end
    
% nn_acc_b
% nn_acc_a
svm_acc_b
svm_acc_a
% ls_acc_b
% ls_acc_a

% calculate the distribution discrepancy

for i=1:size(COEF,2)
    COEF{i}=COEF{i}./norm(COEF{i},'fro');%repmat(max(abs(COEF{i})),size(COEF{i},1),1);
    COEF_New{i}=COEF_New{i}./norm(COEF_New{i},'fro');%repmat(max(abs(COEF_New{i})),size(COEF_New{i},1),1);
    projData{i}=projData{i}./norm(projData{i},'fro');%repmat(max(abs(projData{i})),size(projData{i},1),1);
    projData_New{i}=projData_New{i}./norm(projData_New{i},'fro');%repmat(max(abs(projData_New{i})),size(projData_New{i},1),1);
    
    Diff_COEF(i)=norm(mean(COEF{i})-mean(COEF{1}));
    Diff_COEF_New(i)=norm(mean(COEF_New{i})-mean(COEF_New{1}));
    Diff_projData(i)=norm(mean(projData{i})-mean(projData{1}));
    Diff_projData_New(i)=norm(mean(projData_New{i})-mean(projData_New{1}));
end
    
            