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

Write a Matlab code that will generate a random number, which can take only two

ID: 3363668 • Letter: W

Question

Write a Matlab code that will generate a random number, which can take only two possible values H (representing a heads outcome in a random coin toss) and T (representing a tails in a random coin toss). Generate a sequence of toss outcomes of 10 random trials. Count how many times H and T are generated out of the 10 outcomes. How can you estimate the probability of H and T in this case? Repeat the experiment with a 100, 500 and 1000 outcomes generated. Estimate the probabilities in each case. What do you notice? Hints: Random numbers with equal probability 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.

Explanation / Answer

sample1=randi([0,1],1,10)
p.hat1=sum(sample1)/10
sample2=randi([0,1],1,100)
p.hat2=sum(sample2)/100
sample3=randi([0,1],1,500)
p.hat3=sum(sample3)/500
sample4=randi([0,1],1,1000)
p.hat4=sum(sample4)/1000

We estimate the probability of 1 by proportion of 1 in the sample. SAMPLE MEAN

Result:

p =

hat1: 0.4000
hat2: 0.5200
hat3: 0.4980
hat4: 0.5060

hat1: 0.6000
hat2: 0.5200
hat3: 0.5220
hat4: 0.4960

We run it 2 times. We can try for more!

We see that more the sample size, the better the estimate is getting. So as n gets larger and larger, the sample mean goes to population mean.

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