-
Notifications
You must be signed in to change notification settings - Fork 0
/
beta_to_tscore.py
41 lines (32 loc) · 1.28 KB
/
beta_to_tscore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
def beta_to_tscore(X, Y, beta):
"""
Converts a beta map to a t-score map for simple contrast of one beta map.
This assumes that the contrast vector c is a standard basis vector, one c for each beta.
It also assumes all columns of X are full column rank.
Parameters:
X : numpy.ndarray
Design matrix (n x p)
Y : numpy.ndarray
Dependent variable matrix (n x m)
beta : numpy.ndarray
Estimated coefficients (p x m)
Returns:
t_scores : numpy.ndarray
T-scores for the betas (p x m)
"""
n, p = X.shape # Number of observations, Number of predictors
m = Y.shape[1] # Number of dependent variables
# Compute the residuals
residuals = Y - X @ beta
# Estimate the residual variance for each column of Y
residual_variance = np.sum(residuals**2, axis=0) / (n - p)
# Compute the variance-covariance matrix for betas
invXTX = np.linalg.inv(X.T @ X)
SE = np.sqrt(np.diag(invXTX)[:, np.newaxis] * residual_variance)
cond_num = np.linalg.cond(X.T @ X)
if cond_num > 1e15: # or some other large threshold
print(f"Warning: X'X is ill-conditioned. Condition number: {cond_num}")
# Compute t-scores for each column of beta
t_scores = beta / SE
return t_scores