-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmulti_run_analysis.py
More file actions
1257 lines (1011 loc) · 57.8 KB
/
multi_run_analysis.py
File metadata and controls
1257 lines (1011 loc) · 57.8 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
#!/usr/bin/env python3
"""
Enhanced Multi-Run Marketplace Analysis
Statistical comparison across configurations with comprehensive visualizations.
Uses integrated analysis framework to avoid code duplication and provides
publication-ready analysis.
"""
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
from typing import Dict, List
from collections import defaultdict
import logging
from dataclasses import dataclass
# Import from integrated analysis framework
from src.analysis.core import (
MultiRunMetricsExtractor, TimeSeriesExtractor, StatisticalAnalysis,
ConfigurationParser, DataLoader, SimulationMetrics
)
from src.analysis.visualization import setup_figure, setup_subplots, save_plot
from src.analysis.visualization.market_trend_plots import smooth_line
import warnings
warnings.filterwarnings('ignore')
# Default color palette for configurations
DEFAULT_COLORS = [
'#2E86AB', # Blue
'#87CEEB', # Light Blue
'#A23B72', # Magenta
'#F18F01', # Orange
'#C73E1D', # Red
'#4CAF50', # Green
'#9C27B0', # Purple
'#FF6B6B', # Coral
'#4ECDC4', # Teal
'#45B7D1', # Sky Blue
]
# Metric name constants
METRIC_FILL_RATE = 'Fill Rate'
METRIC_BID_EFFICIENCY = 'Bid Efficiency'
METRIC_PARTICIPATION_RATE = 'Participation Rate'
METRIC_REJECTION_RATE = 'Rejection Rate'
METRIC_FREELANCER_HIRING_RATE = 'Freelancer Hiring Rate'
METRIC_MARKET_HEALTH = 'Market Health'
METRIC_BIDS_PER_JOB = 'Bids per Job'
METRIC_GINI_COEFFICIENT = 'Gini Coefficient'
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def get_significance_level(p_value: float) -> str:
"""
Determine statistical significance level from p-value.
Args:
p_value: The p-value from statistical test
Returns:
Significance notation: '***' (p<0.001), '**' (p<0.01), '*' (p<0.05), or 'ns' (not significant)
"""
if p_value < 0.001:
return "***"
elif p_value < 0.01:
return "**"
elif p_value < 0.05:
return "*"
else:
return "ns"
@dataclass
class AggregatedResults:
"""Container for aggregated results across multiple runs"""
config_name: str
n_runs: int
metrics_mean: SimulationMetrics
metrics_std: SimulationMetrics
metrics_ci_lower: SimulationMetrics
metrics_ci_upper: SimulationMetrics
raw_runs: List[Dict]
time_series_data: List[pd.DataFrame] # Time series from each run
class EnhancedMultiRunAnalyzer:
"""Enhanced analyzer for multiple marketplace simulation runs with comprehensive visualizations"""
def __init__(self, results_dir: str = "results/simuleval"):
self.results_dir = Path(results_dir)
self.configurations = defaultdict(list)
self.aggregated_results = {}
self.varying_params = set() # Track which parameters actually vary
self.synthetic_names = {} # Map config names to synthetic labels
self.config_colors = {} # Dynamic color mapping
def _extract_agent_types(self, config_name: str) -> tuple:
"""Extract freelancer and client types from config name"""
parts = config_name.split('_')
if len(parts) >= 2:
return parts[0].upper(), parts[1].upper()
return None, None
def _check_reflection_status(self, config_name: str) -> str:
"""Check if config name indicates reflection status"""
config_lower = config_name.lower()
if 'refltrue' in config_lower or 'reflection_true' in config_lower:
return 'with'
elif 'reflfalse' in config_lower or 'reflection_false' in config_lower:
return 'without'
return 'unknown'
def _create_agent_labels(self, freelancer_type: str, client_type: str) -> tuple:
"""Create readable labels for freelancer and client types"""
freelancer_label = "LLM-F" if freelancer_type == 'LLM' else "Rand-F"
client_label = "LLM-C" if client_type == 'LLM' else "Rand-C"
return freelancer_label, client_label
def _generate_synthetic_name(self, config_name: str, freelancer_type: str, client_type: str, reflection_status: str) -> str:
"""Generate a synthetic name based on agent types and reflection status"""
if reflection_status == 'with':
return f"{freelancer_type}-{client_type} (w/ Reflections)"
elif reflection_status == 'without':
return f"{freelancer_type}-{client_type} (w/o Reflections)"
elif freelancer_type == 'LLM' and client_type == 'LLM':
# We'll update this in discover_configurations after loading the data
return "LLM-F + LLM-C"
else:
freelancer_label, client_label = self._create_agent_labels(freelancer_type, client_type)
return f"{freelancer_label} + {client_label}"
def _generate_synthetic_names(self, config_names: List[str]) -> Dict[str, str]:
"""Generate synthetic names for configurations based on agent types and reflection status"""
synthetic_names = {}
for config_name in config_names:
freelancer_type, client_type = self._extract_agent_types(config_name)
if freelancer_type and client_type:
reflection_status = self._check_reflection_status(config_name)
synthetic_names[config_name] = self._generate_synthetic_name(
config_name, freelancer_type, client_type, reflection_status
)
else:
# Fallback if format is unexpected
synthetic_names[config_name] = config_name.replace('_', '-').upper()
return synthetic_names
def _generate_synthetic_names_from_data(self, config_groups: Dict[str, List[str]]) -> Dict[str, str]:
"""Generate synthetic names based on actual configuration data"""
synthetic_names = {}
for config_key in config_groups.keys():
synthetic_name = self._create_readable_name_from_key(config_key)
synthetic_names[config_key] = synthetic_name
return synthetic_names
def _create_readable_name_from_key(self, config_key: str) -> str:
"""Create a readable name from configuration key based on agent types"""
# Handle reflection variants first
if 'reflections_true' in config_key:
base_config = config_key.replace('_reflections_true', '')
base_name = self._get_base_agent_name(base_config)
return f"{base_name} (w/ Refl.)"
elif 'reflections_false' in config_key:
base_config = config_key.replace('_reflections_false', '')
base_name = self._get_base_agent_name(base_config)
return f"{base_name} (w/o Refl.)"
else:
return self._get_base_agent_name(config_key)
def _get_base_agent_name(self, config_key: str) -> str:
"""Get base agent name from configuration key"""
parts = config_key.split('_')
if len(parts) >= 2:
freelancer_type = self._format_agent_type(parts[0], 'F')
client_type = self._format_agent_type(parts[1], 'C')
return f"{freelancer_type} + {client_type}"
else:
# Fallback for unexpected format
return config_key.replace('_', '-').upper()
def _format_agent_type(self, agent_type: str, role: str) -> str:
"""Format agent type with role indicator"""
if agent_type.upper() == 'LLM':
return f"LLM-{role}"
elif agent_type.upper() == 'RANDOM':
return f"Rand-{role}"
elif agent_type.upper() == 'GREEDY':
return f"Greedy-{role}"
else:
# Handle any other agent types dynamically
return f"{agent_type.capitalize()}-{role}"
def _assign_colors_to_configs(self, config_keys: List[str]) -> Dict[str, str]:
"""Assign colors to configurations dynamically"""
colors = {}
sorted_configs = self._sort_configurations(config_keys)
for i, config_key in enumerate(sorted_configs):
color_index = i % len(DEFAULT_COLORS)
colors[config_key] = DEFAULT_COLORS[color_index]
return colors
def _parse_reflection_status_from_name(self, config_name: str) -> tuple:
"""Parse reflection status from config name"""
has_reflections = 'reflections_true' in config_name
no_reflections = 'reflections_false' in config_name
return has_reflections, no_reflections
def _get_base_config_name(self, config_name: str) -> str:
"""Remove reflection suffix from config name"""
return config_name.replace('_reflections_true', '').replace('_reflections_false', '')
def _calculate_config_priority(self, freelancer_type: str, client_type: str,
has_reflections: bool, no_reflections: bool) -> int:
"""Calculate priority order for configuration sorting"""
# Priority order: LLM-LLM, LLM-Other, Other-LLM, Other-Other
if freelancer_type == 'LLM' and client_type == 'LLM':
if has_reflections:
return 0 # LLM-LLM with reflections first
elif no_reflections:
return 1 # LLM-LLM without reflections second
else:
return 2 # Other LLM-LLM configurations
elif freelancer_type == 'LLM':
return 3 # LLM freelancer configurations
elif client_type == 'LLM':
return 4 # LLM client configurations
else:
return 5 # Non-LLM configurations
def _get_sort_key(self, config_name: str) -> tuple:
"""Generate sort key for a configuration"""
has_reflections, no_reflections = self._parse_reflection_status_from_name(config_name)
base_config = self._get_base_config_name(config_name)
parts = base_config.split('_')
if len(parts) >= 2:
freelancer_type = parts[0].upper()
client_type = parts[1].upper()
priority = self._calculate_config_priority(
freelancer_type, client_type, has_reflections, no_reflections
)
return (priority, config_name)
else:
return (6, config_name) # Fallback for unexpected format
def _sort_configurations(self, config_names: List[str]) -> List[str]:
"""Sort configurations by agent type priority and reflection status"""
return sorted(config_names, key=self._get_sort_key)
def _align_time_series_efficiently(self, time_series_data: List[pd.DataFrame]) -> List[pd.DataFrame]:
"""Efficiently align time series data with minimal memory usage"""
if not time_series_data:
return []
# Find max rounds more efficiently
max_rounds = max(len(ts) for ts in time_series_data)
aligned_data = []
for ts in time_series_data:
if len(ts) == max_rounds:
# No alignment needed
aligned_data.append(ts.copy())
elif len(ts) > 0:
# Pad efficiently using reindex
full_index = range(1, max_rounds + 1)
ts_reindexed = ts.set_index('round').reindex(full_index, method='ffill')
ts_reindexed['round'] = full_index
ts_reindexed = ts_reindexed.reset_index(drop=True)
aligned_data.append(ts_reindexed)
return aligned_data
def discover_configurations(self) -> Dict[str, List[str]]:
"""Discover and group simulation files by configuration, separating by reflection status"""
logger.info("🔍 Discovering simulation configurations...")
config_groups = defaultdict(list)
simulation_files = DataLoader.discover_simulation_files(self.results_dir)
for file_path in simulation_files:
# Load simulation data to check reflection status for LLM configurations
try:
simulation_data = DataLoader.load_simulation_data(file_path)
config = simulation_data.get('simulation_config', {})
freelancer_type = config.get('freelancer_agent_type', 'unknown')
client_type = config.get('client_agent_type', 'unknown')
enable_reflections = config.get('enable_reflections', False)
# Create configuration key that includes reflection status for LLM agents
if freelancer_type == 'llm' and client_type == 'llm':
if enable_reflections:
config_key = "llm_llm_reflections_true"
else:
config_key = "llm_llm_reflections_false"
else:
# For non-LLM configurations, use standard key
config_key = f"{freelancer_type}_{client_type}"
config_groups[config_key].append(file_path)
# Clear simulation_data from memory
del simulation_data
except Exception as e:
logger.warning(f"Failed to load {file_path} for configuration detection: {e}")
# Fallback to filename-based detection
config_key = ConfigurationParser.extract_config_key(file_path)
config_groups[config_key].append(file_path)
# Filter to configurations with multiple runs
filtered_configs = {k: v for k, v in config_groups.items() if len(v) >= 2}
# Update synthetic names and assign colors based on discovered configurations
self.synthetic_names = self._generate_synthetic_names_from_data(filtered_configs)
self.config_colors = self._assign_colors_to_configs(list(filtered_configs.keys()))
logger.info("📊 Discovered configurations:")
for config, files in filtered_configs.items():
synthetic_name = self.synthetic_names.get(config, config)
logger.info(f" • {synthetic_name}: {len(files)} runs")
return filtered_configs
def aggregate_configuration_results(self, config_name: str, run_files: List[str]) -> AggregatedResults:
"""Aggregate results across multiple runs of the same configuration"""
readable_name = ConfigurationParser.get_readable_config_name(config_name, self.varying_params)
logger.info(f"📈 Aggregating results for {readable_name} ({len(run_files)} files)...")
# Extract metrics and time series from all runs
run_metrics = []
time_series_data = []
for i, file_path in enumerate(run_files):
try:
logger.info(f" Processing file {i+1}/{len(run_files)}: {Path(file_path).name}")
simulation_data = DataLoader.load_simulation_data(file_path)
# Extract basic metrics
metrics = MultiRunMetricsExtractor.extract_basic_metrics(simulation_data)
run_metrics.append(metrics.__dict__)
# Extract time series data
time_series = TimeSeriesExtractor.extract_round_metrics(simulation_data)
time_series_data.append(time_series)
# Clear simulation_data from memory immediately
del simulation_data
except Exception as e:
logger.exception(f"Failed to process {file_path}: {e}")
continue
if not run_metrics:
logger.error(f"❌ No valid metrics found for {config_name}")
return None
# Calculate statistical aggregates for each metric
metrics_df = pd.DataFrame(run_metrics)
means = {}
stds = {}
ci_lowers = {}
ci_uppers = {}
for col in metrics_df.columns:
values = metrics_df[col].values
mean_val, ci_lower, ci_upper = StatisticalAnalysis.calculate_confidence_interval(values)
std_val = np.std(values, ddof=1) if len(values) > 1 else 0
means[col] = mean_val
stds[col] = std_val
ci_lowers[col] = ci_lower
ci_uppers[col] = ci_upper
return AggregatedResults(
config_name=config_name,
n_runs=len(run_metrics),
metrics_mean=SimulationMetrics(**means),
metrics_std=SimulationMetrics(**stds),
metrics_ci_lower=SimulationMetrics(**ci_lowers),
metrics_ci_upper=SimulationMetrics(**ci_uppers),
raw_runs=run_metrics,
time_series_data=time_series_data
)
def run_complete_analysis(self) -> Dict[str, AggregatedResults]:
"""Run complete multi-configuration analysis"""
logger.info("🚀 Starting enhanced multi-run analysis...")
# Discover configurations
configurations = self.discover_configurations()
if not configurations:
logger.error("❌ No configurations with multiple runs found!")
return {}
# Synthetic names are already set in discover_configurations()
# No need to regenerate them here
# Aggregate results for each configuration
results = {}
for config_name, run_files in configurations.items():
aggregated = self.aggregate_configuration_results(config_name, run_files)
if aggregated:
results[config_name] = aggregated
logger.info(f"✅ Analysis complete for {len(results)} configurations")
return results
def create_comprehensive_visualizations(
self,
results: Dict[str, AggregatedResults],
output_dir: str = "analysis_results"
):
"""Create comprehensive comparison visualizations"""
output_path = Path(output_dir)
output_path.mkdir(exist_ok=True)
logger.info(f"📊 Creating comprehensive visualizations in {output_path}")
total_steps = 5
# 1. Performance comparison with error bars
logger.info(f"[1/{total_steps}] Creating performance comparison plot...")
self._create_performance_comparison(results, output_path / "performance_comparison.png")
# 2. Detailed metrics grid
logger.info(f"[2/{total_steps}] Creating detailed metrics grid...")
self._create_detailed_metrics_grid(results, output_path / "detailed_metrics_grid.png")
# 3. **NEW**: Time series trend plots for each configuration
logger.info(f"[3/{total_steps}] Creating individual trend plots...")
self._create_trend_plots(results, output_path / "trend_analysis")
# 4. **NEW**: Comparative time series overlay with variance bands
logger.info(f"[4/{total_steps}] Creating comparative trend overlays...")
self._create_comparative_trends(results, output_path / "comparative_trends.png")
# 5. **NEW**: Market evolution heatmap
logger.info(f"[5/{total_steps}] Creating market evolution heatmap...")
self._create_market_evolution_heatmap(results, output_path / "market_evolution_heatmap.png")
logger.info("✅ All visualizations created successfully")
def _create_performance_comparison(self, results: Dict[str, AggregatedResults], output_path: Path):
"""Create main performance comparison plot with error bars"""
fig, axes = setup_subplots(2, 2, (15, 8))
axes = axes.flatten() # Ensure axes is 1D array for easy indexing
config_names = self._sort_configurations(list(results.keys()))
synthetic_names = [self.synthetic_names.get(name, f"Config {i+1}") for i, name in enumerate(config_names)]
x_pos = np.arange(len(config_names))
colors = [self.config_colors.get(name, '#1f77b4') for name in config_names]
# Fill Rate
fill_rates = [results[name].metrics_mean.fill_rate for name in config_names]
fill_rate_errors = [
(results[name].metrics_mean.fill_rate - results[name].metrics_ci_lower.fill_rate,
results[name].metrics_ci_upper.fill_rate - results[name].metrics_mean.fill_rate)
for name in config_names
]
axes[0].bar(x_pos, fill_rates, yerr=np.array(fill_rate_errors).T,
capsize=5, alpha=0.8, color=colors)
axes[0].set_title(METRIC_FILL_RATE, fontweight='bold')
axes[0].set_ylabel(METRIC_FILL_RATE)
axes[0].set_xticks(x_pos)
axes[0].set_xticklabels(synthetic_names, rotation=15, ha='right')
axes[0].yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
# Bid Efficiency metric
efficiencies = [results[name].metrics_mean.bid_efficiency for name in config_names]
efficiency_errors = [
(results[name].metrics_mean.bid_efficiency - results[name].metrics_ci_lower.bid_efficiency,
results[name].metrics_ci_upper.bid_efficiency - results[name].metrics_mean.bid_efficiency)
for name in config_names
]
axes[1].bar(x_pos, efficiencies, yerr=np.array(efficiency_errors).T,
capsize=5, alpha=0.8, color=colors)
axes[1].set_title(METRIC_BID_EFFICIENCY, fontweight='bold')
axes[1].set_ylabel(METRIC_BID_EFFICIENCY)
axes[1].set_xticks(x_pos)
axes[1].set_xticklabels(synthetic_names, rotation=15, ha='right')
axes[1].yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
# Participation Rate
participation = [results[name].metrics_mean.participation_rate for name in config_names]
participation_errors = [
(results[name].metrics_mean.participation_rate - results[name].metrics_ci_lower.participation_rate,
results[name].metrics_ci_upper.participation_rate - results[name].metrics_mean.participation_rate)
for name in config_names
]
axes[2].bar(x_pos, participation, yerr=np.array(participation_errors).T,
capsize=5, alpha=0.8, color=colors)
axes[2].set_title(METRIC_PARTICIPATION_RATE, fontweight='bold')
axes[2].set_ylabel(METRIC_PARTICIPATION_RATE)
axes[2].set_xticks(x_pos)
axes[2].set_xticklabels(synthetic_names, rotation=15, ha='right')
axes[2].yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
# Market Health
health = [results[name].metrics_mean.market_health_score for name in config_names]
health_errors = [
(results[name].metrics_mean.market_health_score - results[name].metrics_ci_lower.market_health_score,
results[name].metrics_ci_upper.market_health_score - results[name].metrics_mean.market_health_score)
for name in config_names
]
axes[3].bar(x_pos, health, yerr=np.array(health_errors).T,
capsize=5, alpha=0.8, color=colors)
axes[3].set_title('Market Health Score', fontweight='bold')
axes[3].set_ylabel('Health Score')
axes[3].set_xticks(x_pos)
axes[3].set_xticklabels(synthetic_names, rotation=15, ha='right')
save_plot(
fig, output_path,
'Marketplace Performance Comparison\n(Error bars show 95% confidence intervals)'
)
def _create_detailed_metrics_grid(self, results: Dict[str, AggregatedResults], output_path: Path):
"""Create detailed metrics grid with error bars"""
fig, axes = setup_subplots(2, 4, (20, 10))
axes = axes.flatten() # Ensure axes is 1D array for easy indexing
config_names = self._sort_configurations(list(results.keys()))
synthetic_names = [self.synthetic_names.get(name, f"Config {i+1}") for i, name in enumerate(config_names)]
x_pos = np.arange(len(config_names))
metrics = [
(METRIC_FILL_RATE, 'fill_rate', lambda y, _: '{:.0%}'.format(y)),
(METRIC_BID_EFFICIENCY, 'bid_efficiency', lambda y, _: '{:.0%}'.format(y)),
(METRIC_BIDS_PER_JOB, 'avg_bids_per_job', lambda y, _: '{:.1f}'.format(y)),
(METRIC_PARTICIPATION_RATE, 'participation_rate', lambda y, _: '{:.0%}'.format(y)),
(METRIC_REJECTION_RATE, 'rejection_rate', lambda y, _: '{:.0%}'.format(y)),
(METRIC_FREELANCER_HIRING_RATE, 'freelancer_hiring_rate', lambda y, _: '{:.0%}'.format(y)),
(METRIC_GINI_COEFFICIENT, 'gini_coefficient', lambda y, _: '{:.2f}'.format(y)),
(METRIC_MARKET_HEALTH, 'market_health_score', lambda y, _: '{:.1f}'.format(y))
]
for idx, (title, metric_key, formatter) in enumerate(metrics):
ax = axes[idx]
values = [getattr(results[name].metrics_mean, metric_key) for name in config_names]
errors = [getattr(results[name].metrics_std, metric_key) for name in config_names]
colors = [self.config_colors.get(name, '#1f77b4') for name in config_names]
bars = ax.bar(x_pos, values, yerr=errors, capsize=4, alpha=0.8, color=colors)
ax.set_title(title, fontweight='bold')
ax.set_xticks(x_pos)
ax.set_xticklabels(synthetic_names, rotation=45, ha='right', fontsize=10)
ax.yaxis.set_major_formatter(plt.FuncFormatter(formatter))
# Add value labels on bars
for bar, value, error in zip(bars, values, errors):
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height + error,
formatter(value, None), ha='center', va='bottom', fontsize=8)
save_plot(
fig, output_path,
'Detailed Metrics Comparison\n(Error bars show ±1 standard deviation)'
)
def _get_metric_values(self, results: Dict[str, AggregatedResults], config_name: str, metric_key: str) -> List[float]:
"""Extract metric values for a configuration"""
return [getattr(SimulationMetrics(**run), metric_key) for run in results[config_name].raw_runs]
def _calculate_p_value_matrix(self, results: Dict[str, AggregatedResults],
config_names: List[str], metric_key: str) -> np.ndarray:
"""Calculate p-value matrix for a single metric across all configurations"""
n_configs = len(config_names)
p_matrix = np.ones((n_configs, n_configs)) # Initialize with 1s (no significance)
for i, config1 in enumerate(config_names):
for j, config2 in enumerate(config_names):
if i != j:
values1 = self._get_metric_values(results, config1, metric_key)
values2 = self._get_metric_values(results, config2, metric_key)
if len(values1) >= 2 and len(values2) >= 2:
_, p_value = StatisticalAnalysis.perform_t_test(values1, values2)
p_matrix[i, j] = p_value
return p_matrix
def _plot_significance_heatmap(self, ax, p_matrix: np.ndarray,
synthetic_names: List[str], metric_key: str):
"""Plot a single significance heatmap"""
sns.heatmap(p_matrix,
xticklabels=synthetic_names,
yticklabels=synthetic_names,
annot=True,
fmt='.3f',
cmap='RdYlBu_r',
center=0.05,
ax=ax,
cbar_kws={'label': 'p-value'})
ax.set_title(f'{metric_key.replace("_", " ").title()}\nStatistical Significance')
def _create_significance_heatmap(self, results: Dict[str, AggregatedResults], output_path: Path):
"""Create statistical significance heatmap"""
config_names = self._sort_configurations(list(results.keys()))
synthetic_names = [self.synthetic_names.get(name, f"Config {i+1}") for i, name in enumerate(config_names)]
metrics_to_test = ['fill_rate', 'bid_efficiency', 'participation_rate']
fig, axes = setup_subplots(1, len(metrics_to_test),
(5 * len(metrics_to_test), 5))
for metric_idx, metric_key in enumerate(metrics_to_test):
p_matrix = self._calculate_p_value_matrix(results, config_names, metric_key)
ax = axes[metric_idx] if len(metrics_to_test) > 1 else axes
self._plot_significance_heatmap(ax, p_matrix, synthetic_names, metric_key)
save_plot(fig, output_path, 'Statistical Significance Tests (p-values)')
def _create_variance_analysis(self, results: Dict[str, AggregatedResults], output_path: Path):
"""Create variance analysis plot"""
fig, axes = setup_subplots(1, 2, (15, 6))
# axes is already flattened for 1xN layouts
config_names = self._sort_configurations(list(results.keys()))
synthetic_names = [self.synthetic_names.get(name, f"Config {i+1}") for i, name in enumerate(config_names)]
# Coefficient of variation for key metrics
metrics = ['fill_rate', 'bid_efficiency', 'participation_rate', 'rejection_rate']
metric_labels = [METRIC_FILL_RATE, METRIC_BID_EFFICIENCY, METRIC_PARTICIPATION_RATE, METRIC_REJECTION_RATE]
cv_data = []
for config_name in config_names:
cv_row = []
for metric in metrics:
mean_val = getattr(results[config_name].metrics_mean, metric)
std_val = getattr(results[config_name].metrics_std, metric)
cv = std_val / mean_val if mean_val > 0 else 0
cv_row.append(cv)
cv_data.append(cv_row)
# Coefficient of variation heatmap
cv_df = pd.DataFrame(cv_data, index=synthetic_names, columns=metric_labels)
sns.heatmap(cv_df, annot=True, fmt='.3f', cmap='YlOrRd', ax=axes[0])
axes[0].set_title('Coefficient of Variation\n(Lower = More Consistent)')
# Run count and confidence interval width
n_runs = [results[name].n_runs for name in config_names]
colors = [self.config_colors.get(name, '#1f77b4') for name in config_names]
axes[1].bar(synthetic_names, n_runs, alpha=0.8, color=colors)
axes[1].set_title('Number of Runs per Configuration')
axes[1].set_ylabel('Number of Runs')
axes[1].tick_params(axis='x', rotation=45)
save_plot(fig, output_path, 'Variance Analysis')
def _create_trend_plots(self, results: Dict[str, AggregatedResults], output_dir: Path):
"""Create time series trend plots for each configuration"""
output_dir.mkdir(exist_ok=True)
sorted_config_names = self._sort_configurations(list(results.keys()))
logger.info(f"📊 Creating individual trend plots for {len(sorted_config_names)} configurations...")
for i, config_name in enumerate(sorted_config_names):
result = results[config_name]
synthetic_name = self.synthetic_names.get(config_name, f"Config {config_name}")
logger.info(f" Creating trends for {synthetic_name} ({i+1}/{len(sorted_config_names)})...")
# Average time series across runs
if not result.time_series_data:
logger.warning(f" No time series data for {synthetic_name}, skipping...")
continue
# Use more efficient time series alignment
aligned_data = self._align_time_series_efficiently(result.time_series_data)
if not aligned_data:
logger.warning(f" Failed to align time series for {synthetic_name}, skipping...")
continue
# Calculate mean and std across runs for each round
combined_df = pd.concat(aligned_data, keys=range(len(aligned_data)))
mean_df = combined_df.groupby(level=1).mean()
std_df = combined_df.groupby(level=1).std().fillna(0)
# Create trend plots
fig, axes = setup_subplots(2, 3, (18, 12))
axes = axes.flatten() # Ensure axes is 1D array for easy indexing
rounds = mean_df['round'].values
# Fill rate trend
self._plot_trend_with_confidence(
axes[0], rounds,
mean_df['jobs_filled_cumulative'] / mean_df['jobs_posted'].cumsum(),
std_df['jobs_filled_cumulative'] / mean_df['jobs_posted'].cumsum(),
f'{METRIC_FILL_RATE} Over Time', METRIC_FILL_RATE, format_pct=True
)
# Participation rate trend
self._plot_trend_with_confidence(
axes[1], rounds,
mean_df['participation_rate'], std_df['participation_rate'],
f'{METRIC_PARTICIPATION_RATE} Over Time', METRIC_PARTICIPATION_RATE, format_pct=True
)
# Bid rejection rate trend
self._plot_trend_with_confidence(
axes[2], rounds,
mean_df['bid_rejection_rate'], std_df['bid_rejection_rate'],
f'Bid {METRIC_REJECTION_RATE} Over Time', METRIC_REJECTION_RATE, format_pct=True
)
# Market health trend
self._plot_trend_with_confidence(
axes[3], rounds,
mean_df['health_score'], std_df['health_score'],
'Market Health Over Time', 'Health Score'
)
# Bids per job trend
self._plot_trend_with_confidence(
axes[4], rounds,
mean_df['avg_bids_per_job'], std_df['avg_bids_per_job'],
'Competition Level Over Time', 'Avg Bids per Job'
)
# Supply-demand ratio trend
self._plot_trend_with_confidence(
axes[5], rounds,
mean_df['supply_demand_ratio'], std_df['supply_demand_ratio'],
'Supply-Demand Balance Over Time', 'Supply/Demand Ratio'
)
save_plot(
fig, output_dir / f"trends_{config_name}.png",
f'Market Trends: {synthetic_name}\n(n={result.n_runs} runs, shaded areas show ±1 std)'
)
def _plot_trend_with_confidence(self, ax, x, y_mean, y_std, title, ylabel, format_pct=False):
"""Plot trend line with confidence band"""
# Smooth the lines
x_smooth, y_smooth = smooth_line(x, y_mean)
# Plot mean line
ax.plot(x_smooth, y_smooth, linewidth=2, alpha=0.8)
# Plot confidence band
y_upper = y_mean + y_std
y_lower = y_mean - y_std
# Clamp percentage values to valid range [0, 1]
if format_pct:
y_upper = np.clip(y_upper, 0, 1)
y_lower = np.clip(y_lower, 0, 1)
ax.fill_between(x, y_upper, y_lower, alpha=0.3)
ax.set_title(title, fontweight='bold')
ax.set_xlabel('Round')
ax.set_ylabel(ylabel)
ax.grid(True, alpha=0.3)
if format_pct:
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
def _create_comparative_trends(self, results: Dict[str, AggregatedResults], output_path: Path):
"""Create overlay of trends across all configurations with variance bands"""
logger.info("📊 Creating comparative trend overlays...")
fig, axes = setup_subplots(2, 2, (16, 12))
axes = axes.flatten() # Ensure axes is 1D array for easy indexing
metrics_to_plot = [
('fill_rate', f'{METRIC_FILL_RATE} Over Time', True),
('participation_rate', f'{METRIC_PARTICIPATION_RATE} Over Time', True),
('bid_rejection_rate', f'Bid {METRIC_REJECTION_RATE} Over Time', True),
('health_score', 'Market Health Over Time', False)
]
for metric_idx, (metric_key, title, format_pct) in enumerate(metrics_to_plot):
ax = axes[metric_idx]
logger.info(f" Processing metric: {title}")
sorted_config_names = self._sort_configurations(list(results.keys()))
for config_name in sorted_config_names:
result = results[config_name]
if not result.time_series_data:
continue
synthetic_name = self.synthetic_names.get(config_name, f"Config {config_name}")
color = self.config_colors.get(config_name, '#1f77b4')
# Use efficient time series alignment
aligned_data = self._align_time_series_efficiently(result.time_series_data)
if not aligned_data:
continue
# Calculate mean and std across runs
combined_df = pd.concat(aligned_data, keys=range(len(aligned_data)))
mean_df = combined_df.groupby(level=1).mean()
std_df = combined_df.groupby(level=1).std().fillna(0)
rounds = mean_df['round'].values
if metric_key == 'fill_rate':
y_mean = mean_df['jobs_filled_cumulative'] / mean_df['jobs_posted'].cumsum()
y_std = std_df['jobs_filled_cumulative'] / mean_df['jobs_posted'].cumsum()
else:
y_mean = mean_df[metric_key].values
y_std = std_df[metric_key].values
# Smooth the mean line
x_smooth, y_smooth = smooth_line(rounds, y_mean)
# Plot mean line
ax.plot(x_smooth, y_smooth, label=synthetic_name, linewidth=2,
alpha=0.8, color=color)
# Add confidence band (±1 std)
y_upper = y_mean + y_std
y_lower = y_mean - y_std
# Clamp percentage values to valid range [0, 1]
if format_pct:
y_upper = np.clip(y_upper, 0, 1)
y_lower = np.clip(y_lower, 0, 1)
ax.fill_between(rounds, y_upper, y_lower, alpha=0.2, color=color)
ax.set_title(title, fontweight='bold')
ax.set_xlabel('Round')
ax.set_ylabel(metric_key.replace('_', ' ').title())
ax.grid(True, alpha=0.3)
ax.legend()
if format_pct:
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda y, _: '{:.0%}'.format(y)))
save_plot(fig, output_path, 'Comparative Market Trends Across Configurations\n(Shaded areas show ±1 standard deviation)')
def _create_market_evolution_heatmap(self, results: Dict[str, AggregatedResults], output_path: Path):
"""Create heatmap showing market evolution across configurations"""
logger.info("📊 Processing market evolution data...")
config_names = self._sort_configurations(list(results.keys()))
synthetic_names = [self.synthetic_names.get(name, f"Config {i+1}") for i, name in enumerate(config_names)]
# Create evolution matrix (early vs late rounds)
evolution_data = []
for i, config_name in enumerate(config_names):
logger.info(f" Processing evolution for {synthetic_names[i]} ({i+1}/{len(config_names)})...")
result = results[config_name]
if not result.time_series_data:
evolution_data.append([0, 0, 0, 0]) # Placeholder
continue
# Calculate early vs late metrics
combined_df = pd.concat(result.time_series_data, keys=range(len(result.time_series_data)))
mean_df = combined_df.groupby(level=1).mean()
n_rounds = len(mean_df)
# Handle edge cases where DataFrame is too small
if n_rounds < 6: # Need at least 6 rounds to split meaningfully
evolution_data.append([0, 0, 0, 0]) # No meaningful change data
continue
early_rounds = mean_df.iloc[:n_rounds//3] # First third
late_rounds = mean_df.iloc[-n_rounds//3:] # Last third
# Calculate evolution metrics with bounds checking
try:
if len(early_rounds) > 0 and len(late_rounds) > 0:
# Calculate fill rate change safely
early_fill_rate = (
early_rounds['jobs_filled_cumulative'].iloc[-1] / max(early_rounds['jobs_posted'].sum(), 1)
if len(early_rounds) > 0 else 0
)
late_fill_rate = (
late_rounds['jobs_filled_cumulative'].iloc[-1] / max(late_rounds['jobs_posted'].sum(), 1)
if len(late_rounds) > 0 else 0
)
fill_rate_change = late_fill_rate - early_fill_rate
participation_change = (
late_rounds['participation_rate'].mean() -
early_rounds['participation_rate'].mean()
)
health_change = (
late_rounds['health_score'].mean() -
early_rounds['health_score'].mean()
)
competition_change = (
late_rounds['avg_bids_per_job'].mean() -
early_rounds['avg_bids_per_job'].mean()
)
else:
fill_rate_change = participation_change = health_change = competition_change = 0
except (IndexError, KeyError, ZeroDivisionError):
# Handle any calculation errors gracefully
fill_rate_change = participation_change = health_change = competition_change = 0
evolution_data.append([fill_rate_change, participation_change,
health_change, competition_change])
# Create heatmap
fig, ax = setup_figure((12, 8))
evolution_df = pd.DataFrame(
evolution_data,
index=synthetic_names,
columns=['Fill Rate Δ', 'Participation Δ', 'Health Δ', 'Competition Δ']
)
sns.heatmap(evolution_df, annot=True, fmt='.3f', cmap='RdBu_r',
center=0, ax=ax, cbar_kws={'label': 'Change (Late - Early)'})
save_plot(fig, output_path, 'Market Evolution Heatmap\n(Late rounds - Early rounds)')
def _create_radar_chart(self, results: Dict[str, AggregatedResults], output_path: Path):
"""Create radar chart comparing configuration performance"""
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw=dict(projection='polar'))
# Metrics for radar chart (normalized to 0-1 scale)
metrics = [
'fill_rate',
'bid_efficiency',
'participation_rate',
'freelancer_hiring_rate',
'market_health_score'
]
metric_labels = [
METRIC_FILL_RATE,
METRIC_BID_EFFICIENCY,
'Participation', # Abbreviated for radar chart
'Hiring Rate', # Abbreviated for radar chart
'Market Health' # Abbreviated for radar chart
]
# Calculate angles for radar chart
angles = np.linspace(0, 2 * np.pi, len(metrics), endpoint=False).tolist()
angles += angles[:1] # Close the circle
# Plot each configuration
config_names = self._sort_configurations(list(results.keys()))
for config_name in config_names:
result = results[config_name]
synthetic_name = self.synthetic_names.get(config_name, f"Config {config_name}")
color = self.config_colors.get(config_name, '#1f77b4')
# Normalize metrics to 0-1 scale
values = []
for metric in metrics:
raw_value = getattr(result.metrics_mean, metric)
# Normalize based on metric type
if metric == 'market_health_score':
normalized_value = raw_value # Already 0-1 scale
else:
# For rates, already 0-1, for others normalize by max across all configs
if metric in ['fill_rate', 'bid_efficiency', 'participation_rate', 'freelancer_hiring_rate']:
normalized_value = raw_value # Already proportions
else:
max_val = max(getattr(results[cn].metrics_mean, metric) for cn in config_names)
normalized_value = raw_value / max_val if max_val > 0 else 0
values.append(normalized_value)
values += values[:1] # Close the circle
# Plot the configuration
ax.plot(angles, values, 'o-', linewidth=2, label=synthetic_name,
color=color, alpha=0.8)
ax.fill(angles, values, alpha=0.1, color=color)