-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLOO_Lab.html
More file actions
1263 lines (1132 loc) · 58.2 KB
/
Copy pathLOO_Lab.html
File metadata and controls
1263 lines (1132 loc) · 58.2 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
<!DOCTYPE html>
<html lang="de" data-theme="dark">
<head>
<meta charset="UTF-8">
<script>(function(){var t=localStorage.getItem('btl-theme');if(t)document.documentElement.setAttribute('data-theme',t);if(t==='light')document.addEventListener('DOMContentLoaded',function(){['btn-theme','btn-th','btn-th-nav','themeBtn'].forEach(function(id){var b=document.getElementById(id);if(b)b.textContent='🌙 Dark';});});})()</script>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="Leave-One-Out Kreuzvalidierung (LOO-CV) fuer Bayesianische Modellvergleiche: ELPD, PSIS-LOO und Modellgewichte interaktiv verstehen.">
<meta name="keywords" content="LOO-CV, Leave-One-Out, ELPD, Modellvergleich, PSIS, loo, brms, Bayesianische Statistik">
<meta property="og:title" content="LOO Lab — Modellvergleich · Bayes Thinking Lab">
<meta property="og:description" content="Leave-One-Out Kreuzvalidierung (LOO-CV) fuer Bayesianische Modellvergleiche: ELPD, PSIS-LOO und Modellgewichte interaktiv verstehen.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://www.bayes-thinking-lab.uni-osnabrueck.de/LOO_Lab.html">
<meta property="og:site_name" content="Bayes Thinking Lab">
<link rel="canonical" href="https://www.bayes-thinking-lab.uni-osnabrueck.de/LOO_Lab.html">
<title>LOO Lab — Modellvergleich · Bayes Thinking Lab</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=DM+Mono:ital,wght@0,300;0,400;0,500;1,400&family=Fraunces:ital,opsz,wght@0,9..144,200;0,9..144,300;0,9..144,600;0,9..144,700;1,9..144,200;1,9..144,300;1,9..144,400&display=swap');
:root{
--bg:#f0ead8;--paper:#f8f3e8;--ink:#18130e;--ink2:#5a4f42;--ink3:#8a7f72;
--accent:#5BE172;--accent2:#2a6e8a;--accent3:#5a8a2a;--accent4:#7a3a8a;--accent5:#b85c00;--accent6:#0e7c7b;
--grid:#ddd5c5;--panel:#e8e0ce;--border:#c8bfaa;--shadow:#c0b8a4;
--good:#5a8a2a;--ok:#b85c00;--bad:#c94a2a;
}
[data-theme="dark"]{
--bg:#0f1117;--paper:#171b25;--ink:#e8e2d8;--ink2:#9a9080;--ink3:#5a5448;
--accent:#B1F1BC;--accent2:#4ab0d8;--accent3:#80cb52;--accent4:#c87ad8;--accent5:#e07830;--accent6:#2bc7c4;
--grid:#252a36;--panel:#1e2230;--border:#333a4a;--shadow:#080b10;
--good:#80cb52;--ok:#e07830;--bad:#e8614a;
}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
html{font-size:18px;-webkit-font-smoothing:antialiased}
body{font-family:'Fraunces',Georgia,serif;background:var(--bg);color:var(--ink);height:100vh;overflow:hidden;display:flex;flex-direction:column;font-weight:300}
/* ── HEADER (einheitlich, identisch zu Bayesian_model_architect) ── */
#hdr{background:var(--paper);border-bottom:1.5px solid var(--border);
padding:.5rem 1.1rem;display:flex;align-items:center;justify-content:space-between;flex-shrink:0;gap:.5rem;line-height:normal}
.hdr-hrow{display:flex;justify-content:space-between;align-items:flex-start;
padding:1rem 1.8rem .7rem;gap:1rem;flex-wrap:wrap;flex-shrink:0;border-bottom:1px solid var(--border)}
.hdr-h1{font-family:'Fraunces',Georgia,serif;font-size:1.55rem;font-weight:300;letter-spacing:-.02em;line-height:1.2}
.hdr-h1 em{font-style:italic;color:var(--accent)}
.hdr-sub2{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink2);margin-top:.2rem;letter-spacing:.03em}
.hdr-auth{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink2);opacity:.5;margin-top:.1rem}
.hdr-btns{display:flex;gap:.4rem;align-items:center}
.hbtn{font-family:'DM Mono',monospace;font-size:.75rem;background:var(--panel);border:1.5px solid var(--border);color:var(--ink2);padding:.22rem .6rem;cursor:pointer;transition:all .15s}
.hbtn:hover{border-color:var(--accent2);color:var(--accent2)}
.hbtn.act{background:var(--accent2);color:var(--paper);border-color:var(--accent2)}
#shell{flex:1;overflow-y:auto;min-height:0}
/* HEADER */
.hstrip{padding:.8rem 1.5rem .65rem;border-bottom:1.5px solid var(--border)}
.hstrip-title{font-size:1.42rem;font-weight:300;letter-spacing:-.02em}
.hstrip-title em{font-style:italic;color:var(--accent)}
.hstrip-sub{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink2);margin-top:.18rem}
.hstrip-auth{font-family:'DM Mono',monospace;font-size:.62rem;color:var(--ink2);opacity:.42;margin-top:.1rem}
/* TABS */
.tab-bar{display:flex;gap:0;border-bottom:1.5px solid var(--border);background:var(--paper);
position:sticky;top:0;z-index:90;overflow-x:auto}
.tab{font-family:'DM Mono',monospace;font-size:.75rem;padding:.48rem 1.1rem;
background:transparent;border:none;border-bottom:3px solid transparent;
cursor:pointer;color:var(--ink2);transition:all .15s;letter-spacing:.04em;white-space:nowrap}
.tab:hover{color:var(--ink)}
.tab.active{color:var(--accent);border-bottom-color:var(--accent)}
/* STAGE CONTENT */
.stage{display:none;padding:1.3rem 1.5rem 3.5rem;max-width:1060px;margin:0 auto}
.stage.active{display:block}
/* SECTION LABEL */
.sec-lbl{font-family:'DM Mono',monospace;font-size:.62rem;letter-spacing:.13em;text-transform:uppercase;
color:var(--accent);margin-bottom:.55rem;padding-bottom:.28rem;border-bottom:1px solid var(--grid)}
/* LEARN CARD */
.learn-card{background:var(--paper);border:1.5px solid var(--border);padding:.85rem 1rem;
margin-bottom:1rem;box-shadow:2px 2px 0 var(--shadow);font-size:.88rem;line-height:1.68;color:var(--ink2)}
.learn-card strong{color:var(--ink)}
.learn-card+.learn-card{margin-top:-.5rem}
/* STAGE 1 — CONTROLS */
.s1-ctrl{display:flex;align-items:center;gap:.5rem;flex-wrap:wrap;margin-bottom:.9rem}
.abtn{font-family:'DM Mono',monospace;font-size:.75rem;padding:.3rem .75rem;
border:1.5px solid var(--border);background:transparent;cursor:pointer;
color:var(--ink);transition:all .15s;letter-spacing:.04em;white-space:nowrap;min-width:0}
.abtn:hover{background:var(--ink);color:var(--bg)}
.abtn.primary{border-color:var(--accent);color:var(--accent)}
.abtn.primary:hover{background:var(--accent);color:#18130e}
.abtn.active{background:var(--accent);color:#18130e;border-color:var(--accent)}
.step-lbl{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink2);min-width:96px;text-align:center}
input[type=range]{flex:1;max-width:180px;-webkit-appearance:none;height:3px;
background:var(--grid);outline:none;cursor:pointer;border-radius:2px}
input[type=range]::-webkit-slider-thumb{-webkit-appearance:none;width:12px;height:12px;
background:var(--ink);border-radius:50%;cursor:pointer}
input[type=range]::-webkit-slider-thumb:hover{background:var(--accent)}
/* CANVAS GRID */
.canvas-grid{display:grid;grid-template-columns:1fr 1fr;gap:1rem;margin-bottom:.9rem}
.cv-wrap{background:var(--paper);border:1.5px solid var(--border);padding:.6rem .75rem .55rem;
box-shadow:2px 2px 0 var(--shadow)}
.cv-label{font-family:'DM Mono',monospace;font-size:.75rem;margin-bottom:.38rem;
color:var(--ink2);display:flex;justify-content:space-between;align-items:center}
.cv-label strong{color:var(--ink)}
canvas{display:block;width:100%;max-width:420px;height:auto}
.elpd-bar{font-family:'DM Mono',monospace;font-size:.75rem;margin-top:.38rem;
padding:.28rem .5rem;background:var(--panel);border:1px solid var(--grid);
display:flex;justify-content:space-between;align-items:center}
.elpd-val{font-weight:500}
/* STEP TEXT */
.step-text{border-left:3px solid var(--accent);padding:.65rem 1rem;margin-bottom:.8rem;
font-size:.87rem;line-height:1.65;color:var(--ink2);background:var(--panel)}
.step-text strong{color:var(--ink)}
/* FORMULA BOX */
.formula-box{background:var(--paper);border:1.5px solid var(--accent);
padding:.9rem 1.2rem;text-align:center;margin-top:.8rem}
.formula{font-family:'DM Mono',monospace;font-size:.9rem;color:var(--accent);
letter-spacing:.03em;margin-bottom:.48rem}
.formula-note{font-family:'DM Mono',monospace;font-size:.72rem;color:var(--ink2);line-height:1.65}
/* LEGEND */
.legend{display:flex;gap:1.2rem;font-family:'DM Mono',monospace;font-size:.72rem;
color:var(--ink2);margin-bottom:.7rem;flex-wrap:wrap}
.leg-item{display:flex;align-items:center;gap:.32rem}
.leg-line{width:22px;height:2px;display:inline-block}
.leg-dash{width:22px;height:0;border-top:2px dashed;display:inline-block}
.leg-dot{width:8px;height:8px;border-radius:50%;display:inline-block}
/* STAGE 2 */
.paste-grid{display:grid;grid-template-columns:1fr 1fr;gap:1rem;margin-bottom:.8rem}
.paste-block label{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink2);
display:block;margin-bottom:.3rem;letter-spacing:.04em}
textarea{width:100%;height:114px;font-family:'DM Mono',monospace;font-size:.75rem;
background:var(--panel);border:1.5px solid var(--border);color:var(--ink);
padding:.5rem .65rem;resize:vertical;outline:none;line-height:1.58}
textarea:focus{border-color:var(--accent)}
textarea::placeholder{color:var(--ink3);font-size:.7rem}
.btn-row{display:flex;gap:.45rem;margin-top:.6rem;flex-wrap:wrap}
.parse-btn{font-family:'DM Mono',monospace;font-size:.75rem;padding:.36rem 1.05rem;
border:1.5px solid var(--accent);background:var(--accent);cursor:pointer;
color:white;transition:all .15s;letter-spacing:.05em}
.parse-btn:hover{filter:brightness(1.1)}
.ex-btn{font-family:'DM Mono',monospace;font-size:.75rem;padding:.36rem .9rem;
border:1.5px solid var(--border);background:transparent;cursor:pointer;
color:var(--ink2);transition:all .15s}
.ex-btn:hover{border-color:var(--accent2);color:var(--accent2)}
.msg-ok{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--good);
margin-top:.4rem;padding:.35rem .55rem;border:1px solid var(--good)}
.msg-err{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--bad);
margin-top:.4rem;padding:.35rem .55rem;border:1px solid var(--bad)}
/* PLOTS */
.plot-grid{display:grid;grid-template-columns:1fr 1fr;gap:1rem;margin-top:.8rem}
.plot-card{background:var(--paper);border:1.5px solid var(--border);padding:.72rem .9rem;
box-shadow:2px 2px 0 var(--shadow)}
.plot-card.full{grid-column:1/-1}
.plot-title{font-family:'DM Mono',monospace;font-size:.68rem;letter-spacing:.1em;
text-transform:uppercase;color:var(--ink2);margin-bottom:.55rem;padding-bottom:.22rem;
border-bottom:1px solid var(--grid)}
svg.plot{display:block;width:100%;overflow:visible}
/* STAGE 3 */
.verdict-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(195px,1fr));gap:.8rem;margin-bottom:1.3rem}
.verdict-card{background:var(--paper);border:1.5px solid var(--border);padding:.72rem .88rem;
box-shadow:2px 2px 0 var(--shadow)}
.verdict-pair{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink);
font-weight:500;margin-bottom:.35rem}
.verdict-nums{font-family:'DM Mono',monospace;font-size:.72rem;color:var(--ink2);
line-height:1.65;margin-bottom:.42rem}
.tl{display:inline-block;font-family:'DM Mono',monospace;font-size:.68rem;
padding:.2rem .55rem;letter-spacing:.07em;text-transform:uppercase;font-weight:500}
.tl-green{background:var(--good);color:#fff}
.tl-yellow{background:var(--ok);color:#fff}
.tl-red{background:var(--bad);color:#fff}
/* SLIDER SECTION */
.slider-card{background:var(--paper);border:1.5px solid var(--border);
padding:.88rem 1.05rem;margin-bottom:1.2rem;box-shadow:2px 2px 0 var(--shadow)}
.s3-row{display:flex;align-items:center;gap:.75rem;margin-bottom:.55rem;flex-wrap:wrap}
.s3-row label{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink2);min-width:90px}
.s3-row input[type=range]{flex:1;max-width:200px}
.s3-row .val{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--accent);min-width:46px;text-align:right}
.ratio-box{font-family:'DM Mono',monospace;font-size:.75rem;text-align:center;
padding:.6rem;border:1px solid var(--grid);margin-top:.6rem;line-height:1.9}
.ratio-big{font-size:1.05rem;font-weight:500}
/* ACCORDION */
.accordion{margin-bottom:1.2rem}
.acc-item{border:1.5px solid var(--border);margin-bottom:.38rem}
.acc-head{width:100%;background:var(--paper);border:none;cursor:pointer;
padding:.6rem .88rem;display:flex;justify-content:space-between;align-items:center;
font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink);text-align:left;transition:background .15s}
.acc-head:hover{background:var(--panel)}
.acc-arrow{color:var(--ink2);transition:transform .2s;flex-shrink:0}
.acc-body{display:none;padding:.7rem .88rem;font-family:'DM Mono',monospace;
font-size:.75rem;line-height:1.75;color:var(--ink2);border-top:1px solid var(--grid)}
.acc-body.open{display:block}
.acc-body strong{color:var(--ink)}
/* INFO BOX */
.info-box{background:var(--panel);border:1.5px solid var(--border);border-left:3px solid var(--accent2);
padding:.82rem 1rem;margin-bottom:1.2rem}
.info-title{font-family:'DM Mono',monospace;font-size:.65rem;letter-spacing:.1em;
text-transform:uppercase;color:var(--accent2);margin-bottom:.38rem}
.info-box p{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink2);line-height:1.68}
/* CODE */
.rcode{background:var(--panel);border:1px solid var(--grid);padding:.72rem .88rem;
font-family:'DM Mono',monospace;font-size:.75rem;line-height:1.7;overflow-x:auto;margin-bottom:1.2rem}
.rc{color:var(--ink3)}.rf{color:var(--accent2)}.rs{color:var(--good)}
/* FOOTER NAV */
.foot-nav{display:flex;justify-content:space-between;padding:1rem 1.5rem;
border-top:1.5px solid var(--border);background:var(--paper)}
.fn-lnk{font-family:'DM Mono',monospace;font-size:.75rem;color:var(--ink2);
text-decoration:none;border-bottom:1px dotted var(--border);transition:color .15s}
.fn-lnk:hover{color:var(--accent2)}
/* FLOATING HELP */
#help-float{display:none;position:fixed;right:1.4rem;top:50%;transform:translateY(-50%);
width:296px;z-index:600;background:var(--paper);border:1.5px solid var(--border);
box-shadow:4px 4px 0 rgba(0,0,0,.2)}
#help-float.open{display:block}
.hp-head{display:flex;justify-content:space-between;align-items:center;
padding:.48rem .82rem;border-bottom:1px solid var(--grid)}
.hp-title{font-family:'DM Mono',monospace;font-size:.62rem;letter-spacing:.09em;
text-transform:uppercase;color:var(--ink2)}
.hp-close{font-family:'DM Mono',monospace;font-size:.88rem;background:none;border:none;
cursor:pointer;color:var(--ink2);line-height:1;padding:0 .15rem}
.hp-close:hover{color:var(--ink)}
.hp-body{padding:.75rem .82rem;font-family:'DM Mono',monospace;font-size:.7rem;
line-height:1.75;color:var(--ink2);max-height:72vh;overflow-y:auto}
.hp-sect{font-size:.55rem;letter-spacing:.1em;text-transform:uppercase;
margin-top:.85rem;margin-bottom:.26rem;border-bottom:1px solid var(--grid);
padding-bottom:.16rem;color:var(--ink3)}
.hp-sect:first-child{margin-top:0}
.hp-body strong{color:var(--ink)}
.hp-body em{color:var(--accent);font-style:normal}
/* RESPONSIVE */
@media(max-width:680px){
.canvas-grid,.paste-grid,.plot-grid{grid-template-columns:1fr}
.plot-card.full{grid-column:1}
}
</style>
</head>
<body>
<!-- HEADER -->
<nav id="hdr" class="hdr-hrow">
<div>
<div class="hdr-h1">LOO <em>Lab</em></div>
<div class="hdr-sub2">Bayesianischer Modellvergleich via Leave-One-Out Cross-Validation</div>
<div class="hdr-auth">© Dr. Rainer Düsing · Interactive Tools by Claude</div>
</div>
<div class="hdr-btns">
<button class="hbtn" onclick="window.open('index_de.html','_self')" style="border-color:var(--accent);color:var(--accent)">← Übersicht</button>
<button class="hbtn" onclick="toggleHelp()" style="border-color:var(--bad);color:var(--bad)">ℹ Hilfe</button>
<button class="hbtn" id="btn-theme" onclick="toggleTheme()">☀ Light</button>
</div>
</nav>
<div id="shell">
<!-- TABS -->
<div class="tab-bar">
<button class="tab active" onclick="switchTab(1)">Stufe 1 — Die Idee</button>
<button class="tab" onclick="switchTab(2)">Stufe 2 — R-Output analysieren</button>
<button class="tab" onclick="switchTab(3)">Stufe 3 — Entscheidungslogik</button>
</div>
<!-- ===================== STAGE 1 ===================== -->
<div id="stage1" class="stage active">
<div class="learn-card" style="margin-bottom:.9rem;border-left:3px solid var(--accent)">
<strong>LOO-CV fragt: Wie gut sagt mein Modell einen Datenpunkt vorher, den es nicht gesehen hat?</strong>
Das ist die ehrlichste Frage, die man einem Modell stellen kann — und sie entlarvt Overfitting, das in-sample unsichtbar bleibt.
</div>
<!-- Controls -->
<div class="s1-ctrl">
<button class="abtn" id="btn-prev" onclick="stepChange(-1)">← Zurück</button>
<span class="step-lbl" id="stepLbl">Übersicht</span>
<button class="abtn primary" id="btn-next" onclick="stepChange(1)">Weiter →</button>
<input type="range" id="stepSlider" min="-1" max="11" value="-1" oninput="setStep(+this.value)">
<button class="abtn" id="btn-play" onclick="togglePlay()">▶ Animation</button>
<button class="abtn" onclick="setStep(-1)">↺ Reset</button>
</div>
<!-- Legend -->
<div class="legend">
<span class="leg-item">
<span class="leg-line" style="background:var(--ink2);opacity:.4"></span>
Full-data-Fit
</span>
<span class="leg-item">
<span class="leg-dash" style="border-color:var(--accent2)"></span>
<span style="color:var(--accent2)">Modell A LOO-Fit</span>
</span>
<span class="leg-item">
<span class="leg-dash" style="border-color:var(--accent)"></span>
<span style="color:var(--accent)">Modell B LOO-Fit</span>
</span>
<span class="leg-item">
<span class="leg-dot" style="background:var(--ink)"></span>
Datenpunkt
</span>
<span class="leg-item">
<span class="leg-dot" style="background:var(--accent);border-radius:0;transform:rotate(45deg)"></span>
LOO-Vorhersage ŷ
</span>
</div>
<!-- Two canvases -->
<div class="canvas-grid">
<div class="cv-wrap">
<div class="cv-label">
<strong>Modell A</strong>
<span style="color:var(--accent2)">Linear (Grad 1)</span>
</div>
<canvas id="canvasA" width="420" height="275"></canvas>
<div class="elpd-bar" id="elpdA">
<span>elpd<sub>loo</sub> (A)</span>
<span class="elpd-val" style="color:var(--accent2)">—</span>
</div>
</div>
<div class="cv-wrap">
<div class="cv-label">
<strong>Modell B</strong>
<span style="color:var(--accent)">Polynom (Grad 4)</span>
</div>
<canvas id="canvasB" width="420" height="275"></canvas>
<div class="elpd-bar" id="elpdB">
<span>elpd<sub>loo</sub> (B)</span>
<span class="elpd-val" style="color:var(--accent)">—</span>
</div>
</div>
</div>
<!-- Step explanation text -->
<div class="step-text" id="stepText">
<strong>Wozu ein Modell fitten, wenn man es sowieso an den Trainingsdaten misst?</strong>
LOO fragt ehrlicher: Wie gut generalisiert das Modell auf Punkte, die es nicht kannte?
Klicke <em>Weiter →</em> oder starte die Animation, um Punkt für Punkt durch den Datensatz zu gehen.
</div>
<!-- Formula box (shown after full round) -->
<div class="formula-box" id="formulaBox" style="display:none">
<div class="formula">elpd<sub>loo</sub> = Σᵢ log p(yᵢ | y₋ᵢ, M)</div>
<div class="formula-note">
Jeder Term misst, wie gut das Modell den <em>i</em>-ten Punkt vorhersagt, wenn es auf allen anderen Punkten trainiert wurde.<br>
In der Praxis berechnet <strong>loo()</strong> das effizient via Pareto-Importance-Sampling (PSIS) —<br>
ohne das Modell N-mal neu zu fitten.
</div>
</div>
</div><!-- /stage1 -->
<!-- ===================== STAGE 2 ===================== -->
<div id="stage2" class="stage">
<div class="learn-card" style="margin-bottom:1rem;border-left:3px solid var(--accent)">
<strong>Wichtig:</strong> LOO wird in brms direkt ins Modell-Objekt geschrieben —
<code style="font-family:'DM Mono',monospace;font-size:.82em">model.1 <- add_criterion(model.1, c("loo"))</code> —
und steht dann für den Vergleich bereit:
<code style="font-family:'DM Mono',monospace;font-size:.82em">loo_compare(model.1, model.2, model.3)</code>.
Kopiere diesen Output unten ein.
</div>
<div class="sec-lbl">Input</div>
<div class="paste-grid">
<div class="paste-block">
<label>loo_compare() Output aus R — Pflichtfeld</label>
<textarea id="pasteCmp" placeholder=" elpd_diff se_diff model.2 0.0 0.0 model.1 -3.2 2.1 model.3 -8.7 3.4"></textarea>
</div>
<div class="paste-block">
<label>print(model.1$criteria$loo) — optional, Pareto-k eines einzelnen Modells</label>
<textarea id="pastePareto" placeholder="Pareto k diagnostic values: Count Pct. (-Inf, 0.5] (good) 108 90.0% (0.5, 0.7] (ok) 8 6.7% (0.7, 1] (bad) 3 2.5% (1, Inf) (very bad) 1 0.8%"></textarea>
</div>
</div>
<div class="btn-row">
<button class="ex-btn" onclick="loadExample()">Beispiel laden</button>
<button class="parse-btn" onclick="parseAndPlot()">Analysieren</button>
</div>
<div id="parseMsg" style="display:none"></div>
<!-- Plots (hidden until parsed) -->
<div id="plotSection" style="display:none">
<div class="sec-lbl" style="margin-top:1.3rem">Ergebnisse</div>
<div class="plot-grid">
<div class="plot-card full" id="plotForestCard">
<div class="plot-title">elpd_diff ± 2·SE — Modellvergleich</div>
<svg class="plot" id="svgForest"></svg>
<div id="forestNote" style="font-family:'DM Mono',monospace;font-size:.72rem;color:var(--ink2);margin-top:.5rem;line-height:1.65"></div>
</div>
<div class="plot-card" id="plotWeightsCard" style="display:none">
<div class="plot-title">Stacking Weights</div>
<svg class="plot" id="svgWeights"></svg>
</div>
<div class="plot-card" id="plotKCard" style="display:none">
<div class="plot-title">Pareto-k Diagnostics</div>
<svg class="plot" id="svgPareto"></svg>
<div id="paretoNote" style="font-family:'DM Mono',monospace;font-size:.72rem;color:var(--ink2);margin-top:.45rem;line-height:1.65"></div>
</div>
</div>
<div class="learn-card" style="margin-top:.9rem;border-left:3px solid var(--accent)">
Das ist dein Modellvergleich — jetzt nicht mehr als Tabelle, sondern als Entscheidung.
Gehe zu <strong>Stufe 3</strong>, um zu sehen, was der Befund bedeutet.
</div>
</div>
</div><!-- /stage2 -->
<!-- ===================== STAGE 3 ===================== -->
<div id="stage3" class="stage">
<!-- Decision verdicts from Stage 2 (auto-filled) -->
<div id="verdictSection" style="display:none">
<div class="sec-lbl">Modelentscheidungen — aus Stufe 2</div>
<div class="verdict-grid" id="verdictGrid"></div>
</div>
<!-- Manual slider to explore threshold -->
<div class="sec-lbl">Threshold Explorer — Wann ist ein Unterschied belastbar?</div>
<div class="slider-card">
<div class="s3-row">
<label>elpd_diff</label>
<input type="range" id="sl-elpd" min="-20" max="0" step="0.1" value="-5" oninput="updateDecision()">
<span class="val" id="val-elpd">–5.0</span>
</div>
<div class="s3-row">
<label>se_diff</label>
<input type="range" id="sl-se" min="0.2" max="8" step="0.1" value="2.5" oninput="updateDecision()">
<span class="val" id="val-se">2.5</span>
</div>
<div class="ratio-box" id="ratioBox"></div>
</div>
<!-- Scenarios accordion -->
<div class="sec-lbl">Die drei Entscheidungsszenarien</div>
<div class="accordion">
<div class="acc-item">
<button class="acc-head" onclick="toggleAcc(this)">
<span><span class="tl tl-green" style="margin-right:.5rem">Grün</span> Klarer Gewinner — |elpd_diff| > 2·SE</span>
<span class="acc-arrow">▸</span>
</button>
<div class="acc-body">
Der Unterschied ist größer als seine Unsicherheit. Das ist ein <strong>belastbarer Befund</strong>.<br><br>
Beispiel: elpd_diff = −8.7, SE = 3.4 → Ratio = 2.56 → Modell A ist klar besser.<br><br>
<strong>Empfehlung:</strong> Verwende das bessere Modell. Dokumentiere elpd_diff und SE im Bericht.
Prüfe inhaltlich, warum das Modell besser passt — das ist die eigentliche Frage.
</div>
</div>
<div class="acc-item">
<button class="acc-head" onclick="toggleAcc(this)">
<span><span class="tl tl-yellow" style="margin-right:.5rem">Gelb</span> Praktisch gleich — |elpd_diff| < 2·SE</span>
<span class="acc-arrow">▸</span>
</button>
<div class="acc-body">
Die Modelle sind nicht klar trennbar. Das ist <strong>kein Fehler</strong> — es bedeutet, dass beide die Daten
ähnlich gut erklären. Die Messunsicherheit von LOO selbst erlaubt keine klare Entscheidung.<br><br>
<strong>Empfehlung:</strong> Wähle nach Parsimonie (einfacheres Modell) oder inhaltlichen Gründen.
Berichte die Ähnlichkeit transparent. Erwäge Modell-Stacking via <code style="font-family:'DM Mono',monospace">loo_model_weights()</code>.
</div>
</div>
<div class="acc-item">
<button class="acc-head" onclick="toggleAcc(this)">
<span><span class="tl tl-red" style="margin-right:.5rem">Rot</span> Unsichere LOO-Schätzung — Pareto-k > 0.7</span>
<span class="acc-arrow">▸</span>
</button>
<div class="acc-body">
Hohe Pareto-k-Werte zeigen <strong>einflussreiche Datenpunkte</strong> — dort ist die PSIS-Approximation unzuverlässig.
Das LOO-Ergebnis selbst ist mit Vorsicht zu interpretieren.<br><br>
<strong>Empfehlung:</strong> Refitte mit <code style="font-family:'DM Mono',monospace">reloo()</code> für die problematischen Beobachtungen
(echtes LOO, kostet N zusätzliche Fits). Prüfe ob einzelne Ausreißer das Modell dominieren.
Berichte den Anteil problematischer k-Werte.
</div>
</div>
</div>
<!-- WAIC infobox -->
<div class="info-box">
<div class="info-title">WAIC vs. LOO</div>
<p>WAIC und LOO-CV schätzen dasselbe Ziel: Out-of-Sample-Vorhersagegüte.
LOO via PSIS ist robuster und heute der Standard (Vehtari et al. 2017).
Der numerische Unterschied zwischen beiden ist in der Praxis meist gering — solange keine extremen k-Werte vorliegen.
Verwende LOO, und prüfe die Pareto-k-Diagnostics als Qualitätskontrolle.</p>
</div>
<!-- brms code block -->
<div class="sec-lbl">brms-Code für den LOO-Workflow</div>
<div class="rcode">
<span class="rc"># 1. Modelle fitten (save_pars nötig für LOO/WAIC)</span><br>
model.1 <- <span class="rf">brm</span>(y ~ x, data = d, <span class="rs">save_pars = save_pars(all = TRUE)</span>)<br>
model.2 <- <span class="rf">brm</span>(y ~ x + z, data = d, <span class="rs">save_pars = save_pars(all = TRUE)</span>)<br>
model.3 <- <span class="rf">brm</span>(y ~ x + z + w, data = d, <span class="rs">save_pars = save_pars(all = TRUE)</span>)<br>
<br>
<span class="rc"># 2. LOO direkt ins Modell-Objekt schreiben (kein separates loo-Objekt nötig)</span><br>
model.1 <- <span class="rf">add_criterion</span>(model.1, <span class="rs">c("loo")</span>)<br>
model.2 <- <span class="rf">add_criterion</span>(model.2, <span class="rs">c("loo")</span>)<br>
model.3 <- <span class="rf">add_criterion</span>(model.3, <span class="rs">c("loo")</span>)<br>
<br>
<span class="rc"># 3. Modelle vergleichen — LOO wird direkt aus den Objekten gelesen</span><br>
<span class="rf">loo_compare</span>(model.1, model.2, model.3) <span class="rc"># → elpd_diff, se_diff ← diesen Output in Stufe 2 einfügen</span><br>
<br>
<span class="rc"># 4. Pareto-k Diagnostics für ein einzelnes Modell prüfen</span><br>
<span class="rf">print</span>(model.1$criteria$loo) <span class="rc"># ← diesen Output optional in Stufe 2 einfügen</span><br>
<br>
<span class="rc"># 5. Model Stacking — Ensemble-Gewichte statt ein Modell wählen (optional)</span><br>
<span class="rc"># Fragt: welche Kombination der Modelle sagt neue Daten am besten vorher?</span><br>
<span class="rc"># Gewichte summieren zu 1; Modell mit w ≈ 0 trägt nichts bei.</span><br>
<span class="rf">loo_model_weights</span>(model.1, model.2, model.3) <span class="rc"># → z.B. model.1=0.12, model.2=0.73, model.3=0.15</span><br>
<br>
<span class="rc"># 6. Bei hohen k-Werten: reloo() direkt ins Objekt</span><br>
model.1 <- <span class="rf">add_criterion</span>(model.1, <span class="rs">c("loo")</span>, reloo = <span class="rs">TRUE</span>, k_threshold = <span class="rs">0.7</span>)
</div>
<div class="learn-card" style="border-left:3px solid var(--accent3)">
<strong>Nächster Schritt:</strong> Ein Modell wurde ausgewählt — jetzt geht es um die inhaltliche Interpretation des Posteriors. →
<a href="Decision_Lab.html" style="color:var(--accent2);font-family:'DM Mono',monospace;font-size:.85em">Decision Lab</a>
</div>
</div><!-- /stage3 -->
<!-- FOOTER NAV -->
<div class="foot-nav">
<a href="Bayesian_PP_Check.html" class="fn-lnk">← Posterior Predictive Check</a>
<a href="Decision_Lab.html" class="fn-lnk">Decision Lab →</a>
</div>
</div><!-- /shell -->
<!-- FLOATING HELP -->
<div id="help-float">
<div class="hp-head">
<span class="hp-title">LOO Lab — Hilfe</span>
<button class="hp-close" onclick="toggleHelp()">×</button>
</div>
<div class="hp-body">
<div class="hp-sect">Wozu dieses Tool?</div>
LOO-CV (Leave-One-Out Cross-Validation) misst die Out-of-Sample-Vorhersagegüte eines Bayesianischen Modells.
Das Tool hat drei Stufen: Konzept → R-Output analysieren → Entscheidung treffen.<br><br>
<strong>Hinweis:</strong> Dieses Tool <em>berechnet kein LOO</em> — das ist im Browser nicht machbar.
Du berechnest LOO in R mit <em>add_criterion()</em> und kopierst den Output in Stufe 2.
<div class="hp-sect">Stufe 1 — Die Idee</div>
<strong>Modell A</strong> (linear) vs. <strong>Modell B</strong> (Polynom Grad 4) auf demselben Datensatz.
Jeder Schritt: ein Punkt wird herausgenommen, beide Modelle auf N−1 Punkten refittet, dann die Vorhersage verglichen.
Die gestrichelte Linie = LOO-Fit; der ◆ = LOO-Vorhersage; der Pfeil = Residuum.
<div class="hp-sect">Stufe 2 — R-Output</div>
LOO in R wird direkt ins Modell-Objekt geschrieben:<br>
<em>model.1 <- add_criterion(model.1, c("loo"))</em><br>
Danach: <em>loo_compare(model.1, model.2, ...)</em> → Output ins erste Textfeld.<br>
Das zweite Feld (optional) nimmt den Output von <em>print(model.1$criteria$loo)</em>
für die Pareto-k Diagnostics eines <strong>einzelnen</strong> Modells.
<strong>Beispiel laden</strong> zeigt das erwartete Format.
<div class="hp-sect">Model Stacking</div>
<strong>Model Stacking</strong> ist eine Form von Model Averaging — aber ohne Modell-Priors.
Statt ein Modell zu wählen, wird ein Ensemble gebildet:<br><br>
Ŷ = w₁·Ŷ₁ + w₂·Ŷ₂ + w₃·Ŷ₃ (Gewichte summieren zu 1)<br><br>
Die Gewichte werden direkt aus der LOO-Vorhersageleistung optimiert
(Yao et al. 2018). Redundante Modelle bekommen automatisch Gewicht ≈ 0.<br><br>
<strong>vs. klassischem Model Averaging (BMA):</strong><br>
BMA gewichtet nach Modell-Posterior-Wahrscheinlichkeit — Stacking nach
Out-of-Sample-Vorhersagegüte. Stacking ist robuster gegen ähnliche Modelle
und prior-sensitiver BMA-Probleme (McElreath Kap. 7).
<div class="hp-sect">Entscheidungsregel</div>
<strong>|elpd_diff| > 2·SE</strong> → klarer Unterschied<br>
<strong>|elpd_diff| < 2·SE</strong> → praktisch ununterscheidbar<br>
<strong>k > 0.7</strong> → LOO-Schätzung unsicher, reloo() empfohlen
<div class="hp-sect">Voraussetzungen</div>
brms Model Builder (Modellstruktur) · Posterior PPC (Modelldiagnostik)
</div>
</div>
<script>
/* ========================================================
MATH UTILITIES
======================================================== */
function matMul(A, B) {
return A.map(row => B[0].map((_, j) => row.reduce((s, v, k) => s + v * B[k][j], 0)));
}
function matT(A) { return A[0].map((_, j) => A.map(r => r[j])); }
function solveGE(A, b) {
const n = A.length;
const M = A.map((row, i) => [...row, b[i]]);
for (let col = 0; col < n; col++) {
let mx = col;
for (let r = col + 1; r < n; r++) if (Math.abs(M[r][col]) > Math.abs(M[mx][col])) mx = r;
[M[col], M[mx]] = [M[mx], M[col]];
if (Math.abs(M[col][col]) < 1e-12) continue;
for (let r = 0; r < n; r++) {
if (r === col) continue;
const f = M[r][col] / M[col][col];
for (let j = col; j <= n; j++) M[r][j] -= f * M[col][j];
}
const pv = M[col][col];
for (let j = col; j <= n; j++) M[col][j] /= pv;
}
return M.map(row => row[n]);
}
function polyfit(xn, ys, deg) {
const X = xn.map(x => Array.from({ length: deg + 1 }, (_, d) => Math.pow(x, d)));
const Xt = matT(X);
const XtX = matMul(Xt, X);
const Xty = Xt.map(row => row.reduce((s, v, i) => s + v * ys[i], 0));
return solveGE(XtX, Xty);
}
function polyeval(coefs, x) {
return coefs.reduce((s, c, d) => s + c * Math.pow(x, d), 0);
}
function logNormal(y, mu, sigma) {
return -0.5 * Math.log(2 * Math.PI) - Math.log(sigma) - 0.5 * ((y - mu) / sigma) ** 2;
}
/* ========================================================
DATA & LOO COMPUTATION
======================================================== */
const xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
const ys = [2.1, 3.8, 2.6, 4.9, 3.8, 6.0, 4.7, 6.9, 5.8, 8.1, 7.0, 9.2];
const N = xs.length;
const xMin = xs[0], xRange = xs[N - 1] - xs[0];
const xn = xs.map(x => (x - xMin) / xRange); // normalize to [0,1]
// Sigma from full linear fit (shared for both models → comparable elpd)
const coefsLinFull = polyfit(xn, ys, 1);
const residLin = ys.map((y, i) => y - polyeval(coefsLinFull, xn[i]));
const sigma = Math.sqrt(residLin.reduce((s, r) => s + r * r, 0) / (N - 2));
function computeLOO(deg) {
const coefsAll = polyfit(xn, ys, deg);
const items = xs.map((_, i) => {
const xLoo = xn.filter((_, j) => j !== i);
const yLoo = ys.filter((_, j) => j !== i);
const coefs = polyfit(xLoo, yLoo, deg);
const yhat = polyeval(coefs, xn[i]);
const logp = logNormal(ys[i], yhat, sigma);
return { coefs, yhat, logp };
});
return { coefsAll, items, elpd: items.reduce((s, d) => s + d.logp, 0) };
}
const looA = computeLOO(1);
const looB = computeLOO(4);
/* ========================================================
CANVAS DRAWING
======================================================== */
function getCss(v) { return getComputedStyle(document.documentElement).getPropertyValue(v).trim(); }
function hex2rgba(hex, a) {
// Handle both 3 and 6 char hex, with or without #
hex = hex.replace('#', '');
if (hex.length === 3) hex = hex.split('').map(c => c + c).join('');
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
return `rgba(${r},${g},${b},${a})`;
}
function drawCanvas(id, looData, accentColor, stepIdx) {
const canvas = document.getElementById(id);
if (!canvas) return;
const ctx = canvas.getContext('2d');
const W = canvas.width, H = canvas.height;
const pad = { t: 26, r: 18, b: 38, l: 44 };
const pw = W - pad.l - pad.r;
const ph = H - pad.t - pad.b;
const xPl = [0.5, 12.5], yPl = [0.5, 9.8];
const tx = x => pad.l + (x - xPl[0]) / (xPl[1] - xPl[0]) * pw;
const ty = y => H - pad.b - (y - yPl[0]) / (yPl[1] - yPl[0]) * ph;
const C = {
bg: getCss('--paper'), grid: getCss('--grid'),
ink: getCss('--ink'), ink2: getCss('--ink2'),
good: getCss('--good'), bad: getCss('--bad')
};
// Clear
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = C.bg;
ctx.fillRect(0, 0, W, H);
// Grid
ctx.lineWidth = 0.5;
ctx.strokeStyle = C.grid;
[3, 6, 9, 12].forEach(x => {
ctx.beginPath(); ctx.moveTo(tx(x), pad.t); ctx.lineTo(tx(x), H - pad.b); ctx.stroke();
});
[2, 4, 6, 8].forEach(y => {
ctx.beginPath(); ctx.moveTo(pad.l, ty(y)); ctx.lineTo(W - pad.r, ty(y)); ctx.stroke();
});
// Axes
ctx.strokeStyle = C.ink2; ctx.lineWidth = 1;
ctx.beginPath(); ctx.moveTo(pad.l, pad.t); ctx.lineTo(pad.l, H - pad.b); ctx.lineTo(W - pad.r, H - pad.b); ctx.stroke();
// Tick labels
ctx.font = '9px "DM Mono", monospace'; ctx.fillStyle = C.ink2;
ctx.textAlign = 'center';
[2, 4, 6, 8, 10, 12].forEach(x => ctx.fillText(x, tx(x), H - pad.b + 13));
ctx.textAlign = 'right';
[2, 4, 6, 8].forEach(y => ctx.fillText(y, pad.l - 4, ty(y) + 3));
// Full-data fit (thin, faded)
ctx.lineWidth = 1;
ctx.strokeStyle = hex2rgba(C.ink2.replace('#', '#'), 0.3);
ctx.setLineDash([]);
ctx.beginPath();
let first = true;
for (let xi = 0.5; xi <= 12.5; xi += 0.07) {
const yn = polyeval(looData.coefsAll, (xi - xMin) / xRange);
if (yn < yPl[0] - 1.5 || yn > yPl[1] + 1.5) { first = true; continue; }
if (first) { ctx.moveTo(tx(xi), ty(yn)); first = false; } else ctx.lineTo(tx(xi), ty(yn));
}
ctx.stroke();
// LOO fit for current step (dashed, colored)
if (stepIdx >= 0) {
const item = looData.items[stepIdx];
ctx.strokeStyle = accentColor;
ctx.lineWidth = 2;
ctx.setLineDash([5, 3]);
ctx.beginPath(); first = true;
for (let xi = 0.5; xi <= 12.5; xi += 0.07) {
const yn = polyeval(item.coefs, (xi - xMin) / xRange);
if (yn < yPl[0] - 2 || yn > yPl[1] + 2) { first = true; continue; }
if (first) { ctx.moveTo(tx(xi), ty(yn)); first = false; } else ctx.lineTo(tx(xi), ty(yn));
}
ctx.stroke();
ctx.setLineDash([]);
}
// Data points
for (let i = 0; i < N; i++) {
const [xi, yi] = [xs[i], ys[i]];
const isHeld = i === stepIdx;
const isPast = stepIdx >= 0 && i < stepIdx;
ctx.beginPath(); ctx.arc(tx(xi), ty(yi), isHeld ? 6 : 4, 0, 2 * Math.PI);
if (isHeld) {
ctx.strokeStyle = accentColor; ctx.lineWidth = 1.5;
ctx.setLineDash([3, 2]); ctx.stroke(); ctx.setLineDash([]);
ctx.fillStyle = 'rgba(128,128,128,0.18)'; ctx.fill();
} else {
ctx.fillStyle = isPast ? C.good : C.ink;
ctx.strokeStyle = isPast ? C.good : C.ink2;
ctx.lineWidth = 0.8; ctx.fill(); ctx.stroke();
}
}
// Prediction diamond + arrow for held-out point
if (stepIdx >= 0) {
const item = looData.items[stepIdx];
const xi = xs[stepIdx], yi = ys[stepIdx];
const yhat = item.yhat;
const yhatCl = Math.max(yPl[0] + 0.1, Math.min(yPl[1] - 0.1, yhat));
const px = tx(xi), py = ty(yhatCl), pa = ty(yi);
const r = 5;
// Diamond marker at LOO prediction
ctx.fillStyle = accentColor;
ctx.beginPath();
ctx.moveTo(px, py - r); ctx.lineTo(px + r, py);
ctx.lineTo(px, py + r); ctx.lineTo(px - r, py);
ctx.closePath(); ctx.fill();
// Arrow prediction → actual
const residual = yi - yhat;
const arrowCol = Math.abs(residual) > 1.2 * sigma ? C.bad : C.good;
if (Math.abs(pa - py) > 4) {
ctx.strokeStyle = arrowCol; ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.moveTo(px, py); ctx.lineTo(px, pa); ctx.stroke();
const dir = pa > py ? 1 : -1;
ctx.fillStyle = arrowCol;
ctx.beginPath();
ctx.moveTo(px, pa); ctx.lineTo(px - 4, pa - dir * 7); ctx.lineTo(px + 4, pa - dir * 7);
ctx.closePath(); ctx.fill();
// Residual label
ctx.font = '9px "DM Mono", monospace';
ctx.fillStyle = arrowCol;
const rightSide = px < W * 0.6;
ctx.textAlign = rightSide ? 'left' : 'right';
ctx.fillText((residual >= 0 ? '+' : '') + residual.toFixed(2), px + (rightSide ? 9 : -9), (py + pa) / 2 + 3);
}
// log p label at top
ctx.font = 'bold 9px "DM Mono", monospace';
ctx.fillStyle = accentColor;
ctx.textAlign = 'center';
ctx.fillText('log p = ' + item.logp.toFixed(2), px, pad.t - 6);
}
}
/* ========================================================
STAGE 1 — ANIMATION
======================================================== */
let currentStep = -1, isPlaying = false, animTimer = null;
function setStep(step) {
currentStep = Math.max(-1, Math.min(N - 1, step));
document.getElementById('stepSlider').value = currentStep;
document.getElementById('stepLbl').textContent = currentStep < 0 ? 'Übersicht' : `Punkt ${currentStep + 1} von ${N}`;
const accA = getCss('--accent2');
const accB = getCss('--accent');
drawCanvas('canvasA', looA, accA, currentStep);
drawCanvas('canvasB', looB, accB, currentStep);
// Update elpd running totals
const elpdAval = currentStep >= 0 ? looA.items.slice(0, currentStep + 1).reduce((s, d) => s + d.logp, 0) : null;
const elpdBval = currentStep >= 0 ? looB.items.slice(0, currentStep + 1).reduce((s, d) => s + d.logp, 0) : null;
const stepsLabel = currentStep >= 0 ? ` (${currentStep + 1}/${N})` : '';
document.getElementById('elpdA').innerHTML =
`<span>elpd<sub>loo</sub> (A)${stepsLabel}</span><span class="elpd-val" style="color:var(--accent2)">${elpdAval !== null ? elpdAval.toFixed(2) : '—'}</span>`;
document.getElementById('elpdB').innerHTML =
`<span>elpd<sub>loo</sub> (B)${stepsLabel}</span><span class="elpd-val" style="color:var(--accent)">${elpdBval !== null ? elpdBval.toFixed(2) : '—'}</span>`;
updateStepText(currentStep);
const fb = document.getElementById('formulaBox');
if (currentStep === N - 1) {
fb.style.display = 'block';
setTimeout(() => fb.scrollIntoView({ behavior: 'smooth', block: 'nearest' }), 80);
} else {
fb.style.display = 'none';
}
}
function updateStepText(step) {
const el = document.getElementById('stepText');
if (step < 0) {
el.innerHTML = `<strong>Das LOO-Prinzip:</strong> Für jeden der ${N} Datenpunkte fragen wir: Wie gut sagt das Modell diesen Punkt vorher — wenn es ihn <em>nicht gesehen</em> hat? Klicke <strong>Weiter →</strong> oder starte die Animation.`;
return;
}
const a = looA.items[step], b = looB.items[step];
const diff = a.logp - b.logp;
const diffStr = Math.abs(diff) < 0.05
? `Beide Modelle liegen bei diesem Punkt gleich auf (Δ = ${Math.abs(diff).toFixed(2)}).`
: diff > 0
? `Modell A liegt besser (Δ log p = <strong style="color:var(--accent2)">+${diff.toFixed(2)}</strong>).`
: `Modell B liegt besser (Δ log p = <strong style="color:var(--accent)">+${(-diff).toFixed(2)}</strong>).`;
if (step === N - 1) {
const elpdAF = looA.elpd, elpdBF = looB.elpd;
const winner = elpdAF > elpdBF ? 'A' : 'B';
el.innerHTML = `<strong>Alle ${N} Schritte abgeschlossen.</strong><br>
elpd<sub>loo</sub>(A) = <strong style="color:var(--accent2)">${elpdAF.toFixed(2)}</strong> |
elpd<sub>loo</sub>(B) = <strong style="color:var(--accent)">${elpdBF.toFixed(2)}</strong><br>
Differenz: <strong>${(elpdAF - elpdBF).toFixed(2)}</strong> — Modell ${winner} generalisiert besser auf ungesehene Datenpunkte.
Das Polynom (Grad 4) passt die Trainingsdaten enger, sagt aber herausgenommene Punkte <em>schlechter</em> vorher.`;
} else {
el.innerHTML = `<strong>Schritt ${step + 1}:</strong> Punkt (x = ${xs[step]}, y = ${ys[step]}).
Modell A sagt <strong>${a.yhat.toFixed(2)}</strong> vorher (log p = <strong style="color:var(--accent2)">${a.logp.toFixed(2)}</strong>),
Modell B sagt <strong>${b.yhat.toFixed(2)}</strong> vorher (log p = <strong style="color:var(--accent)">${b.logp.toFixed(2)}</strong>).
${diffStr}`;
}
}
function stepChange(dir) {
setStep(currentStep + dir);
}
function togglePlay() {
isPlaying = !isPlaying;
const btn = document.getElementById('btn-play');
if (isPlaying) {
btn.textContent = '⏸ Pause';
btn.classList.add('active');
if (currentStep >= N - 1) setStep(-1);
animTimer = setInterval(() => {
if (currentStep >= N - 1) { stopPlay(); return; }
setStep(currentStep + 1);
}, 1200);
} else {
stopPlay();
}
}
function stopPlay() {
isPlaying = false;
clearInterval(animTimer);
const btn = document.getElementById('btn-play');
btn.textContent = '▶ Animation';
btn.classList.remove('active');
}
/* ========================================================
STAGE 2 — PARSER
======================================================== */
let parsedModels = null;
let parsedK = null;
function parseLooCmp(text) {
const lines = text.trim().split('\n').map(l => l.trim()).filter(l => l.length > 0);
const headerIdx = lines.findIndex(l => /elpd_diff/i.test(l));
if (headerIdx < 0) throw new Error('Keine "elpd_diff" Spalte gefunden. Ist das ein loo_compare() Output?');
const models = [];
for (let i = headerIdx + 1; i < lines.length; i++) {
const m = lines[i].match(/^(\S+)\s+(-?\d+\.?\d*(?:e[+-]?\d+)?)\s+(\d+\.?\d*(?:e[+-]?\d+)?)/);
if (m) models.push({ name: m[1], elpd_diff: parseFloat(m[2]), se_diff: parseFloat(m[3]) });
}
if (models.length < 2) throw new Error('Mindestens 2 Modelle benötigt. Bitte loo_compare() mit ≥ 2 Modellen ausführen.');
// Validate reference model (first should have elpd_diff ≈ 0)
if (Math.abs(models[0].elpd_diff) > 0.5) throw new Error('Das Referenzmodell (erstes Modell) sollte elpd_diff ≈ 0 haben.');
return models;
}
function parseParetoK(text) {
if (!text.trim()) return null;
const good = text.match(/\(good\)\s+(\d+)/i)?.[1];
const ok = text.match(/\(ok\)\s+(\d+)/i)?.[1];
const bad = text.match(/(?<!\w)\(bad\)\s+(\d+)/i)?.[1];
const vbad = text.match(/very bad\)\s+(\d+)/i)?.[1];
if (good === undefined) {
if (/all pareto k estimates are good/i.test(text)) {
const nm = text.match(/by\s+\d+\s+by\s+(\d+)\s+log-likelihood/i)?.[1]
|| text.match(/by\s+\d+\s+by\s+(\d+)/i)?.[1];
const n = nm ? parseInt(nm) : 100;
return { good: n, ok: 0, bad: 0, vbad: 0 };
}
return null;
}
return { good: parseInt(good)||0, ok: parseInt(ok)||0, bad: parseInt(bad)||0, vbad: parseInt(vbad)||0 };
}
function parseAndPlot() {
const cmpText = document.getElementById('pasteCmp').value;
const kText = document.getElementById('pastePareto').value;
const msgEl = document.getElementById('parseMsg');
msgEl.style.display = 'none';
try {
parsedModels = parseLooCmp(cmpText);
} catch (e) {
parsedModels = null;
msgEl.className = 'msg-err'; msgEl.textContent = '✗ ' + e.message; msgEl.style.display = 'block';
return;
}
parsedK = parseParetoK(kText);
msgEl.className = 'msg-ok'; msgEl.textContent = `✓ ${parsedModels.length} Modelle erfolgreich eingelesen.`; msgEl.style.display = 'block';
document.getElementById('plotSection').style.display = 'block';
renderForestPlot(parsedModels);
if (parsedK) {
renderParetoPlot(parsedK);
document.getElementById('plotKCard').style.display = 'block';
document.getElementById('plotForestCard').classList.remove('full');
} else {
document.getElementById('plotKCard').style.display = 'none';
document.getElementById('plotForestCard').classList.add('full');
}
document.getElementById('plotWeightsCard').style.display = 'none';
// Auto-fill Stage 3
buildVerdicts(parsedModels, parsedK);
}
function loadExample() {
document.getElementById('pasteCmp').value =
` elpd_diff se_diff
model.2 0.0 0.0
model.1 -4.3 2.9
model.3 -12.1 4.2`;
document.getElementById('pastePareto').value =
`Computed from 4000 by 120 log-likelihood matrix
Estimate SE
elpd_loo -54.2 4.8
p_loo 5.3 0.7
looic 108.4 9.6
Pareto k diagnostic values:
Count Pct. Min. n_eff
(-Inf, 0.5] (good) 108 90.0% 423
(0.5, 0.7] (ok) 8 6.7% 256
(0.7, 1] (bad) 3 2.5% 89
(1, Inf) (very bad) 1 0.8% 34`;
}
/* ========================================================
STAGE 2 — SVG PLOTS
======================================================== */
function svgEl(tag, attrs) {
const el = document.createElementNS('http://www.w3.org/2000/svg', tag);
Object.entries(attrs).forEach(([k, v]) => el.setAttribute(k, v));
return el;
}
function renderForestPlot(models) {
const svg = document.getElementById('svgForest');
while (svg.firstChild) svg.removeChild(svg.firstChild);
const C = { ink: getCss('--ink'), ink2: getCss('--ink2'), ink3: getCss('--ink3'),