-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPencilandPaperExampleinR.R
More file actions
326 lines (236 loc) · 7.3 KB
/
PencilandPaperExampleinR.R
File metadata and controls
326 lines (236 loc) · 7.3 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
###################################################
##-----------------------------------------------##
## Code/solutions for pencil and paper exercises ##
##-----------------------------------------------##
###################################################
# Conversion of the Supplementary Code for:
# "A Step-by-Step Tutorial on Active Inference Modelling and its Application
# to Empirical Data, by Ryan Smith, Karl J. Friston, Christopher J. Whyte"
# Thanks to Ryan Smith for his quick answer on t and tau!!
#####################
#-------------------#
# Static perception #
#-------------------#
#####################
# We will work with this equation
# s = σ(log(prior) + log(t(likelihood)%*%obs))
# As log(0) is not defined, 0.01 was added to all log-input (usually exp(-16)).
# Note: The example 1 in the paper on p. 134 did not add .01 to logs.
# The matlab code may will (keep in mind, if the answers in the paper
# may differ from here, or from the matlab skript).
# Observations
obs = c(1, 0) # VECTOR
# Our Prior: # MATRIX/VECTOR
prior = matrix(
c(.75,.25),
nrow = 2,
ncol = 1,
byrow = TRUE)
likelihood = matrix( # MATRIX
c(.8, .2,
.2, .8), # the data elements
nrow=2, # number of rows
ncol=2, # number of columns
byrow = TRUE) # fill matrix by rows
# express generative model in terms of update equations
logs = log(prior+.01) + log(t(likelihood +.01)%*%obs)
logs
# Normalized via softmax function:
s = exp(logs+.01)/(sum(exp(logs+.01)))
s
# Alternative: via this function:
softmax <- function(par){ # THANKS TO: https://rpubs.com/FJRubio/softmax
n.par <- length(par) # equivalent: exp(result)/sum(exp(result))
par1 <- sort(par, decreasing = TRUE)
Lk <- par1[1]
for (k in 1:(n.par-1)) {
Lk <- max(par1[k+1], Lk) + log1p(exp(-abs(par1[k+1] - Lk)))
}
val <- exp(par - Lk)
return(val)
}
# Use formula directly:
s = softmax( log(prior+.01) + log(t(likelihood)%*%obs+.01) )
s
########################
#----------------------#
# DYNAMICAL PERCEPTION #
#----------------------#
########################
# As log(0) is not defined, 0.01 was added to all log-input.
# (usually exp(-16))
# For the Pencil and paper example we use the 0.01!
nzlog = .01
# Alternative
nonzerolog = exp(-16)
# Our softmax function
softmax <- function(par){ # THANKS TO: https://rpubs.com/FJRubio/softmax
n.par <- length(par) # equivalent: exp(result)/sum(exp(result))
par1 <- sort(par, decreasing = TRUE)
Lk <- par1[1]
for (k in 1:(n.par-1)) {
Lk <- max(par1[k+1], Lk) + log1p(exp(-abs(par1[k+1] - Lk)))
}
val <- exp(par - Lk)
return(val)
}
# GENERATIVE MODEL (FOR EXAMPEL 2 Dyn. perception)
# D = prior = matrix:
D = matrix( c(.75, .25),
nrow = 2,
ncol = 1,
byrow = TRUE)
D
# A = likelihood = matrix:
A = matrix( c(.8, .2,.2,.8),
nrow = 2,
ncol = 2,
byrow = TRUE)
A
# B transition matrix
B = matrix( c(0, 1, 1, 0),
nrow = 2,
ncol = 2,
byrow = TRUE)
B
##################
## Without loop ##
##################
# Only first and last equation applied, as there are only two timesteps given
# in the example.
# 𝑠𝜏=1=𝜎(12(ln𝐃+ln𝐁𝜏†*𝑠𝜏+1)+ln𝐀T𝑜𝜏)
# 𝑠1<𝜏<𝑇=𝜎(12(ln𝐁𝜏−1s𝜏−1+ln𝐁𝜏†𝑠𝜏+1)+ln𝐀T𝑜𝜏)
# 𝑠𝜏=𝑇=𝜎(12(ln𝐁𝜏−1s𝜏−1)+ln𝐀T𝑜𝜏)
# True observation at t-1 for tau 1 and 2.
ot11 = c(1, 0)
ot12 = c(0, 0)
# True observation at t-2 for ...
ot21 = c(1, 0)
ot22= c(0, 1)
# Initialize Approx. Posterior:
sINt = c(.5, .5)
# Equations:
# p. 135, or 31 (Bayes network...):
################################
# Model inversion time step 1! #
################################
# S t=1
st1 = softmax( ((.5*(log(D+nzlog)) )+ (.5*(log(t((B+nzlog))%*%sINt))) + ((log(t(A)+nzlog)%*%ot11))) )
st1
# > st1
# [,1]
# [1,] 0.8683268
# [2,] 0.1316732
# For S t= 2
stall = softmax ( (.5*(log((B%*%st1)+nzlog))) + (log((t(A)%*%ot12)+nzlog)) )
stall
# [,1]
# [1,] 0.2865401
# [2,] 0.7134599
###############################
# Model inversion time step 2 #
###############################
st21 = softmax( ((.5*(log(D+nzlog)) )+ (.5*(log(t((B+nzlog))%*%stall))) + ((log(t(A)+nzlog)%*%ot21))) )
st21
# [,1]
# [1,] 0.91150706
# [2,] 0.08849294
st2all = softmax( (.5*(log((B%*%st21)+nzlog))) + ((log(t(A)+nzlog)%*%ot22)) )
st2all
# [,1]
# [1,] 0.07813653
# [2,] 0.92186347 # POSTERIOR OVER STATES
#############
#############
# With loop #
#############
#############
# This time for excercise 2 p. 136
# You may clear your environment for this, or make sure to address the
# right generative model:
# Prior
D = matrix( c(.5, .5),
nrow = 2,
ncol = 1,
byrow = TRUE)
D
# A = likelihood = matrix:
A = matrix( c(.9, .1,
.1, .9),
nrow = 2,
ncol = 2,
byrow = TRUE)
A
# B transition matrix
B = matrix( c(1, 0,
0, 1),
nrow = 2,
ncol = 2,
byrow = TRUE)
B
# Timesteps
n = 2
# Initial approx posterior
qs= matrix(c(.5,.5)) # Reset qs when executing loop again!
# Result = all qs for all timesteps and each tau!
for(t in 1:n){
for (tau in 1:n){
o1<- vector("list", 1*2)
# True observation at t 1
dim(o1) = matrix(c(1, 2))
o1[[1]] = matrix(c(1, 0))
o1[[2]] = matrix(c(0, 0))
o2<- vector("list", 1*2)
dim(o2) = matrix(c(1,2))
# True observation at t2
o2[[1]] = matrix(c(1, 0))
o2[[2]] = matrix(c(1, 0))
# list of lists
o <- vector("list", 2*2)
dim(o) = matrix(c(2,2))
o[1,] <- o1 # assign to list
o[2,] <- o2 # -''-
logAo = log(t(A)%*%as.vector(o[[t,tau]])+.01) # has to be up here as well for correct results
if (tau == 1){
logD <- log(D +.01) # past
logBs <- log(t(B)%*%(qs[,])+.01) # future
}
else if(tau == 2){
logBs <- log(t(B)%*%(qs[,])+.01) # no contribution from future
}
logAo = log(t(A)%*%as.vector(o[[t,tau]])+.01) #likelihood
if (tau == 1){
logs = .5*logD + .5*logBs + as.vector(logAo)
}
else if (tau == 2) {
logs = .5*logBs + as.vector(logAo)
}
qs = softmax(logs) # Equivalent: qs = (exp(logs)/sum(exp(logs)))
print(qs)}
}
# Result for exercise 2
# [,1]
# [1,] 0.8921569
# [2,] 0.1078431
# [,1]
# [1,] 0.7345277
# [2,] 0.2654723
# [,1]
# [1,] 0.9315085
# [2,] 0.0684915
# [,1]
# [1,] 0.96627505
# [2,] 0.03372495
# Results for generative model of example 2 (not exercise 2)
# [,1]
# [1,] 0.8683268
# [2,] 0.1316732
# [,1]
# [1,] 0.2865401
# [2,] 0.7134599
# [,1]
# [1,] 0.91150706
# [2,] 0.08849294
# [,1]
# [1,] 0.07813653
# [2,] 0.92186347