

function [obj,C,Ey]=minloglik(iper,A,y,P1,P2,P3,P4,P5,R,LP,BL,cn,o2)

alfa=0;%non c'e' ritardo nell'ingresso
n=size(A,1);%numero misure da utilizzare
p=size(A,2);%lunghezza risposta impulsiva da stimare
m=size(A,3);%numero risposte impulsive da stimare
%cn = dimensione del bias space che stabilisce se vi sono bias space 
%distinti da considerare in rapporto al valore di P3 e P4

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%DIVISIONE IPERPARAMETRI, b2 e b3 polinomi per il bias space%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%[Lamb  beta  BiasN  BiasP  o2]
%[P1     P2     P3     P4   P5]
l=exp(iper(1:P1));%le varianze delle r.i., se l=1 tutte uguali
b1=iper(P1+1:P1+P2);%i beta per il cambiamento di scala, se b1=1 tutti uguali
if P3==0;
    b2=[];
else
    nBSn=P3/cn;%numero di bias spaces da considerare
    for j=1:nBSn
        b2(j,:)=iper((P1+P2+1+cn*(j-1)):(P1+P2+cn*j));%polinomio poli negativi
    end
end
if P4==0;
    b3=[];
else
    nBSp=P4/cn;%numero di bias spaces da considerare
    for j=1:nBSp
        b3(j,:)=iper((P1+P2+P3+1+cn*(j-1)):(P1+P2+P3+cn*j));%polinomio poli negativi
    end
end
%o2=exp(iper(P1+P2+P3+P4+P5));%varianza innovazione
if LP==1
    Lap=exp(iper(P1+P2+P3+P4+P5));%varianza Laplaciana
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%FEASIBILITY DEL BIAS SPACE%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fl=1;
if b1<0 | b1>=1;%stabilita' del cambiamento di scala
    fl=0;
end
if P3>0;
    for j=1:nBSn;
        q=roots([1 b2(j,:)]);
        for i=1:max(size(q));
            c(i)=norm(q(i));
            cr(i)=real(q(i));
            if c(i)>1 | c(i)<R | real(cr(i))>0;%i poli negativi devono appartenere al semicerchio sinistro
                fl=0;
            end
        end
    end
end
if P4>0;
    for j=1:nBSp;
        q=roots([1 b3(j,:)]);
        for i=1:max(size(q));
            c(i)=norm(q(i));
            cr(i)=real(q(i));
            if c(i)>1 | c(i)<R | real(cr(i))<0;%i poli positivi devono appartenere al semicerchio destro
                fl=0;
            end
        end
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%MARGINAL LIKELIHOOD EVALUATION%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if  fl==0;%non c'e' feasibility del bias space
    obj=+inf;
else 
    if isempty(BL) | max(size(y))<BL;
        Nb=1;
        [C,Ey]=Eybuild(p,alfa,b1,b2,b3,l,A,o2,BL,Nb);%un solo blocco di Ey restituito
        [obj,l1,l2]=symm_invdet(Ey,y);
    else
        obj=0;
        Nb=ceil(max(size(y))/BL);
        [C,Ey,Ey2]=Eybuild(p,alfa,b1,b2,b3,l,A,o2,BL,Nb);%restituisce Nb-1 blocchi in Ey e l'ultimo in Ey2
        for kk=1:Nb;
            if kk<Nb;
                I=[(1+(kk-1)*BL):(kk*BL)];
                [ob,l1,l2]=symm_invdet(Ey(:,:,kk),y(I));
                obj=obj+ob;
            else
                I=[(1+(kk-1)*BL):max(size(y))];
                [ob,l1,l2]=symm_invdet(Ey2,y(I));
                obj=obj+ob;
            end
        end
    end
end
if LP==1;%PENALTY FOR SPARSITY
    l=l.^0.5;
    sl=sum(abs(l));
    %R=Lap*eye(m);
    SDl=sqrt(Lap);%deviazione standard dell'esponenziale che modella le radici dei parametri di regolarizzazione
    obj=obj+m*log((SDl))+(sl/SDl);%2*log(det(sqrt(2)*R))+2*sqrt(2)*(sl/sqrt(Lap));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%obj


