quantile_returns¶
Defined in fynance.metrics
- quantile_returns(factor, fwd, n_quantiles=5)[source]
Equal-count quantile-portfolio returns of a factor.
On each bar the assets valid in both
factorandfwd(finite on both sides) are sorted by factor value and split inton_quantilesequal-count buckets — ties are broken by rank order (a stable sort), bucket0holding the lowest factor values and bucketn_quantiles - 1the highest. Each bucket’s return is the equal-weighted mean of its assets’ forward returns. Bars with fewer thann_quantilesvalid assets yield a row ofnp.nan(and zero counts).Alignment.
factor[t]is the score known at bartandfwd[t]the return realized aftert(built by the caller, e.g. viafynance.features.horizon_returns), so a positive top-minus-bottomspreadmeans high-factor assets out-earned low-factor assets.- Parameters:
- factornp.ndarray[dtype, ndim=2]
Factor panel
(T, N)— the per-bar cross-sectional score.- fwdnp.ndarray[dtype, ndim=2]
Forward-return panel
(T, N)aligned withfactor.- n_quantilesint, optional
Number of equal-count buckets
Q(default5), a positive integer of at least2.
- Returns:
- QuantileResult
The per-bar bucket returns, long-short spread, bucket counts and
n_quantiles.
See also
fynance.metrics.information_coefficient,ic_summary
Examples
A two-bar, four-asset panel split into two buckets; the factor ranking is ascending on the first bar and descending on the second:
>>> import numpy as np >>> factor = np.array([[1., 2., 3., 4.], [4., 3., 2., 1.]]) >>> fwd = np.array([[1., 2., 3., 4.], [1., 2., 3., 4.]]) >>> res = quantile_returns(factor, fwd, n_quantiles=2) >>> res.quantile_returns array([[1.5, 3.5], [3.5, 1.5]]) >>> res.spread array([ 2., -2.]) >>> res.counts array([[2, 2], [2, 2]])