2

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]};


ListPlot[
            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]
                        },
            PlotRange->{{-8,8},{-8,8}}
        ]

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]?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
user27119
  • 2,500
  • 13
  • 34
  • 3
    Would With[{rmf = RegionMember[Ellipsoid[XYMean, 1 XYCoVarMat]]}, Select[XYDATA, rmf]] work for you? – J. M.'s missing motivation Nov 15 '19 at 16:09
  • 5
    It would work wonderfully. I'm starting to wonder if you are actually written purely from Mathematica functions. Thanks! – user27119 Nov 15 '19 at 16:14
  • @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. – user27119 Nov 16 '19 at 13:15
  • 1
    Please accept ThatGravityGuy's answer if you think you found it useful. – J. M.'s missing motivation Nov 16 '19 at 13:24

1 Answers1

2

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]]

{{0.997126,0.575375},{1.67482,1.73},{0.71546,1.72933},<<39325>>,{1.78994,1.09683},{1.23188,0.303475}}

NonDairyNeutrino
  • 7,810
  • 1
  • 14
  • 29