choose a number from a matrix based on probability parameter

1 visualización (últimos 30 días)
Hi
I have two different procedures to do, precedure A has 0.8 probabilty and B has 0.2. How can I create a parameter to select those procedures based upon them probability in case of iterative process ( I am doing this thousands of times)?
I have seen some answers here but not so sure that I am doing it right. Two solution are below but I got suspisous its correct when run it a multiple of 10 times where the 1's or 2's werent exactly showing at 80%,20% everytime for the first solution and the use of 'true' in the second one .
I am not expert instastics and probabilty so just asking if that correct way of doing it and is the result ok or not .
% solution 1
out = randsrc(1,1,[1,2;0.8,0.2])
if out==1
Procedure A;
else
procedure B
end
% solution2
p=[1 2];
y = randsample(p,1,true,[0.8,0.2])

Respuesta aceptada

Guillaume
Guillaume el 28 de Abr. de 2019
Either are overly complicated:
if rand > 0.8
do_ProcedureA;
else
do_ProcedureB;
end

Más respuestas (1)

John D'Errico
John D'Errico el 28 de Abr. de 2019
I think perhaps you do not understand probability. Imagine that you have a "fair" coin. Flip it once, and you expect to see it come up heads or tails 50% of the time for each. But flip it JUST ONCE. Is it heads, or is it tails. Did you see a 50% result? Of course not! The sample realization of such a probabilistic thing does not mean it is not a fair coin. It merely says that you took insufficiently many samples. Over the long run, you will expect 50%. In fact, for any odd number of flips, you could never get exactly 50%. But even for an even number, 50% is only a long term thing.
Similarly, flip the coin twice, Some of the time, you will get one head and one tail. But a significant number of times you will get two of one or the other. Yet nothing conflicts with that being a fair coin. It is just a random sample.
Likewise, you have a biased coin, one that comes up heads with frequency 80%, and tails with frequency 20%. Sample a finite number of times however, and there is no presumption that you will get EXACTLY 80/20. That only happens over the long run.
And that is how probability and random variables work.
n = 10;sum(rand(1,n) >= 0.2)/n
ans =
0.7
>> n = 100;sum(rand(1,n) >= 0.2)/n
ans =
0.78
>> n = 1000;sum(rand(1,n) >= 0.2)/n
ans =
0.805
>> n = 10000;sum(rand(1,n) >= 0.2)/n
ans =
0.7935
>> n = 100000;sum(rand(1,n) >= 0.2)/n
ans =
0.79878
>> n = 1000000;sum(rand(1,n) >= 0.2)/n
ans =
0.799477
>> n = 10000000;sum(rand(1,n) >= 0.2)/n
ans =
0.7998852
>> n = 100000000;sum(rand(1,n) >= 0.2)/n
ans =
0.79995754
Every once in a while, you might get exactly the frequency you expected. But don't bet your life savings on it.
  2 comentarios
caesar
caesar el 28 de Abr. de 2019
thanks alot, that was of great help. appreciate it
John D'Errico
John D'Errico el 28 de Abr. de 2019
I think this is a common mistake people make. It arises in many subtly different ways when random sampling is done.
The fallacy is most obvious when you consider a single coin flip, since you then recognize that you cannot have a coin that comes up half heads and half tails on one flip. Ok, it might land balanced just on its edge. And if that ever happens, save that coin, as that must be a REALLY lucky coin. :)

Iniciar sesión para comentar.

Categorías

Más información sobre Spline Postprocessing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by