-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauxiliary_functions.R
More file actions
56 lines (40 loc) · 1.13 KB
/
Copy pathauxiliary_functions.R
File metadata and controls
56 lines (40 loc) · 1.13 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
eye <- function(n){
return(diag(rep(1,n)))
}
nz_vals <- function(A,tol=1e-8) {A[upper.tri(A) & abs(A) > tol]}
nz_count <- function(A,tol=1e-8) {length(nz_vals(A,tol))}
lseq <- function(from, to, length.out) exp(seq(log(from), log(to), length.out=length.out))
projsplx = function(v, b=1){
v[v<0] = 0
u = sort(v, decreasing = TRUE)
sv = cumsum(u)
rho = which(u > (sv-b) / (1:length(v)))
theta = max(0, (sv[rho] - b) / rho )
w = v - theta
w[w<0] = 0
return(w)
}
laplacian <- function(A){
return(diag(apply(A, 1, sum)) - A)
}
reg_laplacian <- function(A){
N = nrow(A)
diag(A) <- 0
diagonalA <-apply(A, 1, FUN = function(x){ifelse(sum(x) > 10^(-5), 1/sqrt(sum(x)), 0)})
diagonalA <- diag(diagonalA)
return(eye(N) - diagonalA %*% A %*% diagonalA)
}
make_correlation <- function(A){
diagonalA <- diag(sqrt(1/diag(A)))
temp = -diagonalA %*% A %*% diagonalA
diag(temp) <- 1
return(temp)
}
covar2cor <- function(Sigma){
d = diag(1/sqrt(diag(Sigma)))
return(d%*%Sigma%*%d)
}
covar2invcor <- function(Sigma){
d = diag(1/sqrt(diag(Sigma)))
return(solve(d%*%Sigma%*%d))
}