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

The following examples show how to use cern.colt.list.DoubleArrayList#elements() . 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 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 2
Source File: Descriptive.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the covariance of two data sequences, which is 
 * <tt>cov(x,y) = (1/(size()-1)) * Sum((x[i]-mean(x)) * (y[i]-mean(y)))</tt>.
 * See the <A HREF="http://www.cquest.utoronto.ca/geog/ggr270y/notes/not05efg.html"> math definition</A>.
 */
public static double covariance(DoubleArrayList data1, DoubleArrayList data2) {
	int size = data1.size();
	if (size != data2.size() || size == 0) throw new IllegalArgumentException();
	double[] elements1 = data1.elements();
	double[] elements2 = data2.elements();
	
	double sumx=elements1[0], sumy=elements2[0], Sxy=0;
	for (int i=1; i<size; ++i) {
		double x = elements1[i];
		double y = elements2[i];
		sumx += x;
		Sxy += (x - sumx/(i+1))*(y - sumy/i);
		sumy += y;
		// Exercise for the reader: Why does this give us the right answer?
	}
	return Sxy/(size-1);
}
 
Example 3
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 4
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns the covariance of two data sequences, which is 
 * <tt>cov(x,y) = (1/(size()-1)) * Sum((x[i]-mean(x)) * (y[i]-mean(y)))</tt>.
 * See the <A HREF="http://www.cquest.utoronto.ca/geog/ggr270y/notes/not05efg.html"> math definition</A>.
 */
public static double covariance(DoubleArrayList data1, DoubleArrayList data2) {
	int size = data1.size();
	if (size != data2.size() || size == 0) throw new IllegalArgumentException();
	double[] elements1 = data1.elements();
	double[] elements2 = data2.elements();
	
	double sumx=elements1[0], sumy=elements2[0], Sxy=0;
	for (int i=1; i<size; ++i) {
		double x = elements1[i];
		double y = elements2[i];
		sumx += x;
		Sxy += (x - sumx/(i+1))*(y - sumy/i);
		sumy += y;
		// Exercise for the reader: Why does this give us the right answer?
	}
	return Sxy/(size-1);
}
 
Example 5
Source File: Descriptive.java    From database with GNU General Public License v2.0 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 6
Source File: Descriptive.java    From database with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the <tt>phi-</tt>quantile; that is, an element <tt>elem</tt> for which holds that <tt>phi</tt> percent of data elements are less than <tt>elem</tt>.
 * The quantile need not necessarily be contained in the data sequence, it can be a linear interpolation.
 * @param sortedData the data sequence; <b>must be sorted ascending</b>.
 * @param phi the percentage; must satisfy <tt>0 &lt;= phi &lt;= 1</tt>.
 */
public static double quantile(DoubleArrayList sortedData, double phi) {
	double[] sortedElements = sortedData.elements();
	int n = sortedData.size();
	
	double index = phi * (n - 1) ;
	int lhs = (int)index ;
	double delta = index - lhs ;
	double result;

	if (n == 0) return 0.0 ;

	if (lhs == n - 1) {
		result = sortedElements[lhs] ;
	}
	else {
		result = (1 - delta) * sortedElements[lhs] + delta * sortedElements[lhs + 1] ;
	}

	return result ;
}
 
Example 7
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 8
Source File: Descriptive.java    From database with GNU General Public License v2.0 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 9
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns the <tt>phi-</tt>quantile; that is, an element <tt>elem</tt> for which holds that <tt>phi</tt> percent of data elements are less than <tt>elem</tt>.
 * The quantile need not necessarily be contained in the data sequence, it can be a linear interpolation.
 * @param sortedData the data sequence; <b>must be sorted ascending</b>.
 * @param phi the percentage; must satisfy <tt>0 &lt;= phi &lt;= 1</tt>.
 */
public static double quantile(DoubleArrayList sortedData, double phi) {
	double[] sortedElements = sortedData.elements();
	int n = sortedData.size();
	
	double index = phi * (n - 1) ;
	int lhs = (int)index ;
	double delta = index - lhs ;
	double result;

	if (n == 0) return 0.0 ;

	if (lhs == n - 1) {
		result = sortedElements[lhs] ;
	}
	else {
		result = (1 - delta) * sortedElements[lhs] + delta * sortedElements[lhs + 1] ;
	}

	return result ;
}
 
Example 10
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 11
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 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: 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 13
Source File: Descriptive.java    From database with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the trimmed 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 trimmedMean(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();
	int N0=N;
	for(int i=0; i<left; ++i)
		mean += (mean-sortedElements[i])/(--N);
	for(int i=0; i<right; ++i)
		mean += (mean-sortedElements[N0-1-i])/(--N);
	return mean;
}
 
Example 14
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the sum of logarithms of a data sequence, which is <tt>Sum( Log(data[i])</tt>.
 * @param data the data sequence.
 * @param from the index of the first data element (inclusive).
 * @param to the index of the last data element (inclusive).
 */
public static double sumOfLogarithms(DoubleArrayList data, int from, int to) {
	double[] elements = data.elements();
	double logsum = 0;
	for (int i=from-1; ++i <= to;) logsum += Math.log(elements[i]);
	return logsum;
}
 
Example 15
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the mean deviation of a dataset.
 * That is <tt>Sum (Math.abs(data[i]-mean)) / data.size())</tt>.
 */
public static double meanDeviation(DoubleArrayList data, double mean) {
	double[] elements = data.elements();
	int size = data.size();
	double sum=0;
	for (int i=size; --i >= 0;) sum += Math.abs(elements[i]-mean);
	return sum/size;
}
 
Example 16
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the trimmed 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 trimmedMean(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();
	int N0=N;
	for(int i=0; i<left; ++i)
		mean += (mean-sortedElements[i])/(--N);
	for(int i=0; i<right; ++i)
		mean += (mean-sortedElements[N0-1-i])/(--N);
	return mean;
}
 
Example 17
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Modifies a data sequence to be standardized.
 * Changes each element <tt>data[i]</tt> as follows: <tt>data[i] = (data[i]-mean)/standardDeviation</tt>.
 */
public static void standardize(DoubleArrayList data, double mean, double standardDeviation) {
	double[] elements = data.elements();
	for (int i=data.size(); --i >= 0;) elements[i] = (elements[i]-mean)/standardDeviation;
}
 
Example 18
Source File: Descriptive.java    From jAudioGIT with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Returns <tt>Sum( (data[i]-c)<sup>k</sup> )</tt> for all <tt>i = from .. to</tt>; optimized for common parameters like <tt>c == 0.0</tt> and/or <tt>k == -2 .. 5</tt>.
 */
public static double sumOfPowerDeviations(final DoubleArrayList data, final int k, final double c, final int from, final int to) {
	final double[] elements = data.elements();
	double sum = 0;
	double v;
	int i;
	switch (k) { // optimized for speed
		case -2: 
			if (c==0.0) for (i=from-1; ++i<=to; ) { v = elements[i]; sum += 1/(v*v); }
			else for (i=from-1; ++i<=to; ) { v = elements[i]-c; sum += 1/(v*v); }
			break;
		case -1:
			if (c==0.0) for (i=from-1; ++i<=to; ) sum += 1/(elements[i]);
			else for (i=from-1; ++i<=to; ) sum += 1/(elements[i]-c);
			break;
		case 0: 
			sum += to-from+1;
			break;
		case 1: 
			if (c==0.0) for (i=from-1; ++i<=to; ) sum += elements[i];
			else for (i=from-1; ++i<=to; ) sum += elements[i]-c;
			break;
		case 2: 
			if (c==0.0) for (i=from-1; ++i<=to; ) { v = elements[i]; sum += v*v; }
			else for (i=from-1; ++i<=to; ) { v = elements[i]-c; sum += v*v; }
			break;
		case 3: 
			if (c==0.0) for (i=from-1; ++i<=to; ) { v = elements[i]; sum += v*v*v; }
			else for (i=from-1; ++i<=to; ) { v = elements[i]-c; sum += v*v*v; }
			break;
		case 4: 
			if (c==0.0) for (i=from-1; ++i<=to; ) { v = elements[i]; sum += v*v*v*v; }
			else for (i=from-1; ++i<=to; ) { v = elements[i]-c; sum += v*v*v*v; }
			break;
		case 5: 
			if (c==0.0) for (i=from-1; ++i<=to; ) { v = elements[i]; sum += v*v*v*v*v; }
			else for (i=from-1; ++i<=to; ) { v = elements[i]-c; sum += v*v*v*v*v; }
			break;
		default:
			for (i=from-1; ++i<=to; ) sum += Math.pow(elements[i]-c, k);
			break;
	}
	return sum;
}
 
Example 19
Source File: DoubleQuantileEstimator.java    From jAudioGIT with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Adds the part of the specified list between indexes <tt>from</tt> (inclusive) and <tt>to</tt> (inclusive) to the receiver.
 *
 * @param values the list of which elements shall be added.
 * @param from the index of the first element to be added (inclusive).
 * @param to the index of the last element to be added (inclusive).
 */
public void addAllOfFromTo(DoubleArrayList values, int from, int to) {
	/*
	// the obvious version, but we can do quicker...
	double[] theValues = values.elements();
	int theSize=values.size();
	for (int i=0; i<theSize; ) add(theValues[i++]);
	*/
	
	double[] valuesToAdd = values.elements();
	int k = this.bufferSet.k();
	int bufferSize = k;
	double[] bufferValues = null;
	if (currentBufferToFill != null) {
		bufferValues = currentBufferToFill.values.elements();
		bufferSize = currentBufferToFill.size();
	}

	for (int i=from-1; ++i <= to; ) {
		if (sampleNextElement()) {
			if (bufferSize == k) { // full
				if (bufferSet._getFirstEmptyBuffer()==null) collapse();
				newBuffer();
				if (!currentBufferToFill.isAllocated) currentBufferToFill.allocate();
				currentBufferToFill.isSorted = false;
				bufferValues = currentBufferToFill.values.elements();
				bufferSize = 0;
			}

			bufferValues[bufferSize++] = valuesToAdd[i];
			if (bufferSize == k) { // full
				currentBufferToFill.values.setSize(bufferSize);
				currentBufferToFill = null;
			}
		}
	}
	if (this.currentBufferToFill != null) {
		this.currentBufferToFill.values.setSize(bufferSize);
	}
	
	this.totalElementsFilled += to-from+1;
}
 
Example 20
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.
}