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

The following examples show how to use gnu.trove.list.TDoubleList#add() . 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: 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 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: 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 4
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 5
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 6
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 7
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 8
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 9
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 10
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();
}