Estimators#

Work in progress.

This module implements Estimator base classes.

Policies, costs, and constraints inherit from this.

class cvxportfolio.estimator.Estimator#

Estimator abstraction, designed for repeated evaluation over time.

Policies, costs, and constraints inherit from this. When overloading methods defined here one should be very careful. The recommended usage (if you want a class that uses our recursive execution model) is to put any sub-estimators at the class attribute level, like we do throughout the library. That ensures that the sub-estimators will be evaluated before the class itself by both initialize_estimator_recursive() and values_in_time_recursive().

class cvxportfolio.estimator.CvxpyExpressionEstimator#

Base class for estimators that are Cvxpy expressions.

class cvxportfolio.estimator.DataEstimator(data, use_last_available_time=False, allow_nans=False, compile_parameter=False, non_negative=False, positive_semi_definite=False, data_includes_cash=False, ignore_shape_check=False)#

Estimator of point-in-time values from internal data.

It also implements logic to check that no nan are returned by its values_in_time_recursive method, which is the way Cvxportfolio objects use this class to get data, to compile and update a Cvxpy parameter, and to slice the data with the current trading universe.

Parameters:
  • data (object, pandas.Series, pandas.DataFrame) – Data expressed preferably as pandas Series or DataFrame where the first index is a pandas.DateTimeIndex. Otherwise you can pass a callable object which implements the values_in_time_recursive() method (with the standard signature) and returns the corresponding value in time, or a constant float, numpy.array, or even pandas Series or DataFrame not indexed by time (e.g., a covariance matrix where both index and columns are the stock symbols).

  • use_last_available_time (bool) – if the pandas index exists and is a pandas.DateTimeIndex you can instruct values_in_time_recursive() to retrieve the last available value at time t by setting this to True. Default is False.

  • allow_nans (bool) – If True, allow data returned to contain nan. Default False.

  • compile_parameter (bool) – If True, compile a Cvxpy parameter that gets updated with the current value of the instance at each point in a backtest. Default False.

  • non_negative (bool) – If True, the compiled Cvxpy parameter is non-negative (this affects certain Cvxpy operations). Default False.

  • positive_semi_definite (bool) – If True, the compiled Cvxpy parameter is market as a positive semi-definite matrix (this affects certain Cvxpy operations). Default False.

  • data_includes_cash (bool) – If True, when the data is sliced with the current trading universe we also look for the values corresponding to the cash account. Default False.

  • ignore_shape_check (bool) – If True, we don’t do any slicing of the data according to the current trading universe. Default False.

Raises:
  • cvxportfolio.NaNError – If nan are present in result.

  • cvxportfolio.MissingTimesError – If some times are missing.

  • cvxportfolio.MissingAssetsError – If some assets are missing.

  • cvxportfolio.DataError – If data is not in the right form.