# 041114Aaron

Jump to navigation Jump to search
Below is my code and results. I think the last problem needs to be modified and the whole code is incredibly inefficient and needs to be updated.
```knightmarg=sum(strncmpi(A(:,1),'N',1)==1);
pawnmarg=100000-knightmarg;
winmarg=sum(strncmpi(A(:,3),'W',1)==1);
lossmarg=sum(strncmpi(A(:,3),'B',1)==1);
drawmarg=100000-winmarg-lossmarg;
j=0;
k=0;
l=0;
m=0;
B=[];
for i=1:100000
if (strncmpi(A(i,1),'N',1)==1) && (strncmpi(A(i,3),'W',1)==1)
j=j+1;
end
if (strncmpi(A(i,1),'N',1)==1) && (strncmpi(A(i,3),'B',1)==1)
k=k+1;
end
if (strncmpi(A(i,1),'N',1)==0) && (strncmpi(A(i,3),'W',1)==1)
l=l+1;
end
if (strncmpi(A(i,1),'N',1)==0) && (strncmpi(A(i,3),'B',1)==1)
m=m+1;
end
end
B(1,1)=j;
B(1,2)=k;
B(1,3)=knightmarg-j-k;

B(2,1)=l;
B(2,2)=m;
B(2,3)=pawnmarg-l-m;

%% Chisquare
format long
L=sum(B,2)*sum(B,1)/sum(sum(B));
chis2=sum(sum(sum((L-B).^2./L)))
pval=1-chi2cdf(chis2,2)

%% problem 2
% this matrix will be 4X3 NN pN Np pp by W L T
o=0;
p=0;
q=0;
r=0;
s=0;
t=0;
u=0;
v=0;
w=0;
x=0;
y=0;
z=0;
for i=1:100000
if (strncmpi(A(i,1),'N',1)==1) && (strncmpi(A(i,2),'N',1)==1) && (strncmpi(A(i,3),'W',1)==1)
o=o+1;
end
if (strncmpi(A(i,1),'N',1)==1) && (strncmpi(A(i,2),'N',1)==0) && (strncmpi(A(i,3),'W',1)==1)
p=p+1;
end
if (strncmpi(A(i,1),'N',1)==0) && (strncmpi(A(i,2),'N',1)==1) && (strncmpi(A(i,3),'W',1)==1)
q=q+1;
end
if (strncmpi(A(i,1),'N',1)==0) && (strncmpi(A(i,2),'N',1)==0) && (strncmpi(A(i,3),'W',1)==1)
r=r+1;
end
end
%NN white win
C(1,1)=o; % NNwin
C(1,2)=p; % Np win
C(1,3)=q; %pN win
C(1,4)=r; % pp win

%% second row Black wins (loss)
for i=1:100000
if (strncmpi(A(i,1),'N',1)==1) && (strncmpi(A(i,2),'N',1)==1) && (strncmpi(A(i,3),'B',1)==1)
s=s+1;
end
if (strncmpi(A(i,1),'N',1)==1) && (strncmpi(A(i,2),'N',1)==0) && (strncmpi(A(i,3),'B',1)==1)
t=t+1;
end
if (strncmpi(A(i,1),'N',1)==0) && (strncmpi(A(i,2),'N',1)==1) && (strncmpi(A(i,3),'B',1)==1)
u=u+1;
end
if (strncmpi(A(i,1),'N',1)==0) && (strncmpi(A(i,2),'N',1)==0) && (strncmpi(A(i,3),'B',1)==1)
v=v+1;
end
end
%NN white win
C(2,1)=s; % NNwin
C(2,2)=t; % Np win
C(2,3)=u; %pN win
C(2,4)=v; % pp win

%% third row tie
for i=1:100000
if (strncmpi(A(i,1),'N',1)==1) && (strncmpi(A(i,2),'N',1)==1) && (strncmpi(A(i,3),'D',1)==1)
w=w+1;
end
if (strncmpi(A(i,1),'N',1)==1) && (strncmpi(A(i,2),'N',1)==0) && (strncmpi(A(i,3),'D',1)==1)
x=x+1;
end
if (strncmpi(A(i,1),'N',1)==0) && (strncmpi(A(i,2),'N',1)==1) && (strncmpi(A(i,3),'D',1)==1)
y=y+1;
end
if (strncmpi(A(i,1),'N',1)==0) && (strncmpi(A(i,2),'N',1)==0) && (strncmpi(A(i,3),'D',1)==1)
z=z+1;
end
end
%NN white win
C(3,1)=w; % NNwin
C(3,2)=x; % Np win
C(3,3)=y; %pN win
C(3,4)=x; % pp win

format short
K=sum(C,2)*sum(C,1)/sum(sum(C));
chis3=sum(sum(sum((K-C).^2./K)))
pval2=1-chi2cdf(chis3,6)

%% Problem 3

effirst=sum(strncmpi(A(:,1),'e4',2)==1);
wer=0;
for i=1:100000
if (strncmpi(A(i,1),'e4',2)==1)
wer=wer+1;
F(wer,:)=A(i,:);
end
end

a=0;
b=0;
c=0;
d=0;
e=0;
f=0;
for i=1:45214
if (strncmpi(F(i,2),'N',1)==1) && (strncmpi(F(i,3),'W',1)==1)
a=a+1;
end
if (strncmpi(F(i,2),'N',1)==1) && (strncmpi(F(i,3),'B',1)==1)
b=b+1;
end
if (strncmpi(F(i,2),'N',1)==1) && (strncmpi(F(i,3),'D',1)==1)
c=c+1;
end
if (strncmpi(F(i,2),'N',1)==0) && (strncmpi(F(i,3),'W',1)==1)
d=d+1;
end
if (strncmpi(F(i,2),'N',1)==0) && (strncmpi(F(i,3),'B',1)==1)
e=e+1;
end
if (strncmpi(F(i,2),'N',1)==0) && (strncmpi(F(i,3),'D',1)==1)
f=f+1;
end
end
%%all knights
G(1,1)=a;
G(1,2)=b;
G(1,3)=c;

% all pawns
G(2,1)=d;
G(2,2)=e;
G(2,3)=f;

format short
M=sum(G,2)*sum(G,1)/sum(sum(G));
chis4=sum(sum(sum((M-G).^2./M)))
pval3=1-chi2cdf(chis4,2)

chis2 =

2.111053690929436e+02

pval =

0

chis3 =

1.3592e+04

pval2 =

0

chis4 =

10.0144

pval3 =

0.0067
```