# 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, 2015 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]][[1, 2]] <= pairs[[1, 1]] + 1 ||
pairs[[1, 2]] < {##}[[1]][[1, 2]] <= pairs[[1, 2]] + 1,
{Red, ChartElementDataFunction["Rectangle"][##]},
"ColorScheme" -> "Rainbow", "GradientOrigin" -> Top][##]] &)] & /@ {"PDF", "CDF"}]


• 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, 2015 at 11:51
• dt={19, 18, 18, 18, 16, 18, 19, 16, 17, 17, 18, 17, 19, 16, 19, 17, 17, \ 19, 17, 17, 19, 17, 18, 18, 19, 17, 14, 19, 20, 20, 18, 18, 18, 18, \ 18, 19, 15, 18, 18, 20, 17, 18, 19, 19, 20, 17, 19, 19, 17, 19, 17, \ 19, 17, 18, 17, 19, 18, 19, 18, 20, 18, 19, 19, 18, 19, 18, 17, 18, \ 18, 18, 17, 17, 19, 18, 18, 17, 17, 20, 18, 18, 15, 20, 18, 18, 20, \ 16, 19, 17, 20, 19, 18, 18, 18, 18, 19, 16, 20, 19, 20, 17}
– kon
Apr 6, 2015 at 12:28
• In the PDF Histogram the probability of 16 is 0.05 alone, and the cumulated probability is 0.08
– kon
Apr 6, 2015 at 12:31
• ... 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[[1]])>0.05 otherwise. Is this correct?
– kglr
Apr 6, 2015 at 12:48
• well technically the probability of y=21 in the example should be 0, and the cumulated probability 1. Maybe the easiest solution is expanding the range for y. I think your solution by distinguishing the cases should give the correct result too.
– kon
Apr 6, 2015 at 13:16