def deflated_sharpe_ratio( self, risk_free: float = 0., var_sharpe: tp.Optional[float] = None, nb_trials: tp.Optional[int] = None, ddof: int = 0, bias: bool = True, wrap_kwargs: tp.KwargsLike = None) -> tp.MaybeSeries: """Deflated Sharpe Ratio (DSR). Expresses the chance that the advertized strategy has a positive Sharpe ratio. If `var_sharpe` is None, is calculated based on all columns. If `nb_trials` is None, is set to the number of columns.""" sharpe_ratio = to_1d(self.sharpe_ratio(risk_free=risk_free), raw=True) if var_sharpe is None: var_sharpe = np.var(sharpe_ratio, ddof=ddof) if nb_trials is None: nb_trials = self.wrapper.shape_2d[1] returns = to_2d(self._obj, raw=True) nanmask = np.isnan(returns) if nanmask.any(): returns = returns.copy() returns[nanmask] = 0. result = metrics.deflated_sharpe_ratio( est_sharpe=sharpe_ratio / np.sqrt(self.ann_factor), var_sharpe=var_sharpe / self.ann_factor, nb_trials=nb_trials, backtest_horizon=self.wrapper.shape_2d[0], skew=skew(returns, axis=0, bias=bias), kurtosis=kurtosis(returns, axis=0, bias=bias)) wrap_kwargs = merge_dicts(dict(name_or_index='deflated_sharpe_ratio'), wrap_kwargs) return self.wrapper.wrap_reduced(result, **wrap_kwargs)
def deflated_sharpe_ratio(self, risk_free=0., var_sharpe=None, nb_trials=None, ddof=0, bias=True): """Deflated Sharpe Ratio (DSR). Expresses the chance that the advertized strategy has a positive Sharpe ratio. If `var_sharpe` is None, is calculated based on all columns. If `nb_trials` is None, is set to the number of columns.""" sharpe_ratio = reshape_fns.to_1d( self.sharpe_ratio(risk_free=risk_free), raw=True) if var_sharpe is None: var_sharpe = np.var(sharpe_ratio, ddof=ddof) if nb_trials is None: nb_trials = self.shape_2d[1] returns = reshape_fns.to_2d(self._obj, raw=True) nanmask = np.isnan(returns) if nanmask.any(): returns = returns.copy() returns[nanmask] = 0. return self.wrap_reduced( metrics.deflated_sharpe_ratio( est_sharpe=sharpe_ratio / np.sqrt(self.ann_factor), var_sharpe=var_sharpe / self.ann_factor, nb_trials=nb_trials, backtest_horizon=self.shape_2d[0], skew=skew(returns, axis=0, bias=bias), kurtosis=kurtosis(returns, axis=0, bias=bias)))