tspy.data_structures.forecasting.ForecastingModel module¶
-
class
tspy.data_structures.forecasting.ForecastingModel.
ForecastingModel
(jvm, algorithm, j_fm=None)¶ Bases:
object
An online forecasting model which has the following components: 1. Wraps a forecasting algorithm (see
forecasters
for algorithms) 2. Updated in real-time with new scalar data, thereby modifying the underlying components of the forecasting algorithm 3. Provides forecasts based on the most recent model updatesExamples
create a forecasting model (auto-forecasting algorithm)
>>> import tspy >>> model = tspy.forecasters.auto(10) >>> model Forecasting Model Algorithm: Unitialized. Min training size=10 HWSAdditive:HWSAdditive=-1 (aLevel=0, bSlope=0, gSeas=0) uninitialized HWSMultiplicative:HWSMultiplicative=-1 (aLevel=0, bSlope=0, gSeas=0) uninitialized RegularARIMAAlgorithm:RegularARIMAAlgorithm [armaAlgorithm=RegularARMAAlgorithm [dataHistory=null, errorHistory=null, dataMean=NaN, minTrainingData=80, pMin=0, pMax=5, qMin=0, qMax=5, forceModel=false], differencer=null, minTrainingData=82, diffOrder=-1] RegularSegmentedLinear:RegularSegmentedLinear,history=-1,width=1 [ ] RegularSegmentedAveraging:RegularSegmentedAveraging,history=-1,width=1 [uninitialized ] RegularLinear:RegularLinear [linearFitter=LeastSquaresFitter [yIntercept=0.0, slope=0.0], minTrainingSamples=2] RegularLinear:RegularLinear [linearFitter=LeastSquaresFitter [yIntercept=0.0, slope=0.0], minTrainingSamples=2] BATSAlgorithm:BATSAlgorithm
update the model with values
>>> for i in range(0,10): ...model.update_model(i, float(i)) >>> model Forecasting Model Algorithm: HWSAdditive:HWSAdditive=1 (aLevel=0.001, bSlope=0.001, gSeas=0.001) level=9.99004488020975, slope=0.9999900896408388, seasonal(amp,per,avg)=(0.0,1, 5,-0.009900448802097483) HWSMultiplicative:HWSMultiplicative=1 (aLevel=0.001, bSlope=0.001, gSeas=0.001) level=9.990109117210386, slope=0.9999901537464366, seasonal(amp,per,avg)=(0.0,1, 5,0.9970972695898375) RegularARIMAAlgorithm:RegularARIMAAlgorithm [armaAlgorithm=RegularARMAAlgorithm [dataHistory=null, errorHistory=null, dataMean=NaN, minTrainingData=80, pMin=0, pMax=5, qMin=0, qMax=5, forceModel=false], differencer=null, minTrainingData=82, diffOrder=-1] RegularSegmentedLinear:RegularSegmentedLinear=1,history=-1,width=1 [(y=1.0*x + 0.0) ] RegularSegmentedAveraging:RegularSegmentedAveraging=1,history=-1,width=1 [(avg=4.5) ] RegularLinear:RegularLinear [linearFitter=LeastSquaresFitter [yIntercept=0.0, slope=1.0], minTrainingSamples=2] RegularLinear:RegularLinear [linearFitter=LeastSquaresFitter [yIntercept=0.0, slope=1.0], minTrainingSamples=2] BATSAlgorithm:BATSAlgorithm
forecast at a time in the future
>>> model.forecast_at(15) 14.993695899928097
- Attributes
average_interval
floatReturns ——- float Get the average interval across all calls to
update_model()
.error_horizon_length
intReturns ——- int Get the value of the error horizon length.
initial_time_updated
intReturns ——- int Get the first time value used to build the forecasting model.
last_time_updated
intReturns ——- int Get the most recent time value used to build the forecasting model.
Methods
bounds_at
(at, confidence)get the lower and upper bounds for the forecast at the given time and the given confidence level
error_at
(at)compute the error estimate for the forecast at the given point in time
forecast_at
(at)compute the forecast at a given time
- Returns
reset the model to an uninitialized state
save
(path)save the model to a file
update_model
(timestamp, value)update the model at a given timestamp
-
property
average_interval
¶ - Returns
- float
Get the average interval across all calls to
update_model()
. Will return -1 if 1 or fewer samples have been provided.
-
bounds_at
(at, confidence)¶ get the lower and upper bounds for the forecast at the given time and the given confidence level
- Parameters
- atint
the time in the future, for which we want to predict the bounds with the confidence value that corresponds to the forecast at that time
- confidencefloat
a number between 0 and 1 representing the confidence required for the return bounds values
- Returns
- list
a list of size 2 containing the lower and upper bounds, in this order, such that the actual value is between these bounds with probability equal to the given confidence. Never return null, however, if the bounds could not be computed, then the lower bound will be negative infinity (min float) and the upper bound will be positive infinity (max float)
-
error_at
(at)¶ compute the error estimate for the forecast at the given point in time
- Parameters
- atint
time in the future at which error is being requested
- Returns
- float
the error estimate. If the underlying algorithm does not support error computation, returns NaN
-
property
error_horizon_length
¶ - Returns
- int
Get the value of the error horizon length. Will return 0 if not set.
-
forecast_at
(at)¶ compute the forecast at a given time
- Parameters
- atint
the time at which to compute a forecast
- Returns
- float
a valid forecast value
-
property
initial_time_updated
¶ - Returns
- int
Get the first time value used to build the forecasting model. Will return a negative value if the model has not been initialized
-
is_initialized
()¶ - Returns
- bool
True if the model has been initialized and is ready for forecasting
-
property
last_time_updated
¶ - Returns
- int
Get the most recent time value used to build the forecasting model. Will return negative value if the model is not updated
-
reset_model
()¶ reset the model to an uninitialized state
-
save
(path)¶ save the model to a file
- Parameters
- pathstring
path to file
-
update_model
(timestamp, value)¶ update the model at a given timestamp
- Parameters
- timestampint
time corresponding to the given data value
- valuefloat
data value to be trained into the model at the given timestamp