# Differentiable Spearman Correlation

**URL:** <https://forum.numer.ai/t/differentiable-spearman-correlation/2588>\
**Category:** Data Science\
**Created:** [March 29, 2021, 2:29pm UTC](https://forum.numer.ai/t/differentiable-spearman-correlation/2588 "2021-03-29T14:29:24Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![oiboy](https://avatars.discourse-cdn.com/v4/letter/o/d07c76/32.png) [@oiboy](https://forum.numer.ai/u/oiboy)\
**Post date:** [March 29, 2021, 11:38pm UTC](https://forum.numer.ai/t/differentiable-spearman-correlation/2588/2 "2021-03-29T23:38:24Z")

</div>

A couple people have been trying this. Check out

> [@Differentiable Spearman in PyTorch (Optimize for CORR directly)](http://forum.numer.ai/t/differentiable-spearman-in-pytorch-optimize-for-corr-directly/2287):
>
> @mdo previously showed how to use a [custom loss function](http://forum.numer.ai/t/custom-loss-functions-for-xgboost-using-pytorch/960) which involved taking the gradient of the sharpe ratio of the Pearson correlations over different eras. Although Pearson and Spearman might return similar values, it could be rewarding to optimize for Spearman directly (or Sharpe of Spearman). Since the ranked Spearman correlation needs a sort operation (which is not differentiable), it has not been possible to compute the gradient with respect to predictions, which eliminated the possibil…

and

> [@Objetive Function](http://forum.numer.ai/t/objetive-function/2235):
>
> Just writing this to share which target functions you use the most when training your models. I was thinking of customizing an Objective Function for boosted models in order to beat the common methods already developed. I know Spearman’s correlation is non-differentiable due to sort and rank steps, but I found some references to try to deal with these problems: [SoDeep](https://github.com/technicolor-research/sodeep)[fast-soft-sort](https://github.com/google-research/fast-soft-sort/tree/master/fast_soft_sort) I’ve tried to use SoDeep loss functions when training my MLPs and it was a complete disaster. So it would be ni…

I can’t find where I read it, but I believe the fast-soft-sort package’s Spearman function is only differentiable once, which would cause your `None` issue. You could try @mdo’s [solution, which is to just use a matrix of ones as the Hessian](http://forum.numer.ai/t/custom-loss-functions-for-xgboost-using-pytorch/960).

---

_[View the full topic](https://forum.numer.ai/t/differentiable-spearman-correlation/2588)._
