Ching-Chuan Chen's Blogger

Statistics, Machine Learning and Programming

0%

Shiny App for Ising Model

Here is a demonstration for ising model with an interative interface created by R package shiny.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
library(lattice)
library(shiny)
app = shinyApp(
ui = shinyUI(pageWithSidebar(
headerPanel('Ising Model'),
sidebarPanel(
sliderInput('girdSize', 'Gird Size', 2, 200, 30),
selectInput('updateAlgorithm', 'Algorithm',
c("Metropolis-Hasting Algorithm", "Gibbs sampling"),
"Metropolis-Hasting Algorithm"),
numericInput('iteration', 'Iteration', 50000, 1, 4000000),
numericInput('updateFreq', 'Draw Model Every N Iteration', 1000, 1, 10000),
sliderInput('temperature', 'Reciprocal of Temperature', -5, 5, 2, step = 0.1),
actionButton('reset', 'Reset'),
actionButton('stop', 'Stop'),
actionButton('start', 'Start')
),
mainPanel(
h3(textOutput("currentIteration")),
plotOutput('IsingPlot', width = "600px", height = "600px")
)
)),
server = function(input, output, session) {
vals = reactiveValues()
range_f = function(X, loc) c(X[loc[1], c(loc[2]-1, loc[2]+1)], X[c(loc[1]-1, loc[1]+1), loc[2]])
resetIsingMatrix = observe({
input$reset
runProcess$suspend()
isolate({
vals$IsingMatrix = replicate(input$girdSize+2, rbinom(input$girdSize+2, 1, .5))
vals$IsingMatrix[c(1, input$girdSize+2),] = vals$IsingMatrix[c(input$girdSize+1, 2),]
vals$IsingMatrix[,c(1, input$girdSize+2)] = vals$IsingMatrix[,c(input$girdSize+1, 2)]
})
}, priority=30)

setup_to_run = observe({
input$start
isolate({
if (is.null(vals$IsingMatrix))
{
vals$IsingMatrix = replicate(input$girdSize+2, rbinom(input$girdSize+2, 1, .5))
vals$IsingMatrix[c(1, input$girdSize+2),] = vals$IsingMatrix[c(input$girdSize+1, 2),]
vals$IsingMatrix[,c(1, input$girdSize+2)] = vals$IsingMatrix[,c(input$girdSize+1, 2)]
}
if (input$updateAlgorithm == "Metropolis-Hasting Algorithm")
{
vals$algo_f = function(mat, temperature){
N = nrow(mat) - 2
loc = floor(N*runif(2)) + 2
if (runif(1) < exp(2*(2-sum(range_f(mat, loc) == mat[loc[1], loc[2]]))*temperature))
mat[loc[1],loc[2]] = 1 - mat[loc[1],loc[2]]
mat[c(1, N+2),] = mat[c(N+1, 2),]
mat[,c(1, N+2)] = mat[,c(N+1, 2)]
mat
}
} else
{
vals$algo_f = function(mat, temperature){
N = nrow(mat) - 2
loc = floor(N*runif(2)) + 2
S = 2-sum(range_f(mat, loc) == mat[loc[1], loc[2]])
if (runif(1) < 1/(exp(-S*temperature)**2+1))
mat[loc[1],loc[2]] = 1 - mat[loc[1],loc[2]]
mat[c(1, N+2),] = mat[c(N+1, 2),]
mat[,c(1, N+2)] = mat[,c(N+1, 2)]
mat
}
}
vals$iteration = input$iteration
vals$updateFreq = input$updateFreq
vals$temperature = input$temperature
vals$iter = 0
})
runProcess$resume()
}, priority=20)

runProcess = observe({
if (input$start == 0) return()
isolate({
result = vals$IsingMatrix
i = 0
while (i < vals$updateFreq)
{
result = vals$algo_f(result, vals$temperature)
i = i + 1
}
vals$IsingMatrix = result
vals$iter = vals$iter + vals$updateFreq
})
if (isolate(vals$iter) < isolate(vals$iteration))
invalidateLater(500, session)
}, priority=10)

output$currentIteration = renderText({
paste0("Current iteration: ", vals$iter)
})

stopProcess = observe({
input$stop
runProcess$suspend()
})

output$IsingPlot = renderPlot({
levelplot(vals$IsingMatrix[2:(input$girdSize+1), 2:(input$girdSize+1)],
col.regions = c("red", "green"),
colorkey = FALSE, xlab = "", ylab = "")
})

session$onSessionEnded(function() {
runProcess$suspend()
})
}
)
runApp(app)