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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
"""End-to-end test script for the dce_analyzer backend.

Run from project root:
    python scripts/test_e2e.py
"""

from __future__ import annotations

import sys
import traceback
from pathlib import Path

# Ensure src/ is importable
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))

import numpy as np
import pandas as pd

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

_results: list[tuple[str, bool, str]] = []


def _run(name: str, fn):
    """Run *fn* and record PASS / FAIL."""
    try:
        fn()
        _results.append((name, True, ""))
        print(f"  PASS  {name}")
    except Exception as exc:
        msg = f"{exc.__class__.__name__}: {exc}"
        _results.append((name, False, msg))
        print(f"  FAIL  {name}")
        traceback.print_exc()
        print()


# ===================================================================
# 1. Import all backend modules
# ===================================================================
def test_imports():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.simulate import generate_simulated_dce
    from dce_analyzer.data import prepare_choice_tensors, ChoiceTensors
    from dce_analyzer.model import (
        MixedLogitEstimator,
        ConditionalLogitEstimator,
        EstimationResult,
    )
    from dce_analyzer.latent_class import LatentClassEstimator, LatentClassResult
    from dce_analyzer.pipeline import estimate_dataframe, PipelineResult
    from dce_analyzer.wtp import compute_wtp
    from dce_analyzer.bootstrap import run_bootstrap, BootstrapResult
    from dce_analyzer.format_converter import (
        detect_format,
        wide_to_long,
        infer_structure,
        normalize_choice_column,
        ColumnInference,
    )
    from dce_analyzer.apollo import APOLLO_DATASETS
    # all imported without error


_run("1. Import all backend modules", test_imports)


# ===================================================================
# 2. Generate simulated data
# ===================================================================
sim_output = None


def test_simulate():
    global sim_output
    from dce_analyzer.simulate import generate_simulated_dce

    sim_output = generate_simulated_dce(
        n_individuals=100, n_tasks=4, n_alts=3, seed=42
    )
    df = sim_output.data
    assert isinstance(df, pd.DataFrame), "Expected DataFrame"
    assert len(df) == 100 * 4 * 3, f"Expected 1200 rows, got {len(df)}"
    for col in ["respondent_id", "task_id", "alternative", "choice",
                "price", "time", "comfort", "reliability"]:
        assert col in df.columns, f"Missing column: {col}"
    assert isinstance(sim_output.true_parameters, dict)
    assert len(sim_output.true_parameters) > 0


_run("2. Generate simulated data (100 ind, 4 tasks, 3 alts)", test_simulate)


# ===================================================================
# 3. Conditional Logit estimation
# ===================================================================
cl_result = None


def test_conditional_logit():
    global cl_result
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
    )
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="conditional", maxiter=200, seed=42
    )
    cl_result = result
    est = result.estimation
    assert est.success, f"CL did not converge: {est.message}"
    assert est.n_parameters == 4
    assert est.n_observations == 100 * 4  # 400 choice tasks
    assert not est.estimates.empty
    assert "estimate" in est.estimates.columns


_run("3. Conditional Logit estimation", test_conditional_logit)


# ===================================================================
# 4. Mixed Logit estimation (n_draws=50)
# ===================================================================
mxl_result = None


def test_mixed_logit():
    global mxl_result
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="mixed", maxiter=200, seed=42
    )
    mxl_result = result
    est = result.estimation
    # 2 normal (mu+sd each) + 2 fixed = 6 params
    assert est.n_parameters == 6, f"Expected 6 params, got {est.n_parameters}"
    assert not est.estimates.empty
    # Should have mu_price, sd_price, mu_time, sd_time, beta_comfort, beta_reliability
    param_names = set(est.estimates["parameter"])
    for expected in ["mu_price", "sd_price", "mu_time", "sd_time",
                     "beta_comfort", "beta_reliability"]:
        assert expected in param_names, f"Missing param: {expected}"


_run("4. Mixed Logit estimation (n_draws=50)", test_mixed_logit)


# ===================================================================
# 5. Latent Class estimation (n_classes=2, n_starts=3)
# ===================================================================
lc_result = None


def test_latent_class():
    global lc_result
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
        n_classes=2,
    )
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="latent_class",
        maxiter=200, seed=42, n_classes=2, n_starts=3,
    )
    lc_result = result
    est = result.estimation
    assert est.n_classes == 2
    assert len(est.class_probabilities) == 2
    assert abs(sum(est.class_probabilities) - 1.0) < 1e-4, "Class probs must sum to 1"
    assert not est.estimates.empty
    assert not est.class_estimates.empty
    assert not est.posterior_probs.empty
    assert est.posterior_probs.shape[1] == 2  # two class columns


_run("5. Latent Class estimation (n_classes=2, n_starts=3)", test_latent_class)


# ===================================================================
# 6. WTP computation
# ===================================================================
def test_wtp():
    from dce_analyzer.wtp import compute_wtp

    # Use CL result (EstimationResult) for WTP
    wtp_df = compute_wtp(cl_result.estimation, cost_variable="price")
    assert isinstance(wtp_df, pd.DataFrame)
    assert len(wtp_df) == 3  # time, comfort, reliability (3 non-cost attrs)
    assert "wtp_estimate" in wtp_df.columns
    assert "wtp_std_error" in wtp_df.columns
    assert "wtp_ci_lower" in wtp_df.columns
    assert "wtp_ci_upper" in wtp_df.columns
    # WTP values should be finite
    for _, row in wtp_df.iterrows():
        assert np.isfinite(row["wtp_estimate"]), f"Non-finite WTP for {row['attribute']}"


_run("6. WTP computation (CL result)", test_wtp)


# ===================================================================
# 7. Bootstrap (n_boot=10)
# ===================================================================
def test_bootstrap():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.bootstrap import run_bootstrap

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
    )
    boot = run_bootstrap(
        df=sim_output.data, spec=spec, model_type="conditional",
        n_replications=10, maxiter=100, seed=42,
    )
    assert boot.n_replications == 10
    assert boot.n_successful >= 2, f"Only {boot.n_successful} succeeded"
    assert len(boot.param_names) == 4
    assert boot.estimates_matrix.shape == (boot.n_successful, 4)
    summary = boot.summary_dataframe()
    assert isinstance(summary, pd.DataFrame)
    assert len(summary) == 4


_run("7. Bootstrap (n_boot=10, conditional logit)", test_bootstrap)


# ===================================================================
# 8. Wide-to-long conversion
# ===================================================================
def test_wide_to_long():
    from dce_analyzer.format_converter import detect_format, wide_to_long

    # Create a small wide-format dataset
    wide_df = pd.DataFrame({
        "id": [1, 1, 2, 2],
        "choice": [1, 2, 1, 3],
        "price_1": [10, 20, 15, 25],
        "price_2": [12, 22, 17, 27],
        "price_3": [14, 24, 19, 29],
        "time_1": [30, 40, 35, 45],
        "time_2": [32, 42, 37, 47],
        "time_3": [34, 44, 39, 49],
    })

    fmt = detect_format(wide_df)
    assert fmt == "wide", f"Expected 'wide', got '{fmt}'"

    long_df = wide_to_long(
        wide_df,
        attribute_groups={
            "price": ["price_1", "price_2", "price_3"],
            "time": ["time_1", "time_2", "time_3"],
        },
        id_col="id",
        choice_col="choice",
    )
    assert isinstance(long_df, pd.DataFrame)
    # 4 rows * 3 alts = 12 rows
    assert len(long_df) == 12, f"Expected 12 rows, got {len(long_df)}"
    assert "alternative" in long_df.columns
    assert "choice" in long_df.columns
    assert "price" in long_df.columns
    assert "time" in long_df.columns
    # Each task should have exactly one chosen alt
    for (rid, tid), grp in long_df.groupby(["respondent_id", "task_id"]):
        assert grp["choice"].sum() == 1, f"Task ({rid},{tid}) has {grp['choice'].sum()} choices"

    # Test detect_format on long data
    fmt2 = detect_format(long_df)
    assert fmt2 == "long", f"Expected 'long' for converted data, got '{fmt2}'"


_run("8. Wide-to-long conversion", test_wide_to_long)


# ===================================================================
# 9. Additional checks: infer_structure, normalize_choice_column
# ===================================================================
def test_infer_and_normalize():
    from dce_analyzer.format_converter import infer_structure, normalize_choice_column

    df = sim_output.data
    inference = infer_structure(df)
    assert inference.id_col is not None, "Should detect id column"
    assert inference.choice_col is not None, "Should detect choice column"

    # Test normalize_choice_column (already binary -- should be no-op)
    normalized = normalize_choice_column(df, "choice", "alternative")
    assert set(normalized["choice"].unique()) <= {0, 1}


_run("9. infer_structure & normalize_choice_column", test_infer_and_normalize)


# ===================================================================
# 10. LatentClassResult.summary_dict()
# ===================================================================
def test_lc_summary():
    est = lc_result.estimation
    sd = est.summary_dict()
    assert "n_classes" in sd
    assert "class_probabilities" in sd
    assert sd["n_classes"] == 2


_run("10. LatentClassResult.summary_dict()", test_lc_summary)


# ===================================================================
# 11. Full correlated MMNL (backward compat)
# ===================================================================
def test_full_correlated_mxl():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="mixed",
        maxiter=200, seed=42, correlated=True,
    )
    est = result.estimation
    assert est.covariance_matrix is not None, "Expected covariance matrix"
    assert est.covariance_matrix.shape == (2, 2), f"Expected 2x2 cov, got {est.covariance_matrix.shape}"
    assert est.correlation_matrix is not None


_run("11. Full correlated MMNL (backward compat)", test_full_correlated_mxl)


# ===================================================================
# 12. Selective correlated MMNL (block-diagonal Cholesky)
# ===================================================================
def test_selective_correlated_mxl():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="normal"),
            VariableSpec(name="reliability", column="reliability", distribution="normal"),
        ],
        n_draws=50,
    )
    # Correlate price-time (group [0,1]) and comfort-reliability (group [2,3])
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="mixed",
        maxiter=200, seed=42,
        correlation_groups=[[0, 1], [2, 3]],
    )
    est = result.estimation
    assert est.covariance_matrix is not None, "Expected covariance matrix"
    assert est.covariance_matrix.shape == (4, 4)
    # Off-block elements should be zero (price-comfort, price-reliability, etc.)
    cov = est.covariance_matrix
    assert abs(cov[0, 2]) < 1e-8, f"Expected 0 cov(price,comfort), got {cov[0,2]}"
    assert abs(cov[0, 3]) < 1e-8, f"Expected 0 cov(price,reliability), got {cov[0,3]}"
    assert abs(cov[1, 2]) < 1e-8, f"Expected 0 cov(time,comfort), got {cov[1,2]}"
    assert abs(cov[1, 3]) < 1e-8, f"Expected 0 cov(time,reliability), got {cov[1,3]}"
    # Within-block elements should be non-zero
    assert abs(cov[0, 1]) > 1e-10 or True  # may be zero by chance, just check shape


_run("12. Selective correlated MMNL (block-diagonal)", test_selective_correlated_mxl)


# ===================================================================
# 13. Selective with standalone random params
# ===================================================================
def test_selective_with_standalone():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="normal"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    # Only correlate price-time, comfort is standalone random
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="mixed",
        maxiter=200, seed=42,
        correlation_groups=[[0, 1]],
    )
    est = result.estimation
    assert est.covariance_matrix is not None
    assert est.covariance_matrix.shape == (3, 3)
    cov = est.covariance_matrix
    # comfort (index 2) is standalone: zero cross-cov with price/time
    assert abs(cov[0, 2]) < 1e-8, f"Expected 0 cov(price,comfort), got {cov[0,2]}"
    assert abs(cov[1, 2]) < 1e-8, f"Expected 0 cov(time,comfort), got {cov[1,2]}"
    # n_parameters: 3 mu + 3 chol(price-time) + 1 sd(comfort) + 1 fixed = 8
    assert est.n_parameters == 8, f"Expected 8 params, got {est.n_parameters}"


_run("13. Selective with standalone random params", test_selective_with_standalone)


# ===================================================================
# 14. Create BWS simulated data
# ===================================================================
bws_df = None


def test_create_bws_data():
    """Create BWS data by adding a 'worst' column to simulated DCE data."""
    global bws_df
    df = sim_output.data.copy()
    # J=3 alts per task. For each task, pick the alt with LOWEST utility-like
    # score as worst. Use negative of choice to ensure worst != best.
    rng = np.random.default_rng(99)
    worst_rows = []
    for (rid, tid), grp in df.groupby(["respondent_id", "task_id"]):
        best_alt = grp.loc[grp["choice"] == 1, "alternative"].values[0]
        non_best = grp[grp["alternative"] != best_alt]
        # Pick random non-best as worst
        worst_alt = non_best["alternative"].values[rng.integers(len(non_best))]
        for _, row in grp.iterrows():
            worst_rows.append(1 if row["alternative"] == worst_alt else 0)
    df["worst"] = worst_rows
    # Verify: each task has exactly 1 worst, 1 best, and worst != best
    for (rid, tid), grp in df.groupby(["respondent_id", "task_id"]):
        assert grp["choice"].sum() == 1, "Exactly one best per task"
        assert grp["worst"].sum() == 1, "Exactly one worst per task"
        best_idx = grp.loc[grp["choice"] == 1].index[0]
        worst_idx = grp.loc[grp["worst"] == 1].index[0]
        assert best_idx != worst_idx, "worst != best"
    bws_df = df
    assert "worst" in bws_df.columns


_run("14. Create BWS simulated data", test_create_bws_data)


# ===================================================================
# 15. BWS + Conditional Logit
# ===================================================================
def test_bws_clogit():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
    )
    result = estimate_dataframe(
        df=bws_df, spec=spec, model_type="conditional",
        maxiter=200, seed=42,
        bws_worst_col="worst", estimate_lambda_w=True,
    )
    est = result.estimation
    assert est.success, f"BWS CL did not converge: {est.message}"
    # 4 betas + 1 lambda_w = 5 params
    assert est.n_parameters == 5, f"Expected 5 params, got {est.n_parameters}"
    # lambda_w should appear in estimates
    param_names = set(est.estimates["parameter"])
    assert "lambda_w (worst scale)" in param_names, f"Missing lambda_w param. Got: {param_names}"
    # lambda_w should be positive
    lw_row = est.estimates[est.estimates["parameter"] == "lambda_w (worst scale)"]
    assert lw_row["estimate"].values[0] > 0, "lambda_w must be positive"


_run("15. BWS + Conditional Logit", test_bws_clogit)


# ===================================================================
# 16. BWS + CLogit with lambda_w fixed (MaxDiff equivalent)
# ===================================================================
def test_bws_clogit_fixed_lw():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
    )
    result = estimate_dataframe(
        df=bws_df, spec=spec, model_type="conditional",
        maxiter=200, seed=42,
        bws_worst_col="worst", estimate_lambda_w=False,
    )
    est = result.estimation
    assert est.success
    # 4 betas only (no lambda_w)
    assert est.n_parameters == 4, f"Expected 4 params, got {est.n_parameters}"
    param_names = set(est.estimates["parameter"])
    assert "lambda_w (worst scale)" not in param_names


_run("16. BWS + CLogit fixed lambda_w (MaxDiff)", test_bws_clogit_fixed_lw)


# ===================================================================
# 17. BWS + Mixed Logit
# ===================================================================
def test_bws_mxl():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    result = estimate_dataframe(
        df=bws_df, spec=spec, model_type="mixed",
        maxiter=200, seed=42,
        bws_worst_col="worst", estimate_lambda_w=True,
    )
    est = result.estimation
    # 2 mu + 2 sd + 2 fixed + 1 lambda_w = 7
    assert est.n_parameters == 7, f"Expected 7 params, got {est.n_parameters}"
    param_names = set(est.estimates["parameter"])
    assert "lambda_w (worst scale)" in param_names
    assert "mu_price" in param_names
    assert "sd_price" in param_names


_run("17. BWS + Mixed Logit", test_bws_mxl)


# ===================================================================
# 18. BWS + GMNL
# ===================================================================
def test_bws_gmnl():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="fixed"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    result = estimate_dataframe(
        df=bws_df, spec=spec, model_type="gmnl",
        maxiter=200, seed=42,
        bws_worst_col="worst", estimate_lambda_w=True,
    )
    est = result.estimation
    # 1 mu + 1 sd + 3 fixed + 1 lambda_w + 3 GMNL(tau,sigma_tau,gamma) = 9
    assert est.n_parameters == 9, f"Expected 9 params, got {est.n_parameters}"
    param_names = set(est.estimates["parameter"])
    assert "lambda_w (worst scale)" in param_names
    assert "tau (scale mean)" in param_names


_run("18. BWS + GMNL", test_bws_gmnl)


# ===================================================================
# 19. BWS + Latent Class
# ===================================================================
def test_bws_lc():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
        n_classes=2,
    )
    result = estimate_dataframe(
        df=bws_df, spec=spec, model_type="latent_class",
        maxiter=200, seed=42, n_classes=2, n_starts=3,
        bws_worst_col="worst", estimate_lambda_w=True,
    )
    est = result.estimation
    assert est.n_classes == 2
    assert len(est.class_probabilities) == 2
    # Check lambda_w appears in estimates
    lw_rows = est.estimates[est.estimates["parameter"].str.contains("lambda_w")]
    assert len(lw_rows) > 0, "Missing lambda_w in LC estimates"


_run("19. BWS + Latent Class", test_bws_lc)


# ===================================================================
# 20. Correlation inference (delta method SEs for cov/cor)
# ===================================================================
def test_correlation_inference():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="mixed",
        maxiter=200, seed=42, correlated=True,
    )
    est = result.estimation
    # Covariance SE matrix should exist and match shape
    assert est.covariance_se is not None, "Expected covariance_se"
    assert est.covariance_se.shape == (2, 2), f"Expected 2x2, got {est.covariance_se.shape}"
    # Correlation SE matrix
    assert est.correlation_se is not None, "Expected correlation_se"
    assert est.correlation_se.shape == (2, 2)
    # Diagonal of correlation SE should be 0 (cor(x,x)=1, no variation)
    for i in range(2):
        assert est.correlation_se[i, i] < 1e-6, f"Diagonal cor SE should be ~0, got {est.correlation_se[i,i]}"
    # Correlation test table
    assert est.correlation_test is not None, "Expected correlation_test DataFrame"
    assert len(est.correlation_test) == 1, "Expected 1 off-diagonal pair for 2 random params"
    row = est.correlation_test.iloc[0]
    assert row["param_1"] == "price"
    assert row["param_2"] == "time"
    assert not np.isnan(row["cor_std_error"]), "SE should not be NaN"
    assert not np.isnan(row["z_stat"]), "z_stat should not be NaN"
    assert not np.isnan(row["p_value"]), "p_value should not be NaN"
    assert 0.0 <= row["p_value"] <= 1.0, f"p-value out of range: {row['p_value']}"


_run("20. Correlation inference (delta method SEs for cov/cor)", test_correlation_inference)


# ===================================================================
# 21. FullModelSpec + estimate_from_spec
# ===================================================================
def test_full_model_spec():
    from dce_analyzer.config import FullModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_from_spec

    spec = FullModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        model_type="mixed",
        n_draws=50,
        maxiter=200,
        seed=42,
    )
    result = estimate_from_spec(df=sim_output.data, spec=spec)
    est = result.estimation
    # Should produce the same kind of result as estimate_dataframe
    assert est.n_parameters == 6, f"Expected 6 params, got {est.n_parameters}"
    assert not est.estimates.empty
    param_names = set(est.estimates["parameter"])
    for expected in ["mu_price", "sd_price", "mu_time", "sd_time",
                     "beta_comfort", "beta_reliability"]:
        assert expected in param_names, f"Missing param: {expected}"
    assert est.n_observations == 100 * 4


_run("21. FullModelSpec + estimate_from_spec", test_full_model_spec)


# ===================================================================
# 22. Heterogeneity interactions with MMNL via FullModelSpec
# ===================================================================
def test_interactions_mmnl():
    from dce_analyzer.config import FullModelSpec, InteractionTerm, VariableSpec
    from dce_analyzer.pipeline import estimate_from_spec

    spec = FullModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="fixed"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        model_type="mixed",
        interactions=[
            InteractionTerm(columns=("price", "income")),
        ],
        n_draws=50,
        maxiter=200,
        seed=42,
    )
    result = estimate_from_spec(df=sim_output.data, spec=spec)
    est = result.estimation
    param_names = set(est.estimates["parameter"])
    # Interaction term should appear as a fixed parameter
    assert "beta_price_x_income" in param_names, (
        f"Missing interaction param. Got: {param_names}"
    )
    # 1 mu + 1 sd (price) + 3 fixed (time, comfort, reliability) + 1 interaction = 6
    assert est.n_parameters == 6, f"Expected 6 params, got {est.n_parameters}"


_run("22. Heterogeneity interactions with MMNL (InteractionTerm)", test_interactions_mmnl)


# ===================================================================
# 23. GMNL + full correlation
# ===================================================================
def test_gmnl_full_correlation():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="gmnl",
        maxiter=200, seed=42, correlated=True,
    )
    est = result.estimation
    assert est.covariance_matrix is not None, "Expected covariance matrix for GMNL+correlated"
    assert est.covariance_matrix.shape == (2, 2), (
        f"Expected 2x2 cov, got {est.covariance_matrix.shape}"
    )
    assert est.correlation_matrix is not None
    # GMNL params: 2 mu + chol(2)=3 + 2 fixed + 3 GMNL(tau,sigma_tau,gamma) = 10
    assert est.n_parameters == 10, f"Expected 10 params, got {est.n_parameters}"
    param_names = set(est.estimates["parameter"])
    assert "tau (scale mean)" in param_names
    assert "sigma_tau (scale SD)" in param_names
    assert "gamma (mixing)" in param_names


_run("23. GMNL + full correlation", test_gmnl_full_correlation)


# ===================================================================
# 24. GMNL + selective correlation
# ===================================================================
def test_gmnl_selective_correlation():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="normal"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    # Correlate price-time only; comfort is standalone random
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="gmnl",
        maxiter=200, seed=42,
        correlation_groups=[[0, 1]],
    )
    est = result.estimation
    assert est.covariance_matrix is not None
    assert est.covariance_matrix.shape == (3, 3)
    cov = est.covariance_matrix
    # comfort (index 2) is standalone: zero cross-cov with price/time
    assert abs(cov[0, 2]) < 1e-8, f"Expected 0 cov(price,comfort), got {cov[0,2]}"
    assert abs(cov[1, 2]) < 1e-8, f"Expected 0 cov(time,comfort), got {cov[1,2]}"
    param_names = set(est.estimates["parameter"])
    assert "tau (scale mean)" in param_names


_run("24. GMNL + selective correlation", test_gmnl_selective_correlation)


# ===================================================================
# 25. BWS composable functions (bws_log_prob, standard_log_prob)
# ===================================================================
def test_bws_composable_functions():
    import torch
    from dce_analyzer.bws import bws_log_prob, standard_log_prob

    # Create simple test tensors: 4 observations, 3 alternatives
    n_obs, n_alts = 4, 3
    torch.manual_seed(42)
    utility = torch.randn(n_obs, n_alts)
    y_best = torch.tensor([0, 1, 2, 0])   # chosen alt indices
    y_worst = torch.tensor([2, 0, 1, 1])  # worst alt indices (different from best)

    # Test standard_log_prob
    log_p = standard_log_prob(utility, y_best, alt_dim=-1)
    assert log_p.shape == (n_obs,), f"Expected shape ({n_obs},), got {log_p.shape}"
    # Log-probabilities must be <= 0
    assert (log_p <= 1e-6).all(), "Log-probabilities must be <= 0"
    # Probabilities must sum to 1 across alternatives (verify via logsumexp)
    log_all = torch.stack([
        standard_log_prob(utility, torch.full((n_obs,), j), alt_dim=-1)
        for j in range(n_alts)
    ], dim=1)
    prob_sums = torch.exp(log_all).sum(dim=1)
    assert torch.allclose(prob_sums, torch.ones(n_obs), atol=1e-5), (
        f"Probabilities don't sum to 1: {prob_sums}"
    )

    # Test bws_log_prob
    lambda_w = 1.0
    log_p_bws = bws_log_prob(utility, y_best, y_worst, lambda_w, alt_dim=-1)
    assert log_p_bws.shape == (n_obs,), f"Expected shape ({n_obs},), got {log_p_bws.shape}"
    assert (log_p_bws <= 1e-6).all(), "BWS log-probabilities must be <= 0"
    # BWS log-prob should be less than standard (it's a product of two probs)
    assert (log_p_bws <= log_p + 1e-6).all(), (
        "BWS log-prob should be <= standard log-prob (product of two probs)"
    )

    # Test with lambda_w as tensor
    lambda_w_tensor = torch.tensor(2.0)
    log_p_bws2 = bws_log_prob(utility, y_best, y_worst, lambda_w_tensor, alt_dim=-1)
    assert log_p_bws2.shape == (n_obs,)

    # Test with 3D utility (simulating draws): (n_obs, n_draws, n_alts)
    n_draws = 5
    utility_3d = torch.randn(n_obs, n_draws, n_alts)
    log_p_3d = standard_log_prob(utility_3d, y_best, alt_dim=-1)
    assert log_p_3d.shape == (n_obs, n_draws), f"Expected ({n_obs},{n_draws}), got {log_p_3d.shape}"

    log_p_bws_3d = bws_log_prob(utility_3d, y_best, y_worst, 1.0, alt_dim=-1)
    assert log_p_bws_3d.shape == (n_obs, n_draws), (
        f"Expected ({n_obs},{n_draws}), got {log_p_bws_3d.shape}"
    )


_run("25. BWS composable functions (bws_log_prob, standard_log_prob)", test_bws_composable_functions)


# ===================================================================
# 26. Heterogeneity interactions with Latent Class via FullModelSpec
# ===================================================================
def test_interactions_lc():
    from dce_analyzer.config import FullModelSpec, InteractionTerm, VariableSpec
    from dce_analyzer.pipeline import estimate_from_spec

    spec = FullModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
        model_type="latent_class",
        interactions=[
            InteractionTerm(columns=("price", "income")),
        ],
        n_classes=2,
        n_starts=3,
        maxiter=200,
        seed=42,
    )
    result = estimate_from_spec(df=sim_output.data, spec=spec)
    est = result.estimation
    assert est.n_classes == 2
    # Interaction param should appear in estimates
    has_interaction = any("price_x_income" in str(p) for p in est.estimates["parameter"])
    assert has_interaction, (
        f"Missing interaction param in LC estimates. Got: {list(est.estimates['parameter'])}"
    )


_run("26. Heterogeneity interactions with Latent Class (InteractionTerm)", test_interactions_lc)


# ===================================================================
# 27. FullModelSpec with dummy coding via estimate_from_spec
# ===================================================================
def test_dummy_coding_via_spec():
    from dce_analyzer.config import DummyCoding, FullModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_from_spec

    # comfort has 2 unique values (0, 1) -> dummy with ref=0 -> one dummy comfort_L1
    spec = FullModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
        model_type="conditional",
        dummy_codings=[
            DummyCoding(column="comfort", ref_level=0),
        ],
        maxiter=200,
        seed=42,
    )
    result = estimate_from_spec(df=sim_output.data, spec=spec)
    est = result.estimation
    param_names = set(est.estimates["parameter"])
    # comfort should be expanded: beta_comfort_L1 instead of beta_comfort
    assert "beta_comfort_L1" in param_names, (
        f"Missing dummy param beta_comfort_L1. Got: {param_names}"
    )
    # Original comfort should NOT appear
    assert "beta_comfort" not in param_names, (
        f"Original column should be replaced by dummy expansion. Got: {param_names}"
    )
    # price, time, reliability remain continuous
    assert "beta_price" in param_names
    assert "beta_time" in param_names
    assert "beta_reliability" in param_names
    # 3 continuous + 1 dummy = 4 params
    assert est.n_parameters == 4, f"Expected 4 params, got {est.n_parameters}"


_run("27. FullModelSpec with dummy coding via estimate_from_spec", test_dummy_coding_via_spec)


# ===================================================================
# 28. Variable ordering: dummy-coded vars expanded in-place
# ===================================================================
def test_variable_ordering_preservation():
    from dce_analyzer.config import DummyCoding, FullModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_from_spec

    # Variables in order: price (continuous), comfort (dummy, binary 0/1), time (continuous), reliability (continuous)
    # After expansion, order must be: price, comfort_L1, time, reliability
    # (not: price, time, reliability, comfort_L1 — the old buggy behavior)
    spec = FullModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="reliability", column="reliability"),
        ],
        model_type="conditional",
        dummy_codings=[
            DummyCoding(column="comfort", ref_level=0),
        ],
        maxiter=200,
        seed=42,
    )
    result = estimate_from_spec(df=sim_output.data, spec=spec)
    est = result.estimation
    param_names = list(est.estimates["parameter"])
    # Check order: price -> comfort dummy -> time -> reliability
    expected_order = ["beta_price", "beta_comfort_L1", "beta_time", "beta_reliability"]
    assert param_names == expected_order, (
        f"Variable ordering not preserved. Expected {expected_order}, got {param_names}"
    )
    # Also verify expanded_spec preserves order
    exp_spec = result.expanded_spec
    exp_var_names = [v.name for v in exp_spec.variables]
    assert exp_var_names == ["price", "comfort_L1", "time", "reliability"], (
        f"Expanded spec variable order wrong: {exp_var_names}"
    )


_run("28. Variable ordering: dummy-coded vars expanded in-place", test_variable_ordering_preservation)


# ===================================================================
# 29. WTP theta_index mapping for MMNL (SE correctness)
# ===================================================================
def test_wtp_theta_index():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe
    from dce_analyzer.wtp import compute_wtp

    # price is random, then time (fixed), comfort (fixed), reliability (fixed)
    # This creates interleaved mu/sd rows: mu_price, sd_price, beta_time, ...
    # The theta_index mapping must be correct for WTP SEs.
    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="fixed"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="mixed",
        maxiter=200, seed=42,
    )
    est = result.estimation

    # Verify theta_index column exists and is correct
    assert "theta_index" in est.estimates.columns, "theta_index column missing"
    # mu_price -> theta 0, sd_price -> theta 4, beta_time -> theta 1,
    # beta_comfort -> theta 2, beta_reliability -> theta 3
    tidx_map = dict(zip(est.estimates["parameter"], est.estimates["theta_index"]))
    assert tidx_map["mu_price"] == 0, f"mu_price should be theta 0, got {tidx_map['mu_price']}"
    assert tidx_map["beta_time"] == 1, f"beta_time should be theta 1, got {tidx_map['beta_time']}"
    assert tidx_map["sd_price"] == 4, f"sd_price should be theta 4, got {tidx_map['sd_price']}"

    # Compute WTP using time as the cost variable
    wtp_df = compute_wtp(est, cost_variable="time")
    assert not wtp_df.empty
    # Check that SEs are not NaN (vcov should be available)
    if est.vcov_matrix is not None:
        for _, row in wtp_df.iterrows():
            if row["attribute"] in ("price", "comfort", "reliability"):
                assert not np.isnan(row["wtp_std_error"]), (
                    f"WTP SE is NaN for {row['attribute']} — theta_index mapping may be wrong"
                )


_run("29. WTP theta_index mapping for MMNL (SE correctness)", test_wtp_theta_index)


# ===================================================================
# 30. 3-way interaction (price × time × income)
# ===================================================================
def test_3way_interaction():
    from dce_analyzer.config import FullModelSpec, InteractionTerm, VariableSpec
    from dce_analyzer.pipeline import estimate_from_spec

    spec = FullModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
        model_type="conditional",
        interactions=[
            InteractionTerm(columns=("price", "time", "income")),
        ],
        maxiter=200,
        seed=42,
    )
    result = estimate_from_spec(df=sim_output.data, spec=spec)
    est = result.estimation
    param_names = set(est.estimates["parameter"])
    # 3-way interaction name: price_x_time_x_income
    assert "beta_price_x_time_x_income" in param_names, (
        f"Missing 3-way interaction param. Got: {param_names}"
    )
    # 4 base + 1 interaction = 5 params
    assert est.n_parameters == 5, f"Expected 5 params, got {est.n_parameters}"


_run("30. 3-way interaction (price × time × income)", test_3way_interaction)


# ===================================================================
# 31. Attribute × attribute interaction (price × time)
# ===================================================================
def test_attribute_x_attribute_interaction():
    from dce_analyzer.config import FullModelSpec, InteractionTerm, VariableSpec
    from dce_analyzer.pipeline import estimate_from_spec

    spec = FullModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
        model_type="conditional",
        interactions=[
            InteractionTerm(columns=("price", "time")),
        ],
        maxiter=200,
        seed=42,
    )
    result = estimate_from_spec(df=sim_output.data, spec=spec)
    est = result.estimation
    param_names = set(est.estimates["parameter"])
    # attribute x attribute interaction
    assert "beta_price_x_time" in param_names, (
        f"Missing attribute x attribute interaction param. Got: {param_names}"
    )
    # 4 base + 1 interaction = 5 params
    assert est.n_parameters == 5, f"Expected 5 params, got {est.n_parameters}"


_run("31. Attribute × attribute interaction (price × time)", test_attribute_x_attribute_interaction)


# ===================================================================
# 32. Custom attribute names in simulation
# ===================================================================
def test_custom_attribute_names():
    from dce_analyzer.simulate import generate_simulated_dce

    output = generate_simulated_dce(
        n_individuals=50, n_tasks=4, n_alts=3, seed=123,
        attribute_names=["cost", "quality", "speed"],
    )
    df = output.data
    for col in ["cost", "quality", "speed"]:
        assert col in df.columns, f"Missing custom attribute column: {col}"
    # true_params should reference custom names
    for attr in ["cost", "quality", "speed"]:
        assert f"mu_{attr}" in output.true_parameters, f"Missing mu_{attr} in true_params"
        assert f"sd_{attr}" in output.true_parameters, f"Missing sd_{attr} in true_params"
    # Default attributes should NOT be present
    for col in ["price", "time", "comfort", "reliability"]:
        assert col not in df.columns, f"Default attribute '{col}' should not be present"


_run("32. Custom attribute names in simulation", test_custom_attribute_names)


# ===================================================================
# 33. Custom covariate names in simulation
# ===================================================================
def test_custom_covariate_names():
    from dce_analyzer.simulate import generate_simulated_dce

    output = generate_simulated_dce(
        n_individuals=50, n_tasks=4, n_alts=3, seed=123,
        covariate_names=["education", "gender"],
    )
    df = output.data
    for col in ["education", "gender"]:
        assert col in df.columns, f"Missing custom covariate column: {col}"
    # Covariates should be constant within each respondent
    for (rid,), grp in df.groupby(["respondent_id"]):
        for col in ["education", "gender"]:
            assert grp[col].nunique() == 1, (
                f"Covariate '{col}' not constant for respondent {rid}"
            )
    # Default covariates should NOT be present
    for col in ["income", "age"]:
        assert col not in df.columns, f"Default covariate '{col}' should not be present"


_run("33. Custom covariate names in simulation", test_custom_covariate_names)


# ===================================================================
# 34. BWS simulation (worst column)
# ===================================================================
def test_bws_simulation():
    from dce_analyzer.simulate import generate_simulated_dce

    output = generate_simulated_dce(
        n_individuals=50, n_tasks=4, n_alts=3, seed=42, bws=True,
    )
    df = output.data
    assert "worst" in df.columns, "Missing 'worst' column"
    # Each task should have exactly 1 worst
    for (rid, tid), grp in df.groupby(["respondent_id", "task_id"]):
        assert grp["worst"].sum() == 1, f"Task ({rid},{tid}) should have exactly 1 worst"
        assert grp["choice"].sum() == 1, f"Task ({rid},{tid}) should have exactly 1 best"
        best_alt = grp.loc[grp["choice"] == 1, "alternative"].values[0]
        worst_alt = grp.loc[grp["worst"] == 1, "alternative"].values[0]
        assert best_alt != worst_alt, f"Task ({rid},{tid}): worst must differ from best"


_run("34. BWS simulation (worst column)", test_bws_simulation)


# ===================================================================
# 35. BWS simulation with n_alts=2 raises ValueError
# ===================================================================
def test_bws_n_alts_2_raises():
    from dce_analyzer.simulate import generate_simulated_dce

    try:
        generate_simulated_dce(n_individuals=10, n_tasks=2, n_alts=2, seed=1, bws=True)
        raise AssertionError("Should have raised ValueError for bws with n_alts=2")
    except ValueError as exc:
        assert "n_alts >= 3" in str(exc), f"Unexpected error message: {exc}"


_run("35. BWS simulation with n_alts=2 raises ValueError", test_bws_n_alts_2_raises)


# ===================================================================
# 36. Default params backward compat (no new args)
# ===================================================================
def test_default_params_backward_compat():
    from dce_analyzer.simulate import generate_simulated_dce

    output = generate_simulated_dce(n_individuals=50, n_tasks=4, n_alts=3, seed=42)
    df = output.data
    # Same columns as original test 2
    for col in ["respondent_id", "task_id", "alternative", "choice",
                "price", "time", "comfort", "reliability", "income", "age"]:
        assert col in df.columns, f"Missing column: {col}"
    assert "worst" not in df.columns, "'worst' column should not be present by default"
    assert len(df) == 50 * 4 * 3
    # true_params should contain the hardcoded keys
    for key in ["mu_price", "sd_price", "mu_time", "sd_time",
                "mu_comfort", "sd_comfort", "beta_reliability"]:
        assert key in output.true_parameters, f"Missing true_param key: {key}"


_run("36. Default params backward compat (no new args)", test_default_params_backward_compat)


# ===================================================================
# 37. Bootstrap with Mixed Logit
# ===================================================================
def test_bootstrap_mixed_logit():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.bootstrap import run_bootstrap

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price", distribution="normal"),
            VariableSpec(name="time", column="time", distribution="normal"),
            VariableSpec(name="comfort", column="comfort", distribution="fixed"),
            VariableSpec(name="reliability", column="reliability", distribution="fixed"),
        ],
        n_draws=50,
    )
    boot = run_bootstrap(
        df=sim_output.data, spec=spec, model_type="mixed",
        n_replications=5, maxiter=100, seed=42,
    )
    assert boot.n_replications == 5
    assert boot.n_successful >= 2, f"Only {boot.n_successful} succeeded"
    # 2 normal (mu+sd each) + 2 fixed = 6 params
    assert len(boot.param_names) == 6, f"Expected 6 params, got {len(boot.param_names)}"
    assert boot.estimates_matrix.shape == (boot.n_successful, 6)
    summary = boot.summary_dataframe()
    assert isinstance(summary, pd.DataFrame)
    assert len(summary) == 6
    # Check that bootstrap SE is computed for all parameters
    for name in boot.param_names:
        se = boot.bootstrap_se[name]
        assert se >= 0, f"Bootstrap SE negative for {name}"
        assert np.isfinite(se), f"Bootstrap SE not finite for {name}"


_run("37. Bootstrap with Mixed Logit", test_bootstrap_mixed_logit)


# ===================================================================
# 38. Latent Class with EM algorithm
# ===================================================================
def test_lc_em():
    from dce_analyzer.config import ModelSpec, VariableSpec
    from dce_analyzer.pipeline import estimate_dataframe

    spec = ModelSpec(
        id_col="respondent_id",
        task_col="task_id",
        alt_col="alternative",
        choice_col="choice",
        variables=[
            VariableSpec(name="price", column="price"),
            VariableSpec(name="time", column="time"),
            VariableSpec(name="comfort", column="comfort"),
            VariableSpec(name="reliability", column="reliability"),
        ],
        n_classes=2,
    )
    result = estimate_dataframe(
        df=sim_output.data, spec=spec, model_type="latent_class",
        maxiter=200, seed=42, n_classes=2, n_starts=3, lc_method="em",
    )
    est = result.estimation
    assert est.n_classes == 2
    assert len(est.class_probabilities) == 2
    assert abs(sum(est.class_probabilities) - 1.0) < 1e-4, "Class probs must sum to 1"
    assert not est.estimates.empty
    assert not est.class_estimates.empty
    assert not est.posterior_probs.empty
    assert est.posterior_probs.shape[1] == 2

    # EM-specific fields
    assert est.optimizer_method == "EM"
    assert est.em_iterations > 0, "EM should run at least 1 iteration"
    assert len(est.em_ll_history) == est.em_iterations
    assert isinstance(est.em_converged, bool)
    # LL should be monotonically non-decreasing (EM guarantee)
    for i in range(1, len(est.em_ll_history)):
        assert est.em_ll_history[i] >= est.em_ll_history[i - 1] - 1e-6, (
            f"EM LL decreased at iter {i}: {est.em_ll_history[i-1]:.6f} -> {est.em_ll_history[i]:.6f}"
        )

    # summary_dict should include EM fields
    sd = est.summary_dict()
    assert "em_iterations" in sd
    assert "em_ll_history" in sd
    assert "em_converged" in sd


_run("38. Latent Class with EM algorithm", test_lc_em)


# ===================================================================
# Summary
# ===================================================================
print()
print("=" * 60)
n_pass = sum(1 for _, ok, _ in _results if ok)
n_fail = sum(1 for _, ok, _ in _results if not ok)
print(f"  {n_pass} passed, {n_fail} failed out of {len(_results)} tests")
print("=" * 60)

if n_fail > 0:
    print()
    print("FAILURES:")
    for name, ok, msg in _results:
        if not ok:
            print(f"  {name}: {msg}")
    print()
    sys.exit(1)
else:
    print("  ALL TESTS PASSED")
    sys.exit(0)