Find inverse of a cdf so as to sample x

조회 수: 9 (최근 30일)
Aishwarya Radhakrishnan
Aishwarya Radhakrishnan 2019년 9월 22일
댓글: Star Strider 2019년 9월 22일
I have a cdf calculated as follows:
mu = 0;
var = 1;
x = -10:0.01:10;
y = cumtrapz((2*pi*(var))^(-0.5)* exp(-((x-mu).^2)/(2*(var))));
This gives the cdf of my probability distribution function (pdf): (2*pi*(var))^(-0.5)* exp(-((x-mu).^2)/(2*(var)))
But i want to sample the values of x randomly for this pdf. So I've created the cdf stored in variable y.
If i can find the inverse cdf, then i can uniformly select y values and then calculate using inverse cdf the corresponding x values that become my sample.
with the inverse function of the cdf, i want to get x in terms of y,
eg, y = F(x)
then x = Finverse(y)
like in figure:
Screen Shot 2019-09-22 at 10.57.04.png
I want to pass in y in the figure to Finverse(y) which is inverse of cdf, and then i can get x as:
x = Finverse(y)
But, i've tried many methods till now, but none seem to help.
i've tried to use :
i = erfinv(y)
but this gives me values as vector, but i need function so that i can pass in the values of y to get x. Please help.

채택된 답변

Star Strider
Star Strider 2019년 9월 22일
As I suggested earlier in find finverse of cumtrapz():
If you want to use cumtrapz, the interp1 function is likely the best option:
mu = 0;
v = 1;
x = -7:0.01:7;
fx = cumtrapz(1/sqrt(2*pi*(v)) * exp(-((x-mu).^2)/(2*(v))));
y = [5 25 50 75 95];
fi = interp1(fx, x, y, 'linear','extrap')
figure
plot(x, fx)
hold on
plot(fi, y, '+')
hold off
grid
This plots ‘+’ markers at the appropriate values of the ‘y’ vector. I call the inverse ‘fi’. I had to restrict your original ‘x’ vector because with the original vector, the ‘fx’ points were not unique, as interp1 defines that.
Enlarging on that:
mu = 0;
v = 1;
x = -7:0.01:7;
fx = cumtrapz(1/sqrt(2*pi*(v)) * exp(-((x-mu).^2)/(2*(v))));
y = [5 25 50 75 95];
Finverse = @(y) interp1(fx, x, y, 'linear','extrap');
figure
plot(x, fx)
hold on
plot(Finverse(y), y, '+')
hold off
grid
That should do what you want.
  댓글 수: 2
Aishwarya Radhakrishnan
Aishwarya Radhakrishnan 2019년 9월 22일
Ohh thank you so much !
Star Strider
Star Strider 2019년 9월 22일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Special Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by