This package can be used to sample states from the Ising model and compute the probability of states. Sampling can be done for any number of nodes, but due to the intractibility of the Ising model the distribution can only be computed up to ~10 nodes.

Package: | IsingSampler |

Type: | Package |

Version: | 1.0 |

Date: | 2013-08-02 |

License: | What license is it under? |

Sacha Epskamp

Maintainer: Sacha Epskamp <mail@sachaepskamp.com>

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | ```
## This code compares the different sampling algorithms to the expected
## distribution of states in a tractible number of nodes.
## In the end are examples on how to obtain the distribution.
# Input:
N <- 5 # Number of nodes
nSample <- 5000 # Number of samples
# Ising parameters:
Graph <- matrix(sample(0:1,N^2,TRUE,prob = c(0.7, 0.3)),N,N) * rnorm(N^2)
Graph <- pmax(Graph,t(Graph)) / N
diag(Graph) <- 0
Thresh <- -(rnorm(N)^2)
Beta <- 1
# Response options (0,1 or -1,1):
Resp <- c(0L,1L)
# All posible states:
AllStates <- do.call(expand.grid,lapply(1:N,function(x)Resp))
# Simulate with metropolis:
MetData <- IsingSampler(nSample, Graph, Thresh, Beta, 1000/N,
responses = Resp, method = "MH")
# Simulate exact samples (CFTP):
ExData <- IsingSampler(nSample, Graph, Thresh, Beta, 100,
responses = Resp, method = "CFTP")
# Direct simulation:
DirectData <- IsingSampler(nSample, Graph, Thresh, Beta, method = "direct")
# State distirbutions:
MetDist <- apply(AllStates,1,function(x)sum(colSums(t(MetData) == x)==N))
ExDist <- apply(AllStates,1,function(x)sum(colSums(t(ExData) == x)==N))
DirectDist <- apply(AllStates,1,function(x)sum(colSums(t(DirectData) == x)==N))
ExpDist <- exp(- Beta * apply(AllStates,1,function(s)IsingSampler:::H(Graph,s,Thresh)))
ExpDist <- ExpDist/sum(ExpDist) * nSample
## Plot to compare distributions:
plot(MetDist, type="l", col="blue", pch=16, xlab="State", ylab="Freq",
ylim=c(0,max(MetDist,DirectDist,ExDist)))
points(DirectDist,type="l",col="red",pch=16)
points(ExpDist,type="l",col="green",pch=16)
points(ExDist,type="l",col="purple",pch=16)
legend("topright", col=c("blue","red","purple","green"),
legend=c("Metropolis","Direct","Exact","Expected"),lty=1,bty='n')
## Likelihoods:
# Sumscores:
IsingSumLikelihood(Graph, Thresh, Beta, Resp)
# All states:
IsingLikelihood(Graph, Thresh, Beta, Resp)
# Single state:
IsingStateProb(rep(Resp[1],N),Graph, Thresh, Beta, Resp)
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

All documentation is copyright its authors; we didn't write any of that.