Java Code Examples for cern.colt.list.DoubleArrayList#size()

The following examples show how to use cern.colt.list.DoubleArrayList#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Descriptive.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the winsorized mean of a sorted data sequence.
 *
 * @param sortedData the data sequence; <b>must be sorted ascending</b>.
 * @param mean the mean of the (full) sorted data sequence.
 * @left the number of leading elements to trim.
 * @right the number of trailing elements to trim.
 */
public static double winsorizedMean(DoubleArrayList sortedData, double mean, int left, int right) {
	int N = sortedData.size();
	if (N==0) throw new IllegalArgumentException("Empty data.");
	if (left+right >= N) throw new IllegalArgumentException("Not enough data.");

	double[] sortedElements = sortedData.elements();

	double leftElement = sortedElements[left];
	for(int i=0; i<left; ++i)
		mean += (leftElement-sortedElements[i])/N;

	double rightElement = sortedElements[N-1-right];
	for(int i=0; i<right; ++i)
		mean += (rightElement-sortedElements[N-1-i])/N;

	return mean;
}
 
Example 2
Source File: AbstractDoubleIntMap.java    From jAudioGIT with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns a string representation of the receiver, containing
 * the String representation of each key-value pair, sorted ascending by key.
 */
public String toString() {
	DoubleArrayList theKeys = keys();
	theKeys.sort();

	StringBuffer buf = new StringBuffer();
	buf.append("[");
	int maxIndex = theKeys.size() - 1;
	for (int i = 0; i <= maxIndex; i++) {
		double key = theKeys.get(i);
	    buf.append(String.valueOf(key));
		buf.append("->");
	    buf.append(String.valueOf(get(key)));
		if (i < maxIndex) buf.append(", ");
	}
	buf.append("]");
	return buf.toString();
}
 
Example 3
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns the lag-1 autocorrelation of a dataset; 
 * Note that this method has semantics different from <tt>autoCorrelation(..., 1)</tt>;
 */
public static double lag1(DoubleArrayList data, double mean) {
	int size = data.size();
	double[] elements = data.elements();
	double r1 ;
	double q = 0 ;
	double v = (elements[0] - mean) * (elements[0] - mean) ;

	for (int i = 1; i < size ; i++) {
		double delta0 = (elements[i-1] - mean);
		double delta1 = (elements[i] - mean);
		q += (delta0 * delta1 - q)/(i + 1);
		v += (delta1 * delta1 - v)/(i + 1);
	}

	r1 = q / v ;
	return r1;
}
 
Example 4
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns the weighted mean of a data sequence.
 * That is <tt> Sum (data[i] * weights[i]) / Sum ( weights[i] )</tt>.
 */
public static double weightedMean(DoubleArrayList data, DoubleArrayList weights) {
	int size = data.size();
	if (size != weights.size() || size == 0) throw new IllegalArgumentException();
	
	double[] elements = data.elements();
	double[] theWeights = weights.elements();
	double sum = 0.0;
	double weightsSum = 0.0;
	for (int i=size; --i >= 0; ) {
		double w = theWeights[i];
		sum += elements[i] * w;
		weightsSum += w;
	}

	return sum/weightsSum;
}
 
Example 5
Source File: Descriptive.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Durbin-Watson computation.
 */
public static double durbinWatson(DoubleArrayList data) {
	int size = data.size();
	if (size < 2) throw new IllegalArgumentException("data sequence must contain at least two values.");

	double[] elements = data.elements();
	double run = 0;
	double run_sq = 0;
	run_sq = elements[0] * elements[0];
	for(int i=1; i<size; ++i) {
		double x = elements[i] - elements[i-1];
		run += x*x;
		run_sq += elements[i] * elements[i];
	}

	return run / run_sq;
}
 
Example 6
Source File: UnknownDoubleQuantileEstimator.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the specified quantile elements over the values previously added.
 * @param phis the quantiles for which elements are to be computed. Each phi must be in the interval (0.0,1.0]. <tt>phis</tt> must be sorted ascending.
 * @return the approximate quantile elements.
 */
public DoubleArrayList quantileElements(DoubleArrayList phis) {
	if (precomputeEpsilon<=0.0) return super.quantileElements(phis);
	
	int quantilesToPrecompute = (int) Utils.epsilonCeiling(1.0 / precomputeEpsilon);
	/*
	if (phis.size() > quantilesToPrecompute) {
		// illegal use case!
		// we compute results, but loose explicit approximation guarantees.
		return super.quantileElements(phis);
	}
	*/
 
	//select that quantile from the precomputed set that corresponds to a position closest to phi.
	phis = phis.copy();
	double e = precomputeEpsilon;
	for (int index=phis.size(); --index >= 0;) {
		double phi = phis.get(index);
		int i = (int) Math.round( ((2.0*phi/e) - 1.0 ) / 2.0); // finds closest
		i = Math.min(quantilesToPrecompute-1, Math.max(0,i));
		double augmentedPhi = (e/2.0)*(1+2*i);
		phis.set(index,augmentedPhi);				
	}
	
	return super.quantileElements(phis);
}
 
Example 7
Source File: UnknownDoubleQuantileEstimator.java    From jAudioGIT with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Computes the specified quantile elements over the values previously added.
 * @param phis the quantiles for which elements are to be computed. Each phi must be in the interval (0.0,1.0]. <tt>phis</tt> must be sorted ascending.
 * @return the approximate quantile elements.
 */
public DoubleArrayList quantileElements(DoubleArrayList phis) {
	if (precomputeEpsilon<=0.0) return super.quantileElements(phis);
	
	int quantilesToPrecompute = (int) Utils.epsilonCeiling(1.0 / precomputeEpsilon);
	/*
	if (phis.size() > quantilesToPrecompute) {
		// illegal use case!
		// we compute results, but loose explicit approximation guarantees.
		return super.quantileElements(phis);
	}
	*/
 
	//select that quantile from the precomputed set that corresponds to a position closest to phi.
	phis = phis.copy();
	double e = precomputeEpsilon;
	for (int index=phis.size(); --index >= 0;) {
		double phi = phis.get(index);
		int i = (int) Math.round( ((2.0*phi/e) - 1.0 ) / 2.0); // finds closest
		i = Math.min(quantilesToPrecompute-1, Math.max(0,i));
		double augmentedPhi = (e/2.0)*(1+2*i);
		phis.set(index,augmentedPhi);				
	}
	
	return super.quantileElements(phis);
}
 
Example 8
Source File: SparseVector.java    From first-stories-twitter with MIT License 5 votes vote down vote up
/**
 * Returns the Euclid norm which is sum(x[i]^2)
 * @return
 */
public double getEuclidNorm()
{
    DoubleArrayList dbls = new DoubleArrayList(this.cardinality());
    this.getNonZeros(null, dbls);
    double norm = 0;
    for (int i = 0; i < dbls.size(); i++) {
        norm += Math.pow(dbls.getQuick(i), 2);
    }
    return Math.sqrt(norm);
}
 
Example 9
Source File: QuantileFinderTest.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Finds the first and last indexes of a specific element within a sorted list.
 * @return int[]
 * @param list cern.colt.list.DoubleArrayList
 * @param element the element to search for
 */
protected static IntArrayList binaryMultiSearch(DoubleArrayList list, double element) {
	int index = list.binarySearch(element);
	if (index<0) return null; //not found

	int from = index-1;
	while (from>=0 && list.get(from)==element) from--;
	from++;
	
	int to = index+1;
	while (to<list.size() && list.get(to)==element) to++;
	to--;
	
	return new IntArrayList(new int[] {from,to});
}
 
Example 10
Source File: KnownDoubleQuantileEstimator.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 */
protected DoubleArrayList preProcessPhis(DoubleArrayList phis) {
	if (beta>1.0) {
		phis = phis.copy();
		for (int i=phis.size(); --i >=0;) {
			phis.set(i, (2*phis.get(i) + beta - 1) / (2*beta));
		}		
	}
	return phis;
}
 
Example 11
Source File: Descriptive.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the auto-correlation of a data sequence.
 */
public static double autoCorrelation(DoubleArrayList data, int lag, double mean, double variance) {
	int N = data.size();
	if (lag >= N) throw new IllegalArgumentException("Lag is too large");

	double[] elements = data.elements();
	double run = 0;
	for( int i=lag; i<N; ++i)
		run += (elements[i]-mean)*(elements[i-lag]-mean);
  
	return (run/(N-lag)) / variance;
}
 
Example 12
Source File: DynamicBin1D.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns a String representation of the receiver.
 */
public synchronized String toString() {
	StringBuffer buf = new StringBuffer(super.toString());
	DoubleArrayList distinctElements = new DoubleArrayList();
	IntArrayList frequencies = new IntArrayList();
	frequencies(distinctElements,frequencies);
	if (distinctElements.size() < 100) { // don't cause unintended floods
		buf.append("Distinct elements: "+distinctElements+"\n");
		buf.append("Frequencies: "+frequencies+"\n");
	}
	else {
		buf.append("Distinct elements & frequencies not printed (too many).");
	}
	return buf.toString();
}
 
Example 13
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the sample variance of a data sequence.
 * That is <tt>Sum ( (data[i]-mean)^2 ) / (data.size()-1)</tt>.
 */
public static double sampleVariance(DoubleArrayList data, double mean) {
	double[] elements = data.elements();
	int size = data.size();	
	double sum = 0 ;
	// find the sum of the squares 
	for (int i = size; --i >= 0; ) {
		double delta = elements[i] - mean;
		sum += delta * delta;
	}

	return sum / (size-1);
}
 
Example 14
Source File: AbstractDoubleIntMap.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Fills all keys and values <i>sorted ascending by key</i> into the specified lists.
 * Fills into the lists, starting at index 0.
 * After this call returns the specified lists both have a new size that equals <tt>this.size()</tt>.
 * <p>
 * <b>Example:</b>
 * <br>
 * <tt>keys = (8,7,6), values = (1,2,2) --> keyList = (6,7,8), valueList = (2,2,1)</tt>
 *
 * @param keyList the list to be filled with keys, can have any size.
 * @param valueList the list to be filled with values, can have any size.
 */
public void pairsSortedByKey(final DoubleArrayList keyList, final IntArrayList valueList) {
	/*
	keys(keyList); 
	values(valueList);
	
	final double[] k = keyList.elements();
	final int[] v = valueList.elements();
	cern.colt.Swapper swapper = new cern.colt.Swapper() {
		public void swap(int a, int b) {
			int t1;	double t2;
			t1 = v[a]; v[a] = v[b]; v[b] = t1;
			t2 = k[a]; k[a] = k[b];	k[b] = t2;
		}
	}; 

	cern.colt.function.IntComparator comp = new cern.colt.function.IntComparator() {
		public int compare(int a, int b) {
			return k[a]<k[b] ? -1 : k[a]==k[b] ? 0 : 1;
		}
	};
	cern.colt.MultiSorting.sort(0,keyList.size(),comp,swapper);
	*/	
	

	
	// this variant may be quicker
	//cern.colt.map.OpenDoubleIntHashMap.hashCollisions = 0;
	//System.out.println("collisions="+cern.colt.map.OpenDoubleIntHashMap.hashCollisions);
	keys(keyList);
	keyList.sort();
	valueList.setSize(keyList.size());
	for (int i=keyList.size(); --i >= 0; ) {
		valueList.setQuick(i,get(keyList.getQuick(i)));
	}
	//System.out.println("collisions="+cern.colt.map.OpenDoubleIntHashMap.hashCollisions);
	
}
 
Example 15
Source File: DoubleBuffer.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds all elements of the specified list to the receiver.
 * @param list the list of which all elements shall be added.
 */
public void addAllOf(DoubleArrayList list) {
	int listSize = list.size();
	if (this.size + listSize >= this.capacity) flush();
	this.target.addAllOf(list);
}
 
Example 16
Source File: DoubleBuffer2D.java    From database with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Adds all specified points (x,y) to the receiver.
 * @param x the x-coordinates of the points to add.
 * @param y the y-coordinates of the points to add.
 */
public void addAllOf(DoubleArrayList x, DoubleArrayList y) {
	int listSize = x.size();
	if (this.size + listSize >= this.capacity) flush();
	this.target.addAllOf(x, y);
}
 
Example 17
Source File: Descriptive.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Incrementally maintains and updates sum and sum of squares of a <i>weighted</i> data sequence.
 *
 * Assume we have already recorded some data sequence elements 
 * and know their sum and sum of squares.
 * Assume further, we are to record some more elements 
 * and to derive updated values of sum and sum of squares.
 * <p>
 * This method computes those updated values without needing to know the already recorded elements.
 * This is interesting for interactive online monitoring and/or applications that cannot keep the entire huge data sequence in memory.
 * <p>
 * <br>Definition of sum: <tt>sum = Sum ( data[i] * weights[i] )</tt>.
 * <br>Definition of sumOfSquares: <tt>sumOfSquares = Sum ( data[i] * data[i] * weights[i])</tt>.
 *
 *
 * @param data the additional elements to be incorporated into min, max, etc.
 * @param weights the weight of each element within <tt>data</tt>.
 * @param from the index of the first element within <tt>data</tt> (and <tt>weights</tt>) to consider.
 * @param to the index of the last element within <tt>data</tt> (and <tt>weights</tt>) to consider.
 * The method incorporates elements <tt>data[from], ..., data[to]</tt>.
 * @param inOut the old values in the following format:
 * <ul>
 * <li><tt>inOut[0]</tt> is the old sum.
 * <li><tt>inOut[1]</tt> is the old sum of squares.
 * </ul>
 * If no data sequence elements have so far been recorded set the values as follows 
 * <ul>
 * <li><tt>inOut[0] = 0.0</tt> as the old sum.
 * <li><tt>inOut[1] = 0.0</tt> as the old sum of squares.
 * </ul>
 *
 * @return the updated values filled into the <tt>inOut</tt> array.
 */
public static void incrementalWeightedUpdate(DoubleArrayList data, DoubleArrayList weights, int from, int to, double[] inOut) {
	int dataSize = data.size();
	checkRangeFromTo(from,to,dataSize);
	if (dataSize != weights.size()) throw new IllegalArgumentException("from="+from+", to="+to+", data.size()="+dataSize+", weights.size()="+weights.size());

	// read current values
	double sum = inOut[0];
	double sumOfSquares = inOut[1];

	double[] elements = data.elements();
	double[] w = weights.elements();
	
	for (int i=from-1; ++i<=to; ) {
		double element = elements[i];
		double weight = w[i];
		double prod = element*weight;
		
		sum += prod;
		sumOfSquares += element * prod;
	}

	// store new values
	inOut[0] = sum;
	inOut[1] = sumOfSquares;

	// At this point of return the following postcondition holds:
	// data.size()-from elements have been consumed by this call.
}
 
Example 18
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Incrementally maintains and updates sum and sum of squares of a <i>weighted</i> data sequence.
 *
 * Assume we have already recorded some data sequence elements 
 * and know their sum and sum of squares.
 * Assume further, we are to record some more elements 
 * and to derive updated values of sum and sum of squares.
 * <p>
 * This method computes those updated values without needing to know the already recorded elements.
 * This is interesting for interactive online monitoring and/or applications that cannot keep the entire huge data sequence in memory.
 * <p>
 * <br>Definition of sum: <tt>sum = Sum ( data[i] * weights[i] )</tt>.
 * <br>Definition of sumOfSquares: <tt>sumOfSquares = Sum ( data[i] * data[i] * weights[i])</tt>.
 *
 *
 * @param data the additional elements to be incorporated into min, max, etc.
 * @param weights the weight of each element within <tt>data</tt>.
 * @param from the index of the first element within <tt>data</tt> (and <tt>weights</tt>) to consider.
 * @param to the index of the last element within <tt>data</tt> (and <tt>weights</tt>) to consider.
 * The method incorporates elements <tt>data[from], ..., data[to]</tt>.
 * @param inOut the old values in the following format:
 * <ul>
 * <li><tt>inOut[0]</tt> is the old sum.
 * <li><tt>inOut[1]</tt> is the old sum of squares.
 * </ul>
 * If no data sequence elements have so far been recorded set the values as follows 
 * <ul>
 * <li><tt>inOut[0] = 0.0</tt> as the old sum.
 * <li><tt>inOut[1] = 0.0</tt> as the old sum of squares.
 * </ul>
 *
 * @return the updated values filled into the <tt>inOut</tt> array.
 */
public static void incrementalWeightedUpdate(DoubleArrayList data, DoubleArrayList weights, int from, int to, double[] inOut) {
	int dataSize = data.size();
	checkRangeFromTo(from,to,dataSize);
	if (dataSize != weights.size()) throw new IllegalArgumentException("from="+from+", to="+to+", data.size()="+dataSize+", weights.size()="+weights.size());

	// read current values
	double sum = inOut[0];
	double sumOfSquares = inOut[1];

	double[] elements = data.elements();
	double[] w = weights.elements();
	
	for (int i=from-1; ++i<=to; ) {
		double element = elements[i];
		double weight = w[i];
		double prod = element*weight;
		
		sum += prod;
		sumOfSquares += element * prod;
	}

	// store new values
	inOut[0] = sum;
	inOut[1] = sumOfSquares;

	// At this point of return the following postcondition holds:
	// data.size()-from elements have been consumed by this call.
}
 
Example 19
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns how many percent of the elements contained in the receiver are <tt>&lt;= element</tt>.
 * Does linear interpolation if the element is not contained but lies in between two contained elements.
 *
 * @param sortedList the list to be searched (must be sorted ascending).
 * @param element the element to search for.
 * @return the percentage <tt>phi</tt> of elements <tt>&lt;= element</tt> (<tt>0.0 &lt;= phi &lt;= 1.0)</tt>.
 */
public static double quantileInverse(DoubleArrayList sortedList, double element) {
	return rankInterpolated(sortedList,element) / sortedList.size();
}
 
Example 20
Source File: Descriptive.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns how many percent of the elements contained in the receiver are <tt>&lt;= element</tt>.
 * Does linear interpolation if the element is not contained but lies in between two contained elements.
 *
 * @param sortedList the list to be searched (must be sorted ascending).
 * @param element the element to search for.
 * @return the percentage <tt>phi</tt> of elements <tt>&lt;= element</tt> (<tt>0.0 &lt;= phi &lt;= 1.0)</tt>.
 */
public static double quantileInverse(DoubleArrayList sortedList, double element) {
	return rankInterpolated(sortedList,element) / sortedList.size();
}