Selecting a random number with some probability
260 次查看(过去 30 天)
显示 更早的评论
Hello Everyone, I am using this one line of code to generate a single value either to be 1 or 2 with equal probability but my question is that how can i select the value to be 1 with 60% probability and the value to be 2 with 40% probability ? Thank you.
select=randi(2,1,1);
0 个评论
采纳的回答
更多回答(5 个)
pankaj singh
2018-3-10
The simplest technique is to use inbuilt Matlab function 'randscr'.
Suppose you want to generate M by N matrix of W, X, Y, and Z with probabilities i,j,k, and l. Then use
out = randsrc(M,N,[W,X,Y,Z;i,j,k,l]); % i+j+k+l = 1;
In your case, as you want a single value to be generated, your M x N = 1 x 1 matrix; the values are 1 with 60% probability (i.e. 0.6) and 2 with 40% (i.e. 0.4) probability, therefore use this;
out = randsrc(1,1,[1,2;0.6,0.4]);
Note that the above is just an example. You can create any matrix size with any number of values. The sum of probabilities must be equal to 1.
3 个评论
Jonathan Ford
2021-7-11
I'm not sure what is causing your error, but you could try writing your own randsrc function, something like this:
function X = myrandsrc(M, N, A)
X = reshape(A(1,sum(A(2,:) < rand(M*N,1)*ones(1,size(A,2)),2)+1),M,N);
end
Then:
X = myrandsrc(4,5,[1 2 3 4; 0.4 0.7 0.9 1])
will return something like:
X =
1 2 3 2 2
2 1 4 3 2
3 2 2 3 2
2 2 2 3 2
Note that for this implementation, you need to use the cumulative probability distribution in the second row of A, so with the above call you will get ~40% of 1, 30% of 2, 20% of 3 and 10% of 4.
Below is the function separated out into multiple lines, to better explain how it works:
function X = myrandsrc(M, N, A)
% the number of elements to chose from
sz = size(A,2);
% generate some random numbers
r = rand(M*N,1)*ones(1,sz);
% determine the correct elements
r = sum(A(2,:) < r,2)+1;
% select the correct elements
X = A(1,r);
% Reshape into M x N matrix
X = reshape(X,M,N);
end
Daniel
2015-2-25
You can use rand, which gives uniform distribution and look if the number is below or above 0.6.
if(0.6 <= rand()){
select = 1;
} else {
select = 2;
}
That should give 60/40 chances. There are more elegant ways to do that though.
2 个评论
Trung Khoa Le
2019-1-17
Could you please give me a bit explanation why this way makes sense or some documentation that I can read to gain some intuition? Thanks
Luciano Anastassiou
2019-5-22
Hi Trung Khoa Le,
The intuition is simply that "rand" generates a random number between 0 and 1. Then when you apply the "if" constraint, you are telling the system to only give out the result "select = 1" when that random number is below 0.6. Otherwise it will give out "select = 2".
If you repeated this 1000s of times, it would give out "select = 1" 60% of the time, because 60% of those random numbers between 0 and 1 will be below 0.6, and the other 40% of the time it will give out "select = 2".
Jos (10584)
2015-2-25
编辑:Jos (10584)
2015-2-25
For two values it is simple
VAL = [10 20] % 2 values
P = .8 % probabbility of selecting first value
Ndraw = 20 % number of draws
R = rand(Ndraw,1) < P
SEL = VAL(2 - R) % use as index into VAL
For more complicated cases you might be interested inTake a look at my RANDP function, which picks random values with relative probabilities.
Steven Lord
2022-6-16
Another approach is to use the discretize function to discretize a uniform random number between 0 and 1 as generated by the rand function. Because edges is [0, 0.6, 1] any values in uniform that are in the range [0, 0.6) will be mapped to 1 in oneOrTwo and any values in uniform in the range [0.6, 1] will be mapped to 2.
probabilities = [0.6 0.4];
edges = cumsum([0 probabilities])
uniform = rand(1, 1e4);
oneOrTwo = discretize(uniform, edges);
We can check using histogram that the generated numbers have the right distribution (or close to it.)
histogram(oneOrTwo, 'Normalization', 'probability')
% Draw lines at the desired probabilities
yline(probabilities, 'r:')
Those look to be in pretty good agreement with the desired probabilities to me.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!