Gaussian Mixtures and Clustering
This example demonstrates Gaussian mixture operations and clustering algorithms.
Overview
Gaussian mixtures and clustering are essential for:
Multi-hypothesis tracking: Representing multiple target hypotheses
Mixture reduction: Limiting computational complexity
Data clustering: Grouping measurements or tracks
Clustering Algorithms
- K-Means
Partitions data into k clusters
Minimizes within-cluster variance
Fast and scalable
- DBSCAN
Density-based clustering
Handles arbitrary cluster shapes
Identifies outliers automatically
- Hierarchical
Builds cluster tree (dendrogram)
Multiple linkage options
Flexible number of clusters
Gaussian Mixture Operations
Moment matching: Reducing mixture to single Gaussian
Runnalls’ algorithm: Optimal mixture reduction
West’s algorithm: Alternative reduction method
Splitting/merging: Dynamic mixture management
Code Highlights
The example demonstrates:
K-means clustering with
kmeans()DBSCAN with
dbscan()Hierarchical clustering with
agglomerative_clustering()Gaussian mixture reduction with
reduce_mixture_runnalls()
Source Code
1"""
2Gaussian Mixtures Example
3=========================
4
5This example demonstrates Gaussian mixture operations and clustering
6algorithms in PyTCL:
7
8Gaussian Mixture Operations:
9- Component representation and manipulation
10- Moment matching (computing mean and covariance)
11- Mixture merging and reduction
12- Runnalls' and West's reduction algorithms
13
14Clustering Algorithms:
15- K-means with K-means++ initialization
16- DBSCAN (density-based clustering)
17- Hierarchical/agglomerative clustering
18- Elbow method for K selection
19
20These algorithms are essential for multi-target tracking (PHD filters),
21hypothesis reduction in MHT, and general density estimation.
22"""
23
24from pathlib import Path
25
26import numpy as np
27import plotly.graph_objects as go
28
29# Output directory for generated plots
30OUTPUT_DIR = Path(__file__).parent.parent / "docs" / "_static" / "images" / "examples"
31OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
32from plotly.subplots import make_subplots
33
34# Global flag to control plotting
35SHOW_PLOTS = True
36
37
38def create_ellipse_trace(mean, cov, color="blue", opacity=0.3, n_std=2, name=None):
39 """Create a plotly trace for an ellipse representing a 2D Gaussian covariance."""
40 eigvals, eigvecs = np.linalg.eigh(cov)
41 # Angle in radians
42 angle = np.arctan2(eigvecs[1, 0], eigvecs[0, 0])
43
44 # Semi-axes lengths
45 a = n_std * np.sqrt(eigvals[0])
46 b = n_std * np.sqrt(eigvals[1])
47
48 # Parametric ellipse
49 t = np.linspace(0, 2 * np.pi, 100)
50 x = a * np.cos(t)
51 y = b * np.sin(t)
52
53 # Rotate
54 x_rot = x * np.cos(angle) - y * np.sin(angle) + mean[0]
55 y_rot = x * np.sin(angle) + y * np.cos(angle) + mean[1]
56
57 return go.Scatter(
58 x=x_rot,
59 y=y_rot,
60 mode="lines",
61 fill="toself",
62 fillcolor=color,
63 opacity=opacity,
64 line=dict(color=color, width=2),
65 name=name,
66 showlegend=name is not None,
67 )
68
69
70from pytcl.clustering import ( # Gaussian mixture operations; K-means; DBSCAN; Hierarchical clustering
71 DBSCANResult,
72 GaussianComponent,
73 GaussianMixture,
74 HierarchicalResult,
75 KMeansResult,
76 agglomerative_clustering,
77 compute_distance_matrix,
78 cut_dendrogram,
79 dbscan,
80 dbscan_predict,
81 kmeans,
82 kmeans_elbow,
83 kmeans_plusplus_init,
84 merge_gaussians,
85 moment_match,
86 prune_mixture,
87 reduce_mixture_runnalls,
88 reduce_mixture_west,
89 runnalls_merge_cost,
90)
91
92
93def _rng(seed: int = 42) -> np.random.Generator:
94 """A seeded Generator for library calls that take one.
95
96 ``np.random.seed`` seeds the legacy global RNG; ``kmeans`` and friends
97 default to ``np.random.default_rng()``, a separate stream that ignores it.
98 Without passing this the figures under docs/_static change every rebuild.
99 """
100 return np.random.default_rng(seed)
101
102
103def demo_gaussian_components():
104 """Demonstrate Gaussian component operations."""
105 print("=" * 70)
106 print("Gaussian Component Operations Demo")
107 print("=" * 70)
108
109 # Create individual Gaussian components
110 comp1 = GaussianComponent(
111 weight=0.4,
112 mean=np.array([0.0, 0.0]),
113 covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
114 )
115
116 comp2 = GaussianComponent(
117 weight=0.6,
118 mean=np.array([3.0, 3.0]),
119 covariance=np.array([[2.0, 0.5], [0.5, 2.0]]),
120 )
121
122 print("\nComponent 1:")
123 print(f" Weight: {comp1.weight}")
124 print(f" Mean: {comp1.mean}")
125 print(f" Covariance diagonal: {np.diag(comp1.covariance)}")
126
127 print("\nComponent 2:")
128 print(f" Weight: {comp2.weight}")
129 print(f" Mean: {comp2.mean}")
130 print(f" Covariance diagonal: {np.diag(comp2.covariance)}")
131
132 # Create a mixture
133 mixture = GaussianMixture([comp1, comp2])
134 print(f"\nMixture has {len(mixture)} components")
135 print(f"Total weight: {sum(c.weight for c in mixture.components)}")
136
137
138def demo_moment_matching():
139 """Demonstrate moment matching for mixture approximation."""
140 print("\n" + "=" * 70)
141 print("Moment Matching Demo")
142 print("=" * 70)
143
144 # Create a mixture of 3 components
145 weights = np.array([0.3, 0.5, 0.2])
146 means = [np.array([0.0, 0.0]), np.array([2.0, 1.0]), np.array([1.0, 3.0])]
147 covariances = [np.eye(2) * 0.5, np.eye(2) * 0.8, np.eye(2) * 0.3]
148
149 print("\nOriginal mixture (3 components):")
150 for i, (w, m) in enumerate(zip(weights, means)):
151 print(f" Component {i + 1}: weight={w:.1f}, mean={m}")
152
153 # Moment match to single Gaussian - takes (weights, means, covariances)
154 mean, cov = moment_match(weights, means, covariances)
155
156 print("\nMoment-matched single Gaussian:")
157 print(f" Mean: ({mean[0]:.3f}, {mean[1]:.3f})")
158 print(f" Covariance:\n{cov}")
159
160 # The mean should be the weighted average
161 weighted_mean = sum(w * m for w, m in zip(weights, means))
162 print(
163 f"\nVerification - weighted mean: ({weighted_mean[0]:.3f}, "
164 f"{weighted_mean[1]:.3f})"
165 )
166
167
168def demo_mixture_merging():
169 """Demonstrate merging Gaussian components."""
170 print("\n" + "=" * 70)
171 print("Mixture Merging Demo")
172 print("=" * 70)
173
174 # Two nearby components
175 comp1 = GaussianComponent(0.4, np.array([0.0, 0.0]), np.eye(2) * 1.0)
176 comp2 = GaussianComponent(0.3, np.array([0.5, 0.5]), np.eye(2) * 1.0)
177
178 # Two far apart components
179 comp3 = GaussianComponent(0.3, np.array([5.0, 5.0]), np.eye(2) * 1.0)
180
181 print("\nThree components:")
182 print(f" 1: mean={comp1.mean}, weight={comp1.weight}")
183 print(f" 2: mean={comp2.mean}, weight={comp2.weight}")
184 print(f" 3: mean={comp3.mean}, weight={comp3.weight}")
185
186 # Merge costs
187 cost_12 = runnalls_merge_cost(comp1, comp2)
188 cost_13 = runnalls_merge_cost(comp1, comp3)
189 cost_23 = runnalls_merge_cost(comp2, comp3)
190
191 print("\nRunnalls merge costs (lower = better candidates for merging):")
192 print(f" Cost(1,2): {cost_12:.4f}")
193 print(f" Cost(1,3): {cost_13:.4f}")
194 print(f" Cost(2,3): {cost_23:.4f}")
195
196 # Merge the closest pair
197 merge_result = merge_gaussians(comp1, comp2)
198 merged = merge_result.component
199
200 print("\nMerged component (1+2):")
201 print(f" Weight: {merged.weight:.2f}")
202 print(f" Mean: ({merged.mean[0]:.3f}, {merged.mean[1]:.3f})")
203 print(f" Merge cost: {merge_result.cost:.4f}")
204
205
206def demo_mixture_reduction():
207 """Demonstrate mixture reduction algorithms."""
208 print("\n" + "=" * 70)
209 print("Mixture Reduction Demo")
210 print("=" * 70)
211
212 np.random.seed(42)
213
214 # Create a large mixture (e.g., from PHD filter output)
215 n_components = 20
216 components = []
217
218 # Cluster 1: around (0, 0)
219 for _ in range(8):
220 mean = np.array([0, 0]) + np.random.randn(2) * 0.5
221 cov = np.eye(2) * (0.3 + np.random.rand() * 0.2)
222 weight = 0.3 + np.random.rand() * 0.4
223 components.append(GaussianComponent(weight, mean, cov))
224
225 # Cluster 2: around (5, 3)
226 for _ in range(7):
227 mean = np.array([5, 3]) + np.random.randn(2) * 0.5
228 cov = np.eye(2) * (0.3 + np.random.rand() * 0.2)
229 weight = 0.2 + np.random.rand() * 0.3
230 components.append(GaussianComponent(weight, mean, cov))
231
232 # Scattered components
233 for _ in range(5):
234 mean = np.random.rand(2) * 10
235 cov = np.eye(2) * 0.5
236 weight = 0.05 + np.random.rand() * 0.1
237 components.append(GaussianComponent(weight, mean, cov))
238
239 print(f"\nOriginal mixture: {len(components)} components")
240 print(f"Total weight: {sum(c.weight for c in components):.2f}")
241
242 # Prune low-weight components
243 pruned = prune_mixture(components, weight_threshold=0.1)
244 print(f"\nAfter pruning (weight_threshold=0.1): {len(pruned)} components")
245
246 # Reduce using Runnalls' algorithm
247 n_target = 5
248 result_runnalls = reduce_mixture_runnalls(components, n_target)
249 print(f"\nRunnalls reduction to {n_target} components:")
250 print(f" Final components: {result_runnalls.n_reduced}")
251 print(f" Total merge cost: {result_runnalls.total_cost:.4f}")
252 for i, c in enumerate(result_runnalls.components):
253 print(
254 f" {i + 1}: weight={c.weight:.3f}, "
255 f"mean=({c.mean[0]:.2f}, {c.mean[1]:.2f})"
256 )
257
258 # Reduce using West's algorithm
259 result_west = reduce_mixture_west(components, n_target)
260 print(f"\nWest reduction to {n_target} components:")
261 print(f" Final components: {result_west.n_reduced}")
262 print(f" Total merge cost: {result_west.total_cost:.4f}")
263
264
265def demo_kmeans():
266 """Demonstrate K-means clustering."""
267 print("\n" + "=" * 70)
268 print("K-Means Clustering Demo")
269 print("=" * 70)
270
271 np.random.seed(42)
272
273 # Generate clustered data
274 n_per_cluster = 50
275 centers_true = np.array(
276 [
277 [0, 0],
278 [5, 5],
279 [0, 5],
280 ]
281 )
282
283 data = []
284 for center in centers_true:
285 cluster = center + np.random.randn(n_per_cluster, 2) * 0.8
286 data.append(cluster)
287 data = np.vstack(data)
288
289 print(f"\nGenerated {len(data)} points in 3 clusters")
290 print(f"True centers:\n{centers_true}")
291
292 # K-means clustering
293 result = kmeans(data, n_clusters=3, max_iter=100, rng=_rng())
294
295 print(f"\nK-means result:")
296 print(f" Iterations: {result.n_iter}")
297 print(f" Inertia (within-cluster sum of squares): {result.inertia:.2f}")
298 print(f" Found centers:\n{result.centers}")
299
300 # Cluster sizes
301 unique, counts = np.unique(result.labels, return_counts=True)
302 print("\n Cluster sizes:", dict(zip(unique, counts)))
303
304 # Plot K-means result
305 if SHOW_PLOTS:
306 fig = make_subplots(
307 rows=1,
308 cols=2,
309 subplot_titles=["Ground Truth Clusters", "K-means Clustering Result"],
310 )
311
312 # True clusters
313 colors = ["blue", "green", "orange"]
314 for i, center in enumerate(centers_true):
315 mask = np.arange(len(data)) // n_per_cluster == i
316 fig.add_trace(
317 go.Scatter(
318 x=data[mask, 0],
319 y=data[mask, 1],
320 mode="markers",
321 marker=dict(color=colors[i], size=6, opacity=0.6),
322 name=f"True cluster {i}",
323 ),
324 row=1,
325 col=1,
326 )
327
328 fig.add_trace(
329 go.Scatter(
330 x=centers_true[:, 0],
331 y=centers_true[:, 1],
332 mode="markers",
333 marker=dict(color="black", size=15, symbol="x", line=dict(width=3)),
334 name="True centers",
335 ),
336 row=1,
337 col=1,
338 )
339
340 # K-means result
341 colors_km = ["red", "green", "blue"]
342 for i in range(3):
343 mask = result.labels == i
344 fig.add_trace(
345 go.Scatter(
346 x=data[mask, 0],
347 y=data[mask, 1],
348 mode="markers",
349 marker=dict(color=colors_km[i], size=6, opacity=0.6),
350 name=f"Cluster {i}",
351 showlegend=True,
352 ),
353 row=1,
354 col=2,
355 )
356
357 fig.add_trace(
358 go.Scatter(
359 x=result.centers[:, 0],
360 y=result.centers[:, 1],
361 mode="markers",
362 marker=dict(color="black", size=15, symbol="x", line=dict(width=3)),
363 name="K-means centers",
364 ),
365 row=1,
366 col=2,
367 )
368
369 fig.update_xaxes(title_text="x")
370 fig.update_yaxes(title_text="y")
371 fig.update_layout(height=500, width=1000, showlegend=True)
372 fig.write_html(
373 str(OUTPUT_DIR / "gaussian_kmeans.html"),
374 include_plotlyjs="cdn",
375 div_id="gaussian_kmeans",
376 )
377 print("\n [Plot saved to gaussian_kmeans.html]")
378
379
380def demo_kmeans_plusplus():
381 """Demonstrate K-means++ initialization."""
382 print("\n" + "=" * 70)
383 print("K-Means++ Initialization Demo")
384 print("=" * 70)
385
386 np.random.seed(42)
387
388 # Generate data with 4 well-separated clusters
389 data = np.vstack(
390 [
391 np.random.randn(30, 2) + [0, 0],
392 np.random.randn(30, 2) + [10, 0],
393 np.random.randn(30, 2) + [0, 10],
394 np.random.randn(30, 2) + [10, 10],
395 ]
396 )
397
398 n_clusters = 4
399
400 # Random initialization
401 random_centers = data[np.random.choice(len(data), n_clusters, replace=False)]
402
403 # K-means++ initialization
404 plusplus_centers = kmeans_plusplus_init(data, n_clusters)
405
406 print(f"\nComparing initialization methods for n_clusters={n_clusters}:")
407
408 # Run K-means with each initialization
409 # For random, run multiple times (use init='random' for random init)
410 results_random = []
411 for _ in range(10):
412 idx = np.random.choice(len(data), n_clusters, replace=False)
413 result = kmeans(
414 data, n_clusters=n_clusters, init=data[idx], n_init=1, rng=_rng()
415 )
416 results_random.append(result.inertia)
417
418 result_plusplus = kmeans(
419 data, n_clusters=n_clusters, init=plusplus_centers, n_init=1, rng=_rng()
420 )
421
422 print(f"\n Random initialization (10 runs):")
423 print(f" Mean inertia: {np.mean(results_random):.2f}")
424 print(f" Std inertia: {np.std(results_random):.2f}")
425 print(f" Best inertia: {np.min(results_random):.2f}")
426
427 print(f"\n K-means++ initialization:")
428 print(f" Inertia: {result_plusplus.inertia:.2f}")
429
430 print("\nNote: K-means++ typically provides better, more consistent results.")
431
432
433def demo_elbow_method():
434 """Demonstrate elbow method for K selection."""
435 print("\n" + "=" * 70)
436 print("Elbow Method Demo")
437 print("=" * 70)
438
439 np.random.seed(42)
440
441 # Generate data with 3 true clusters
442 data = np.vstack(
443 [
444 np.random.randn(40, 2) + [0, 0],
445 np.random.randn(40, 2) + [4, 4],
446 np.random.randn(40, 2) + [8, 0],
447 ]
448 )
449
450 print(f"\nData: 120 points from 3 true clusters")
451 print("\nInertia for different K values:")
452 print("-" * 40)
453
454 elbow_result = kmeans_elbow(data, k_range=range(1, 8), rng=_rng())
455 k_values = elbow_result["k_values"]
456 inertias = elbow_result["inertias"]
457
458 for k, inertia in zip(k_values, inertias):
459 bar = "#" * int(inertia / max(inertias) * 30)
460 print(f" K={k}: {inertia:>8.1f} {bar}")
461
462 print("\nThe 'elbow' should appear around K=3")
463 print("(where adding more clusters gives diminishing returns)")
464
465 # Plot elbow method
466 if SHOW_PLOTS:
467 fig = go.Figure()
468
469 fig.add_trace(
470 go.Scatter(
471 x=list(k_values),
472 y=inertias,
473 mode="lines+markers",
474 line=dict(color="blue", width=2),
475 marker=dict(size=10),
476 name="Inertia",
477 )
478 )
479
480 fig.add_vline(
481 x=3, line_dash="dash", line_color="red", annotation_text="True K=3"
482 )
483
484 fig.update_layout(
485 title="Elbow Method for K Selection",
486 xaxis_title="Number of Clusters (K)",
487 yaxis_title="Inertia (Within-cluster Sum of Squares)",
488 height=500,
489 width=700,
490 showlegend=True,
491 )
492 fig.write_html(
493 str(OUTPUT_DIR / "gaussian_elbow.html"),
494 include_plotlyjs="cdn",
495 div_id="gaussian_elbow",
496 )
497 print("\n [Plot saved to gaussian_elbow.html]")
498
499
500def demo_dbscan():
501 """Demonstrate DBSCAN clustering."""
502 print("\n" + "=" * 70)
503 print("DBSCAN Clustering Demo")
504 print("=" * 70)
505
506 np.random.seed(42)
507
508 # Generate data: two dense clusters + noise
509 cluster1 = np.random.randn(50, 2) * 0.5 + [0, 0]
510 cluster2 = np.random.randn(50, 2) * 0.5 + [4, 4]
511 noise = np.random.uniform(-2, 8, (20, 2)) # Scattered noise points
512
513 data = np.vstack([cluster1, cluster2, noise])
514
515 print(f"\nData: 100 cluster points + 20 noise points")
516
517 # DBSCAN clustering
518 result = dbscan(data, eps=0.8, min_samples=5)
519
520 print(f"\nDBSCAN result (eps=0.8, min_samples=5):")
521 print(f" Clusters found: {result.n_clusters}")
522 print(f" Core sample indices: {len(result.core_sample_indices)}")
523 print(f" Noise points: {result.n_noise}")
524
525 # Cluster sizes
526 unique_labels = np.unique(result.labels)
527 for label in unique_labels:
528 count = np.sum(result.labels == label)
529 if label == -1:
530 print(f" Noise points: {count}")
531 else:
532 print(f" Cluster {label}: {count} points")
533
534 print("\nNote: DBSCAN identifies noise points (label=-1)")
535 print("and doesn't require specifying the number of clusters.")
536
537 # Plot DBSCAN result
538 if SHOW_PLOTS:
539 fig = make_subplots(
540 rows=1,
541 cols=2,
542 subplot_titles=[
543 "Ground Truth",
544 f"DBSCAN Result ({result.n_clusters} clusters)",
545 ],
546 )
547
548 # Ground truth
549 fig.add_trace(
550 go.Scatter(
551 x=cluster1[:, 0],
552 y=cluster1[:, 1],
553 mode="markers",
554 marker=dict(color="blue", size=8, opacity=0.6),
555 name="Cluster 1",
556 ),
557 row=1,
558 col=1,
559 )
560
561 fig.add_trace(
562 go.Scatter(
563 x=cluster2[:, 0],
564 y=cluster2[:, 1],
565 mode="markers",
566 marker=dict(color="green", size=8, opacity=0.6),
567 name="Cluster 2",
568 ),
569 row=1,
570 col=1,
571 )
572
573 fig.add_trace(
574 go.Scatter(
575 x=noise[:, 0],
576 y=noise[:, 1],
577 mode="markers",
578 marker=dict(color="red", size=8, opacity=0.6),
579 name="Noise",
580 ),
581 row=1,
582 col=1,
583 )
584
585 # DBSCAN result
586 colors = ["blue", "green", "purple", "orange"]
587 for label in unique_labels:
588 mask = result.labels == label
589 if label == -1:
590 fig.add_trace(
591 go.Scatter(
592 x=data[mask, 0],
593 y=data[mask, 1],
594 mode="markers",
595 marker=dict(color="red", size=8, opacity=0.6, symbol="x"),
596 name="Noise",
597 showlegend=True,
598 ),
599 row=1,
600 col=2,
601 )
602 else:
603 fig.add_trace(
604 go.Scatter(
605 x=data[mask, 0],
606 y=data[mask, 1],
607 mode="markers",
608 marker=dict(
609 color=colors[label % len(colors)], size=8, opacity=0.6
610 ),
611 name=f"Cluster {label}",
612 showlegend=True,
613 ),
614 row=1,
615 col=2,
616 )
617
618 # Mark core samples
619 core_mask = np.zeros(len(data), dtype=bool)
620 core_mask[result.core_sample_indices] = True
621 fig.add_trace(
622 go.Scatter(
623 x=data[core_mask, 0],
624 y=data[core_mask, 1],
625 mode="markers",
626 marker=dict(
627 color="rgba(0,0,0,0)", size=15, line=dict(color="black", width=1)
628 ),
629 name="Core samples",
630 showlegend=True,
631 ),
632 row=1,
633 col=2,
634 )
635
636 fig.update_xaxes(title_text="x")
637 fig.update_yaxes(title_text="y")
638 fig.update_layout(height=500, width=1000, showlegend=True)
639 fig.write_html(
640 str(OUTPUT_DIR / "gaussian_dbscan.html"),
641 include_plotlyjs="cdn",
642 div_id="gaussian_dbscan",
643 )
644 print("\n [Plot saved to gaussian_dbscan.html]")
645
646
647def demo_hierarchical():
648 """Demonstrate hierarchical clustering."""
649 print("\n" + "=" * 70)
650 print("Hierarchical Clustering Demo")
651 print("=" * 70)
652
653 np.random.seed(42)
654
655 # Generate small dataset for visualization
656 data = np.array(
657 [
658 [0, 0],
659 [0.5, 0.5],
660 [1, 0], # Cluster A
661 [5, 5],
662 [5.5, 5.5],
663 [5, 6], # Cluster B
664 [2.5, 2.5], # Between clusters
665 ]
666 )
667
668 print(f"\nData points:\n{data}")
669
670 # Compute distance matrix
671 dist_matrix = compute_distance_matrix(data)
672 print(f"\nDistance matrix:\n{np.round(dist_matrix, 2)}")
673
674 # Agglomerative clustering
675 result = agglomerative_clustering(data, linkage="average")
676
677 print("\nHierarchical clustering (average linkage):")
678 print(f" Labels: {result.labels}")
679 print(f" Number of clusters: {result.n_clusters}")
680
681 # Cut at different thresholds
682 n_samples = len(data)
683 for threshold in [1.0, 3.0, 5.0]:
684 labels = cut_dendrogram(
685 result.linkage_matrix, n_samples, distance_threshold=threshold
686 )
687 n_clusters = len(set(labels))
688 print(
689 f" Threshold {threshold:.1f}: {n_clusters} clusters, labels={list(labels)}"
690 )
691
692
693def demo_tracking_application():
694 """Demonstrate mixture reduction in tracking context."""
695 print("\n" + "=" * 70)
696 print("Tracking Application Demo")
697 print("=" * 70)
698
699 np.random.seed(42)
700
701 # Simulated PHD filter output: mixture representing target density
702 # After several updates, the mixture can have many components
703
704 # True targets at these locations
705 true_targets = np.array(
706 [
707 [10.0, 20.0],
708 [30.0, 40.0],
709 [25.0, 25.0],
710 ]
711 )
712
713 print(f"\nTrue target positions: {len(true_targets)} targets")
714 for i, t in enumerate(true_targets):
715 print(f" Target {i + 1}: ({t[0]:.1f}, {t[1]:.1f})")
716
717 # Create mixture with components clustered around true targets
718 # Plus some spurious components (false alarms, etc.)
719 components = []
720
721 # Components near true targets (higher weight)
722 for target in true_targets:
723 for _ in range(4):
724 mean = target + np.random.randn(2) * 1.0
725 cov = np.eye(2) * (0.5 + np.random.rand() * 0.5)
726 weight = 0.6 + np.random.rand() * 0.4
727 components.append(GaussianComponent(weight, mean, cov))
728
729 # Spurious components (lower weight)
730 for _ in range(8):
731 mean = np.random.uniform(5, 45, 2)
732 cov = np.eye(2) * 2.0
733 weight = 0.05 + np.random.rand() * 0.1
734 components.append(GaussianComponent(weight, mean, cov))
735
736 print(f"\nPHD mixture: {len(components)} components")
737 total_weight = sum(c.weight for c in components)
738 print(f" Total weight (expected target count): {total_weight:.2f}")
739
740 # Reduce to extract target estimates
741 n_expected = int(round(total_weight))
742 reduced = reduce_mixture_runnalls(components, n_expected)
743
744 print(f"\nAfter reduction to {n_expected} components:")
745 for i, c in enumerate(reduced.components):
746 # Find closest true target
747 dists = [np.linalg.norm(c.mean - t) for t in true_targets]
748 closest = np.argmin(dists)
749 error = min(dists)
750 print(
751 f" Estimate {i + 1}: ({c.mean[0]:.1f}, {c.mean[1]:.1f}), "
752 f"weight={c.weight:.2f}, error to target {closest + 1}={error:.2f}"
753 )
754
755
756def main():
757 """Run all demonstrations."""
758 # Seed once for the whole run. Individual demos below reseed, but
759 # not all of them do, and the figures are committed under
760 # docs/_static -- an unseeded draw makes every rebuild produce a
761 # different file and a spurious diff.
762 np.random.seed(42)
763 print("\n" + "#" * 70)
764 print("# PyTCL Gaussian Mixtures and Clustering Example")
765 print("#" * 70)
766
767 # Gaussian mixture operations
768 demo_gaussian_components()
769 demo_moment_matching()
770 demo_mixture_merging()
771 demo_mixture_reduction()
772
773 # Clustering algorithms
774 demo_kmeans()
775 demo_kmeans_plusplus()
776 demo_elbow_method()
777 demo_dbscan()
778 demo_hierarchical()
779
780 # Application
781 demo_tracking_application()
782
783 print("\n" + "=" * 70)
784 print("Example complete!")
785 if SHOW_PLOTS:
786 print(
787 "Plots saved: gaussian_kmeans.html, gaussian_elbow.html, gaussian_dbscan.html"
788 )
789 print("=" * 70)
790
791
792if __name__ == "__main__":
793 main()
Running the Example
python examples/gaussian_mixtures.py
See Also
Spatial Data Structures - KD-trees for clustering
Advanced Filters Comparison - Gaussian sum filters