File tree Expand file tree Collapse file tree 1 file changed +34
-4
lines changed Expand file tree Collapse file tree 1 file changed +34
-4
lines changed Original file line number Diff line number Diff line change @@ -49,26 +49,56 @@ def two_gamma(timepoints):
49
49
return hrf
50
50
51
51
52
- def regress (Y , X ):
52
+
53
+ def regress (Y , X , l = 0. ):
54
+
53
55
'''
54
56
Parameters
55
57
----------
56
58
Y : floating point array (observations-by-outcomes)
57
59
outcome variables
58
60
X : floating pint array (observation-by-predictors)
59
61
predictors
62
+ l : float
63
+ (optional) ridge penalty parameter
60
64
61
65
Returns
62
66
-------
63
- floating point array (predictors-by-outcomes)
67
+ beta : floating point array (predictors-by-outcomes)
64
68
beta coefficients
65
69
'''
70
+
71
+ if X .ndim > 1 :
72
+ n_observations , n_predictors = X .shape
73
+
74
+ else :
75
+ n_observations = X .size
76
+ n_predictors = 1
66
77
67
- return np .matmul (
78
+
79
+ if n_observations < n_predictors :
80
+ U , D , V = np .linalg .svd (X , full_matrices = False )
81
+
82
+ D = np .diag (D )
83
+ beta = np .matmul (
84
+ np .matmul (
85
+ np .matmul (
86
+ np .matmul (
87
+ V .transpose (),
88
+ sc .linalg .inv (
89
+ D ** 2 +
90
+ l * np .eye (n_observations ))),
91
+ D ),
92
+ U .transpose ()), Y )
93
+ else :
94
+ beta = np .matmul (
68
95
np .matmul (
69
96
sc .linalg .inv (
70
- np .matmul (X .transpose (), X )),
97
+ np .matmul (X .transpose (), X ) +
98
+ l * np .eye (n_predictors )),
71
99
X .transpose ()), Y )
100
+
101
+ return beta
72
102
73
103
def correct_autocorr (X , W ):
74
104
'''
You can’t perform that action at this time.
0 commit comments