How to draw a probability density function in MatLab?

x = [1 2 3 3 4] cdfplot(x) 

After Googling, I believe that the code above will draw a cumulative distribution function for me in Matlab.
Is there an easy way to draw a probability density function?

Clarify. I need a graph with a uniformly distributed x axis. And I would prefer that it does not look like a histogram. (I would have millions of integers)
Sorry, update again. My data is integers, but in fact it represents time (I expect several rather high peaks with the same value, while the other value should look as if it were not discrete). I am really starting to wonder if this is actually not a discrete integer. CDF will definitely work, but coming to PDF seems like it is harder than I expected.

+6
source share
4 answers

You can generate a discrete probability distribution for your integers using the hist function:

 data = [1 2 3 3 4]; %# Sample data xRange = 0:10; %# Range of integers to compute a probability for N = hist(data,xRange); %# Bin the data plot(xRange,N./numel(data)); %# Plot the probabilities for each integer xlabel('Integer value'); ylabel('Probability'); 

And here is the result:

enter image description here


UPDATE:

In newer versions of MATLAB, hist no longer recommended. Instead, you can use the histcounts function to create the same pattern as above:

 data = [1 2 3 3 4]; N = histcounts(data, 'BinLimits', [0 10], 'BinMethod', 'integers', 'Normalization', 'pdf'); plot(N); xlabel('Integer value'); ylabel('Probability'); 
+7
source

If you want a continuous distribution function, try this.

 x = [1 2 3 3 4] subplot(2,1,1) ksdensity(x) axis([-4 8 0 0.4]) subplot(2,1,2) cdfplot(x) grid off axis([-4 8 0 1]) title('') 

What deduces this. enter image description here

The cumulative distribution function is located below, the estimate of the density of the nucleus from above.

+7
source

type "ksdensity" in the Matlab help and you will learn about a function that will give you a continuous PDF form. I think this is exactly what you are looking for.

+2
source

In addition to the smooth PDF obtained with ksdensity(x) , you can also plot a smooth CDF graph with ksdensity(x,'function','cdf') .

enter image description here

0
source

Source: https://habr.com/ru/post/886491/


All Articles