QFSeries

class qf_lib.containers.series.qf_series.QFSeries(data: Optional[object] = None, index: Optional[object] = None, dtype: Optional[object] = None, name: Optional[object] = None, copy: bool = False, fastpath: bool = False)[source]

Bases: Series, TimeIndexedContainer

Base class for all time-indexed series used in the quant-fin project.

Methods:

exponential_average([lambda_coeff])

Calculates the exponential average of a series.

get_frequency()

Attempts to infer the frequency of this series.

min_max_normalized([original_min_value, ...])

Normalizes the data using min-max scaling: it maps all the data to the [0;1] range, so that 0 corresponds to the minimal value in the original series and 1 corresponds to the maximal value.

rolling_window(window_size, func[, step, ...])

Looks at a number of windows of size window_size and transforms the data in those windows based on the specified func.

rolling_window_with_benchmark(benchmark, ...)

Looks at a number of windows of size window_size and transforms the data in those windows based on the specified func.

to_log_returns()

Converts timeseries to the timeseries of logarithmic returns.

to_prices([initial_price, ...])

Converts a timeseries into series of prices.

to_simple_returns()

Converts timeseries to the timeseries of simple returns.

total_cumulative_return()

Calculates the total cumulative return for the series.

exponential_average(lambda_coeff: float = 0.94) QFSeries[source]

Calculates the exponential average of a series.

Parameters:

lambda_coeff – lambda coefficient

Returns:

exponential average of the series

Return type:

QFSeries

get_frequency() Frequency[source]

Attempts to infer the frequency of this series. The analysis uses pandas’ infer_freq, as well as a heuristic to reduce the amount of Irregular results.

See the implementation of the Frequency.infer_freq function for more information.

min_max_normalized(original_min_value: Optional[float] = None, original_max_value: Optional[float] = None) QFSeries[source]

Normalizes the data using min-max scaling: it maps all the data to the [0;1] range, so that 0 corresponds to the minimal value in the original series and 1 corresponds to the maximal value. It is also possible to specify values which should correspond to 0 and 1 after applying the normalization. It is useful if the same normalization parameters are used to normalize different data.

Parameters:
  • original_min_value – value which should correspond to 0 after applying the normalization

  • original_max_value – value which should correspond to 1 after applying the normalization

Returns:

series of normalized values

Return type:

normalized_series

rolling_window(window_size: int, func: Callable[[Union[QFSeries, ndarray]], float], step: int = 1, optimised: bool = False) QFSeries[source]

Looks at a number of windows of size window_size and transforms the data in those windows based on the specified func.

The window indices are stepped at a rate specified by step.

Parameters:
  • window_size – The size of the window to look at specified as the number of data points.

  • func – The function to call during each iteration. When other is None this function should take one QFSeries and return a value (Usually a number such as a float). Otherwise, this function should take two QFSeries arguments and return a value.

  • step – The amount of data points to step through after each iteration, i.e. how much to move the window by in each iteration.

  • optimised – Whether the more efficient pandas algorithm should be used for the rolling window application. Note: This has some limitations: The step must be 1 and func will get an ndarray parameter which only contains values and no index.

Returns:

A QFSeries containing the transformed data.

Return type:

QFSeries

rolling_window_with_benchmark(benchmark: QFSeries, window_size: int, func: Callable[[QFSeries], float], step: int = 1) QFSeries[source]

Looks at a number of windows of size window_size and transforms the data in those windows based on the specified func.

The window indices are stepped at a rate specified by step. This function runs a “correlated” rolling window iteration. The func must accept two arguments, one from each series.

Parameters:
  • benchmark – The benchmark to compare to.

  • window_size – The size of the window to look at specified as the number of data points.

  • func – The function to call during each iteration. When other is None this function should take two QFSeries arguments and return a value. (Usually a number such as a float).

  • step – The amount of data points to step through after each iteration, i.e. how much to move the window by in each iteration.

Returns:

A QFSeries containing the transformed data.

Return type:

QFSeries

to_log_returns() LogReturnsSeries[source]

Converts timeseries to the timeseries of logarithmic returns. First date of prices in the returns timeseries won’t be present.

Returns:

timeseries of log returns

Return type:

LogReturnsSeries

to_prices(initial_price: float = None, suggested_initial_date: Union[datetime, int, float] = None, frequency: Frequency = None) PricesSeries[source]

Converts a timeseries into series of prices. The timeseries of prices returned will have an extra date at the beginning (in comparison to the returns’ timeseries). The difference between the extra date and the rest of the dates can be inferred from the returns’ timeseries or can be calculated using the frequency passed as the optional argument. Additional date at the beginning (so called “initial date”) is caused by the fact, that return for the first date of prices timeseries cannot be calculated, so it’s missing. Thus, during the opposite conversion, extra date at the beginning will be added.

Parameters:
  • initial_price – initial price of the timeseries. If no price will be specified, then it will be assumed to be 1.

  • suggested_initial_date – the first date or initial value for the prices series. It won’t be necessarily the first date of the price series (e.g. if the method is run on the PricesSeries then it won’t be used).

  • frequency – the frequency of the returns’ timeseries. It is used to infer the initial date for the prices series.

Returns:

series of prices

Return type:

PricesSeries

to_simple_returns() SimpleReturnsSeries[source]

Converts timeseries to the timeseries of simple returns. First date of prices in the returns timeseries won’t be present.

Returns:

timeseries of simple returns

Return type:

SimpleReturnsSeries

total_cumulative_return() float[source]

Calculates the total cumulative return for the series.