12
$\begingroup$

This is inspired by this answer which features code that I cannot get to work at all, and I tried to figure out why.

It boils down to using the function RandomPoint, which is new and a little bit tricky to use (see here and here), in combination with ImplicitRegion.

Here are two and three dimensional regions,

twoDRegion = 
  ImplicitRegion[{0 <= a <= 1, 0 <= b <= 1, a + b == 1}, {a, b}];
threeDRegion = 
  ImplicitRegion[{0 <= a <= 1, 0 <= b <= 1, 0 <= c <= 1, 
    a + b + c == 1}, {a, b, c}];

These regions are the set of all positive tuples that sum to 1,

RandomPoint works on the 2D version, but not the 3D version,

RandomPoint /@ {twoDRegion, threeDRegion}
(* {{0.146978, 0.853022}, 
 RandomPoint[
  ImplicitRegion[
   0 <= a <= 1 && 0 <= b <= 1 && 0 <= c <= 1 && a + b + c == 1, {a, b,
     c}]]} *)

Interestingly, RegionPlot also has trouble with the 3D version,

RegionPlot /@ {twoDRegion, threeDRegion}

enter image description here

but DiscretizeRegion can handle both

DiscretizeRegion /@ {twoDRegion, threeDRegion}

enter image description here

This led me to think that I just need to discretize before selecting the random point, and this in fact works for the 3D case

RandomPoint@DiscretizeRegion@# & /@ {twoDRegion, threeDRegion}
(* {{0.579605, 0.420395}, {0.154819, 0.0404491, 0.804732}} *)

But it won't work for higher dimensions since DiscretizeRegion is limited to 3 dimensions or lower.

Is this a bug?

$\endgroup$
3
  • $\begingroup$ I have run into the problem you describe here myself and have accepted the behavior as either an incomplete implementation or a design choice by the developers, not a bug. That is only a speculation on my part, of course. I hope you get an informed answer, perhaps from someone connected to WRI. $\endgroup$
    – m_goldberg
    Commented Feb 8, 2016 at 12:03
  • $\begingroup$ Dear OP, did you find any solution for this issue? I also have similar requirement to use RandomPoint in higher dimension region. Thanks $\endgroup$
    – Prashanth
    Commented Nov 7, 2016 at 2:57
  • $\begingroup$ Any word on a fix for this? I can't sample from BooleanRegions of with n>3. $\endgroup$
    – Edmund
    Commented May 18, 2018 at 23:27

1 Answer 1

3
$\begingroup$

In 12.1.1 this is still a problem. For a workaround I've noticed that making the a + b + c == 1 plane 'thicker' using 1 - 10^-6 <= a + b + c <= 1 allows you to generate points, and you can correct the small error by using #/Total[#] & so that a + b + c is exactly 1 without introducing any significant bias into the sampling:

#/Total[#] &@
 RandomPoint@
  ImplicitRegion[{0 <= a <= 1, 0 <= b <= 1, 0 <= c <= 1, 
    1 - 10^-6 < a + b + c <= 1}, {a, b, c}]

Interestingly, the thinner the final inequality gets the harder Mathematica has to work, and when close to the $MachineEpsilon it hangs indefinitely on my machine. There is definitely something Mathematica doesn't like about sampling in 3D with a strict equality constraint.

$\endgroup$

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.