kappa_sdk.get_closest_value_at_date_from_vector#
- kappa_sdk.get_closest_value_at_date_from_vector(vector, targeted_date, backward=True)[source]#
Find the closest value in a vector based on a given date.
This utility function retrieves the closest value from a specified vector of dates and values, based on a target date. The function accounts for a forward or backward search direction when determining the closest value.
The function assumes the dates in the vector are sorted in ascending order. If the target date lies outside the bounds of the vector, it returns the nearest boundary value. If the vector is empty, it raises a ValueError.
- Parameters:
vector (
Vector) – The vector containing dates and values, where dates is a sorted list of datetime objects and values is a list of corresponding float values.targeted_date (
datetime) – The target date for which the closest value in the vector is being sought.backward (
bool, optional) – If True, returns the last value at or before the targeted date (backward search). If False, returns the first value at or after the targeted date (forward search). Default: True.
- Returns:
float– The closest value from the vector corresponding to the target date.- Raises:
ValueError – If the vector provided is empty.
- Return type:
float