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

Here is the code developed in software assignment#1: (1) the Matlab code develop

ID: 3583276 • Letter: H

Question

Here is the code developed in software assignment#1: (1) 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 Tor H) and returns the outcome of the trial (either Tor H). b. Test that your function is actually producing the success 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 myBernolli0 to retum as an output the number of successes x. d. Write a Matlab function called myG that takes as an input the eometrico probability of success p and the outcome defined as success Sand uses myBernolli to return as an output the number of trials till first success x. e. Verify that myBinomia 0 and myG generates values that follows eometrico 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 pare generated in Matlab using the command function rando. Read the help on Matlab to know how to use the function by typing help rand in Matlab command line. Histogram plots [hist00 is a function in Matlab. Read its help to know how to produce the requirements in part e.

Explanation / Answer

head=0;

tail=0;

for i=1:1000000

    x=rand(1);

if x>=0.5

    tail=tail+1;

else

    head=head+1;

end

end

heads=head/100

tail=tail/100

for 100 trails

heads =

    0.5300

tail =

    0.4700

For 1000 trails

heads =

    4.9000

tail =

    5.1000

For 10000 trails

heads =

   50.3300

tail =

   49.6700

For 100000 trails

heads =

500.1000

tail =

499.9000

From that clearly probabilities are getting closer when number of trails increased.

a)

p=0.1;

function result = mybernoulli(p)

U1 = rand(1); % Generate a sample in Unif(0,1)

if U1 < p

fprintf('the sucess ');

else

fprintf('the failure ');

end

answer:

the success

b)

p=0.1;

c=0;

function result = mybernoulli(p)

for i=1000

U1(i) = rand(1); % Generate a sample in Unif(0,1)

if U1(i) < p

fprintf('the sucess ');

c=c+1;

disp(c);

else

fprintf('the failure ');

end

end

answer:

the sucess

     567

c)

function probdf = mybernoli(k,n,p)

    m =5000 ;

    idxx = 0;

    for i=1:m

        idxx= idxx+ double(nnz(rand(n,1) < p)==k);

    end

    probdf = idxx/m;

end

k = 0:5000;

y1_pdf = mybernoli(k,20,0.5);

y1_cdf = cumsum(y1_pdf);

figure;

subplot(1,2,1);

stem(k,y1_pdf);

title('PDF');

subplot(1,2,2);

stairs(k,y1_cdf);

axis([0 20 0 1]);

title('CDF')

d)

function probdf = mygeometri(k,p)

    m = 10000;

    probdf = zeros(numel(k));

    for j=1:numel(k)

        idxx = 0;

        for i=1:m

            idxx = idxx + double(nnz(rand(j,1) < p) < 1);

        end

        probdf(j) = idxx/m;

    end

end

k = 0:5000;

y1_pdf = mygeometri(k,0.5);

y1_cdf = cumsum(y1_pdf);

figure;

subplot(1,2,1);

stem(k,y1_pdf)

title('PDF');

subplot(1,2,2);

stairs(k,y1_cdf);

axis([0 20 0 1]);

title('CDF');

e)

function probdf = mygeometri(k,p)

    m = 10000;

    probdf = zeros(numel(k));

    for j=1:numel(k)

        idxx = 0;

        for i=1:m

            idxx = idxx + double(nnz(rand(j,1) < p) < 1);

        end

        probdf(j) = idxx/m;

    end

end

k = 0:5000;

y1_pdf = mygeometri(k,0.5);

y1_cdf = cumsum(y1_pdf);

figure;

subplot(1,2,1);

stem(k,y1_pdf)

title('PDF');

subplot(1,2,2);

stairs(k,y1_cdf);

axis([0 20 0 1]);

title('CDF');

function probdf = mybernoli(k,n,p)

    m =5000 ;

    idxx = 0;

    for i=1:m

        idxx= idxx+ double(nnz(rand(n,1) < p)==k);

    end

    probdf = idxx/m;

end

k = 0:5000;

y1_pdf = mybernoli(k,20,0.5);

y1_cdf = cumsum(y1_pdf);

figure;

subplot(1,2,1);

stem(k,y1_pdf);

title('PDF');

subplot(1,2,2);

stairs(k,y1_cdf);

axis([0 20 0 1]);

title('CDF')

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote