Given a matrix A how do you estimate the probability mass fu
Given a matrix A, how do you estimate the probability mass function of a random variable evaluated the bin centers?
Eventually this will need to be written in matlab code.
Solution
X = randi([-9 9], [100 1]); [V,~,labels] = grp2idx(X); mx = max(V); %# TABULATE (internally uses HIST) t = tabulate(V); pmf1 = t(:, 3) ./ 100; %# HIST (internally uses HISTC) pmf2 = hist(V, mx)\' ./ numel(V); %#\' %# HISTC pmf3 = histc(V, 1:mx) ./ numel(V); %# ACCUMARRAY pmf4 = accumarray(V, 1) ./ numel(V); %# SORT/FIND/DIFF pmf5 = diff( find( [diff([0;sort(V)]) ; 1] ) ) ./ numel(V); %# SORT/UNIQUE/DIFF [~,idx] = unique( sort(V) ); pmf6 = diff([0;idx]) ./ numel(V); %# ARRAYFUN pmf7 = arrayfun(@(x) sum(V==x), 1:mx)\' ./ numel(V); %#\' %# BSXFUN pmf8 = sum( bsxfun(@eq, V, 1:mx) )\' ./ numel(V); %#\' >> [labels pmf] ans = -9 0.03 -8 0.07 -7 0.04 -6 0.07 -5 0.03 -4 0.06 -3 0.05 -2 0.05 -1 0.06 0 0.05 1 0.04 2 0.07 3 0.03 4 0.09 5 0.08 6 0.02 7 0.03 8 0.08 9 0.05