7cc0a5e4cc5c02e42a3c28ec68b1eb87ca374ec4
[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 # Bet that at least one run succeded:
49 do.call(rbind,best_par)
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 mu <- computeMu(io$X, io$Y, list(M=fargs$M))
57 fargs$mu <- mu
58 fargs$X <- io$X
59 fargs$Y <- io$Y
60 fargs
61 }, N=N, ncores=ncores, verbose=TRUE)
62 for (i in 1:2)
63 res[[i]] <- alignMatrices(res[[i]], ref=rbind(p,beta,b), ls_mode="exact")
64 res
65 }
66
67 # Default values:
68 link = "logit"
69 N <- 10
70 n <- 1e4
71 ncores <- 1
72 nstart <- 3 #nstart-1 random starting points for each MC run
73
74 cmd_args <- commandArgs()
75 for (arg in cmd_args)
76 {
77 if (substr(arg,1,1)!='-')
78 {
79 spl <- strsplit(arg,'=')[[1]]
80 if (spl[1] == "nc") {
81 ncores <- as.integer(spl[2])
82 } else if (spl[1] == "N") {
83 N <- as.integer(spl[2])
84 } else if (spl[1] == "n") {
85 n <- as.integer(spl[2])
86 } else if (spl[1] == "d") {
87 d <- as.integer(spl[2])
88 } else if (spl[1] == "link") {
89 link <- spl[2]
90 } else if (spl[1] == "nstart") {
91 nstart <- spl[2]
92 }
93 }
94 }
95
96 if (d == 2) {
97 p <- .5
98 b <- c(-.2, .5)
99 beta <- matrix( c(1,-2, 3,1), ncol=2 )
100 } else if (d == 5) {
101 p <- .5
102 b <- c(-.2, .5)
103 beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=2 )
104 } else if (d == 10) {
105 p <- c(.3, .3)
106 b <- c(-.2, 0, .5)
107 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 )
108 }
109
110 mr <- testMultistart(N, n, p, beta, b, link, nstart, ncores)
111 mr_params <- list("N"=N, "nc"=ncores, "n"=n, "link"=link,
112 "p"=c(p,1-sum(p)), "beta"=beta, "b"=b, "nstart"=nstart)
113
114 save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,"_",nstart,".RData",sep=""))