| 1 |
### R code from vignette source 'elliptic.Rnw' |
| 2 |
|
| 3 |
################################################### |
| 4 |
### code chunk number 1: requirepackage |
| 5 |
################################################### |
| 6 |
require(elliptic,quietly=TRUE) |
| 7 |
|
| 8 |
|
| 9 |
################################################### |
| 10 |
### code chunk number 2: setOverallImageQuality |
| 11 |
################################################### |
| 12 |
n <- 400 |
| 13 |
n_BACCO <- 40 |
| 14 |
|
| 15 |
|
| 16 |
################################################### |
| 17 |
### code chunk number 3: require_packages |
| 18 |
################################################### |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
################################################### |
| 23 |
### code chunk number 4: elliptic.Rnw:234-237 |
| 24 |
################################################### |
| 25 |
require(elliptic) |
| 26 |
require(emulator) |
| 27 |
require(calibrator) |
| 28 |
|
| 29 |
|
| 30 |
################################################### |
| 31 |
### code chunk number 5: simple_usage_of_P |
| 32 |
################################################### |
| 33 |
z <- 1.9+1.8i |
| 34 |
P(z,g=c(1,1i)) |
| 35 |
P(z,Omega=c(1,1i)) |
| 36 |
|
| 37 |
|
| 38 |
################################################### |
| 39 |
### code chunk number 6: define_maxdiff |
| 40 |
################################################### |
| 41 |
maxdiff <- function(x,y){max(abs(x-y))} |
| 42 |
|
| 43 |
|
| 44 |
################################################### |
| 45 |
### code chunk number 7: laurent |
| 46 |
################################################### |
| 47 |
g <- c(3,2+4i) |
| 48 |
z <- seq(from=1,to=0.4+1i,len=34) |
| 49 |
|
| 50 |
|
| 51 |
################################################### |
| 52 |
### code chunk number 8: maxdiff_laurent |
| 53 |
################################################### |
| 54 |
maxdiff(P(z,g), P.laurent(z,g)) |
| 55 |
|
| 56 |
|
| 57 |
################################################### |
| 58 |
### code chunk number 9: abs_e18.10.9 |
| 59 |
################################################### |
| 60 |
abs( e18.10.9(parameters(g=g))) |
| 61 |
|
| 62 |
|
| 63 |
################################################### |
| 64 |
### code chunk number 10: lattice_figure |
| 65 |
################################################### |
| 66 |
jj <- parameters(g=c(1+1i,2-3i))$Omega |
| 67 |
latplot(jj,xlim=c(-4,4),ylim=c(-4,4),xlab="Re(z)", |
| 68 |
ylab="Im(z)") |
| 69 |
polygon(Re(c(jj[1],sum(jj),jj[2],0)), |
| 70 |
Im(c(jj[1],sum(jj),jj[2],0)),lwd=2,col="gray90",pch=16,cex=3) |
| 71 |
|
| 72 |
kk <- -c(3*jj[1] + 2*jj[2] , jj[1] + jj[2]) #det(matrix(c(3,2,1,1),2,2,T)==1 |
| 73 |
|
| 74 |
polygon(Re(c(kk[1],sum(kk),kk[2],0)), |
| 75 |
Im(c(kk[1],sum(kk),kk[2],0)),lwd=2,col="gray30",pch=16,cex=3) |
| 76 |
|
| 77 |
|
| 78 |
################################################### |
| 79 |
### code chunk number 11: congruence |
| 80 |
################################################### |
| 81 |
M <- congruence(c(4,9)) |
| 82 |
|
| 83 |
|
| 84 |
################################################### |
| 85 |
### code chunk number 12: define_o |
| 86 |
################################################### |
| 87 |
o <- c(1,1i) |
| 88 |
|
| 89 |
|
| 90 |
################################################### |
| 91 |
### code chunk number 13: maxdiff_o |
| 92 |
################################################### |
| 93 |
maxdiff(g.fun(o), g.fun(M %*% o,maxiter=800)) |
| 94 |
|
| 95 |
|
| 96 |
################################################### |
| 97 |
### code chunk number 14: u_udash |
| 98 |
################################################### |
| 99 |
u <- function(x){exp(pi*2i*x)} |
| 100 |
udash <- function(x){pi*2i*exp(pi*2i*x)} |
| 101 |
Zeta <- function(z){zeta(z,g)} |
| 102 |
Sigma <- function(z){sigma(z,g)} |
| 103 |
WeierstrassP <- function(z){P(z,g)} |
| 104 |
|
| 105 |
|
| 106 |
################################################### |
| 107 |
### code chunk number 15: integrate |
| 108 |
################################################### |
| 109 |
jj <- integrate.contour(Zeta,u,udash) |
| 110 |
|
| 111 |
|
| 112 |
################################################### |
| 113 |
### code chunk number 16: maxdiff_integrate |
| 114 |
################################################### |
| 115 |
maxdiff(jj, 2*pi*1i) |
| 116 |
|
| 117 |
|
| 118 |
################################################### |
| 119 |
### code chunk number 17: abs_integrate |
| 120 |
################################################### |
| 121 |
abs(integrate.contour(WeierstrassP,u,udash)) |
| 122 |
|
| 123 |
|
| 124 |
################################################### |
| 125 |
### code chunk number 18: jj_omega |
| 126 |
################################################### |
| 127 |
jj.omega <- half.periods(g=c(1+1i,2-3i)) |
| 128 |
|
| 129 |
|
| 130 |
################################################### |
| 131 |
### code chunk number 19: calculate_wp_figure |
| 132 |
################################################### |
| 133 |
x <- seq(from=-4, to=4, len=n) |
| 134 |
y <- x |
| 135 |
z <- outer(x,1i*x, "+") |
| 136 |
f <- P(z, c(1+1i,2-3i)) |
| 137 |
|
| 138 |
|
| 139 |
################################################### |
| 140 |
### code chunk number 20: wp_figure_file |
| 141 |
################################################### |
| 142 |
png("wp_figure.png",width=800,height=800) |
| 143 |
|
| 144 |
|
| 145 |
################################################### |
| 146 |
### code chunk number 21: wp_figure_plot |
| 147 |
################################################### |
| 148 |
persp(x, y, limit(Re(f)), xlab="Re(z)",ylab="Im(z)",zlab="Re(P(z))", |
| 149 |
theta=30, phi=30, r=1e9, border=NA, shade=0.8, expand=0.6) |
| 150 |
|
| 151 |
|
| 152 |
################################################### |
| 153 |
### code chunk number 22: wp_figure_close |
| 154 |
################################################### |
| 155 |
null <- dev.off() |
| 156 |
|
| 157 |
|
| 158 |
################################################### |
| 159 |
### code chunk number 23: thallerfig_file |
| 160 |
################################################### |
| 161 |
png("thallerfig.png",width=800,height=800) |
| 162 |
|
| 163 |
|
| 164 |
################################################### |
| 165 |
### code chunk number 24: thallerfig_plot |
| 166 |
################################################### |
| 167 |
par(pty="s") |
| 168 |
view(x,y,f,code=0,real.contour=FALSE, imag.contour=FALSE,drawlabel=FALSE,col="red",axes=FALSE,xlab="Re(z)",ylab="Im(z)") |
| 169 |
axis(1,pos = -4) |
| 170 |
axis(2,pos = -4) |
| 171 |
lines(x=c(-4,4),y=c(4,4)) |
| 172 |
lines(y=c(-4,4),x=c(4,4)) |
| 173 |
|
| 174 |
|
| 175 |
################################################### |
| 176 |
### code chunk number 25: thallerfig_close |
| 177 |
################################################### |
| 178 |
null <- dev.off() |
| 179 |
|
| 180 |
|
| 181 |
################################################### |
| 182 |
### code chunk number 26: sigma_green_calc |
| 183 |
################################################### |
| 184 |
x <- seq(from= -12, to=12, len=n) |
| 185 |
y <- x |
| 186 |
z <- outer(x, 1i*y, "+") |
| 187 |
f <- sigma(z, c(1+1i,2-3i)) |
| 188 |
|
| 189 |
|
| 190 |
################################################### |
| 191 |
### code chunk number 27: sigma_green_file |
| 192 |
################################################### |
| 193 |
png("sigma_green.png",width=800,height=800) |
| 194 |
|
| 195 |
|
| 196 |
################################################### |
| 197 |
### code chunk number 28: sigma_green_plot |
| 198 |
################################################### |
| 199 |
par(pty="s") |
| 200 |
view(x,y,f,scheme=4,real.contour=FALSE,drawlabels=FALSE,axes=FALSE, xlab="Re(z)",ylab="Im(z)") |
| 201 |
axis(1,pos= -12) |
| 202 |
axis(2,pos= -12) |
| 203 |
lines(x=c(-12,12),y=c(12,12)) |
| 204 |
lines(y=c(-12,12),x=c(12,12)) |
| 205 |
lines(x=c(-12,12),y=-c(12,12)) |
| 206 |
lines(y=c(-12,12),x=-c(12,12)) |
| 207 |
|
| 208 |
|
| 209 |
################################################### |
| 210 |
### code chunk number 29: sigma_green_close |
| 211 |
################################################### |
| 212 |
null <- dev.off() |
| 213 |
|
| 214 |
|
| 215 |
################################################### |
| 216 |
### code chunk number 30: calculate_zeta |
| 217 |
################################################### |
| 218 |
zeta.z <- zeta(z, c(1+1i,2-3i)) |
| 219 |
|
| 220 |
|
| 221 |
################################################### |
| 222 |
### code chunk number 31: zetafig_file |
| 223 |
################################################### |
| 224 |
png("zetafig.png",width=800,height=800) |
| 225 |
|
| 226 |
|
| 227 |
################################################### |
| 228 |
### code chunk number 32: zetafig_plot |
| 229 |
################################################### |
| 230 |
par(pty="s") |
| 231 |
view(x,y,zeta.z,scheme=0,real.contour=FALSE,drawlabels=FALSE,xlab="Re(z)",ylab="Im(z)") |
| 232 |
|
| 233 |
|
| 234 |
################################################### |
| 235 |
### code chunk number 33: zetafig_close |
| 236 |
################################################### |
| 237 |
null <- dev.off() |
| 238 |
|
| 239 |
|
| 240 |
################################################### |
| 241 |
### code chunk number 34: calculate_sn |
| 242 |
################################################### |
| 243 |
jj <- seq(from=-40,to=40,len=n) |
| 244 |
m <- outer(jj,1i*jj,"+") |
| 245 |
f <- sn(u=5-2i,m=m,maxiter=1000) |
| 246 |
|
| 247 |
|
| 248 |
################################################### |
| 249 |
### code chunk number 35: sn_figure_file |
| 250 |
################################################### |
| 251 |
png("sn_figure.png",width=800,height=800) |
| 252 |
|
| 253 |
|
| 254 |
################################################### |
| 255 |
### code chunk number 36: sn_figure_plot |
| 256 |
################################################### |
| 257 |
par(pty="s") |
| 258 |
view(jj,jj,f,scheme=0,r0=1/5,real=T,imag=F,levels=c(0,-0.4,-1),drawlabels=F,xlab="Re(m)",ylab="Im(m)") |
| 259 |
|
| 260 |
|
| 261 |
################################################### |
| 262 |
### code chunk number 37: sn_figure_close |
| 263 |
################################################### |
| 264 |
null <- dev.off() |
| 265 |
|
| 266 |
|
| 267 |
################################################### |
| 268 |
### code chunk number 38: stag_calc |
| 269 |
################################################### |
| 270 |
f <- function(z){1i*z^2} |
| 271 |
x <- seq(from=-6,to=6,len=n) |
| 272 |
y <- seq(from=-6,to=6,len=n) |
| 273 |
z <- outer(x,1i*y,"+") |
| 274 |
|
| 275 |
|
| 276 |
################################################### |
| 277 |
### code chunk number 39: stag_point_file |
| 278 |
################################################### |
| 279 |
png("stag_point.png",width=800,height=800) |
| 280 |
|
| 281 |
|
| 282 |
################################################### |
| 283 |
### code chunk number 40: stag_point_plot |
| 284 |
################################################### |
| 285 |
par(pty="s") |
| 286 |
view(x,y,f(z),nlevels=14,imag.contour=TRUE,real.cont=TRUE,scheme=-1, |
| 287 |
drawlabels=FALSE, |
| 288 |
axes=FALSE,xlab="Re(z)",ylab="Im(z)") |
| 289 |
axis(1,pos=-6) |
| 290 |
axis(2,pos=-6) |
| 291 |
lines(x=c(-6,6),y=c(6,6)) |
| 292 |
lines(y=c(-6,6),x=c(6,6)) |
| 293 |
d1 <- c(-0.1,0,0.1) |
| 294 |
d2 <- c( 0.1,0,0.1) |
| 295 |
lines(x=d1,y=1+d2) |
| 296 |
lines(x=d1,y=-1-d2) |
| 297 |
lines(x=1-d2,y=d1) |
| 298 |
lines(x=-1+d2,y=d1) |
| 299 |
|
| 300 |
|
| 301 |
################################################### |
| 302 |
### code chunk number 41: stag_point_close |
| 303 |
################################################### |
| 304 |
null <- dev.off() |
| 305 |
|
| 306 |
|
| 307 |
################################################### |
| 308 |
### code chunk number 42: two_calc |
| 309 |
################################################### |
| 310 |
f <- function(z){1i*log((z-1.7+3i)*(z-1.7-3i)/(z+1-0.6i)/(z+1+0.6i))} |
| 311 |
x <- seq(from=-6,to=6,len=n) |
| 312 |
y <- seq(from=-6,to=6,len=n) |
| 313 |
z <- outer(x,1i*y,"+") |
| 314 |
|
| 315 |
|
| 316 |
################################################### |
| 317 |
### code chunk number 43: two_sources_two_sinks_file |
| 318 |
################################################### |
| 319 |
png("two_sources_two_sinks.png",width=800,height=800) |
| 320 |
|
| 321 |
|
| 322 |
################################################### |
| 323 |
### code chunk number 44: two_sources_two_sinks_plot |
| 324 |
################################################### |
| 325 |
par(pty="s") |
| 326 |
view(x,y,f(z),nlevels=24,imag.contour=TRUE,real.cont=TRUE,scheme=17,power=0.1,drawlabels=FALSE,axes=FALSE,xlab="Re(z)",ylab="Im(z)") |
| 327 |
axis(1,pos=-6) |
| 328 |
axis(2,pos=-6) |
| 329 |
lines(x=c(-6,6),y=c(6,6)) |
| 330 |
lines(y=c(-6,6),x=c(6,6)) |
| 331 |
|
| 332 |
|
| 333 |
################################################### |
| 334 |
### code chunk number 45: two_sources_two_sinks_close |
| 335 |
################################################### |
| 336 |
null <- dev.off() |
| 337 |
|
| 338 |
|
| 339 |
################################################### |
| 340 |
### code chunk number 46: rect_calc3 |
| 341 |
################################################### |
| 342 |
m <- 0.5 |
| 343 |
K <- K.fun(m) |
| 344 |
iK <- K.fun(1-m) |
| 345 |
|
| 346 |
#b <- sn(1.8 + 0.8i, m=m) # 1.8 to the right and 0.8 up. |
| 347 |
#b <- 0 # middle bottom |
| 348 |
b <- sn(0 + 1i*iK/2,m=m) #dead centre of the rectangle. |
| 349 |
#b <- -1 # lower left |
| 350 |
#b <- 1/sqrt(m) # top right |
| 351 |
#b <- -1/sqrt(m) # top left |
| 352 |
#b <- 1e9*1i # top centre |
| 353 |
|
| 354 |
|
| 355 |
a <- 1 #bottom right hand side corner |
| 356 |
|
| 357 |
|
| 358 |
f <- function(z){1i*log((z-a)*(z-Conj(a))/(z-b)/(z-Conj(b)))} |
| 359 |
|
| 360 |
x <- seq(from=-K,to=K,len=n) |
| 361 |
y <- seq(from=0,to=iK,len=n) |
| 362 |
z <- outer(x,1i*y,"+") |
| 363 |
fsn <- f(sn(z,m=m)) |
| 364 |
|
| 365 |
|
| 366 |
################################################### |
| 367 |
### code chunk number 47: rectangle_pot_flow_file |
| 368 |
################################################### |
| 369 |
png("rectangle_pot_flow.png",width=800,height=800) |
| 370 |
|
| 371 |
|
| 372 |
################################################### |
| 373 |
### code chunk number 48: rectangle_pot_flow_plot |
| 374 |
################################################### |
| 375 |
view(x,y,fsn,nlevels=44,imag.contour=FALSE,real.cont=TRUE,scheme=17,power=0.1,drawlabels=FALSE,axes=FALSE,xlab="",ylab="") |
| 376 |
rect(-K,0,K,iK,lwd=3) |
| 377 |
|
| 378 |
|
| 379 |
################################################### |
| 380 |
### code chunk number 49: rectangle_pot_flow_close |
| 381 |
################################################### |
| 382 |
null <- dev.off() |
| 383 |
|
| 384 |
|
| 385 |
################################################### |
| 386 |
### code chunk number 50: bacco_flow |
| 387 |
################################################### |
| 388 |
# Choose the size of the computational mesh: |
| 389 |
n <- n_BACCO |
| 390 |
|
| 391 |
# Choose the number of code observations for D1: |
| 392 |
n.code.obs <- 30 |
| 393 |
|
| 394 |
# And the number of field observations for D2: |
| 395 |
n.field.obs <- 31 |
| 396 |
|
| 397 |
# First, up the D1 design matrix. Recall that D1 is the set of code |
| 398 |
# observations, which here means the observations of fluid speed when |
| 399 |
# the sink is at a known, specified, position. |
| 400 |
|
| 401 |
suppressWarnings(RNGversion("3.5.0")) |
| 402 |
set.seed(0) |
| 403 |
|
| 404 |
latin.hypercube <- function (n, d){ |
| 405 |
sapply(seq_len(d), function(...) { (sample(1:n) - 0.5)/n }) |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
D1.elliptic <- latin.hypercube(n.code.obs , 4) |
| 410 |
colnames(D1.elliptic) <- c("x","y","x.sink","y.sink") |
| 411 |
D1.elliptic[,c(1,3)] <- (D1.elliptic[,c(1,3)] -0.5)*2 |
| 412 |
#D1.elliptic[,c(2,4)] <- D1.elliptic[,c(2,4)] *iK |
| 413 |
|
| 414 |
# now a D2 design matrix. This is field observations: observations of |
| 415 |
# fluid speed when the sink is at the true, unknown, specified, |
| 416 |
# position. |
| 417 |
D2.elliptic <- latin.hypercube(n.field.obs , 2) |
| 418 |
colnames(D2.elliptic) <- c("x","y") |
| 419 |
D2.elliptic[,1] <- (D2.elliptic[,1] -0.5)*2 |
| 420 |
|
| 421 |
|
| 422 |
# Now a function that, given x and y and x.sink and y.sink, returns |
| 423 |
# the log of the fluid speed at x,y: |
| 424 |
|
| 425 |
fluid.speed <- function(x.pos, y.pos, x.sink, y.sink){ |
| 426 |
|
| 427 |
a <- 1 #bottom right hand side corner |
| 428 |
b <- sn(x.pos/K + 1i*iK*y.pos,m=m) #position (x.pos , y.pos) |
| 429 |
f <- function(z){1i*log((z-a)*(z-Conj(a))/(z-b)/(z-Conj(b)))} |
| 430 |
|
| 431 |
x <- seq(from=-K,to=K,len=n) |
| 432 |
y <- seq(from=0,to=iK,len=n) |
| 433 |
z <- outer(x,1i*y,"+") |
| 434 |
potential <- f(sn(z,m=m)) |
| 435 |
|
| 436 |
get.log.ke <- function(x,y,potential){ |
| 437 |
jj <- Re(potential) |
| 438 |
jj.x <- cbind(jj[,-1]-jj[,-ncol(jj)],0) |
| 439 |
jj.y <- rbind(jj[-1,]-jj[-nrow(jj),],0) |
| 440 |
kinetic.energy <- jj.x^2 + jj.y^2 |
| 441 |
n.x <- round(n * (x-(-1))/2) |
| 442 |
n.y <- round(n * y) |
| 443 |
return(log(kinetic.energy[n.x , n.y]+0.01)) |
| 444 |
} |
| 445 |
|
| 446 |
return(get.log.ke(x.pos,y.pos,potential)) |
| 447 |
} |
| 448 |
|
| 449 |
# now fill in code outputs y: |
| 450 |
y.elliptic <- rep(NA,nrow(D1.elliptic)) |
| 451 |
for(i in 1:length(y.elliptic)){ |
| 452 |
jj <- D1.elliptic[i,,drop=TRUE] |
| 453 |
y.elliptic[i] <- fluid.speed(jj[1],jj[2],jj[3],jj[4]) |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
# Now do the field observations; here the source is known to be at the |
| 458 |
# centre of the rectangle: |
| 459 |
|
| 460 |
z.elliptic <- rep(NA,nrow(D2.elliptic)) |
| 461 |
for(i in 1:length(z.elliptic)){ |
| 462 |
jj <- D2.elliptic[i,,drop=TRUE] |
| 463 |
z.elliptic[i] <- fluid.speed(jj[1],jj[2],0,0.5) |
| 464 |
} |
| 465 |
|
| 466 |
# Create design matrix plus observations for didactic purposes: |
| 467 |
D1 <- round(cbind(D1.elliptic,observation=y.elliptic),2) |
| 468 |
D2 <- round(cbind(D2.elliptic,observation=z.elliptic),2) |
| 469 |
|
| 470 |
|
| 471 |
# create a data vector: |
| 472 |
d.elliptic <- c(y.elliptic , z.elliptic) |
| 473 |
|
| 474 |
#now a h1.toy() equivalent: |
| 475 |
h1.elliptic <- function(x){ |
| 476 |
out <- c(1,x[1]) |
| 477 |
} |
| 478 |
|
| 479 |
#now a H1.toy() equivalent: |
| 480 |
H1.elliptic <- |
| 481 |
function (D1) |
| 482 |
{ |
| 483 |
if (is.vector(D1)) { |
| 484 |
D1 <- t(D1) |
| 485 |
} |
| 486 |
out <- t(apply(D1, 1, h1.elliptic)) |
| 487 |
colnames(out)[1] <- "h1.const" |
| 488 |
return(out) |
| 489 |
} |
| 490 |
|
| 491 |
h2.elliptic <- |
| 492 |
function(x){ |
| 493 |
c(1,x[1]) |
| 494 |
} |
| 495 |
|
| 496 |
H2.elliptic <- |
| 497 |
function(D2){ |
| 498 |
if (is.vector(D2)) { |
| 499 |
D2 <- t(D2) |
| 500 |
} |
| 501 |
out <- t(apply(D2, 1, h2.elliptic)) |
| 502 |
colnames(out)[1] <- "h2.const" |
| 503 |
return(out) |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
#Now an extractor function: |
| 508 |
extractor.elliptic <- |
| 509 |
function (D1) |
| 510 |
{ |
| 511 |
return(list(x.star = D1[, 1:2, drop = FALSE], t.vec = D1[, |
| 512 |
3:4, drop = FALSE])) |
| 513 |
} |
| 514 |
|
| 515 |
# Now a whole bunch of stuff to define a phi.fun.elliptic() |
| 516 |
# and, after that, to call it: |
| 517 |
phi.fun.elliptic <- |
| 518 |
function (rho, lambda, psi1, psi1.apriori, psi2, psi2.apriori, |
| 519 |
theta.apriori, power) |
| 520 |
{ |
| 521 |
"pdm.maker.psi1" <- function(psi1) { |
| 522 |
jj.omega_x <- diag(psi1[1:2]) |
| 523 |
rownames(jj.omega_x) <- names(psi1[1:2]) |
| 524 |
colnames(jj.omega_x) <- names(psi1[1:2]) |
| 525 |
jj.omega_t <- diag(psi1[3:4]) |
| 526 |
rownames(jj.omega_t) <- names(psi1[3:4]) |
| 527 |
colnames(jj.omega_t) <- names(psi1[3:4]) |
| 528 |
sigma1squared <- psi1[5] |
| 529 |
return(list(omega_x = jj.omega_x, omega_t = jj.omega_t, |
| 530 |
sigma1squared = sigma1squared)) |
| 531 |
} |
| 532 |
"pdm.maker.psi2" <- function(psi1) { |
| 533 |
jj.omegastar_x <- diag(psi2[1:2]) |
| 534 |
sigma2squared <- psi2[3] |
| 535 |
return(list(omegastar_x = jj.omegastar_x, sigma2squared = sigma2squared)) |
| 536 |
} |
| 537 |
jj.mean <- theta.apriori$mean |
| 538 |
jj.V_theta <- theta.apriori$sigma |
| 539 |
jj.discard.psi1 <- pdm.maker.psi1(psi1) |
| 540 |
jj.omega_t <- jj.discard.psi1$omega_t |
| 541 |
jj.omega_x <- jj.discard.psi1$omega_x |
| 542 |
jj.sigma1squared <- jj.discard.psi1$sigma1squared |
| 543 |
jj.discard.psi2 <- pdm.maker.psi2(psi2) |
| 544 |
jj.omegastar_x <- jj.discard.psi2$omegastar_x |
| 545 |
jj.sigma2squared <- jj.discard.psi2$sigma2squared |
| 546 |
jj.omega_t.upper <- chol(jj.omega_t) |
| 547 |
jj.omega_t.lower <- t(jj.omega_t.upper) |
| 548 |
jj.omega_x.upper <- chol(jj.omega_x) |
| 549 |
jj.omega_x.lower <- t(jj.omega_x.upper) |
| 550 |
jj.a <- solve(solve(jj.V_theta) + 2 * jj.omega_t, solve(jj.V_theta, |
| 551 |
jj.mean)) |
| 552 |
jj.b <- t(2 * solve(solve(jj.V_theta) + 2 * jj.omega_t) %*% |
| 553 |
jj.omega_t) |
| 554 |
jj.c <- jj.sigma1squared/sqrt(det(diag(nrow = nrow(jj.V_theta)) + |
| 555 |
2 * jj.V_theta %*% jj.omega_t)) |
| 556 |
jj.A <- solve(jj.V_theta + solve(jj.omega_t)/4) |
| 557 |
jj.A.upper <- chol(jj.A) |
| 558 |
jj.A.lower <- t(jj.A.upper) |
| 559 |
list(rho = rho, lambda = lambda, psi1 = psi1, psi1.apriori = psi1.apriori, |
| 560 |
psi2 = psi2, psi2.apriori = psi2.apriori, theta.apriori = theta.apriori, |
| 561 |
power = power, omega_x = jj.omega_x, omega_t = jj.omega_t, |
| 562 |
omegastar_x = jj.omegastar_x, sigma1squared = jj.sigma1squared, |
| 563 |
sigma2squared = jj.sigma2squared, omega_x.upper = jj.omega_x.upper, |
| 564 |
omega_x.lower = jj.omega_x.lower, omega_t.upper = jj.omega_t.upper, |
| 565 |
omega_t.lower = jj.omega_t.lower, a = jj.a, b = jj.b, |
| 566 |
c = jj.c, A = jj.A, A.upper = jj.A.upper, A.lower = jj.A.lower) |
| 567 |
} |
| 568 |
|
| 569 |
# OK, that's the function defined. Now to create some jj.* variables |
| 570 |
# to call it: |
| 571 |
|
| 572 |
jj.psi1 <- c(rep(1,4),0.3) |
| 573 |
names(jj.psi1)[1:4] <- colnames(D1.elliptic) |
| 574 |
names(jj.psi1)[5] <- "sigma1squared" |
| 575 |
|
| 576 |
jj.mean.psi1 <- rep(1,5) |
| 577 |
names(jj.mean.psi1) <- names(jj.psi1) |
| 578 |
|
| 579 |
jj.sigma.psi1 <- diag(0.1,nrow=5) |
| 580 |
rownames(jj.sigma.psi1) <- names(jj.psi1) |
| 581 |
colnames(jj.sigma.psi1) <- names(jj.psi1) |
| 582 |
|
| 583 |
jj.psi2 <- c(1,1,0.3) |
| 584 |
names(jj.psi2)[1:2] <- colnames(D2.elliptic) |
| 585 |
names(jj.psi2)[3] <- "sigma2squared" |
| 586 |
|
| 587 |
jj.mean.psi2 <- rep(1,4) |
| 588 |
names(jj.mean.psi2) <- c("x.sink", "y.sink","rho","lambda") |
| 589 |
|
| 590 |
jj.sigma.psi2 <- diag(0.1,4) |
| 591 |
rownames(jj.sigma.psi2) <- names(jj.mean.psi2) |
| 592 |
colnames(jj.sigma.psi2) <- names(jj.mean.psi2) |
| 593 |
|
| 594 |
jj.mean.th <- c(1,0.5) |
| 595 |
names(jj.mean.th) <- colnames(D1.elliptic)[3:4] |
| 596 |
|
| 597 |
jj.sigma.th <- diag(rep(1,2)) |
| 598 |
rownames(jj.sigma.th) <- colnames(D1.elliptic)[3:4] |
| 599 |
colnames(jj.sigma.th) <- colnames(D1.elliptic)[3:4] |
| 600 |
|
| 601 |
# Now call phi.fun.elliptic(): |
| 602 |
phi.elliptic <- |
| 603 |
phi.fun.elliptic( |
| 604 |
rho=1, |
| 605 |
lambda=0.1, |
| 606 |
psi1=jj.psi1, |
| 607 |
psi2=jj.psi2, |
| 608 |
psi1.apriori=list(mean=jj.mean.psi1, sigma=jj.sigma.psi1), |
| 609 |
psi2.apriori=list(mean=jj.mean.psi2, sigma=jj.sigma.psi2), |
| 610 |
theta.apriori=list(mean=jj.mean.th, sigma=jj.sigma.th), |
| 611 |
power=2 |
| 612 |
) |
| 613 |
|
| 614 |
# Now an E.theta.elliptic(): |
| 615 |
E.theta.elliptic <- |
| 616 |
function (D2 = NULL, H1 = NULL, x1 = NULL, x2 = NULL, phi, give.mean = TRUE) |
| 617 |
{ |
| 618 |
if (give.mean) { |
| 619 |
m_theta <- phi$theta.apriori$mean |
| 620 |
return(H1(D1.fun(D2, t.vec = m_theta))) |
| 621 |
} |
| 622 |
else { |
| 623 |
out <- matrix(0, 2,2) |
| 624 |
rownames(out) <- c("h1.const","x") |
| 625 |
colnames(out) <- c("h1.const","x") |
| 626 |
return(out) |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
#Now an Edash.theta.elliptic(). Because the basis vector is not a |
| 631 |
#function of theta, this is a bit academic as we can use a function |
| 632 |
#that is identical to Edash.theta.toy(): |
| 633 |
|
| 634 |
Edash.theta.elliptic <- |
| 635 |
function (x, t.vec, k, H1, fast.but.opaque = FALSE, a = NULL, |
| 636 |
b = NULL, phi = NULL) |
| 637 |
{ |
| 638 |
if (fast.but.opaque) { |
| 639 |
edash.mean <- a + crossprod(b, t.vec[k, ]) |
| 640 |
} |
| 641 |
else { |
| 642 |
V_theta <- phi$theta.apriori$sigma |
| 643 |
m_theta <- phi$theta.apriori$mean |
| 644 |
omega_t <- phi$omega_t |
| 645 |
edash.mean <- solve(solve(V_theta) + 2 * omega_t, solve(V_theta, |
| 646 |
m_theta) + 2 * crossprod(omega_t, t.vec[k, ])) |
| 647 |
} |
| 648 |
jj <- as.vector(edash.mean) |
| 649 |
names(jj) <- rownames(edash.mean) |
| 650 |
edash.mean <- jj |
| 651 |
return(H1(D1.fun(x, edash.mean))) |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
# Define a wrapper for equation 8: |
| 657 |
# First, calculate the constant to subtract to ensure that |
| 658 |
# the support has a maximum of about zero: |
| 659 |
|
| 660 |
maximum.likelihood.support <- p.eqn8.supp(theta=c(0,1/2), D1=D1.elliptic, D2=D2.elliptic, H1=H1.elliptic, H2=H2.elliptic, d=d.elliptic, include.prior=FALSE, lognormally.distributed=FALSE, return.log=TRUE, phi=phi.elliptic) |
| 661 |
|
| 662 |
support <- function(x){ |
| 663 |
p.eqn8.supp(theta=x, D1=D1.elliptic, D2=D2.elliptic, H1=H1.elliptic, H2=H2.elliptic, d=d.elliptic, include.prior=FALSE, lognormally.distributed=FALSE, return.log=TRUE, phi=phi.elliptic) - maximum.likelihood.support |
| 664 |
} |
| 665 |
|
| 666 |
#define a local function called optim() for aesthetic reasons (ie it |
| 667 |
# improves the appearance of the call to optim(): |
| 668 |
|
| 669 |
optim <- |
| 670 |
function(par,fn){ |
| 671 |
stats::optim(par=par,fn=fn,control=list(fnscale = -1))$par |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
################################################### |
| 677 |
### code chunk number 51: head_D1 |
| 678 |
################################################### |
| 679 |
head(D1) |
| 680 |
|
| 681 |
|
| 682 |
################################################### |
| 683 |
### code chunk number 52: head_D2 |
| 684 |
################################################### |
| 685 |
head(D2) |
| 686 |
|
| 687 |
|
| 688 |
################################################### |
| 689 |
### code chunk number 53: calc_b_sn |
| 690 |
################################################### |
| 691 |
b <- sn(D1[1,3] + 1i*D1[1,4],m=m) #point corresponding to first line of D1 |
| 692 |
fsnz2 <- f(sn(z,m=m)) |
| 693 |
|
| 694 |
|
| 695 |
################################################### |
| 696 |
### code chunk number 54: code_obs_file |
| 697 |
################################################### |
| 698 |
png("code_obs.png",width=800,height=800) |
| 699 |
|
| 700 |
|
| 701 |
################################################### |
| 702 |
### code chunk number 55: code_obs_plot |
| 703 |
################################################### |
| 704 |
view(x,y,fsnz2,nlevels=44,imag.contour=FALSE,real.cont=TRUE,scheme=-1,drawlabels=FALSE,axes=FALSE,xlab="",ylab="") |
| 705 |
points(x=K*D1[1,1],y=D1[1,2]*iK,pch=4) |
| 706 |
rect(-K,0,K,iK,lwd=3) |
| 707 |
|
| 708 |
|
| 709 |
################################################### |
| 710 |
### code chunk number 56: code_obs_close |
| 711 |
################################################### |
| 712 |
null <- dev.off() |
| 713 |
|
| 714 |
|
| 715 |
################################################### |
| 716 |
### code chunk number 57: calc_b2 |
| 717 |
################################################### |
| 718 |
b <- sn(0 + 1i*iK/2,m=m) |
| 719 |
fsnzz <- f(sn(z,m=m)) |
| 720 |
|
| 721 |
|
| 722 |
################################################### |
| 723 |
### code chunk number 58: true_flow_file |
| 724 |
################################################### |
| 725 |
png("true_flow.png",width=800,height=800) |
| 726 |
|
| 727 |
|
| 728 |
################################################### |
| 729 |
### code chunk number 59: true_flow_plot |
| 730 |
################################################### |
| 731 |
view(x,y,fsnzz,nlevels=44,imag.contour=FALSE,real.cont=TRUE,scheme=-1,drawlabels=FALSE,axes=FALSE,xlab="",ylab="") |
| 732 |
points(x=K*D2[,1],y=D2[,2]*iK,pch=4) |
| 733 |
rect(-K,0,K,iK,lwd=3) |
| 734 |
|
| 735 |
|
| 736 |
################################################### |
| 737 |
### code chunk number 60: true_flow_close |
| 738 |
################################################### |
| 739 |
null <- dev.off() |
| 740 |
|
| 741 |
|
| 742 |
################################################### |
| 743 |
### code chunk number 61: support |
| 744 |
################################################### |
| 745 |
support(c(0,1/2)) #centre of the rectangle |
| 746 |
support(c(-1,1)) #top left corner |
| 747 |
|
| 748 |
|
| 749 |
################################################### |
| 750 |
### code chunk number 62: mle_calc |
| 751 |
################################################### |
| 752 |
mle <- optim(c(0,1/2),support) |
| 753 |
|
| 754 |
|
| 755 |
################################################### |
| 756 |
### code chunk number 63: print_mle |
| 757 |
################################################### |
| 758 |
mle |
| 759 |
|
| 760 |
|
| 761 |
################################################### |
| 762 |
### code chunk number 64: support_of_mle |
| 763 |
################################################### |
| 764 |
support(mle) |
| 765 |
|
| 766 |
|