@@ -486,80 +486,79 @@ def create_bayesian_tear_sheet_oos(returns, live_start_date,
486
486
start = returns .index [0 ],
487
487
end = returns .index [- 1 ])
488
488
489
- if live_start_date is not None :
490
- live_start_date = utils .get_utc_timestamp (live_start_date )
491
- df_train = returns .loc [returns .index < live_start_date ]
492
- df_test = returns .loc [returns .index >= live_start_date ]
493
-
494
- # Run T model with missing data
495
- trace_t = bayesian .run_model ('t' , df_train , returns_test = df_test ,
496
- samples = 2000 )
497
-
498
- # Compute BEST model
499
- trace_best = bayesian .run_model ('best' , df_train ,
500
- returns_test = df_test ,
501
- samples = 2000 )
502
-
503
- # Plot results
504
-
505
- fig = plt .figure (figsize = (14 , 10 * 2 ))
506
- gs = gridspec .GridSpec (9 , 2 , wspace = 0.3 , hspace = 0.3 )
507
-
508
- axs = []
509
- row = 0
510
-
511
- # Plot Bayesian cone
512
- ax_cone = plt .subplot (gs [row , :])
513
- bayesian .plot_bayes_cone (df_train , df_test ,
514
- trace = trace_t ,
515
- ax = ax_cone )
516
-
517
- # Plot BEST results
518
- row += 1
519
- axs .append (plt .subplot (gs [row , 0 ]))
520
- axs .append (plt .subplot (gs [row , 1 ]))
521
- row += 1
522
- axs .append (plt .subplot (gs [row , 0 ]))
523
- axs .append (plt .subplot (gs [row , 1 ]))
524
- row += 1
525
- axs .append (plt .subplot (gs [row , 0 ]))
526
- axs .append (plt .subplot (gs [row , 1 ]))
527
- row += 1
528
- # Effect size across two
529
- axs .append (plt .subplot (gs [row , :]))
530
-
531
- bayesian .plot_best (trace = trace_best , axs = axs )
532
-
533
- # Compute Bayesian predictions
534
- row += 1
535
- ax_ret_pred_day = plt .subplot (gs [row , 0 ])
536
- ax_ret_pred_week = plt .subplot (gs [row , 1 ])
537
- day_pred = trace_t ['returns_missing' ][:, 0 ]
538
- p5 = scipy .stats .scoreatpercentile (day_pred , 5 )
539
- sns .distplot (day_pred ,
540
- ax = ax_ret_pred_day
541
- )
542
- ax_ret_pred_day .axvline (p5 , linestyle = '--' , linewidth = 3. )
543
- ax_ret_pred_day .set_xlabel ('Predicted returns 1 day' )
544
- ax_ret_pred_day .set_ylabel ('Frequency' )
545
- ax_ret_pred_day .text (0.4 , 0.9 , 'Bayesian VaR = %.2f' % p5 ,
546
- verticalalignment = 'bottom' ,
547
- horizontalalignment = 'right' ,
548
- transform = ax_ret_pred_day .transAxes )
549
- # Plot Bayesian VaRs
550
- week_pred = (
551
- np .cumprod (trace_t ['returns_missing' ][:, :5 ] + 1 , 1 ) - 1 )[:, - 1 ]
552
- p5 = scipy .stats .scoreatpercentile (week_pred , 5 )
553
- sns .distplot (week_pred ,
554
- ax = ax_ret_pred_week
555
- )
556
- ax_ret_pred_week .axvline (p5 , linestyle = '--' , linewidth = 3. )
557
- ax_ret_pred_week .set_xlabel ('Predicted cum returns 5 days' )
558
- ax_ret_pred_week .set_ylabel ('Frequency' )
559
- ax_ret_pred_week .text (0.4 , 0.9 , 'Bayesian VaR = %.2f' % p5 ,
560
- verticalalignment = 'bottom' ,
561
- horizontalalignment = 'right' ,
562
- transform = ax_ret_pred_week .transAxes )
489
+ live_start_date = utils .get_utc_timestamp (live_start_date )
490
+ df_train = returns .loc [returns .index < live_start_date ]
491
+ df_test = returns .loc [returns .index >= live_start_date ]
492
+
493
+ # Run T model with missing data
494
+ trace_t = bayesian .run_model ('t' , df_train , returns_test = df_test ,
495
+ samples = 2000 )
496
+
497
+ # Compute BEST model
498
+ trace_best = bayesian .run_model ('best' , df_train ,
499
+ returns_test = df_test ,
500
+ samples = 2000 )
501
+
502
+ # Plot results
503
+
504
+ fig = plt .figure (figsize = (14 , 10 * 2 ))
505
+ gs = gridspec .GridSpec (9 , 2 , wspace = 0.3 , hspace = 0.3 )
506
+
507
+ axs = []
508
+ row = 0
509
+
510
+ # Plot Bayesian cone
511
+ ax_cone = plt .subplot (gs [row , :])
512
+ bayesian .plot_bayes_cone (df_train , df_test ,
513
+ trace = trace_t ,
514
+ ax = ax_cone )
515
+
516
+ # Plot BEST results
517
+ row += 1
518
+ axs .append (plt .subplot (gs [row , 0 ]))
519
+ axs .append (plt .subplot (gs [row , 1 ]))
520
+ row += 1
521
+ axs .append (plt .subplot (gs [row , 0 ]))
522
+ axs .append (plt .subplot (gs [row , 1 ]))
523
+ row += 1
524
+ axs .append (plt .subplot (gs [row , 0 ]))
525
+ axs .append (plt .subplot (gs [row , 1 ]))
526
+ row += 1
527
+ # Effect size across two
528
+ axs .append (plt .subplot (gs [row , :]))
529
+
530
+ bayesian .plot_best (trace = trace_best , axs = axs )
531
+
532
+ # Compute Bayesian predictions
533
+ row += 1
534
+ ax_ret_pred_day = plt .subplot (gs [row , 0 ])
535
+ ax_ret_pred_week = plt .subplot (gs [row , 1 ])
536
+ day_pred = trace_t ['returns_missing' ][:, 0 ]
537
+ p5 = scipy .stats .scoreatpercentile (day_pred , 5 )
538
+ sns .distplot (day_pred ,
539
+ ax = ax_ret_pred_day
540
+ )
541
+ ax_ret_pred_day .axvline (p5 , linestyle = '--' , linewidth = 3. )
542
+ ax_ret_pred_day .set_xlabel ('Predicted returns 1 day' )
543
+ ax_ret_pred_day .set_ylabel ('Frequency' )
544
+ ax_ret_pred_day .text (0.4 , 0.9 , 'Bayesian VaR = %.2f' % p5 ,
545
+ verticalalignment = 'bottom' ,
546
+ horizontalalignment = 'right' ,
547
+ transform = ax_ret_pred_day .transAxes )
548
+ # Plot Bayesian VaRs
549
+ week_pred = (
550
+ np .cumprod (trace_t ['returns_missing' ][:, :5 ] + 1 , 1 ) - 1 )[:, - 1 ]
551
+ p5 = scipy .stats .scoreatpercentile (week_pred , 5 )
552
+ sns .distplot (week_pred ,
553
+ ax = ax_ret_pred_week
554
+ )
555
+ ax_ret_pred_week .axvline (p5 , linestyle = '--' , linewidth = 3. )
556
+ ax_ret_pred_week .set_xlabel ('Predicted cum returns 5 days' )
557
+ ax_ret_pred_week .set_ylabel ('Frequency' )
558
+ ax_ret_pred_week .text (0.4 , 0.9 , 'Bayesian VaR = %.2f' % p5 ,
559
+ verticalalignment = 'bottom' ,
560
+ horizontalalignment = 'right' ,
561
+ transform = ax_ret_pred_week .transAxes )
563
562
564
563
565
564
# Run alpha beta model
0 commit comments