%ccpdf.m
clear all 
close all 
load IMAGEDATA 
%The euclidean distances of the individual vectors of class 1 
%with the centroid of the class 1 and class 2 in the higher %dimensional space is computed as follows. %Number of vectors in each classes are given as 10.
sigma=5; 
for i=1:1:10 
    for j=1:1:10
        temp{1}(i,j)=gbf(CLASS1{i},CLASS1{j},sigma);
    end
end
for i=1:1:10
    for j=1:1:10 
        temp{2}(i,j)=gbf(CLASS2{i},CLASS2{j},sigma);
    end
end
for i=1:1:10 
    for j=1:1:10
        temp3(i,j)=gbf(CLASS1{i},CLASS2{j},sigma);
    end
end
%Distance between the vectors in the first class %with centroid of the
%first class 
for i=1:1:10 
    for j=1:1:2 
        for q=1:1:2 
            if(j~=q) 
                d(i,j,q)=temp{j}(i,i)-(2/10)* sum(temp3(q,:))+(1/100)* sum(sum(temp{q})) 
            else
                d(i,j,q)=temp{j}(i,i)-(2/10)* sum(temp{j}(i,:))+(1/100)* sum(sum(temp{q}))
            end
        end
    end
end
%The posterior density function is computed %as following 
NUM=0
for i=1:1:10 
    for j=1:1:2 
        for q=1:1:2
            NUM=NUM+d(i,j,q); 
        end
        t1=NUM/d(i,j,1);
        t2=NUM/d(i,j,2 );
        PX1(i,j)=t1/(t1+t2);
        PX2(i,j)=t2/(t1+t2);
    end
end
%ANN 1 is trained using elements of the matrix PX1 %as the target values corresponding to the input %CLASS{1} and CLASS{2} to model posterior density function of 
%class 1 
temp1=cell2mat(CLASS1');
temp2=cell2mat(CLASS2');
TRAINDATA=[temp1(1:1:5,:) ones(5,1); temp2(1:1:5,:) ones(5,1)];
TESTDATA=[temp1(6:1:10,:) ones(5,1);temp2(6:1:10,:) ones(5,1)]; 
temp=reshape(PX1,1,20); 
TRAINTARGET=[temp([1:1:5]) temp([11:1:15])]; 
TESTTARGET=[temp([6:1:10]) temp([16:1:20])];
W1=randn(2501,1000)*0.5; 
W2=randn(1,1001)*0.5;
JCOL=[];
for iteration=1:1:20
    YCOLTRAIN=[];
    for n=1:1:10 
        H=logsig(TRAINDATA(n,:)*W1); 
        H=[H 1];
        Y=H*W2';
        Y=1/(1+exp(-Y));
        YCOLTRAIN=[YCOLTRAIN;Y(1)];
        %Updating weights W2 
        W2(1,:)=W2(1,:)+0.01*(TRAINTARGET(1,n)-Y(1)).*H; 
        %Updating weights W1 
        G1=(TRAINTARGET(1,n)-Y(1)).*W2(1,:).*H.*(1-H); 
        G1=G1(1:1:1000); 
        W1=W1+0.01*TRAINDATA(n,:)'*(G1);
    end
    JCOL=[JCOL sum((TRAINTARGET-YCOLTRAIN').^2)]; 
end
figure
plot(JCOL) 
title('Convergence of the Multi-class BPNN')
YCOLTEST=[];
for n=1:1:10
    H=logsig(TESTDATA(n,:)*W1); 
    H=[H 1];
    Y=H*W2';
    Y=1/(1+exp(-Y));
    YCOLTEST=[YCOLTEST;Y];
end
figure
subplot(2,1,1)
stem(YCOLTRAIN,'k-')
hold on
stem(TRAINTARGET,'k*')
subplot(2,1,2)
stem(YCOLTEST,'k-')
hold on 
stem(TESTTARGET,'k*')
PART1=YCOLTEST;
%In the same fashion, ANN 2 is trained using elements of the matrix PX2 %as the target values corresponding to the input %CLASS{1} and CLASS{2} to model posterior density
%function of class 2 
temp1=cell2mat(CLASS1'); 
temp2=cell2mat(CLASS2'); 
TRAINDATA=[temp1(1:1:5,:)  ones(5,1); temp2(1:1:5,:) ones(5,1)];
TESTDATA=[temp1(6:1:10,:) ones(5,1);temp2(6:1:10,:) ones(5,1)];
temp=reshape(PX2,1,20);
TRAINTARGET=[temp([1:1:5]) temp([11:1:15])];
TESTTARGET=[temp([6:1:10]) temp([16:1:20])];
W1=randn(2501,1000)*0.5;
W2=randn(1,1001)*0.5; 
JCOL=[];
for iteration=1:1:20
    YCOLTRAIN=[];
    for n=1:1:10
        H=logsig(TRAINDATA(n,:)*W1); 
        H=[H 1];
        Y=H*W2';
        Y=1/(1+exp(-Y)); YCOLTRAIN=[YCOLTRAIN;Y(1)];
        %Updating weights W2 
        W2(1,:)=W2(1,:)+0.01*(TRAINTARGET(1,n)-Y(1)).*H; 
        %Updating weights W1 
        G1=(TRAINTARGET(1,n)-Y(1)).*W2(1,:).*H.*(1-H); 
        G1=G1(1:1:1000); 
        W1=W1+0.01*TRAINDATA(n,:)'*(G1);
    end
    JCOL=[JCOL sum((TRAINTARGET-YCOLTRAIN').^2)]; 
end
figure
plot(JCOL)
title('Convergence of the Multi-class BPNN')
YCOLTEST=[];
for n=1:1:10
    H=logsig(TESTDATA(n,:)*W1); 
    H=[H 1]; 
    Y=H*W2';
    Y=1/(1+exp(-Y));
    YCOLTEST=[YCOLTEST;Y];
end
figure 
subplot(2,1,1)
stem(YCOLTRAIN,'k-') 
hold on 
stem(TRAINTARGET,'k*') 
subplot(2,1,2)
stem(YCOLTEST,'k-')
hold on 
stem(TESTTARGET,'k*') 
PART2=YCOLTEST;
[p,q]=max([PART1 PART2]') 
REF=[ones(1,5) ones(1,5)*2];
POS=length(find((REF-q)==0))*10; 
figure 
subplot(2,1,1) 
stem(REF) 
subplot(2,1,2)
stem(q,'r')

