Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simulate the following scenario: in a telephone survey of 100 people, 45% stated

ID: 3859726 • Letter: S

Question

Simulate the following scenario: in a telephone survey of 100 people, 45% stated their preference for candidate A, 48% prefer candidate B, and the rest are undecided.

USE MATLAB

(a) Modify the above code to run this simulation 10000 times, storing the results. That is, the variables voteA, voteB and voteUndecided should now be arrays of 10000 elements, and create a loop to run this 10000 times.

(b) Use the command hist(voteA, 100) to make a histograph showing the distribution of the votes for candidate A using 100 bins. Similar for voteB and voteUndecided. Hand in the printouts of the three histograms and your code.

(c) Among the 10000 simulations, how many resulted in candidate A getting at least 50 votes? How many resulted in candidate B getting at least 50 votes? How many simulations resulted in candidate A getting more votes than candidate B?

(d) Calculate the probability that candidate A will get at least 50% of the vote using two ways: (i) summing up the binomial distribution (using matlab) and (ii) approximating binomial distribution by the normal distribution. Comment how your results compare to the simulation in (c)

(e) Calculate the probability that A will get more votes than B, using using (i) binomial and (ii) normal distributions. Comment how your results compare to the simulation in (c).

My work so far

voteA = zeros(100,10000);
voteB = zeros(100,100000);
voteUn = zeros(100,10000);


voteA =0; voteB =0; voteUn =0;
nsim=10000;
N1=100;

for sim=1:nsim
for i=1:N1
oo(i) = rand;
if oo(i) <= 0.45
voteA = (voteA+1);
elseif oo(i) <= 0.45+0.48
voteB = (voteB+1);
else
voteUn = (voteUn+1);
end;
end;
sum(voteA)
sum(voteB)
sum(voteUn)
exvoteA=voteA/nsim
exvoteB=voteB/nsim
exvoteUn=voteUn/nsim


end;

t=1:1000;

plot(t, exvoteA, 'o-g', t, exvoteB, 'x-r', t, exvoteUn, 'd-k')

Explanation / Answer

(a)

clear all;
close all;
voteA = zeros(100,10000);
voteB = zeros(100,100000);
voteUn = zeros(100,10000);

voteA =0; voteB =0; voteUn =0;
nsim=10000;
N1=100;

for sim=1:nsim
for i=1:N1
oo = rand;
if(oo <=0.45)
voteA(i,sim) = 1;
elseif(oo <= 0.93)
voteB(i,sim) = 1;
else
voteUn(i,sim) = 1;
end
end;
end

sum(sum(voteA) + sum(voteB) + sum(voteUn))

(b)

1. Histogram of voteA

clear all;

close all;
voteA = zeros(100,10000);
voteB = zeros(100,100000);
voteUn = zeros(100,10000);

voteA =0; voteB =0; voteUn =0;
nsim=10000;
N1=100;

for sim=1:nsim
for i=1:N1
oo = rand;
if(oo <=0.45)
voteA(i,sim) = 1;
elseif(oo <= 0.93)
voteB(i,sim) = 1;
else
voteUn(i,sim) = 1;
end
end;
end
hist(sum(voteA),100); % for hist of voteA

2. Histogram of voteB

clear all;

close all;
voteA = zeros(100,10000);
voteB = zeros(100,100000);
voteUn = zeros(100,10000);

voteA =0; voteB =0; voteUn =0;
nsim=10000;
N1=100;

for sim=1:nsim
for i=1:N1
oo = rand;
if(oo <=0.45)
voteA(i,sim) = 1;
elseif(oo <= 0.93)
voteB(i,sim) = 1;
else
voteUn(i,sim) = 1;
end
end;
end
hist(sum(voteB),100); % for hist of voteB

3. Histogram of voteUn

clear all;

close all;
voteA = zeros(100,10000);
voteB = zeros(100,100000);
voteUn = zeros(100,10000);

voteA =0; voteB =0; voteUn =0;
nsim=10000;
N1=100;

for sim=1:nsim
for i=1:N1
oo = rand;
if(oo <=0.45)
voteA(i,sim) = 1;
elseif(oo <= 0.93)
voteB(i,sim) = 1;
else
voteUn(i,sim) = 1;
end
end;
end

hist(sum(voteUn),100); %for hist of voteUn

(c)

1. candidate A getting at least 50 votes

clear all;
close all;
voteA = zeros(100,10000);
voteB = zeros(100,100000);
voteUn = zeros(100,10000);

voteA =0; voteB =0; voteUn =0;
nsim=10000;
N1=100;

for sim=1:nsim
for i=1:N1
oo = rand;
if(oo <=0.45)
voteA(i,sim) = 1;
elseif(oo <= 0.93)
voteB(i,sim) = 1;
else
voteUn(i,sim) = 1;
end
end;
end

tmp1 = (sum(voteA,1));
tmp2 = (sum(voteB,1));
A_GT50 = 0;
B_GT50 = 0;
A_GTB = 0;
for i = 1:10000
if(tmp1(1,i) >= 50)
A_GT50 = A_GT50 + 1;
end
end
A_GT50

2. candidate B getting at least 50 votes

clear all;
close all;
voteA = zeros(100,10000);
voteB = zeros(100,100000);
voteUn = zeros(100,10000);

voteA =0; voteB =0; voteUn =0;
nsim=10000;
N1=100;

for sim=1:nsim
for i=1:N1
oo = rand;
if(oo <=0.45)
voteA(i,sim) = 1;
elseif(oo <= 0.93)
voteB(i,sim) = 1;
else
voteUn(i,sim) = 1;
end
end;
end

tmp1 = (sum(voteA,1));
tmp2 = (sum(voteB,1));
A_GT50 = 0;
B_GT50 = 0;
A_GTB = 0;
for i = 1:10000
if(tmp2(1,i) >= 50)
B_GT50 = B_GT50 + 1;
end
end
B_GT50
3.candidate A getting more votes than candidate B

clear all;
close all;
voteA = zeros(100,10000);
voteB = zeros(100,100000);
voteUn = zeros(100,10000);

voteA =0; voteB =0; voteUn =0;
nsim=10000;
N1=100;

for sim=1:nsim
for i=1:N1
oo = rand;
if(oo <=0.45)
voteA(i,sim) = 1;
elseif(oo <= 0.93)
voteB(i,sim) = 1;
else
voteUn(i,sim) = 1;
end
end;
end

tmp1 = (sum(voteA,1));
tmp2 = (sum(voteB,1));
A_GT50 = 0;
B_GT50 = 0;
A_GTB = 0;
for i = 1:10000
if(tmp1(1,i) > tmp2(1,i))
A_GTB = A_GTB + 1;
end   
end
A_GTB

Please note that you have posted multiple subparts, according to chegg rules I am supposed to answer only initial 4 subparts.