function pf=convpf(pf1, pf2) % convpf(pf1, pf2) generates a probability function for the convolution of % the given probability functions % % convpf is slow when one pf is discrete and the other is continuous % % actually, it can be slow all the time % % for a pair of discrete pmfs, the x values should be integers, or % convpf will likely give you the wrong answer if ispf(pf1)==0 error('pf1 invalid') end if ispf(pf2)==0 error('pf2 invalid') end x1=xvals(pf1); x2=xvals(pf2); p1=pvals(pf1); p2=pvals(pf2); if isdiscrt(pf1)==0 & isdiscrt(pf2)==0 W1=x1(length(x1))-x1(1); W2=x2(length(x2))-x2(1); x2=(-x2(length(x2):-1:1)); p2=p2(length(p2):-1:1); pf2=[0 1; x2' p2']; conp2=sample(pf2, -W2, W2, (W1+2*W2)/1000); conx=[-W2:(W1+2*W2)/1000:W2]; if ~(length(conp2)==length(conx)) conx=conx(1:(length(conx)-1)); end slen=length(conp2); conp1=sample(pf1, x1(1)-2*W2, x1(1)+W1+2*W2, (W1+2*W2)/1000); newx=[x1(1)-W2:(W1+2*W2)/1000:x1(1)+W1+W2]; if length(conp1)0 index1=1; while newp(index1)==0 index1=index+1; end index2=length(newp); while newp(index2)==0 index2=index2-1; end newp=newp(index1:index2); newx=newx(index1:index2); end pf=[1 1; newx' newp']; end if inrange(ttlprob(pf), 0, 1e-4)==1 disp('Result is zero') elseif ispf(pf)==0 & isdiscrt(pf)==0 newp=newp./ttlprob(pf); pf=[0 1; newx' newp']; end