Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-221

Canonical Correlation for Given Weights

Problem

Return the correlation between the composites a'x and b'y: (a'Rxy b)/sqrt((a'Rxx a)(b'Ryy b)), given 2-vectors of weights and the correlation blocks. Return 0.0 for wrong shapes or non-positive composite variances.

Hint

This is the canonical correlation once the weight vectors are fixed.

Test cases

case 1
in: [[1,0],[1,0],[[1,0],[0,1]],[[1,0],[0,1]],[[0.6,0.2],[0.1,0.5]]]
expected: 0.6
case 2
in: [[1,1],[1,1],[[1,0.3],[0.3,1]],[[1,0.4],[0.4,1]],[[0.5,0.4],[0.4,0.5]]]
expected: 0.6671243849949912
case 3
in: [[1,-1],[1,1],[[1,0.3],[0.3,1]],[[1,0.4],[0.4,1]],[[0.5,0.4],[0.4,0.5]]]
expected: 0
case 4
in: [[1,0],[0,1],[[1,0],[0,1]],[[1,0],[0,1]],[[0.6,0.2],[0.1,0.5]]]
expected: 0.2
case 5
in: [[1],[1],[[1]],[[1]],[[1]]]
expected: 0
Starter codePython
def canonical_corr_weights(a, b, rxx, ryy, rxy):
    # Your code here
    pass

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