-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_stats.py
More file actions
350 lines (284 loc) · 10.9 KB
/
Copy pathbenchmark_stats.py
File metadata and controls
350 lines (284 loc) · 10.9 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
#!/usr/bin/env python3
"""
Statistical Benchmark for CRDT vs Centralized Comparison.
Runs 100 trials per scenario and computes:
- Mean and standard deviation
- 95% confidence intervals
- P-values (two-tailed t-test)
- Duplicate work overhead for CRDT
This produces defensible, statistically rigorous results.
"""
import sys
import math
import logging
from dataclasses import dataclass
from typing import List, Dict, Tuple
# Suppress matplotlib warnings
import warnings
warnings.filterwarnings('ignore')
from src.simulation.engine import FairSimulation, SCENARIOS, SimulationMetrics
# Configure logging
logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
@dataclass
class StatResult:
"""Statistical result for a metric."""
mean: float
std: float
ci_lower: float
ci_upper: float
n: int
def __str__(self) -> str:
return f"{self.mean:.1f} ± {self.std:.1f} (95% CI: [{self.ci_lower:.1f}, {self.ci_upper:.1f}])"
def compute_stats(values: List[float], confidence: float = 0.95) -> StatResult:
"""Compute mean, std, and confidence interval."""
n = len(values)
if n == 0:
return StatResult(0, 0, 0, 0, 0)
mean = sum(values) / n
if n == 1:
return StatResult(mean, 0, mean, mean, n)
# Sample standard deviation
variance = sum((x - mean) ** 2 for x in values) / (n - 1)
std = math.sqrt(variance)
# t-value for 95% CI with n-1 degrees of freedom
# Using approximation for large n, exact values for small n
t_values = {
10: 2.262, 20: 2.093, 30: 2.045, 50: 2.009,
100: 1.984, 200: 1.972, 500: 1.965
}
# Find closest t-value
t_val = 1.96 # default for large n
for sample_n, t in sorted(t_values.items()):
if n <= sample_n:
t_val = t
break
margin = t_val * std / math.sqrt(n)
ci_lower = mean - margin
ci_upper = mean + margin
return StatResult(mean, std, ci_lower, ci_upper, n)
def t_test(values1: List[float], values2: List[float]) -> Tuple[float, float]:
"""
Two-sample t-test (Welch's t-test for unequal variances).
Returns (t_statistic, p_value).
"""
n1, n2 = len(values1), len(values2)
if n1 < 2 or n2 < 2:
return 0.0, 1.0
mean1 = sum(values1) / n1
mean2 = sum(values2) / n2
var1 = sum((x - mean1) ** 2 for x in values1) / (n1 - 1)
var2 = sum((x - mean2) ** 2 for x in values2) / (n2 - 1)
# Welch's t-test
se = math.sqrt(var1 / n1 + var2 / n2)
if se == 0:
return 0.0, 1.0
t_stat = (mean1 - mean2) / se
# Welch-Satterthwaite degrees of freedom
num = (var1 / n1 + var2 / n2) ** 2
denom = (var1 / n1) ** 2 / (n1 - 1) + (var2 / n2) ** 2 / (n2 - 1)
df = num / denom if denom > 0 else 1
# Approximate p-value using normal distribution for large df
# For more accuracy, would need scipy.stats.t.sf
# This is a reasonable approximation for df > 30
z = abs(t_stat)
# Approximation of 2-tailed p-value
if z > 4:
p_value = 0.0001
elif z > 3:
p_value = 0.003
elif z > 2.576:
p_value = 0.01
elif z > 1.96:
p_value = 0.05
elif z > 1.645:
p_value = 0.10
else:
# Rough approximation
p_value = 2 * (1 - 0.5 * (1 + math.erf(z / math.sqrt(2))))
return t_stat, p_value
def run_statistical_benchmark(
scenarios: List[str] = None,
num_trials: int = 100,
num_robots: int = 5,
num_tasks: int = 10,
max_steps: int = 1000,
seed: int = 42
) -> Dict[str, Dict]:
"""
Run statistically rigorous benchmark.
Args:
scenarios: List of scenario names to test
num_trials: Number of trials per scenario (default 100)
num_robots: Number of robots in simulation
num_tasks: Number of tasks to complete
max_steps: Maximum steps before timeout
seed: Base random seed
Returns:
Dictionary with detailed statistical results per scenario
"""
if scenarios is None:
scenarios = ["LEO", "LEO_Eclipse", "Lunar", "Mars"]
results = {}
print("=" * 70)
print("STATISTICAL BENCHMARK: CRDT vs Centralized (100 trials per scenario)")
print("=" * 70)
print(f"Configuration: {num_robots} robots, {num_tasks} tasks, {num_trials} trials")
print("=" * 70)
for scenario_name in scenarios:
print(f"\nRunning {scenario_name}...", end=" ", flush=True)
crdt_steps = []
cent_steps = []
crdt_duplicate = []
crdt_partition = []
cent_idle = []
sim = FairSimulation(
num_robots=num_robots,
num_tasks=num_tasks,
scenario=scenario_name,
seed=seed,
max_steps=max_steps
)
for trial in range(num_trials):
# Progress indicator
if (trial + 1) % 25 == 0:
print(f"{trial + 1}", end=" ", flush=True)
# Generate new tasks and synchronized events for this trial
sim.tasks = sim._create_tasks()
sim._prepare_trial()
# Run CRDT
crdt_result = sim.run_crdt(max_steps)
crdt_steps.append(crdt_result.steps)
crdt_duplicate.append(crdt_result.duplicate_work)
crdt_partition.append(crdt_result.partition_steps)
# Run Centralized with same random events
cent_result = sim.run_centralized(max_steps)
cent_steps.append(cent_result.steps)
cent_idle.append(cent_result.idle_steps)
print("Done")
# Compute statistics
crdt_stats = compute_stats(crdt_steps)
cent_stats = compute_stats(cent_steps)
t_stat, p_value = t_test(crdt_steps, cent_steps)
# Duplicate work stats
dup_stats = compute_stats(crdt_duplicate)
total_work = sim.tasks and sum(t.duration for t in sim.tasks.values()) or 100
dup_pct = (dup_stats.mean / total_work * 100) if total_work > 0 else 0
# Improvement calculation
if cent_stats.mean > 0:
improvement = (cent_stats.mean - crdt_stats.mean) / cent_stats.mean * 100
else:
improvement = 0
results[scenario_name] = {
"crdt": {
"steps": crdt_stats,
"partition_steps": compute_stats(crdt_partition),
"duplicate_work": dup_stats,
"duplicate_pct": dup_pct,
},
"centralized": {
"steps": cent_stats,
"idle_steps": compute_stats(cent_idle),
},
"comparison": {
"improvement_pct": improvement,
"t_statistic": t_stat,
"p_value": p_value,
"significant": p_value < 0.05,
"winner": "CRDT" if crdt_stats.mean < cent_stats.mean else "Centralized",
},
"scenario_params": {
"reliability": SCENARIOS[scenario_name].reliability,
"latency": SCENARIOS[scenario_name].latency_steps,
"partition_prob": SCENARIOS[scenario_name].partition_probability,
}
}
return results
def print_results(results: Dict[str, Dict]) -> None:
"""Print formatted results table."""
print("\n" + "=" * 70)
print("RESULTS SUMMARY")
print("=" * 70)
# Header
print(f"\n{'Scenario':<12} {'CRDT Steps':<20} {'Centralized':<20} {'Diff':<10} {'p-value':<10}")
print("-" * 72)
for scenario, data in results.items():
crdt = data["crdt"]["steps"]
cent = data["centralized"]["steps"]
comp = data["comparison"]
sig = "***" if comp["p_value"] < 0.001 else "**" if comp["p_value"] < 0.01 else "*" if comp["p_value"] < 0.05 else ""
print(f"{scenario:<12} "
f"{crdt.mean:>6.1f} ± {crdt.std:<6.1f} "
f"{cent.mean:>6.1f} ± {cent.std:<6.1f} "
f"{comp['improvement_pct']:>+5.1f}% "
f"{comp['p_value']:<6.4f}{sig}")
print("-" * 72)
print("Significance: *** p<0.001, ** p<0.01, * p<0.05")
# Detailed results
print("\n" + "=" * 70)
print("DETAILED ANALYSIS")
print("=" * 70)
for scenario, data in results.items():
crdt = data["crdt"]
cent = data["centralized"]
comp = data["comparison"]
params = data["scenario_params"]
print(f"\n{scenario}:")
print(f" Scenario: {params['reliability']*100:.0f}% reliability, "
f"{params['latency']} step latency, "
f"{params['partition_prob']*100:.1f}% partition prob")
print(f" CRDT: {crdt['steps']}")
print(f" Centralized: {cent['steps']}")
print(f" Winner: {comp['winner']} ({comp['improvement_pct']:+.1f}%)")
print(f" Statistical significance: p={comp['p_value']:.4f} "
f"({'YES' if comp['significant'] else 'NO'})")
print(f" CRDT duplicate work: {crdt['duplicate_work'].mean:.1f} units "
f"({crdt['duplicate_pct']:.1f}% overhead)")
print(f" Centralized idle: {cent['idle_steps'].mean:.1f} steps")
def generate_markdown_table(results: Dict[str, Dict]) -> str:
"""Generate markdown table for README."""
lines = [
"| Scenario | CRDT (steps) | Centralized (steps) | Difference | p-value | Winner |",
"|----------|--------------|---------------------|------------|---------|--------|"
]
for scenario, data in results.items():
crdt = data["crdt"]["steps"]
cent = data["centralized"]["steps"]
comp = data["comparison"]
sig = "***" if comp["p_value"] < 0.001 else "**" if comp["p_value"] < 0.01 else "*" if comp["p_value"] < 0.05 else ""
winner = f"**{comp['winner']}**" if comp["significant"] else comp["winner"]
lines.append(
f"| {scenario} | {crdt.mean:.0f} ± {crdt.std:.0f} | "
f"{cent.mean:.0f} ± {cent.std:.0f} | "
f"{comp['improvement_pct']:+.1f}% | "
f"{comp['p_value']:.4f}{sig} | {winner} |"
)
return "\n".join(lines)
if __name__ == "__main__":
# Run benchmark
results = run_statistical_benchmark(
scenarios=["LEO", "LEO_Eclipse", "Lunar", "Mars"],
num_trials=100,
num_robots=5,
num_tasks=10,
max_steps=1000,
seed=42
)
# Print results
print_results(results)
# Generate markdown
print("\n" + "=" * 70)
print("MARKDOWN TABLE (for README)")
print("=" * 70)
print(generate_markdown_table(results))
# Summary for tweet
print("\n" + "=" * 70)
print("TWEET-READY SUMMARY")
print("=" * 70)
lunar = results.get("Lunar", {})
mars = results.get("Mars", {})
if lunar and mars:
print(f"100-trial benchmark (p<0.05):")
print(f"- Lunar: CRDT {lunar['comparison']['improvement_pct']:+.0f}% vs centralized")
print(f"- Mars: CRDT {mars['comparison']['improvement_pct']:+.0f}% vs centralized")
print(f"- CRDT overhead: ~{lunar['crdt']['duplicate_pct']:.0f}% duplicate work")