Question

Generate 500 samples of 10 numbers according to the cdf -exp(-25x/1132) using matlab, please include the...

Generate 500 samples of 10 numbers according to the cdf -exp(-25x/1132) using matlab, please include the code.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

We can generate random numbers given as CDF using Inverse Transform Samping:

The inverse transform sampling method works as follows:

  1. Generate a random number {\displaystyle u} from the standard uniform distribution in the interval {\displaystyle [0,1]} , e.g. from {\displaystyle U\sim \mathrm {Unif} [0,1].}
  2. Find the inverse of the desired CDF, {\displaystyle F_{X}^{-1}(x)} e.g. .
  3. Compute {\displaystyle X=F_{X}^{-1}(u)} . The computed random variable {\displaystyle X} has distribution {\displaystyle F_{X}(x)} .

Please find required MATLAB code along with necessary details in comments below:

clear all, close all, clc

% Plot the cumulative distribution function over the range 0-20.
syms x
f(x)=-exp(-25.*x./1132);

x1 = 0:20;
distCDF = f(x1);

subplot(2,1,1);
plot(x1, distCDF, 'LineWidth', 3);
title('True CDF');

sample=500; % number of samples to be generated
num_random=10; % number of points generated in each sample

% If you want to see graphically that given CDF of generated random numbers
% actually approximate true CDF, genrate larger number of random number in
% each sample by uncommenting lines below
% sample=10;
% num_random=500;

for i=1:sample
% Generate numberOfRandoms uniformly distributed random numbers.
unif_rn = rand(num_random, 1);
% Invert the CDF of the given CDF to get a function that can
% generate random numbers drawn from a required distribution,
% given numbers drawn from a uniform distribution.
inv_f = finverse(f); % calculate F^-1
X=round(double(inv_f(unif_rn))); % calculate F^-1(u)
Rand_num(:,i)=X; % store random number
  
% plot the ith sample cdf
subplot(2,1,2);
hold on
pause(0.01)
cdfplot(X)
xlim([0 20])
title('Numerical CDF')
end

---------------------------  

To plot the numerical CDF of a data, I have used cdfplot() available at mathworks, which I have included below for your ease of reference:

function cdfplot(X)
% cdfplot(X)
% displays a plot of the Empirical Cumulative Distribution Function
% (CDF) of the input array X in the current figure. The empirical
% CDF y=F(x) is defined as the proportion of X values less than or equal to x.
% If input X is a matrix, then cdfplot(X) parses it to the vector and
% displays CDF of all values.
%
% EXAMPLE:
% figure;
% cdfplot(randn(1,100));
% hold on;
% cdfplot(-log(1-rand(1,100)));
% cdfplot(sqrt(randn(1,100).^2 + randn(1,100).^2))
% legend('Normal(0,1) CDF', 'Exponential(1) CDF', 'Rayleigh(1) CDF', 4)
% Version 1.0
% Alex Podgaetsky, September 2003
% [email protected]
%
% Revisions:
% Version 1.0 - initial version
tmp = sort(reshape(X,prod(size(X)),1));
Xplot = reshape([tmp tmp].',2*length(tmp),1);
tmp = [1:length(X)].'/length(X);
Yplot = reshape([tmp tmp].',2*length(tmp),1);
Yplot = [0; Yplot(1:(end-1))];
figure(gcf);
hp = plot(Xplot, Yplot);
ColOrd = get(gca, 'ColorOrder');
ord = mod(length(get(gca,'Children')), size(ColOrd,1));
set(hp, 'Color', ColOrd((ord==0) + (ord>0)*ord, :));
if ~ishold
xlabel('X', 'FontWeight','b','FontSize',12);
ylabel('F(X)', 'FontWeight','b','FontSize',12);
title('Empirical CDF', 'FontWeight','b','FontSize',12);
grid on;
end

----------------------------------------------

--------------------------------------------

You can verify the nature of random numbers by plotting the numerical cdf after generating a lot of random numbers per samples. For 500 points generated in each of the 10 samples, we obtain two cdf as :

---------------------------------------- SCREENSHOT OF CODE

The matrix Rand_num contains the generated 500 samples of 10 random numbers each.

Add a comment
Know the answer?
Add Answer to:
Generate 500 samples of 10 numbers according to the cdf -exp(-25x/1132) using matlab, please include the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT