Note
Go to the end to download the full example code or to run this example in your browser via Binder.
The merge hierarchy: segmentogram and persistence#
Bottom-up segmentation produces a whole hierarchy of segmentations in one run. The segmentogram shows every level; change points that survive many merges are the important ones. The cost curve and its relative reduction rate (Bankó, 2015) tell where adding segments stops paying off.
import matplotlib.pyplot as plt
from mvtsa import segmentation as sg
from mvtsa import viz
from mvtsa.datasets import make_correlation_switching
X, cps, _ = make_correlation_switching(
n_segments=5, length=120, random_state=7
)
One run, all levels#
bu = sg.BottomUp("pca", initial_size=10, cost_params={"n_components": 1})
tree = bu.full_tree(X)
print("levels:", tree.n_levels, "| 5 segments:", tree.cut(5), "| true:", cps)
pers = tree.persistence()
print(
"most persistent change points:",
sorted(pers, key=pers.get, reverse=True)[:4],
)
viz.plot_segmentogram(tree, max_segments=12)

levels: 60 | 5 segments: [120 240 360 480] | true: [120 240 360 480]
most persistent change points: [240, 480, 120, 360]
<Figure size 740x340 with 2 Axes>
Cost curve and relative reduction rate#
curve = {c: v for c, v in tree.cost_curve().items() if c <= 12}
rr = sg.relative_reduction(curve)
print({c: round(v, 3) for c, v in rr.items()})
viz.plot_cost_curve(curve, selected=5)
plt.show()

{2: 0.14, 3: 0.415, 4: 0.493, 5: 0.942, 6: 0.004, 7: 0.008, 8: 0.008, 9: 0.008, 10: 0.007, 11: 0.005, 12: 0.007}
Total running time of the script: (0 minutes 0.203 seconds)