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 a Python program which generates a random number between 0 and 24 inclusive and that...
Write a Python program which generates a random number between 0 and 24 inclusive and that print out: • the double of the random number if the random number is greater than 12, • the triple of the random number if it is equal to 12 • and the quadruple of the random number if it is less than 12
Write a program with the aim of performing an audiometry test at MATLAB. The program should...
Write a program with the aim of performing an audiometry test at MATLAB. The program should be as interactive as possible. For example, at first, which ear is to be tested should be chosen so that the sound is only given to that channel of the ear. In addition, whether the test frequency increases automatically or manually should be asked as a parameter. The frequencies of the person being tested should be entered by the user as well as whether...
*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...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT