Previous topic

fynance.features.metrics.perf_strat

Next topic

fynance.features.metrics.z_score

fynance.features.metrics.sharpe

fynance.features.metrics.sharpe(X, rf=0, period=252, log=False, axis=0, dtype=None, ddof=0)

Compute the Sharpe ratio for each X’ series.

Parameters:
X : np.ndarray[dtype, ndim=1 or 2]

Time-series of prices, performances or index.

rf : float, optional

Means the annualized risk-free rate, default is 0.

period : int, optional

Number of period per year, default is 252 (trading days).

log : bool, optional

If true compute sharpe with the formula for log-returns, default is False.

axis : {0, 1}, optional

Axis along wich the computation is done. Default is 0.

dtype : np.dtype, optional

The type of the output array. If dtype is not given, infer the data type from X input.

ddof : int, optional

Means Delta Degrees of Freedom, the divisor used in calculations is T - ddof, where T represents the number of elements in time axis. Default is 0.

Returns:
dtype or np.ndarray[dtype, ndim=1]

Value of Sharpe ratio for each series.

Notes

Sharpe ratio [7] is computed as the annualized expected returns (annual_return) minus the risk-free rate (noted \(rf\)) over the annualized volatility of returns (annual_volatility) such that:

\[\begin{split}sharpeRatio = \frac{E(R) - rf}{\sqrt{period \times Var(R)}} \\ \\\end{split}\]

where, \(R_1 = 0\) and \(R_{2:T} = \begin{cases}ln(\frac{X_{2:T}} {X_{1:T-1}}) \text{, if log=True}\\ \frac{X_{2:T}}{X_{1:T-1}} - 1 \text{, otherwise} \\ \end{cases}\)

References

[7]https://en.wikipedia.org/wiki/Sharpe_ratio

Examples

Assume a series X of monthly prices:

>>> X = np.array([70, 100, 80, 120, 160, 80]).astype(np.float64)
>>> sharpe(X, period=12)
0.24475518072327812
>>> sharpe(X.reshape([6, 1]), period=12)
array([0.24475518])