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
| import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from scipy import stats from scipy.optimize import minimize_scalar import seaborn as sns
np.random.seed(42)
plt.style.use('seaborn-v0_8-darkgrid') sns.set_palette("husl")
class BiosignatureDetector: """ Bayesian biosignature detection system with false positive minimization """ def __init__(self, prior_bio=0.01): """ Initialize detector with prior probability of biological origin Parameters: ----------- prior_bio : float Prior probability that a signal is biological (default: 1%) """ self.prior_bio = prior_bio self.prior_abiotic = 1 - prior_bio self.bio_ch4_mean = 15.0 self.bio_ch4_std = 3.0 self.bio_isotope_mean = -60.0 self.bio_isotope_std = 5.0 self.bio_temporal_mean = 0.7 self.bio_temporal_std = 0.15 self.abiotic_ch4_mean = 8.0 self.abiotic_ch4_std = 4.0 self.abiotic_isotope_mean = -40.0 self.abiotic_isotope_std = 8.0 self.abiotic_temporal_mean = 0.3 self.abiotic_temporal_std = 0.2 def likelihood_bio(self, ch4, isotope, temporal): """Calculate likelihood P(X|Bio) for multi-feature observation""" l1 = stats.norm.pdf(ch4, self.bio_ch4_mean, self.bio_ch4_std) l2 = stats.norm.pdf(isotope, self.bio_isotope_mean, self.bio_isotope_std) l3 = stats.norm.pdf(temporal, self.bio_temporal_mean, self.bio_temporal_std) return l1 * l2 * l3 def likelihood_abiotic(self, ch4, isotope, temporal): """Calculate likelihood P(X|Abiotic) for multi-feature observation""" l1 = stats.norm.pdf(ch4, self.abiotic_ch4_mean, self.abiotic_ch4_std) l2 = stats.norm.pdf(isotope, self.abiotic_isotope_mean, self.abiotic_isotope_std) l3 = stats.norm.pdf(temporal, self.abiotic_temporal_mean, self.abiotic_temporal_std) return l1 * l2 * l3 def posterior_bio(self, ch4, isotope, temporal): """Calculate posterior probability P(Bio|X) using Bayes' theorem""" l_bio = self.likelihood_bio(ch4, isotope, temporal) l_abiotic = self.likelihood_abiotic(ch4, isotope, temporal) numerator = l_bio * self.prior_bio denominator = l_bio * self.prior_bio + l_abiotic * self.prior_abiotic if denominator < 1e-100: return 0.5 return numerator / denominator def generate_samples(self, n_bio=500, n_abiotic=500): """Generate synthetic observation samples""" bio_ch4 = np.random.normal(self.bio_ch4_mean, self.bio_ch4_std, n_bio) bio_isotope = np.random.normal(self.bio_isotope_mean, self.bio_isotope_std, n_bio) bio_temporal = np.random.normal(self.bio_temporal_mean, self.bio_temporal_std, n_bio) abiotic_ch4 = np.random.normal(self.abiotic_ch4_mean, self.abiotic_ch4_std, n_abiotic) abiotic_isotope = np.random.normal(self.abiotic_isotope_mean, self.abiotic_isotope_std, n_abiotic) abiotic_temporal = np.random.normal(self.abiotic_temporal_mean, self.abiotic_temporal_std, n_abiotic) return (bio_ch4, bio_isotope, bio_temporal), (abiotic_ch4, abiotic_isotope, abiotic_temporal) def calculate_fpr_tpr(self, bio_samples, abiotic_samples, threshold): """Calculate False Positive Rate and True Positive Rate for a given threshold""" bio_ch4, bio_isotope, bio_temporal = bio_samples abiotic_ch4, abiotic_isotope, abiotic_temporal = abiotic_samples bio_posteriors = np.array([ self.posterior_bio(ch4, iso, temp) for ch4, iso, temp in zip(bio_ch4, bio_isotope, bio_temporal) ]) abiotic_posteriors = np.array([ self.posterior_bio(ch4, iso, temp) for ch4, iso, temp in zip(abiotic_ch4, abiotic_isotope, abiotic_temporal) ]) tpr = np.sum(bio_posteriors >= threshold) / len(bio_posteriors) fpr = np.sum(abiotic_posteriors >= threshold) / len(abiotic_posteriors) return fpr, tpr def find_optimal_threshold(self, bio_samples, abiotic_samples, max_fpr=0.05): """Find optimal threshold that minimizes FPR while maintaining detection capability""" thresholds = np.linspace(0, 1, 1000) fprs = [] tprs = [] for threshold in thresholds: fpr, tpr = self.calculate_fpr_tpr(bio_samples, abiotic_samples, threshold) fprs.append(fpr) tprs.append(tpr) fprs = np.array(fprs) tprs = np.array(tprs) valid_indices = np.where(fprs <= max_fpr)[0] if len(valid_indices) == 0: optimal_idx = np.argmin(fprs) else: optimal_idx = valid_indices[np.argmax(tprs[valid_indices])] optimal_threshold = thresholds[optimal_idx] optimal_fpr = fprs[optimal_idx] optimal_tpr = tprs[optimal_idx] return optimal_threshold, optimal_fpr, optimal_tpr, thresholds, fprs, tprs
detector = BiosignatureDetector(prior_bio=0.01)
print("Generating synthetic biosignature observations...") bio_samples, abiotic_samples = detector.generate_samples(n_bio=1000, n_abiotic=1000)
print("Optimizing detection threshold to minimize false positives...") optimal_threshold, opt_fpr, opt_tpr, thresholds, fprs, tprs = detector.find_optimal_threshold( bio_samples, abiotic_samples, max_fpr=0.05 )
print(f"\n{'='*60}") print(f"OPTIMAL BIOSIGNATURE DETECTION PARAMETERS") print(f"{'='*60}") print(f"Optimal Threshold: {optimal_threshold:.4f}") print(f"False Positive Rate: {opt_fpr:.4f} ({opt_fpr*100:.2f}%)") print(f"True Positive Rate: {opt_tpr:.4f} ({opt_tpr*100:.2f}%)") print(f"{'='*60}\n")
fig = plt.figure(figsize=(20, 12))
ax1 = plt.subplot(2, 3, 1) bio_ch4, bio_isotope, bio_temporal = bio_samples abiotic_ch4, abiotic_isotope, abiotic_temporal = abiotic_samples
ax1.hist(bio_ch4, bins=40, alpha=0.6, label='Biological', color='green', density=True) ax1.hist(abiotic_ch4, bins=40, alpha=0.6, label='Abiotic', color='red', density=True) ax1.set_xlabel('Methane Concentration (ppm)', fontsize=11) ax1.set_ylabel('Probability Density', fontsize=11) ax1.set_title('Feature 1: CH₄ Concentration Distribution', fontsize=12, fontweight='bold') ax1.legend() ax1.grid(True, alpha=0.3)
ax2 = plt.subplot(2, 3, 2) ax2.hist(bio_isotope, bins=40, alpha=0.6, label='Biological', color='green', density=True) ax2.hist(abiotic_isotope, bins=40, alpha=0.6, label='Abiotic', color='red', density=True) ax2.set_xlabel('δ¹³C Isotopic Ratio (‰)', fontsize=11) ax2.set_ylabel('Probability Density', fontsize=11) ax2.set_title('Feature 2: Isotopic Signature Distribution', fontsize=12, fontweight='bold') ax2.legend() ax2.grid(True, alpha=0.3)
ax3 = plt.subplot(2, 3, 3) ax3.hist(bio_temporal, bins=40, alpha=0.6, label='Biological', color='green', density=True) ax3.hist(abiotic_temporal, bins=40, alpha=0.6, label='Abiotic', color='red', density=True) ax3.set_xlabel('Seasonal Variability Coefficient', fontsize=11) ax3.set_ylabel('Probability Density', fontsize=11) ax3.set_title('Feature 3: Temporal Variation Distribution', fontsize=12, fontweight='bold') ax3.legend() ax3.grid(True, alpha=0.3)
ax4 = plt.subplot(2, 3, 4) ax4.plot(fprs, tprs, linewidth=2.5, color='blue', label='ROC Curve') ax4.plot([0, 1], [0, 1], 'k--', linewidth=1.5, label='Random Classifier') ax4.scatter([opt_fpr], [opt_tpr], color='red', s=200, zorder=5, label=f'Optimal Point\n(FPR={opt_fpr:.3f}, TPR={opt_tpr:.3f})', marker='*') ax4.set_xlabel('False Positive Rate', fontsize=11) ax4.set_ylabel('True Positive Rate', fontsize=11) ax4.set_title('ROC Curve: Detection Performance', fontsize=12, fontweight='bold') ax4.legend(loc='lower right') ax4.grid(True, alpha=0.3) ax4.set_xlim([-0.02, 1.02]) ax4.set_ylim([-0.02, 1.02])
auc = np.trapz(tprs, fprs) ax4.text(0.6, 0.2, f'AUC = {auc:.3f}', fontsize=12, bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5))
ax5 = plt.subplot(2, 3, 5) ax5.plot(thresholds, fprs, linewidth=2.5, color='red', label='False Positive Rate') ax5.plot(thresholds, tprs, linewidth=2.5, color='green', label='True Positive Rate') ax5.axvline(optimal_threshold, color='black', linestyle='--', linewidth=2, label=f'Optimal Threshold = {optimal_threshold:.3f}') ax5.axhline(0.05, color='orange', linestyle=':', linewidth=2, label='Target FPR = 0.05') ax5.set_xlabel('Detection Threshold', fontsize=11) ax5.set_ylabel('Rate', fontsize=11) ax5.set_title('Detection Rates vs Threshold', fontsize=12, fontweight='bold') ax5.legend() ax5.grid(True, alpha=0.3)
ax6 = plt.subplot(2, 3, 6) bio_posteriors = np.array([ detector.posterior_bio(ch4, iso, temp) for ch4, iso, temp in zip(bio_ch4, bio_isotope, bio_temporal) ]) abiotic_posteriors = np.array([ detector.posterior_bio(ch4, iso, temp) for ch4, iso, temp in zip(abiotic_ch4, abiotic_isotope, abiotic_temporal) ])
ax6.hist(bio_posteriors, bins=50, alpha=0.6, label='True Biological', color='green', density=True) ax6.hist(abiotic_posteriors, bins=50, alpha=0.6, label='True Abiotic', color='red', density=True) ax6.axvline(optimal_threshold, color='black', linestyle='--', linewidth=2, label=f'Decision Threshold') ax6.set_xlabel('Posterior P(Bio|X)', fontsize=11) ax6.set_ylabel('Probability Density', fontsize=11) ax6.set_title('Posterior Probability Distributions', fontsize=12, fontweight='bold') ax6.legend() ax6.grid(True, alpha=0.3)
plt.tight_layout() plt.savefig('biosignature_detection_2d.png', dpi=300, bbox_inches='tight') plt.show()
print("2D visualization complete.\n")
print("Generating 3D decision boundary visualization...")
fig = plt.figure(figsize=(20, 6))
ax_3d1 = fig.add_subplot(131, projection='3d')
n_samples = 300 bio_indices = np.random.choice(len(bio_ch4), n_samples, replace=False) abiotic_indices = np.random.choice(len(abiotic_ch4), n_samples, replace=False)
bio_post_3d = np.array([ detector.posterior_bio(bio_ch4[i], bio_isotope[i], bio_temporal[i]) for i in bio_indices ]) abiotic_post_3d = np.array([ detector.posterior_bio(abiotic_ch4[i], abiotic_isotope[i], abiotic_temporal[i]) for i in abiotic_indices ])
scatter1 = ax_3d1.scatter(bio_ch4[bio_indices], bio_isotope[bio_indices], bio_post_3d, c='green', marker='o', s=30, alpha=0.6, label='Biological') scatter2 = ax_3d1.scatter(abiotic_ch4[abiotic_indices], abiotic_isotope[abiotic_indices], abiotic_post_3d, c='red', marker='^', s=30, alpha=0.6, label='Abiotic')
ch4_range = np.linspace(0, 25, 30) iso_range = np.linspace(-80, -20, 30) CH4_grid, ISO_grid = np.meshgrid(ch4_range, iso_range) THRESHOLD_grid = np.full_like(CH4_grid, optimal_threshold)
ax_3d1.plot_surface(CH4_grid, ISO_grid, THRESHOLD_grid, alpha=0.3, color='yellow', label='Decision Boundary')
ax_3d1.set_xlabel('CH₄ (ppm)', fontsize=10) ax_3d1.set_ylabel('δ¹³C (‰)', fontsize=10) ax_3d1.set_zlabel('P(Bio|X)', fontsize=10) ax_3d1.set_title('3D Decision Space:\nCH₄ vs Isotope vs Posterior', fontsize=11, fontweight='bold') ax_3d1.legend(loc='upper left') ax_3d1.view_init(elev=20, azim=45)
ax_3d2 = fig.add_subplot(132, projection='3d')
scatter3 = ax_3d2.scatter(bio_ch4[bio_indices], bio_temporal[bio_indices], bio_post_3d, c='green', marker='o', s=30, alpha=0.6, label='Biological') scatter4 = ax_3d2.scatter(abiotic_ch4[abiotic_indices], abiotic_temporal[abiotic_indices], abiotic_post_3d, c='red', marker='^', s=30, alpha=0.6, label='Abiotic')
temp_range = np.linspace(0, 1, 30) CH4_grid2, TEMP_grid = np.meshgrid(ch4_range, temp_range) THRESHOLD_grid2 = np.full_like(CH4_grid2, optimal_threshold)
ax_3d2.plot_surface(CH4_grid2, TEMP_grid, THRESHOLD_grid2, alpha=0.3, color='yellow')
ax_3d2.set_xlabel('CH₄ (ppm)', fontsize=10) ax_3d2.set_ylabel('Temporal Var.', fontsize=10) ax_3d2.set_zlabel('P(Bio|X)', fontsize=10) ax_3d2.set_title('3D Decision Space:\nCH₄ vs Temporal vs Posterior', fontsize=11, fontweight='bold') ax_3d2.legend(loc='upper left') ax_3d2.view_init(elev=20, azim=45)
ax_3d3 = fig.add_subplot(133, projection='3d')
scatter5 = ax_3d3.scatter(bio_ch4[bio_indices], bio_isotope[bio_indices], bio_temporal[bio_indices], c=bio_post_3d, cmap='RdYlGn', marker='o', s=40, alpha=0.7, vmin=0, vmax=1, label='Biological') scatter6 = ax_3d3.scatter(abiotic_ch4[abiotic_indices], abiotic_isotope[abiotic_indices], abiotic_temporal[abiotic_indices], c=abiotic_post_3d, cmap='RdYlGn', marker='^', s=40, alpha=0.7, vmin=0, vmax=1, label='Abiotic')
ax_3d3.set_xlabel('CH₄ (ppm)', fontsize=10) ax_3d3.set_ylabel('δ¹³C (‰)', fontsize=10) ax_3d3.set_zlabel('Temporal Var.', fontsize=10) ax_3d3.set_title('3D Feature Space\n(Color = Posterior Probability)', fontsize=11, fontweight='bold') ax_3d3.view_init(elev=25, azim=60)
cbar = plt.colorbar(scatter5, ax=ax_3d3, shrink=0.5, aspect=5) cbar.set_label('P(Bio|X)', fontsize=9)
plt.tight_layout() plt.savefig('biosignature_detection_3d.png', dpi=300, bbox_inches='tight') plt.show()
print("3D visualization complete.\n")
bio_predictions = bio_posteriors >= optimal_threshold abiotic_predictions = abiotic_posteriors >= optimal_threshold
true_positives = np.sum(bio_predictions) false_negatives = np.sum(~bio_predictions) false_positives = np.sum(abiotic_predictions) true_negatives = np.sum(~abiotic_predictions)
print(f"{'='*60}") print(f"CONFUSION MATRIX AT OPTIMAL THRESHOLD") print(f"{'='*60}") print(f" Predicted Bio Predicted Abiotic") print(f"True Bio {true_positives:8d} {false_negatives:8d}") print(f"True Abiotic {false_positives:8d} {true_negatives:8d}") print(f"{'='*60}") print(f"\nPrecision: {true_positives/(true_positives + false_positives):.4f}") print(f"Recall (TPR): {true_positives/(true_positives + false_negatives):.4f}") print(f"Specificity: {true_negatives/(true_negatives + false_positives):.4f}") print(f"F1-Score: {2*true_positives/(2*true_positives + false_positives + false_negatives):.4f}") print(f"{'='*60}\n")
|