sampling error from a truncated normal distribution

Hi everyone,
Please see the code below. I tried to sample from a truncated normal distibution but the sample histogram is not matching with the actual truncated distribution plot. Can anyone help me with this.
t = 0.01;
n = 10000;
d_mu = 0.0052;
d_COV = 61.35;
d_sig = d_COV*d_mu/100;
pd = makedist('Normal','mu',d_mu,'sigma',d_sig); % normal distribution
tpd = truncate(pd,0,t); % truncated normal distribution
d_norm = random(pd,n,1); % sample from normal
d_tnorm = random(tpd,n,1); % sample from truncated normal
x = linspace(-.01,.02,n);
figure
plot(x,pdf(pd,x))
hold on
plot(x,pdf(tpd,x),'LineStyle','--')
histogram(d_tnorm,30)
legend('Normal','Truncated','Truncated bar')
hold off

Answers (2)

When you use truncate(), it does not make the samples outside the range just vanish: it adjusts the distribution so that the total is still 1.
Truncated distribution, returned as a probability distribution object. The probability distribution function (pdf) of t is 0 outside the truncation interval. Inside the truncation interval, the pdf of t is equal to the pdf of pd, but divided by the probability assigned to that interval by pd.

5 Comments

yes. I want my sample to be in between 0 to t (t=0.01) only. The dotted orange line which is truncted is above the blue line (withount truncation). But the sample that I generated (d_tnorm = random(tpd,n,1)) from the truncated does not match with the mother distributaion (truncated normal distribution).
In order for there to be a match like you want, what you would have to have is something that drew randomly from the normal distribution and threw away the samples outside the truncation region. The result would not be a probability distribution -- it would not have the required pdf and cdf properties to be a probability distribution.
The Statistics Toolbox truncate() operation, on the other hand, constructs something that is still a probability distribution, by dividing the pdf of the truncated area by the probability of the interval, getting out something whose total probability is still 1.
For example, there is some point on the normal distribution that where the probability is 1/10, and you want the probability to still be 1/10 for that point, but it can't be 1/10 anymore because the probability of selecting anything outside the truncated area is 0 and the total probability that was originally outside the truncation area has to go somewhere.
Hi Walter,
I am interested in constructing a probability distribution in an interval which I generated
pd = makedist('Normal','mu',d_mu,'sigma',d_sig); % normal distribution
tpd = truncate(pd,0,t); % truncated normal distribution
"tpd" is a probability distribution with area 1.
Now I want to sample some data from this distribution which I did by
d_tnorm = random(tpd,n,1); % sample from truncated normal
now if I am plotting "tpd" and histogram of "d_tnorm" data, both should match but they do not. Hope it clears my problem.
d_pit_COV and d_pit_mu are not defined.
Sorry. The correct code is
t = 0.01;
n = 10000;
d_mu = 0.0052;
d_COV = 61.35;
d_sig = d_COV*d_mu/100;
pd = makedist('Normal','mu',d_mu,'sigma',d_sig); % normal distribution
tpd = truncate(pd,0,t); % truncated normal distribution
d_norm = random(pd,n,1); % sample from normal
d_tnorm = random(tpd,n,1); % sample from truncated normal
x = linspace(-.01,.02,n);
figure
plot(x,pdf(pd,x))
hold on
plot(x,pdf(tpd,x),'LineStyle','--')
histogram(d_tnorm,30)
legend('Normal','Truncated','Truncated bar')
hold off

Sign in to comment.

Change the histogram line to this:
histogram(d_tnorm,30,'Normalization','pdf')

Products

Release

R2018a

Asked:

on 16 Mar 2020

Answered:

on 16 Mar 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!