net.sourceforge.openforecast.DataPoint Java Examples
The following examples show how to use
net.sourceforge.openforecast.DataPoint.
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: DelimitedTextOutputter.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * Writes a DataSet - a collection of DataPoints - to the current writer. * The DataSet should contain all DataPoints to be output. * * <p>Depending on the setting of outputHeaderRow, a header row containing * the variable names of the data points will be output. To enable/disable * this feature, use the {@link #setOutputHeaderRow} method.</li> * @param dataSet the DataSet to be output to the current writer. * @throws IOException if an I/O error occurs. */ public void output( DataSet dataSet ) throws IOException { if ( outputHeaderRow ) writeHeader( dataSet.getIndependentVariables() ); Iterator<DataPoint> it = dataSet.iterator(); while ( it.hasNext() ) { DataPoint dataPoint = it.next(); output( dataPoint ); } out.flush(); }
Example #2
Source File: DataSetTest.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * Implements a test of the DataSet contains and containsAll methods. */ public void testContains() { assertTrue("Checking for self-containment", dataSet1.containsAll(dataSet1)); assertTrue("Checking for containment of similar data sets (1)", dataSet1.containsAll(dataSet2)); assertTrue("Checking for containment of similar data sets (2)", dataSet2.containsAll(dataSet1)); assertFalse("Checking for non-containment", dataSet1.containsAll(dataSet4)); Iterator<DataPoint> it = dataSet1.iterator(); assertTrue("Checking for valid iterator", it.hasNext()); DataPoint dp = (DataPoint)it.next(); assertTrue("Checking for individual DataPoint containment", dataSet1.contains(dp)); assertFalse("Checking for individual DataPoint non-containment", dataSet4.contains(dp)); }
Example #3
Source File: ExponentialSmoothingChartDemo.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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 #4
Source File: DataSetTest.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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 #5
Source File: ForecastingChartDemo.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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 #6
Source File: DelimitedTextOutputter.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * Outputs the given DataPoint to the current Writer. * @param dataPoint the DataPoint to output to the current Writer. * @throws IOException if an I/O error occurs. */ public void output( DataPoint dataPoint ) throws IOException { String[] varNames = dataPoint.getIndependentVariableNames(); // Output each column/variable name for ( int var=0; var<varNames.length; var++ ) out.write( ""+dataPoint.getIndependentValue(varNames[var]) +delimiter ); // Output dependent variable name out.write( ""+dataPoint.getDependentValue() ); out.newLine(); }
Example #7
Source File: DelimitedTextOutputterTest.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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 #8
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 #9
Source File: ResultSetBuilder.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * Builds a DataPoint from the given row in the ResultSet. Assumes the * ResultSet has been positioned on the required row. This method does not * change the ResultSet, or the cursor within the ResultSet. The row is * expected/assumed to be made up of numeric fields only. * @param rs the ResultSet from which a row is to be read and used to * construct a new DataPoint. * @return a DataPoint object with values as specified by the current row * in the given ResultSet. * @throws SQLException if a database access error occurs. */ private DataPoint build( ResultSet rs ) throws SQLException { Observation dataPoint = new Observation( 0.0 ); int n = getNumberOfVariables(); for ( int column=0; column<n; column++ ) { // Treat all columns as numeric data double value = rs.getDouble( column ); // If this is the last value on the line, treat it // as the dependent variable value if ( column == n ) dataPoint.setDependentValue( value ); else dataPoint.setIndependentValue( getVariableName(column), value ); } return dataPoint; }
Example #10
Source File: AbstractTimeBasedModel.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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 #11
Source File: ResultSetBuilder.java From OpenForecast with GNU Lesser General Public License v2.1 | 6 votes |
/** * 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 #12
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 #13
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 #14
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 #15
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 #16
Source File: TimeSeriesOutputter.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * Adds a DataSet - a collection of DataPoints - to the current TimeSeries. * The DataSet should contain all DataPoints to be output. * @param dataSet the DataSet to be output to the current TimeSeries. */ public void output( DataSet dataSet ) throws InstantiationException, IllegalAccessException, InvocationTargetException, InstantiationException { String timeVariable = dataSet.getTimeVariable(); Iterator<DataPoint> it = dataSet.iterator(); while ( it.hasNext() ) { DataPoint dataPoint = it.next(); output( dataPoint, timeVariable ); } }
Example #17
Source File: TimeSeriesBuilder.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * 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 #18
Source File: BasicTest.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * 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 #19
Source File: OpenForecastTestCase.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * 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 #20
Source File: DataSetTest.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * 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 #21
Source File: DataSetTest.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * Implements a test of the DataSet remove and removeAll methods. */ public void testRemove() { Iterator<DataPoint> it = dataSet1.iterator(); assertTrue("Checking for valid iterator", it.hasNext()); // Save initial size of data set int initialSize = dataSet1.size(); DataPoint dp = (DataPoint)it.next(); assertTrue("Checking dataSet1 contains data point", dataSet1.contains(dp)); // Remove a single data point dataSet1.remove( dp ); assertEquals("Checking size of data set decreased after removing a data point", initialSize-1, dataSet1.size()); try { assertTrue("Checking data set changed after removeAll", dataSet1.removeAll(dataSet1)); assertTrue("Checking size of data set after removeAll", dataSet1.isEmpty() && dataSet1.size()==0); fail("removeAll is not supposed to be supported yet!"); } catch ( UnsupportedOperationException ex ) { // Expected result - at least until removeAll is implemented } }
Example #22
Source File: DataSetTest.java From OpenForecast with GNU Lesser General Public License v2.1 | 5 votes |
/** * Implements a test of the DataSet retainAll method. */ public void testRetainAll() { assertEquals("Checking dataSet1.equals(dataSet2)", dataSet1, dataSet2); // Remove half of the DataPoints from dataSet2 for ( int i=0; i<SIZE/2; i++ ) { Iterator<DataPoint> it = dataSet2.iterator(); assertTrue("Checking for valid iterator", it.hasNext()); dataSet2.remove( (DataPoint)it.next() ); } int expectedSize = dataSet2.size(); try { // Use retainAll to remove data points from dataSet1 dataSet1.retainAll( dataSet2 ); assertEquals("Checking size of dataSet1 after retaingAll", expectedSize, dataSet1.size()); assertEquals("Checking size of dataSet1 and dataSet2 after retainAll", dataSet1.size(), dataSet2.size()); fail("retainAll is not supposed to be supported yet!"); } catch ( UnsupportedOperationException ex ) { // Expected result - at least until retainAll is implemented } }
Example #23
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 #24
Source File: OpenForecaster.java From yawl with GNU Lesser General Public License v3.0 | 5 votes |
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 #25
Source File: OpenForecaster.java From yawl with GNU Lesser General Public License v3.0 | 5 votes |
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 #26
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 #27
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 #28
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 #29
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 #30
Source File: OpenForecaster.java From yawl with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void add(double value) { checkSize(); DataPoint dp = new Observation(value); dp.setIndependentValue("timestamp", System.currentTimeMillis()); _series.add(dp); }