/** * @param list the list * @param start the start index * @param end the end index * @return sublist */ public static DoubleList subList(DoubleList list, int start, int end) { if (list.isEmpty()) { return list; } if (start < 0 || end > list.size()) { throw new IndexOutOfBoundsException(""); } return new DoubleList(copyOfRange(list.toArray(), start, end), end - start); } }
/** * @param scale to be applied to the values of this list * @return a new instance scaled with the given parameter */ public DoubleList scale(double scale) { DoubleList scaled = new DoubleList(size); for (int i = 0; i < size; i++) { scaled.add(doubles[i] * scale); } return scaled; }
/** * Sorts the time series values. */ public void sort() { if (timestamps.size() > 1) { LongList sortedTimes = new LongList(timestamps.size()); DoubleList sortedValues = new DoubleList(values.size()); points().sorted((o1, o2) -> Long.compare(o1.getTimestamp(), o2.getTimestamp())).forEachOrdered(p -> { sortedTimes.add(p.getTimestamp()); sortedValues.add(p.getValue()); }); timestamps = sortedTimes; values = sortedValues; } }
/** * Returns a shallow copy of this <tt>LongList</tt> instance. (The * elements themselves are not copied.) * * @return a clone of this <tt>LongList</tt> instance */ public DoubleList copy() { DoubleList v = new DoubleList(size); v.doubles = Arrays.copyOf(doubles, size); v.size = size; return v; }
/** * Gets the metric data point at the index i * * @param i the index position of the metric value * @return the metric value */ public double getValue(int i) { return values.get(i); }
values = new DoubleList(calcAmountOfPoints); values.addAll(ts.getValuesAsArray());
/** * Implemented the quantile type 7 referred to * http://tolstoy.newcastle.edu.au/R/e17/help/att-1067/Quartiles_in_R.pdf * and * http://stat.ethz.ch/R-manual/R-patched/library/stats/html/quantile.html * as its the default quantile implementation * <p> * <code> * QuantileType7 = function (v, p) { * v = sort(v) * h = ((length(v)-1)*p)+1 * v[floor(h)]+((h-floor(h))*(v[floor(h)+1]- v[floor(h)])) * } * </code> * * @param percentile - the percentile (0 - 1), e.g. 0.25 * @return the value of the n-th percentile */ public double percentile(final double percentile) { if (values.isEmpty()) { return Double.NaN; } return values.percentile(percentile); }
/** * In some cases if one just want to access all values, * that method is faster than {@see getValues} due to no {@see DoubleList} initialization. * * @return a copy of the values as array */ public double[] getValuesAsArray() { return values.toArray(); }
/** * Appends all of the elements in the specified collection to the end of * this list, in the order that they are returned by the * specified collection's Iterator. The behavior of this operation is * undefined if the specified collection is modified while the operation * is in progress. (This implies that the behavior of this call is * undefined if the specified collection is this list, and this * list is nonempty.) * * @param c collection containing elements to be added to this list * @return <tt>true</tt> if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ public boolean addAll(DoubleList c) { double[] a = c.toArray(); int numNew = a.length; int newCapacity = calculateNewCapacity(doubles.length, size + numNew); growIfNeeded(newCapacity); System.arraycopy(a, 0, doubles, size, numNew); size += numNew; return numNew != 0; }
/** * Adds the given single data point to the time series * * @param timestamp the timestamp of the value * @param value the belonging value * @return the builder */ public Builder point(long timestamp, double value) { metricTimeSeries.timestamps.add(timestamp); metricTimeSeries.values.add(value); return this; }
/** * Implemented the quantile type 7 referred to * http://tolstoy.newcastle.edu.au/R/e17/help/att-1067/Quartiles_in_R.pdf * and * http://stat.ethz.ch/R-manual/R-patched/library/stats/html/quantile.html * as its the default quantile implementation * <p> * <code> * QuantileType7 = function (v, p) { * v = sort(v) * h = ((length(v)-1)*p)+1 * v[floor(h)]+((h-floor(h))*(v[floor(h)+1]- v[floor(h)])) * } * </code> * * @param percentile - the percentile (0 - 1), e.g. 0.25 * @return the value of the n-th percentile */ public double percentile(double percentile) { double[] copy = toArray(); Arrays.sort(copy);// Attention: this is only necessary because this list is not restricted to non-descending values return evaluateForDoubles(copy, percentile); }
/** * @param timestamps the timestamps as long[] * @param values the values as double[] */ public final void addAll(long[] timestamps, double[] values) { this.timestamps.addAll(timestamps); this.values.addAll(values); needsSort = true; }
/** * Calculates the standard deviation * * @param doubles a list with values * @return the standard deviation */ public static double dev(DoubleList doubles) { if (doubles.isEmpty()) { return Double.NaN; } return Math.sqrt(variance(doubles)); }
/** * @return average of the values of the list */ public double avg() { return values.avg(); }
/** * Sorts the time series values. */ public void sort() { if (needsSort && timestamps.size() > 1) { LongList sortedTimes = new LongList(timestamps.size()); DoubleList sortedValues = new DoubleList(values.size()); points().sorted((o1, o2) -> Long.compare(o1.getTimestamp(), o2.getTimestamp())).forEachOrdered(p -> { sortedTimes.add(p.getTimestamp()); sortedValues.add(p.getValue()); }); timestamps = sortedTimes; values = sortedValues; needsSort = false; } }
/** * Private constructor. * To instantiate a metric time series use the builder class. */ private MetricTimeSeries() { timestamps = new LongList(500); values = new DoubleList(500); }
/** * Gets the metric data point at the index i * * @param i the index position of the metric value * @return the metric value */ public double getValue(int i) { return values.get(i); }