public double topPercentile(double percent)
{
if (!(percent > 0.0 && percent <= 1.0))
throw new ApplicationException("percentile (" + percent + ") must be in (0.0, 1.0]");
double sampleWeight = weightSum();
if (!(sampleWeight > 0)) throw new ApplicationException("empty sample set");
sort();
double integral = 0, target = 1 - percent*sampleWeight;
int pos = samples_.Count<KeyValuePair<double, double>>(x => { integral += x.Value; return integral < target; } );
return samples_[pos].Key;
}