RandomProbabilities
From QETLAB
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.
Contents
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:
Source code
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) =
% 1.
%
% URL: http://www.qetlab.com/RandomProbabilities
% requires: nothing
% author: Nathaniel Johnston (nathaniel@njohnston.ca)
% 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.
p = diff([0,sort(rand(1,n-1),'ascend'),1]);