RandomProbabilities

From QETLAB
Jump to: navigation, search
RandomProbabilities
Generates a random probability vector

Other toolboxes required none
Related functions RandomStateVector
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.

Syntax

  • P = RandomProbabilities(N)

Argument descriptions

  • N: The number of entries in the desired probability vector.

Examples

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:

>> for j = 1:10000
     p(j,:) = RandomProbabilities(3);
   end
>> scatter3(p(:,1),p(:,2),p(:,3),1) % plot the probability vectors in 3D-space

The scatter plot produced by the above code is displayed below:

RandomProbabilities uniform.png

Source code

Click on "expand" to the right to view the MATLAB source code for this function.

  1. %%  RANDOMPROBABILITIES   Generates a random probability vector
  2. %   This function has one required argument:
  3. %     N: the length of the probability vector
  4. %
  5. %   P = RandomProbabilities(N) generates a length-N probability vector,
  6. %   uniformly on the (N-1) unit simplex. That is, it generates a vector P
  7. %   with N entries, each of which is between 0 and 1 and such that sum(P) =
  8. %   1.
  9. %
  10. %   URL: http://www.qetlab.com/RandomProbabilities
  11.  
  12. %   requires: nothing
  13. %   author: Nathaniel Johnston (nathaniel@njohnston.ca)
  14. %   package: QETLAB
  15. %   last updated: December 2, 2014
  16.  
  17. function p = RandomProbabilities(n)
  18.  
  19. % Start by splitting the interval [0,1] up into n bins and then find the
  20. % width of each bin.
  21. p = diff([0,sort(rand(1,n-1),'ascend'),1]);