-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_all_figures.py
More file actions
505 lines (384 loc) · 18.5 KB
/
generate_all_figures.py
File metadata and controls
505 lines (384 loc) · 18.5 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
#!/usr/bin/env python3
"""
Generate all figures for Proth Prime ML Research Paper
Creates publication-quality visualizations from project data
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pathlib import Path
import json
# Set publication-quality style
plt.style.use('seaborn-v0_8-paper')
sns.set_palette("husl")
plt.rcParams['figure.dpi'] = 300
plt.rcParams['savefig.dpi'] = 300
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 11
plt.rcParams['axes.titlesize'] = 12
plt.rcParams['xtick.labelsize'] = 9
plt.rcParams['ytick.labelsize'] = 9
plt.rcParams['legend.fontsize'] = 9
# Create output directory
OUTPUT_DIR = Path("figures")
OUTPUT_DIR.mkdir(exist_ok=True)
print("=" * 70)
print("PROTH PRIME ML PROJECT - FIGURE GENERATION")
print("=" * 70)
def figure_2_data_distribution():
"""Figure 2: Data Distribution in (k, n)-Space"""
print("\n[Figure 2] Data Distribution in (k, n)-Space...")
try:
df = pd.read_csv("proth_ml_features.csv")
# Sample if too large
if len(df) > 10000:
df_sample = df.sample(n=10000, random_state=42)
else:
df_sample = df
fig, ax = plt.subplots(figsize=(10, 6))
# Plot composites
composites = df_sample[df_sample['label'] == 0]
ax.scatter(composites['n'], composites['k'],
alpha=0.3, s=10, c='lightcoral', label='Composite', marker='.')
# Plot primes
primes = df_sample[df_sample['label'] == 1]
ax.scatter(primes['n'], primes['k'],
alpha=0.6, s=20, c='dodgerblue', label='Prime', marker='*')
ax.set_xlabel('n (exponent in $k \\cdot 2^n + 1$)')
ax.set_ylabel('k (multiplier)')
ax.set_title('Training Data Distribution in (k, n)-Space')
ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig(OUTPUT_DIR / "figure2_data_distribution.png", bbox_inches='tight')
plt.savefig(OUTPUT_DIR / "figure2_data_distribution.pdf", bbox_inches='tight')
plt.close()
print(f" ✅ Saved: {OUTPUT_DIR / 'figure2_data_distribution.png'}")
print(f" 📊 Plotted {len(primes)} primes, {len(composites)} composites")
except FileNotFoundError:
print(" ⚠️ File not found: proth_ml_features.csv")
def figure_3_precision_vs_baseline():
"""Figure 3: Precision vs Random Baseline"""
print("\n[Figure 3] Precision vs Random Baseline...")
# Example data - replace with your actual stats
datasets = ['Gold\n(≥0.999)', 'Silver\n(≥0.9)', 'Bronze\n(Top 1000)']
model_precision = [12.0, 8.5, 5.2] # % primes found
baseline_precision = [2.2, 2.0, 2.3] # Random baseline %
x = np.arange(len(datasets))
width = 0.35
fig, ax = plt.subplots(figsize=(10, 6))
bars1 = ax.bar(x - width/2, model_precision, width,
label='XGBoost Model', color='#2E86AB', alpha=0.8)
bars2 = ax.bar(x + width/2, baseline_precision, width,
label='Random Baseline', color='#A23B72', alpha=0.8)
ax.set_ylabel('Prime Fraction (%)')
ax.set_xlabel('Candidate List')
ax.set_title('Model Precision vs Random Baseline by Probability Threshold')
ax.set_xticks(x)
ax.set_xticklabels(datasets)
ax.legend()
ax.grid(True, alpha=0.3, axis='y')
# Add value labels on bars
for bars in [bars1, bars2]:
for bar in bars:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height,
f'{height:.1f}%', ha='center', va='bottom', fontsize=8)
plt.tight_layout()
plt.savefig(OUTPUT_DIR / "figure3_precision_vs_baseline.png", bbox_inches='tight')
plt.savefig(OUTPUT_DIR / "figure3_precision_vs_baseline.pdf", bbox_inches='tight')
plt.close()
print(f" ✅ Saved: {OUTPUT_DIR / 'figure3_precision_vs_baseline.png'}")
def figure_4_enrichment_factor():
"""Figure 4: Enrichment Factor vs Probability Threshold"""
print("\n[Figure 4] Enrichment Factor vs Threshold...")
thresholds = [0.85, 0.90, 0.95, 0.99, 0.999]
enrichment = [2.2, 3.8, 4.2, 5.5, 6.0]
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(thresholds, enrichment, marker='o', linewidth=2,
markersize=8, color='#F18F01', label='Enrichment Factor')
ax.axhline(y=1.0, color='gray', linestyle='--', alpha=0.5, label='No Enrichment')
ax.set_xlabel('Probability Threshold')
ax.set_ylabel('Enrichment Factor\n(Model Precision / Baseline Precision)')
ax.set_title('Enrichment Factor vs Probability Threshold')
ax.grid(True, alpha=0.3)
ax.legend()
# Add value labels
for x, y in zip(thresholds, enrichment):
ax.text(x, y + 0.2, f'{y:.1f}×', ha='center', fontsize=9)
plt.tight_layout()
plt.savefig(OUTPUT_DIR / "figure4_enrichment_factor.png", bbox_inches='tight')
plt.savefig(OUTPUT_DIR / "figure4_enrichment_factor.pdf", bbox_inches='tight')
plt.close()
print(f" ✅ Saved: {OUTPUT_DIR / 'figure4_enrichment_factor.png'}")
def figure_5_feature_importance():
"""Figure 5: Feature Importance from XGBoost Model"""
print("\n[Figure 5] Feature Importance...")
# Train model to get feature importances
try:
df = pd.read_csv("proth_ml_features.csv")
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
features = ['k', 'n', 'log_k', 'log_n', 'k_mod_4', 'n_mod_4',
'bit_length_k', 'bit_length_n', 'log_proth']
X = df[features]
y = df['label']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y)
model = XGBClassifier(use_label_encoder=False, eval_metric='logloss',
n_jobs=-1, random_state=42)
model.fit(X_train, y_train)
importances = model.feature_importances_
feature_names = features
# Sort by importance
indices = np.argsort(importances)
fig, ax = plt.subplots(figsize=(10, 6))
ax.barh(range(len(indices)), importances[indices], color='#06A77D')
ax.set_yticks(range(len(indices)))
ax.set_yticklabels([feature_names[i] for i in indices])
ax.set_xlabel('Feature Importance (Gain)')
ax.set_title('XGBoost Feature Importance for Proth Prime Prediction')
ax.grid(True, alpha=0.3, axis='x')
plt.tight_layout()
plt.savefig(OUTPUT_DIR / "figure5_feature_importance.png", bbox_inches='tight')
plt.savefig(OUTPUT_DIR / "figure5_feature_importance.pdf", bbox_inches='tight')
plt.close()
print(f" ✅ Saved: {OUTPUT_DIR / 'figure5_feature_importance.png'}")
print(f" 📊 Top feature: {feature_names[indices[-1]]} ({importances[indices[-1]]:.3f})")
except Exception as e:
print(f" ⚠️ Error: {e}")
def figure_6_calibration_plot():
"""Figure 6: Calibration Plot (Reliability Diagram)"""
print("\n[Figure 6] Calibration Plot...")
try:
df = pd.read_csv("proth_ml_features.csv")
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
features = ['k', 'n', 'log_k', 'log_n', 'k_mod_4', 'n_mod_4',
'bit_length_k', 'bit_length_n', 'log_proth']
X = df[features]
y = df['label']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y)
model = XGBClassifier(use_label_encoder=False, eval_metric='logloss',
n_jobs=-1, random_state=42)
model.fit(X_train, y_train)
y_proba = model.predict_proba(X_test)[:, 1]
# Create calibration bins
n_bins = 10
bins = np.linspace(0, 1, n_bins + 1)
bin_centers = (bins[:-1] + bins[1:]) / 2
bin_fractions = []
bin_counts = []
for i in range(n_bins):
mask = (y_proba >= bins[i]) & (y_proba < bins[i + 1])
if mask.sum() > 0:
fraction = y_test[mask].mean()
bin_fractions.append(fraction)
bin_counts.append(mask.sum())
else:
bin_fractions.append(np.nan)
bin_counts.append(0)
fig, ax = plt.subplots(figsize=(8, 8))
# Perfect calibration line
ax.plot([0, 1], [0, 1], 'k--', label='Perfect Calibration', alpha=0.5)
# Actual calibration
valid_bins = [i for i, c in enumerate(bin_counts) if c > 0]
ax.plot([bin_centers[i] for i in valid_bins],
[bin_fractions[i] for i in valid_bins],
marker='o', linewidth=2, markersize=8,
color='#C73E1D', label='Model Calibration')
ax.set_xlabel('Mean Predicted Probability')
ax.set_ylabel('Empirical Prime Fraction')
ax.set_title('Calibration Plot (Reliability Diagram)')
ax.legend()
ax.grid(True, alpha=0.3)
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
plt.tight_layout()
plt.savefig(OUTPUT_DIR / "figure6_calibration_plot.png", bbox_inches='tight')
plt.savefig(OUTPUT_DIR / "figure6_calibration_plot.pdf", bbox_inches='tight')
plt.close()
print(f" ✅ Saved: {OUTPUT_DIR / 'figure6_calibration_plot.png'}")
except Exception as e:
print(f" ⚠️ Error: {e}")
def figure_7_roc_curve():
"""Figure 7: ROC Curve on Test Set"""
print("\n[Figure 7] ROC Curve...")
try:
df = pd.read_csv("proth_ml_features.csv")
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve, auc
features = ['k', 'n', 'log_k', 'log_n', 'k_mod_4', 'n_mod_4',
'bit_length_k', 'bit_length_n', 'log_proth']
X = df[features]
y = df['label']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y)
model = XGBClassifier(use_label_encoder=False, eval_metric='logloss',
n_jobs=-1, random_state=42)
model.fit(X_train, y_train)
y_proba = model.predict_proba(X_test)[:, 1]
fpr, tpr, _ = roc_curve(y_test, y_proba)
roc_auc = auc(fpr, tpr)
fig, ax = plt.subplots(figsize=(8, 8))
ax.plot(fpr, tpr, color='#F18F01', linewidth=2,
label=f'ROC Curve (AUC = {roc_auc:.3f})')
ax.plot([0, 1], [0, 1], 'k--', alpha=0.5, label='Random Classifier')
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('ROC Curve on Test Set')
ax.legend(loc='lower right')
ax.grid(True, alpha=0.3)
ax.set_xlim([0, 1])
ax.set_ylim([0, 1])
plt.tight_layout()
plt.savefig(OUTPUT_DIR / "figure7_roc_curve.png", bbox_inches='tight')
plt.savefig(OUTPUT_DIR / "figure7_roc_curve.pdf", bbox_inches='tight')
plt.close()
print(f" ✅ Saved: {OUTPUT_DIR / 'figure7_roc_curve.png'}")
print(f" 📊 ROC AUC: {roc_auc:.3f}")
except Exception as e:
print(f" ⚠️ Error: {e}")
def figure_8_score_histogram():
"""Figure 8: Score Histogram - Primes vs Composites"""
print("\n[Figure 8] Score Histogram...")
try:
df = pd.read_csv("proth_ml_features.csv")
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
features = ['k', 'n', 'log_k', 'log_n', 'k_mod_4', 'n_mod_4',
'bit_length_k', 'bit_length_n', 'log_proth']
X = df[features]
y = df['label']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y)
model = XGBClassifier(use_label_encoder=False, eval_metric='logloss',
n_jobs=-1, random_state=42)
model.fit(X_train, y_train)
y_proba = model.predict_proba(X_test)[:, 1]
fig, ax = plt.subplots(figsize=(10, 6))
# Primes
primes_proba = y_proba[y_test == 1]
ax.hist(primes_proba, bins=50, alpha=0.6, color='dodgerblue',
label=f'Primes (n={len(primes_proba)})', density=True)
# Composites
composites_proba = y_proba[y_test == 0]
ax.hist(composites_proba, bins=50, alpha=0.6, color='lightcoral',
label=f'Composites (n={len(composites_proba)})', density=True)
ax.set_xlabel('Predicted Probability')
ax.set_ylabel('Density')
ax.set_title('Distribution of Predicted Probabilities: Primes vs Composites')
ax.legend()
ax.grid(True, alpha=0.3, axis='y')
plt.tight_layout()
plt.savefig(OUTPUT_DIR / "figure8_score_histogram.png", bbox_inches='tight')
plt.savefig(OUTPUT_DIR / "figure8_score_histogram.pdf", bbox_inches='tight')
plt.close()
print(f" ✅ Saved: {OUTPUT_DIR / 'figure8_score_histogram.png'}")
except Exception as e:
print(f" ⚠️ Error: {e}")
def figure_9_prime_discovery_over_time():
"""Figure 9: Prime Discovery Over Time"""
print("\n[Figure 9] Prime Discovery Over Time...")
try:
# Try to load from proth_primes.csv or prime_logfile.csv
try:
df = pd.read_csv("proth_primes.csv")
df['timestamp'] = pd.to_datetime(df['timestamp'])
except:
df = pd.read_csv("prime_logfile.csv")
df['timestamp'] = pd.to_datetime(df['timestamp'])
df = df.sort_values('timestamp')
df['cumulative_count'] = range(1, len(df) + 1)
# Calculate time elapsed in hours
df['hours_elapsed'] = (df['timestamp'] - df['timestamp'].min()).dt.total_seconds() / 3600
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(df['hours_elapsed'], df['cumulative_count'],
linewidth=2, color='#2E86AB', marker='o', markersize=4)
ax.set_xlabel('Time Elapsed (hours)')
ax.set_ylabel('Cumulative Primes Discovered')
ax.set_title('Prime Discovery Rate Over Time')
ax.grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig(OUTPUT_DIR / "figure9_prime_discovery_time.png", bbox_inches='tight')
plt.savefig(OUTPUT_DIR / "figure9_prime_discovery_time.pdf", bbox_inches='tight')
plt.close()
print(f" ✅ Saved: {OUTPUT_DIR / 'figure9_prime_discovery_time.png'}")
print(f" 📊 Total primes: {len(df)}, Time span: {df['hours_elapsed'].max():.1f} hours")
except Exception as e:
print(f" ⚠️ File not found or error: {e}")
def figure_10_k_score_vs_yield():
"""Figure 10: K-Value Modular Score vs Prime Yield"""
print("\n[Figure 10] K-Value Score vs Prime Yield...")
try:
# Load k scores
k_scores = pd.read_csv("top_1000_k_by_modular_score.csv", sep=';')
if 'collision_score' in k_scores.columns:
k_scores = k_scores.rename(columns={'collision_score': 'score'})
# Load primes found
primes = pd.read_csv("proth_primes.csv")
prime_counts = primes.groupby('k').size().reset_index(name='prime_count')
# Merge
merged = k_scores.merge(prime_counts, on='k', how='left')
merged['prime_count'] = merged['prime_count'].fillna(0)
# Take top 50 k values for clarity
merged = merged.head(50)
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8), sharex=True)
# Top: Modular scores
ax1.bar(range(len(merged)), merged['score'], color='#A23B72', alpha=0.7)
ax1.set_ylabel('Modular Collision Score')
ax1.set_title('K-Value Modular Score vs Empirical Prime Yield (Top 50 k)')
ax1.grid(True, alpha=0.3, axis='y')
# Bottom: Prime counts
ax2.bar(range(len(merged)), merged['prime_count'], color='#06A77D', alpha=0.7)
ax2.set_xlabel('k-value (ranked by modular score)')
ax2.set_ylabel('Primes Discovered')
ax2.set_xticks(range(0, len(merged), 5))
ax2.set_xticklabels([f"{int(merged.iloc[i]['k'])}" for i in range(0, len(merged), 5)], rotation=45)
ax2.grid(True, alpha=0.3, axis='y')
plt.tight_layout()
plt.savefig(OUTPUT_DIR / "figure10_k_score_vs_yield.png", bbox_inches='tight')
plt.savefig(OUTPUT_DIR / "figure10_k_score_vs_yield.pdf", bbox_inches='tight')
plt.close()
print(f" ✅ Saved: {OUTPUT_DIR / 'figure10_k_score_vs_yield.png'}")
# Compute correlation
corr = merged['score'].corr(merged['prime_count'])
print(f" 📊 Correlation (score vs yield): {corr:.3f}")
except Exception as e:
print(f" ⚠️ Error: {e}")
def main():
"""Generate all figures"""
print("\n🎨 Starting figure generation...\n")
# Results figures (use existing data)
figure_2_data_distribution()
figure_3_precision_vs_baseline()
figure_4_enrichment_factor()
# Model analysis (requires training)
figure_5_feature_importance()
figure_6_calibration_plot()
figure_7_roc_curve()
figure_8_score_histogram()
# Pipeline/search behavior
figure_9_prime_discovery_over_time()
figure_10_k_score_vs_yield()
print("\n" + "=" * 70)
print(f"✅ Figure generation complete!")
print(f"📁 All figures saved to: {OUTPUT_DIR.absolute()}")
print("=" * 70)
print("\nGenerated figures:")
print(" • Figure 2: Data Distribution (scatter plot)")
print(" • Figure 3: Precision vs Baseline (bar chart)")
print(" • Figure 4: Enrichment Factor (line plot)")
print(" • Figure 5: Feature Importance (bar chart)")
print(" • Figure 6: Calibration Plot (reliability diagram)")
print(" • Figure 7: ROC Curve")
print(" • Figure 8: Score Histogram")
print(" • Figure 9: Prime Discovery Over Time")
print(" • Figure 10: K-Value Score vs Yield")
print("\n📝 Note: Figure 1 (Pipeline Flowchart) should be created manually")
print(" using PowerPoint, draw.io, or TikZ for best results.")
if __name__ == "__main__":
main()