gnu.trove.list.TDoubleList Java Examples

The following examples show how to use gnu.trove.list.TDoubleList. 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: AnomalyLikelihood.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Given a list of anomaly scores return a list of averaged records.
 * anomalyScores is assumed to be a list of records of the form:
 * <pre>
 *      Sample:
 *           dt = Tuple(2013, 8, 10, 23, 0) --> Date Fields
 *           sample = (double) 6.0
 *           metric(avg) = (double) 1.0
 * </pre>
 *           
 * @param anomalyScores     List of {@link Sample} objects (described contents above)
 * @param windowSize        Count of historical items over which to compute the average
 * 
 * @return Each record in the returned list contains [datetime field, value, averaged score]
 */
public AveragedAnomalyRecordList anomalyScoreMovingAverage(List<Sample> anomalyScores, int windowSize) {
    TDoubleList historicalValues = new TDoubleArrayList();
    double total = 0.0;
    List<Sample> averagedRecordList = new ArrayList<Sample>();
    for(Sample record : anomalyScores) {
        ////////////////////////////////////////////////////////////////////////////////////////////
        // Python version has check for malformed records here, but can't happen in java version. //
        ////////////////////////////////////////////////////////////////////////////////////////////
        
        Calculation calc = MovingAverage.compute(historicalValues, total, record.score, windowSize);
        
        Sample avgRecord = new Sample(
            record.date,
            record.value,
            calc.getAverage());
        averagedRecordList.add(avgRecord);
        total = calc.getTotal();
        
        if(LOG.isDebugEnabled()) {
            LOG.debug("Aggregating input record: {}, Result: {}", record, averagedRecordList.get(averagedRecordList.size() - 1));
        }
    }
    
    return new AveragedAnomalyRecordList(averagedRecordList, historicalValues, total);
}
 
Example #2
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 #3
Source File: ScalarEncoderTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Test closenessScores for a periodic encoder
 */
@Test
public void testCloseness() {
	setUp();
	builder.name("day of week")
        .w(7)
        .radius(1.0)
        .minVal(0.0)
        .maxVal(7.0)
        .periodic(true)
        .forced(true);
	initSE();
	
	TDoubleList expValues = new TDoubleArrayList(new double[] { 2, 4, 7 });
	TDoubleList actValues = new TDoubleArrayList(new double[] { 4, 2, 1 });
	
	TDoubleList scores = se.closenessScores(expValues, actValues, false);
	for(Tuple t : ArrayUtils.zip(Arrays.asList(2, 2, 1), Arrays.asList(scores.get(0)))) {
		double a = (int)t.get(0);
		double b = (double)t.get(1);
		assertTrue(a == b);
	}
}
 
Example #4
Source File: DateEncoder.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
// Adapted from MultiEncoder
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void encodeIntoArray(DateTime inputData, int[] output) {

    if(inputData == null) {
        throw new IllegalArgumentException("DateEncoder requires a valid Date object but got null");
    }

    // Get the scalar values for each sub-field
    TDoubleList scalars = getScalars(inputData);

    int fieldCounter = 0;
    for (EncoderTuple t : getEncoders(this)) {
        Encoder encoder = t.getEncoder();
        int offset = t.getOffset();

        int[] tempArray = new int[encoder.getWidth()];
        encoder.encodeIntoArray(scalars.get(fieldCounter), tempArray);

        System.arraycopy(tempArray, 0, output, offset, tempArray.length);

        ++fieldCounter;
    }
}
 
Example #5
Source File: DateEncoder.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns an array containing the sub-field bucket indices for
 * each sub-field of the inputData. To get the associated field names for each of
 * the buckets, call getScalarNames().
 * @param  	input 	The data from the source. This is typically a object with members.
 *
 * @return 	array of bucket indices
 */
public int[] getBucketIndices(DateTime input) {

    TDoubleList scalars = getScalars(input);

    TIntList l = new TIntArrayList();
    List<EncoderTuple> encoders = getEncoders(this);
    if(encoders != null && encoders.size() > 0) {
        int i = 0;
        for(EncoderTuple t : encoders) {
            l.addAll(t.getEncoder().getBucketIndices(scalars.get(i)));
            ++i;
        }
    }else{
        throw new IllegalStateException("Should be implemented in base classes that are not " +
                "containers for other encoders");
    }
    return l.toArray();
}
 
Example #6
Source File: PassThroughEncoderTest.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
@Ignore
private void testCloseInner(int[] bitmap1, int[] bitmap2, double expectedScore){
	PassThroughEncoder<int[]> encoder = new PassThroughEncoder<>(9, ArrayUtils.where(bitmap1, ArrayUtils.WHERE_1).length);
	encoder.setName("foo");
	
	int[] out1 = encoder.encode(bitmap1);
	encoder.setW(ArrayUtils.where(bitmap2, ArrayUtils.WHERE_1).length);
	int[] out2 = encoder.encode(bitmap2);

	TDoubleList result = encoder.closenessScores(new TDoubleArrayList(ArrayUtils.toDoubleArray(out1)), new TDoubleArrayList(ArrayUtils.toDoubleArray(out2)), true);
	assertTrue(result.size() == 1 );
	assertEquals(expectedScore, result.get(0), 0.0);
	
	encoder = PassThroughEncoder.builder()
			.n(9)
			.w(ArrayUtils.where(bitmap1, ArrayUtils.WHERE_1).length)
			.name("foo")
			.build();
	out1 = encoder.encode(bitmap1);
	encoder.setW(ArrayUtils.where(bitmap2, ArrayUtils.WHERE_1).length);
	out2 = encoder.encode(bitmap2);
	result = encoder.closenessScores(new TDoubleArrayList(ArrayUtils.toDoubleArray(out1)), new TDoubleArrayList(ArrayUtils.toDoubleArray(out2)), true);
	assertTrue(result.size() == 1 );
	assertEquals(expectedScore, result.get(0), 0.0);
}
 
Example #7
Source File: SDRCategoryEncoder.java    From htm.java with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <S> TDoubleList getScalars(S input) {
    String inputCasted = (String)input;
    int index = 0;
    TDoubleList result = new TDoubleArrayList();
    if (inputCasted == null || inputCasted.isEmpty()) {
        result.add(0);
        return result;
    }
    if (!sdrByCategory.containsKey(input)) {
        if (isEncoderLearningEnabled()) {
            index = sdrByCategory.size();
            addCategory(inputCasted);
        }
    } else {
        index = sdrByCategory.getIndexByCategory(inputCasted);
    }
    result.add(index);
    return result;
}
 
Example #8
Source File: CategoryEncoder.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TDoubleList closenessScores(TDoubleList expValues, TDoubleList actValues, boolean fractional) {
	double expValue = expValues.get(0);
	double actValue = actValues.get(0);

	double closeness = expValue == actValue ? 1.0 : 0;
	if(!fractional) closeness = 1.0 - closeness;

	return new TDoubleArrayList(new double[]{ closeness });
}
 
Example #9
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 #10
Source File: Anomaly.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns a list of the sample values in the contained averaged record list.
 * @return
 */
public TDoubleList getSamples() {
    TDoubleList retVal = new TDoubleArrayList();
    for(Sample s : averagedRecords) {
        retVal.add(s.value);
    }
    
    return retVal;
}
 
Example #11
Source File: Anomaly.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Returns a list of the averages in the contained averaged record list.
 * @return
 */
public TDoubleList getMetrics() {
    TDoubleList retVal = new TDoubleArrayList();
    for(Sample s : averagedRecords) {
        retVal.add(s.score);
    }
    
    return retVal;
}
 
Example #12
Source File: SparsePassThroughEncoderTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Ignore
private void testCloseInner(int[] bitmap1, int outputWidth1, int[] bitmap2, int outputWidth2, double expectedScore) {
    SparsePassThroughEncoder encoder1 = new SparsePassThroughEncoder(outputWidth1, ArrayUtils.where(bitmap1, ArrayUtils.GREATER_OR_EQUAL_0).length);
    SparsePassThroughEncoder encoder2 = new SparsePassThroughEncoder(outputWidth2, ArrayUtils.where(bitmap2, ArrayUtils.GREATER_OR_EQUAL_0).length);

    int[] out1 = encoder1.encode(bitmap1);
    int[] out2 = encoder2.encode(bitmap2);

    TDoubleList result = encoder1.closenessScores(new TDoubleArrayList(ArrayUtils.toDoubleArray(out1)), new TDoubleArrayList(ArrayUtils.toDoubleArray(out2)), true);
    assertTrue(result.size() == 1);
    assertEquals(expectedScore, result.get(0), 0.0);

    encoder1 = SparsePassThroughEncoder.sparseBuilder()
            .n(outputWidth1)
            .w(ArrayUtils.where(bitmap1, ArrayUtils.GREATER_OR_EQUAL_0).length)
            .build();
    encoder2 = SparsePassThroughEncoder.sparseBuilder()
            .n(outputWidth2)
            .w(ArrayUtils.where(bitmap2, ArrayUtils.GREATER_OR_EQUAL_0).length)
            .build();

    out1 = encoder1.encode(bitmap1);
    out2 = encoder2.encode(bitmap2);

    result = encoder1.closenessScores(new TDoubleArrayList(ArrayUtils.toDoubleArray(out1)), new TDoubleArrayList(ArrayUtils.toDoubleArray(out2)), true);
    assertTrue(result.size() == 1);
    assertEquals(expectedScore, result.get(0), 0.0);
}
 
Example #13
Source File: ScalarEncoderTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void testGetScalars() {
    setUp();
       initSE();
       
    TDoubleList scalars = se.getScalars(42.42d);
    assertEquals(42.42d, scalars.get(0), 0.01);
}
 
Example #14
Source File: LogEncoder.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TDoubleList closenessScores(TDoubleList expValues, TDoubleList actValues, boolean fractional) {
	TDoubleList retVal = new TDoubleArrayList();

	double expValue, actValue;
	if (expValues.get(0) > 0) {
		expValue = Math.log10(expValues.get(0));
	} else {
		expValue = minScaledValue;
	}
	if (actValues.get(0) > 0) {
		actValue = Math.log10(actValues.get(0));
	} else {
		actValue = minScaledValue;
	}

	double closeness;
	if (fractional) {
		double err = Math.abs(expValue - actValue);
		double pctErr = err / (maxScaledValue - minScaledValue);
		pctErr = Math.min(1.0,  pctErr);
		closeness = 1.0 - pctErr;
	} else {
		closeness = Math.abs(expValue - actValue);;
	}

	retVal.add(closeness);
	return retVal;
}
 
Example #15
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 #16
Source File: ScalarEncoder.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param <S>	the input value, in this case a double
 * @return	a list of one input double
 */
@Override
public <S> TDoubleList getScalars(S d) {
    TDoubleList retVal = new TDoubleArrayList();
    retVal.add((Double)d);
    return retVal;
}
 
Example #17
Source File: AnomalyLikelihoodMetricsTest.java    From htm.java with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void testCopy() {
    double[] likelihoods = new double[] { 0.2, 0.3 };

    Sample s = new Sample(new DateTime(), 0.1, 0.1);
    List<Sample> samples = new ArrayList<>();
    samples.add(s);
    TDoubleList d = new TDoubleArrayList();
    d.add(0.5);
    double total = 0.4;
    AveragedAnomalyRecordList avges = (
            new Anomaly() {
                @Override
                public double compute(int[] activeColumns, int[] predictedColumns, double inputValue, long timestamp) {
                    return 0;
                }
            }
    ).new AveragedAnomalyRecordList(samples, d, total);

    Statistic stat = new Statistic(0.1, 0.1, 0.1);
    MovingAverage ma = new MovingAverage(new TDoubleArrayList(), 1);
    AnomalyParams params = new AnomalyParams(new String[] { Anomaly.KEY_DIST, Anomaly.KEY_MVG_AVG, Anomaly.KEY_HIST_LIKE}, stat, ma, likelihoods);

    // Test equality
    AnomalyLikelihoodMetrics metrics = new AnomalyLikelihoodMetrics(likelihoods, avges, params);
    AnomalyLikelihoodMetrics metrics2 = metrics.copy();
    assertEquals(metrics, metrics2);
}
 
Example #18
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); 
}
 
Example #19
Source File: MovingAverage.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
public Calculation(TDoubleList historicalValues, double currentValue, double total) {
    this.average = currentValue;
    this.historicalValues = historicalValues;
    this.total = total;
}
 
Example #20
Source File: AnomalyLikelihoodMetricsTest.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void testEquals() {
    double[] likelihoods = new double[] { 0.2, 0.3 };

    Sample s = new Sample(new DateTime(), 0.1, 0.1);
    List<Sample> samples = new ArrayList<>();
    samples.add(s);
    TDoubleList d = new TDoubleArrayList();
    d.add(0.5);
    double total = 0.4;
    AveragedAnomalyRecordList avges = (
            new Anomaly() {
                @Override
                public double compute(int[] activeColumns, int[] predictedColumns, double inputValue, long timestamp) {
                    return 0;
                }
            }
    ).new AveragedAnomalyRecordList(samples, d, total);

    Statistic stat = new Statistic(0.1, 0.1, 0.1);
    MovingAverage ma = new MovingAverage(new TDoubleArrayList(), 1);
    AnomalyParams params = new AnomalyParams(new String[] { Anomaly.KEY_DIST, Anomaly.KEY_MVG_AVG, Anomaly.KEY_HIST_LIKE}, stat, ma, likelihoods);

    // Test equality
    AnomalyLikelihoodMetrics metrics = new AnomalyLikelihoodMetrics(likelihoods, avges, params);
    AnomalyLikelihoodMetrics metrics2 = metrics.copy();
    assertEquals(metrics, metrics2);

    assertTrue(metrics.equals(metrics));
    assertFalse(metrics.equals(null));
    assertFalse(metrics.equals(s));

    AnomalyLikelihoodMetrics metricsNoRecs = new AnomalyLikelihoodMetrics(likelihoods, null, params);
    assertFalse(metricsNoRecs.equals(metrics));

    double[] likelihoods2 = new double[] { 0.1, 0.2 };
    AnomalyLikelihoodMetrics metricsDiffLikes = new AnomalyLikelihoodMetrics(likelihoods2, avges, params);
    assertFalse(metrics.equals(metricsDiffLikes));

    AnomalyLikelihoodMetrics metricsNoLikes = new AnomalyLikelihoodMetrics(null, avges, params);
    assertFalse(metricsNoLikes.equals(metricsDiffLikes));

    AnomalyLikelihoodMetrics metricsNoParams = new AnomalyLikelihoodMetrics(likelihoods, avges, null);
    assertFalse(metricsNoParams.equals(metrics));
}
 
Example #21
Source File: AnomalyLikelihood.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Compute updated probabilities for anomalyScores using the given params.
 * 
 * @param anomalyScores     a list of records. Each record is a list with a {@link Sample} containing the
                            following three elements: [timestamp, value, score]
 * @param params            Associative {@link NamedTuple} returned by the {@link AnomalyLikelihoodMetrics} from
 *                          {@link #estimateAnomalyLikelihoods(List, int, int)}
 * @return
 */
public AnomalyLikelihoodMetrics updateAnomalyLikelihoods(List<Sample> anomalyScores, NamedTuple params) {
    int anomalySize = anomalyScores.size();
    
    if(LOG.isDebugEnabled()) {
        LOG.debug("in updateAnomalyLikelihoods");
        LOG.debug("Number of anomaly scores: {}", anomalySize);
        LOG.debug("First 20: {}", anomalyScores.subList(0, Math.min(20, anomalySize)));
        LOG.debug("Params: {}", params);
    }
    
    if(anomalyScores.size() == 0) {
        throw new IllegalArgumentException("Must have at least one anomaly score.");
    }
    
    if(!isValidEstimatorParams(params)) {
        throw new IllegalArgumentException("\"params\" is not a valid parameter structure");
    }
    
    double[] histLikelihoods;
    if(!params.hasKey("historicalLikelihoods") || 
        (histLikelihoods = (double[])params.get("historicalLikelihoods")) == null || 
            histLikelihoods.length == 0) {
        
        params = new NamedTuple(
            new String[] { "distribution", "movingAverage", "historicalLikelihoods" },
                params.get("distribution"), 
                params.get("movingAverage"),
                histLikelihoods = new double[] { 1 });    
    }
    
    // Compute moving averages of these new scores using the previous values
    // as well as likelihood for these scores using the old estimator
    MovingAverage mvgAvg = (MovingAverage)params.get("movingAverage");
    TDoubleList historicalValues = mvgAvg.getSlidingWindow();
    double total = mvgAvg.getTotal();
    int windowSize = mvgAvg.getWindowSize();
    
    List<Sample> aggRecordList = new ArrayList<>(anomalySize);
    double[] likelihoods = new double[anomalySize];
    int i = 0;
    for(Sample sample : anomalyScores) {
        Calculation calc = MovingAverage.compute(historicalValues, total, sample.score, windowSize);
        aggRecordList.add(
            new Sample(
                sample.date,
                sample.value,
                calc.getAverage()));
        total = calc.getTotal();
        likelihoods[i++] = normalProbability(calc.getAverage(), (Statistic)params.get("distribution"));
    }
    
    // Filter the likelihood values. First we prepend the historical likelihoods
    // to the current set. Then we filter the values.  We peel off the likelihoods
    // to return and the last windowSize values to store for later.
    double[] likelihoods2 = ArrayUtils.concat(histLikelihoods, likelihoods);
    double[] filteredLikelihoods = filterLikelihoods(likelihoods2);
    likelihoods = Arrays.copyOfRange(filteredLikelihoods, filteredLikelihoods.length - likelihoods.length, filteredLikelihoods.length);
    double[] historicalLikelihoods = Arrays.copyOf(likelihoods2, likelihoods2.length - Math.min(windowSize, likelihoods2.length));
    
    // Update the estimator
    AnomalyParams newParams = new AnomalyParams(
        new String[] { "distribution", "movingAverage", "historicalLikelihoods" },
            params.get("distribution"),
            new MovingAverage(historicalValues, total, windowSize),
            historicalLikelihoods);
    
    return new AnomalyLikelihoodMetrics(
        likelihoods, 
        new AveragedAnomalyRecordList(aggRecordList, historicalValues, total), 
        newParams);  
}
 
Example #22
Source File: TDoubleArrayList.java    From baleen with Apache License 2.0 4 votes vote down vote up
/** See {@link gnu.trove.list.array.TDoubleArrayList#grep(TDoubleProcedure)} */
public TDoubleList grep(TDoubleProcedure arg0) {
  return delegate.grep(arg0);
}
 
Example #23
Source File: CategoryEncoder.java    From htm.java with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public <T> TDoubleList getScalars(T d) {
	return new TDoubleArrayList(new double[] { categoryToIndex.get(d) });
}
 
Example #24
Source File: TDoubleArrayList.java    From baleen with Apache License 2.0 4 votes vote down vote up
/** See {@link gnu.trove.list.array.TDoubleArrayList#subList(int, int)} */
public TDoubleList subList(int arg0, int arg1) {
  return delegate.subList(arg0, arg1);
}
 
Example #25
Source File: TDoubleArrayList.java    From baleen with Apache License 2.0 4 votes vote down vote up
/** See {@link gnu.trove.list.array.TDoubleArrayList#inverseGrep(TDoubleProcedure)} */
public TDoubleList inverseGrep(TDoubleProcedure arg0) {
  return delegate.inverseGrep(arg0);
}
 
Example #26
Source File: MovingAverage.java    From htm.java with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Internal method to update running totals.
 * 
 * @param c
 * @param slidingWindow
 * @param value
 * @param total
 * @return
 */
private static Calculation copyInto(Calculation c, TDoubleList slidingWindow, double average, double total) {
    c.historicalValues = slidingWindow;
    c.average = average;
    c.total = total;
    return c;
}
 
Example #27
Source File: ArrayUtils.java    From htm.java with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Returns an array which starts from lowerBounds (inclusive) and
 * ends at the upperBounds (exclusive).
 *
 * @param lowerBounds the starting value
 * @param upperBounds the maximum value (exclusive)
 * @param interval    the amount by which to increment the values
 * @return
 */
public static double[] arange(double lowerBounds, double upperBounds, double interval) {
    TDoubleList doubs = new TDoubleArrayList();
    for (double i = lowerBounds; i < upperBounds; i += interval) {
        doubs.add(i);
    }
    return doubs.toArray();
}
 
Example #28
Source File: Encoder.java    From htm.java with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Returns an {@link TDoubleList} containing the sub-field scalar value(s) for
    * each sub-field of the inputData. To get the associated field names for each of
    * the scalar values, call getScalarNames().
 *
    * For a simple scalar encoder, the scalar value is simply the input unmodified.
    * For category encoders, it is the scalar representing the category string
    * that is passed in.
    *
    * TODO This is not correct for DateEncoder:
    *
    * For the datetime encoder, the scalar value is the
    * the number of seconds since epoch.
 *
    * The intent of the scalar representation of a sub-field is to provide a
    * baseline for measuring error differences. You can compare the scalar value
    * of the inputData with the scalar value returned from topDownCompute() on a
    * top-down representation to evaluate prediction accuracy, for example.
    *
    * @param <S>  the specifically typed input object
    *
 * @return
 */
public <S> TDoubleList getScalars(S d) {
	TDoubleList retVals = new TDoubleArrayList();
	double inputData = (Double)d;
	List<EncoderTuple> encoders = getEncoders(this);
	if(encoders != null) {
		for(EncoderTuple t : encoders) {
			TDoubleList values = t.getEncoder().getScalars(inputData);
			retVals.addAll(values);
		}
	}
	return retVals;
}
 
Example #29
Source File: MovingAverage.java    From htm.java with GNU Affero General Public License v3.0 2 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, int windowSize) {
    this(historicalValues, -1, windowSize);
}
 
Example #30
Source File: MovingAverage.java    From htm.java with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Routine for computing a moving average
 * 
 * @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
 */
public static Calculation compute(TDoubleList slidingWindow, double total, double newVal, int windowSize) {
    return compute(null, slidingWindow, total, newVal, windowSize);
}