%stat351_introduction5.m % run tests >>X=[ 1 1 0 1 1 1 0 0 1 1] X = 1 1 0 1 1 1 0 0 1 1 %Probability of gettig 3 runs of type 1 in our example >>nchoosek(7-1,3-1)*nchoosek(3+1,3)/nchoosek(10,7) ans = 0.5000 %Iteration: For-loop >>P=zeros(1,10) P = 0 0 0 0 0 0 0 0 0 0 >>for i=1:10 P(i)=i; end; >>P P = 1 2 3 4 5 6 7 8 9 10 >>for i=1:2:10 i end; i = 1 i = 3 i = 5 i = 7 i = 9 >>mod(P,2) ans = 1 0 1 0 1 0 1 0 1 0 %Test if the above binary sequence is random %Example on p.72 n1=5; n2=4; n=n1+n2; >>P=zeros(1,n); >>for r=2:2:n P(r)=2*nchoosek(n1-1,r/2-1)*nchoosek(n2-1,r/2-1)/nchoosek(n,n1); end; >>for r=3:2:n P(r)=(nchoosek(n1-1,(r-1)/2)*nchoosek(n2-1,(r-3)/2)+ nchoosek(n1-1,(r-3)/2)... *nchoosek(n2-1,(r-1)/2))/nchoosek(n,n1); end; %You will get error message ??? Error using ==> nchoosek K must be an integer between 0 and N >> P' ans = 0 0.0159 0.0556 0.1905 0.2381 0.2857 0.1429 0.0635 0 >>sum(P) ans = 0.9921 >>r=9 >>P(r)=nchoosek(n1-1,(r-1)/2)*nchoosek(n2-1,(r-3)/2)/nchoosek(n,n1); >>P' ans = 1.0000 >> P' ans = 0 0.0159 0.0556 0.1905 0.2381 0.2857 0.1429 0.0635 0.0079 %At 5% level reject if r <=2 or r >=9