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

The following examples show how to use net.sourceforge.openforecast.DataPoint#getDependentValue() . 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: AbstractTimeBasedModel.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns the observed value of the dependent variable for the given
 * value of the independent time variable.
 * @param timeValue the value of the independent time variable for which
 * the observed value is required.
 * @return the observed value of the dependent variable for the given
 * value of the independent time variable.
 * @throws IllegalArgumentException if the given value of the time
 * variable was not found in the observations originally passed to init.
 */
protected double getObservedValue( double timeValue )
    throws IllegalArgumentException
{
    // Find required forecast value in set of
    //  pre-computed forecasts
    Iterator<DataPoint> it = observedValues.iterator();
    while ( it.hasNext() )
        {
            DataPoint dp = it.next();
            double currentTime
                = dp.getIndependentValue( timeVariable );
            
            // If required data point found,
            //  return pre-computed forecast
            if ( Math.abs(currentTime-timeValue) < TOLERANCE )
                return dp.getDependentValue();
        }
    
    throw new
        IllegalArgumentException("No observation found for time value, "
                                 +timeVariable+"="+timeValue);
}
 
Example 2
Source File: TimeSeriesOutputter.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Outputs the given DataPoint to the current TimeSeries.
 * @param dataPoint the DataPoint to output to the current TimeSeries.
 */
private void output( DataPoint dataPoint, String timeVariable )
    throws InstantiationException, IllegalAccessException,
    InvocationTargetException, InstantiationException
{
    long timeValue = (long)dataPoint.getIndependentValue(timeVariable);
    
    Object[] args = new Object[1];
    args[0] = new Date( timeValue );
    RegularTimePeriod period
        = (RegularTimePeriod)timePeriodConstructor.newInstance(args);
    
    double value = dataPoint.getDependentValue();
    
    timeSeries.add( new TimeSeriesDataItem(period,value) );
}
 
Example 3
Source File: DataSetTest.java    From OpenForecast with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Tests the correct initialization of a DataSet.
 */
public void testDataSet()
{
    DataSet data = new DataSet( dataSet1 );
    
    // Verify data set contains the correct number of entries
    assertTrue( data.size() == dataSet1.size() );
    
    // Vefify that only one independent variable name is reported
    String[] independentVariables = data.getIndependentVariables();
    assertTrue( independentVariables.length == 1 );
    assertTrue( independentVariables[0].equals("x") );
    
    // Verify the dependent values stored
    Iterator<DataPoint> it = data.iterator();
    while ( it.hasNext() )
        {
            DataPoint dp = it.next();
            double value = dp.getDependentValue();
            double TOLERANCE = 0.001;
            assertTrue( value>-TOLERANCE && value<SIZE+TOLERANCE );
        }
}
 
Example 4
Source File: OpenForecaster.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
private double getLastValue(DataSet forecasted) {
    Iterator<DataPoint> itr = forecasted.iterator();
    while (itr.hasNext()) {
        DataPoint dp = itr.next();
        if (! itr.hasNext()) {
            return dp.getDependentValue();
        }
    }
    return 0;
}
 
Example 5
Source File: AbstractTimeBasedModel.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the forecast value for the dependent variable for the given
 * value of the independent time variable. This method is only intended
 * for use by models that base future forecasts, in part, on past
 * forecasts.
 * @param timeValue the value of the independent time variable for which
 * the forecast value is required. This value must be greater than the
 * minimum time value defined by the observations passed into the init
 * method.
 * @return the forecast value of the dependent variable for the given
 * value of the independent time variable.
 * @throws IllegalArgumentException if the given value of the time
 * variable was not a valid value for forecasts.
 */
protected double getForecastValue( double timeValue )
    throws IllegalArgumentException
{
    if ( timeValue>=minTimeValue-TOLERANCE
         && timeValue<=maxTimeValue+TOLERANCE )
        {
            // Find required forecast value in set of
            //  pre-computed forecasts
            Iterator<DataPoint> it = forecastValues.iterator();
            while ( it.hasNext() )
                {
                    DataPoint dp = it.next();
                    double currentTime
                        = dp.getIndependentValue( timeVariable );
                    
                    // If required data point found,
                    //  return pre-computed forecast
                    if ( Math.abs(currentTime-timeValue) < TOLERANCE )
                        return dp.getDependentValue();
                }
        }
    
    try
        {
            return initForecastValue( timeValue );
        }
    catch ( IllegalArgumentException idex )
        {
            throw new IllegalArgumentException(
                                               "Time value (" + timeValue
                                               + ") invalid for Time Based forecasting model. Valid values are in the range "
                                               + minTimeValue + "-" + maxTimeValue
                                               + " in increments of " + timeDiff + "." );
        }
}
 
Example 6
Source File: RegressionModel.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Initializes the coefficients to use for this regression model. The
 * intercept and slope are derived so as to give the best fit line for the
 * given data set.
 *
 * <p>Additionally, the accuracy indicators are calculated based on this
 * data set.
 * @param dataSet the set of observations to use to derive the regression
 * coefficients for this model.
 */
public void init( DataSet dataSet )
{
    int n = dataSet.size();
    double sumX = 0.0;
    double sumY = 0.0;
    double sumXX = 0.0;
    double sumXY = 0.0;
    
    Iterator<DataPoint> it = dataSet.iterator();
    while ( it.hasNext() )
        {
            DataPoint dp = it.next();
            
            double x = dp.getIndependentValue( independentVariable );
            double y = dp.getDependentValue();
            
            sumX += x;
            sumY += y;
            sumXX += x*x;
            sumXY += x*y;
        }
    
    double xMean = sumX / n;
    double yMean = sumY / n;
    
    slope = (n*sumXY - sumX*sumY) / (n*sumXX - sumX*sumX);
    intercept = yMean - slope*xMean;
    
    // Calculate the accuracy of this model
    calculateAccuracyIndicators( dataSet );
}
 
Example 7
Source File: OpenForecastTestCase.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * A helper function that validates the actual results obtaining for
 * a DataSet match the expected results. This is the same as the other
 * checkResults method except that with this method, the caller can
 * specify an acceptance tolerance when comparing actual with expected
 * results.
 * @param actualResults the DataSet returned from the forecast method
 *        that contains the data points for which forecasts were done.
 * @param expectedResults an array of expected values for the forecast
 *        data points. The order should match the order of the results
 *        as defined by the DataSet iterator.
 * @param tolerance the tolerance to accept when comparing the actual
 *        results (obtained from a forecasting model) with the expected
 *        results.
 */
protected void checkResults( DataSet actualResults,
                             double[] expectedResults,
                             double tolerance )
{
    // This is just to safeguard against a bug in the test case!  :-)
    assertNotNull( "Checking expected results is not null",
                   expectedResults );
    assertTrue( "Checking there are some expected results",
                expectedResults.length > 0 );
    
    assertEquals( "Checking the correct number of results returned",
                  expectedResults.length, actualResults.size() );
    
    // Iterate through the results, checking each one in turn
    Iterator<DataPoint> it = actualResults.iterator();
    int i=0;
    while ( it.hasNext() )
        {
            // Check that the results are within specified tolerance
            //  of the expected values
            DataPoint fc = (DataPoint)it.next();
            double fcValue = fc.getDependentValue();
            
            assertEquals( "Checking result",
                          expectedResults[i], fcValue, tolerance );
            i++;
        }
}
 
Example 8
Source File: TripleExponentialSmoothingModel.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Since this version of triple exponential smoothing uses the current
 * observation to calculate a smoothed value, we must override the
 * calculation of the accuracy indicators.
 * @param dataSet the DataSet to use to evaluate this model, and to
 *        calculate the accuracy indicators against.
 */
protected void calculateAccuracyIndicators( DataSet dataSet )
{
    // WARNING: THIS STILL NEEDS TO BE VALIDATED

    // Note that the model has been initialized
    initialized = true;

    // Reset various helper summations
    double sumErr = 0.0;
    double sumAbsErr = 0.0;
    double sumAbsPercentErr = 0.0;
    double sumErrSquared = 0.0;

    String timeVariable = getTimeVariable();
    double timeDiff = getTimeInterval();

    // Calculate the Sum of the Absolute Errors
    Iterator<DataPoint> it = dataSet.iterator();
    while ( it.hasNext() )
        {
            // Get next data point
            DataPoint dp = it.next();
            double x = dp.getDependentValue();
            double time = dp.getIndependentValue( timeVariable );
            double previousTime = time - timeDiff;

            // Get next forecast value, using one-period-ahead forecast
            double forecastValue
                = getForecastValue( previousTime )
                + getTrend( previousTime );

            // Calculate error in forecast, and update sums appropriately
            double error = forecastValue - x;
            sumErr += error;
            sumAbsErr += Math.abs( error );
            sumAbsPercentErr += Math.abs( error / x );
            sumErrSquared += error*error;
        }

    // Initialize the accuracy indicators
    int n = dataSet.size();

    accuracyIndicators.setBias( sumErr / n );
    accuracyIndicators.setMAD( sumAbsErr / n );
    accuracyIndicators.setMAPE( sumAbsPercentErr / n );
    accuracyIndicators.setMSE( sumErrSquared / n );
    accuracyIndicators.setSAE( sumAbsErr );
}
 
Example 9
Source File: DoubleExponentialSmoothingModel.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Since this version of double exponential smoothing uses the current
 * observation to calculate a smoothed value, we must override the
 * calculation of the accuracy indicators.
 * @param dataSet the DataSet to use to evaluate this model, and to
 *        calculate the accuracy indicators against.
 */
protected void calculateAccuracyIndicators( DataSet dataSet )
{
    // Note that the model has been initialized
    initialized = true;
    
    // Reset various helper summations
    double sumErr = 0.0;
    double sumAbsErr = 0.0;
    double sumAbsPercentErr = 0.0;
    double sumErrSquared = 0.0;
    
    String timeVariable = getTimeVariable();
    double timeDiff = getTimeInterval();
    
    // Calculate the Sum of the Absolute Errors
    Iterator<DataPoint> it = dataSet.iterator();
    while ( it.hasNext() )
        {
            // Get next data point
            DataPoint dp = it.next();
            double x = dp.getDependentValue();
            double time = dp.getIndependentValue( timeVariable );
            double previousTime = time - timeDiff;
            
            // Get next forecast value, using one-period-ahead forecast
            double forecastValue
                = getForecastValue( previousTime )
                + getSlope( previousTime );
            
            // Calculate error in forecast, and update sums appropriately
            double error = forecastValue - x;
            sumErr += error;
            sumAbsErr += Math.abs( error );
            sumAbsPercentErr += Math.abs( error / x );
            sumErrSquared += error*error;
        }
    
    // Initialize the accuracy indicators
    int n = dataSet.size();
    
    accuracyIndicators.setBias( sumErr / n );
    accuracyIndicators.setMAD( sumAbsErr / n );
    accuracyIndicators.setMAPE( sumAbsPercentErr / n );
    accuracyIndicators.setMSE( sumErrSquared / n );
    accuracyIndicators.setSAE( sumAbsErr );
}
 
Example 10
Source File: MultipleLinearRegressionModel.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Initializes the coefficients to use for this regression model. The
 * coefficients are derived so as to give the best fit hyperplane for the
 * given data set.
 *
 * <p>Additionally, the accuracy indicators are calculated based on this
 * data set.
 * @param dataSet the set of observations to use to derive the regression
 *        coefficients for this model.
 */
public void init( DataSet dataSet )
{
    String varNames[] = dataSet.getIndependentVariables();
    
    // If no coefficients have been defined for this model,
    //  use all that exist in this data set
    if ( coefficient == null )
        setIndependentVariables( varNames );
    
    int n = varNames.length;
    double a[][] = new double[n+1][n+2];
    
    // Iterate through dataSet
    Iterator<DataPoint> it = dataSet.iterator();
    while ( it.hasNext() )
        {
            // Get next data point
            DataPoint dp = it.next();
            
            // For each row in the matrix, a
            for ( int row=0; row<n+1; row++ )
                {
                    double rowMult = 1.0;
                    if ( row != 0 )
                        {
                            // Get value of independent variable, row
                            String rowVarName = varNames[row-1];
                            rowMult = dp.getIndependentValue(rowVarName);
                        }
                    
                    // For each column in the matrix, a
                    for ( int col=0; col<n+2; col++ )
                        {
                            double colMult = 1.0;
                            if ( col == n+1 )
                                {
                                    // Special case, for last column
                                    //  use value of dependent variable
                                    colMult = dp.getDependentValue();
                                }
                            else if ( col > 0 )
                                {
                                    // Get value of independent variable, col
                                    String colVarName = varNames[col-1];
                                    colMult = dp.getIndependentValue(colVarName);
                                }
                            
                            a[row][col] += rowMult * colMult;
                        }
                }
        }
    
    // Solve equations to derive coefficients
    double coeff[] = Utils.GaussElimination( a );
    
    // Assign coefficients to independent variables
    intercept = coeff[0];
    for ( int i=1; i<n+1; i++ )
        coefficient.put( varNames[i-1], new Double(coeff[i]) );
    
    // Calculate the accuracy indicators
    calculateAccuracyIndicators( dataSet );
}
 
Example 11
Source File: AbstractForecastingModel.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * A helper method to calculate the various accuracy indicators when
 * applying the given DataSet to the current forecasting model.
 * @param dataSet the DataSet to use to evaluate this model, and to
 *        calculate the accuracy indicators against.
 */
protected void calculateAccuracyIndicators( DataSet dataSet )
{
    // Note that the model has been initialized
    initialized = true;
    
    // Reset various helper summations
    double sumErr = 0.0;
    double sumAbsErr = 0.0;
    double sumAbsPercentErr = 0.0;
    double sumErrSquared = 0.0;
    
    // Obtain the forecast values for this model
    DataSet forecastValues = new DataSet( dataSet );
    forecast( forecastValues );
    
    // Calculate the Sum of the Absolute Errors
    Iterator<DataPoint> it = dataSet.iterator();
    Iterator<DataPoint> itForecast = forecastValues.iterator();
    while ( it.hasNext() )
        {
            // Get next data point
            DataPoint dp = it.next();
            double x = dp.getDependentValue();
            
            // Get next forecast value
            DataPoint dpForecast = itForecast.next();
            double forecastValue = dpForecast.getDependentValue();
            
            // Calculate error in forecast, and update sums appropriately
            double error = forecastValue - x;
            sumErr += error;
            sumAbsErr += Math.abs( error );
            sumAbsPercentErr += Math.abs( error / x );
            sumErrSquared += error*error;
        }
    
    // Initialize the accuracy indicators
    int n = dataSet.size();
    int p = getNumberOfPredictors();

    accuracyIndicators.setAIC( n*Math.log(2*Math.PI)
               + Math.log(sumErrSquared/n)
               + 2 * ( p+2 ) );
    accuracyIndicators.setBias( sumErr / n );
    accuracyIndicators.setMAD( sumAbsErr / n );
    accuracyIndicators.setMAPE( sumAbsPercentErr / n );
    accuracyIndicators.setMSE( sumErrSquared / n );
    accuracyIndicators.setSAE( sumAbsErr );
}