skfolio.preprocessing.prices_to_returns#

skfolio.preprocessing.prices_to_returns(X, y=None, log_returns=False, nan_threshold=1, join='outer', drop_inceptions_nan=True, fill_nan=True)[source]#

Transforms a DataFrame of prices to linear or logarithmic returns.

Linear returns (also called simple returns) are defined as:
\[\frac{S_{t}}{S_{t-1}} - 1\]
Logarithmic returns (also called continuously compounded return) are defined as:
\[ln\Biggl(\frac{S_{t}}{S_{t-1}}\Biggr)\]

With \(S_{t}\) the asset price at time \(t\).

Warning

The linear returns aggregate across securities, meaning that the linear return of the portfolio is the weighted average of the linear returns of the securities. For this reason, portfolio optimization should be performed using linear returns [1].

On the other hand, the logarithmic returns aggregate across time, meaning that the total logarithmic return over K time periods is the sum of all K single-period logarithmic returns.

See also

data preparation

Parameters:
XDataFrame

The DataFrame of assets prices.

yDataFrame, optional

The DataFrame of target or factors prices. If provided, it is joined with the DataFrame of prices to ensure identical observations.

log_returnsbool, default=True

If this is set to True, logarithmic returns are used instead of simple returns.

joinstr, default=”outer”

The join method between X and y when y is provided.

nan_thresholdfloat, default=1.0

Drop observations (rows) that have a percentage of missing assets prices above this threshold. The default (1.0) is to keep all the observations.

drop_inceptions_nanbool, default=True

If set to True, observations at the beginning are dropped if any of the asset values are missing, otherwise we keep the NaNs. This is useful when you work with a large universe of assets with different inception dates coupled with a pre-selection Transformer.

fill_nanbool, default=True

If set to True, missing prices (NaNs) are forward filled using the previous price. Otherwise, NaNs are kept.

Returns:
XDataFrame

The DataFrame of price returns of the input X.

yDataFrame, optional

The DataFrame of price returns of the input y when provided.

References

[1]

“Linear vs. Compounded Returns - Common Pitfalls in Portfolio Management”. GARP Risk Professional. Attilio Meucci (2010).