Java Code Examples for net.sourceforge.openforecast.DataSet#add()

The following examples show how to use net.sourceforge.openforecast.DataSet#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: 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: ResultSetBuilder.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Retrieves a DataSet - a collection of DataPoints - from the current
 * input source. The DataSet should contain all DataPoints defined by
 * the input source.
 *
 * <p>In general, build will attempt to convert all rows in the ResultSet
 * to data points. In this implementation, all columns are assumed to
 * contain numeric data. This restriction may be relaxed at a later date.
 * @return a DataSet built from the current input source.
 * @throws SQLException if a database access error occurs.
 */
public DataSet build()
     throws SQLException
{
     DataSet dataSet = new DataSet();
     
     setColumnNames();
     
     // Make sure we're on the first record
     if ( !rs.isBeforeFirst() )
           rs.beforeFirst();
     
     // Iterate through ResultSet,
     //  creating new DataPoint instance for each row
     while ( rs.next() )
           {
                DataPoint dp = build( rs );
                dataSet.add( dp );
           }
     
     return dataSet;
}
 
Example 4
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 5
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 6
Source File: TimeSeriesBuilder.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Retrieves a DataSet - a collection of DataPoints - from the current
 * (JFreeChart) TimeSeries. The DataSet should contain all DataPoints
 * defined by the TimeSeries.
 *
 * <p>In general, build will attempt to convert all values in the
 * TimeSeries to data points.
 * @return a DataSet built from the current TimeSeries.
 */
public DataSet build()
{
    DataSet dataSet = new DataSet();
    
    dataSet.setTimeVariable( getTimeVariable() );
    
    // Iterate through TimeSeries,
    //  creating new DataPoint instance for each row
    int numberOfPeriods = timeSeries.getItemCount();
    for ( int t=0; t<numberOfPeriods; t++ )
        dataSet.add( build(timeSeries.getDataItem(t)) );
    
    return dataSet;
}
 
Example 7
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 8
Source File: CSVBuilder.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Retrieves a DataSet - a collection of DataPoints - from the current
 * input source. The DataSet should contain all DataPoints defined by
 * the input source.
 *
 * <p>In general, build will attempt to convert all lines/rows in the CSV
 * input to data points. The exceptions are as follows:
 * <ul>
 *  <li>Blank lines (lines containing only whitespace) will be ignored,
 *      and can be used for spacing in the input.</li>
 *  <li>Lines beginning with a '#' will be treated as comments, and will
 *      be ignored.</li>
 *  <li>If a header row is included - as specified in one of the
 *      constructors - then it will be treated as containing field/variable
 *      names for use by the DataSet.</li>
 * </ul>
 * @return a DataSet built from the current input source.
 * @throws IOException if an error occurred reading from the CSV file.
 */
public DataSet build()
    throws IOException
{
    DataSet dataSet = new DataSet();
    
    boolean firstLineRead = false;
    
    BufferedReader reader = new BufferedReader( fileReader );
    String line;
    do
        {
            // Get next line (trimmed)
            line = reader.readLine();
            if ( line == null )
                continue;
            
            line = line.trim();
            
            // Skip blank lines
            if ( line.length() == 0 )
                continue;
            
            // Skip comment lines
            if ( line.startsWith( "#" ) )
                continue;
            
            if ( !firstLineRead )
                {
                    firstLineRead = true;
                    if ( hasHeaderRow != HAS_HEADER_ROW_FALSE )
                        {
                            try
                                {
                                    // Treat first line as header
                                    readHeaderRow( line );
                                    continue;
                                }
                            catch ( NoHeaderException nhex )
                                {
                                    // No header row found, so treat it
                                    //  as the first row of data
                                }
                        }
                    
                    // Calculate how many independent values per line
                    // TODO: Fix this to handle quoted commas
                    int n = 0;
                    for ( int pos=0;
                          (pos=line.indexOf(SEPARATOR,pos)) > 0;
                          pos++ )
                        n++;
                    setNumberOfVariables( n );
                }
            
            DataPoint dp = build( line );
            dataSet.add( dp );
        }
    while ( line != null );   // line == null when EOF is reached
    
    return dataSet;
}
 
Example 9
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 10
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 );
}