Java Code Examples for gnu.trove.list.TDoubleList#size()

The following examples show how to use gnu.trove.list.TDoubleList#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: MovingAverage.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Internal method which does actual calculation
 * 
 * @param calc              Re-used calculation object
 * @param slidingWindow     a list of previous values to use in the computation that
 *                          will be modified and returned
 * @param total             total the sum of the values in the  slidingWindow to be used in the
 *                          calculation of the moving average
 * @param newVal            newVal a new number to compute the new windowed average
 * @param windowSize        windowSize how many values to use in the moving window
 * @return
 */
private static Calculation compute(
    Calculation calc, TDoubleList slidingWindow, double total, double newVal, int windowSize) {
    
    if(slidingWindow == null) {
        throw new IllegalArgumentException("slidingWindow cannot be null.");
    }
    
    if(slidingWindow.size() == windowSize) {
        total -= slidingWindow.removeAt(0);
    }
    slidingWindow.add(newVal);
    total += newVal;
    
    if(calc == null) {
        return new Calculation(slidingWindow, total / (double)slidingWindow.size(), total);
    }
    
    return copyInto(calc, slidingWindow, total / (double)slidingWindow.size(), total);
}
 
Example 2
Source File: Encoder.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
public TDoubleList closenessScores(TDoubleList expValues, TDoubleList actValues, boolean fractional) {
	TDoubleList retVal = new TDoubleArrayList();

	//Fallback closenss is a percentage match
	List<EncoderTuple> encoders = getEncoders(this);
	if(encoders == null || encoders.size() < 1) {
		double err = Math.abs(expValues.get(0) - actValues.get(0));
		double closeness = -1;
		if(fractional) {
			double denom = Math.max(expValues.get(0), actValues.get(0));
			if(denom == 0) {
				denom = 1.0;
			}

			closeness = 1.0 - err/denom;
			if(closeness < 0) {
				closeness = 0;
			}
		}else{
			closeness = err;
		}

		retVal.add(closeness);
		return retVal;
	}

	int scalarIdx = 0;
	for(EncoderTuple res : getEncoders(this)) {
		TDoubleList values = res.getEncoder().closenessScores(
			expValues.subList(scalarIdx, expValues.size()), actValues.subList(scalarIdx, actValues.size()), fractional);

		scalarIdx += values.size();
		retVal.addAll(values);
	}

	return retVal;
}
 
Example 3
Source File: MovingAverage.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a new {@code MovingAverage}
 * 
 * @param historicalValues  list of entry values
 * @param windowSize        length over which to take the average
 */
public MovingAverage(TDoubleList historicalValues, double total, int windowSize) {
    if(windowSize <= 0) {
        throw new IllegalArgumentException("Window size must be > 0");
    }
    this.windowSize = windowSize;
    
    calc = new Calculation();
    calc.historicalValues = 
        historicalValues == null || historicalValues.size() < 1 ?
            new TDoubleArrayList(windowSize) : historicalValues;
    calc.total = total != -1 ? total : calc.historicalValues.sum();
}
 
Example 4
Source File: AnomalyLikelihood.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Given a series of anomaly scores, compute the likelihood for each score. This
 * function should be called once on a bunch of historical anomaly scores for an
 * initial estimate of the distribution. It should be called again every so often
 * (say every 50 records) to update the estimate.
 * 
 * @param anomalyScores
 * @param averagingWindow
 * @param skipRecords
 * @return
 */
public AnomalyLikelihoodMetrics estimateAnomalyLikelihoods(List<Sample> anomalyScores, int averagingWindow, int skipRecords) {
    if(anomalyScores.size() == 0) {
        throw new IllegalArgumentException("Must have at least one anomaly score.");
    }
    
    // Compute averaged anomaly scores
    AveragedAnomalyRecordList records = anomalyScoreMovingAverage(anomalyScores, averagingWindow);
    
    // Estimate the distribution of anomaly scores based on aggregated records
    Statistic distribution;
    if(records.averagedRecords.size() <= skipRecords) {
        distribution = nullDistribution();
    }else{
        TDoubleList samples = records.getMetrics();
        final int numRecordsToCopy = samples.size() - skipRecords;
        distribution = estimateNormal(samples.toArray(skipRecords, numRecordsToCopy), true);
        
        /*  Taken from the Python Documentation
           
         # HACK ALERT! The CLA model currently does not handle constant metric values
         # very well (time of day encoder changes sometimes lead to unstable SDR's
         # even though the metric is constant). Until this is resolved, we explicitly
         # detect and handle completely flat metric values by reporting them as not
         # anomalous.
         
         */
        samples = records.getSamples();
        Statistic metricDistribution = estimateNormal(samples.toArray(skipRecords, numRecordsToCopy), false);
        
        if(metricDistribution.variance < 1.5e-5) {
            distribution = nullDistribution();
        }
    }
    
    // Estimate likelihoods based on this distribution
    int i = 0;
    double[] likelihoods = new double[records.averagedRecords.size()];
    for(Sample sample : records.averagedRecords) {
        likelihoods[i++] = normalProbability(sample.score, distribution);
    }
    
    // Filter likelihood values
    double[] filteredLikelihoods = filterLikelihoods(likelihoods);
    
    int len = likelihoods.length;
    AnomalyParams params = new AnomalyParams(
        new String[] { "distribution", "movingAverage", "historicalLikelihoods" },
            distribution, 
            new MovingAverage(records.historicalValues, records.total, averagingWindow), 
            len > 0 ? 
                Arrays.copyOfRange(likelihoods, len - Math.min(averagingWindow, len), len) : 
                    new double[0]); 
    
    if(LOG.isDebugEnabled()) {
        LOG.debug(
            "Discovered params={} Number of likelihoods:{}  First 20 likelihoods:{}", 
                params, len, Arrays.copyOfRange(filteredLikelihoods, 0, 20));
    }
    
    return new AnomalyLikelihoodMetrics(filteredLikelihoods, records, params); 
}