I must calculate an equation as follows:
where k1,k2
are given. I am using MATLAB to compute P
. I think I have a correct implementation fo
You can save results of nchoosek to a table to prevent repeated evaluation of the function, also an implementation of binomial coefficients provided:
%binomial coefficients
function nk=nchoosek2(n, k)
if n-k > k
nk = prod((k+1:n) .* prod((1:n-k).^ (-1/(n-k))));
else
nk = prod((n-k+1:n) .* prod((1:k).^ (-1/k)) ) ;
end
end
%function to store and retrieve results of nchoosek to/from a table
function ret = choose (n,k, D, K1, K2)
persistent binTable = zeros(max([D+1,K1+K2+1]) , D+1);
if binTable(n+1,k+1) == 0
binTable(n+1,k+1) = nchoosek2(n,k);
end
ret = binTable(n+1,k+1);
end
function P = tst()
P=0;k1=150; k2=150; D=200; P=0;
choose(1,0,D,k1,k2);
for i = 0:D-1
for j = j=max(i - k2 , 0):min (i,k1-1)
P=P+choose(k1,j)*choose(k2,i-j)/choose((k1+k2),i);
end
end
end
Your code with nchoosek2 compared with this: online demo