Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-084

Semipartial Correlation

Problem

Return the semipartial correlation of x with y controlling for z: (r_xy - r_xz*r_yz) / sqrt(1 - r_yz^2). Return 0.0 when |r_yz| >= 1.

Hint

Only y is residualized, so the denominator omits the r_xz term.

Test cases

case 1
in: [0.6,0.5,0.4]
expected: 0.4364357804719847
case 2
in: [0.5,0,0]
expected: 0.5
case 3
in: [0.3,0.1,0.1]
expected: 0.2914609664251715
case 4
in: [0.5,0.5,0.5]
expected: 0.2886751345948129
case 5
in: [0.9,0.9,0.9]
expected: 0.20647416048350556
Starter codePython
def semipartial_correlation(r_xy, r_xz, r_yz):
    # Your code here
    pass

5 test cases · run your code against them in the editor