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

The following examples show how to use net.sourceforge.openforecast.DataSet#size() . 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: TripleExponentialSmoothingModel.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Factory method that returns a best fit triple exponential smoothing
 * model for the given data set. This, like the overloaded
 * {@link #getBestFitModel(DataSet)}, attempts to derive "good" -
 * hopefully near optimal - values for the alpha and beta smoothing
 * constants.
 *
 * <p>To determine which model is "best", this method currently uses only
 * the Mean Squared Error (MSE). Future versions may use other measures in
 * addition to the MSE. However, the resulting "best fit" model - and the
 * associated values of alpha and beta - is expected to be very similar
 * either way.
 *
 * <p>Note that the approach used to calculate the best smoothing
 * constants - alpha and beta - <em>may</em> end up choosing values near
 * a local optimum. In other words, there <em>may</em> be other values for
 * alpha and beta that result in an even better model.
 * @param dataSet the observations for which a "best fit" triple
 * exponential smoothing model is required.
 * @param alphaTolerance the required precision/accuracy - or tolerance
 * of error - required in the estimate of the alpha smoothing constant.
 * @param betaTolerance the required precision/accuracy - or tolerance
 * of error - required in the estimate of the beta smoothing constant.
 * @return a best fit triple exponential smoothing model for the given
 * data set.
 */
public static TripleExponentialSmoothingModel
    getBestFitModel( DataSet dataSet,
                     double alphaTolerance, double betaTolerance )
{
    // Check we have the minimum amount of data points
    if ( dataSet.size() < NUMBER_OF_YEARS*dataSet.getPeriodsPerYear() )
        throw new IllegalArgumentException("TripleExponentialSmoothing models require a minimum of a full two years of data in the data set.");

    // Check alphaTolerance is in the expected range
    if ( alphaTolerance < 0.0  || alphaTolerance > 0.5 )
        throw new IllegalArgumentException("The value of alphaTolerance must be significantly less than 1.0, and no less than 0.0. Suggested value: "+DEFAULT_SMOOTHING_CONSTANT_TOLERANCE);

    // Check betaTolerance is in the expected range
    if ( betaTolerance < 0.0  || betaTolerance > 0.5 )
        throw new IllegalArgumentException("The value of betaTolerance must be significantly less than 1.0, and no less than 0.0. Suggested value: "+DEFAULT_SMOOTHING_CONSTANT_TOLERANCE);

    TripleExponentialSmoothingModel model1
        = findBestBeta( dataSet, 0.0, 0.0, 1.0, betaTolerance );
    TripleExponentialSmoothingModel model2
        = findBestBeta( dataSet, 0.5, 0.0, 1.0, betaTolerance );
    TripleExponentialSmoothingModel model3
        = findBestBeta( dataSet, 1.0, 0.0, 1.0, betaTolerance );

    // First rough estimate of alpha and beta to the nearest 0.1
    TripleExponentialSmoothingModel bestModel
        = findBest( dataSet, model1, model2, model3,
                    alphaTolerance, betaTolerance );

    return bestModel;
}
 
Example 2
Source File: TripleExponentialSmoothingModel.java    From OpenForecast with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Used to initialize the time based model. This method must be called
 * before any other method in the class. Since the time based model does
 * not derive any equation for forecasting, this method uses the input
 * DataSet to calculate forecast values for all values of the independent
 * time variable within the initial data set.
 * @param dataSet a data set of observations that can be used to
 * initialize the forecasting parameters of the forecasting model.
 */
public void init( DataSet dataSet )
{
    initTimeVariable( dataSet );
    String timeVariable = getTimeVariable();

    if ( dataSet.getPeriodsPerYear() <= 1 )
        throw new IllegalArgumentException("Data set passed to init in the triple exponential smoothing model does not contain seasonal data. Don't forget to call setPeriodsPerYear on the data set to set this.");

    periodsPerYear = dataSet.getPeriodsPerYear();

    // Check we have the minimum amount of data points
    if ( dataSet.size() < NUMBER_OF_YEARS*periodsPerYear )
        throw new IllegalArgumentException("TripleExponentialSmoothing models require a minimum of a full two years of data to initialize the model.");

    // Calculate initial values for base and trend
    initBaseAndTrendValues( dataSet );

    // Initialize seasonal indices using data for all complete years
    initSeasonalIndices( dataSet );

    Iterator<DataPoint> it = dataSet.iterator();
    maxObservedTime = Double.NEGATIVE_INFINITY;
    while ( it.hasNext() )
        {
            DataPoint dp = it.next();
            if ( dp.getIndependentValue(timeVariable) > maxObservedTime )
                maxObservedTime = dp.getIndependentValue(timeVariable);
        }

    super.init( dataSet );
}
 
Example 3
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 4
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 5
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 6
Source File: AbstractTimeBasedModel.java    From OpenForecast with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Used to initialize the time based model. This method must be called
 * before any other method in the class. Since the time based model does
 * not derive any equation for forecasting, this method uses the input
 * DataSet to calculate forecast values for all values of the independent
 * time variable within the initial data set.
 * @param dataSet a data set of observations that can be used to initialize
 *        the forecasting parameters of the forecasting model.
 */
public void init( DataSet dataSet )
{
    initTimeVariable( dataSet );
    
    if ( dataSet == null  || dataSet.size() == 0 )
        throw new IllegalArgumentException("Data set cannot be empty in call to init.");
    
    int minPeriods = getNumberOfPeriods();
    
    if ( dataSet.size() < minPeriods )
        throw new IllegalArgumentException("Data set too small. Need "
                                           +minPeriods
                                           +" data points, but only "
                                           +dataSet.size()
                                           +" passed to init.");
    
    observedValues = new DataSet( dataSet );
    observedValues.sort( timeVariable );
    
    // Check that intervals between data points are consistent
    //  i.e. check for complete data set
    Iterator<DataPoint> it = observedValues.iterator();
    
    DataPoint dp = it.next();  // first data point
    double lastValue = dp.getIndependentValue(timeVariable);
    
    dp = it.next();  // second data point
    double currentValue = dp.getIndependentValue(timeVariable);
    
    // Create data set in which to save new forecast values
    forecastValues = new DataSet();
    
    // Determine "standard"/expected time difference between observations
    timeDiff = currentValue - lastValue;
    
    // Min. time value is first observation time
    minTimeValue = lastValue;
    
    while ( it.hasNext() )
        {
            lastValue = currentValue;
            
            // Get next data point
            dp = it.next();
            currentValue = dp.getIndependentValue(timeVariable);
            
            double diff = currentValue - lastValue;
            if ( Math.abs(timeDiff - diff) > TOLERANCE )
                throw new IllegalArgumentException( "Inconsistent intervals found in time series, using variable '"+timeVariable+"'" );
            
            try
                {
                    initForecastValue( currentValue );
                }
            catch (IllegalArgumentException ex)
                {
                    // We can ignore these during initialization
                }
        }
    
    // Create test data set for determining accuracy indicators
    //  - same as input data set, but without the first n data points
    DataSet testDataSet = new DataSet( observedValues );
    int count = 0;
    while ( count++ < minPeriods )
        testDataSet.remove( (testDataSet.iterator()).next() );
    
    // Calculate accuracy
    calculateAccuracyIndicators( testDataSet );
}
 
Example 7
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 );
}