24
24
from random import sample
25
25
from typing import Any , BinaryIO , Callable , Dict , List , Optional , Sequence , Tuple , Union
26
26
27
+ try :
28
+ from typing import Literal
29
+ except ImportError :
30
+ from typing_extensions import Literal
31
+
27
32
import numpy as np
28
33
import pandas as pd
29
34
@@ -560,6 +565,7 @@ def historical_forecasts(
560
565
num_samples : int = 1 ,
561
566
train_length : Optional [int ] = None ,
562
567
start : Optional [Union [pd .Timestamp , float , int ]] = None ,
568
+ start_format : Literal ["position" , "value" ] = "value" ,
563
569
forecast_horizon : int = 1 ,
564
570
stride : int = 1 ,
565
571
retrain : Union [bool , int , Callable [..., bool ]] = True ,
@@ -609,15 +615,14 @@ def historical_forecasts(
609
615
steps available, all steps up until prediction time are used, as in default case. Needs to be at least
610
616
`min_train_series_length`.
611
617
start
612
- Optionally, the first point in time at which a prediction is computed for a future time.
613
- This parameter supports: ``float``, ``int`` and ``pandas.Timestamp``, and ``None``.
614
- If a ``float``, the parameter will be treated as the proportion of the time series
615
- that should lie before the first prediction point.
616
- If an ``int``, the parameter will be treated as an integer index to the time index of
617
- `series` that will be used as first prediction time.
618
- If a ``pandas.Timestamp``, the time stamp will be used to determine the first prediction time
619
- directly.
620
- If ``None``, the first prediction time will automatically be set to:
618
+ Optionally, the first point in time at which a prediction is computed. This parameter supports:
619
+ ``float``, ``int``, ``pandas.Timestamp``, and ``None``.
620
+ If a ``float``, it is the proportion of the time series that should lie before the first prediction point.
621
+ If an ``int``, it is either the index position of the first prediction point for `series` with a
622
+ `pd.DatetimeIndex`, or the index value for `series` with a `pd.RangeIndex`. The latter can be changed to
623
+ the index position with `start_format="position"`.
624
+ If a ``pandas.Timestamp``, it is the time stamp of the first prediction point.
625
+ If ``None``, the first prediction point will automatically be set to:
621
626
622
627
- the first predictable point if `retrain` is ``False``, or `retrain` is a Callable and the first
623
628
predictable point is earlier than the first trainable point.
@@ -628,6 +633,13 @@ def historical_forecasts(
628
633
Note: Raises a ValueError if `start` yields a time outside the time index of `series`.
629
634
Note: If `start` is outside the possible historical forecasting times, will ignore the parameter
630
635
(default behavior with ``None``) and start at the first trainable/predictable point.
636
+ start_format
637
+ Defines the `start` format. Only effective when `start` is an integer and `series` is indexed with a
638
+ `pd.RangeIndex`.
639
+ If set to 'position', `start` corresponds to the index position of the first predicted point and can range
640
+ from `(-len(series), len(series) - 1)`.
641
+ If set to 'value', `start` corresponds to the index value/label of the first predicted point. Will raise
642
+ an error if the value is not in `series`' index. Default: ``'value'``
631
643
forecast_horizon
632
644
The forecast horizon for the predictions.
633
645
stride
@@ -798,6 +810,7 @@ def retrain_func(
798
810
future_covariates = future_covariates ,
799
811
num_samples = num_samples ,
800
812
start = start ,
813
+ start_format = start_format ,
801
814
forecast_horizon = forecast_horizon ,
802
815
stride = stride ,
803
816
overlap_end = overlap_end ,
@@ -876,6 +889,7 @@ def retrain_func(
876
889
forecast_horizon = forecast_horizon ,
877
890
overlap_end = overlap_end ,
878
891
start = start ,
892
+ start_format = start_format ,
879
893
show_warnings = show_warnings ,
880
894
)
881
895
@@ -1030,6 +1044,7 @@ def backtest(
1030
1044
num_samples : int = 1 ,
1031
1045
train_length : Optional [int ] = None ,
1032
1046
start : Optional [Union [pd .Timestamp , float , int ]] = None ,
1047
+ start_format : Literal ["position" , "value" ] = "value" ,
1033
1048
forecast_horizon : int = 1 ,
1034
1049
stride : int = 1 ,
1035
1050
retrain : Union [bool , int , Callable [..., bool ]] = True ,
@@ -1085,25 +1100,31 @@ def backtest(
1085
1100
steps available, all steps up until prediction time are used, as in default case. Needs to be at least
1086
1101
`min_train_series_length`.
1087
1102
start
1088
- Optionally, the first point in time at which a prediction is computed for a future time.
1089
- This parameter supports: ``float``, ``int`` and ``pandas.Timestamp``, and ``None``.
1090
- If a ``float``, the parameter will be treated as the proportion of the time series
1091
- that should lie before the first prediction point.
1092
- If an ``int``, the parameter will be treated as an integer index to the time index of
1093
- `series` that will be used as first prediction time.
1094
- If a ``pandas.Timestamp``, the time stamp will be used to determine the first prediction time
1095
- directly.
1096
- If ``None``, the first prediction time will automatically be set to:
1097
- - the first predictable point if `retrain` is ``False``, or `retrain` is a Callable and the first
1098
- predictable point is earlier than the first trainable point.
1099
-
1100
- - the first trainable point if `retrain` is ``True`` or ``int`` (given `train_length`),
1101
- or `retrain` is a Callable and the first trainable point is earlier than the first predictable point.
1102
-
1103
- - the first trainable point (given `train_length`) otherwise
1103
+ Optionally, the first point in time at which a prediction is computed. This parameter supports:
1104
+ ``float``, ``int``, ``pandas.Timestamp``, and ``None``.
1105
+ If a ``float``, it is the proportion of the time series that should lie before the first prediction point.
1106
+ If an ``int``, it is either the index position of the first prediction point for `series` with a
1107
+ `pd.DatetimeIndex`, or the index value for `series` with a `pd.RangeIndex`. The latter can be changed to
1108
+ the index position with `start_format="position"`.
1109
+ If a ``pandas.Timestamp``, it is the time stamp of the first prediction point.
1110
+ If ``None``, the first prediction point will automatically be set to:
1111
+
1112
+ - the first predictable point if `retrain` is ``False``, or `retrain` is a Callable and the first
1113
+ predictable point is earlier than the first trainable point.
1114
+ - the first trainable point if `retrain` is ``True`` or ``int`` (given `train_length`),
1115
+ or `retrain` is a Callable and the first trainable point is earlier than the first predictable point.
1116
+ - the first trainable point (given `train_length`) otherwise
1117
+
1104
1118
Note: Raises a ValueError if `start` yields a time outside the time index of `series`.
1105
1119
Note: If `start` is outside the possible historical forecasting times, will ignore the parameter
1106
1120
(default behavior with ``None``) and start at the first trainable/predictable point.
1121
+ start_format
1122
+ Defines the `start` format. Only effective when `start` is an integer and `series` is indexed with a
1123
+ `pd.RangeIndex`.
1124
+ If set to 'position', `start` corresponds to the index position of the first predicted point and can range
1125
+ from `(-len(series), len(series) - 1)`.
1126
+ If set to 'value', `start` corresponds to the index value/label of the first predicted point. Will raise
1127
+ an error if the value is not in `series`' index. Default: ``'value'``
1107
1128
forecast_horizon
1108
1129
The forecast horizon for the point predictions.
1109
1130
stride
@@ -1160,6 +1181,7 @@ def backtest(
1160
1181
num_samples = num_samples ,
1161
1182
train_length = train_length ,
1162
1183
start = start ,
1184
+ start_format = start_format ,
1163
1185
forecast_horizon = forecast_horizon ,
1164
1186
stride = stride ,
1165
1187
retrain = retrain ,
@@ -1210,6 +1232,7 @@ def gridsearch(
1210
1232
forecast_horizon : Optional [int ] = None ,
1211
1233
stride : int = 1 ,
1212
1234
start : Union [pd .Timestamp , float , int ] = 0.5 ,
1235
+ start_format : Literal ["position" , "value" ] = "value" ,
1213
1236
last_points_only : bool = False ,
1214
1237
show_warnings : bool = True ,
1215
1238
val_series : Optional [TimeSeries ] = None ,
@@ -1275,17 +1298,38 @@ def gridsearch(
1275
1298
forecast_horizon
1276
1299
The integer value of the forecasting horizon. Activates expanding window mode.
1277
1300
stride
1278
- The number of time steps between two consecutive predictions. Only used in expanding window mode .
1301
+ Only used in expanding window mode. The number of time steps between two consecutive predictions.
1279
1302
start
1280
- The ``int``, ``float`` or ``pandas.Timestamp`` that represents the starting point in the time index
1281
- of `series` from which predictions will be made to evaluate the model.
1282
- For a detailed description of how the different data types are interpreted, please see the documentation
1283
- for `ForecastingModel.backtest`. Only used in expanding window mode.
1303
+ Only used in expanding window mode. Optionally, the first point in time at which a prediction is computed.
1304
+ This parameter supports: ``float``, ``int``, ``pandas.Timestamp``, and ``None``.
1305
+ If a ``float``, it is the proportion of the time series that should lie before the first prediction point.
1306
+ If an ``int``, it is either the index position of the first prediction point for `series` with a
1307
+ `pd.DatetimeIndex`, or the index value for `series` with a `pd.RangeIndex`. The latter can be changed to
1308
+ the index position with `start_format="position"`.
1309
+ If a ``pandas.Timestamp``, it is the time stamp of the first prediction point.
1310
+ If ``None``, the first prediction point will automatically be set to:
1311
+
1312
+ - the first predictable point if `retrain` is ``False``, or `retrain` is a Callable and the first
1313
+ predictable point is earlier than the first trainable point.
1314
+ - the first trainable point if `retrain` is ``True`` or ``int`` (given `train_length`),
1315
+ or `retrain` is a Callable and the first trainable point is earlier than the first predictable point.
1316
+ - the first trainable point (given `train_length`) otherwise
1317
+
1318
+ Note: Raises a ValueError if `start` yields a time outside the time index of `series`.
1319
+ Note: If `start` is outside the possible historical forecasting times, will ignore the parameter
1320
+ (default behavior with ``None``) and start at the first trainable/predictable point.
1321
+ start_format
1322
+ Only used in expanding window mode. Defines the `start` format. Only effective when `start` is an integer
1323
+ and `series` is indexed with a `pd.RangeIndex`.
1324
+ If set to 'position', `start` corresponds to the index position of the first predicted point and can range
1325
+ from `(-len(series), len(series) - 1)`.
1326
+ If set to 'value', `start` corresponds to the index value/label of the first predicted point. Will raise
1327
+ an error if the value is not in `series`' index. Default: ``'value'``
1284
1328
last_points_only
1285
- Whether to use the whole forecasts or only the last point of each forecast to compute the error. Only used
1286
- in expanding window mode .
1329
+ Only used in expanding window mode. Whether to use the whole forecasts or only the last point of each
1330
+ forecast to compute the error .
1287
1331
show_warnings
1288
- Whether to show warnings related to the `start` parameter. Only used in expanding window mode .
1332
+ Only used in expanding window mode. Whether to show warnings related to the `start` parameter.
1289
1333
val_series
1290
1334
The TimeSeries instance used for validation in split mode. If provided, this series must start right after
1291
1335
the end of `series`; so that a proper comparison of the forecast can be made.
@@ -1386,6 +1430,7 @@ def _evaluate_combination(param_combination) -> float:
1386
1430
future_covariates = future_covariates ,
1387
1431
num_samples = 1 ,
1388
1432
start = start ,
1433
+ start_format = start_format ,
1389
1434
forecast_horizon = forecast_horizon ,
1390
1435
stride = stride ,
1391
1436
metric = metric ,
@@ -1893,6 +1938,7 @@ def _optimized_historical_forecasts(
1893
1938
future_covariates : Optional [Sequence [TimeSeries ]] = None ,
1894
1939
num_samples : int = 1 ,
1895
1940
start : Optional [Union [pd .Timestamp , float , int ]] = None ,
1941
+ start_format : Literal ["position" , "value" ] = "value" ,
1896
1942
forecast_horizon : int = 1 ,
1897
1943
stride : int = 1 ,
1898
1944
overlap_end : bool = False ,
0 commit comments