@@ -1435,24 +1435,38 @@ def calculate_weights_spn(correlations, cov):
1435
1435
# Check if the correlations are positive, otherwise rho = 10e-5
1436
1436
correlations = np .where (correlations < 10e-5 , 10e-5 , correlations )
1437
1437
1438
- if correlations .shape [0 ] > 1 :
1438
+ if correlations .shape [0 ] > 1 and len ( cov ) > 1 :
1439
1439
if isinstance (cov , type (None )):
1440
1440
raise ValueError ("cov must contain a covariance matrix" )
1441
1441
else :
1442
+ print (cov )
1442
1443
# Make a numpy matrix out of cov and get the inverse
1444
+ cov = np .where (cov == 0.0 , 10e-5 , cov )
1445
+ # Make sure the determinant of the matrix is not zero, otherwise
1446
+ # subtract 10e-5 from the cross-correlations between the models
1447
+ if np .linalg .det (cov ) == 0.0 :
1448
+ cov = cov - 10e-5
1449
+ # Ensure the correlation of the model with itself is always 1.0
1450
+ for i , _ in enumerate (cov ):
1451
+ cov [i ][i ] = 1.0
1452
+ # Make a numpy matrix out of the array
1443
1453
cov_matrix = np .asmatrix (cov )
1454
+ # Get the inverse of the matrix
1444
1455
cov_matrix_inv = cov_matrix .getI ()
1445
1456
# The component weights are the dot product between cov_matrix_inv
1446
1457
# and cor_vec
1447
1458
weights = cov_matrix_inv .dot (correlations )
1459
+ weights = np .nan_to_num (
1460
+ weights , copy = True , nan = 10e-5 , posinf = 10e-5 , neginf = 10e-5
1461
+ )
1448
1462
# If the dot product of the weights with the correlations is
1449
1463
# larger than 1.0, we assign a weight of 0.0 to the noise (to make
1450
1464
# it numerically stable)
1451
1465
if weights .dot (correlations ) > 1.0 :
1452
1466
noise_weight = np .array ([0 ])
1453
1467
# Calculate the noise weight
1454
1468
else :
1455
- noise_weight = np .asarray (np .sqrt (1 - weights .dot (correlations )))[0 ]
1469
+ noise_weight = np .asarray (np .sqrt (1.0 - weights .dot (correlations )))[0 ]
1456
1470
# Make sure the weights are positive, otherwise weight = 0.0
1457
1471
weights = np .where (weights < 0.0 , 0.0 , weights )[0 ]
1458
1472
# Finally, add the noise_weights to the weights variable.
@@ -1467,6 +1481,9 @@ def calculate_weights_spn(correlations, cov):
1467
1481
noise_weight = 1.0 - correlations
1468
1482
weights = np .concatenate ((correlations , noise_weight ), axis = 0 )
1469
1483
1484
+ # Make sure weights are always a real number
1485
+ weights = np .nan_to_num (weights , copy = True , nan = 10e-5 , posinf = 10e-5 , neginf = 10e-5 )
1486
+
1470
1487
return weights
1471
1488
1472
1489
0 commit comments