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