All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.numenta.nupic.algorithms.AnomalyLikelihoodMetrics Maven / Gradle / Ivy

There is a newer version: 0.6.13
Show newest version
/* ---------------------------------------------------------------------
 * Numenta Platform for Intelligent Computing (NuPIC)
 * Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
 * with Numenta, Inc., for a separate license for this software code, the
 * following terms and conditions apply:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 *
 * http://numenta.org/licenses/
 * ---------------------------------------------------------------------
 */

package org.numenta.nupic.algorithms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.numenta.nupic.algorithms.Anomaly.AveragedAnomalyRecordList;
import org.numenta.nupic.algorithms.AnomalyLikelihood.AnomalyParams;

/**
 * Container class to hold the results of {@link AnomalyLikelihood} estimations
 * and updates.
 * 
 * @author David Ray
 * @see AnomalyLikelihood
 * @see AnomalyLikelihoodTest
 */
public class AnomalyLikelihoodMetrics {
    private AnomalyParams params;
    private AveragedAnomalyRecordList aggRecordList;
    private double[] likelihoods;
    
    /**
     * Constructs a new {@code AnomalyLikelihoodMetrics}
     * 
     * @param likelihoods       array of pre-computed estimations
     * @param aggRecordList     List of {@link Sample}s which are basically a set of date, value, average score,
     *                          a list of historical values, and a running total.
     * @param params            {@link AnomalyParams} which are a {@link Statistic}, array of likelihoods,
     *                          and a {@link MovingAverage} 
     */
    public AnomalyLikelihoodMetrics(double[] likelihoods, AveragedAnomalyRecordList aggRecordList, AnomalyParams params) {
        this.params = params;
        this.aggRecordList = aggRecordList;
        this.likelihoods = likelihoods;
    }
    
    /**
     * Utility method to copy this {@link AnomalyLikelihoodMetrics} object.
     * @return
     */
    public AnomalyLikelihoodMetrics copy() {
        List vals = new ArrayList();
        for(String key : params.keys()) {
            vals.add(params.get(key));
        }
        
        return new AnomalyLikelihoodMetrics(
            Arrays.copyOf(likelihoods, likelihoods.length), 
            aggRecordList, 
            new AnomalyParams(params.keys(), vals.toArray()));
    }
    
    /**
     * Returns the array of computed likelihoods
     * @return
     */
    public double[] getLikelihoods() {
        return likelihoods;
    }
    
    /**
     * 
     * Returns the record list which are:
     *     List of {@link Sample}s which are basically a set of date, value, average score,
     *     a list of historical values, and a running total.
     * 
* @return */ public AveragedAnomalyRecordList getAvgRecordList() { return aggRecordList; } /** *
     * Returns the {@link AnomalyParams} which is:
     *     a {@link Statistic}, array of likelihoods,
     *     and a {@link MovingAverage}
     * 
* @return */ public AnomalyParams getParams() { return params; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((aggRecordList == null) ? 0 : aggRecordList.hashCode()); result = prime * result + Arrays.hashCode(likelihoods); result = prime * result + ((params == null) ? 0 : params.hashCode()); return result; } @Override public boolean equals(Object obj) { if(this == obj) return true; if(obj == null) return false; if(getClass() != obj.getClass()) return false; AnomalyLikelihoodMetrics other = (AnomalyLikelihoodMetrics)obj; if(aggRecordList == null) { if(other.aggRecordList != null) return false; } else if(!aggRecordList.equals(other.aggRecordList)) return false; if(!Arrays.equals(likelihoods, other.likelihoods)) return false; if(params == null) { if(other.params != null) return false; } else if(!params.equals(other.params)) return false; return true; } }