# Correlation loss function

Hi,

I have a correlation function that amplifies the tails and I’m trying to port it to PyTorch. The loss function looks like this (preds and targets are a single column):

``````def strong_tails_corr(preds, target):
ranked_preds = (preds.rank(method="average").values - 0.5) / preds.count()
gauss_ranked_preds = stats.norm.ppf(ranked_preds)
centered_target = target - target.mean()
preds_p15 = np.sign(gauss_ranked_preds) * np.abs(gauss_ranked_preds) ** 1.5
target_p15 = np.sign(centered_target) * np.abs(centered_target) ** 1.5
return np.corrcoef(preds_p15, target_p15)[0, 1]
``````

Any ideas on how I can do that? What is troubling me the most is how to build ranked_preds and gauss_ranked_preds, the rest I think I have it:

``````def corrcoef_torch(preds, targets):
preds = preds - preds.mean(dim=0)
preds = preds / preds.norm(dim=0)
targets = targets - targets.mean(dim=0)
targets = targets / targets.norm(dim=0)
return (preds * targets).sum()

from torch.distributions import Normal
import torchsort
normal = Normal(0,1)
def strong_tails_corr(preds, target):
ranked_preds = (torchsort.soft_rank(preds, regularization_strength=.0001) - 0.5) / preds.size(dim=0)
gauss_ranked_preds = normal.icdf(ranked_preds)
centered_target = target - target.mean(dim=0)
preds_p15 = torch.sign(gauss_ranked_preds) * torch.abs(gauss_ranked_preds) ** 1.5 #hadamard product
target_p15 = torch.sign(centered_target) * torch.abs(centered_target) ** 1.5 #hadamard product
return corrcoef_torch(preds_p15, target_p15)
``````