Skip to content

dates

ies_pi_predict.dates

determine_training_dates

determine_training_dates(
    prediction_start: datetime,
    prediction_end: datetime,
    min_training_days=default_min_training_days,
    max_training_days=default_max_training_days,
    min_factor=default_min_factor,
) -> tuple[datetime, datetime]

determine_training_dates calculates the dates needed for accurate training, given the dates for the prediction.

It uses min_factor to multiply the period of time between the two prediction dates, and it adjust the values with min_training_days and max_training_days.

Parameters:

Name Type Description Default
prediction_start datetime

The start date of the prediction.

required
prediction_end datetime

The end date of the prediction.

required
min_training_days int

The minimum number of training days to consider. Defaults to config.default_min_training_days.

default_min_training_days
max_training_days int

The maximum number of training days to consider. Defaults to config.default_max_training_days.

default_max_training_days
min_factor float

The multiplying factor of the period of time. Defaults to config.default_min_factor.

default_min_factor

Raises:

Type Description
ValueError

When end_date comes before start_date.

Returns:

Type Description
tuple[datetime, datetime]

tuple[datetime, datetime]: A tuple containing the training_start_date and training_end_date.

Source code in src/ies_pi_predict/dates.py
def determine_training_dates(prediction_start: datetime, 
                             prediction_end: datetime,
                             min_training_days = default_min_training_days,
                             max_training_days = default_max_training_days,
                             min_factor = default_min_factor) -> tuple[datetime, datetime]:
    """
    determine_training_dates calculates the dates needed for accurate training,
    given the dates for the prediction.

    It uses min_factor to multiply the period of time between the two prediction
    dates, and it adjust the values with min_training_days and max_training_days.

    Args:
        prediction_start (datetime): The start date of the prediction.
        prediction_end (datetime): The end date of the prediction.
        min_training_days (int, optional): The minimum number of training days to consider.
                                              Defaults to config.default_min_training_days.
        max_training_days (int, optional): The maximum number of training days to consider.
                                              Defaults to config.default_max_training_days.
        min_factor (float, optional): The multiplying factor of the period of time. 
                                       Defaults to config.default_min_factor.

    Raises:
        ValueError: When end_date comes before start_date.

    Returns:
        tuple[datetime, datetime]: A tuple containing the training_start_date and
                                   training_end_date.
    """    
    if prediction_start >= prediction_end:
        raise ValueError("end date is before or equal to start date")

    prediction_days = (prediction_end - prediction_start).days
    training_days = prediction_days * min_factor
    training_days = max(training_days, min_training_days)
    training_days = min(training_days, max_training_days)

    training_start = prediction_start - timedelta(days=training_days)
    training_end = prediction_end

    return (training_start, training_end)