I think there is a bug in MultinomialDistribution[]. I want to generate N random variables with densities vec{p}={p1,p2,p3,...}

Of course I check that Total[vec{p}]=1 (they are densities so they sup up to 1).

Then I run RandomVariate[MultinomialDistribution[N, {p1,p2,p3,..}]]

It gives me the error: BinomialDistribution::probprm: Parameter 1.000000000000032` at position 2 in BinomialDistribution[147914,1.] is expected to be between 0 and 1 inclusive.

RandomVariate::distv: The result returned by Random`DistributionVector for the distribution MultinomialDistribution[7065081,{0.0000273949,0.00181668,0.0000287371,0.0000829715,4.1410310^-6,<<23>>,0.000244175,0.000468644,0.000268529,7.8525610^-6,<<5223>>}] was {{212,12896,168,561,26,1181,1647,3246,1901,43,<<5223>>}}, which is not a vector of complex numbers of length 1.

It looks likes the densities do not sum up to 1, but I assure that they do (tested with Total). Both N and the number of components of vec{p} are large, so that may be the reason, it is like Binomial is approximating badly something. Here is the N and the vec{p}. Here you can find the notebook with an example of N and vec{p}: https://drive.google.com/drive/folders/10akc6La0prsiqx5Zgj221MLrJD5nkCCn?usp=share_link

  • 1
    $\begingroup$ Can you concretize your question? RandomVariate[MultinomialDistribution[100, {1/2, 1/6, 1/3}], 50] works well for me. $\endgroup$
    – user64494
    Feb 8, 2023 at 15:05
  • 2
    $\begingroup$ Almost 1 is not 1. Don't use machine precision. pvec = Rationalize[pvec, 0]; pvec = pvec/Total[pvec]; RandomVariate[MultinomialDistribution[n1, pvec]] It will; however, be much slower. $\endgroup$
    – Bob Hanlon
    Feb 8, 2023 at 15:28
  • $\begingroup$ @user64494 The question is to understand why the case of the notebook I have provided gives error. $\endgroup$
    – sam84
    Feb 8, 2023 at 16:25
  • $\begingroup$ @BobHanlon thanks. I will test Rationalize. $\endgroup$
    – sam84
    Feb 8, 2023 at 16:26


Your Answer

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

Browse other questions tagged or ask your own question.