2

Is there a Matlab function which builds up the exact probability mass function (or probability density function) given a vector of data?

I mean something like this:

X = [1 2 4 1 4 3 2 3 4 1];
[x px] = a_function(X)
x = 
   1 2 3 4
px = 
   0.3 0.2 0.2 0.3
the_candyman
  • 1,563
  • 4
  • 22
  • 36

2 Answers2

5

you can use accumarray

pmf = accumarray(X(:),1)./numel(X);
pmf = pmf./sum(pmf);

or hist:

pmf = hist(X, max(X))' ./ numel(X);      

or tabulate :

t= tabulate(X);
pmf  = t(:, 3) ./ 100 ;

and there's probably at least 10 more ways doing so...

for px just use px=unique(X), or t(:, 1) in the tabulate solution, etc...

bla
  • 25,846
  • 10
  • 70
  • 101
2

Here's a function that I use (comments % replaced with # as StackOverflow doesn't parse Matlab correctly).

This could be neatened up (and probably speeded up) using either accumarray or hist as in natan's answer.

function [vals freqs] = pmf(X)
#PMF Return the probability mass function for a vector/matrix.
#
#INPUTS:
#   X       Input matrix
#
#OUTPUTS:
#   VALS    Vector of unique values
#   FREQS   Vector of frequencies of occurence of each value.
#

    [vals junk idx] = unique(X);

    vals   = vals(:);
    frequs = NaN(length(vals),1);

    for i = 1:length(vals)
        freqs(i) = mean(idx == i);
    end

    # If 0 or 1 output is requested, put the values and counts in two columns
    # of a matrix.
    if nargout < 2
        vals = [vals freqs];
    end

end
Community
  • 1
  • 1
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154