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

net.sourceforge.openforecast.Observation Maven / Gradle / Ivy

The newest version!
//
//  OpenForecast - open source, general-purpose forecasting package.
//  Copyright (C) 2002-2011  Steven R. Gould
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

package net.sourceforge.openforecast;


import java.util.Enumeration;
import java.util.Hashtable;


/**
 * Represents a single observation or data point, consisting of one value
 * of a dependent variable, and one or more values of independent variables.
 * Note that an Observation can refer to a previously observed data value,
 * or a future forecast value (an expected Observation).
 * @author Steven R. Gould
 * @since 0.3
 */
public class Observation implements DataPoint
{
    /**
     * Stores the dependent value for this observation.
     */
    private double dependentValue;
    
    /**
     * A collection of name-value pairs representing the independent variables
     * associated with the current dependentValue. In each name-value pair, the
     * name is a string representation of the name of the independent variable,
     * and the value is its value corresponding to the current value of the
     * dependent variable.
     */
    private Hashtable independentValues;
    
    /**
     * Initializes the current Observation with the given value of the
     * dependent variable. Note that this is provided as somewhat of a
     * convenience - it will be necessary to call setIndependentValue for
     * each independent variable name and value associated with this data
     * point
     * @param dependentValue the initial value of the dependent variable.
     */
    public Observation( double dependentValue )
    {
        this.dependentValue = dependentValue;
        independentValues = new Hashtable();
    }
    
    /**
     * Like a copy constructor, but constructs a new Observation object by
     * making a copy of the values from the given data point.
     * @param dataPoint the data point that is to be copied.
     */
    public Observation( DataPoint dataPoint )
    {
        this.dependentValue = dataPoint.getDependentValue();
        
        String[] varNames = dataPoint.getIndependentVariableNames();
        int numberOfVars = varNames.length;
        this.independentValues = new Hashtable( numberOfVars );
        for ( int dp=0; dp keys = independentValues.keys();
        for ( int index=0; keys.hasMoreElements(); index++ )
            varNames[index] = keys.nextElement();
        
        return varNames;
    }
    
    /**
     * Compares the given DataPoint to the current DataPoint/Observation,
     * and returns true if, and only if, the two data points represent the
     * same data point. That is, the dependent value matches and all the
     * independent values match.
     * @param dp the DataPoint to compare this DataPoint/Observation object
     *        to.
     * @return true if the given DataPoint represents the same data point
     *         as this DataPoint/Observation object.
     */
    public boolean equals( DataPoint dp )
    {
        if ( dependentValue != dp.getDependentValue() )
            return false;
        
        // Get a list of independent variable names
        String[] vars = getIndependentVariableNames();
        
        // Check that the given DataPoint has the same number of variables
        String[] dpVars = getIndependentVariableNames();
        if ( vars.length != dpVars.length )
            return false;
        
        // Check the values of each variable match
        for ( int v=0; v e = independentValues.keys();
        while ( e.hasMoreElements() )
            {
                Object obj = e.nextElement();
                String key = obj.toString();
                String value = independentValues.get(obj).toString();
                
                result += key + "=" + value + ",";
            }
        
        return result + "dependentValue=" + dependentValue + ")";
    }
}
/*
 * Local Variables:
 * tab-width: 4
 * End:
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy