This maybe a simple question, but I am just stuck with it. I want to do some simulation, say with 0.9 probability, I get a 1, and 0.1 probability get a 0.
How would I do that? Where should I start?
Thanks!
This maybe a simple question, but I am just stuck with it. I want to do some simulation, say with 0.9 probability, I get a 1, and 0.1 probability get a 0.
How would I do that? Where should I start?
Thanks!
BernoulliDistribution is a perfect fit for this.
RandomVariate[BernoulliDistribution[1 - 0.1], {50}]
{1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1}
Also, as kguler states, you can use RandomChoice, but the benefit of BernoulliDistribution is that you can operate it also as an abstract distribution, not only a source of randomness. For instance, you can compute its symbolic variance:
Variance[BernoulliDistribution[1 - 1/n]]
(1 - 1/n)/n
RandomChoice does have the benefit of being compilable, unlike BernoulliDistribution[].
– dr.blochwave
Apr 04 '15 at 22:29
f = Compile[{{n, _Integer}}, RandomChoice[{.9, .1} -> {1, 0}, n]] seems to compile for me, with the function RandomChoiceWeights in the result of CompilePrint
– dr.blochwave
May 11 '15 at 19:20
_Integer on that one but not the others. :/ Now the compiled version is faster until about 10^4 or 10^5. Sorry for the noise.
– Michael E2
May 11 '15 at 19:36
RandomChoice[{.9, .1} -> {1, 0}, 10]
(* {0, 1, 1, 1, 1, 1, 1, 0, 1, 1} *)
Timing results
Timing[RandomVariate[BernoulliDistribution[.9], {10^8}];] (* {3.38014, Null} *)Timing[RandomChoice[{.9, .1} -> {1, 0}, 10^8];](* {5.64937, Null} *)dist[] := If[RandomReal[] > 0.9, 0, 1]; Timing[Table[dist[], {i, 10^8}];] (* {13.9842, Null} *)One nice property of RandomChoice is that it can be generalized, for instance:
RandomChoice[{.7, .2, .1}-> {0, 1, 2}]
BernoulliDistribution be generalized to give the same as RandomChoice[{.3, .2, .4, .05, .05}-> {-2, 5, 3, 2, "A"}] naturally, i.e., without multiple calls?
– David G. Stork
Apr 05 '15 at 02:28
RandomSample@ Flatten@MapThread[ ConstantArray[#1, #2] &, {#3, RandomVariate[MultinomialDistribution[#1, #2]]}] & given count, probs. and returned values... no real reason to do so, but it is one call to a distribution ;=)
– ciao
Apr 05 '15 at 03:47
RandomSample@ Flatten@MapThread[ ConstantArray[#1, #2] &, {#3, RandomVariate[ MultinomialDistribution[#1, #2]]}] &[20, {.3, .2, .4, .05, \ .05}, {-2, 5, 3, 2, "A"}] gives same kind of result as RandomChoice[{.3, .2, .4, .05, .05}-> {-2, 5, 3, 2, "A"},20]. But again, no real reason to do this (I suppose if one were generating the dist. probs. programmatically and wanted to use probabilistic functions against them also this has a place) - just some gentle leg-pulling...
– ciao
Apr 05 '15 at 04:28
Take a uniform random distribution and check if it is above some threshold (0.9 in your case).
For example:
dist[] := If[RandomReal[] > 0.9, 0, 1];
Table[dist[], {i, 100}]
RandomChoise[{1,1,1,1,1,1,1,1,1,0}]assuming thatRandomChoisehas a constant probabality of choosing every element – k_v Apr 04 '15 at 16:22RandomChoice[{0.1, 0.9} -> {0, 1}, n]to do the weighting instead, it's much more flexible this way. – 2012rcampion Apr 04 '15 at 22:34