Fix typo
[morpheus.git] / reports / multistart.R
CommitLineData
1d014a86
BA
1library(morpheus)
2
01bd5c5b 3testMultistart <- function(N, n, d, K, p, beta, b, link, nstart, ncores)
0ad4c8de 4{
778a955e 5 res <- multiRun(
01bd5c5b 6 list(n=n,p=p,beta=beta,b=b,optargs=list(K=K,d=d,link=link,nstart=nstart)),
0ad4c8de
BA
7 list(
8 function(fargs) {
9 # 1 start
10 library(morpheus)
11 K <- fargs$optargs$K
12 op <- optimParams(K, fargs$optargs$link, fargs$optargs)
13 x_init <- list(p=rep(1/K,K-1), beta=fargs$mu, b=rep(0,K))
778a955e 14 res2 <- NULL
0ad4c8de 15 tryCatch({
778a955e 16 res2 <- do.call(rbind, op$run(x_init))
01bd5c5b 17 }, error = function(e) {})
778a955e 18 res2
0ad4c8de
BA
19 },
20 function(fargs) {
21 # B starts
22 library(morpheus)
23 K <- fargs$optargs$K
01bd5c5b 24 d <- fargs$optargs$d
0ad4c8de
BA
25 op <- optimParams(K, fargs$optargs$link, fargs$optargs)
26 best_val <- Inf
27 best_par <- list()
28 for (i in 1:fargs$optargs$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))
01bd5c5b
BA
34 par <- NULL
35 tryCatch({
0ad4c8de 36 par <- op$run(x_init)
01bd5c5b
BA
37 }, error = function(e) {})
38 if (!is.null(par))
0ad4c8de
BA
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$optargs$link)
55 fargs$optargs$M <- computeMoments(io$X, io$Y)
56 mu <- computeMu(io$X, io$Y, fargs$optargs)
57 fargs$mu <- mu
01bd5c5b 58 fargs
0ad4c8de
BA
59 }, N=N, ncores=ncores, verbose=TRUE)
60 for (i in 1:2)
e134ce3c 61 res[[i]] <- alignMatrices(res[[i]], ref=rbind(p,beta,b), ls_mode="exact")
778a955e 62 res
0ad4c8de
BA
63}
64
1d014a86
BA
65#model = binomial
66K <- 2
67p <- .5
68b <- c(-.2, .5)
69# Default values:
70link = "logit"
0ad4c8de 71N <- 10
1d014a86
BA
72d <- 2
73n <- 1e4
74ncores <- 1
75nstart <- 3 #nstart-1 random starting points for each MC run
76
77cmd_args <- commandArgs()
78for (arg in cmd_args)
79{
80 if (substr(arg,1,1)!='-')
81 {
82 spl <- strsplit(arg,'=')[[1]]
83 if (spl[1] == "nc") {
84 ncores <- as.integer(spl[2])
85 } else if (spl[1] == "N") {
86 N <- as.integer(spl[2])
87 } else if (spl[1] == "n") {
88 n <- as.integer(spl[2])
89 } else if (spl[1] == "d") {
90 d <- as.integer(spl[2])
91 } else if (spl[1] == "link") {
92 link <- spl[2]
93 } else if (spl[1] == "nstart") {
94 nstart <- spl[2]
95 }
96 }
97}
98betas <- list(
99 matrix( c(1,-2, 3,1), ncol=K ), #d=2
100 matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=K ), #d=5
101 matrix( c(1,2,-1,0,3,4,-1,-3,0,2, 2,-3,0,1,0,-1,-4,3,2,0), ncol=K ) ) #d=10
102beta <- betas[[ ifelse( d==2, 1, ifelse(d==5,2,3) ) ]]
103
778a955e
BA
104mr <- testMultistart(N, n, d, K, p, beta, b, link, nstart, ncores)
105mr_params <- list("N"=N, "nc"=ncores, "n"=n, "K"=K, "d"=d, "link"=link,
0ad4c8de 106 "p"=c(p,1-sum(p)), "beta"=beta, "b"=b, "nstart"=nstart)
1d014a86 107
778a955e 108save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,"_",nstart,".RData",sep=""))