how to estimate cdf from ksdensity pdf

20 次查看(过去 30 天)
I have a quick question about ksdensity. For a given variable I derive distribution by binning into a specified number of bins, calculating the number of samples in each bin. For example, I used 10 bins to get the pdfs
To sample distributions, those pdf's are converted to cdf's, I draw random values from 0-1 so sample that cdf at bin center i. This allows to only get back the values at each bin center which for contionous variable is not what I want. Thus I then sample uniformatly from i-1 to i+1.
I was wondering if I can used ksdensity to do this as the more robust soluton. So essentially finding CDF from PDF that was estimated using Kernel Desnity?
Thank you!

回答(1 个)

Star Strider
Star Strider 2018-11-14
There is an option for ksdensity to return the cdf:
x = [randn(30,1); 5+randn(30,1)];
[f,xi] = ksdensity(x);
figure
plot(xi,f)
grid
title('PDF')
[f,xi] = ksdensity(x, 'Function','cdf');
figure
plot(xi,f)
grid
title('CDF')
Experiment with your own distribution. This is discontinuous, so you may need to use the interp1 function for intermediate values.
  4 个评论
jenka
jenka 2018-11-14
actually, I think this might work:
x = [randn(30,1); 5+randn(30,1)];
[f,xi] = ksdensity(x);
% Generate uniform random number between 0 and 1
ur = rand(m,n);
% Interpolate ur from empirical cdf and extraplolate for out of range
% values.
xr = interp1(xi,f,ur,[],'extrap');
Star Strider
Star Strider 2018-11-14
O.K. I am not certain what m and n are, and without more information, I continue to guess at the appropriate solution. I alluded (and linked) to interp1 earlier.
You can calculate the probabilities, then weight them appropriately.
Example —
rn = repelem(xi, fix(f/min(f))); % Generate Random Values
figure
histogram(rn, numel(f)) % Plot Histogram
That will reproduce the PDF of the distribution.
Experiment to get the result you want.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by