Advertisement
BenjaminLind

Model parameters in Forest Fire Game (igraph)

Aug 25th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.99 KB | None | 0 0
  1. library(igraph)
  2. library(compiler)
  3.  
  4. ffg.optim<-function(g, sample.size=100, ambassadors=1, multicore=TRUE, init.fw=.5, init.bw=1, hess=TRUE, ...){
  5.   #_g_ is an igraph 'graph' object.
  6.     #If _g_ has multiple edges and/or loops, they will be removed.
  7.   #_sample.size_ is how many simulations to perform for each optimization iteration
  8.     #Larger values increase the accuracy, yet decrease the speed.
  9.   #_ambassadors_ is the ambassadors parameter for forest.fire.game().
  10.     #It must be an integer greater than or equal to 1.
  11.     #optim has a hard time estimating it.
  12.   #Set _multicore_ to TRUE if you have this package installed and would like a speed boost.
  13.   #_init.fw_ and _init.bw_ are the initial guesses for the forward and backward burning parameters.
  14.   #_hess_ is to set the hessian logical argument in optim. Defaults to TRUE.
  15.   #_..._ are other arguments to pass to optim.  E.g., hessian=TRUE will provide a hessian matrix for the estimate.
  16.   require(igraph)
  17.   if (is.simple(g)==FALSE)
  18.     g<-simplify(g)
  19.   ambassadors<-round(ambassadors)
  20.   if (ambassadors<1){
  21.     ambassadors<-1
  22.     cat("\n  WARNING: ambassadors must be greater than or equal to one. Setting ambassadors=1 to correct user error.\n")
  23.     }
  24.   obs.stats<-function(the.g){
  25.     dens<-graph.density(the.g)
  26.     apl<-average.path.length(the.g, directed=FALSE)
  27.     gtrans<-transitivity(the.g)
  28.     return(c(density=dens, average.path.length=apl, transitivity=gtrans))
  29.     }
  30.   serr<-function(test.vec, obs) #Squared error function
  31.     return(((test.vec-obs)/obs)^2)
  32.   obs<-obs.stats(g) #Target observations
  33.  
  34.   min.this<-function(test.mat){ #Sum of squared errors
  35.     a<-sapply(1:length(obs), function(x) return(serr(test.mat[,x], obs[x])))
  36.     return(sqrt(sum(a)))}
  37.   n<-vcount(g)
  38.  
  39.   fun2optimize<-function(params){
  40.     fw<-params["fw"]; bw<-params["bw"]
  41.     test.vals.wrap<-function(){ #This wrapper saves memory, as the simulations are never stored.
  42.       temp.g<-forest.fire.game(nodes=n, fw.prob=fw, bw.factor=bw, ambs=ambassadors, directed=is.directed(g))
  43.       temp.g.obs<-obs.stats(temp.g)
  44.       return(temp.g.obs)
  45.       }
  46.     if (multicore&("multicore"%in%installed.packages())){
  47.       require(multicore)
  48.       test.vals<-do.call(rbind, mclapply(1:sample.size, function(x) return(test.vals.wrap())))
  49.       }
  50.     else
  51.       test.vals<-do.call(rbind, lapply(1:sample.size, function(x) return(test.vals.wrap())))
  52.     return(min.this(test.vals))
  53.     }
  54.   out.optim<-optim(par=c(fw=init.fw, bw=init.bw), fn=fun2optimize, method="L-BFGS-B", lower=c(fw=.000000001, bw=.000000001), upper=c(fw=1, bw=Inf), hessian=hess, ...)
  55.   return(out.optim)
  56.   }; ffg.optim<-cmpfun(ffg.optim)
  57. g<-forest.fire.game(100, .1, 3, ambs=1, directed=TRUE)
  58. g.fit<-ffg.optim(g, 5000, init.fw=.3, init.bw=1, multicore=TRUE, control=list(maxit=10000, lmm=10))
  59. #g.fit$par is usually quite off and the standard errors, sqrt(diag(solve(-g.fit$hessian))), produce NaNs due to non-invertibility of hessian matrix (positive values)
  60. #The target stats are usually good, though.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement