Skip to content

Commit 12cbc9e

Browse files
authored
feat: add piecewise linear cpd (#91)
1 parent a12b215 commit 12cbc9e

File tree

18 files changed

+129
-629
lines changed

18 files changed

+129
-629
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,6 @@ docs/build/*
8383
ehthumbs.db
8484
Thumbs.db
8585
site/
86+
ruptures/utils/_utils/convert_path_matrix.c
87+
ruptures/detection/_detection/continuous_linear_cpd.c
88+
ruptures/detection/_detection/ekcpd.c
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Continuous linear change (CostCLinear)
2+
3+
::: ruptures.costs.costclinear.CostCLinear
4+
rendering:
5+
show_root_heading: true

docs/code-reference/detection/greedyar-reference.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/code-reference/detection/greedylinear-reference.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/code-reference/detection/omp-reference.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/code-reference/detection/ompk-reference.md

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Continuous linear change (`CostCLinear`)
2+
3+
## Description
4+
5+
For a given set of indexes (also called knots) $t_k$ ($k=1,\dots,K$), a linear spline $f$ is such that:
6+
7+
1. $f$ is affine on each interval $t_k..t_{k+1}$, i.e. $f(t)=\alpha_k (t-t_k) + \beta_k$ ($\alpha_k, \beta_k \in \mathbb{R}^d$) for all $t=t_k,t_k+1,\dots,t_{k+1}-1$;
8+
2. $f$ is continuous.
9+
10+
The cost function [`CostCLinear`][ruptures.costs.costclinear.CostCLinear] measures the error when approximating the signal with a linear spline.
11+
Formally, it is defined for $0<a<b\leq T$ by
12+
13+
$$
14+
c(y_{a..b}) := \sum_{t=a}^{b-1} \left\lVert y_t - y_{a-1} - \frac{t-a+1}{b-a}(y_{b-1}-y_{a-1}) \right\rVert^2
15+
$$
16+
17+
and $c(y_{0..b}):=c(y_{1..b})$ (by convention).
18+
19+
## Usage
20+
21+
Start with the usual imports and create a signal with piecewise linear trends.
22+
23+
```python
24+
import numpy as np
25+
import matplotlib.pylab as plt
26+
import ruptures as rpt
27+
28+
# creation of data
29+
n_samples, n_dims = 500, 3 # number of samples, dimension
30+
n_bkps, sigma = 3, 5 # number of change points, noise standard deviation
31+
signal, bkps = rpt.pw_constant(n_samples, n_dims, n_bkps, noise_std=sigma)
32+
signal = np.cumsum(signal, axis=1)
33+
```
34+
35+
Then create a [`CostCLinear`][ruptures.costs.costclinear.CostCLinear] instance and print the cost of the sub-signal `signal[50:150]`.
36+
37+
```python
38+
c = rpt.costs.CostCLinear().fit(signal)
39+
print(c.error(50, 150))
40+
```
41+
42+
You can also compute the sum of costs for a given list of change points.
43+
44+
```python
45+
print(c.sum_of_costs(bkps))
46+
print(c.sum_of_costs([10, 100, 200, 250, n]))
47+
```
48+
49+
In order to use this cost class in a change point detection algorithm (inheriting from [`BaseEstimator`][ruptures.base.BaseEstimator]), either pass a [`CostCLinear`][ruptures.costs.costclinear.CostCLinear] instance (through the argument `custom_cost`) or set `model="clinear"`.
50+
51+
```python
52+
c = rpt.costs.CostCLinear()
53+
algo = rpt.Dynp(custom_cost=c)
54+
# is equivalent to
55+
algo = rpt.Dynp(model="clinear")
56+
```

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ nav:
5555
- 'CostRbf': user-guide/costs/costrbf.md
5656
- 'CostCosine': user-guide/costs/costcosine.md
5757
- 'CostLinear': user-guide/costs/costlinear.md
58+
- 'CostCLinear': user-guide/costs/costclinear.md
5859
- 'CostRank': user-guide/costs/costrank.md
5960
- 'CostMl': user-guide/costs/costml.md
6061
- 'CostAR': user-guide/costs/costautoregressive.md
@@ -99,6 +100,7 @@ nav:
99100
- 'CostRbf': code-reference/costs/costrbf-reference.md
100101
- 'CostCosine': code-reference/costs/costcosine-reference.md
101102
- 'CostLinear': code-reference/costs/costlinear-reference.md
103+
- 'CostCLinear': code-reference/costs/costclinear-reference.md
102104
- 'CostRank': code-reference/costs/costrank-reference.md
103105
- 'CostMl': code-reference/costs/costml-reference.md
104106
- 'CostAR': code-reference/costs/costautoregressive-reference.md

ruptures/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
BottomUp,
1111
Dynp,
1212
KernelCPD,
13-
Omp,
14-
OmpK,
1513
Pelt,
1614
Window,
17-
GreedyAR,
18-
GreedyLinear,
1915
)
2016
from .show import display

ruptures/costs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .costl1 import CostL1
44
from .costl2 import CostL2
55
from .costlinear import CostLinear
6+
from .costclinear import CostCLinear
67
from .costrbf import CostRbf
78
from .costnormal import CostNormal
89
from .costautoregressive import CostAR

0 commit comments

Comments
 (0)