0

I have to generate a random variable that ranges from 20 to 30, with 0.1 interval, and the pmf of it is given by 1x101 matrix (for x=20, 20.1, ... 29.9, 30).

Now I have to make random variable with this given pmf,

But all I know about generating R.V is something related to gaussian, or uniformly distributed function.

So is there any way to implement generating random variables with arbitrary pmf?

Santhan Salai
  • 3,888
  • 19
  • 29
CSjune
  • 23
  • 2
  • 8

2 Answers2

1

I think this should do the trick.

%// First I created some random data
N=1e6;
x=randn(1,N);x=(x-min(x))/(max(x)-min(x));
pmf=hist(x,101)./N;
%// It's a normal distribution but scaled to output to [0 1]

%// First, get the cdf
xh=cumsum(pmf);

%// Create the list of possible values of this random variable
Y=20:.1:30;

%// Create a uniformly distributed random variable between 0 and 1
R=rand()

%// Find where the cdf value is closest to R
[~,J]=min(abs(xh-R));

%// Find the resulting sample values
Y(J)

%// Just a little plot to make sure it's working
hold on
plot(Y,xh)
plot([20 30],[R R],[Y(J) Y(J)],[0 1])
hold off
David
  • 8,449
  • 1
  • 22
  • 32
0

I think what you are trying to do is multinomial sampling, see http://se.mathworks.com/help/stats/mnrnd.html?refresh=true

If you just want one sample, you can (crudely) do something like

find( mnrnd(1,pmf))

where pmf is the vector of probabilities (assert(sum(pmf)==1))

Note that there is nothing special of using 20:.1:30, you basically have 101 bins each with probability given by pmf

alexandre iolov
  • 582
  • 3
  • 11