# PiecewiseExpand fails with min of two piecewise functions

I have two Piecewise functions that are (in this case) just the empirical CDFs of two sets of data:

\$MaxPiecewiseCases = 1000;
DataPoints1=Sort[RandomVariate[NormalDistribution[RandomReal[{3,7}],RandomReal[{0.5,2}]],100]];
DataPoints2=Sort[RandomVariate[NormalDistribution[RandomReal[{3,7}],RandomReal[{0.5,2}]],100]];
PartialCDF1=Piecewise[Table[{1.*(i/Length[DataPoints1]),DataPoints1[[i]]<=x<DataPoints1[[i+1]]},{i,1,Length[DataPoints1]-1}]];
PartialCDF2=Piecewise[Table[{1.*(i/Length[DataPoints2]),DataPoints2[[i]]<=x<DataPoints2[[i+1]]},{i,1,Length[DataPoints2]-1}]];


When I find

Min[PartialCDF1, PartialCDF2]// FullForm


it just returns Min[...Piecewise1...Piecewise2...] instead of the combined function of the minimum across the range. So (based on a comment in this question) I tried

Simplify@PiecewiseExpand[Min[PartialCDF1, PartialCDF2]]


and that just never finishes. Also, it uses 24Gb of RAM while failing to perform this simple task. I've also tried every other combination of Simplify, PiecewiseExpand, and combinations with Evaluate, but nothing works to get the combined piecewise min function.

I've succeeded in doing this with other piecewise linear functions using the same code in a fraction of a second, so I really have no idea what is different in this case. How do I get Mathematica to provide the combined function in a case like this?

• You might want to look at EmpiricalDistribution. I think this does what your are trying to implement – mikado Apr 9 '17 at 11:02
• I think the problem must be that in the PiecewiseExpand it considers every possible piecewise case (e.g. about 10000 pieces) from the two functions it is combining with Min. – mikado Apr 9 '17 at 11:23
• Yes, the problem is clearly with PiecewiseExpand, but making 10,000 comparisons is easy and nearly instantaneous for Mathematica on other functions, so the cause must be something to do with the function specification that is incompatible with this way of using PiecewiseExpand. – Aaron Bramson Apr 9 '17 at 12:03
• Maybe you should explain what you are trying to achieve and explain the coefficient of overlapping (OVL). – gwr Apr 9 '17 at 13:28
• @gwr: It is true that if these were PDFs instead of CDFs then the integral of the min function would be the overlap coefficient. I need the min function in a form so that I can extract the boundaries of the piecewise min function to perform the integral; Mathematica won't do it properly without manual exclusions (as per my linked question). – Aaron Bramson Apr 9 '17 at 13:55