PSquarePercentile.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.stat.descriptive.rank;

  22. import java.io.IOException;
  23. import java.io.ObjectInputStream;
  24. import java.io.Serializable;
  25. import java.text.DecimalFormat;
  26. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.Collection;
  29. import java.util.Collections;
  30. import java.util.List;

  31. import org.hipparchus.analysis.UnivariateFunction;
  32. import org.hipparchus.analysis.interpolation.LinearInterpolator;
  33. import org.hipparchus.analysis.interpolation.NevilleInterpolator;
  34. import org.hipparchus.analysis.interpolation.UnivariateInterpolator;
  35. import org.hipparchus.exception.LocalizedCoreFormats;
  36. import org.hipparchus.exception.MathIllegalArgumentException;
  37. import org.hipparchus.stat.descriptive.AbstractStorelessUnivariateStatistic;
  38. import org.hipparchus.stat.descriptive.StorelessUnivariateStatistic;
  39. import org.hipparchus.util.MathArrays;
  40. import org.hipparchus.util.MathUtils;
  41. import org.hipparchus.util.Precision;

  42. /**
  43.  * A {@link StorelessUnivariateStatistic} estimating percentiles using the
  44.  * <a href=http://www.cs.wustl.edu/~jain/papers/ftp/psqr.pdf>P<SUP>2</SUP></a>
  45.  * Algorithm as explained by <a href=http://www.cse.wustl.edu/~jain/>Raj
  46.  * Jain</a> and Imrich Chlamtac in
  47.  * <a href=http://www.cse.wustl.edu/~jain/papers/psqr.htm>P<SUP>2</SUP> Algorithm
  48.  * for Dynamic Calculation of Quantiles and Histogram Without Storing
  49.  * Observations</a>.
  50.  * <p>
  51.  * Note: This implementation is not synchronized and produces an approximate
  52.  * result. For small samples, where data can be stored and processed in memory,
  53.  * {@link Percentile} should be used.
  54.  */
  55. public class PSquarePercentile extends AbstractStorelessUnivariateStatistic
  56.     implements StorelessUnivariateStatistic, Serializable {

  57.     /** The maximum array size used for psquare algorithm */
  58.     private static final int PSQUARE_CONSTANT = 5;

  59.     /**
  60.      * A Default quantile needed in case if user prefers to use default no
  61.      * argument constructor.
  62.      */
  63.     private static final double DEFAULT_QUANTILE_DESIRED = 50d;

  64.     /** Serial ID */
  65.     private static final long serialVersionUID = 20150412L;

  66.     /** A decimal formatter for print convenience */
  67.     private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("00.00");

  68.     /**
  69.      * Initial list of 5 numbers corresponding to 5 markers. <b>NOTE:</b>watch
  70.      * out for the add methods that are overloaded
  71.      */
  72.     private final List<Double> initialFive = new FixedCapacityList<>(PSQUARE_CONSTANT);

  73.     /**
  74.      * The quantile needed should be in range of 0-1. The constructor
  75.      * {@link #PSquarePercentile(double)} ensures that passed in percentile is
  76.      * divided by 100.
  77.      */
  78.     private final double quantile;

  79.     /**
  80.      * lastObservation is the last observation value/input sample. No need to
  81.      * serialize.
  82.      */
  83.     private transient double lastObservation;

  84.     /**
  85.      * Markers is the marker collection object which comes to effect
  86.      * only after 5 values are inserted
  87.      */
  88.     private PSquareMarkers markers;

  89.     /**
  90.      * Computed p value (i,e percentile value of data set hither to received)
  91.      */
  92.     private double pValue = Double.NaN;

  93.     /**
  94.      * Counter to count the values/observations accepted into this data set
  95.      */
  96.     private long countOfObservations;

  97.     /**
  98.      * Constructs a PSquarePercentile with the specific percentile value.
  99.      * @param p the percentile
  100.      * @throws MathIllegalArgumentException  if p is not greater than 0 and less
  101.      * than or equal to 100
  102.      */
  103.     public PSquarePercentile(final double p) {
  104.         if (p > 100 || p < 0) {
  105.             throw new MathIllegalArgumentException(LocalizedCoreFormats.OUT_OF_RANGE,
  106.                                                    p, 0, 100);
  107.         }
  108.         this.quantile = p / 100d;// always set it within (0,1]
  109.     }

  110.     /**
  111.      * Default constructor that assumes a {@link #DEFAULT_QUANTILE_DESIRED
  112.      * default quantile} needed.
  113.      */
  114.     PSquarePercentile() {
  115.         this(DEFAULT_QUANTILE_DESIRED);
  116.     }

  117.     /**
  118.      * Copy constructor, creates a new {@code PSquarePercentile} identical
  119.      * to the {@code original}.
  120.      *
  121.      * @param original the {@code PSquarePercentile} instance to copy
  122.      * @throws org.hipparchus.exception.NullArgumentException if original is null
  123.      */
  124.     public PSquarePercentile(PSquarePercentile original) {
  125.         super();

  126.         this.quantile = original.quantile;

  127.         if (original.markers != null) {
  128.             this.markers = original.markers.copySelf();
  129.         }

  130.         this.countOfObservations = original.countOfObservations;
  131.         this.pValue = original.pValue;
  132.         this.initialFive.addAll(original.initialFive);
  133.     }

  134.     /** {@inheritDoc} */
  135.     @Override
  136.     public int hashCode() {
  137.         double result = getResult();
  138.         result = Double.isNaN(result) ? 37 : result;
  139.         final double markersHash = markers == null ? 0 : markers.hashCode();
  140.         final double[] toHash = {result, quantile, markersHash, countOfObservations};
  141.         return Arrays.hashCode(toHash);
  142.     }

  143.     /**
  144.      * Returns true iff {@code o} is a {@code PSquarePercentile} returning the
  145.      * same values as this for {@code getResult()} and {@code getN()} and also
  146.      * having equal markers
  147.      *
  148.      * @param o object to compare
  149.      * @return true if {@code o} is a {@code PSquarePercentile} with
  150.      * equivalent internal state
  151.      */
  152.     @Override
  153.     public boolean equals(Object o) {
  154.         boolean result = false;
  155.         if (this == o) {
  156.             result = true;
  157.         } else if (o instanceof PSquarePercentile) {
  158.             PSquarePercentile that = (PSquarePercentile) o;
  159.             boolean isNotNull = markers != null && that.markers != null;
  160.             boolean isNull = markers == null && that.markers == null;
  161.             result = isNotNull ? markers.equals(that.markers) : isNull;
  162.             // markers as in the case of first
  163.             // five observations
  164.             result = result && getN() == that.getN();
  165.         }
  166.         return result;
  167.     }

  168.     /**
  169.      * {@inheritDoc}The internal state updated due to the new value in this
  170.      * context is basically of the marker positions and computation of the
  171.      * approximate quantile.
  172.      *
  173.      * @param observation the observation currently being added.
  174.      */
  175.     @Override
  176.     public void increment(final double observation) {
  177.         // Increment counter
  178.         countOfObservations++;

  179.         // Store last observation
  180.         this.lastObservation = observation;

  181.         // 0. Use Brute force for <5
  182.         if (markers == null) {
  183.             if (initialFive.add(observation)) {
  184.                 Collections.sort(initialFive);
  185.                 pValue = initialFive.get((int) (quantile * (initialFive.size() - 1)));
  186.                 return;
  187.             }
  188.             // 1. Initialize once after 5th observation
  189.             markers = newMarkers(initialFive, quantile);
  190.         }
  191.         // 2. process a Data Point and return pValue
  192.         pValue = markers.processDataPoint(observation);
  193.     }

  194.     /**
  195.      * Returns a string containing the last observation, the current estimate
  196.      * of the quantile and all markers.
  197.      *
  198.      * @return string representation of state data
  199.      */
  200.     @Override
  201.     public String toString() {
  202.         synchronized (this) {
  203.             synchronized (DECIMAL_FORMAT) {
  204.                 if (markers == null) {
  205.                     return String.format("obs=%s pValue=%s",
  206.                                          DECIMAL_FORMAT.format(lastObservation),
  207.                                          DECIMAL_FORMAT.format(pValue));
  208.                 } else {
  209.                     return String.format("obs=%s markers=%s",
  210.                                          DECIMAL_FORMAT.format(lastObservation), markers.toString());
  211.                 }
  212.             }
  213.         }
  214.    }

  215.     /** {@inheritDoc} */
  216.     @Override
  217.     public long getN() {
  218.         return countOfObservations;
  219.     }

  220.     /** {@inheritDoc} */
  221.     @Override
  222.     public PSquarePercentile copy() {
  223.         return new PSquarePercentile(this);
  224.     }

  225.     /**
  226.      * Returns the quantile estimated by this statistic in the range [0.0-1.0]
  227.      *
  228.      * @return quantile estimated by {@link #getResult()}
  229.      */
  230.     public double quantile() {
  231.         return quantile;
  232.     }

  233.     /**
  234.      * {@inheritDoc}. This basically clears all the markers, the
  235.      * initialFive list and sets countOfObservations to 0.
  236.      */
  237.     @Override
  238.     public void clear() {
  239.         markers = null;
  240.         initialFive.clear();
  241.         countOfObservations = 0L;
  242.         pValue = Double.NaN;
  243.     }

  244.     /**
  245.      * {@inheritDoc}
  246.      */
  247.     @Override
  248.     public double getResult() {
  249.         if (Double.compare(quantile, 1d) == 0) {
  250.             pValue = maximum();
  251.         } else if (Double.compare(quantile, 0d) == 0) {
  252.             pValue = minimum();
  253.         }
  254.         return pValue;
  255.     }

  256.     /** Get quantile estimated by this statistic.
  257.      * @return the quantile estimated by this statistic
  258.      */
  259.     public double getQuantile() {
  260.         return quantile;
  261.     }

  262.     /** Get maximum in the data set added to this statistic.
  263.      * @return maximum in the data set added to this statistic
  264.      */
  265.     private double maximum() {
  266.         double val = Double.NaN;
  267.         if (markers != null) {
  268.             val = markers.height(PSQUARE_CONSTANT);
  269.         } else if (!initialFive.isEmpty()) {
  270.             val = initialFive.get(initialFive.size() - 1);
  271.         }
  272.         return val;
  273.     }

  274.     /** Get minimum in the data set added to this statistic.
  275.      * @return minimum in the data set added to this statistic
  276.      */
  277.     private double minimum() {
  278.         double val = Double.NaN;
  279.         if (markers != null) {
  280.             val = markers.height(1);
  281.         } else if (!initialFive.isEmpty()) {
  282.             val = initialFive.get(0);
  283.         }
  284.         return val;
  285.     }

  286.     /**
  287.      * Markers is an encapsulation of the five markers/buckets as indicated in
  288.      * the original works.
  289.      */
  290.     private static class Markers implements PSquareMarkers, Serializable {
  291.         /**
  292.          * Serial version id
  293.          */
  294.         private static final long serialVersionUID = 1L;

  295.         /** Low marker index */
  296.         private static final int LOW = 2;

  297.         /** High marker index */
  298.         private static final int HIGH = 4;

  299.         /**
  300.          * Array of 5+1 Markers (The first marker is dummy just so we
  301.          * can match the rest of indexes [1-5] indicated in the original works
  302.          * which follows unit based index)
  303.          */
  304.         private final Marker[] markerArray;

  305.         /**
  306.          * Constructor
  307.          *
  308.          * @param theMarkerArray marker array to be used, a reference to the array will be stored
  309.          */
  310.         private Markers(final Marker[] theMarkerArray) { // NOPMD - storing a reference to the array is intentional and documented here
  311.             MathUtils.checkNotNull(theMarkerArray);
  312.             markerArray = theMarkerArray;
  313.             for (int i = 1; i < PSQUARE_CONSTANT; i++) {
  314.                 markerArray[i].previous(markerArray[i - 1])
  315.                         .next(markerArray[i + 1]).index(i);
  316.             }
  317.             markerArray[0].previous(markerArray[0])
  318.                           .next(markerArray[1])
  319.                           .index(0);
  320.             markerArray[5].previous(markerArray[4])
  321.                           .next(markerArray[5])
  322.                           .index(5);
  323.         }

  324.         /**
  325.          * Constructor
  326.          *
  327.          * @param initialFive elements required to build Marker
  328.          * @param p quantile required to be computed
  329.          */
  330.         private Markers(final List<Double> initialFive, final double p) {
  331.             this(createMarkerArray(initialFive, p));
  332.         }

  333.         /**
  334.          * Creates a marker array using initial five elements and a quantile
  335.          *
  336.          * @param initialFive list of initial five elements
  337.          * @param p the pth quantile
  338.          * @return Marker array
  339.          */
  340.         private static Marker[] createMarkerArray(
  341.                 final List<Double> initialFive, final double p) {
  342.             final int countObserved =
  343.                     initialFive == null ? -1 : initialFive.size();
  344.             if (countObserved < PSQUARE_CONSTANT) {
  345.                 throw new MathIllegalArgumentException(
  346.                         LocalizedCoreFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE,
  347.                         countObserved, PSQUARE_CONSTANT);
  348.             }
  349.             Collections.sort(initialFive);
  350.             return new Marker[] {
  351.                     new Marker(),// Null Marker
  352.                     new Marker(initialFive.get(0), 1, 0, 1),
  353.                     new Marker(initialFive.get(1), 1 + 2 * p, p / 2, 2),
  354.                     new Marker(initialFive.get(2), 1 + 4 * p, p, 3),
  355.                     new Marker(initialFive.get(3), 3 + 2 * p, (1 + p) / 2, 4),
  356.                     new Marker(initialFive.get(4), 5, 1, 5) };
  357.         }

  358.         /**
  359.          * {@inheritDoc}
  360.          */
  361.         @Override
  362.         public int hashCode() {
  363.             return Arrays.deepHashCode(markerArray);
  364.         }

  365.         /**
  366.          * {@inheritDoc}.This equals method basically checks for marker array to
  367.          * be deep equals.
  368.          *
  369.          * @param o is the other object
  370.          * @return true if the object compares with this object are equivalent
  371.          */
  372.         @Override
  373.         public boolean equals(Object o) {
  374.             boolean result = false;
  375.             if (this == o) {
  376.                 result = true;
  377.             } else if (o instanceof Markers) {
  378.                 Markers that = (Markers) o;
  379.                 result = Arrays.deepEquals(markerArray, that.markerArray);
  380.             }
  381.             return result;
  382.         }

  383.         /**
  384.          * Process a data point
  385.          *
  386.          * @param inputDataPoint is the data point passed
  387.          * @return computed percentile
  388.          */
  389.         @Override
  390.         public double processDataPoint(final double inputDataPoint) {

  391.             // 1. Find cell and update minima and maxima
  392.             final int kthCell = findCellAndUpdateMinMax(inputDataPoint);

  393.             // 2. Increment positions
  394.             incrementPositions(1, kthCell + 1, 5);

  395.             // 2a. Update desired position with increments
  396.             updateDesiredPositions();

  397.             // 3. Adjust heights of m[2-4] if necessary
  398.             adjustHeightsOfMarkers();

  399.             // 4. Return percentile
  400.             return getPercentileValue();
  401.         }

  402.         /**
  403.          * Returns the percentile computed thus far.
  404.          *
  405.          * @return height of mid point marker
  406.          */
  407.         @Override
  408.         public double getPercentileValue() {
  409.             return height(3);
  410.         }

  411.         /**
  412.          * Finds the cell where the input observation / value fits.
  413.          *
  414.          * @param observation the input value to be checked for
  415.          * @return kth cell (of the markers ranging from 1-5) where observed
  416.          *         sample fits
  417.          */
  418.         private int findCellAndUpdateMinMax(final double observation) {
  419.             if (observation < height(1)) {
  420.                 markerArray[1].markerHeight = observation;
  421.                 return 1;
  422.             } else if (observation < height(2)) {
  423.                 return 1;
  424.             } else if (observation < height(3)) {
  425.                 return 2;
  426.             } else if (observation < height(4)) {
  427.                 return 3;
  428.             } else if (observation <= height(5)) {
  429.                 return 4;
  430.             } else {
  431.                 markerArray[5].markerHeight = observation;
  432.                 return 4;
  433.             }
  434.         }

  435.         /**
  436.          * Adjust marker heights by setting quantile estimates to middle markers.
  437.          */
  438.         private void adjustHeightsOfMarkers() {
  439.             for (int i = LOW; i <= HIGH; i++) {
  440.                 estimate(i);
  441.             }
  442.         }

  443.         /**
  444.          * {@inheritDoc}
  445.          */
  446.         @Override
  447.         public double estimate(final int index) {
  448.             MathUtils.checkRangeInclusive(index, LOW, HIGH);
  449.             return markerArray[index].estimate();
  450.         }

  451.         /**
  452.          * Increment positions by d. Refer to algorithm paper for the
  453.          * definition of d.
  454.          *
  455.          * @param d The increment value for the position
  456.          * @param startIndex start index of the marker array
  457.          * @param endIndex end index of the marker array
  458.          */
  459.         private void incrementPositions(final int d, final int startIndex,
  460.                 final int endIndex) {
  461.             for (int i = startIndex; i <= endIndex; i++) {
  462.                 markerArray[i].incrementPosition(d);
  463.             }
  464.         }

  465.         /**
  466.          * Desired positions incremented by bucket width. The bucket width is
  467.          * basically the desired increments.
  468.          */
  469.         private void updateDesiredPositions() {
  470.             for (int i = 1; i < markerArray.length; i++) {
  471.                 markerArray[i].updateDesiredPosition();
  472.             }
  473.         }

  474.         /**
  475.          * Sets previous and next markers after default read is done.
  476.          *
  477.          * @param anInputStream the input stream to be deserialized
  478.          * @throws ClassNotFoundException thrown when a desired class not found
  479.          * @throws IOException thrown due to any io errors
  480.          */
  481.         private void readObject(ObjectInputStream anInputStream)
  482.                 throws ClassNotFoundException, IOException {
  483.             // always perform the default de-serialization first
  484.             anInputStream.defaultReadObject();
  485.             // Build links
  486.             for (int i = 1; i < PSQUARE_CONSTANT; i++) {
  487.                 markerArray[i].previous(markerArray[i - 1]).next(markerArray[i + 1]).index(i);
  488.             }
  489.             markerArray[0].previous(markerArray[0]).next(markerArray[1]).index(0);
  490.             markerArray[5].previous(markerArray[4]).next(markerArray[5]).index(5);
  491.         }

  492.         /**
  493.          * Return marker height given index
  494.          *
  495.          * @param markerIndex index of marker within (1,6)
  496.          * @return marker height
  497.          */
  498.         @Override
  499.         public double height(final int markerIndex) {
  500.             MathUtils.checkRangeInclusive(markerIndex, 1, markerArray.length - 1);
  501.             return markerArray[markerIndex].markerHeight;
  502.         }

  503.         /** {@inheritDoc} */
  504.         @Override
  505.         public Markers copySelf() {
  506.             return new Markers(new Marker[] {
  507.                 new Marker(),
  508.                 markerArray[1].copySelf(),
  509.                 markerArray[2].copySelf(),
  510.                 markerArray[3].copySelf(),
  511.                 markerArray[4].copySelf(),
  512.                 markerArray[5].copySelf()
  513.             });

  514.         }

  515.         /**
  516.          * Returns string representation of the Marker array.
  517.          *
  518.          * @return Markers as a string
  519.          */
  520.         @Override
  521.         public String toString() {
  522.             return String.format("m1=[%s],m2=[%s],m3=[%s],m4=[%s],m5=[%s]",
  523.                     markerArray[1].toString(), markerArray[2].toString(),
  524.                     markerArray[3].toString(), markerArray[4].toString(),
  525.                     markerArray[5].toString());
  526.         }

  527.     }

  528.     /**
  529.      * The class modeling the attributes of the marker of the P-square algorithm
  530.      */
  531.     private static class Marker implements Serializable {

  532.         /**
  533.          * Serial Version ID
  534.          */
  535.         private static final long serialVersionUID = -3575879478288538431L;

  536.         /**
  537.          * The marker index which is just a serial number for the marker in the
  538.          * marker array of 5+1.
  539.          */
  540.         private int index;

  541.         /**
  542.          * The integral marker position. Refer to the variable n in the original
  543.          * works.
  544.          */
  545.         private double intMarkerPosition;

  546.         /**
  547.          * Desired marker position. Refer to the variable n' in the original
  548.          * works.
  549.          */
  550.         private double desiredMarkerPosition;

  551.         /**
  552.          * Marker height or the quantile. Refer to the variable q in the
  553.          * original works.
  554.          */
  555.         private double markerHeight;

  556.         /**
  557.          * Desired marker increment. Refer to the variable dn' in the original
  558.          * works.
  559.          */
  560.         private double desiredMarkerIncrement;

  561.         /**
  562.          * Next and previous markers for easy linked navigation in loops. this
  563.          * is not serialized as they can be rebuilt during deserialization.
  564.          */
  565.         private transient Marker next;

  566.         /**
  567.          * The previous marker links
  568.          */
  569.         private transient Marker previous;

  570.         /**
  571.          * Nonlinear interpolator
  572.          */
  573.         private final UnivariateInterpolator nonLinear = new NevilleInterpolator();

  574.         /**
  575.          * Linear interpolator which is not serializable
  576.          */
  577.         private transient UnivariateInterpolator linear = new LinearInterpolator();

  578.         /**
  579.          * Default constructor
  580.          */
  581.         private Marker() {
  582.             this.next = this.previous = this;
  583.         }

  584.         /**
  585.          * Constructor of the marker with parameters
  586.          *
  587.          * @param heightOfMarker represent the quantile value
  588.          * @param makerPositionDesired represent the desired marker position
  589.          * @param markerPositionIncrement represent increments for position
  590.          * @param markerPositionNumber represent the position number of marker
  591.          */
  592.         private Marker(double heightOfMarker, double makerPositionDesired,
  593.                 double markerPositionIncrement, double markerPositionNumber) {
  594.             this();
  595.             this.markerHeight = heightOfMarker;
  596.             this.desiredMarkerPosition = makerPositionDesired;
  597.             this.desiredMarkerIncrement = markerPositionIncrement;
  598.             this.intMarkerPosition = markerPositionNumber;
  599.         }

  600.         /**
  601.          * Sets the previous marker.
  602.          *
  603.          * @param previousMarker the previous marker to the current marker in
  604.          *            the array of markers
  605.          * @return this instance
  606.          */
  607.         private Marker previous(final Marker previousMarker) {
  608.             MathUtils.checkNotNull(previousMarker);
  609.             this.previous = previousMarker;
  610.             return this;
  611.         }

  612.         /**
  613.          * Sets the next marker.
  614.          *
  615.          * @param nextMarker the next marker to the current marker in the array
  616.          *            of markers
  617.          * @return this instance
  618.          */
  619.         private Marker next(final Marker nextMarker) {
  620.             MathUtils.checkNotNull(nextMarker);
  621.             this.next = nextMarker;
  622.             return this;
  623.         }

  624.         /**
  625.          * Sets the index of the marker.
  626.          *
  627.          * @param indexOfMarker the array index of the marker in marker array
  628.          * @return this instance
  629.          */
  630.         private Marker index(final int indexOfMarker) {
  631.             this.index = indexOfMarker;
  632.             return this;
  633.         }

  634.         /**
  635.          * Update desired Position with increment.
  636.          */
  637.         private void updateDesiredPosition() {
  638.             desiredMarkerPosition += desiredMarkerIncrement;
  639.         }

  640.         /**
  641.          * Increment Position by d.
  642.          *
  643.          * @param d a delta value to increment
  644.          */
  645.         private void incrementPosition(final int d) {
  646.             intMarkerPosition += d;
  647.         }

  648.         /**
  649.          * Difference between desired and actual position
  650.          *
  651.          * @return difference between desired and actual position
  652.          */
  653.         private double difference() {
  654.             return desiredMarkerPosition - intMarkerPosition;
  655.         }

  656.         /**
  657.          * Estimate the quantile for the current marker.
  658.          *
  659.          * @return estimated quantile
  660.          */
  661.         private double estimate() {
  662.             final double di = difference();
  663.             final boolean isNextHigher =
  664.                     next.intMarkerPosition - intMarkerPosition > 1;
  665.             final boolean isPreviousLower =
  666.                     previous.intMarkerPosition - intMarkerPosition < -1;

  667.             if (di >= 1 && isNextHigher || di <= -1 && isPreviousLower) {
  668.                 final int d = di >= 0 ? 1 : -1;
  669.                 final double[] xval = { previous.intMarkerPosition, intMarkerPosition, next.intMarkerPosition };
  670.                 final double[] yval = { previous.markerHeight, markerHeight, next.markerHeight };
  671.                 final double xD = intMarkerPosition + d;

  672.                 UnivariateFunction univariateFunction =
  673.                         nonLinear.interpolate(xval, yval);
  674.                 markerHeight = univariateFunction.value(xD);

  675.                 // If parabolic estimate is bad then turn linear
  676.                 if (isEstimateBad(yval, markerHeight)) {
  677.                     int delta = xD - xval[1] > 0 ? 1 : -1;
  678.                     final double[] xBad = { xval[1], xval[1 + delta] };
  679.                     final double[] yBad = { yval[1], yval[1 + delta] };
  680.                     MathArrays.sortInPlace(xBad, yBad);// since d can be +/- 1
  681.                     univariateFunction = linear.interpolate(xBad, yBad);
  682.                     markerHeight = univariateFunction.value(xD);
  683.                 }
  684.                 incrementPosition(d);
  685.             }
  686.             return markerHeight;
  687.         }

  688.         /**
  689.          * Check if parabolic/nonlinear estimate is bad by checking if the
  690.          * ordinate found is beyond the y[0] and y[2].
  691.          *
  692.          * @param y the array to get the bounds
  693.          * @param yD the estimate
  694.          * @return true if yD is a bad estimate
  695.          */
  696.         private boolean isEstimateBad(final double[] y, final double yD) {
  697.             return yD <= y[0] || yD >= y[2];
  698.         }

  699.         /**
  700.          * {@inheritDoc}<i>This equals method checks for marker attributes and
  701.          * as well checks if navigation pointers (next and previous) are the same
  702.          * between this and passed in object</i>
  703.          *
  704.          * @param o Other object
  705.          * @return true if this equals passed in other object o
  706.          */
  707.         @Override
  708.         public boolean equals(Object o) {
  709.             boolean result = false;
  710.             if (this == o) {
  711.                 result = true;
  712.             } else if (o instanceof Marker) {
  713.                 Marker that = (Marker) o;

  714.                 result = Double.compare(markerHeight, that.markerHeight) == 0;
  715.                 result =
  716.                         result &&
  717.                                 Double.compare(intMarkerPosition,
  718.                                         that.intMarkerPosition) == 0;
  719.                 result =
  720.                         result &&
  721.                                 Double.compare(desiredMarkerPosition,
  722.                                         that.desiredMarkerPosition) == 0;
  723.                 result =
  724.                         result &&
  725.                                 Double.compare(desiredMarkerIncrement,
  726.                                         that.desiredMarkerIncrement) == 0;

  727.                 result = result && next.index == that.next.index;
  728.                 result = result && previous.index == that.previous.index;
  729.             }
  730.             return result;
  731.         }

  732.         /** {@inheritDoc} */
  733.         @Override
  734.         public int hashCode() {
  735.             return Arrays.hashCode(new double[] {markerHeight, intMarkerPosition,
  736.                 desiredMarkerIncrement, desiredMarkerPosition, previous.index, next.index});
  737.         }

  738.         /**
  739.          * Read Object to deserialize.
  740.          *
  741.          * @param anInstream Stream Object data
  742.          * @throws IOException thrown for IO Errors
  743.          * @throws ClassNotFoundException thrown for class not being found
  744.          */
  745.         private void readObject(ObjectInputStream anInstream)
  746.                 throws ClassNotFoundException, IOException {
  747.             anInstream.defaultReadObject();
  748.             previous=next=this;
  749.             linear = new LinearInterpolator();
  750.         }

  751.         /** Copy this instance.
  752.          * @return copy of the instance
  753.          */
  754.         public Marker copySelf() {
  755.             return new Marker(markerHeight, desiredMarkerPosition, desiredMarkerIncrement, intMarkerPosition);
  756.         }

  757.         /**
  758.          * {@inheritDoc}
  759.          */
  760.         @Override
  761.         public String toString() {
  762.             return String.format(
  763.                     "index=%.0f,n=%.0f,np=%.2f,q=%.2f,dn=%.2f,prev=%d,next=%d",
  764.                     (double) index, Precision.round(intMarkerPosition, 0),
  765.                     Precision.round(desiredMarkerPosition, 2),
  766.                     Precision.round(markerHeight, 2),
  767.                     Precision.round(desiredMarkerIncrement, 2), previous.index,
  768.                     next.index);
  769.         }
  770.     }

  771.     /**
  772.      * A simple fixed capacity list that has an upper bound to growth.
  773.      * Once its capacity is reached, {@code add} is a no-op, returning
  774.      * {@code false}.
  775.      *
  776.      * @param <E> type of the elements
  777.      */
  778.     private static class FixedCapacityList<E> extends ArrayList<E> implements Serializable {

  779.         /**
  780.          * Serialization Version Id
  781.          */
  782.         private static final long serialVersionUID = 2283952083075725479L;
  783.         /**
  784.          * Capacity of the list
  785.          */
  786.         private final int capacity;

  787.         /**
  788.          * This constructor constructs the list with given capacity and as well
  789.          * as stores the capacity
  790.          *
  791.          * @param fixedCapacity the capacity to be fixed for this list
  792.          */
  793.         FixedCapacityList(final int fixedCapacity) {
  794.             super(fixedCapacity);
  795.             this.capacity = fixedCapacity;
  796.         }

  797.         /**
  798.          * {@inheritDoc} In addition it checks if the {@link #size()} returns a
  799.          * size that is within capacity and if true it adds; otherwise the list
  800.          * contents are unchanged and {@code false} is returned.
  801.          *
  802.          * @return true if addition is successful and false otherwise
  803.          */
  804.         @Override
  805.         public boolean add(final E e) {
  806.             return size() < capacity && super.add(e);
  807.         }

  808.         /**
  809.          * {@inheritDoc} In addition it checks if the sum of Collection size and
  810.          * this instance's {@link #size()} returns a value that is within
  811.          * capacity and if true it adds the collection; otherwise the list
  812.          * contents are unchanged and {@code false} is returned.
  813.          *
  814.          * @return true if addition is successful and false otherwise
  815.          */
  816.         @Override
  817.         public boolean addAll(Collection<? extends E> collection) {
  818.             boolean isCollectionLess =
  819.                     collection != null &&
  820.                             collection.size() + size() <= capacity;
  821.             return isCollectionLess && super.addAll(collection);
  822.         }

  823.         /** {@inheritDoc} */
  824.         @Override
  825.         public boolean equals(final Object other) {
  826.             return super.equals(other) && capacity == ((FixedCapacityList<?>) other).capacity;
  827.         }

  828.         /** {@inheritDoc} */
  829.         @Override
  830.         public int hashCode() {
  831.             return super.hashCode() + capacity;
  832.         }

  833.     }

  834.     /**
  835.      * A creation method to build Markers
  836.      *
  837.      * @param initialFive list of initial five elements
  838.      * @param p the quantile desired
  839.      * @return an instance of PSquareMarkers
  840.      */
  841.     public static PSquareMarkers newMarkers(final List<Double> initialFive, final double p) {
  842.         return new Markers(initialFive, p);
  843.     }

  844.     /**
  845.      * An interface that encapsulates abstractions of the
  846.      * P-square algorithm markers as is explained in the original works. This
  847.      * interface is exposed with protected access to help in testability.
  848.      */
  849.     protected interface PSquareMarkers {
  850.         /**
  851.          * Returns Percentile value computed thus far.
  852.          *
  853.          * @return percentile
  854.          */
  855.         double getPercentileValue();

  856.         /**
  857.          * A deep copy function to clone the current instance.
  858.          *
  859.          * @return deep copy of this instance
  860.          */
  861.         PSquareMarkers copySelf();

  862.         /**
  863.          * Returns the marker height (or percentile) of a given marker index.
  864.          *
  865.          * @param markerIndex is the index of marker in the marker array
  866.          * @return percentile value of the marker index passed
  867.          * @throws MathIllegalArgumentException in case the index is not within [1-5]
  868.          */
  869.         double height(int markerIndex);

  870.         /**
  871.          * Process a data point by moving the marker heights based on estimator.
  872.          *
  873.          * @param inputDataPoint is the data point passed
  874.          * @return computed percentile
  875.          */
  876.         double processDataPoint(double inputDataPoint);

  877.         /**
  878.          * An Estimate of the percentile value of a given Marker
  879.          *
  880.          * @param index the marker's index in the array of markers
  881.          * @return percentile estimate
  882.          * @throws MathIllegalArgumentException in case if index is not within [1-5]
  883.          */
  884.         double estimate(int index);
  885.     }
  886. }