EEN 310 - Matlab Exercise 1 Exercise 1: Simulation of Uniform and Gaussian Random Variables In Matlab, there are two basic functions for generating random variables: rand() and randn(). The function...

The assignment is a probability assignment in which the code is provided and you have to modify the quote depending on the question given. The assignment has to be submitted in MATLAB code


EEN 310 - Matlab Exercise 1 Exercise 1: Simulation of Uniform and Gaussian Random Variables In Matlab, there are two basic functions for generating random variables: rand() and randn(). The function rand() can be used to generate samples or realizations of the standard uniform random variables that is uniformly distributed in the interval [0, 1], and the function randn() can be used to generate realizations of the standard Gaussian random variables with zero mean and unit variance. In this exercise, we will learn to use these two functions to generate different random variables. If we have generated N realizations of a random variable X, xi, i = 1, · · · , N . The mean of X can be estimated by the sample mean ˆ̄X = 1 N N∑ i=1 xi, (1) ant the variance of X can be estimated by the sample variance s = 1 N − 1 N∑ i=1 (xi − ˆ̄X)2 (2) We can also estimated the PDF of X using the normalized histogram. The number of realizations, N , should be sufficiently large to obtain an accurate estimate of the mean, variance, or, PDF. 1 Uniform Random Variable A uniform random variable X, that is distributed uniformly in the interval [a, b], has the following PDF: fX(x) = { 1 b−a , x ∈ [a, b] 0, otherwise (3) It is easy to show that the mean and variance of X are E[X] = b−a 2 and V ar(X) = (b−a) 2 12 , respectively. The function rand() in Matlab can be used to generate realizations of the standard uniform random variable, with a = 0 and b = 1. The following codes generate N = 105 realizations of the standard uniform random variable, and estimate the mean and variance. Are the estimated mean and variance close to the theoretical ones? clear; N=1e5; EEN 310 - Matlab Exercise 2 x=rand(1,N); xmean=sum(x)/N xvar=sum((x-xmean).^2)/(N-1) Experiment 1: We can use function rand() to generate realizations of more general uniform random vari- able Y which is uniformly distributed on interval [a b]. Suppose a = 1 and b = 5, generate N = 105 realizations of Y , estimate its mean and variance, and compare the estimated mean and variance with the theoretical mean and variance. Now, we want to generate realizations of a Bernoulli random variable B that takes on value 0 and 1 with equal probability. We can use the function rand() to generate B as follows: temp=rand(1) if temp>1/2 B=1; else B=0; end Or, we can simply use the following line: B=round(rand(1)); Experiment 2: Generate N = 105 realizations of B, and estimate the mean and variance, and then compare the estimated results with theoretical ones. The probability mass function (pmf) of B is P (B = 1) = P (B = 0) = 1/2. We can estimate the pmf of B by computing the frequencies of 1 and 0 in N realizations generated in Experiment 2 as follows: N=1e5; temp=rand(1,N); B=round(temp); p1=sum(B)/N p0=sum(1-B)/N EEN 310 - Matlab Exercise 3 Experiment 3: If B = 1 with probability p = 1/4 and B = 0 with probability 3/4. Repeat simulations in Experiment 2. Experiment 4: If we have a random variable X whose pmf is pX(x) = { 1/4 X = −3,−1, 1, 3 0 otherwise (4) Determine the mean and variance of X. Generate N = 105 realizations of X, and estimate the mean, variance and pmf, and then compare the estimated results with theoretical ones. 2 Gaussian Random Variable The Matlab function randn() can be used to generate realizations of the standard Gaussian random variable whose mean is zero and variance one. The following Matlab codes generate N = 105 realizations of the standard Gaussian random variable, and estimate its mean, variance and PDF, and then compare the estimated results with theoretical ones. clear; N=1e5; x=randn(1,N); xmean=sum(x)/N xvar=sum((x-xmean).^2)/(N-1) Recall that for a standard Gaussian random variable X, we have P (X > x) = Q(x). There is no Q-function in Matlab, but it can be computed from the Matlab function erf(x) as follows: Q(x) = 1−erf(x/ √ 2) 2 . The following Matlab codes estimated P (X > 1) and compare it with the Q(1). clear; N=1e5; x=randn(1,N); Threshold=1; xbigerthanT=find(x>Threshold); Pest=length(xbigerthanT)/N; P=1/2-1/2*erf(Threshold/sqrt(2)); [Pest P] EEN 310 - Matlab Exercise 4 Experiment 5: Let us denote the standard Gaussian random variable as G. Then, Y = aG+ b is a Gaussian random variable with mean b and variance a2. Using this relationship and a = 2, b = 3, generate N = 105 realizations of Y , and estimate its mean and variance, and then compare the estimated results with theoretical ones. Experiment 6: Estimate the probability P (Y > 2.5). The probability P (Y > y) can be calculated as P (Y > y) = Q ( y−mY σY ) where mY and σY are the mean and standard deviation of Y , respectively. Compare the estimated P (Y > 2.5) with the theoretical one. EEN 310 - Matlab Exercise 1 Exercise 2: Simulation of Common Random Variables In this exercise, we will learn to generate samples from several common variables including binomial, geometric and exponential random variables and to estimate the pmf or pdf of a random variable from the realizations generated. First, let us simulate a discrete random variableX whose pmf is pX(0) = 0.2, pX(1) = 0.3, pX(3) = 0.4 and pX(4.5) = 0.1 and estimate the pmf from simulated samples. The following is the Matlab code. clear; N=1e5; temp=rand(N,1); for i=1:N if temp(i)<0.2 x(i)="0;" elseif=""><0.5 x(i)="1;" elseif=""><0.9 x(i)="3;" else="" x(i)="4.5;" end="" end="" p="[length(find(x==0))" length(find(x="=1))" length(find(x="=3))" length(find(x="=4.5))]/N" xvalues="[0" 1="" 3="" 4.5];="" stem(xvalues,p);="" grid="" on;="" xlabel(’x’);="" ylabel(’probability’);="" title(’estimated="" pmf’);="" if="" a="" random="" experiment="" has="" two="" outcomes="" with="" probabilities="" p="" and="" 1−p,="" respectively,="" we="" call="" such="" an="" experiment="" a="" bernoulli="" trial,="" and="" refer="" to="" the="" two="" outcomes="" –="" often="" arbitrarily="" –="" as="" success="" and="" failure.="" a="" bernoulli="" random="" variable="" x="" is="" defined="" for="" a="" bernoulli="" trial;="" it="" has="" two="" values,="" say="" x="1" for="" success="" and="" 0="" for="" failure,="" and="" pmf="" px(1)="p" and="" px(0)="1−" p.="" if="" we="" perform="" a="" sequence="" of="" n="" independent="" bernoulli="" trials="" and="" define="" a="" random="" variable="" y="" as="" the="" number="" of="" successes,="" then="" y="" is="" a="" binomial="" random="" variables="" with="" parameters="" n="" and="" p.="" een="" 310="" -="" matlab="" exercise="" 2="" experiment="" 1:="" let="" xi="" be="" the="" bernoulli="" random="" variable="" for="" the="" outcome="" of="" the="" ith="" trial,="" then="" y="∑n" i="1" xi="" is="" a="" binomial="" random="" variable.="" suppose="" n="10" and="" p="1/2." use="" the="" method="" for="" simulating="" bernoulli="" random="" variables="" described="" in="" exercise="" 1="" to="" generate="" 105="" samples="" of="" the="" binomial="" random="" variable="" and="" plot="" estimated="" pmf="" from="" 105="" samples;="" compute="" the="" theoretical="" pmf="" and="" compare="" it="" with="" the="" estimated="" pmf.="" use="" matlab="" function="" nchoosek()="" to="" compute="" the="" theoretical="" pmf.="" in="" a="" random="" experiment,="" if="" we="" keep="" performing="" bernoulli="" trials="" until="" success,="" then="" the="" number="" of="" trials="" before="" the="" success="" is="" a="" geometric="" random="" variable="" whose="" pmf="" is="" p(x="n)" =="" p(1−="" p)n−1,="" n="1," 2,="" ·="" ·="" ·="" .="" the="" following="" matlab="" code="" can="" be="" used="" to="" generate="" a="" sample="" of="" a="" geometric="" random="" variable="" with="" parameter="" p="1/2:" b="0;" g="0;" while="" b~="1" temp="rand(1);" if=""><1 2="" b="1;" else="" b="0;" end="" g="g+1;" end="" g="g-1;" experiment="" 2:="" generate="" n="105" realizations="" of="" a="" geometric="" random="" variable="" with="" parameter="" p="1/2," estimate="" the="" pmf.="" plot="" the="" estimated="" pmf="" and="" compare="" it="" with="" the="" theoretical="" one.="" to="" estimate="" the="" pdf="" of="" a="" continuous="" random="" variable="" x,="" we="" can="" generate="" n="" samples="" of="" x,="" divide="" the="" range="" of="" these="" n="" samples="" into="" certain="" number="" of="" bins="" and="" then="" count="" the="" number="" of="" samples="" in="" each="" bin,="" which="" can="" be="" done="" using="" matlab="" function="" hist().="" the="" following="" matlab="" code="" estimate="" the="" pdf="" of="" the="" standard="" uniform="" random="" variable.="" n="1e5;" x="rand(1,N);" [m,="" y]="hist(x,50);" %histogram="" of="" x="" een="" 310="" -="" matlab="" exercise="" 3="" pdfest="M/(N*(y(2)-y(1)));" %normalize="" histogram="" to="" estimate="" pdf="" plot(y,pdfest);="" axis([0="" 1.2="" 0="" 1.1]);="" hold="" on;="" plot([0="" 1],[1="" 1],’r’);="" %="" plot="" theoretical="" pdf="" legend(’estimated="" pdf’,="" ’theoretical="" pdf’);="" experiment="" 3:="" generate="" n="105" realizations="" of="" the="" standard="" gaussian="" random="" variable,="" estimate="" the="" pdf="" and="" plot="" it="" along="" with="" the="" theoretical="" pdf.="" the="" following="" matlab="" code="" can="" be="" used="" to="" plot="" the="" theoretical="" pdf:="" t="-4:0.1:4;" pdf="1/sqrt(2*pi)*exp(-t.^2/2);" plot(t,pdf,’r--’);="" the="" pdf="" of="" an="" exponential="" random="" variable="" x="" with="" parameter="" λ="" is="" fx(x)="λe" −λx,="" x=""> 0. If Y is a standard uniform random variable, it can be shown that X = −ln(1− Y )/λ is an exponential random variable with parameter λ. Experiment 4: Generate N = 105 samples of an exponential random variable with parameter λ = 2, plot the estimated pdf and the theoretical pdf. EEN 310 - Matlab Exercise 1 Exercise 3: Computer Simulations of Real Problems Computer simulations are used widely in engineering designs or scientific research from designing small circuits, big aircrafts to nuclear bombs. In this exercise, we will see that computer simulation can easily solve some hard problems. We will first solve the Monty Hall Problem that we discussed in the class. Here is the Monty Hall Problem: Suppose you are on a game show, and you are given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what is behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice? If you do not switch, the probability of getting a car is obviously 1/3. If you switch, our analysis showed that the probability of getting a car is increased to 2/3. As we have seen, the analysis is not straightforward. However, we can write a simple program to simulate the game and to estimate the probability of getting a car. The following is the Matlab code for simulating the game. Run the code and you will see that the estimated probability of getting a car will be close to 2/3, if you switch. clear; N=1e5; %repeat the game N times Nc=0; %count the number of times to find a car if switching the choice d=[1 2 3]; %door numbers for i=1:N c=ceil(3*rand(1)); %randomly put a car behind door c x=ceil(3*rand(1)); %you randomly pick a door x I=find(d~=x); doorleft=d(I); %the doors not chosen I=find(doorleft~=c);%the host finds the door(s) hiding a goat if length(I)==1 %if there is only one door hiding a goat, the host opens it Nc=Nc+1; %and the remaining door hides the car end end p=Nc/N %estimated probability of wining a car if you switch your choice Experiment 1: Suppose that there are four doors, one car and three goats in the Monty Hall Problem. Simulate the game and estimate
May 12, 2020
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here