Question

In: Electrical Engineering

Write a program (MATLAB) which generates exponential random variables, and use it to test the Central...

Write a program (MATLAB) which generates exponential random variables, and use it to test the Central Limit Theorem as follows. For various values of n (say, 5, 10, 20, 50, 100, 1000), generate samples of the random variable Mn = 1/n \tiny \sum Xi where Xi are iid exponential random variables. A very simple method for generating exponential random variables is given on page 196 of the textbook (read that section). Plot the discretized CDF for Mn (an approximation of the CDF using discrete bins) for each n superimposed on the theoretical Gaussian CDF according to the Central Limit Theorem (or plot them next to each other). Calculate the sample mean and variance according to the material in the book and compare these to the theoretical values. Comment briefly on your results. Show all graphs and calculations. Include a copy of your code. Remember, you will need to generate a large number of samples of Mn for each n in order to get decent histograms. I suggest at least 100+ samples. 100 samples of M1000 require 100,000 exponential samples. You may choose any mean and variance you want for your exponential random numbers, but keep these fixed for all samples so that the results are comparable. Using the program written for the previous problem, repeat the whole experiment, but now choose a random mean and variance for each exponential sample. Thus, the Mn now will not be sums of identically distributed random variables. Show your results and comment. For this case, make sure that the distributions you use to choose your mean and variance at each sample are fixed throughout. Write the code in MATLAB.

Solutions

Expert Solution

% Distribution of Random variables
% Initialization
clear all;clc;
% Rate (Exponential Distribution)
lambda=4;
% Probability for bernoulli distributons
prob=0.6;
% maximum number of iterations
maxItr=10e3;
% Uniform Distribution (Linear Congruential Generator, LCG)
u=zeros(maxItr,1);
for i=1:maxItr
    
    % parameters of LCG
    t = datetime('now');
    z=minute(t)+randi([1,10]); c=second(t)+randi([1,10]);
    m=hour(t)+randi([1,10]); a=second(t)+randi([1,10]);
    
    u(i)=(mod((a*z+c),m))/m;
end
figure
histogram(u)
% Exponential Distribution
% Inverse transform method
exp=-log(u)/lambda;
figure
histogram(exp)
% Erlang Distribution
 sum=0;num_dist=10;
for i=1:num_dist
    
    % Generate exponential random numbers
    exp=exprnd(lambda,maxItr,1);
    sum=sum+exp;
    
end
erlang=sum/num_dist;
figure
histogram(erlang)
% Bernoulli Distribution
ber(u <= prob)=1;
figure
histogram(ber)
% Binomial Distribution
sum=0;num_dist=10;
for i=1:num_dist
    
    % Generate bernoulli random number
    u=rand(maxItr,1);
    ber(u <= prob)=1;
    ber(u > prob)=0;
    
    sum = sum + ber;
end
bimonial = sum;
figure
histogram(bimonial)
% Geometric Distribution
% Inverse transform method
geo=log(u)/log(1-prob);
figure
histogram(geo)
% Negative Binomial Distribution
sum=0;num_dist=10;
for i=1:num_dist
    
    % Generate geometric random number
    geo=geornd(prob,maxItr,1);
    
    sum=sum+geo;
end
bimonial=sum;
figure
histogram(bimonial)
% Normal Distribution (using Box Muller Method)
u1=zeros(maxItr,1);
u2=zeros(maxItr,1);
v1=zeros(maxItr,1);
v2=zeros(maxItr,1);
s=zeros(maxItr,1);

for i=1:maxItr
    
    while (1)
    
        %generating u1 = u~(0,1) 
        
        t = datetime('now');
        z=minute(t)+randi([1,10]); c=second(t)+randi([1,10]);
        m=hour(t)+randi([1,10]); a=second(t)+randi([1,10]);
        u1(i)=(mod((a*z+c),m))/m;
        
        %generating u1 = u~(0,1) 
        
        t = datetime('now');
        z=minute(t)+randi([1,10]); c=second(t)+randi([1,10]);
        m=hour(t)+randi([1,10]); a=second(t)+randi([1,10]);
        u2(i)=(mod((a*z+c),m))/m;
        
        % calculating 's'
        
        v1(i)=2*u1(i)-1;
        v2(i)=2*u2(i)-1;
        s(i)=v1(i)^2+v2(i)^2;
        
        if s(i)<=1
            break;
        end

    end

end

% This method generates two different Normal Variates
Normal_X=sqrt(-2*log(s)./s).*v1;
Normal_Y=sqrt(-2*log(s)./s).*v2;
figure
hold on
histogram(Normal_X,30)
histogram(Normal_Y,30)
hold off

Related Solutions

Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number...
Random Number Guessing Game (python) *use comments Write a program guess.py that generates a random number in the range of 1 through 20, and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." If the user guesses the number, the application should congratulate the...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A...
*Write in C* Write a program that generates a random Powerball lottery ticket number . A powerball ticket number consists of 5 integer numbers ( between 1-69) and One power play number( between 1-25).
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input...
USE MATLAB Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!” If the user’s die is smaller, it should display, “Gising na friend,...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses...
this has to be coded in c ++ • Write a program which generates a DIFFERENT...
this has to be coded in c ++ • Write a program which generates a DIFFERENT random number between 1 and 100 everytime you run the program. Please go online and see how poeple are trying to solve this. You can submit their code. Make sure you include a comment which shows from where the code was taken. You do not need to understand the code but if you can, thats great. This program will be used in the upcoming...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT