2

I have two RV , one uniform and one Gaussian and I would like to superimpose their PDFs. I am trying the hold on function but it does not work, as it displays only second plot. How do I do this in MATLAB?

% MATLAB R2019a
% Setup
N = [1:5 10 20 40];
LB = 0;
UB = 3;
n = 10000;

% Generate random variates
X = LB + (UB - LB)*rand(max(N),n);
Sn = cumsum(X); 

mu = 1.5;
sigma = .75;
S_1 = mu + sigma.*randn(n, 1)

hist1= histogram(Sn(1,:),'Normalization','pdf','EdgeColor', 'blue', 'FaceColor',  'blue')
hold on
hist2 = histogram(S_1(:), 'EdgeColor', 'green', 'FaceColor',  'green', 'FaceAlpha', 0.2);
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
devadisha
  • 21
  • 2
  • 1
    Both are displayed, you just haven't normalised the second histogram like you have the first. – David Dec 12 '19 at 04:32
  • One issue with using histograms to estimate the PDF is that they rely on a sufficient sample size to closely approximate the true distribution. Fortunately for the Uniform and Gaussian (Normal) distribution, there are multiple ways to plot the true PDF or CDF directly (with and without toolboxes). – SecretAgentMan Dec 13 '19 at 14:10
  • Did [my update](https://stackoverflow.com/questions/59293591/pdf-and-cdf-plot-for-central-limit-theorem-using-matlab/59295040#comment104816598_59295040) address your question on the variance? – SecretAgentMan Dec 13 '19 at 22:37

1 Answers1

0

tl;dr: Multiple ways to do this both with and without the Statistics toolbox.


The methods eliminate the need for a large enough sample size to empirically estimate the PDF with a histogram.

Method 1: Use probability distribution objects (requires Statistics toolbox)
You can plot the probability density functions (PDFs) directly using the probability distribution objects which lets you exploit the makedist() and pdf() functions.

The PDF for X ~ Uniform(a,b) is obtained from pdf(pdX,x) where x contains the values from the domain (support) for the random variable X where you want to evaluate the PDF.

% MATLAB R2019a
% Setup
a = 0;          % lowerbound for X ~ Uniform(a,b)
b = 3;          % upperbound for X
mu = 1.5;       % mean for Y ~ Normal(mu,sigma)       E[Y] = mu
sigma = 0.75;   % standard dev                        sqrt(Var(Y)) = sigma

% Create distributions
pdX = makedist('Uniform',a,b);                        X ~ Uniform(a,b)
pdY = makedist('Normal',mu,sigma);                    Y ~ Normal(mu,sigma)


step = 0.1;
Domain = -2:step:5;               % Useful for plotting

% Plot
figure, hold on
plot(Domain,pdf(pdX,Domain),'b-','DisplayName','Uniform(0,3)')      % Plot pdf for X
plot(Domain,pdf(pdY,Domain),'k-','DisplayName','Normal(1.5,0.75)')  % Plot pdf for Y
legend('show')

Plot showing the PDFs for the Uniform(0,3) and Normal(1.5,0.75) random variables.


Method 2: Use normpdf() (requires Statistics toolbox)
You can easily implement the pdf for the Uniform distribution with

fXh =@(x) 0*(x<a) + (1/(b-a))*(a<=x & x<=b) + 0*(b<x);   % pdf for Uniform(a,b)

which will require no toolbox.

Then use the built-in normpdf() function (requires Statistics toolbox) with syntax normpdf(x,mu,sigma).

You get the same result.

figure, hold on
plot(Domain,fXh(Domain),'b-','DisplayName','Uniform(0,3)')
plot(Domain,normpdf(Domain,mu,sigma),'k-','DisplayName','Normal(1.5,0.75)')
legend('show')

Method 3: No toolboxes required

Implement the PDF for the Normal distribution (Gaussian) which will only have one term since it is unbounded. This avoids the need for the Statistics toolbox.

The PDFs for X and Y are obtained from fXh(Domain) and fYh(Domain), respectively.

fYh =@(x) (1/(sigma*sqrt(2*pi)))*exp(-0.5*((x-mu)/sigma).^2);  % pdf for Normal(mu,sigma)

% Plot
figure, hold on
plot(Domain,fXh(Domain),'b-','DisplayName','Uniform(0,3)')
plot(Domain,fYh(Domain),'k-','DisplayName','Normal(1.5,0.75)')
legend('show')
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41