I was looking at a very nice answer here, where the question was applying a confidence region to some data.

If we plot some data as a scatter plot:

XData = RandomVariate[NormalDistribution[1,1], 100000];
YData = RandomVariate[NormalDistribution[1,1], 100000];

XYDATA = Transpose[{XData, YData}];

XYCoVarMat = Covariance[XYDATA];
XYMean = {Mean[XData], Mean[YData]};

            XYDATA, Frame->True, FrameLabel->{"X","Y"}, LabelStyle->16, AspectRatio->1,
            Epilog -> {
                            Opacity[0.32, Green], EdgeForm[{Green, AbsoluteThickness[1]}], Ellipsoid[XYMean, 4 XYCoVarMat],
                            Opacity[0.32, Blue], EdgeForm[{Blue, AbsoluteThickness[1]}], Ellipsoid[XYMean, 3 XYCoVarMat],
                            Opacity[0.32, Red], EdgeForm[{Red, AbsoluteThickness[1]}], Ellipsoid[XYMean, 2 XYCoVarMat],
                            Opacity[0.32, Yellow], EdgeForm[{Yellow, AbsoluteThickness[1]}], Ellipsoid[XYMean, 1 XYCoVarMat]

I was wondering if anyone knew a way of filtering the data using this approach for example only select data that is with the ellipsoid boundary of Ellipsoid[XYMean, 1 XYCoVarMat]?

  • 3
    $\begingroup$ Would With[{rmf = RegionMember[Ellipsoid[XYMean, 1 XYCoVarMat]]}, Select[XYDATA, rmf]] work for you? $\endgroup$ – J. M. will be back soon Nov 15 at 16:09
  • 5
    $\begingroup$ It would work wonderfully. I'm starting to wonder if you are actually written purely from Mathematica functions. Thanks! $\endgroup$ – Q.P. Nov 15 at 16:14
  • $\begingroup$ @J.M.willbebacksoon I don't know if you want to post an answer yourself or not, but given you provided the solution I feel you should get credit. If credit doesn't concern you I will accept ThatGravityGuy's answer so that the question is completed and helps users who find this question. $\endgroup$ – Q.P. Nov 16 at 13:15
  • 1
    $\begingroup$ Please accept ThatGravityGuy's answer if you think you found it useful. $\endgroup$ – J. M. will be back soon Nov 16 at 13:24

As @J.M. pointed out in the comments, the key here is the function RegionMember. Since you already know the region that you want to work with, namely Ellipsoid[XYMean, 1 XYCoVarMat], you can use Select as

With[{rmf = RegionMember[Ellipsoid[XYMean, 1 XYCoVarMat]]}, Select[XYDATA, rmf]]



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.