-
Notifications
You must be signed in to change notification settings - Fork 0
/
covariation_matrix.R
157 lines (152 loc) · 5.6 KB
/
covariation_matrix.R
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#' Co-variation matrices
#'
#' Construction of model-preserving co-variation matrices for objects of class \code{CI}.
#'
#' Functions to compute total, partial, row-based and column-based co-variation matrices to ensure the conditional independences of the original Bayesian network hold after a variation. If no co-variation is required for model-preservation the functions return a matrix filled with ones (no co-variation).
#'
#'@return A co-variation matrix of the same size of the covariance matrix of \code{CI}.
#'
#'@examples total_covar_matrix(synthetic_ci,c(1,1),0.3)
#'@examples total_covar_matrix(synthetic_ci,c(1,2),0.3)
#'@examples partial_covar_matrix(synthetic_ci,c(1,2),0.3)
#'@examples row_covar_matrix(synthetic_ci,c(1,2),0.3)
#'@examples col_covar_matrix(synthetic_ci,c(1,2),0.3)
#'
#' @seealso \code{\link{model_pres_cov}}
#'@param ci object of class \code{CI}.
#'@param entry a vector of length two specifying the entry of the covariance matrix to vary.
#'@param delta multiplicative variation coefficient for the entry of the covariance matrix given in \code{entry}.
#'
#'@references C. Görgen & M. Leonelli (2020), Model-preserving sensitivity analysis for families of Gaussian distributions. Journal of Machine Learning Research, 21: 1-32.
#' @name covariation_matrix
NULL
#' @rdname covariation_matrix
#'
#' @export
total_covar_matrix <- function(ci,entry,delta){
simpleci<- simple_ci(ci)
submatrix <- ci_submatrix(simpleci)
rows <- unique(c(submatrix$A,submatrix$C))
cols <- unique(c(submatrix$B,submatrix$C))
ind <- integer(0)
if(ci$order[entry[1]] %in% rows){ind <- which(cols == ci$order[entry[2]])}
if(ci$order[entry[2]] %in% rows){ind <- c(ind,which(cols == ci$order[entry[1]]))}
ind <- unique(ind)
if(identical(ind,integer(0))){return(matrix(1,nrow = nrow(ci$covariance), ncol = ncol(ci$covariance)))}else{
covar <- matrix(delta,nrow = nrow(ci$covariance), ncol = ncol(ci$covariance))
covar[entry[1],entry[2]] <- 1
covar[entry[2],entry[1]] <- 1
return(covar)}
}
#' @rdname covariation_matrix
#'
#' @export
col_covar_matrix <- function(ci,entry,delta){
covar <- matrix(1,nrow = nrow(ci$covariance), ncol = ncol(ci$covariance))
simpleci<- simple_ci(ci)
submatrix <- ci_submatrix(simpleci)
rows <- unique(c(submatrix$A,submatrix$C))
cols <- unique(c(submatrix$B,submatrix$C))
temp <- matrix(1,nrow = length(rows), ncol = length(cols))
ind <- integer(0)
if(ci$order[entry[1]] %in% rows){ind <- which(cols == ci$order[entry[2]])}
if(ci$order[entry[2]] %in% rows){ind <- c(ind,which(cols == ci$order[entry[1]]))}
ind <- unique(ind)
if(identical(ind,integer(0))){return(covar)}else{
while(length(ind)>0){
for(k in 1:length(ind)){
for (i in 1:length(rows)){
temp[i,ind[k]] <- delta
if(rows[i] %in% cols & cols[ind[k]] %in% rows) {temp[which(cols[ind[k]]==rows),which(cols == rows[i])] <- delta}
}
}
check <- c()
ind <- c()
for (i in 1: length(cols)){
check[i] <- sum(temp[,i]== delta)
if(check[i] > 0 & check[i] < length(rows)){ind <- c(ind,i)}
}
}
for(i in 1:nrow(covar)){
for(j in 1:ncol(covar)){
ind <- c(which(ci$order[i]==rows),which(ci$order[j]==cols))
if(length(ind) == 2){
if(temp[ind[1],ind[2]] == delta){
covar[i,j] <- delta; covar[j,i] <- delta
}
}
}
}
covar[entry[1],entry[2]] <- 1
covar[entry[2],entry[1]] <- 1
return(covar)}
}
#' @rdname covariation_matrix
#'
#' @export
partial_covar_matrix <- function(ci, entry, delta){
covar <- matrix(1,nrow = nrow(ci$covariance), ncol = ncol(ci$covariance))
simpleci<- simple_ci(ci)
submatrix <- ci_submatrix(simpleci)
rows <- unique(c(submatrix$A,submatrix$C))
cols <- unique(c(submatrix$B,submatrix$C))
ind <- integer(0)
if(ci$order[entry[1]] %in% rows){ind <- which(cols == ci$order[entry[2]])}
if(ci$order[entry[2]] %in% rows){ind <- c(ind,which(cols == ci$order[entry[1]]))}
ind <- unique(ind)
if(identical(ind,integer(0))){return(covar)}else{
for(i in 1:length(ci$order)){
for(j in 1:length(ci$order)){
if(ci$order[i] %in% rows & ci$order[j] %in% cols){
covar[i,j] <- delta
covar[j,i] <- delta
}
}
}
covar[entry[1],entry[2]] <- 1
covar[entry[2],entry[1]] <- 1
return(covar)}
}
#' @rdname covariation_matrix
#'
#' @export
row_covar_matrix <- function(ci, entry, delta){
covar <- matrix(1,nrow = nrow(ci$covariance), ncol = ncol(ci$covariance))
simpleci<- simple_ci(ci)
submatrix <- ci_submatrix(simpleci)
rows <- unique(c(submatrix$A,submatrix$C))
cols <- unique(c(submatrix$B,submatrix$C))
temp <- matrix(1,nrow = length(rows), ncol = length(cols))
ind <- integer(0)
if(ci$order[entry[1]] %in% cols){ind <- which(rows == ci$order[entry[2]])}
if(ci$order[entry[2]] %in% cols){ind <- c(ind,which(rows == ci$order[entry[1]]))}
ind <- unique(ind)
if(identical(ind,integer(0))){return(covar)}else{
while(length(ind)>0){
for(k in 1:length(ind)){
for (i in 1:length(cols)){
temp[ind[k],i] <- delta
if(cols[i] %in% rows & rows[ind[k]] %in% cols) {temp[which(rows == cols[i]),which(rows[ind[k]]==cols)] <- delta}
}
}
check <- c()
ind <- c()
for (i in 1: length(rows)){
check[i] <- sum(temp[i,]== delta)
if(check[i] > 0 & check[i] < length(cols)){ind <- c(ind,i)}
}
}
for(i in 1:nrow(covar)){
for(j in 1:ncol(covar)){
ind <- c(which(ci$order[i]==rows),which(ci$order[j]==cols))
if(length(ind) == 2){
if(temp[ind[1],ind[2]] == delta){
covar[i,j] <- delta; covar[j,i] <- delta
}
}
}
}
covar[entry[1],entry[2]] <- 1
covar[entry[2],entry[1]] <- 1
return(covar)}
}