7
$\begingroup$

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!

$\endgroup$
5
  • 1
    $\begingroup$ RandomChoise[{1,1,1,1,1,1,1,1,1,0}] assuming that RandomChoise has a constant probabality of choosing every element $\endgroup$
    – k_v
    Commented Apr 4, 2015 at 16:22
  • 3
    $\begingroup$ There is an incongruence between title and content of the question? $\endgroup$
    – enzotib
    Commented Apr 4, 2015 at 16:27
  • $\begingroup$ @k_v Try RandomChoice[{0.1, 0.9} -> {0, 1}, n] to do the weighting instead, it's much more flexible this way. $\endgroup$ Commented Apr 4, 2015 at 22:34
  • $\begingroup$ @MichaelE2 Sorry that I forgot to accept it after a long time! I used to accept answers pretty quickly, but I was suggested that perhaps I should have waited for more people to have a go with it. This is case, I simply forgot to come back and accept it! $\endgroup$ Commented May 11, 2015 at 19:44
  • $\begingroup$ No problem. I happened to come across it. Sometimes I forget for a while, too. :) $\endgroup$
    – Michael E2
    Commented May 11, 2015 at 19:45

3 Answers 3

26
$\begingroup$

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

$\endgroup$
4
  • 7
    $\begingroup$ RandomChoice does have the benefit of being compilable, unlike BernoulliDistribution[]. $\endgroup$ Commented Apr 4, 2015 at 22:29
  • 1
    $\begingroup$ @MichaelE2 Are you sure? f = Compile[{{n, _Integer}}, RandomChoice[{.9, .1} -> {1, 0}, n]] seems to compile for me, with the function RandomChoiceWeights in the result of CompilePrint $\endgroup$ Commented May 11, 2015 at 19:20
  • $\begingroup$ @blochwave, Oops, my bad. I forgot the _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. $\endgroup$
    – Michael E2
    Commented May 11, 2015 at 19:36
  • $\begingroup$ No problem! Admittedly performance tails off for large samples... $\endgroup$ Commented May 11, 2015 at 19:57
6
$\begingroup$
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}]
$\endgroup$
6
  • $\begingroup$ "One nice property of RandomChoice is that it can be generalized..." - so too can BernoulliDistribution... $\endgroup$
    – ciao
    Commented Apr 5, 2015 at 1:43
  • $\begingroup$ @rasher How can the 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? $\endgroup$ Commented Apr 5, 2015 at 2:28
  • $\begingroup$ Multiple Bernoulli is just Multinomial, hence, with one call to multinomial, this gives same kind of results as RandomChoice: 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 ;=) $\endgroup$
    – ciao
    Commented Apr 5, 2015 at 3:47
  • $\begingroup$ @rasher Could you give a working example, with actual values (as I did)? For instance, I couldn't see how to get your example to work with five probabilities and five variables. Thanks. $\endgroup$ Commented Apr 5, 2015 at 3:54
  • $\begingroup$ Sure, e.g. 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... $\endgroup$
    – ciao
    Commented Apr 5, 2015 at 4:28
5
$\begingroup$

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}]
$\endgroup$
1
  • 3
    $\begingroup$ No need for the i in the iterator. $\endgroup$ Commented Apr 4, 2015 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.