net.sourceforge.openforecast.Observation Java Examples

The following examples show how to use net.sourceforge.openforecast.Observation. 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: ExponentialSmoothingChartDemo.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * A helper function to convert data points (from startIndex to
 * endIndex) of a (JFreeChart) TimeSeries object into an
 * OpenForecast DataSet.
 * @param series the series of data points stored as a JFreeChart
 * TimeSeries object.
 * @param startIndex the index of the first data point required from the
 * series.
 * @param endIndex the index of the last data point required from the
 * series.
 * @return an OpenForecast DataSet representing the data points extracted
 * from the TimeSeries.
 */
private DataSet getDataSet( TimeSeries series,
                            int startIndex, int endIndex )
{
    DataSet dataSet = new DataSet();
    if ( endIndex > series.getItemCount() )
        endIndex = series.getItemCount();
    
    for ( int i=startIndex; i<endIndex; i++ )
        {
            TimeSeriesDataItem dataPair = series.getDataItem(i);
            DataPoint dp = new Observation( dataPair.getValue().doubleValue() );
            dp.setIndependentValue( "t", i );
            dataSet.add( dp );
        }
    
    return dataSet;
}
 
Example #2
Source File: ForecastingChartDemo.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * A helper function to convert data points (from startIndex to
 * endIndex) of a (JFreeChart) TimeSeries object into an
 * OpenForecast DataSet.
 * @param series the series of data points stored as a JFreeChart
 * TimeSeries object.
 * @param startIndex the index of the first data point required from the
 * series.
 * @param endIndex the index of the last data point required from the
 * series.
 * @return an OpenForecast DataSet representing the data points extracted
 * from the TimeSeries.
 */
private DataSet getDataSet( TimeSeries series,
                            int startIndex, int endIndex )
{
    DataSet dataSet = new DataSet();
    if ( endIndex > series.getItemCount() )
        endIndex = series.getItemCount();
    
    for ( int i=startIndex; i<endIndex; i++ )
        {
            TimeSeriesDataItem dataPair = series.getDataItem(i);
            DataPoint dp = new Observation( dataPair.getValue().doubleValue() );
            dp.setIndependentValue( "t", i );
            dataSet.add( dp );
        }
    
    return dataSet;
}
 
Example #3
Source File: AbstractTimeBasedModel.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * A helper method that calculates a complete set of forecast values
 * derived from the given DataSet. These are calculated in advance due
 * to the way in which forecast values are so dependent on the original
 * data set. The resulting forecast values are stored in the private
 * DataSet forecastValues. Additionally, this method initializes the
 * private member variables minTimeValue and maxTimeValue.
 * @param dataSet the set of data points on which to base the forecast.
 * @return a data set containing all "possible" forecast DataPoints
 * that can reasonably be supported by this forecasting model. "Possible"
 * forecast DataPoints generally are those which are even partially
 * based on an observed value, since forecasting purely off of forecast
 * values tends to be unreliable at best.
 */
private double initForecastValue( double timeValue )
    throws IllegalArgumentException
{
    // Temporary store for current forecast value
    double forecast = forecast(timeValue);
    
    // Create new forecast data point
    DataPoint dpForecast = new Observation( forecast );
    dpForecast.setIndependentValue( timeVariable, timeValue );
    
    // Add new data point to forecast set
    forecastValues.add( dpForecast );
    
    // Update maximum time value, if necessary
    if ( timeValue > maxTimeValue )
        maxTimeValue = timeValue;
    
    return forecast;
}
 
Example #4
Source File: ResultSetBuilder.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Builds a DataPoint from the given row in the ResultSet. Assumes the
 * ResultSet has been positioned on the required row. This method does not
 * change the ResultSet, or the cursor within the ResultSet. The row is
 * expected/assumed to be made up of numeric fields only.
 * @param rs the ResultSet from which a row is to be read and used to
 * construct a new DataPoint.
 * @return a DataPoint object with values as specified by the current row
 * in the given ResultSet.
 * @throws SQLException if a database access error occurs.
 */
private DataPoint build( ResultSet rs )
     throws SQLException
{
     Observation dataPoint = new Observation( 0.0 );
     
     int n = getNumberOfVariables();
     for ( int column=0; column<n; column++ )
           {
                // Treat all columns as numeric data
                double value = rs.getDouble( column );
                
                // If this is the last value on the line, treat it
                //  as the dependent variable value
                if ( column == n )
                     dataPoint.setDependentValue( value );
                else
                     dataPoint.setIndependentValue( getVariableName(column),
                                                               value );
           }
     
     return dataPoint;
}
 
Example #5
Source File: BasicTest.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Tests that an IllegalArgumentException is thrown if an attempt is made
 * to retrieve the value of an unknown independent variable.
 */
public void testUnknownVariableInObservation()
{
    Observation observation = new Observation( 5.0 );
    try
        {
            // Independent variable should not exist
            double value = observation.getIndependentValue( "y" );
            
            fail("Attempt to obtain the value of an unknown variable from an Observation returned a value ("+value+") - it should have thrown an exception");
        }
    catch ( IllegalArgumentException e )
        {
            // Expected result!
        }
}
 
Example #6
Source File: DelimitedTextOutputterTest.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Creates a dummy data setto be written by all test cases.
 */
public void setUp()
{
    // Constants used to determine size of test
    int MAX_X1 = 10;
    int MAX_X2 = 10;
    
    // Set up array for expected results
    expectedDataSet = new DataSet();
    
    // Create test DataSet
    int numberOfDataPoints = 0;
    for ( int x1=0; x1<MAX_X1; x1++ )
        for ( int x2=0; x2<MAX_X2; x2++ )
            {
                double expectedValue = x1+2*x2+3.14;
                DataPoint dp = new Observation( expectedValue );
                dp.setIndependentValue( "x1", x1 );
                dp.setIndependentValue( "x2", x2 );
                expectedDataSet.add( dp );
                numberOfDataPoints++;
            }
    
    assertEquals("Checking correct number of data points created",
                 numberOfDataPoints, expectedDataSet.size());
}
 
Example #7
Source File: OpenForecaster.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void add(double value) {
    checkSize();
    DataPoint dp = new Observation(value);
    dp.setIndependentValue("timestamp", System.currentTimeMillis());
    _series.add(dp);
}
 
Example #8
Source File: OpenForecaster.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
private DataSet getForecastTransport() {
    DataSet transport = new DataSet();
    long now = System.currentTimeMillis();
    for (int i=0; i< Config.getForecastLookahead(); i++) {
        DataPoint dp = new Observation(0.0);
        dp.setIndependentValue("timestamp", now);
        transport.add(dp);
        now += Config.getPollInterval();
    }
    return transport;
}
 
Example #9
Source File: TimeSeriesBuilder.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Builds a DataPoint from the given TimeSeriesDataItem. The name used for
 * the independent, time variable can be changed using the setTimeVariable
 * method.
 * @param dataItem the TimeSeriesDataItem from which the values are to be
 * read and used to construct a new DataPoint.
 * @return a DataPoint object with values as specified by the given
 * TimeSeriesDataItem.
 */
private DataPoint build( TimeSeriesDataItem dataItem )
{
    DataPoint dataPoint
        = new Observation( dataItem.getValue().doubleValue() );
    
    // Get time value (at middle of time period)
    double timeValue = dataItem.getPeriod().getMiddleMillisecond();
    
    // Store time value as independent variable
    dataPoint.setIndependentValue( getVariableName(0),
                                   timeValue );
    
    return dataPoint;
}
 
Example #10
Source File: BasicTest.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Tests that two DataPoint objects initialized differently but with the
 * same data are equal.
 */
public void testDataPoint()
{
    DataPoint dp1 = new Observation( 1.0 );
    dp1.setIndependentValue( "x", 2.0 );
    dp1.setIndependentValue( "t", 3.0 );
    
    DataPoint dp2 = new Observation( 1.0 );
    dp2.setIndependentValue( "t", 3.0 );
    dp2.setIndependentValue( "x", 2.0 );
    
    assertTrue( dp1.equals( dp2 ) );
}
 
Example #11
Source File: DataSetTest.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates four simple DataSet for use by the tests. The first three
 * DataSets are created to contain the same data (though different
 * DataPoint objects), whereas the fourth DataSet is the same size but
 * contains different data as the others.
 */
public void setUp()
{
    dataSet1 = new DataSet();
    dataSet2 = new DataSet();
    dataSet3 = new DataSet();
    dataSet4 = new DataSet(); // Different data set
    
    for ( int count=0; count<SIZE; count++ )
        {
            DataPoint dp1 = new Observation( (double)count );
            DataPoint dp2 = new Observation( (double)count );
            DataPoint dp3 = new Observation( (double)count );
            DataPoint dp4 = new Observation( (double)count );
            
            dp1.setIndependentValue( "x", count );
            dp2.setIndependentValue( "x", count );
            dp3.setIndependentValue( "x", count );
            dp4.setIndependentValue( "x", count+1 );
            
            dataSet1.add( dp1 );
            dataSet2.add( dp2 );
            dataSet3.add( dp3 );
            dataSet4.add( dp4 );
        }
    
    // Verify data set contains the correct number of entries
    assertTrue("Checking dataSet1 contains correct number of data points",
               dataSet1.size() == SIZE );
    assertTrue("Checking dataSet2 contains correct number of data points",
               dataSet2.size() == SIZE );
    assertTrue("Checking dataSet3 contains correct number of data points",
               dataSet3.size() == SIZE );
    assertTrue("Checking dataSet4 contains correct number of data points",
               dataSet4.size() == SIZE );
}
 
Example #12
Source File: CSVBuilder.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Builds a DataPoint from the given CSV line. This method should only be
 * used to parse a line that is expected to be made up of numeric data
 * only. Use {@link #readHeaderRow} to read a header row if one is expected.
 * @param line the input line of comma separated values to parse and use
 * to construct a new DataPoint.
 * @return a DataPoint object with values as specified by the given input
 * String.
 */
private DataPoint build( String line )
    throws IOException
{
    Observation dataPoint = new Observation( 0.0 );
    
    StreamTokenizer tokenizer
        = new StreamTokenizer( new StringReader( line ) );
    
    tokenizer.commentChar( '#' );
    tokenizer.eolIsSignificant( true );
    tokenizer.parseNumbers();
    
    int i = 0;
    int n = getNumberOfVariables();
    int lastToken = SEPARATOR;
    do
        {
            // Read next token
            tokenizer.nextToken();
            switch ( tokenizer.ttype )
                {
                case '\t':
                case ' ':
                    // Skip whitespace
                    continue;
                    
                case SEPARATOR:
                    // Check for two adjacent commas
                    if ( lastToken != SEPARATOR )
                        break;
                    
                    // Two adjacent commas. Assume 0.0 between them
                    tokenizer.nval = 0.0;
                    
                    // Fall through, and handle as a number
                    
                case StreamTokenizer.TT_NUMBER:
                    // Handle numbers appropriately as data
                    
                    // If this is the last value on the line, treat it
                    //  as the dependent variable value
                    if ( i == n )
                        dataPoint.setDependentValue(tokenizer.nval);
                    else
                        dataPoint.setIndependentValue(getVariableName(i),
                                                      tokenizer.nval);
                    
                    i++;
                    break;
                    
                case StreamTokenizer.TT_WORD:
                    throw new IOException( "Invalid input in CSV file. Number expected, found '"+tokenizer.sval+"'");
                    
                case StreamTokenizer.TT_EOL:
                case StreamTokenizer.TT_EOF:
                    break;
                    
                default:
                }
            
            lastToken = tokenizer.ttype;
        }
    while ( tokenizer.ttype != StreamTokenizer.TT_EOF );
    
    return dataPoint;
}
 
Example #13
Source File: MultipleLinearRegressionTest.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Tests the use of user-defined coefficients with the multiple
 * variable linear regression model.
 */
public void testUserDefinedCoefficientsWithNamedVars()
{
    // Reset the observedData, to ensure that it is *not* used
    observedData.clear();
    observedData = null;
    
    // Initialize coefficients
    final int NUMBER_OF_COEFFS = 5;
    double intercept = 0.12345;
    Hashtable<String,Double> coeffs = new Hashtable<String,Double>();
    String varNames[] = new String[NUMBER_OF_COEFFS];
    for ( int c=0; c<NUMBER_OF_COEFFS; c++ )
        {
            varNames[c] = new String( "param"+(c+1) );
            coeffs.put( varNames[c], new Double( Math.pow(10,c) ) );
        }

    // Create a data set for forecasting
    DataSet fcValues = new DataSet();

    for ( int count=0; count<10; count++ )
        {
            DataPoint dp = new Observation( 0.0 );
            dp.setIndependentValue( "param1", count+4 );
            dp.setIndependentValue( "param2", count+3 );
            dp.setIndependentValue( "param3", count+2 );
            dp.setIndependentValue( "param4", count+1 );
            dp.setIndependentValue( "param5", count   );
            fcValues.add( dp );
        }

    // Get forecast values
    MultipleLinearRegressionModel model
        = new MultipleLinearRegressionModel( varNames );

    model.init( intercept, coeffs );
    DataSet results = model.forecast( fcValues );
    assertTrue( fcValues.size() == results.size() );

    // These are the expected results
    double expectedResult[] = {   1234.12345,
                                 12345.12345,
                                 23456.12345,
                                 34567.12345,
                                 45678.12345,
                                 56789.12345,
                                 67900.12345,
                                 79011.12345,
                                 90122.12345,
                                101233.12345 };

    // Check results against expected results
    checkResults( results, expectedResult );
}
 
Example #14
Source File: MultipleLinearRegressionTest.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Tests the use of user-defined coefficients with the multiple
 * variable linear regression model.
 */
public void testUserDefinedCoefficients()
{
    // Reset the observedData, to ensure that it is *not* used
    observedData.clear();
    observedData = null;
    
    // Initialize coefficients
    final int NUMBER_OF_COEFFS = 5;
    double intercept = 0.12345;
    Hashtable<String,Double> coeffs = new Hashtable<String,Double>();
    String varNames[] = new String[NUMBER_OF_COEFFS];
    for ( int c=0; c<NUMBER_OF_COEFFS; c++ )
        {
            varNames[c] = new String( "param"+(c+1) );
            coeffs.put( varNames[c], new Double( Math.pow(10,c) ) );
        }

    // Create a data set for forecasting
    DataSet fcValues = new DataSet();

    for ( int count=0; count<10; count++ )
        {
            DataPoint dp = new Observation( 0.0 );
            dp.setIndependentValue( "param1", count+4 );
            dp.setIndependentValue( "param2", count+3 );
            dp.setIndependentValue( "param3", count+2 );
            dp.setIndependentValue( "param4", count+1 );
            dp.setIndependentValue( "param5", count   );
            fcValues.add( dp );
        }

    // Get forecast values
    MultipleLinearRegressionModel model
        = new MultipleLinearRegressionModel();

    model.init( intercept, coeffs );
    DataSet results = model.forecast( fcValues );
    assertTrue( fcValues.size() == results.size() );

    // These are the expected results
    double expectedResult[] = {   1234.12345,
                                 12345.12345,
                                 23456.12345,
                                 34567.12345,
                                 45678.12345,
                                 56789.12345,
                                 67900.12345,
                                 79011.12345,
                                 90122.12345,
                                101233.12345 };

    // Check results against expected results
    checkResults( results, expectedResult );
}