Remove OpenMP dependency (and unused variable)
[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 mu <- computeMu(fargs$X, fargs$Y, list(K=K, M=fargs$M))
13 op <- optimParams(fargs$X, fargs$Y, K, fargs$link, fargs$M, 1)
14 x_init <- list(p=rep(1/K,K-1), beta=mu, b=rep(0,K))
15 res2 <- NULL
16 tryCatch({
17 res2 <- do.call(rbind, op$run(x_init))
18 }, error = function(e) {})
19 res2
20 },
21 function(fargs) {
22 # B starts
23 library(morpheus)
24 K <- ncol(fargs$beta)
25 d <- nrow(fargs$beta)
26 op <- optimParams(fargs$X, fargs$Y, K, fargs$link, fargs$M)
27 best_val <- Inf
28 best_par <- list()
29 for (i in 1:fargs$nstart)
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))
35 par <- NULL
36 tryCatch({
37 par <- op$run(x_init)
38 }, error = function(e) {})
39 if (!is.null(par))
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 }
49 do.call(rbind,best_par) #return NULL on empty list
50 }
51 ),
52 prepareArgs = function(fargs, index) {
53 library(morpheus)
54 io = generateSampleIO(fargs$n, fargs$p, fargs$beta, fargs$b, fargs$link)
55 fargs$M <- computeMoments(io$X, io$Y)
56 fargs$X <- io$X
57 fargs$Y <- io$Y
58 fargs
59 }, N=N, ncores=ncores, verbose=TRUE)
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=""))
67 res[[i]] <- alignMatrices(res[[i]], ref=rbind(p,beta,b), ls_mode="exact")
68 }
69 res
70 }
71
72 # Default values:
73 link = "logit"
74 N <- 10
75 n <- 1e4
76 ncores <- 1
77 nstart <- 3 #nstart-1 random starting points for each MC run
78
79 cmd_args <- commandArgs()
80 for (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 }
100
101 if (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
115 mr <- testMultistart(N, n, p, beta, b, link, nstart, ncores)
116 mr_params <- list("N"=N, "nc"=ncores, "n"=n, "link"=link,
117 "p"=c(p,1-sum(p)), "beta"=beta, "b"=b, "nstart"=nstart)
118
119 save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,"_",nstart,".RData",sep=""))