Spearman's Rank Correlation Coefficient

Prob. and Stats

Difficulty: 1 | Problem written by Junaid Ahmed
A quantitative metric of the linearity of a relation between two datasets is the Spearman rank-order correlation coefficient:

\(r_{s} = 1 - \frac{6\sum d_{i}^{2}}{n(n^{2}-1)}\)

d is the difference between the two data points

n is the total number of observations

Write a Python function to implement the Spearman rank-order correlation coefficient.


d, n


Correlation coefficient


Sample Input:
<class 'float'>
d1: 0.2
<class 'float'>
d2: 0.4
<class 'float'>
d3: 0.5
<class 'int'>
n: 2

Expected Output:
<class 'float'>

This is a premium problem, to view more details of this problem please sign up for MLPro Premium. MLPro premium offers access to actual machine learning and data science interview questions and coding challenges commonly asked at tech companies all over the world

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Get access to Premium only exclusive educational content available to only Premium users.

Have an issue, the MLPro support team is available 24X7 to Premium users.

This is a premium feature.
To access this and other such features, click on upgrade below.

Log in to post a comment

Jump to comment-111
uozcan12 • 5 months, 3 weeks ago


I applied this and I've got 0.55 but expected score seems 0.66. But what should I do?


# Please do not change the below function name and parameters
def sR(d1,d2,d3,n):
    d_square_sum = d1**2 + d2**2 + d3**2
    divided_by = n*((n**2)-1)
    division_part = (6*d_square_sum)
    return 1-(division_part/divided_by)


Jump to comment-161
jdy33 • 2 months, 3 weeks ago


I think it should be 0.55 though


Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)