Random Numbers with Gaussian and Uniform Distributions in matlab

試著忘記壹切 提交于 2019-11-28 22:05:25

Use rand(dimensions) for a Uniform Distribution between 0 and 1.

Use randn(dimensions) * sqrt(sigma) + mu for a Gaussian Distribution with a mean of mu and variance of sigma.

randn is the function to generate Gaussian distributed variables (randi and rand produce uniformly distributed ones).

raj

You can generate any distribution from rand().

For example , lets say you want to generate 100000 samples for rayleigh dist.The way to do this is that you invert the cdf of that particular function.The basic idea is that since the cdf has to be between 0 and 1 , we can find the value of the random variable by inputting the value of cdf b/w 0 and 1. So for rayleigh, it would be

for i = 1:100000
  data(i) = (2*sigma^2 *(-(log(1 - rand(1,1)))))^.5;
end

You can do something similar for gaussian distribution.

Congrulations, you already generating pseudo-random numbers with a gaussian distribution. Normal distribution is a synonym for it.

The only other possible interpretation I can get from your question is that you want something that has mean != 0 and/or variance != 1. To do that, simply perform mean + sqrt(var) * randn(X).

Following raj's answer: by using the Box-Muller Transform you can generate independent standard Normal/Gaussian random numbers:

N = 1e6; z = sqrt(-2*log(rand(N, 1))) .* cos(2*pi * rand(N, 1)); figure; hist(z, 100)
N = 1e6; z = sqrt(-2*log(rand(N, 1))) .* sin(2*pi * rand(N, 1)); figure; hist(z, 100)

If you want to apply the Inverse Transformation Method, you can use the Inverse Complementary Error Function (erfcinv):

N = 1e6; z = -sqrt(2) * erfcinv(2 * rand(1e6, 1)); figure; hist(z, 100)

But I hope randn works better.

It is true you can generate just about anything from rand but that it isn't always convenient, especially for some complicated distributions.

MATLAB has introduced Probability Distribution Objects which make this a lot easier and allow you to seamless access mean, var, truncate, pdf, cdf, icdf (inverse transform), median, and other functions.

You can fit a distribution to data. In this case, we use makedist to define the probability distribution object. Then we can generate using random.

% Parameters
mu = 10; 
sigma = 3;
a = 5; b = 15;
N = 5000;

% Older Approaches Still Work
rng(1775)
Z = randn(N,1);    % Standard Normal  Z~N(0,1)
X = mu + Z*sigma;  % X ~ Normal(mu,sigma)

U = rand(N,1);     % U ~ Uniform(0,1)
V = a + (b-a)*U;   % V ~ Uniform(a,b)

% New Approaches Are Convenient
rng(1775)
pdX = makedist('Normal',mu,sigma);
X2 = random(pdX,N,1);

pdV = makedist('Uniform',a,b);
V2 = random(pdV,N,1);

A reproducible example:

Support = (0:0.01:20)';
figure 
s(1) = subplot(2,2,1)
h(1) = histogram(X,'Normalization','pdf')
xlabel('Normal')
s(2) = subplot(2,2,2)
h(2) = histogram(V,'Normalization','pdf')
xlabel('Uniform')
s(3) = subplot(2,2,3), hold on, box on
h(3) = histogram(X2,'Normalization','pdf')
plot(Support,pdf(pdX,Support),'r-','LineWidth',1.2)
xlabel('Normal (new)')
s(4) = subplot(2,2,4), hold on, box on
h(4) = histogram(V2,'Normalization','pdf')
plot(Support,pdf(pdV,Support),'r-','LineWidth',1.2)
xlabel('Uniform (new)')
xlim(s,[0 20])  

References:
Uniform Distribution
Normal (Gaussian) Distribution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!