Return models¶
This module contains classes that define return models for portfolio optimization policies and related objects.
- class cvxportfolio.ReturnsForecast(r_hat=<class 'cvxportfolio.forecast.HistoricalMeanReturn'>, decay=1.0)View on GitHub¶
Returns forecast for non-cash assets, provided by the user or computed from the data.
It represents the objective term:
\[\hat{r}_t^T z_t.\]Optimization-based policies use this, typically as the first element of their objectives. See chapters 4 and 5 of the paper, for example page 25 for more details.
This class can either get return forecasts from the user, for example computed with some machine learning technique, or it can estimate them automatically from the data. The returns’ forecast provided by the user must be supplied for all assets excluding cash. See the Passing Data manual page for more information on how these are passed.
- Parameters:
r_hat (pd.Series, pd.DataFrame, float,
cvxportfolio.forecast.BaseForecast
class or instance) – constant or time varying returns estimates, either user-provided (see the manual page on passing data or computed internally by a forecaster class or instance. The default iscvxportfolio.forecast.HistoricalMeanReturn
which computes the historical means of the past returns, at each point in the back-test. It is instantiated internally with default parameters, so it computes the full historical means at each point in time. If you prefer to change that (e.g., do rolling mean or exponential moving window) you can instantiatecvxportfolio.forecast.HistoricalMeanReturn
with your chosen parameters and pass the instance.decay (float) – decay factor used in
cvxportfolio.MultiPeriodOptimization
policies. It is as a number in \([0,1]\). At step \(\tau\) of the MPO policy, where \(\tau=0\) is the initial one, the return predictions are multiplied by \(\texttt{decay}^{\tau}\). So,decay
close to zero models a fast signal whiledecay
close to one a slow signal. The default value is 1.
- Example:
>>> import cvxportfolio as cvx >>> policy = cvx.SinglePeriodOptimization(cvx.ReturnsForecast() - \ 0.5 * cvx.FullCovariance(), [cvx.LongOnly(), cvx.LeverageLimit(1)]) >>> cvx.MarketSimulator(['AAPL', 'MSFT', 'GOOG']).backtest(policy).plot()
Defines a single period optimization policy where the returns’ forecasts \(\hat{r}_t\) are the full average of past returns at each point in time and the risk model is the full covariance, also computed from the past returns.
- Example:
>>> my_forecasts = pd.DataFrame(...) >>> returns_model = cvx.ReturnsForecast(r_hat=my_forecasts)
With user-provided forecasts.
- Example:
>>> from cvxportfolio.forecast import HistoricalMeanReturn >>> returns_model = cvx.ReturnsForecast( r_hat=HistoricalMeanReturn(rolling = pd.Timedelta('365d')))
Instead of the default (full historical means at each point in time), use rolling means of 1 year.
- class cvxportfolio.CashReturn(cash_returns=None)View on GitHub¶
Objective term representing cash return.
By default, the forecast of cash return \({\left(\hat{r}_t\right)}_n\) is the observed value from last period \({\left({r}_{t-1}\right)}_n\).
This object is included automatically in
cvxportfolio.SinglePeriodOptimization
andcvxportfolio.MultiPeriodOptimization
policies. You can change this behavior by setting theirinclude_cash_return
argument to False. If you do so, you may include this cost explicitely in the objective. You need to do so (only) if you provide your own cash return forecast.- Parameters:
cash_returns (float or pd.Series or None) – if you have your forecast for the cash return, you should pass it here, either as a float (if constant) or as pd.Series with datetime index (if it changes in time). If you leave the default, None, the cash return forecast at time t is the observed cash return at time t-1.