1
$\begingroup$

I need to use the multinomial distribution.

In a certain step, by coincidence, the input probability vector is {2.6662875218*^-903, 6.180318409067*^-751, 0.5, 6.180318409*^-751, 0.5}.

When I do

RandomVariate[
 MultinomialDistribution[
  1, {2.6662875218*^-903, 6.180318409067*^-751,  0.5, 
     6.180318409*^-751, 0.5}], 1]

I get an error message for the values 2.6662875218*^-903, and 6.180318409*^-751, not being machine-size real numbers... When I run my programme on data, the vector of probabilities number of components will be of magnitude of 10^3.

What's the most efficient way to solve this?

Edit: In the above vector, the true values were slightly different. The values 0.5 aren't truly 0.5, since if we sum all the probabilities we must obtain 1. I just changed them to simplify the problem, but I now understand it may induce people into disregarding an important detail. Sorry.

$\endgroup$
2
$\begingroup$

You may use Threshold to push these tiny numbers towards 0. For example,

ϵ = $MachineEpsilon/2;
RandomVariate[
 MultinomialDistribution[
  1,
  Normalize[
   Threshold[
    {2.6662875218*^-903, 6.180318409067*^-751, 0.5, 6.180318409*^-751, 0.5}, 
    ϵ
    ],
   Total
   ]
  ], 
 1
 ]
$\endgroup$
  • $\begingroup$ Thanks for the answer Henrik. However, one thing that I didn't say, but I thought it was implicit is that the probabilities in the vector must sum to 1. If I just use the threshould, mathematica will realise they don't and stop all computations... $\endgroup$ – An old man in the sea. Nov 28 '18 at 9:57
  • $\begingroup$ Ah, I see. Normalize can cure the summation problem. Please have a look at my edit. $\endgroup$ – Henrik Schumacher Nov 28 '18 at 10:09
  • $\begingroup$ Numerical issues tend to be solved with logarithms, so the OP would be using log probabilities. Is that feasible in this case? I doubt it, but it may be worth considering. $\endgroup$ – Titus Nov 28 '18 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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