I guess there must be an algorithm that does my problem most efficiently but I haven't found it. My problem is to compute a discrete distribution for A, Prob(A). Using conditional Probability, we know: Prob(A)=Prob(A|B,C,D)*Prob(B|C,D)*Prob(C|D)*Prob(D) A,B,C,D are dependent and I only know the expressions for each term above. So in my code, I used 4 layers of for loop:
Solution=zeros(1,max_A) % store Prob(A=0,1,2,3,...max_A) in each cell.
for a =0 to max_A
for b=0 to max_B
for c=0 to max_C
for d=0 to max_D
Result= Prob(A=a|B=b,C=c,D=d)*Prob(B|C,D)*Prob(C|D)*Prob(D)
Solution(a)= Solution(a)+Result % sum up the result
%in each iteration
end
end
end
end
In the real program I am dealing with Prob(A|B,C,D), Prob(B|C,D), Prob(C|D), Prob(D), each invokes a 21 layers of For Loop. It is terribly inefficient and slow. Matlab choked on my code and it has been 5 days that it run.
Really appreciate any idea or demo codes that help me eliminate some of the loops.
Many Thanks! Ester