function beta = hmmbackward(a, pi, b, o)

spi = size(pi');
so = size(o');

T = so(1);
N = spi(1);

for i = 1:N
	beta(T, i) = 1;
end

for t = (T-1):-1:1
	for i = 1:N
		beta(t, i) = 0;
		for j = 1:N
			beta(t,i) = beta(t,i) + a(i,j)*b(j,o(t+1))*beta(t+1,j);
		end
	end
end
