-
Notifications
You must be signed in to change notification settings - Fork 9
/
pyhddmjagsutils.py
784 lines (650 loc) · 30.4 KB
/
pyhddmjagsutils.py
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
# pyhddmjagsutils.py - Definitions for simulation, model diagnostics, and parameter recovery
#
# Copyright (C) 2021 Michael D. Nunez, <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Record of Revisions
#
# Date Programmers Descriptions of Change
# ==== ================ ======================
# 06/29/20 Michael Nunez Original code
# 12/04/20 Michael Nunez Update explanation of summary output
# 01/14/21 Michael Nunez Add simuldiff2ndt() and flipstanout()
# 28-Feb-22 Michael Nunez Simulate diffusion model directly
# Modules
import numpy as np
import numpy.matlib
import pyjags
import scipy.io as sio
from scipy import stats
import warnings
import os
import matplotlib.pyplot as plt
### Definitions ###
# Simulate diffusion models slowly with intrinsic trial-to-trial variability in parameters
def simul_ratcliff_slow(N=100, Alpha=1, Tau=.4, Nu=1, Beta=.5, rangeTau=0, rangeBeta=0, Eta=.3, Varsigma=1, nsteps=300, step_length=.01):
"""
SIMUL_RATCLIFF_SLOW Generates data according to a drift diffusion model with optional trial-to-trial variability
Parameters
----------
N: a integer denoting the size of the output vector
(defaults to 100 experimental trials)
Alpha: the mean boundary separation across trials in evidence units
(defaults to 1 evidence unit)
Tau: the mean non-decision time across trials in seconds
(defaults to .4 seconds)
Nu: the mean drift rate across trials in evidence units per second
(defaults to 1 evidence units per second, restricted to -5 to 5 units)
Beta: the initial bias in the evidence process for choice A as a proportion of boundary Alpha
(defaults to .5 or 50% of total evidence units given by Alpha)
rangeTau: Non-decision time across trials is generated from a uniform
distribution of Tau - rangeTau/2 to Tau + rangeTau/2 across trials
(defaults to 0 seconds)
rangeZeta: Bias across trials is generated from a uniform distribution
of Zeta - rangeZeta/2 to Zeta + rangeZeta/2 across trials
(defaults to 0 evidence units)
Eta: Standard deviation of the drift rate across trials
(defaults to 3 evidence units per second, restricted to less than 3 evidence units)
Varsigma: The diffusion coefficient, the standard deviation of the
evidence accumulation process within one trial. It is recommended that
this parameter be kept fixed unless you have reason to explore this parameter
(defaults to 1 evidence unit per second)
Returns
-------
Numpy array with reaction times (in seconds) multiplied by the response vector
such that negative reaction times encode response B and positive reaction times
encode response A
"""
if (Nu < -5) or (Nu > 5):
Nu = np.sign(Nu)*5
warnings.warn('Nu is not in the range [-5 5], bounding drift rate to %.1f...' % (Nu))
if (Eta > 3):
warning.warn('Standard deviation of drift rate is out of bounds, bounding drift rate to 3')
eta = 3
if (Eta == 0):
Eta = 1e-16
# Initialize output vectors
rts = np.zeros(N)
choice = np.zeros(N)
for n in range(0,N):
random_walk = np.empty(nsteps)
start_point = np.random.uniform(Beta - rangeBeta/2,
Beta + rangeBeta/2)
ndt = np.random.uniform(Tau - rangeTau/2, Tau + rangeTau/2)
drift = stats.norm.rvs(loc=Nu, scale=Eta)
random_walk[0] = start_point*Alpha
for s in range(1,nsteps):
random_walk[s] = random_walk[s-1] + stats.norm.rvs(loc=drift*step_length,
scale=Varsigma*np.sqrt(step_length))
if random_walk[s] >= Alpha:
random_walk[s:] = Alpha
rts[n] = s*step_length + ndt
choice[n] = 1
break
elif random_walk[s] <= 0:
random_walk[s:] = 0
rts[n] = s*step_length + ndt
choice[n] = -1
break
elif s == (nsteps-1):
rts[n] = np.nan
choice[n] = np.nan
break
result = rts*choice
return result
# Simulate diffusion models quickly with intrinsic trial-to-trial variability in parameters
def simulratcliff(N=100,Alpha=1,Tau=.4,Nu=1,Beta=.5,rangeTau=0,rangeBeta=0,Eta=.3,Varsigma=1):
"""
SIMULRATCLIFF Generates data according to a drift diffusion model with optional trial-to-trial variability
Reference:
Tuerlinckx, F., Maris, E.,
Ratcliff, R., & De Boeck, P. (2001). A comparison of four methods for
simulating the diffusion process. Behavior Research Methods,
Instruments, & Computers, 33, 443-456.
Parameters
----------
N: a integer denoting the size of the output vector
(defaults to 100 experimental trials)
Alpha: the mean boundary separation across trials in evidence units
(defaults to 1 evidence unit)
Tau: the mean non-decision time across trials in seconds
(defaults to .4 seconds)
Nu: the mean drift rate across trials in evidence units per second
(defaults to 1 evidence units per second, restricted to -5 to 5 units)
Beta: the initial bias in the evidence process for choice A as a proportion of boundary Alpha
(defaults to .5 or 50% of total evidence units given by Alpha)
rangeTau: Non-decision time across trials is generated from a uniform
distribution of Tau - rangeTau/2 to Tau + rangeTau/2 across trials
(defaults to 0 seconds)
rangeZeta: Bias across trials is generated from a uniform distribution
of Zeta - rangeZeta/2 to Zeta + rangeZeta/2 across trials
(defaults to 0 evidence units)
Eta: Standard deviation of the drift rate across trials
(defaults to 3 evidence units per second, restricted to less than 3 evidence units)
Varsigma: The diffusion coefficient, the standard deviation of the
evidence accumulation process within one trial. It is recommended that
this parameter be kept fixed unless you have reason to explore this parameter
(defaults to 1 evidence unit per second)
Returns
-------
Numpy array with reaction times (in seconds) multiplied by the response vector
such that negative reaction times encode response B and positive reaction times
encode response A
Converted from simuldiff.m MATLAB script by Joachim Vandekerckhove
See also http://ppw.kuleuven.be/okp/dmatoolbox.
"""
if (Nu < -5) or (Nu > 5):
Nu = np.sign(Nu)*5
warnings.warn('Nu is not in the range [-5 5], bounding drift rate to %.1f...' % (Nu))
if (Eta > 3):
warning.warn('Standard deviation of drift rate is out of bounds, bounding drift rate to 3')
eta = 3
if (Eta == 0):
Eta = 1e-16
#Initialize output vectors
result = np.zeros(N)
T = np.zeros(N)
XX = np.zeros(N)
#Called sigma in 2001 paper
D = np.power(Varsigma,2)/2
#Program specifications
eps = 2.220446049250313e-16 #precision from 1.0 to next double-precision number
delta=eps
for n in range(0,N):
r1 = np.random.normal()
mu = Nu + r1*Eta
bb = Beta - rangeBeta/2 + rangeBeta*np.random.uniform()
zz = bb*Alpha
finish = 0
totaltime = 0
startpos = 0
Aupper = Alpha - zz
Alower = -zz
radius = np.min(np.array([np.abs(Aupper), np.abs(Alower)]))
while (finish==0):
lambda_ = 0.25*np.power(mu,2)/D + 0.25*D*np.power(np.pi,2)/np.power(radius,2)
# eq. formula (13) in 2001 paper with D = sigma^2/2 and radius = Alpha/2
F = D*np.pi/(radius*mu)
F = np.power(F,2)/(1 + np.power(F,2) )
# formula p447 in 2001 paper
prob = np.exp(radius*mu/D)
prob = prob/(1 + prob)
dir_ = 2*(np.random.uniform() < prob) - 1
l = -1
s2 = 0
while (s2>l):
s2=np.random.uniform()
s1=np.random.uniform()
tnew=0
told=0
uu=0
while (np.abs(tnew-told)>eps) or (uu==0):
told=tnew
uu=uu+1
tnew = told + (2*uu+1) * np.power(-1,uu) * np.power(s1,(F*np.power(2*uu+1,2)));
# infinite sum in formula (16) in BRMIC,2001
l = 1 + np.power(s1,(-F)) * tnew;
# rest of formula (16)
t = np.abs(np.log(s1))/lambda_;
# is the negative of t* in (14) in BRMIC,2001
totaltime=totaltime+t
dir_=startpos+dir_*radius
ndt = Tau - rangeTau/2 + rangeTau*np.random.uniform()
if ( (dir_ + delta) > Aupper):
T[n]=ndt+totaltime
XX[n]=1
finish=1
elif ( (dir_-delta) < Alower ):
T[n]=ndt+totaltime
XX[n]=-1
finish=1
else:
startpos=dir_
radius=np.min(np.abs([Aupper, Alower]-startpos))
result = T*XX
return result
def simuldiff2ndt(N=100,Alpha=1,Vet=.2,Rmr=.2,Nu=1,Zeta=None,rangeVet=0,rangeRmr=0,rangeZeta=0,Eta=.3,Varsigma=1):
"""
SIMULDIFF2NDT Generates data according to a diffusion model with non-decision time split into two parts with independent variance
Reference:
Tuerlinckx, F., Maris, E.,
Ratcliff, R., & De Boeck, P. (2001). A comparison of four methods for
simulating the diffusion process. Behavior Research Methods,
Instruments, & Computers, 33, 443-456.
Parameters
----------
N: a integer denoting the size of the output vector
(defaults to 100 experimental trials)
Alpha: the mean boundary separation across trials in evidence units
(defaults to 1 evidence unit)
Vet: the mean visual encoding time across trials in seconds
(defaults to .2 seconds)
Rmr: the mean residual motor response time across trials in seconds
(defaults to .2 seconds)
Nu: the mean drift rate across trials in evidence units per second
(defaults to 1 evidence units per second, restricted to -5 to 5 units)
Zeta: the initial bias in the evidence process for choice A
(defaults to 50% of total evidence units given by Alpha)
rangeVet: Visual encoding time across trials is generated from a uniform
distribution of Vet - rangeVet/2 to Vet + rangeVet/2 across trials
(defaults to 0 seconds)
rangeRmr: Residual motor response time across trials is generated from a uniform
distribution of Rmr - rangeRmr/2 to Rmr + rangeRmr/2 across trials
(defaults to 0 seconds)
rangeZeta: Bias across trials is generated from a uniform distribution
of Zeta - rangeZeta/2 to Zeta + rangeZeta/2 across trials
(defaults to 0 evidence units)
Eta: Standard deviation of the drift rate across trials
(defaults to 3 evidence units per second, restricted to less than 3 evidence units)
Varsigma: The diffusion coefficient, the standard deviation of the
evidence accumulation process within one trial. It is recommended that
this parameter be kept fixed unless you have reason to explore this parameter
(defaults to 1 evidence unit per second)
Returns
-------
Numpy complex vector with 1) Real component ( np.real(x) ): reaction times (in seconds) multiplied by the response vector
such that negative reaction times encode response B and positive reaction times
encode response A and 2) Imaginary component ( np.imag(x) ): N200 peak-latencies in seconds
Converted from simuldiff.m MATLAB script by Joachim Vandekerckhove
See also http://ppw.kuleuven.be/okp/dmatoolbox.
"""
if Zeta is None:
Zeta = .5*Alpha
if (Nu < -5) or (Nu > 5):
Nu = np.sign(Nu)*5
warnings.warn('Nu is not in the range [-5 5], bounding drift rate to %.1f...' % (Nu))
if (Eta > 3):
warning.warn('Standard deviation of drift rate is out of bounds, bounding drift rate to 3')
eta = 3
if (Eta == 0):
Eta = 1e-16
#Initialize output vectors
result = np.zeros(N)
T = np.zeros(N)
XX = np.zeros(N)
N200 = np.zeros(N)
#Called sigma in 2001 paper
D = np.power(Varsigma,2)/2
#Program specifications
eps = 2.220446049250313e-16 #precision from 1.0 to next double-precision number
delta=eps
for n in range(0,N):
r1 = np.random.normal()
mu = Nu + r1*Eta
zz = Zeta - rangeZeta/2 + rangeZeta*np.random.uniform()
finish = 0
totaltime = 0
startpos = 0
Aupper = Alpha - zz
Alower = -zz
radius = np.min(np.array([np.abs(Aupper), np.abs(Alower)]))
while (finish==0):
lambda_ = 0.25*np.power(mu,2)/D + 0.25*D*np.power(np.pi,2)/np.power(radius,2)
# eq. formula (13) in 2001 paper with D = sigma^2/2 and radius = Alpha/2
F = D*np.pi/(radius*mu)
F = np.power(F,2)/(1 + np.power(F,2) )
# formula p447 in 2001 paper
prob = np.exp(radius*mu/D)
prob = prob/(1 + prob)
dir_ = 2*(np.random.uniform() < prob) - 1
l = -1
s2 = 0
while (s2>l):
s2=np.random.uniform()
s1=np.random.uniform()
tnew=0
told=0
uu=0
while (np.abs(tnew-told)>eps) or (uu==0):
told=tnew
uu=uu+1
tnew = told + (2*uu+1) * np.power(-1,uu) * np.power(s1,(F*np.power(2*uu+1,2)));
# infinite sum in formula (16) in BRMIC,2001
l = 1 + np.power(s1,(-F)) * tnew;
# rest of formula (16)
t = np.abs(np.log(s1))/lambda_;
# is the negative of t* in (14) in BRMIC,2001
totaltime=totaltime+t
dir_=startpos+dir_*radius
vetime = Vet - rangeVet/2 + rangeVet*np.random.uniform()
rmrt = Rmr - rangeRmr/2 + rangeRmr*np.random.uniform()
if ( (dir_ + delta) > Aupper):
T[n]=vetime+totaltime+rmrt
XX[n]=1
N200[n]=vetime
finish=1
elif ( (dir_-delta) < Alower ):
T[n]=vetime+totaltime+rmrt
XX[n]=-1
N200[n]=vetime
finish=1
else:
startpos=dir_
radius=np.min(np.abs([Aupper, Alower]-startpos))
result = T*XX + N200*1.j
return result
def diagnostic(insamples):
"""
Returns two versions of Rhat (measure of convergence, less is better with an approximate
1.10 cutoff) and Neff, number of effective samples). Note that 'rhat' is more diagnostic than 'oldrhat' according to
Gelman et al. (2014).
Reference for preferred Rhat calculation (split chains) and number of effective sample calculation:
Gelman, A., Carlin, J. B., Stern, H. S., Dunson, D. B., Vehtari, A. & Rubin, D. B. (2014).
Bayesian data analysis (Third Edition). CRC Press:
Boca Raton, FL
Reference for original Rhat calculation:
Gelman, A., Carlin, J., Stern, H., & Rubin D., (2004).
Bayesian Data Analysis (Second Edition). Chapman & Hall/CRC:
Boca Raton, FL.
Parameters
----------
insamples: dic
Sampled values of monitored variables as a dictionary where keys
are variable names and values are numpy arrays with shape:
(dim_1, dim_n, iterations, chains). dim_1, ..., dim_n describe the
shape of variable in JAGS model.
Returns
-------
dict:
rhat, oldrhat, neff, posterior mean, and posterior std for each variable. Prints maximum Rhat and minimum Neff across all variables
"""
result = {} # Initialize dictionary
maxrhatsold = np.zeros((len(insamples.keys())), dtype=float)
maxrhatsnew = np.zeros((len(insamples.keys())), dtype=float)
minneff = np.ones((len(insamples.keys())), dtype=float)*np.inf
allkeys ={} # Initialize dictionary
keyindx = 0
for key in insamples.keys():
if key[0] != '_':
result[key] = {}
possamps = insamples[key]
# Number of chains
nchains = possamps.shape[-1]
# Number of samples per chain
nsamps = possamps.shape[-2]
# Number of variables per key
nvars = np.prod(possamps.shape[0:-2])
# Reshape data
allsamps = np.reshape(possamps, possamps.shape[:-2] + (nchains * nsamps,))
# Reshape data to preduce R_hatnew
possampsnew = np.empty(possamps.shape[:-2] + (int(nsamps/2), nchains * 2,))
newc=0
for c in range(nchains):
possampsnew[...,newc] = np.take(np.take(possamps,np.arange(0,int(nsamps/2)),axis=-2),c,axis=-1)
possampsnew[...,newc+1] = np.take(np.take(possamps,np.arange(int(nsamps/2),nsamps),axis=-2),c,axis=-1)
newc += 2
# Index of variables
varindx = np.arange(nvars).reshape(possamps.shape[0:-2])
# Reshape data
alldata = np.reshape(possamps, (nvars, nsamps, nchains))
# Mean of each chain for rhat
chainmeans = np.mean(possamps, axis=-2)
# Mean of each chain for rhatnew
chainmeansnew = np.mean(possampsnew, axis=-2)
# Global mean of each parameter for rhat
globalmean = np.mean(chainmeans, axis=-1)
globalmeannew = np.mean(chainmeansnew, axis=-1)
result[key]['mean'] = globalmean
result[key]['std'] = np.std(allsamps, axis=-1)
globalmeanext = np.expand_dims(
globalmean, axis=-1) # Expand the last dimension
globalmeanext = np.repeat(
globalmeanext, nchains, axis=-1) # For differencing
globalmeanextnew = np.expand_dims(
globalmeannew, axis=-1) # Expand the last dimension
globalmeanextnew = np.repeat(
globalmeanextnew, nchains*2, axis=-1) # For differencing
# Between-chain variance for rhat
between = np.sum(np.square(chainmeans - globalmeanext),
axis=-1) * nsamps / (nchains - 1.)
# Mean of the variances of each chain for rhat
within = np.mean(np.var(possamps, axis=-2), axis=-1)
# Total estimated variance for rhat
totalestvar = (1. - (1. / nsamps)) * \
within + (1. / nsamps) * between
# Rhat (original Gelman-Rubin statistic)
temprhat = np.sqrt(totalestvar / within)
maxrhatsold[keyindx] = np.nanmax(temprhat) # Ignore NANs
allkeys[keyindx] = key
result[key]['oldrhat'] = temprhat
# Between-chain variance for rhatnew
betweennew = np.sum(np.square(chainmeansnew - globalmeanextnew),
axis=-1) * (nsamps/2) / ((nchains*2) - 1.)
# Mean of the variances of each chain for rhatnew
withinnew = np.mean(np.var(possampsnew, axis=-2), axis=-1)
# Total estimated variance
totalestvarnew = (1. - (1. / (nsamps/2))) * \
withinnew + (1. / (nsamps/2)) * betweennew
# Rhatnew (Gelman-Rubin statistic from Gelman et al., 2013)
temprhatnew = np.sqrt(totalestvarnew / withinnew)
maxrhatsnew[keyindx] = np.nanmax(temprhatnew) # Ignore NANs
result[key]['rhat'] = temprhatnew
# Number of effective samples from Gelman et al. (2013) 286-288
neff = np.empty(possamps.shape[0:-2])
for v in range(0, nvars):
whereis = np.where(varindx == v)
rho_hat = []
rho_hat_even = 0
rho_hat_odd = 0
t = 2
while ((t < nsamps - 2) & (float(rho_hat_even) + float(rho_hat_odd) >= 0)):
variogram_odd = np.mean(np.mean(np.power(alldata[v,(t-1):nsamps,:] - alldata[v,0:(nsamps-t+1),:],2),axis=0)) # above equation (11.7) in Gelman et al., 2013
rho_hat_odd = 1 - np.divide(variogram_odd, 2*totalestvar[whereis]) # Equation (11.7) in Gelman et al., 2013
rho_hat.append(rho_hat_odd)
variogram_even = np.mean(np.mean(np.power(alldata[v,t:nsamps,:] - alldata[v,0:(nsamps-t),:],2),axis=0)) # above equation (11.7) in Gelman et al., 2013
rho_hat_even = 1 - np.divide(variogram_even, 2*totalestvar[whereis]) # Equation (11.7) in Gelman et al., 2013
rho_hat.append(rho_hat_even)
t += 2
rho_hat = np.asarray(rho_hat)
neff[whereis] = np.divide(nchains*nsamps, 1 + 2*np.sum(rho_hat)) # Equation (11.8) in Gelman et al., 2013
result[key]['neff'] = np.round(neff)
minneff[keyindx] = np.nanmin(np.round(neff))
keyindx += 1
# Geweke statistic?
# print("Maximum old Rhat was %3.2f for variable %s" % (np.max(maxrhatsold),allkeys[np.argmax(maxrhatsold)]))
maxrhatkey = allkeys[np.argmax(maxrhatsnew)]
maxrhatindx = np.unravel_index( np.argmax(result[maxrhatkey]['rhat']) , result[maxrhatkey]['rhat'].shape)
print("Maximum Rhat was %3.2f for variable %s at index %s" % (np.max(maxrhatsnew), maxrhatkey, maxrhatindx))
minneffkey = allkeys[np.argmin(minneff)]
minneffindx = np.unravel_index( np.argmin(result[minneffkey]['neff']) , result[minneffkey]['neff'].shape)
print("Minimum number of effective samples was %d for variable %s at index %s" % (np.min(minneff), minneffkey, minneffindx))
return result
def summary(insamples):
"""
Returns parameter estimates for each posterior distribution (mean and median posteriors) as well as 95% and 99% credible intervals (.5th, 2.5th, 97.5th, 99.5th percentiles)
Parameters
----------
insamples: dic
Sampled values of monitored variables as a dictionary where keys
are variable names and values are numpy arrays with shape:
(dim_1, dim_n, iterations, chains). dim_1, ..., dim_n describe the
shape of variable in JAGS model.
"""
result = {} # Initialize dictionary
maxrhats = np.zeros((len(insamples.keys())), dtype=float)
maxrhatsnew = np.zeros((len(insamples.keys())), dtype=float)
minneff = np.ones((len(insamples.keys())), dtype=float)*np.inf
allkeys ={} # Initialize dictionary
keyindx = 0
for key in insamples.keys():
if key[0] != '_':
result[key] = {}
possamps = insamples[key]
# Number of chains
nchains = possamps.shape[-1]
# Number of samples per chain
nsamps = possamps.shape[-2]
# Number of variables per key
nvars = np.prod(possamps.shape[0:-2])
# Reshape data
allsamps = np.reshape(possamps, possamps.shape[:-2] + (nchains * nsamps,))
# Reshape data to preduce R_hatnew
possampsnew = np.empty(possamps.shape[:-2] + (int(nsamps/2), nchains * 2,))
newc=0
for c in range(nchains):
possampsnew[...,newc] = np.take(np.take(possamps,np.arange(0,int(nsamps/2)),axis=-2),c,axis=-1)
possampsnew[...,newc+1] = np.take(np.take(possamps,np.arange(int(nsamps/2),nsamps),axis=-2),c,axis=-1)
newc += 2
result[key]['mean'] = np.mean(allsamps, axis=-1)
result[key]['std'] = np.std(allsamps, axis=-1)
result[key]['median'] = np.quantile(allsamps,0.5, axis=-1)
result[key]['95lower'] = np.quantile(allsamps,0.025, axis=-1)
result[key]['95upper'] = np.quantile(allsamps,0.975, axis=-1)
result[key]['99lower'] = np.quantile(allsamps,0.005, axis=-1)
result[key]['99upper'] = np.quantile(allsamps,0.995, axis=-1)
return result
def flipstanout(insamples):
result = {} # Initialize dictionary
allkeys ={} # Initialize dictionary
keyindx = 0
for key in insamples.keys():
if key[0] != '_':
possamps = insamples[key]
transamps = np.moveaxis(possamps,0,-1)
bettersamps = np.moveaxis(transamps,0,-1)
if len(bettersamps.shape) == 2:
reshapedsamps = np.reshape(bettersamps, (1,) + bettersamps.shape[0:2])
result[key] = reshapedsamps
else:
result[key] = bettersamps
return result
def jellyfish(possamps): # jellyfish plots
"""Plots posterior distributions of given posterior samples in a jellyfish
plot. Jellyfish plots are posterior distributions (mirrored over their
horizontal axes) with 99% and 95% credible intervals (currently plotted
from the .5% and 99.5% & 2.5% and 97.5% percentiles respectively.
Also plotted are the median and mean of the posterior distributions"
Parameters
----------
possamps : ndarray of posterior chains where the last dimension is
the number of chains, the second to last dimension is the number of samples
in each chain, all other dimensions describe the shape of the parameter
"""
# Number of chains
nchains = possamps.shape[-1]
# Number of samples per chain
nsamps = possamps.shape[-2]
# Number of dimensions
ndims = possamps.ndim - 2
# Number of variables to plot
nvars = np.prod(possamps.shape[0:-2])
# Index of variables
varindx = np.arange(nvars).reshape(possamps.shape[0:-2])
# Reshape data
alldata = np.reshape(possamps, (nvars, nchains, nsamps))
alldata = np.reshape(alldata, (nvars, nchains * nsamps))
# Plot properties
LineWidths = np.array([2, 5])
teal = np.array([0, .7, .7])
blue = np.array([0, 0, 1])
orange = np.array([1, .3, 0])
Colors = [teal, blue]
# Initialize ylabels list
ylabels = ['']
for v in range(0, nvars):
# Create ylabel
whereis = np.where(varindx == v)
newlabel = ''
for l in range(0, ndims):
newlabel = newlabel + ('_%i' % whereis[l][0])
ylabels.append(newlabel)
# Compute posterior density curves
kde = stats.gaussian_kde(alldata[v, :])
bounds = stats.scoreatpercentile(alldata[v, :], (.5, 2.5, 97.5, 99.5))
for b in range(0, 2):
# Bound by .5th percentile and 99.5th percentile
x = np.linspace(bounds[b], bounds[-1 - b], 100)
p = kde(x)
# Scale distributions down
maxp = np.max(p)
# Plot jellyfish
upper = .25 * p / maxp + v + 1
lower = -.25 * p / maxp + v + 1
lines = plt.plot(x, upper, x, lower)
plt.setp(lines, color=Colors[b], linewidth=LineWidths[b])
if b == 1:
# Mark mode
wheremaxp = np.argmax(p)
mmode = plt.plot(np.array([1., 1.]) * x[wheremaxp],
np.array([lower[wheremaxp], upper[wheremaxp]]))
plt.setp(mmode, linewidth=3, color=orange)
# Mark median
mmedian = plt.plot(np.median(alldata[v, :]), v + 1, 'ko')
plt.setp(mmedian, markersize=10, color=[0., 0., 0.])
# Mark mean
mmean = plt.plot(np.mean(alldata[v, :]), v + 1, '*')
plt.setp(mmean, markersize=10, color=teal)
# Display plot
plt.setp(plt.gca(), yticklabels=ylabels, yticks=np.arange(0, nvars + 1))
def recovery(possamps, truevals): # Parameter recovery plots
"""Plots true parameters versus 99% and 95% credible intervals of recovered
parameters. Also plotted are the median (circles) and mean (stars) of the posterior
distributions.
Parameters
----------
possamps : ndarray of posterior chains where the last dimension is the
number of chains, the second to last dimension is the number of samples in
each chain, all other dimensions must match the dimensions of truevals
truevals : ndarray of true parameter values
"""
# Number of chains
nchains = possamps.shape[-1]
# Number of samples per chain
nsamps = possamps.shape[-2]
# Number of variables to plot
nvars = np.prod(possamps.shape[0:-2])
# Reshape data
alldata = np.reshape(possamps, (nvars, nchains, nsamps))
alldata = np.reshape(alldata, (nvars, nchains * nsamps))
truevals = np.reshape(truevals, (nvars))
# Plot properties
LineWidths = np.array([2, 5])
teal = np.array([0, .7, .7])
blue = np.array([0, 0, 1])
orange = np.array([1, .3, 0])
Colors = [teal, blue]
for v in range(0, nvars):
# Compute percentiles
bounds = stats.scoreatpercentile(alldata[v, :], (.5, 2.5, 97.5, 99.5))
for b in range(0, 2):
# Plot credible intervals
credint = np.ones(100) * truevals[v]
y = np.linspace(bounds[b], bounds[-1 - b], 100)
lines = plt.plot(credint, y)
plt.setp(lines, color=Colors[b], linewidth=LineWidths[b])
if b == 1:
# Mark median
mmedian = plt.plot(truevals[v], np.median(alldata[v, :]), 'o')
plt.setp(mmedian, markersize=10, color=[0., 0., 0.])
# Mark mean
mmean = plt.plot(truevals[v], np.mean(alldata[v, :]), '*')
plt.setp(mmean, markersize=10, color=teal)
# Plot line y = x
tempx = np.linspace(np.min(truevals), np.max(
truevals), num=100)
recoverline = plt.plot(tempx, tempx)
plt.setp(recoverline, linewidth=3, color=orange)
def rsquared_pred(trueval,predval):
"""
RSQUARED_PRED Calculates R^2_prediction for data and statistics derived from data
"""
divisor = np.sum(np.isfinite(trueval)) -1
# Mean squared error of prediction
MSEP = np.nansum(np.power(trueval - predval,2)) / divisor
# Variance estimate of the true values
vartrue = np.nansum(np.power(trueval - np.nanmean(trueval),2)) / divisor
# R-squared definition
rsquared = 1 - (MSEP / vartrue)
return rsquared