update
[morpheus.git] / reports / multistart.R
CommitLineData
1d014a86
BA
1library(morpheus)
2
86f257f8 3testMultistart <- function(N, n, p, beta, b, link, nstart, ncores)
0ad4c8de 4{
778a955e 5 res <- multiRun(
86f257f8 6 list(n=n, p=p, beta=beta, b=b, link=link, nstart=nstart),
0ad4c8de
BA
7 list(
8 function(fargs) {
9 # 1 start
10 library(morpheus)
86f257f8 11 K <- ncol(fargs$beta)
147b2ef3 12 mu <- computeMu(fargs$X, fargs$Y, list(K=K, M=fargs$M))
5af71d43 13 op <- optimParams(fargs$X, fargs$Y, K, fargs$link, fargs$M, 1)
7e8d970e 14 x_init <- list(p=rep(1/K,K-1), beta=mu, b=rep(0,K))
778a955e 15 res2 <- NULL
0ad4c8de 16 tryCatch({
778a955e 17 res2 <- do.call(rbind, op$run(x_init))
01bd5c5b 18 }, error = function(e) {})
778a955e 19 res2
0ad4c8de
BA
20 },
21 function(fargs) {
22 # B starts
23 library(morpheus)
86f257f8
BA
24 K <- ncol(fargs$beta)
25 d <- nrow(fargs$beta)
26 op <- optimParams(fargs$X, fargs$Y, K, fargs$link, fargs$M)
0ad4c8de
BA
27 best_val <- Inf
28 best_par <- list()
86f257f8 29 for (i in 1:fargs$nstart)
0ad4c8de
BA
30 {
31 #x_init <- list(p=rep(1/K,K-1), beta=i*fargs$mu, b=rep(0,K))
32 M <- matrix(rnorm(d*K), nrow=d, ncol=K)
33 M <- t(t(M) / sqrt(colSums(M^2)))
34 x_init <- list(p=rep(1/K,K-1), beta=M, b=rep(0,K))
01bd5c5b
BA
35 par <- NULL
36 tryCatch({
0ad4c8de 37 par <- op$run(x_init)
01bd5c5b
BA
38 }, error = function(e) {})
39 if (!is.null(par))
0ad4c8de
BA
40 {
41 val <- op$f( op$linArgs(par) )
42 if (val < best_val)
43 {
44 best_par <- par
45 best_val <- val
46 }
47 }
48 }
0e0fa9ff 49 do.call(rbind,best_par) #return NULL on empty list
0ad4c8de
BA
50 }
51 ),
52 prepareArgs = function(fargs, index) {
53 library(morpheus)
86f257f8
BA
54 io = generateSampleIO(fargs$n, fargs$p, fargs$beta, fargs$b, fargs$link)
55 fargs$M <- computeMoments(io$X, io$Y)
86f257f8
BA
56 fargs$X <- io$X
57 fargs$Y <- io$Y
01bd5c5b 58 fargs
0ad4c8de 59 }, N=N, ncores=ncores, verbose=TRUE)
0e0fa9ff
BA
60 p <- c(p, 1-sum(p))
61 for (i in 1:length(res)) {
62 for (j in N:1) {
63 if (is.null(res[[i]][[j]]) || is.na(res[[i]][[j]]))
64 res[[i]][[j]] <- NULL
65 }
66 print(paste("Count valid runs for ",i," = ",length(res[[i]]),sep=""))
e134ce3c 67 res[[i]] <- alignMatrices(res[[i]], ref=rbind(p,beta,b), ls_mode="exact")
0e0fa9ff 68 }
778a955e 69 res
0ad4c8de
BA
70}
71
1d014a86
BA
72# Default values:
73link = "logit"
0ad4c8de 74N <- 10
1d014a86
BA
75n <- 1e4
76ncores <- 1
77nstart <- 3 #nstart-1 random starting points for each MC run
78
79cmd_args <- commandArgs()
80for (arg in cmd_args)
81{
82 if (substr(arg,1,1)!='-')
83 {
84 spl <- strsplit(arg,'=')[[1]]
85 if (spl[1] == "nc") {
86 ncores <- as.integer(spl[2])
87 } else if (spl[1] == "N") {
88 N <- as.integer(spl[2])
89 } else if (spl[1] == "n") {
90 n <- as.integer(spl[2])
91 } else if (spl[1] == "d") {
92 d <- as.integer(spl[2])
93 } else if (spl[1] == "link") {
94 link <- spl[2]
95 } else if (spl[1] == "nstart") {
96 nstart <- spl[2]
97 }
98 }
99}
1d014a86 100
86f257f8
BA
101if (d == 2) {
102 p <- .5
103 b <- c(-.2, .5)
104 beta <- matrix( c(1,-2, 3,1), ncol=2 )
105} else if (d == 5) {
106 p <- .5
107 b <- c(-.2, .5)
108 beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=2 )
109} else if (d == 10) {
110 p <- c(.3, .3)
111 b <- c(-.2, 0, .5)
112 beta <- matrix( c(1,2,-1,0,3,4,-1,-3,0,2, 2,-3,0,1,0,-1,-4,3,2,0, -1,1,3,-1,0,0,2,0,1,-2), ncol=3 )
113}
114
115mr <- testMultistart(N, n, p, beta, b, link, nstart, ncores)
116mr_params <- list("N"=N, "nc"=ncores, "n"=n, "link"=link,
0ad4c8de 117 "p"=c(p,1-sum(p)), "beta"=beta, "b"=b, "nstart"=nstart)
1d014a86 118
778a955e 119save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,"_",nstart,".RData",sep=""))