Eleonora Ciceri
Eleonora Ciceri

Reputation: 1798

Sample from half a Gaussian distribution

Suppose n = N(0,1) is a normal distribution. In MATLAB, when the randn(1,1) function is used, a sample is extracted from n.

However, I have a different objective: I would like to sample from the upper (or lower) half distribution, i.e., from the half going from the left tail to the mean (or from the mean to the right tail).

A dummy way of doing this would be:

while sample > mean
    sample from gaussian
end

However, since I have to extract a lot of samples in my code, this solution would not be appreciated. Is there a smarter way of extracting those samples, without involving a loop?

Upvotes: 2

Views: 1412

Answers (1)

H.Muster
H.Muster

Reputation: 9317

Given that your Gaussian is symmetrical around zero, you can use

sample = randn(n, 1); 
sample(sample < 0) = -sample(sample < 0);

Note that this only works if the mean of the Gaussian is zero.

For Gaussians with arbitrary means, you can use:

sample(sample < mean(sample)) = -sample(sample < mean(sample)) + 2*mean(sample);

Upvotes: 4

Related Questions