In: Math
(1)Using the Matlab code developed in Software Assignment #1:
a. Convert the code that generates the random number (H,T) with equal probabilities into a function called myBernolli(p, S) that takes as an input the probability of success p and S is the outcome defined as success (either T or H) and returns the outcome of the trial (either T or H).
b. Test that your function is actually producing the successful outcome with probability p by running the function in a loop of 1000 trials and counting how many times success is produced (it should be close to p*1000).
c. Write a Matlab function called myBinomial(n,p) that takes as an input the total number of trials n, the probability of success p and the outcome defined as success S and uses myBernolli() to return as an output the number of successes x.
d. Write a Matlab function called myGeometric() that takes as an input the probability of success p and the outcome defined as success S and uses myBernolli() to return as an output the number of trials till first success x.
e. Verify that myBinomial() and myGeometric() generates values that follow Binomial and Geometric Distributions by running each of them 5000 times in a loop and plotting a histogram of the random variable x generated from each.
Hints: Random numbers with probability p are generated in Matlab using the command function rand(). Read the help on Matlab to know how to use the function by typing help rand in Matlab command line. Histogram plots [hist()] is a function in Matlab. Read its help to know how to produce
ans :
code to copy::
a. bernoulis:
%Bernoulli’s function
function trialOutcome=myBernolli(p,S)
x=rand(1,1); %generate uniform random value from 0 to1
%check if random value is LESS THAN probability
%if so assign triout with 1
triout=(x<=p);
if triout==S
t=1;
else
t=0;
end
%assign to output variable
trialOutcome=t;
end
b. running Bernoulis function:
cnt=0;
for nn=1:1000 %1000 trials
%call function to check success outcome
t=myBernolli(0.56,1);
%if success outcome then increment the counter
if(t==1)
cnt=cnt+1;
end
end
%display the number of success outcome for given probability
disp("no of times success produced:");
disp(cnt);
c.binomial distribution:
%Bernoulli’s function
function trialOutcome=myBernolli(p,S)
x=rand(1,1); %generate uniform random value from 0 to1
%check if random value is less than probability
%if so assign triout with 1
triout=(x<=p);
if triout==S
t=1;
else
t=0;
end
%assign to output variable
trialOutcome=t;
end
%function for binomial distribution
function x=myBinomial(n,p)
ns=0; %counter for number of successes
S=0; %set success outcome
for kk=1:n %till said trials
if(tt==1) %if SUCCESS OUTCOME
end %if end
end%for end
x=ns; %output variable for number of success
end %end function
d. geometric distribution:
function x=myGeometric(p,S)
ns=0; %counter for number of successes
while 1 %till first success
tt=myBernolli(p,S);
if(tt==1) %if SUCCESS OUTCOME
ns=ns+1;
break;
else
ns=ns+1;
end
end
x=ns; %output variable
end %end function
e. plot:
h1=zeros(1,5000);
%to store return value from Geometric function
h2=zeros(1,5000);
for nn=1:5000
h1(nn)= myBinomial(100,0.25);
h2(nn)= myGeometric(0.25,1);
end
h1
h2
hist(h1);
hist(h2);