function[bound] = inferChain3(cur,n,pred)

global DST;

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

bound = 0;

if(cur==1)
    DST(cur).vphi = log(DST(cur).phi); % + 1e-6);    
    for t=2:T
        DST(cur).vPhi(:,:,t) = log(DST(cur).Phi);
    end      
else
          
    DST(cur).vPhi = zeros(DST(cur).S,DST(cur).S,T);
    DST(cur).vphi = zeros(DST(cur).S,1);
    
    for i=1:DST(DST(cur).parent).S
  
         DST(cur).vphi = DST(cur).vphi + DST(DST(cur).parent).s{n}(i,1) * log(DST(cur).phi(:,i));
        
         for t=2:T
             DST(cur).vPhi(:,:,t) = DST(cur).vPhi(:,:,t) + DST(DST(cur).parent).s{n}(i,t) * log(DST(cur).Phi(:,:,i));
        end

    end
end
    
%%for each child
for m=1:length(DST(cur).child)

    %%if the child is an HMM or internal chain
    if( DST(DST(cur).child(m)).type == 1 | DST(DST(cur).child(m)).type == 2)

%         for i=1:DST(DST(cur).child(m)).S

            for j=1:DST(cur).S

                DST(cur).vphi(j) = DST(cur).vphi(j) + sum(DST(DST(cur).child(m)).s{n}(:,1) .* log( DST(DST(cur).child(m)).phi(:,j)) );
                
%                 DST(cur).vphi(j) = DST(cur).vphi(j) + DST(DST(cur).child(m)).s(i,1) * log( DST(DST(cur).child(m)).phi(i,j) );
      
            end
            
            
%         end
 
        for t=2:T

            for k=1:DST(cur).S
 
%                 DST(cur).vPhi(k,:,t)  = DST(cur).vPhi(k,:,t) + DST(DST(cur).child(m)).s{n}(:,t) * DST(DST(cur).child(m)).s{n}(:,t-1)' .* log( DST(DST(cur).child(m)).Phi(:,:,k) );
                DST(cur).vPhi(k,:,t)  = DST(cur).vPhi(k,:,t) + sum(sum(DST(DST(cur).child(m)).xi{n}(:,:,t-1) .* log( DST(DST(cur).child(m)).Phi(:,:,k) )));

            end
        end

    %%else child is lds        
    else

        pt = DST(DST(cur).child(m)).sigma{n}(:,:,1) + DST(DST(cur).child(m)).x{n}(:,1) * DST(DST(cur).child(m)).x{n}(:,1)' ;
        
        for i=1:DST(cur).S
                         
            DST(cur).vphi(i) = DST(cur).vphi(i) - .5 *( trace(DST(DST(cur).child(m)).Q1inv(:,:,i) * pt ) + ...
                DST(DST(cur).child(m)).mu(:,i)' * DST(DST(cur).child(m)).Q1inv(:,:,i) * DST(DST(cur).child(m)).mu(:,i) - ...
                2 * DST(DST(cur).child(m)).x{n}(:,1)' * DST(DST(cur).child(m)).Q1inv(:,:,i) * DST(DST(cur).child(m)).mu(:,i) + ...
                log(safedet(DST(DST(cur).child(m)).Q1(:,:,i) )));
       
        end


        for t=2:T

            % pt-1
            pt1 = pt;

            %pt
            pt = DST(DST(cur).child(m)).sigma{n}(:,:,t) + DST(DST(cur).child(m)).x{n}(:,t) * DST(DST(cur).child(m)).x{n}(:,t)';
            
            %pt,t-1       
            pt2 = DST(DST(cur).child(m)).sigma2{n}(:,:,t) + DST(DST(cur).child(m)).x{n}(:,t) * DST(DST(cur).child(m)).x{n}(:,t-1)' ;
        
            for i=1:DST(cur).S
     
                DST(cur).vPhi(i,:,t) = DST(cur).vPhi(i,:,t) - .5 * ( trace(DST(DST(cur).child(m)).Qinv(:,:,i) * pt) + ...
                    trace(DST(DST(cur).child(m)).A(:,:,i)' * DST(DST(cur).child(m)).Qinv(:,:,i) * DST(DST(cur).child(m)).A(:,:,i) * pt1) + ...
                    -2 * trace(DST(DST(cur).child(m)).Qinv(:,:,i) * DST(DST(cur).child(m)).A(:,:,i) * pt2') + ...
                     log(safedet(DST(DST(cur).child(m)).Q(:,:,i))));
             
            end

        end

    end
    
end

JTChain3(cur,n,0);

if(cur==1)
    
    H = computeH2(cur,n);
    
    bound = bound - H; 
end

for m=1:length(DST(cur).child)
    childH = computeH2(DST(cur).child(m),n);
    
    bound = bound - childH;
end

ent = entropy4(cur,n);

bound = bound + ent;
 