sir=[0 3 1 0 3 0 2 1 3 2 1 0 2 1 3 1 0 2 1 0 2 3 2 3 0 2 1 0 3 0 2 1 3 1 0 2 1 0 2 1 0 2 3 1 0 3 0 2 1 0 2 1 0 2 1 0 3 0 2 1 0 2 1 0 2 1 0 3 2 3 1 0 2 1 0 3 1 0 2 3 1 3 2 1 0 2 3 0 2 1 0 2 1 0 2 1 0 2 3 3 0]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%  A one state model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=0:3
    histo(i+1)=sum(sir==i);
end
histo
pist=histo/sum(histo);
h1(1)=-sum(pist.*log2(pist));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%  A two state model
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
warning off
A=[0 0 1 0
    3 0 3 0 ];
hh2=[];Aaa=[];
for machines=0:(2^8-1)
    aa= bitget(machines,8:-1:1);
    A = [aa(1:4); aa(5:8)];
    s_crt=0;
    for i=1:length(sir)
        symbol=sir(i);
        state(i)=s_crt;
        %s_next=mod1(s_crt,symbol,A);
        s_next=A(s_crt+1,symbol+1);
        s_crt=s_next;
    end
    for i=0:1
        for j=0:3
            counti(i+1,j+1)=sum((state==i).*(sir==j));
        end
    end
    for i=0:1
        for j=0:3
            counti(i+1,j+1)=sum((state==i).*(sir==j));
        end
        histo=counti(i+1,:);
        ivalid=find(histo>0);
        pist=histo/sum(histo);
        h2(i+1)=-sum(pist(ivalid).*log2(pist(ivalid)));
    end
    pstate=sum(counti')/sum(sum(counti));
    hh2=[hh2 sum(pstate.*h2)];
    hh3(machines+1)=sum(pstate.*h2);
    Aaa=[Aaa;A];
end

[val ind]=sort(hh3);
for i=1:length(ind)
    machines=ind(i)-1;
    aa= bitget(machines,8:-1:1);
    A = [aa(1:4); aa(5:8)];
    s_crt=0;
    for i=1:length(sir)
        symbol=sir(i);
        state(i)=s_crt;
        %s_next=mod1(s_crt,symbol,A);
        s_next=A(s_crt+1,symbol+1);
        s_crt=s_next;

        if(s_next==3)
            error3
        end
    end
    for i=0:1
        for j=0:3
            counti(i+1,j+1)=sum((state==i).*(sir==j));
        end
    end
    for i=0:1
        for j=0:3
            counti(i+1,j+1)=sum((state==i).*(sir==j));
        end
        histo=counti(i+1,:);
        ivalid=find(histo>0);
        pist=histo/sum(histo);
        h2(i+1)=-sum(pist(ivalid).*log2(pist(ivalid)));
    end
    pstate=sum(counti')/sum(sum(counti));
    hh2=[hh2 sum(pstate.*h2)];
    clf
    %hh=plot(0,5,'o','MarkerSize',20)
    xx=1:500;
    circ=[];elip=[];
    circ(1,:)= 5*cos(2*pi*xx/500);
    circ(2,:)= 5*sin(2*pi*xx/500);
    elip(1,:)= 8*cos(2*pi*xx/520-pi/2+pi/15);
    elip(2,:)= 20*sin(2*pi*xx/520-pi/2+pi/15);
    elip=[elip(1,:) elip(1,1)+3*(0:0.1:1) elip(1,1)+1*(0:0.1:1)
        elip(2,:) elip(2,1)+1*(0:0.1:1) elip(2,1)+3*(0:0.1:1)];

    plot(0,-30,'.k',100,70,'k.')
    hold on
    hhp=plot(circ(1,:), 5+circ(2,:),'-b','LineWidth',3);
    if(sum(A(1,:)==0)>0)
        plot(elip(1,:), 30+elip(2,:))
    end

    plot(60+circ(1,:), 5+circ(2,:),'-b','LineWidth',3)
    if(sum(A(2,:)==1)>0)
        plot(60+elip(1,:), 30+elip(2,:),'-b')
    end
    if(sum(A(1,:)==1)>0)
        plot( [60-9 9],[8 8],'b-',[60-9-3 60-9],[8+1.5 8],'b-',[60-9-3 60-9],[8-1.5 8],'b-')
    end
    if(sum(A(2,:)==0)>0)
        plot( [60-9 9],[2 2],'b-',[9+3 9],[2+1.5 2],'b-',[9+3 9],[2-1.5 2],'b-')
    end
    for i=1:4
        if( A(1,i)==0 )
            hhpp=text(11,46-(i-1)*4,[6*16+i   '   n_' 6*16+i '=' num2str(counti(1,i))],'FontSize',14);
        end
    end

    for i=1:4
        if(A(1,i)==1)
            text(21,25-(i-1)*4,[6*16+i   '   n_' 6*16+i '=' num2str(counti(1,i))],'FontSize',14)
        end
    end
    for i=1:4
        if(A(2,i)==0)
            text(21,-5-(i-1)*4,[6*16+i   '   n_' 6*16+i '=' num2str(counti(2,i))],'FontSize',14)
        end
    end
    for i=1:4
        if(A(2,i)==1)
            text(72,46-(i-1)*4,[6*16+i   '   n_' 6*16+i '=' num2str(counti(2,i))],'FontSize',14)
        end
    end
    text(60,-20,['Entropy = ' num2str(sum(pstate.*h2))],'FontSize',14)
    A
    % pause
    axis off
end
sir =

  Columns 1 through 13

     0     3     1     0     3     0     2     1     3     2     1     0     2

  Columns 14 through 26

     1     3     1     0     2     1     0     2     3     2     3     0     2

  Columns 27 through 39

     1     0     3     0     2     1     3     1     0     2     1     0     2

  Columns 40 through 52

     1     0     2     3     1     0     3     0     2     1     0     2     1

  Columns 53 through 65

     0     2     1     0     3     0     2     1     0     2     1     0     2

  Columns 66 through 78

     1     0     3     2     3     1     0     2     1     0     3     1     0

  Columns 79 through 91

     2     3     1     3     2     1     0     2     3     0     2     1     0

  Columns 92 through 101

     2     1     0     2     1     0     2     3     3     0


histo =

    30    26    26    19


A =

     1     0     0     0
     0     0     0     0


A =

     1     0     0     0
     0     1     0     0


A =

     1     0     0     0
     1     0     0     0


A =

     1     0     0     0
     1     1     0     0


A =

     0     0     1     1
     0     1     1     1


A =

     0     1     1     1
     0     1     1     1


A =

     1     0     1     1
     0     1     1     1


A =

     1     1     1     1
     0     1     1     1


A =

     1     0     1     1
     0     0     1     0


A =

     1     0     1     1
     1     0     1     0


A =

     1     0     0     1
     1     0     1     1


A =

     1     0     1     1
     1     0     1     1


A =

     1     1     0     1
     1     0     1     1


A =

     1     1     1     1
     1     0     1     1


A =

     1     0     1     0
     0     0     0     0


A =

     1     0     1     0
     1     0     0     0


A =

     1     0     1     0
     0     0     1     0


A =

     1     0     1     0
     1     0     1     0


A =

     1     0     1     1
     1     0     0     0


A =

     1     0     0     1
     1     0     0     0


A =

     0     1     0     0
     0     0     0     0


A =

     0     1     0     0
     0     0     1     0


A =

     0     1     0     0
     0     1     0     0


A =

     0     1     0     0
     0     1     1     0


A =

     1     1     0     1
     0     1     0     0


A =

     1     0     0     1
     1     0     1     0


A =

     0     0     1     0
     0     0     0     0


A =

     0     0     1     0
     0     0     1     0


A =

     0     0     1     0
     1     0     0     0


A =

     0     0     1     0
     1     0     1     0


A =

     0     1     1     1
     0     1     0     0


A =

     0     1     0     1
     0     1     0     0


A =

     1     0     0     0
     0     0     1     0


A =

     1     0     0     0
     1     0     1     0


A =

     0     0     1     1
     0     0     1     1


A =

     0     1     1     1
     0     0     1     1


A =

     0     1     1     1
     0     1     0     1


A =

     0     1     1     1
     0     1     1     0


A =

     1     1     1     1
     0     1     0     1


A =

     1     1     0     0
     1     0     0     0


A =

     1     1     0     0
     1     1     0     0


A =

     0     0     1     0
     0     0     0     1


A =

     0     1     0     1
     0     1     0     1


A =

     0     1     0     1
     0     1     1     0


A =

     0     1     0     1
     1     1     0     1


A =

     0     1     1     1
     1     1     0     1


A =

     1     1     0     1
     0     1     0     1


A =

     1     0     0     1
     0     0     1     0


A =

     1     1     0     1
     1     1     0     1


A =

     1     1     1     1
     1     1     0     1


A =

     1     0     1     0
     1     1     0     0


A =

     0     0     1     0
     0     0     1     1


A =

     1     1     1     0
     1     1     0     1


A =

     1     0     0     1
     1     0     0     1


A =

     1     0     1     1
     1     0     0     1


A =

     1     1     0     1
     0     1     1     0


A =

     1     0     0     0
     1     0     0     1


A =

     0     1     0     1
     0     1     1     1


A =

     1     1     0     0
     1     1     0     1


A =

     1     1     0     1
     0     1     1     1


A =

     0     0     1     1
     0     1     0     1


A =

     1     0     0     1
     0     0     0     0


A =

     0     1     1     0
     0     1     0     0


A =

     0     1     1     0
     0     1     1     0


A =

     0     1     1     0
     1     1     0     1


A =

     0     1     1     0
     0     1     1     1


A =

     1     0     1     1
     0     0     0     0


A =

     0     1     0     0
     1     1     0     1


A =

     1     0     1     1
     0     0     1     1


A =

     1     1     1     1
     0     0     1     1


A =

     0     0     1     1
     1     0     1     1


A =

     0     1     1     1
     1     0     1     1


A =

     0     0     1     0
     1     0     0     1


A =

     1     0     0     0
     1     1     0     1


A =

     0     1     1     0
     0     0     0     1


A =

     0     0     1     0
     1     0     1     1


A =

     0     0     1     1
     0     0     1     0


A =

     0     1     1     0
     0     0     1     1


A =

     0     1     1     1
     0     0     1     0


A =

     0     0     1     0
     0     1     1     1


A =

     0     1     1     1
     0     0     0     0


A =

     1     0     1     0
     0     1     0     0


A =

     1     1     1     0
     1     0     0     1


A =

     1     0     1     0
     1     0     1     1


A =

     0     1     0     0
     1     0     0     0


A =

     0     1     0     0
     1     1     0     0


A =

     0     0     1     1
     0     0     0     1


A =

     1     1     1     1
     0     1     1     0


A =

     1     1     0     0
     1     0     0     1


A =

     1     1     1     1
     1     0     0     0


A =

     1     1     0     1
     1     0     0     0


A =

     1     0     1     1
     1     1     0     0


A =

     1     1     0     0
     0     0     0     0


A =

     1     1     0     0
     0     1     0     0


A =

     1     1     0     1
     1     1     0     0


A =

     1     0     0     1
     1     1     0     0


A =

     1     1     1     0
     1     1     0     0


A =

     1     1     1     1
     0     1     0     0


A =

     1     1     1     1
     1     0     1     0


A =

     1     0     1     0
     1     0     0     1


A =

     1     0     0     1
     1     1     0     1


A =

     1     0     1     1
     1     1     0     1


A =

     0     1     1     0
     0     0     0     0


A =

     0     1     1     0
     0     0     1     0


A =

     0     1     1     1
     0     0     0     1


A =

     0     0     1     0
     0     1     0     0


A =

     0     0     1     0
     0     1     1     0


A =

     0     0     1     1
     0     1     0     0


A =

     0     0     1     1
     0     1     1     0


A =

     0     1     0     0
     0     1     0     1


A =

     1     1     0     1
     1     0     0     1


A =

     1     1     1     1
     1     0     0     1


A =

     1     1     1     0
     1     0     0     0


A =

     0     0     1     1
     0     0     0     0


A =

     0     1     1     0
     0     1     0     1


A =

     1     0     1     1
     0     1     0     1


A =

     1     0     0     0
     1     0     1     1


A =

     1     1     1     1
     1     1     0     0


A =

     0     1     0     1
     0     0     0     0


A =

     1     1     1     0
     1     0     1     1


A =

     0     0     1     1
     1     0     1     0


A =

     1     1     0     1
     1     0     1     0


A =

     0     1     0     0
     0     1     1     1


A =

     0     1     1     1
     1     0     1     0


A =

     1     1     1     0
     0     1     1     1


A =

     1     0     1     0
     1     1     0     1


A =

     1     0     1     0
     0     0     1     1


A =

     0     0     1     0
     0     1     0     1


A =

     0     1     0     0
     0     0     0     1


A =

     0     1     0     0
     1     0     0     1


A =

     1     0     0     0
     0     0     0     1


A =

     0     1     0     1
     1     0     0     0


A =

     1     1     1     0
     1     0     1     0


A =

     0     1     0     1
     0     0     1     0


A =

     1     1     0     1
     0     0     0     0


A =

     1     0     0     0
     0     1     0     1


A =

     1     0     1     0
     0     1     1     1


A =

     0     1     0     1
     1     1     0     0


A =

     0     1     0     1
     0     0     0     1


A =

     1     1     1     0
     0     0     1     1


A =

     1     0     0     1
     0     0     1     1


A =

     1     1     0     0
     0     1     0     1


A =

     0     1     1     0
     1     0     1     0


A =

     1     1     0     0
     1     0     1     1


A =

     0     1     1     0
     1     0     1     1


A =

     0     1     0     1
     1     0     0     1


A =

     1     1     1     1
     0     0     1     0


A =

     0     1     0     0
     0     0     1     1


A =

     1     1     1     0
     0     0     1     0


A =

     1     0     1     1
     0     1     0     0


A =

     1     1     0     0
     1     0     1     0


A =

     1     1     0     0
     0     1     1     0


A =

     1     0     0     1
     0     0     0     1


A =

     1     0     0     0
     0     0     1     1


A =

     1     1     0     0
     0     0     0     1


A =

     0     1     0     1
     0     0     1     1


A =

     1     1     1     0
     0     1     1     0


A =

     1     1     0     1
     0     0     0     1


A =

     0     0     1     1
     1     0     0     0


A =

     1     0     0     1
     0     1     0     0


A =

     1     0     1     1
     0     0     0     1


A =

     1     0     1     0
     0     0     0     1


A =

     1     1     1     0
     0     1     0     0


A =

     1     1     0     1
     0     0     1     0


A =

     1     1     1     0
     0     1     0     1


A =

     1     1     0     0
     0     1     1     1


A =

     0     1     1     0
     1     0     0     1


A =

     1     0     1     0
     0     1     0     1


A =

     0     1     1     1
     1     1     0     0


A =

     0     0     1     1
     1     0     0     1


A =

     0     1     1     0
     1     1     0     0


A =

     1     0     1     1
     0     1     1     0


A =

     0     0     0     1
     1     0     1     1


A =

     1     0     0     0
     1     1     1     0


A =

     1     0     1     0
     1     1     1     0


A =

     1     0     1     1
     1     1     1     0


A =

     1     1     0     0
     0     0     1     1


A =

     0     0     0     1
     1     0     1     0


A =

     0     1     0     1
     1     0     1     1


A =

     0     0     0     1
     0     1     1     1


A =

     0     0     0     1
     0     1     0     1


A =

     0     0     0     1
     0     1     0     0


A =

     0     1     0     0
     1     1     1     0


A =

     0     0     0     1
     1     0     0     0


A =

     1     0     1     0
     0     1     1     0


A =

     0     0     0     1
     1     0     0     1


A =

     0     1     1     1
     1     0     0     0


A =

     0     1     0     0
     1     0     1     0


A =

     0     0     0     1
     1     1     0     1


A =

     1     0     0     1
     0     1     0     1


A =

     1     0     0     1
     1     1     1     0


A =

     0     0     1     0
     1     1     1     0


A =

     0     1     1     0
     1     0     0     0


A =

     0     1     1     1
     1     0     0     1


A =

     0     1     0     1
     1     1     1     0


A =

     0     1     0     1
     1     0     1     0


A =

     1     1     1     0
     1     1     1     0


A =

     0     0     0     1
     0     1     1     0


A =

     0     1     1     1
     1     1     1     0


A =

     0     1     1     0
     1     1     1     0


A =

     0     0     1     1
     1     1     1     0


A =

     0     0     0     1
     0     0     0     1


A =

     1     1     1     1
     1     1     1     0


A =

     1     0     0     0
     0     1     1     0


A =

     0     0     1     1
     1     1     0     1


A =

     0     0     0     1
     0     0     0     0


A =

     1     0     0     1
     0     1     1     1


A =

     1     1     1     0
     0     0     0     0


A =

     0     0     1     0
     1     1     0     0


A =

     1     1     0     0
     1     1     1     0


A =

     0     0     0     1
     0     0     1     1


A =

     0     1     0     0
     1     0     1     1


A =

     1     1     1     1
     0     0     0     1


A =

     0     0     0     1
     1     1     1     1


A =

     0     0     1     1
     1     1     1     1


A =

     0     1     0     1
     1     1     1     1


A =

     0     1     1     1
     1     1     1     1


A =

     0     0     0     1
     1     1     0     0


A =

     0     0     0     1
     0     0     1     0


A =

     1     0     0     0
     1     1     1     1


A =

     1     0     0     1
     1     1     1     1


A =

     1     0     1     0
     1     1     1     1


A =

     1     0     1     1
     1     1     1     1


A =

     1     1     0     0
     1     1     1     1


A =

     1     1     0     1
     1     1     1     1


A =

     1     1     1     0
     1     1     1     1


A =

     1     1     1     1
     1     1     1     1


A =

     1     1     0     1
     1     1     1     0


A =

     0     1     0     0
     1     1     1     1


A =

     0     1     1     0
     1     1     1     1


A =

     1     1     0     1
     0     0     1     1


A =

     0     0     1     0
     1     1     1     1


A =

     1     1     0     0
     0     0     1     0


A =

     1     1     1     0
     0     0     0     1


A =

     1     1     1     1
     0     0     0     0


A =

     1     0     0     1
     0     1     1     0


A =

     0     0     1     0
     1     1     0     1


A =

     1     0     0     0
     0     1     1     1


A =

     0     0     0     1
     1     1     1     0


A =

     0     0     1     1
     1     1     0     0


A =

     0     0     0     0
     0     0     0     0


A =

     0     0     0     0
     0     0     0     1


A =

     0     0     0     0
     0     0     1     0


A =

     0     0     0     0
     0     0     1     1


A =

     0     0     0     0
     0     1     0     0


A =

     0     0     0     0
     0     1     0     1


A =

     0     0     0     0
     0     1     1     0


A =

     0     0     0     0
     0     1     1     1


A =

     0     0     0     0
     1     0     0     0


A =

     0     0     0     0
     1     0     0     1


A =

     0     0     0     0
     1     0     1     0


A =

     0     0     0     0
     1     0     1     1


A =

     0     0     0     0
     1     1     0     0


A =

     0     0     0     0
     1     1     0     1


A =

     0     0     0     0
     1     1     1     0


A =

     0     0     0     0
     1     1     1     1