-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredictions.R
More file actions
2705 lines (2382 loc) · 113 KB
/
Copy pathpredictions.R
File metadata and controls
2705 lines (2382 loc) · 113 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Function for compute conditional mean and variance for normal distribution given data.
# Xp | Xd has (conditional) mean:
# muc = muP + SigmaPtoD %*% SigmaD^(-1) %*% (Xd - muD)
# and (conditional) variance:
# Sigmac = SigmaP - SigmaPtoD %*% SigmaD^(-1) %*% SigmaDtoP
conditionalNormal = function(Xd, muP, muD, SigmaP, SigmaD, SigmaPtoD) {
# SigmaDInv = solve(SigmaD) # NOTE: luckily we only need to do this once.
# muc = muP + SigmaPtoD %*% SigmaDTildeInv %*% (Xd - muD)
# Sigmac = SigmaP - SigmaPtoD %*% SigmaDInv %*% SigmaDtoP
# compute conditional mean and variance of zeta
muc = muP + SigmaPtoD %*% solve(SigmaD, Xd - muD)
Sigmac = SigmaP - SigmaPtoD %*% solve(SigmaD, t(SigmaPtoD))
return(list(muc=muc, Sigmac=Sigmac))
}
# Function for generating simulations from the model predictive distribution given
# the GPS data. Same is predsGivenGPS but also generates predictions and
# simulations at GPS coordinates
predsGivenGPSFull = function(params, nsim=100, muVec=NULL, gpsDat=slipDatCSZ, fault=csz, normalizeTaper=FALSE) {
# get fit MLEs
if(is.null(muVec)) {
lambda = params[1]
muZeta = params[2]
sigmaZeta = params[3]
lambda0 = params[4]
muXi = params[5]
muZetaGPS = muZeta
}
else {
if(length(muVec) == 1)
muVec = rep(muVec, nrow(gpsDat) + nrow(fault))
lambda = params[1]
sigmaZeta = params[3]
lambda0 = params[4]
muXi = params[5]
muZeta=muVec
muZetaGPS = muVec[1:nrow(gpsDat)]
}
# set other relevant parameters
nuZeta = 3/2 # Matern smoothness
phiZeta = 232.5722 # fit from fitGPSCovariance()
# Calculate the standard error vector of xi. Derivation from week 08_30_17.Rmd presentation.
# Transformation from additive error to multiplicative lognormal model with asympototic
# median and variance matching.
sigmaXi = sqrt(log(.5*(sqrt(4*gpsDat$slipErr^2/gpsDat$slip^2 + 1) + 1)))
# get log GPS data
logX = log(gpsDat$slip)
# get GPS data
xs = cbind(gpsDat$lon, gpsDat$lat)
# compute relevant covariances
arealCSZCor = getArealCorMat(fault)
SigmaB = arealCSZCor * sigmaZeta^2
SigmaSB = pointArealZetaCov(params, xs, fault, nDown=9, nStrike=12)
SigmaS = stationary.cov(xs, Covariance="Matern", theta=phiZeta,
smoothness=nuZeta, Distance="rdist.earth",
Dist.args=list(miles=FALSE)) * sigmaZeta^2
# now block them into predictions and data covariance matrices
SigmaP = cbind(rbind(SigmaS, t(SigmaSB)), rbind(SigmaSB, SigmaB))
SigmaD = SigmaS + diag(sigmaXi^2)
SigmaPtoD = rbind(SigmaS, t(SigmaSB))
# get block means
muP = muZeta
if(is.null(muVec))
muD = rep(muZetaGPS + muXi, length(logX))
else
muD = muZetaGPS + muXi
# compute conditional normal mean and standard error in mean estimate
Xd = logX
condDistn = conditionalNormal(Xd, muP, muD, SigmaP, SigmaD, SigmaPtoD)
muc = condDistn$muc
Sigmac = condDistn$Sigmac
##### now generate the conditional simulations. Note that we still use the
##### marginal covariance structure, but we've conditionally updated the mean.
# get prediction locations
# get CSZ prediction coordinates
xd = cbind(gpsDat$lon, gpsDat$lat) # d is GPS locations
xp = cbind(fault$longitude, fault$latitude) # p is fault areal locations
nd = nrow(xd)
np = nrow(xp)
point = 1:nd
areal = (nd+1):(np+nd)
# Cholesky decomp used for simulations (don't add Sigmac because that
# can lead to non-physical results. Here we just treat the mean as
# constant and use the marginal variability)
# NOTE: deflate variance to account for increased variation due to
# conditional mean (using MSE = Var + bias^2 formula)
# SigmaPred = SigmaP + Sigmac
# varDeflation = 1 - mean((muc[point] - muZeta)^2)/sigmaZeta^2
# SigmaPred = SigmaP * varDeflation
# SigmaPred = SigmaP
SigmaPred = Sigmac
SigmaL = t(chol(SigmaPred))
# generate predictive simulations
zSims = matrix(rnorm(nsim*(nrow(xp)+nrow(xd))), nrow=nrow(xp)+nrow(xd), ncol=nsim)
logZetaSims0 = SigmaL %*% zSims # each column is a zero mean simulation
logZetaSims = sweep(logZetaSims0, 1, muc, "+") # add conditional mean to simulations
zetaSims = exp(logZetaSims)
tvec = taper(c(getFaultCenters(fault)[,3], gpsDat$Depth), lambda = lambda, normalize=normalizeTaper)
slipSims = sweep(zetaSims, 1, tvec, FUN="*")
# seperate areal average sims from GPS point location sims
slipSimsGPS = slipSims[point,]
slipSims = slipSims[areal,]
# get mean slip prediction field
meanSlip = exp(muc[areal] + diag(SigmaPred[areal,areal])/2) * tvec[areal]
meanSlipGPS = exp(muc[point] + diag(SigmaPred[point,point])/2) * tvec[point]
return(list(meanSlip=meanSlip, meanSlipGPS=meanSlipGPS, slipSims=slipSims, slipSimsGPS=slipSimsGPS,
Sigmac=Sigmac[areal, areal], muc=muc[areal], SigmacGPS = Sigmac[point, point],
mucGPS=muc[point], Sigma=SigmaPred[areal,areal], SigmaGPS=SigmaPred[point,point]))
}
# Function for generating simulations from the model predictive distribution given
# the GPS data. The predictive model is given in the presentation for the week of
# 08/30/17:
# log(zeta) | X has (conditional) mean:
# muc = muZeta + SigmaPtoD %*% (SigmaD + diag(sigmaXi^2))^(-1) %*% (log(X) - muZeta - muXi)
# and (conditional) variance"
# Sigmac = SigmaP - SigmaPtoD %*% (SigmaD + diag(sigmaXi^2))^(-1) %*% SigmaDtoP
predsGivenGPS = function(params, nsim=100, muVec=NULL, tvec=NULL, fault=csz, posNormalModel=FALSE,
normalModel=posNormalModel, normalizeTaper=FALSE, dStar=28000,
anisotropic=FALSE) {
# get fit MLEs
if(is.null(muVec)) {
lambda = params[1]
muVecGPS = params[2]
muVecCSZ = params[2]
sigmaZeta = params[3]
lambda0 = params[4]
muXi = params[5]
}
else {
lambda = params[1]
sigmaZeta = params[3]
lambda0 = params[4]
muXi = params[5]
muVecGPS = muVec[1:nrow(slipDatCSZ)]
muVecCSZ = muVec[(nrow(slipDatCSZ)+1):length(muVec)]
}
stop("predsGivenGPS function no longer supported")
# get the taper
if(is.null(tvec))
tvec = taper(getFaultCenters(fault)[,3], lambda=lambda, normalize=normalizeTaper, dStar=dStar)
# set other relevant parameters
corPar = getCorPar(normalModel=normalModel)
phiZeta = corPar$phiZeta
nuZeta = corPar$nuZeta
# Calculate the standard error vector of xi. Derivation from week 08_30_17.Rmd presentation.
# Transformation from additive error to multiplicative lognormal model with asympototic
# median and variance matching.
if(!normalModel)
sigmaXi = sqrt(log(.5*(sqrt(4*slipDatCSZ$slipErr^2/slipDatCSZ$slip^2 + 1) + 1)))
else
sigmaXi = slipDatCSZ$slipErr
# get log GPS data
if(!normalModel)
x = log(slipDatCSZ$slip)
else
x = slipDatCSZ$slip
# get GPS data and CSZ prediction coordinates
xd = cbind(slipDatCSZ$lon, slipDatCSZ$lat)
xp = cbind(fault$longitude, fault$latitude)
# compute relevant covariance matrices
SigmaD = stationary.cov(xd, Covariance="Matern", Distance="rdist.earth", Dist.args=list(miles=FALSE),
theta=phiZeta, smoothness=nuZeta) * sigmaZeta^2
if(normalModel)
SigmaD = muXi^2 * SigmaD
SigmaD = SigmaD + diag(sigmaXi^2)
SigmaDInv = solve(SigmaD) # NOTE: luckily we only need to do this once.
# These lines have been replaced with the appropriate code for computing
# covariance between areal averages of zeta and points or other averages:
# SigmaPtoD = stationary.cov(xp, xd, Covariance="Matern", Distance="rdist.earth", Dist.args=list(miles=FALSE),
# theta=phiZeta, smoothness=nuZeta) * sigmaZeta^2
# SigmaDtoP = t(SigmaPtoD)
# SigmaP = stationary.cov(xp, Covariance="Matern", Distance="rdist.earth", Dist.args=list(miles=FALSE),
# theta=phiZeta, smoothness=nuZeta) * sigmaZeta^2
arealCSZCor = getArealCorMat(fault, normalModel = normalModel)
SigmaP = arealCSZCor * sigmaZeta^2
SigmaDtoP = pointArealZetaCov(params, xd, fault, nDown=9, nStrike=12, normalModel=normalModel)
if(normalModel) {
SigmaP = muXi^2 * SigmaP
SigmaDtoP = muXi^2 * SigmaDtoP
}
SigmaPtoD = t(SigmaDtoP)
# compute conditional mean and variance of zeta and take Cholesky decomp
# NOTE: use total variance, conditional covariance gives the SEs for the
# conditional mean estimate.
if(!normalModel)
muX = muVecGPS + muXi
else
muX = muVecGPS * muXi
muc = muVecCSZ + SigmaPtoD %*% SigmaDInv %*% (x - muX)
Sigmac = SigmaP - SigmaPtoD %*% SigmaDInv %*% SigmaDtoP
# varDeflation = 1 - mean((muc - muZeta)^2)/sigmaZeta^2
# SigmaPred = SigmaP * varDeflation # total variance is conditional variance
SigmaPred = Sigmac
SigmaPredL = t(chol(SigmaPred))
# # generate predictive simulations
# zSims = matrix(rnorm(nsim*nrow(xp)), nrow=nrow(xp), ncol=nsim)
# logZetaSims = sweep(SigmaPredL %*% zSims, 1, muc, FUN="+") # add muc to each zero mean simulation
# if(!normalModel)
# zetaSims = exp(logZetaSims)
# else
# zetaSims = logZetaSims
# slipSims = sweep(zetaSims, 1, tvec, FUN="*")
# generate predictive simulations
notAllPos=TRUE
zetaSims = matrix(-1, nrow=nrow(xp), ncol=nsim)
while(notAllPos) {
# generate simulations until all slips are positive, if necessary
negCol = function(simCol) {
any(simCol < 0)
}
negCols = apply(zetaSims, 2, negCol)
nNewSims = sum(negCols)
zSims = matrix(rnorm(nNewSims*nrow(xp)), nrow=nrow(xp), ncol=nNewSims)
logZetaSims = sweep(SigmaPredL %*% zSims, 1, muc, "+") # add muZeta to each zero mean simulation
if(!normalModel)
zetaSims[,negCols] = exp(logZetaSims)
else
zetaSims[,negCols] = logZetaSims
notAllPos = any(zetaSims < 0) && posNormalModel
}
slipSims = sweep(zetaSims, 1, tvec, FUN="*")
# get mean slip prediction field
if(!normalModel)
meanSlip = exp(muc + diag(SigmaPred)/2) * tvec
else if(!posNormalModel)
meanSlip = muc * tvec
else {
meanSlip = apply(slipSims, 1, mean)
if(nsim < 1000)
warning("mean slip estimates may be poor with positive normal mode for <1000 simulations")
}
##### generate predictions at GPS locations
mucGPS = muVecGPS + SigmaD %*% SigmaDInv %*% (x - muX)
SigmacGPS = SigmaD - (SigmaD - 2*diag(sigmaXi^2) + diag(sigmaXi^4) %*% SigmaDInv)
SigmaPredGPS = SigmacGPS + SigmaD
return(list(meanSlip=meanSlip, slipSims=slipSims, tvec=tvec, muc=muc, Sigmac=Sigmac,
mucGPS=mucGPS, SigmacDiagGPS = diag(SigmacGPS), Sigma=SigmaPred, SigmaGPS=SigmaPredGPS))
}
# generate predictions given only the parameter MLEs (no GPS or subsidence data)
preds = function(params, nsim=100, fault=csz, muVec=NULL, tvec=rep(params[1], nrow(fault)),
posNormalModel=FALSE, normalModel=posNormalModel, phiZeta=NULL,
taperedGPSDat=FALSE, anisotropic=FALSE, fastPNSim=TRUE) {
# get parameters
if(is.null(muVec)) {
lambda = params[1]
muZeta = params[2]
sigmaZeta = params[3]
muXi = params[5]
muZetaGPS = rep(muZeta, nrow(slipDatCSZ))
muZetaCSZ = rep(muZeta, nrow(fault))
}
else {
lambda = params[1]
sigmaZeta = params[3]
muXi = params[5]
muZeta = muVec
muZetaGPS = muVec[1:nrow(slipDatCSZ)]
muZetaCSZ = muVec[(nrow(slipDatCSZ)+1):length(muVec)]
}
# set other relevant parameters
if(is.null(phiZeta)) {
corPar = getCorPar(normalModel=normalModel)
phiZeta = corPar$phiZeta
nuZeta = corPar$nuZeta
}
else {
nuZeta = 3/2
}
# Calculate the standard error vector of xi. Derivation from week 08_30_17.Rmd presentation.
# Transformation from additive error to multiplicative lognormal model with asympototic
# median and variance matching.
if(!normalModel)
sigmaXi = sqrt(log(.5*(sqrt(4*slipDatCSZ$slipErr^2/slipDatCSZ$slip^2 + 1) + 1)))
else
sigmaXi = slipDatCSZ$slipErr
# get CSZ prediction coordinates
xp = cbind(fault$longitude, fault$latitude)
# compute relevant covariance matrices
# NOTE: previous code replaced with code calculating covariance between
# areal averages of zeta
# Sigma = stationary.cov(xp, Covariance="Matern", Distance="rdist.earth", Dist.args=list(miles=FALSE),
# theta=phiZeta, smoothness=nuZeta, onlyUpper=TRUE) * sigmaZeta^2
# Load the precomputed correlation matrix.
if(!taperedGPSDat) {
arealCSZCor = getArealCorMat(fault, normalModel=normalModel)
}
else {
phiZ = params[length(params) - anisotropic]
coordsZ = cbind(fault$longitude, fault$latitude)
xd = cbind(slipDatCSZ$lon, slipDatCSZ$lat)
if(!anisotropic) {
distMatZ = rdist.earth(coordsZ, miles=FALSE)
arealCSZCor = stationary.cov(coordsZ, Covariance="Matern", theta=phiZ,
onlyUpper=FALSE, distMat=distMatZ, smoothness=3/2)
# compute covariance at GPS locations
SigmaD = stationary.cov(xd, Covariance="Matern", Distance="rdist.earth", Dist.args=list(miles=FALSE),
theta=phiZeta, smoothness=nuZeta) * sigmaZeta^2
}
else {
alpha = params[length(params)]
coordsCSZ = cbind(fault$longitude, fault$latitude)
### Rather than training the fault, we redefine an axis to be the strike access in Euclidean space
### using a Lambert projection and PCA
out = straightenFaultLambert()
faultGeomStraight = out$fault
scale = out$scale
parameters = out$projPar
transformation = out$transformation
cszStraight = divideFault2(faultGeomStraight)
centers = getFaultCenters(csz)[,1:2]
newCenters = transformation(centers)
cszStraight$centerX = newCenters[,1]
cszStraight$centerY = newCenters[,2]
straightenedGpsCoords = transformation(cbind(slipDatCSZ$lon, slipDatCSZ$lat))
# calculate along strike and along dip squared distances in kilometers
strikeCoordsCSZ = cbind(0, cszStraight$centerY)
dipCoordsCSZ = cbind(cszStraight$centerX, 0)
squareStrikeDistCsz = rdist(strikeCoordsCSZ)^2
squareDipDistCsz = rdist(dipCoordsCSZ)^2
# do the same for the gps data
strikeCoordsGps = cbind(0, straightenedGpsCoords[,2])
dipCoordsGps = cbind(straightenedGpsCoords[,1], 0)
squareStrikeDistGps = rdist(strikeCoordsGps)^2
squareDipDistGps = rdist(dipCoordsGps)^2
# compute gps, fault, and cross distance matrices
distMatGPS = sqrt(alpha^2 * squareStrikeDistGps + alpha^(-2) * squareDipDistGps)
distMatCSZ = sqrt(alpha^2 * squareStrikeDistCsz + alpha^(-2) * squareDipDistCsz)
# now compute the covariances
xs = cbind(fault$longitude, fault$latitude)
arealCSZCor = stationary.cov(xs, Covariance="Matern", theta=phiZeta,
smoothness=nuZeta, distMat = distMatCSZ)
SigmaD = stationary.cov(xd, Covariance="Matern", theta=phiZeta,
smoothness=nuZeta, distMat = distMatGPS) * sigmaZeta^2
}
}
Sigma = arealCSZCor * sigmaZeta^2
SigmaL = t(chol(Sigma))
# # generate predictive simulations
# notAllPos=TRUE
# zetaSims = matrix(-1, nrow=nrow(xp), ncol=nsim)
# nNewSims = nsim
# while(notAllPos) {
# # generate simulations until all slips are positive, if necessary
# negCol = function(simCol) {
# any(simCol < 0)
# }
# negCols = apply(zetaSims, 2, negCol)
# if(nNewSims != sum(negCols)) {
# nNewSims = sum(negCols)
# print(paste0("number of simulations remaining: ", nNewSims))
# }
#
# zSims = matrix(rnorm(nNewSims*nrow(xp)), nrow=nrow(xp), ncol=nNewSims)
# logZetaSims = sweep(SigmaL %*% zSims, 1, muZetaCSZ, "+") # add muZeta to each zero mean simulation
# if(!normalModel)
# zetaSims[,negCols] = exp(logZetaSims)
# else
# zetaSims[,negCols] = logZetaSims
#
# notAllPos = any(zetaSims < 0) && posNormalModel
# }
# slipSims = sweep(zetaSims, 1, tvec, FUN="*")
if(posNormalModel) {
notAllPos=TRUE
nNewSims = nsim
zetaSims = matrix(-1, nrow=nrow(SigmaL), ncol=nsim) # multiply by two for consistency with Stan MCMC results
while(notAllPos) {
# generate simulations until all slips are positive, if necessary
# can check probability of generating all positive simulation with this code:
# library(mvtnorm)
# pmvnorm(upper=rep(0, nrow(csz)), mean=muc[-(1:nrow(gpsDat))], sigma=Sigmac[-(1:nrow(gpsDat)),-(1:nrow(gpsDat))])
negCol = function(simCol) {
any(simCol < 0)
}
negCols = apply(zetaSims, 2, negCol)
if(nNewSims != sum(negCols)) {
nNewSims = sum(negCols)
print(paste0("number of simulations remaining: ", nNewSims))
}
if(! fastPNSim) {
# simulate only for remaining columns
zSims = matrix(rnorm(nNewSims*nrow(SigmaL)), nrow=nrow(SigmaL), ncol=nNewSims)
thisZetaSims = sweep(SigmaL %*% zSims, 1, muZetaCSZ, "+") # add muZeta to each zero mean simulation
zetaSims[,negCols] = thisZetaSims
}
else {
# simulate a bunch and take any sims that are positive
zSims = matrix(rnorm(nsim*nrow(SigmaL)), nrow=nrow(SigmaL), ncol=nsim)
thisZetaSims = sweep(SigmaL %*% zSims, 1, muZetaCSZ, "+") # add muZeta to each zero mean simulation
thisPosCols = which(!apply(thisZetaSims, 2, negCol))
if(length(thisPosCols) > nNewSims) {
zetaSims[,negCols] = thisZetaSims[,thisPosCols[1:nNewSims]]
}
else if(length(thisPosCols) > 0) {
negColsI = which(negCols)
zetaSims[,negColsI[1:length(thisPosCols)]] = thisZetaSims[,thisPosCols]
}
}
notAllPos = any(zetaSims < 0) && posNormalModel
}
if(!normalModel) {
logZetaSims = zetaSims
zetaSims = exp(logZetaSims)
} else {
logZetaSims = log(zetaSims)
}
}
else {
zSims = matrix(rnorm(nsim*nrow(SigmaL)), nrow=nrow(SigmaL), ncol=nsim)
zetaSims = sweep(SigmaL %*% zSims, 1, muZetaCSZ, "+") # add muZeta to each zero mean simulation
logZetaSims = matrix(NA, ncol=2, nrow=nrow(zetaSims))
}
slipSims = sweep(zetaSims, 1, tvec, FUN="*")
# get mean slip prediction field
if(!normalModel)
meanSlip = exp(muZetaCSZ + diag(Sigma)/2) * tvec
else if(!posNormalModel)
meanSlip = muZetaCSZ * tvec
else {
meanSlip = apply(slipSims, 1, mean)
if(nsim < 1000)
warning("mean slip estimates may be poor with positive normal mode for <1000 simulations")
}
return(list(meanSlip=meanSlip, slipSims=slipSims, Sigma=Sigma, Sigmac=Sigma, muc=muZetaCSZ,
SigmacGPS = SigmaD, mucGPS=muZetaGPS))
}
# generate pointwise predictions over a grid given only the parameter MLEs (no GPS or subsidence data)
predsPoint = function(params, nsim=100, fault=csz, muVec=NULL, lonLatGrid=NULL, dStar=26000, nKnots=5,
normalizeTaper=FALSE) {
# get parameters
if(is.null(muVec)) {
lambda = params[1]
muZeta = params[2]
sigmaZeta = params[3]
muXi = params[5]
muZetaGPS = rep(muZeta, nrow(slipDatCSZ))
muZetaCSZ = rep(muZeta, nrow(fault))
}
else {
lambda = params[1]
sigmaZeta = params[3]
muXi = params[5]
muZeta = muVec
muZetaGPS = muVec[1:nrow(slipDatCSZ)]
muZetaCSZ = muVec[(nrow(slipDatCSZ)+1):length(muVec)]
}
# get tvec
if(is.na(lambda))
taperPar = params[6:(5+nKnots)]
# make the grid over which to make pointwise estimates (~7040 points)
if(is.null(lonLatGrid)) {
latRange=range(slipDatCSZ$lat)
lonRange=range(slipDatCSZ$lon)
nx = 80
ny = 240
lonGrid = seq(lonRange[1], lonRange[2], l=nx)
latGrid = seq(latRange[1], latRange[2], l=ny)
lonLatGrid = make.surface.grid(list(lon=lonGrid, lat=latGrid))
lonLatGrid = data.frame(list(lon=lonLatGrid[,1], lat=lonLatGrid[,2]))
# make sure we only generate predictions over our fault geometry (which has some gaps in it)
lonLatGrid = as.matrix(getFaultGPSDat(lonLatGrid))
}
muZetaCSZ = rep(muZeta, nrow(lonLatGrid))
# get predicted depth at prediction locations
phiZeta = 232.5722
out = fastTps(cbind(slipDat$lon, slipDat$lat), slipDat$Depth, m=3, theta=phiZeta, lon.lat=TRUE,
Dist.args=list(miles=FALSE, method="greatcircle"))
depths = predict(out, lonLatGrid)
negDepth = depths < 0
depths[negDepth] = 0
# get tvec
predData = data.frame(list(latitude=lonLatGrid[,2], depth=depths))
tvec = getTaperSpline(taperPar, fault=predData, dStar=dStar, normalize=normalizeTaper)
# compute covariance at prediction locations
nuZeta=3/2
Sigma = stationary.cov(lonLatGrid, Covariance="Matern", Distance="rdist.earth", Dist.args=list(miles=FALSE),
theta=phiZeta, smoothness=nuZeta) * sigmaZeta^2
# generate predictive simulations
SigmaL = t(chol(Sigma))
zSims = matrix(rnorm(nsim*nrow(lonLatGrid)), nrow=nrow(lonLatGrid), ncol=nsim)
logZetaSims = sweep(SigmaL %*% zSims, 1, muZetaCSZ, "+") # add muZeta to each zero mean simulation
zetaSims = exp(logZetaSims)
slipSims = sweep(zetaSims, 1, tvec, FUN="*")
# get mean slip prediction field
meanSlip = exp(muZeta + diag(Sigma)/2) * tvec
return(list(meanSlip=meanSlip, slipSims=slipSims, Sigma=Sigma, lonLatGrid=lonLatGrid))
}
# generate predictions given only the parameter MLEs (no GPS or subsidence data) at
# GPS locations as well as areal averages over the fault grid cells.
predsArealAndLoc = function(params, nsim=100, fault=csz, gpsDat=slipDatCSZ, muVec=params[2],
normalizeTaper=FALSE, dStar=28000) {
# get fit MLEs
lambda = params[1]
muZeta = params[2]
sigmaZeta = params[3]
lambda0 = params[4]
muXi = params[5]
# get vector mean
if(length(muVec) == 1)
muVec = rep(muVec, nrow(fault)+nrow(gpsDat))
muVecGPS = muVec[1:nrow(gpsDat)]
muVecCSZ = muVec[(nrow(gpsDat)+1):length(muVec)]
# set other relevant parameters
nuZeta = 3/2 # Matern smoothness
phiZeta = 232.5722 # fit from fitGPSCovariance()
# Calculate the standard error vector of xi. Derivation from week 08_30_17.Rmd presentation.
# Transformation from additive error to multiplicative lognormal model with asympototic
# median and variance matching.
sigmaXi = sqrt(log(.5*(sqrt(4*gpsDat$slipErr^2/gpsDat$slip^2 + 1) + 1)))
# get CSZ prediction coordinates
xd = cbind(gpsDat$lon, gpsDat$lat)
xp = cbind(fault$longitude, fault$latitude)
nd = nrow(xd)
np = nrow(xp)
# compute relevant covariance matrices
# NOTE: previous code replaced with code calculating covariance between
# areal averages of zeta
# Sigma = stationary.cov(xp, Covariance="Matern", Distance="rdist.earth", Dist.args=list(miles=FALSE),
# theta=phiZeta, smoothness=nuZeta, onlyUpper=TRUE) * sigmaZeta^2
if(! identical(fault, csz))
SigmaCSZ = arealZetaCov(params, fault, nDown1=9, nStrike1=12)
else {
# in this case, it takes too long to compute. Load the precomputed correlation matrix.
arealCSZCor = getArealCorMat(fault)
SigmaCSZ = arealCSZCor * sigmaZeta^2
}
SigmaD = stationary.cov(xd, Covariance="Matern", Distance="rdist.earth", Dist.args=list(miles=FALSE),
theta=phiZeta, smoothness=nuZeta, onlyUpper=TRUE) * sigmaZeta^2
SigmaDtoP = pointArealZetaCov(params, xd, fault, nDown=9, nStrike=12)
SigmaPtoD = t(SigmaDtoP)
Sigma = cbind(rbind(SigmaD, SigmaPtoD), rbind(SigmaDtoP, SigmaCSZ))
SigmaL = t(chol(Sigma))
# generate predictive simulations
zSims = matrix(rnorm(nsim*(nrow(xp)+nrow(xd))), nrow=nrow(xp)+nrow(xd), ncol=nsim)
logZetaSims = sweep(SigmaL %*% zSims, 1, muVec, "+") # add muZeta (vector) to each zero mean simulation
zetaSims = exp(logZetaSims)
tvec = taper(c(gpsDat$Depth, getFaultCenters(fault)[,3]), lambda=lambda, normalize=normalizeTaper, dStar=dStar)
slipSims = sweep(zetaSims, 1, tvec, FUN="*")
# seperate areal average sims from GPS point location sims
point = 1:nd
areal = (nd+1):(nd+np)
slipSimsGPS = slipSims[point,]
slipSims = slipSims[areal,]
# get mean slip prediction field
meanSlip = rep(exp(muZeta) + SigmaCSZ^2/2, nrow(xp)) * tvec[areal]
return(list(meanSlip=meanSlip, slipSims=slipSims, slipSimsGPS=slipSimsGPS, Sigmac=Sigma,
muc=rep(muZeta, nrow(fault)), SigmacGPS = SigmaD, mucGPS=rep(muZeta, nrow(xd))))
}
# Compute subsidence from the prediction simulations using the Okada model (NOTE:
# returned ``subsidence'' is really uplift here).
# Preds is a list with elements named meanSlip (vector) and slipSims (matrix)
predsToSubsidence = function(params, preds, fault=csz, useMVNApprox=TRUE, G=NULL,
subDat=dr1, posNormalModel=FALSE, normalModel=posNormalModel, tvec=NULL,
normalizeTaper=FALSE, dStar=28000) {
# get fit MLEs
lambda = params[1]
muZeta = params[2]
sigmaZeta = params[3]
lambda0 = params[4]
muXi = params[5]
# get predictions from input list
meanSlip = preds$meanSlip
slipSims = preds$slipSims
# get taper if necessary
if(is.null(tvec))
tvec = taper(getFaultCenters(fault)[,3], lambda=lambda, normalize=normalizeTaper, dStar=dStar)
# get Okada linear transformation matrix
if(is.null(G)) {
nx = 300
ny= 900
lonGrid = seq(lonRange[1], lonRange[2], l=nx)
latGrid = seq(latRange[1], latRange[2], l=ny)
G = okadaAll(fault, lonGrid, latGrid, cbind(subDat$Lon, subDat$Lat), slip=1, poisson=lambda0)
}
# transform slips into subsidences
meanSub = G %*% cbind(meanSlip)
subSims = G %*% slipSims
# approximate upper and lower 95% quantiles (with either MVN approximate or simulations)
# NOTE: use preds$Sigma not preds$Sigmac since Sigmac gives the covariance in mean estimate
sigmaEps = subDat$Uncertainty
if(normalModel && !posNormalModel && useMVNApprox) {
subMVN = estSubsidenceMeanCov(preds$muc, lambda, preds$Sigma, G, fault=fault, subDat=subDat,
normalModel=TRUE, tvec=tvec)
subMu = subMVN$mu
subSigma = subMVN$Sigma
l95 = qnorm(.025, mean=subMu, sd=sqrt(diag(subSigma)))
u95 = qnorm(.975, mean=subMu, sd=sqrt(diag(subSigma)))
sigmaNoise = sqrt(diag(subSigma) + sigmaEps^2)
l95Noise = qnorm(.025, mean=subMu, sd=sigmaNoise)
u95Noise = qnorm(.975, mean=subMu, sd=sigmaNoise)
subSimsNoise=NULL
}
else if(useMVNApprox) {
subMVN = estSubsidenceMeanCov(preds$muc, lambda, preds$Sigma, G, subDat=subDat, fault=fault,
tvec=tvec)
subMu = subMVN$mu
subSigma = subMVN$Sigma
l95 = qnorm(.025, mean=subMu, sd=sqrt(diag(subSigma)))
u95 = qnorm(.975, mean=subMu, sd=sqrt(diag(subSigma)))
sigmaNoise = sqrt(diag(subSigma) + sigmaEps^2)
l95Noise = qnorm(.025, mean=subMu, sd=sigmaNoise)
u95Noise = qnorm(.975, mean=subMu, sd=sigmaNoise)
subSimsNoise=NULL
}
else {
l95 = apply(subSims, 1, quantile, probs=.025)
u95 = apply(subSims, 1, quantile, probs=.975)
noiseSims = matrix(rnorm(length(subSims), 0, sigmaEps), nrow=nrow(subSims))
subSimsNoise = subSims + noiseSims
l95Noise = apply(subSimsNoise, 1, quantile, probs=.025)
u95Noise = apply(subSimsNoise, 1, quantile, probs=.975)
}
# simulate middle 95% interval in observations
return(list(meanSub = meanSub, subSims = subSims, l95=l95, u95=u95, l95Noise=l95Noise,
u95Noise=u95Noise, noiseSims=subSimsNoise))
}
############################################################################
############################################################################
############################################################################
############################################################################
############################################################################
############################################################################
##### functions for bootstrapping
# Function for computing standard errors for nonnegative weighted least squares.
# Assumes regression coefficients follow a folded normal distribution, which is
# why traditional WLS estimates are needed in addition to the NNWLS estimates.
# - beta is the vector of nnls coefficient estimates
# - betaWLS is the vector of WLS coefficient estimates
# - V is the diagonal of the data variance matrix. Here we assume the
# variance matrix is itself diagonal as in weighted least squares.
# - A is the design matrix. In our case this is G %*% T
getNNLSSE = function(beta, betaWLS, V, A) {
# make the variance matrix
Vinv = diag(V^(-1))
Sigma = pseudoinverse(A %*% Vinv %*% t(A), 10^(-7))
# compute the parameter standard errors under normal assumption
SEs = sqrt(diag(Sigma))
# Under our conditional normal distribution (positive normal)
# the the distribution will be cut off at 0 so we need to modify
# the SEs to account for this:
return(SEs)
}
# NOTE: might want to also make a function for confidence intervals
# bootstrap residuals to get standard errors for NNLS fit
nnlsBootstrapSE = function(A, y, yFitted, nSamples=10000) {
resids = y - yFitted
n = length(y)
# store regression estimates (beta) in a matrix with nSamples rows
betas = matrix(nrow=nSamples, ncol=ncol(A))
for(i in 1:nSamples) {
# print progress
if(i %% 500 == 0)
print(paste0("iteration ", i, "/", nSamples))
# resample residuals and get nnls parameter estimates
epsStar = sample(resids, n, replace = TRUE)
yStar = yFitted + epsStar
nnlsMod = nnls(A, yStar)
betas[i,] = nnlsMod$x
}
# get boostrap marginal standard errors
SEs = apply(betas, 2, sd)
return(SEs)
}
############################################################################
############################################################################
############################################################################
############################################################################
############################################################################
############################################################################
##### functions for generating predictions when conditioning on all the data
##### Generate samples from the predictive distribution conditional on all the data
##### assuming log zeta, log X, and Y is all multivariate normal.
genFullPredsMVN = function(params, nsim=1000, sdAdd=0, normalizeTaper=FALSE, dStar=28000) {
# get fit MLEs
lambda = params[1]
muZeta = params[2]
sigmaZeta = params[3]
lambda0 = params[4]
muXi = params[5]
# set other relevant parameters
nuZeta = 3/2 # Matern smoothness
phiZeta = 232.5722 # fit from fitGPSCovariance()
# Calculate the standard error vector of xi. Derivation from week 08_30_17.Rmd presentation.
# Transformation from additive error to multiplicative lognormal model with asympototic
# median and variance matching.
sigmaXi = sqrt(log(.5*(sqrt(4*(slipDatCSZ$slipErr+sdAdd)^2/slipDatCSZ$slip^2 + 1) + 1)))
# get data
logX = log(slipDatCSZ$slip)
Y = -dr1$subsidence
# get Okada linear transformation matrix
nx = 300
ny= 900
lonGrid = seq(lonRange[1], lonRange[2], l=nx)
latGrid = seq(latRange[1], latRange[2], l=ny)
G = okadaAll(csz, lonGrid, latGrid, cbind(dr1$Lon, dr1$Lat), slip=1, poisson=lambda0)
# get taper vector
tvec = taper(csz$depth, lambda=lambda, normalize=normalizeTaper, dStar=dStar)
# compute G %*% T
GT = sweep(G, 2, tvec, "*")
# get coordinates for GPS data
xs = cbind(slipDatCSZ$lon, slipDatCSZ$lat)
# compute relevant covariances
arealCSZCor = getArealCorMat(fault)
SigmaB = arealCSZCor * sigmaZeta^2
SigmaSB = pointArealZetaCov(params, xs, csz, nDown=9, nStrike=12)
SigmaS = stationary.cov(xs, Covariance="Matern", theta=phiZeta,
smoothness=nuZeta, Distance="rdist.earth",
Dist.args=list(miles=FALSE)) * sigmaZeta^2
SigmaYMod = diag(exp(muZeta + diag(SigmaB)/2)) %*% t(GT)
SigmaBY = SigmaB %*% SigmaYMod
SigmaSY = SigmaSB %*% SigmaYMod
subDistn = getSubsidenceVarianceMat(params, fault = csz, G = G)
SigmaY = subDistn$Sigma
# now block them into predictions and data covariance matrices
SigmaP = cbind(rbind(SigmaB, SigmaSB), rbind(t(SigmaSB), SigmaS))
SigmaD = cbind(rbind(SigmaS + diag(sigmaXi^2), t(SigmaSY)), rbind(SigmaSY, SigmaY))
SigmaPtoD = cbind(rbind(t(SigmaSB), SigmaS), rbind(SigmaBY, SigmaSY))
# get block means
muP = muZeta
muD = c(rep(muZeta + muXi, length(logX)), GT %*% exp(muZeta + diag(SigmaB)/2))
# compute conditional normal mean and standard error in mean estimate
Xd = c(logX, Y)
condDistn = conditionalNormal(Xd, muP, muD, SigmaP, SigmaD, SigmaPtoD)
muc = condDistn$muc
Sigmac = condDistn$Sigmac
##### now generate the conditional simulations. Note that we still use the
##### marginal covariance structure, but we've conditionally updated the mean.
# get prediction locations
# get CSZ prediction coordinates
xd = cbind(slipDatCSZ$lon, slipDatCSZ$lat) # d is GPS locations
xp = cbind(csz$longitude, csz$latitude) # p is fault areal locations
nd = nrow(xd)
np = nrow(xp)
areal = 1:np
point = (np+1):(np+nd)
# Cholesky decomp used for simulations (don't add Sigmac because that
# can lead to non-physical results. Here we just treat the mean as
# constant and use the marginal variability)
# NOTE: deflate variance to account for increased variation due to
# conditional mean (using MSE = Var + bias^2 formula)
# SigmaPred = SigmaP + Sigmac
# varDeflation = 1 - mean((muc[point] - muZeta)^2)/sigmaZeta^2
# SigmaPred = SigmaP * varDeflation
# SigmaPred = SigmaP
SigmaPred = Sigmac
SigmaL = t(chol(SigmaPred))
# generate predictive simulations
zSims = matrix(rnorm(nsim*(nrow(xp)+nrow(xd))), nrow=nrow(xp)+nrow(xd), ncol=nsim)
logZetaSims0 = SigmaL %*% zSims # each column is a zero mean simulation
logZetaSims = sweep(logZetaSims0, 1, muc, "+") # add conditional mean to simulations
zetaSims = exp(logZetaSims)
tvec = taper(c(csz$depth, slipDatCSZ$Depth), lambda=lambda, normalize=normalizeTaper, dStar=dStar)
slipSims = sweep(zetaSims, 1, tvec, FUN="*")
# seperate areal average sims from GPS point location sims
slipSimsGPS = slipSims[point,]
slipSims = slipSims[areal,]
# get mean slip prediction field
meanSlip = exp(muc[areal] + diag(SigmaPred[areal,areal])/2) * tvec[areal]
meanSlipGPS = exp(muc[point] + diag(SigmaPred[point,point])/2) * tvec[point]
return(list(meanSlip=meanSlip, meanSlipGPS=meanSlipGPS, slipSims=slipSims, slipSimsGPS=slipSimsGPS,
Sigmac=Sigmac[areal, areal], muc=muc[areal], SigmacGPS = Sigmac[point, point],
mucGPS=muc[point], Sigma=SigmaPred[areal,areal], SigmaGPS=SigmaPred[point,point]))
}
##### Try importance sampling for approximate predictive distribution. Sample from
##### log zeta(B:) conditional on GPS and subsidence data under a MVN approximation
##### to the true predictive distribution, and weight by
##### f(X | log zeta(B:)) f(Y | log zeta(B:)) f(zeta) / [f(X) f(Y) f*(zeta)],
##### where f*(zeta) is the MVN approximation to the predictive distribution.
##### Once all samples are taken, the probability of accepting the sample is the
##### weight divided by the max weight of all the samples. Note that the hope is
##### that the distribution sampled from has a decent portion of the density
##### aligning with the high density regions of the weighting function. Otherwise
##### the estimate will be very poor.
genFullPreds = function(params, nsim=1000, normalizeTaper=FALSE, dStar=28000) {
# get fit MLEs
lambda = params[1]
muZeta = params[2]
sigmaZeta = params[3]
lambda0 = params[4]
muXi = params[5]
# set other relevant parameters
nuZeta = 3/2 # Matern smoothness
phiZeta = 232.5722 # fit from fitGPSCovariance()
# get Okada linear transformation matrix
nx = 300
ny= 900
lonGrid = seq(lonRange[1], lonRange[2], l=nx)
latGrid = seq(latRange[1], latRange[2], l=ny)
G = okadaAll(csz, lonGrid, latGrid, cbind(dr1$Lon, dr1$Lat), slip=1, poisson=lambda0)
# get taper vector
tvec = taper(csz$depth, lambda=lambda, normalize=normalizeTaper, dStar=dStar)
# generate simulations of zeta marginally
zeta = predsArealAndLoc(params, nsim)
zetaGPS = zeta$slipSimsGPS
zetaAreal = zeta$slipSims
# convert to subsidence simulations (compute G %*% T %*% zeta)
subs = G %*% sweep(zetaAreal, 1, tvec, "*")
## compute f(Y | zeta) for each simulation of zeta
eps = dr1$Uncertainty
diffs = sweep(subs, 1, -dr1$subsidence, "-")
logFYGivenZeta = apply(dnorm(diffs, 0, eps, log = TRUE), 2, sum)
## compute f(X | zeta) for each simulation of zeta
# Calculate the standard error vector of xi. Derivation from week 08_30_17.Rmd presentation.
# Transformation from additive error to multiplicative lognormal model with asympototic
# median and variance matching.
logX = log(slipDatCSZ$slip)
sigmaXi = sqrt(log(.5*(sqrt(4*slipDatCSZ$slipErr^2/slipDatCSZ$slip^2 + 1) + 1)))
diffs = sweep(zetaGPS, 1, logX, "-")
logFXGivenZeta = apply(dnorm(diffs, 0, sigmaXi, log = TRUE), 2, sum)
## compute f(Y)
# for now, we'll assume normality of Y even though that's not a great
# assumption.
arealCSZCor = getArealCorMat(fault)
SigmaZeta = arealCSZCor * sigmaZeta^2
moments = estSubsidenceMeanCov(muZeta, lambda, SigmaZeta, G, tvec, TRUE, csz)
muEst = moments$mu
SigmaEstU = chol(moments$Sigma + diag(dr1$Uncertainty^2))
logFY = logLikGP(-dr1$subsidence - muEst, SigmaEstU)
## compute f(X)
logXCntr = logX - muXi - muZeta
coords = cbind(slipDatCSZ$lon, slipDatCSZ$lat)
corMatGPS = stationary.cov(coords, Covariance="Matern", theta=phiZeta,
onlyUpper=TRUE, smoothness=nuZeta,
Distance="rdist.earth", Dist.args=list(miles=FALSE))
SigmaX = corMatGPS * sigmaZeta^2 + diag(sigmaXi^2)
logFX = logLikGP(logXCntr, chol(SigmaX))
# get sample weights
logWeights = logFXGivenZeta + logFYGivenZeta - logFY - logFX
weights = exp(logWeights)
# compute weighted samples
weightedZeta = exp(sweep(log(zeta), 2, logWeights, "+"))
# estimate mean, standard deviation, standard error
muEst = apply(weightedZeta, 1, mean)
sdEst = apply(weightedZeta, 1, sd)
seEst = sdEst/nsim
return(list(muEst=muEst, sdEst=sdEst, seEst=seEst, zetaGivenXSims=zetaGivenX, weightedZetaSims=weightedZetaSims))
}
##### Try importance sampling for full predictive distribution. Sample from
##### log zeta(B:) | X and weight by f(Y | log zeta(B:)) / f(Y). Alternatively,
##### one could sample from log zeta(B:) marginally and weight by
##### f(X | log zeta(B:)) f(Y | log zeta(B:)) / [f(X) f(Y)]. Once all samples
##### are taken, the probability of accepting the sample is the weight divided
##### by the max weight of all the samples. Note that the hope is that the
##### distribution sampled from is heavy tailed, according the rejection sampling
##### algorithm.
genFullPredsGPS = function(params, nsim=25000, normalizeTaper=FALSE, dStar=28000) {
# get fit MLEs
lambda = params[1]
muZeta = params[2]
sigmaZeta = params[3]
lambda0 = params[4]
muXi = params[5]
# get Okada linear transformation matrix
nx = 300
ny= 900
lonGrid = seq(lonRange[1], lonRange[2], l=nx)
latGrid = seq(latRange[1], latRange[2], l=ny)
G = okadaAll(csz, lonGrid, latGrid, cbind(dr1$Lon, dr1$Lat), slip=1, poisson=lambda0)
# get taper vector
tvec = taper(csz$depth, lambda=lambda, normalize=normalizeTaper, dStar=dStar)
# generate simulations of zeta given the GPS data
zetaGivenX = predsGivenGPS(params, nsim)$slipSims
# convert to subsidence simulations (compute G %*% T %*% zeta | X)
subsGivenX = G %*% sweep(zetaGivenX, 1, tvec, "*")