📄 abstracttimebasedmodel.java
字号:
/** * 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; } /** * 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 ); } /** * Returns the forecast value of the dependent variable for the given * value of the independent time variable. Subclasses must implement * this method in such a manner consistent with the forecasting model * they implement. Subclasses can make use of the getForecastValue and * getObservedValue methods to obtain "earlier" forecasts and * observations respectively. * @param timeValue the value of the time variable for which a forecast * value is required. * @return the forecast value of the dependent variable for the given * time. * @throws IllegalArgumentException if there is insufficient historical * data - observations passed to init - to generate a forecast for the * given time value. */ protected abstract double forecast( double timeValue ) throws IllegalArgumentException; /** * 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 it = forecastValues.iterator(); while ( it.hasNext() ) { DataPoint dp = (DataPoint)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 + "." ); } } /** * 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 it = observedValues.iterator(); while ( it.hasNext() ) { DataPoint dp = (DataPoint)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); } /** * Returns the name of the independent variable representing the time * value used by this model. * @return the name of the independent variable representing the time * value. */ public String getTimeVariable() { return timeVariable; } /** * Returns the minimum value of the independent time variable currently * forecast by this model. * @return the minimum value of the independent time variable. */ public double getMinimumTimeValue() { return minTimeValue; } /** * Returns the maximum value of the independent time variable currently * forecast by this model. * @return the maximum value of the independent time variable. */ public double getMaximumTimeValue() { return maxTimeValue; } /** * Returns the independent variable - or the time variable - used in this * model. * @return the independent variable in this model. */ public String getIndependentVariable() { return timeVariable; } /** * Returns the current time interval between observations. * @return the current time interval between observations. */ protected double getTimeInterval() { return timeDiff; } /** * Returns a one or two word name of this type of forecasting model. Keep * this short. A longer description should be implemented in the toString * method. * @return a string representation of the type of forecasting model * implemented. */ public String getForecastType() { return "Time Based Model"; } /** * This should be overridden to provide a textual description of the * current forecasting model including, where possible, any derived * parameters used. * @return a string representation of the current forecast model, and its * parameters. */ public String toString() { return "time based model, spanning " + getNumberOfPeriods() + " periods and using a time variable of " + timeVariable+"."; }}// Local Variables:// tab-width: 4// End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -