Page 54
Matlab: Roulette Wheel Selection, Breakpoint Method % set up the outcomes oc(1)=2; oc(2)=3; oc(3)=4; oc(4)=5; % set up the breakpoints bp(1)=.15; bp(2)=bp(1)+.2; bp(3)=bp(2)+.60; bp(4)=1; % end of set up, go here to get samples U=rand; k=1; while( U >= bp(k)) k=k+1; end % k is selected selected = oc(k)
Page 55
Matlab: Roulette Wheel Selection, Density Method i=1; for j=1:15 oc(i)=2; i=i+1; end; for j=1:20 oc(i)=3; i=i+1; end; for j=1:60 oc(i)=4; i=i+1; end; for j=1:5 oc(i)=5; i=i+1; end; % end of setup, go here to get samples k=floor(100*rand)+1; selected = oc(k)
Page 55
Matlab: Roulette Wheel Selection, Vector Method b(1)=.15; b(2)=b(1)+.2; b(3)=b(2)+.6; U=rand(1,10000); %10000 U(0,1) samples w2 = (U < b(1))*2; w3 = (U >=b(1) & U < b(2))*3; w4 = (U >=b(2) & U < b(3))*4; w5 = (U >=b(3))*5; w=w2+w3+w4+w5; % w a random 10000 vector % with the right frequencies
Page 73
Matlab: Normal CDF via Rational Approximation function w = normalCDF(x) a1 = 0.31938153; a2 = -0.356563782; a3 = 1.781477937; a4 = -1.821255978; a5 = 1.330274429; b=0.2316410; c=1/sqrt(2*pi); L=abs(x); K=1./(1+b.*L); w = 1-c*exp(-L.*L/2).*... (a1.*K+a2.*K.^2+a3.*K.^3+a4.*K.^4+a5.*K.^5); msign = -(x <0); mfact = 2*msign+1; w = mfact.*(w+msign);