@@ -342,7 +342,7 @@ def bpic(trace, model=None):
342
342
343
343
344
344
def compare (traces , models , ic = 'WAIC' , method = 'stacking' , b_samples = 1000 ,
345
- alpha = 1 , seed = None ):
345
+ alpha = 1 , seed = None , round_to = 2 ):
346
346
"""Compare models based on the widely available information criterion (WAIC)
347
347
or leave-one-out (LOO) cross-validation.
348
348
Read more theory here - in a paper by some of the leading authorities on
@@ -378,6 +378,8 @@ def compare(traces, models, ic='WAIC', method='stacking', b_samples=1000,
378
378
If int or RandomState, use it for seeding Bayesian bootstrap. Only
379
379
useful when method = 'BB-pseudo-BMA'. Default None the global
380
380
np.random state is used.
381
+ round_to : int
382
+ Number of decimals used to round results (default 2).
381
383
382
384
Returns
383
385
-------
@@ -421,11 +423,11 @@ def compare(traces, models, ic='WAIC', method='stacking', b_samples=1000,
421
423
422
424
if len (set ([len (m .observed_RVs ) for m in models ])) != 1 :
423
425
raise ValueError (
424
- 'The Observed RVs should be the same across all models' )
426
+ 'The number of observed RVs should be the same across all models' )
425
427
426
428
if method not in ['stacking' , 'BB-pseudo-BMA' , 'pseudo-BMA' ]:
427
- raise NotImplementedError (
428
- 'The method to compute weights {} is not supported.' .format (method ))
429
+ raise ValueError ( 'The method {}, to compute weights,'
430
+ ' is not supported.' .format (method ))
429
431
430
432
warns = np .zeros (len (models ))
431
433
@@ -449,7 +451,7 @@ def add_warns(*args):
449
451
Km = K - 1
450
452
451
453
def w_fuller (w ):
452
- return np .concatenate ((w , 1. - np .sum (w , keepdims = True ) ))
454
+ return np .concatenate ((w , [ max ( 1. - np .sum (w ), 0. )] ))
453
455
454
456
def log_score (w ):
455
457
w_full = w_fuller (w )
@@ -514,7 +516,12 @@ def gradient(w):
514
516
d_se = np .sqrt (len (diff ) * np .var (diff ))
515
517
se = ses [i ]
516
518
weight = weights [i ]
517
- df_comp .at [idx ] = (res [0 ], res [2 ], d_ic , weight , se , d_se ,
519
+ df_comp .at [idx ] = (round (res [0 ], round_to ),
520
+ round (res [2 ], round_to ),
521
+ round (d_ic , round_to ),
522
+ round (weight , round_to ),
523
+ round (se , round_to ),
524
+ round (d_se , round_to ),
518
525
warns [idx ])
519
526
520
527
return df_comp .sort_values (by = ic )
@@ -526,10 +533,15 @@ def _ic_matrix(ics):
526
533
"""
527
534
N = len (ics [0 ][1 ][3 ])
528
535
K = len (ics )
529
-
530
536
ic_i = np .zeros ((N , K ))
537
+
531
538
for i in range (K ):
532
- ic_i [:, i ] = ics [i ][1 ][3 ]
539
+ ic = ics [i ][1 ][3 ]
540
+ if len (ic ) != N :
541
+ raise ValueError ('The number of observations should be the same '
542
+ 'across all models' )
543
+ else :
544
+ ic_i [:, i ] = ic
533
545
534
546
return N , K , ic_i
535
547
0 commit comments