|Generates a random probability vector|
|Other toolboxes required||none|
|Function category||Random things|
RandomProbabilities is a function that generates a random probability vector (i.e., its entries are non-negative and sum to 1), uniformly distributed on the unit simplex.
- P = RandomProbabilities(N)
- N: The number of entries in the desired probability vector.
Uniform distribution of length-3 probability vectors
The following code randomly generates 10000 probability vectors of length 3, and then demonstrates that their distribution on the unit simplex really is approximately uniform:
The scatter plot produced by the above code is displayed below:
Click on "expand" to the right to view the MATLAB source code for this function.
%% RANDOMPROBABILITIES Generates a random probability vector
% This function has one required argument:
% N: the length of the probability vector
% P = RandomProbabilities(N) generates a length-N probability vector,
% uniformly on the (N-1) unit simplex. That is, it generates a vector P
% with N entries, each of which is between 0 and 1 and such that sum(P) =
% URL: http://www.qetlab.com/RandomProbabilities
% requires: nothing
% author: Nathaniel Johnston (email@example.com)
% package: QETLAB
% last updated: December 2, 2014
function p = RandomProbabilities(n)
% Start by splitting the interval [0,1] up into n bins and then find the
% width of each bin.