public static double[] HPDInterval(double proportion,double[] x,int[] indices){
  double minRange=Double.MAX_VALUE;
  int hpdIndex=0;
  final int diff=(int)Math.round(proportion * (double)x.length);
  for (int i=0; i <= (x.length - diff); i++) {
    final double minValue=x[indices[i]];
    final double maxValue=x[indices[i + diff - 1]];
    final double range=Math.abs(maxValue - minValue);
    if (range < minRange) {
      minRange=range;
      hpdIndex=i;
    }
  }
  return new double[]{x[indices[hpdIndex]],x[indices[hpdIndex + diff - 1]]};
}

public static double [ ] HPDInterval ( double proportion , double [ ] x , int [ ] indices ) { double minRange = Double . MAX_VALUE ; int hpdIndex = 0 ; final int diff = ( int ) Math . round ( proportion * ( double ) x . length ) ; for ( int i = 0 ; i <= ( x . length - diff ) ; i ++ ) { final double minValue = x [ indices [ i ] ] ; final double maxValue = x [ indices [ i + diff - 1 ] ] ; final double range = Math . abs ( maxValue - minValue ) ; if ( range < minRange ) { minRange = range ; hpdIndex = i ; } } return new double [ ] { x [ indices [ hpdIndex ] ] , x [ indices [ hpdIndex + diff - 1 ] ] } ; }
determine the highest posterior density for a list of values . the hpd is the smallest interval containing the required amount of elements .
