function [ent] = entropy4(cur,n)

global DST

ent = 0;

T = DST(cur).T(n);

if(DST(cur).type == 3)
    
    entx(1) = .5 *DST(cur).xdim + .5 *(log( (2*pi)^DST(cur).xdim * safedet(DST(cur).sigma{n}(:,:,1)) ) );
    
    if(DST(cur).missing{n}(1))
        
        sigmaxy = DST(cur).sigma{n}(:,:,1) * DST(cur).C';
        sigmayy = DST(cur).C * DST(cur).sigma{n}(:,:,1) * DST(cur).C' + DST(cur).R;

        entxy(1) = .5 * (DST(cur).xdim + DST(cur).ydim) + .5 * log( (2*pi)^(DST(cur).xdim +  DST(cur).ydim) * ...
                              safedet([DST(cur).sigma{n}(:,:,1) sigmaxy ;...
                                   sigmaxy' sigmayy] ) );
                           
        ent = ent + entxy(1);

    else
        ent = ent + entx(1);
    end

    
    
%           DST(cur).sigma{n}(:,:,1) - DST(cur).sigma{n}(:,:,1)'
%         
%         eig(symmat(DST(cur).sigma{n}(:,:,1))) 
%         
%         
%           DST(cur).sigma{n}(:,:,2) - DST(cur).sigma{n}(:,:,2)'
%         
%         eig(symmat(DST(cur).sigma{n}(:,:,2))) 
 
 
    for t=2:T
       
        entx(t) = .5 *DST(cur).xdim + .5 *(log( (2*pi)^DST(cur).xdim * safedet(DST(cur).sigma{n}(:,:,t)) ) );
               
        entxx(t) = DST(cur).xdim + .5 * ( log( (2*pi)^(2*DST(cur).xdim) * ...
                                      safedet([DST(cur).sigma{n}(:,:,t) DST(cur).sigma2{n}(:,:,t) ;...
                                           DST(cur).sigma2{n}(:,:,t)' DST(cur).sigma{n}(:,:,t-1)] ) ) );
  

        if(DST(cur).missing{n}(t))
            
            sigmaxy = DST(cur).sigma{n}(:,:,t) * DST(cur).C';
            sigmayy = DST(cur).C * DST(cur).sigma{n}(:,:,t) * DST(cur).C' + DST(cur).R;

            entxy(t) = .5 * (DST(cur).xdim + DST(cur).ydim) + .5 * ( log( (2*pi)^(DST(cur).xdim +  DST(cur).ydim) * ...
                                  safedet([DST(cur).sigma{n}(:,:,t) sigmaxy ;...
                                       sigmaxy' sigmayy] ) ) );
                                              
            ent = ent + entxy(t) - entx(t);
            
        end
        
        ent = ent + entxx(t) - entx(t-1);
        
    end

    
%HMM or chain      
else

%     ent = ent - sum(sum(sum((DST(cur).xi{n} + 1e-300) .* log(DST(cur).xi{n} + 1e-300))));
%     ent = ent + sum(sum( (DST(cur).s{n}(:,2:T-1) + 1e-300).* log( (DST(cur).s{n}(:,2:T-1)+ 1e-300) ) ));

     ent4 = sum(sum(sum((DST(cur).xi{n}) .* log(DST(cur).xi{n}))));
     
     ent5 = sum(sum( (DST(cur).s{n}(:,2:T-1)).* log( (DST(cur).s{n}(:,2:T-1)) ) ) );

     ent = ent - ent4 + ent5;
     %ent = ent + 


end