forked from docteurZ/cohsmix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulations.R
More file actions
executable file
·130 lines (119 loc) · 3.5 KB
/
simulations.R
File metadata and controls
executable file
·130 lines (119 loc) · 3.5 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
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
## __________________________________________________________
##
## class.ind
##
## ----------------------------------------------------------
class.ind<-function (cl) {
n <- length(cl);
cl <- as.factor(cl);
x <- matrix(0, n, length(levels(cl)));
x[(1:n) + n * (unclass(cl) - 1)] <- 1;
dimnames(x) <- list(names(cl), levels(cl));
return(x);
}
## __________________________________________________________
##
## graph.affiliation
## warning : could be faster.
##
## INPUT n: number of vertex
## alphaVect: vecteur of class proportion
## lambda: proba of edge given same classe
## epsilon: proba of edge given two different classes
## OUTPUT x: adjacency matrix
## cluster: class vector
## ----------------------------------------------------------
graph.affiliation<-function(n=100,alphaVect=c(1/2,1/2),lambda=0.7,epsilon=0.05) {
x <- matrix(0,n,n);
Q <- length(alphaVect);
rmultinom(1, size=n, prob = alphaVect)->nq;
Z <- class.ind(rep(1:Q,nq));
Z <- Z[sample(1:n,n),];
for (i in 1:n) {
for (j in i:n) {
# if i and j in same class
if (which.max(Z[i,]) == which.max(Z[j,])) p<-lambda else p<-epsilon
if ((rbinom(1,1,p))&(i != j)) {
x[i,j]<-1;
x[j,i]<-1;
}
}
}
return(list(x=x,cluster=apply(Z,1,which.max)) )
}
## __________________________________________________________
##
## CreateMu
##
## INPUT: num.classes: Number of classes
## Mu1: Mean for the nodes belonging to the same cluster
## Mu2: Mean for the nodes belonging to different cluster
##
## OUTPUT: Mu: Matrix of the means for the variable Y
##
## __________________________________________________________
CreateMu <- function(num.classes, Mu1, Mu2) {
size <- num.classes;
Mu <- matrix( Mu2, nrow=size, ncol=size);
diag(Mu) <- Mu1;
return(Mu);
}
## __________________________________________________________
##
## SimDataYcondZ
##
## INPUT: node.classes: Vector of node class labels
## MU: Matrix of the means
## Sigma: Standard Error
##
## OUTPUT: Y: Similarity matrix
##
## Simulate a similarity matrix conditionally to Z
## __________________________________________________________
SimDataYcondZ <- function(node.classes, Mu, Sigma, SelfLoop = FALSE) {
num.nodes <- length (node.classes);
Y <- matrix(0, num.nodes, num.nodes);
for (i in 1:num.nodes) {
for (j in 1:i) {
Y[i,j] <- rnorm(1,Mu[ node.classes[i], node.classes[j]], Sigma)
Y[j,i] <- Y[i,j]
}
if (SelfLoop == FALSE){
Y[i,i] <- 0;
}
}
return (Y);
}
## __________________________________________________________
##
## SimDataYcondXZ
##
## INPUT: node.classes: Vector of node class labels
## X: Adjency matrix
## MuX0: Matrix of the means (case X=0)
## MuX1: Matrix of the means (case X=1)
## Sigma: Variance
##
## OUTPUT: Y: Similarity matrix
##
## Simulate a similarity matrix conditionally to X and Z
## __________________________________________________________
SimDataYcondXZ <- function(node.classes, X, MuX0, MuX1, Sigma, SelfLoop = FALSE) {
num.nodes <- length (node.classes);
Y <- matrix(0, num.nodes, num.nodes);
for (i in 1:num.nodes) {
for (j in 1:i) {
if (X[i,j] ==0 ){
Y[i,j] <- rnorm(1,MuX0[ node.classes[i], node.classes[j]], Sigma);
Y[j,i] <- Y[i,j] ;
} else {
Y[i,j] <- rnorm(1,MuX1[node.classes[i], node.classes[j]], Sigma);
Y[j,i] <- Y[i,j] ;
}
}
if (SelfLoop == FALSE){
Y[i,i] = 0;
}
}
return (Y);
}