ergm
ergm copied to clipboard
Make SAN more efficient
There are several ways to make san()
more efficient:
- [x] 1. Autodetect when the annealing has "converged" for a given temperature and so should move to the next lower temperature.
- [ ] 2. A smoother annealing curve: currently, the temperature only changes between
C
calls. - [ ] 3. Keep track of the best network configuration so far, rather than the last one.
- [x] 4. Make sure the "covariance" calculation does not get stuck in a feedback loop. (I.e., those statistics that vary more get a smaller weight, which causes them to vary more, etc..
Many changes have been applied to SAN -- most important is this. Can this be closed now?
No, items 1-3 may still be worth doing.
@martinamorris , @chad-klumb , to elaborate on 1 as promised, a similar idea can be used as with tergm
: https://github.com/statnet/tergm/blob/616fdfd24ed292251e99c9aac7c4b62adcaab955/src/MCMCDyn.c#L408-L430 : when the trend towards improvement flatlines, we declare convergence and go to the next temperature.
@chad-klumb , @martinamorris , can I ask you to test and benchmark the latest statnet/ergm@adaptive_SAN ? (Note that some of the parameter defaults have changed.)
Running
require(ergm)
data(cohab)
set.seed(0)
net_size <- 50000
nw <- network.initialize(net_size, directed = FALSE)
inds <- sample(seq_len(NROW(cohab_PopWts)), net_size, TRUE, cohab_PopWts$weight)
set.vertex.attribute(nw, names(cohab_PopWts)[-1], cohab_PopWts[inds,-1])
astats <- NULL
rstats <- NULL
times <- list()
nrun <- 10
for(i in seq_len(nrun)) {
st <- Sys.time()
rv <- san(nw ~ edges + nodefactor("sex.ident", levels = 3) + nodecov("age") + nodecov("agesq") +
nodefactor("race", levels = -5) + nodefactor("othr.net.deg", levels = -1) +
nodematch("race", diff = TRUE) + absdiff("sqrt.age.adj"),
target.stats = cohab_TargetStats,
constraints = ~bd(maxout = 1) + blocks(attr = ~sex, levels2 = diag(TRUE, 2)) + strat(attr = ~race, pmat = cohab_MixMat),
control = list(SAN.nsteps = 1e6))
et <- Sys.time()
times[[i]] <- et - st
stats <- summary(rv ~ edges + nodefactor("sex.ident", levels = 3) + nodecov("age") + nodecov("agesq") +
nodefactor("race", levels = -5) + nodefactor("othr.net.deg", levels = -1) +
nodematch("race", diff = TRUE) + absdiff("sqrt.age.adj"))
astats <- rbind(astats, stats)
rstats <- rbind(rstats, (stats - cohab_TargetStats)/cohab_TargetStats)
}
with master
produces
> rstats
edges nodefactor.sex.ident.msmf nodecov.age nodecov.agesq nodefactor.race.B nodefactor.race.BI nodefactor.race.H nodefactor.race.HI nodefactor.othr.net.deg.1
[1,] 1.409872e-04 0.001961128 1.186024e-04 1.016840e-04 0.000938888 -0.0005712538 0.0005448477 0.0007406009 0.012535246
[2,] 1.409872e-04 0.001961128 1.297674e-04 1.200626e-04 0.000938888 -0.0005712538 0.0005448477 -0.0001273441 0.012535246
[3,] 1.409872e-04 0.001961128 1.148808e-04 9.421767e-05 0.000938888 -0.0005712538 0.0005448477 -0.0001273441 0.012535246
[4,] 7.747409e-05 0.001961128 6.649937e-05 5.845251e-05 0.000938888 -0.0005712538 0.0005448477 -0.0001273441 0.006878624
[5,] 2.045003e-04 0.001961128 1.772184e-04 1.563760e-04 0.000938888 -0.0005712538 0.0005448477 -0.0001273441 0.018191867
[6,] 2.045003e-04 0.001961128 1.660535e-04 1.354390e-04 0.000938888 -0.0005712538 0.0005448477 0.0007406009 0.018191867
[7,] 7.747409e-05 0.001961128 7.208184e-05 6.915595e-05 0.000938888 -0.0005712538 0.0005448477 -0.0001273441 0.006878624
[8,] 7.747409e-05 0.001961128 7.115143e-05 6.850330e-05 0.000938888 -0.0005712538 0.0005448477 -0.0001273441 0.006878624
[9,] 7.747409e-05 0.001961128 5.905607e-05 4.581722e-05 0.000938888 -0.0005712538 0.0005448477 -0.0001273441 0.012535246
[10,] 7.747409e-05 0.001961128 6.184731e-05 5.007249e-05 0.000938888 -0.0005712538 0.0005448477 -0.0001273441 0.006878624
nodematch.race.B nodematch.race.BI nodematch.race.H nodematch.race.HI nodematch.race.W absdiff.sqrt.age.adj
[1,] 0.0009370905 -0.0008655188 0.0002204319 0.0007462078 4.870113e-05 0.0002809130
[2,] 0.0009370905 -0.0008655188 0.0002204319 -0.0004595106 1.294936e-04 0.0003036064
[3,] 0.0009370905 -0.0008655188 0.0002204319 -0.0004595106 1.294936e-04 0.0003712218
[4,] 0.0009370905 -0.0008655188 0.0002204319 -0.0004595106 4.870113e-05 0.0003052223
[5,] 0.0009370905 -0.0008655188 0.0002204319 -0.0004595106 2.102860e-04 0.0004786536
[6,] 0.0009370905 -0.0008655188 0.0002204319 0.0007462078 1.294936e-04 0.0004674121
[7,] 0.0009370905 -0.0008655188 0.0002204319 -0.0004595106 4.870113e-05 0.0002606969
[8,] 0.0009370905 -0.0008655188 0.0002204319 -0.0004595106 4.870113e-05 0.0002144426
[9,] 0.0009370905 -0.0008655188 0.0002204319 -0.0004595106 4.870113e-05 0.0002740289
[10,] 0.0009370905 -0.0008655188 0.0002204319 -0.0004595106 4.870113e-05 0.0002384881
> times
[[1]]
Time difference of 2.551823 secs
[[2]]
Time difference of 2.172043 secs
[[3]]
Time difference of 2.168547 secs
[[4]]
Time difference of 2.15673 secs
[[5]]
Time difference of 2.195569 secs
[[6]]
Time difference of 2.172992 secs
[[7]]
Time difference of 2.312782 secs
[[8]]
Time difference of 2.24275 secs
[[9]]
Time difference of 2.328353 secs
[[10]]
Time difference of 2.187537 secs
and with adaptive_SAN
produces
> rstats
edges nodefactor.sex.ident.msmf nodecov.age nodecov.agesq nodefactor.race.B nodefactor.race.BI nodefactor.race.H nodefactor.race.HI nodefactor.othr.net.deg.1
[1,] -3.671177e-04 0.05762564 -0.002708920 -0.004645345 -0.0002050422 0.0033075124 -0.0008035955 0.0007406009 1.206082
[2,] 6.490921e-04 0.03907080 -0.002402814 -0.004913898 -0.0002050422 0.0020145903 0.0005448477 0.0007406009 1.511540
[3,] -4.955213e-05 0.05762564 -0.002576801 -0.004645763 -0.0024929025 0.0007216683 0.0005448477 0.0011745734 1.307902
[4,] 9.666577e-04 0.04834822 -0.001263060 -0.003073140 -0.0013489724 0.0046004344 0.0005448477 0.0011745734 1.234365
[5,] 2.045003e-04 0.03907080 -0.002661469 -0.004994722 -0.0024929025 -0.0005712538 0.0018932909 0.0020425184 1.471944
[6,] 9.666577e-04 0.03907080 -0.001994363 -0.004390708 0.0032267483 0.0046004344 -0.0021520387 -0.0009952892 1.545480
[7,] 4.585528e-04 0.05762564 -0.002087405 -0.004205512 -0.0002050422 -0.0057429420 0.0005448477 -0.0001273441 1.313558
[8,] 3.950397e-04 0.06690305 -0.002158116 -0.004265452 0.0020828181 -0.0005712538 -0.0008035955 -0.0009952892 1.341841
[9,] -1.130652e-04 0.03907080 -0.002368389 -0.004194443 0.0032267483 -0.0031570979 -0.0014778171 0.0011745734 1.149516
[10,] 2.680134e-04 0.05762564 -0.002052979 -0.003945941 -0.0013489724 -0.0044500199 0.0012190693 0.0011745734 1.336185
nodematch.race.B nodematch.race.BI nodematch.race.H nodematch.race.HI nodematch.race.W absdiff.sqrt.age.adj
[1,] -0.0026894932 0.0029190815 0.0002204319 0.0019519261 -0.0005168459 0.02486426
[2,] 0.0009370905 0.0029190815 0.0046073637 0.0019519261 0.0006950406 0.03245800
[3,] -0.0026894932 0.0029190815 0.0046073637 0.0019519261 -0.0001128837 0.02723278
[4,] 0.0009370905 0.0067036818 -0.0041664998 0.0007462078 0.0009374179 0.02650023
[5,] -0.0026894932 -0.0008655188 0.0046073637 0.0031576444 0.0001294936 0.03132467
[6,] 0.0045636742 0.0067036818 -0.0041664998 -0.0016652289 0.0012605876 0.03557270
[7,] 0.0009370905 -0.0084347194 0.0002204319 0.0007462078 0.0007758330 0.02713985
[8,] 0.0045636742 -0.0008655188 0.0002204319 -0.0016652289 0.0004526633 0.02883330
[9,] 0.0045636742 -0.0046501191 -0.0041664998 0.0007462078 -0.0001936762 0.02524625
[10,] -0.0026894932 -0.0046501191 0.0046073637 0.0019519261 0.0003718708 0.02829383
> times
[[1]]
Time difference of 2.609514 secs
[[2]]
Time difference of 2.125356 secs
[[3]]
Time difference of 2.299301 secs
[[4]]
Time difference of 2.146126 secs
[[5]]
Time difference of 2.140812 secs
[[6]]
Time difference of 2.154998 secs
[[7]]
Time difference of 2.234803 secs
[[8]]
Time difference of 2.124712 secs
[[9]]
Time difference of 2.14153 secs
[[10]]
Time difference of 2.266072 secs
so times appear similar but stat match is better with the non-adaptive code.