Matlab: selecting a number 'i' with a probability 'i' in matlab -
i stuck @ problem in matlab. how select number 'i' probability 'i' in matrix. want in matrix, select number 'i' each row, probability 'i'.
any help appreciated
a sample matrix( dont need select zeros):
w1= .47; w2= .023; m1= .06; m2= .037; x=rand(1,m1)<=m1; tolerance= 0.01; transition=[m1 w1 0 w1 0 0 0; 0 m1 w1 0 0 0 0; 0 0 m1 w1 0 0 0; 0 0 0 m2 w2 0 0; 0 0 0 0 m2 w1 w1; 0 0 0 0 0 m1 w1 0 0 0 0 0 0 m1];
first, calculate matrix of cumulative probabilities tc
in preparation strategy finds first column exceeding random value between 0 , 1.
tc = zeros(size(t)); tc(:, 1) = t(:, 1); k = 2 : size(t, 2) tc(:, k) = tc(:, k - 1) + t(:, k); end
to draw numbers each row, first draw p = rand(size(t, 2), 1)
, find column p
falls bucket of cumulative probability:
for k = 1 : size(t, 1) col = find(t(k, :) > p(k), 1, 'first'); if isempty(col) fprintf('nothing drawn row %d\n', k); else fprintf('row %d, col %d, p = %f\n', k, col, t(k, col)); end end
this works because order not matter. example, p = 0.2, arbitrary cumulative distribution increment 0.65 0.85. probability rand
yield value in interval indeed 0.2. if lastly non-zero entry of t
, rand
homecoming value 0.15 probability no number drawn. in example, if entries in t
zero, cumulative distribution never exceed can drawn rand
. lastly example, if there 2 entries @ 0.5, each, cumulative distribution exceed what's drawn rand
half , half @ either column.
matlab probability
No comments:
Post a Comment