Java Code Examples for net.sourceforge.openforecast.DataPoint#getIndependentValue()
The following examples show how to use
net.sourceforge.openforecast.DataPoint#getIndependentValue() .
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 |
/** * 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: MultipleLinearRegressionModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * Using the current model parameters (initialized in init), apply the * forecast model to the given data point. The data point must have valid * values for the independent variables. Upon return, the value of the * dependent variable will be updated with the forecast value computed for * that data point. * @param dataPoint the data point for which a forecast value (for the * dependent variable) is required. * @return the forecast value of the dependent variable for the given data * point. * @throws ModelNotInitializedException if forecast is called before the * model has been initialized with a call to init. */ public double forecast( DataPoint dataPoint ) { if ( !initialized ) throw new ModelNotInitializedException(); double forecastValue = intercept; Iterator< Map.Entry<String,Double> > it = coefficient.entrySet().iterator(); while ( it.hasNext() ) { Map.Entry<String,Double> entry = it.next(); // Get value of independent variable double x = dataPoint.getIndependentValue( (String)entry.getKey() ); // Get coefficient for this variable double coeff = ((Double)entry.getValue()).doubleValue(); forecastValue += coeff * x; } dataPoint.setDependentValue( forecastValue ); return forecastValue; }
Example 3
Source File: TimeSeriesOutputter.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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 4
Source File: ExponentialSmoothingChartDemo.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * Use the given forecasting model to produce a TimeSeries object * representing the periods startIndex through endIndex, and containing * the forecast values produced by the model. * @param model the forecasting model to use to generate the forecast * series. * @param initDataSet data set to use to initialize the forecasting model. * @param startIndex the index of the first data point to use from the * set of potential forecast values. * @param endIndex the index of the last data point to use from the set * of potential forecast values. * @param title a title to give to the TimeSeries created. */ private TimeSeries getForecastTimeSeries( ForecastingModel model, DataSet initDataSet, int startIndex, int endIndex, String title ) { // Initialize the forecasting model model.init( initDataSet ); // Get range of data required for forecast DataSet fcDataSet = getDataSet( fc, startIndex, endIndex ); // Obtain forecast values for the forecast data set model.forecast( fcDataSet ); // Create a new TimeSeries TimeSeries series = new TimeSeries(title,fc.getTimePeriodClass()); // Iterator through the forecast results, adding to the series Iterator it = fcDataSet.iterator(); while ( it.hasNext() ) { DataPoint dp = (DataPoint)it.next(); int index = (int)dp.getIndependentValue("t"); series.add( fc.getTimePeriod(index), dp.getDependentValue() ); } return series; }
Example 5
Source File: ForecastingChartDemo.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * Use the given forecasting model to produce a TimeSeries object * representing the periods startIndex through endIndex, and containing * the forecast values produced by the model. * @param model the forecasting model to use to generate the forecast * series. * @param initDataSet data set to use to initialize the forecasting model. * @param startIndex the index of the first data point to use from the * set of potential forecast values. * @param endIndex the index of the last data point to use from the set * of potential forecast values. * @param title a title to give to the TimeSeries created. */ private TimeSeries getForecastTimeSeries( ForecastingModel model, DataSet initDataSet, int startIndex, int endIndex, String title ) { // Initialize the forecasting model model.init( initDataSet ); // Get range of data required for forecast DataSet fcDataSet = getDataSet( fc, startIndex, endIndex ); // Obtain forecast values for the forecast data set model.forecast( fcDataSet ); // Create a new TimeSeries TimeSeries series = new TimeSeries(title,fc.getTimePeriodClass()); // Iterator through the forecast results, adding to the series Iterator it = fcDataSet.iterator(); while ( it.hasNext() ) { DataPoint dp = (DataPoint)it.next(); int index = (int)dp.getIndependentValue("t"); series.add( fc.getTimePeriod(index), dp.getDependentValue() ); } return series; }
Example 6
Source File: TripleExponentialSmoothingModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * 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 7
Source File: AbstractTimeBasedModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * 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 8
Source File: PolynomialRegressionModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * Using the current model parameters (initialized in init), apply the * forecast model to the given data point. The data point must have valid * values for the independent variables. Upon return, the value of the * dependent variable will be updated with the forecast value computed for * that data point. * @param dataPoint the data point for which a forecast value (for the * dependent variable) is required. * @return the same data point passed in but with the dependent value * updated to contain the new forecast value. * @throws ModelNotInitializedException if forecast is called before the * model has been initialized with a call to init. */ public double forecast( DataPoint dataPoint ) { if ( !initialized ) throw new ModelNotInitializedException(); double x = dataPoint.getIndependentValue( independentVariable ); double forecastValue = 0.0; for ( int i=0; i<order; i++ ) forecastValue += coefficient[i] * Math.pow(x,i); dataPoint.setDependentValue( forecastValue ); return forecastValue; }
Example 9
Source File: RegressionModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * 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 10
Source File: RegressionModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * Using the current model parameters (initialized in init), apply the * forecast model to the given data point. The data point must have valid * values for the independent variables. Upon return, the value of the * dependent variable will be updated with the forecast value computed for * that data point. * @param dataPoint the data point for which a forecast value (for the * dependent variable) is required. * @return the same data point passed in but with the dependent value * updated to contain the new forecast value. * @throws ModelNotInitializedException if forecast is called before the * model has been initialized with a call to init. */ public double forecast( DataPoint dataPoint ) throws ModelNotInitializedException { if ( !initialized ) throw new ModelNotInitializedException(); double x = dataPoint.getIndependentValue( independentVariable ); double forecastValue = intercept + slope*x; dataPoint.setDependentValue( forecastValue ); return forecastValue; }
Example 11
Source File: TripleExponentialSmoothingModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 4 votes |
/** * 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 12
Source File: DoubleExponentialSmoothingModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 4 votes |
/** * 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 13
Source File: AbstractTimeBasedModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 4 votes |
/** * 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 14
Source File: MultipleLinearRegressionModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 4 votes |
/** * 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 15
Source File: CSVBuilderTest.java From OpenForecast with GNU Lesser General Public License v2.1 | 4 votes |
/** * Tests the correct initialization of a DataSet from a CSV file where * the input is valid, yet poorly and irregularly formatted. For example, * the CSVBuilder is supposed to treat as a zero field two commas following * each other. This test will also test naming the columns and the use of * blank lines and comments in the input. */ public void testExtremeCSVBuilder() throws FileNotFoundException, IOException { // Constants used to determine size of test double expectedValue[] = { 4,5,6,7,8 }; int numberOfDataPoints = expectedValue.length; // Create test CSV file File testFile = File.createTempFile( "test", ".csv" ); PrintStream out = new PrintStream( new FileOutputStream(testFile) ); out.println("# This is a test CSV file with various 'peculiarities'"); out.println(" # thrown in to try and trip it up"); out.println("Field1, Field2, \"Field, 3\", Observation"); out.println("-1, -2 ,-3,4"); out.println(",,,5"); out.println(" 1 , 2 , 3 , 6 "); out.println(" 2, 4, 6, 7"); out.println("3 ,6 ,9 ,8"); out.close(); // Create CSV builder and use it to create the DataSet CSVBuilder builder = new CSVBuilder( testFile, true ); DataSet dataSet = builder.build(); // Verify data set contains the correct number of entries assertEquals( "DataSet created is of the wrong size", numberOfDataPoints, dataSet.size() ); // Vefify that only three independent variable names are reported String[] independentVariables = dataSet.getIndependentVariables(); assertEquals( "Checking the correct number of independent variables", 3, independentVariables.length ); // Note these will have been sorted into alphabetical order assertTrue( "Checking variable 0 name is as expected", independentVariables[0].compareTo("Field, 3")==0 ); assertTrue( "Checking variable 1 name is as expected", independentVariables[1].compareTo("Field1")==0 ); assertTrue( "Checking variable 2 name is as expected", independentVariables[2].compareTo("Field2")==0 ); // Test the data set created by the builder Iterator<DataPoint> it = dataSet.iterator(); while ( it.hasNext() ) { DataPoint dataPoint = it.next(); double field1 = dataPoint.getIndependentValue("Field1"); double field2 = dataPoint.getIndependentValue("Field2"); double field3 = dataPoint.getIndependentValue("Field, 3"); // field2 was set to twice field1 // field3 was set to three times field1 assertTrue( "Checking independent values are correct", field2==2*field1 && field3==3*field1 ); // The data was set up with this simple equation double expectedResult = 5.0 + field1; assertEquals("Checking data point "+dataPoint, expectedResult, dataPoint.getDependentValue(), TOLERANCE); } // Clean up - remove test file testFile.delete(); }
Example 16
Source File: AbstractTimeBasedModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 3 votes |
/** * Using the current model parameters (initialized in init), apply the * forecast model to the given data point. The data point must have a valid * value for the independent variable. Upon return, the value of the * dependent variable will be updated with the forecast value computed for * that data point. * @param dataPoint the data point for which a forecast value (for the * dependent variable) is required. * @return the same data point passed in but with the dependent value * updated to contain the new forecast value. * @throws ModelNotInitializedException if forecast is called before the * model has been initialized with a call to init. * @throws IllegalArgumentException if the forecast period specified by * the dataPoint is invalid with respect to the historical data * provided. */ public double forecast( DataPoint dataPoint ) throws IllegalArgumentException { if ( !initialized ) throw new ModelNotInitializedException(); // Get value of independent variable (the time variable) double t = dataPoint.getIndependentValue( timeVariable ); return getForecastValue( t ); }