Revert sample.R back to working code
[morpheus.git] / reports / multistart.R
... / ...
CommitLineData
1library(morpheus)
2
3testMultistart <- function(N, n, d, K, p, beta, b, link, nstart, ncores)
4{
5 res <- multiRun(
6 list(n=n,p=p,beta=beta,b=b,optargs=list(K=K,d=d,link=link,nstart=nstart)),
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))
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 <- fargs$optargs$K
24 d <- fargs$optargs$d
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))
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$optargs$link)
55 fargs$optargs$M <- computeMoments(io$X, io$Y)
56 mu <- computeMu(io$X, io$Y, fargs$optargs)
57 fargs$mu <- mu
58 fargs
59 }, N=N, ncores=ncores, verbose=TRUE)
60 for (i in 1:2)
61 res[[i]] <- alignMatrices(ms[[i]], ref=rbind(p,beta,b), ls_mode="exact")
62 res
63}
64
65#model = binomial
66K <- 2
67p <- .5
68b <- c(-.2, .5)
69# Default values:
70link = "logit"
71N <- 10
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
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,
106 "p"=c(p,1-sum(p)), "beta"=beta, "b"=b, "nstart"=nstart)
107
108save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,"_",nstart,".RData",sep=""))