# Accumulating elements of lists taking the criteria out of another list

In order to find the critical values for the significance of a discrete distribution, for example BinomialDistribution[20,0.6], i need a function which works on the cumulated distrubtion function CDF and the point density function PDF.

Let pBin(x) be the probability of value x and cBin(x) the cumulated probability. Now i want to know the first x where the statement cBin(x)+1-cBin(y)>0.05 is true with y being the first value for which pBin(x)>=pBin(y) is true while y>x.

I tried to work with Select and Position but none of my attempts worked.

• Please show the work you've done so far. Click on the gold help when editing for code formatting help. – ciao Apr 5 '15 at 18:30

dt = RandomVariate[BinomialDistribution[20, 0.6], 100];

dist = EmpiricalDistribution[dt];
range = Range[Min@#, 1 + Max@#] &@dist["Domain"];
pairs = Select[MapIndexed[Function[{x, pos}, Join @@ {{x},
Select[range[[First[pos] + 1 ;;]],
PDF[dist, #] <= PDF[dist, x] && CDF[dist, x] + 1 - CDF[dist, #] >= .05 &, 1]}],
range], Length@# > 1 &, 1]
(* {{8,16}} *)

Row[Histogram[dt, {1}, #, ImageSize -> 400,
ChartElementFunction -> (If[pairs[[1, 1]] < {##}[][[1, 2]] <= pairs[[1, 1]] + 1 ||
pairs[[1, 2]] < {##}[][[1, 2]] <= pairs[[1, 2]] + 1,
{Red, ChartElementDataFunction["Rectangle"][##]}, • This is a very good method if the distribution is not too asymmetric. When choosing BinomialDistribution[20,0.9] however, the critical value is already reached at 16, but because this method can't find the corresponding pos it will instead give the pair {17,20}. – kon Apr 6 '15 at 11:51
• ... Looks like you are taking First x such that (1) cBin(x) > 0.05 if ylst= {y s.t y>x && pBin(x)<pBin(y)}=={}, (2) cBin(x)+1-cBin(ylst[])>0.05 otherwise. Is this correct? – kglr Apr 6 '15 at 12:48