Java Code Examples for net.sourceforge.openforecast.DataPoint#setIndependentValue()

The following examples show how to use net.sourceforge.openforecast.DataPoint#setIndependentValue() . 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: 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
@Override
public void add(double value) {
    checkSize();
    DataPoint dp = new Observation(value);
    dp.setIndependentValue("timestamp", System.currentTimeMillis());
    _series.add(dp);
}
 
Example 6
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 7
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 8
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 9
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 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 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 11
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 );
}