%Bayesianapproachdemo.m
%Bayesian approach % 
clc 
alpha=0.1; 
beta=0.2; 
W1=rand(1,2);
B1=rand(2,1);
W2=rand(2,1);
B2=rand(1,1);
%save WEIGTHS W1 B1 W2 B2 
%DATA GENERATION 
INPUT=rand(1000,1)*2-1;
H=INPUT*W1+repmat(B1',1000,1);
H=1./(1+exp(H)); 
OUTPUT=H*W2+repmat(B2',1000,1);
OUTPUT=1./(1+exp(OUTPUT))+randn(1000,1)*beta;
M=mean(INPUT'); 
sigma=var(INPUT'); 
B1=[];
for i=1:1:size(INPUT,1) 
    B1=[B1 gbf(INPUT(i,:),M',sigma)];
end
%Formulation of matrix 
MAT=[ones(size(INPUT,1),1) B1'];
MAT1=MAT'*MAT; 
%save MAT MAT 
W=pinv(MAT1+(alpha/beta)*eye(size(MAT1,1)))*MAT'* OUTPUT; 
%save W W
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Estimating W using Bayesian approach %For the first input (First row of MAT file) 
load W
%load MAT
WDATA=randn(2,10000)*sqrt(alpha);
%ma=max(max(WDATA)); 
mi=min(min(WDATA)); 
mibias=abs(round((mi*10))); 
%mabias=abs(round((ma*10))); 
PRIOR=zeros(50,50); 
for i=1:1:1000 
    PRIOR(round(WDATA(1,i)*10)+mibias+10,round(WDATA(2,i) *10)+mibias+10)=PRIOR(round(WDATA(1,i)*10)+mibias+10,round(WDATA(2,i) *10)+mibias+10)+1;
end
PRIOR=PRIOR/1000;
POST=PRIOR;
figure(1)
contour(PRIOR) 
LFMAT=zeros(50,50);
COL=[];
sub=0;
for i=1:1:10 
    sub=sub+1;
    for p=1:1:10
        LFMAT=zeros(size(POST,1),size(POST,2));
        for j=1:1:100 
            W1=randn(2,1)*sqrt(alpha); 
            COL=[]; 
            for k=1:1:10000
                COL=[COL MAT(i,:)*W1+randn(1)*beta];
            end
            h=hist(COL,15);
            temp=h/sum(h);
            [U,V]=max(temp);
            LFMAT(round(W1(1)*10)+mibias+10,round(W1(2)*10)+mibias +10)=temp(V);
        end
    end
    figure(2)
    subplot(5,2,sub)
    contour(LFMAT)
    pause(0.1)
    POST=LFMAT.*POST
    figure(3)
    subplot(5,2,sub)
    contour(POST)
    pause(0.1)
end
figure(4)
contour(POST)