Skip to main content

Random Forests for Change Point Detection

Project description

Random Forests for Change Point Detection

Change point detection aims to identify structural breaks in the probability distribution of a time series. Existing methods either assume a parametric model for within-segment distributions or are based on ranks or distances and thus fail in scenarios with a reasonably large dimensionality.

changeforest implements a classifier-based algorithm that consistently estimates change points without any parametric assumptions, even in high-dimensional scenarios. It uses the out-of-bag probability predictions of a random forest to construct a classifier log-likelihood ratio that gets optimized using a computationally feasible two-step method.

See [1] for details.

Installation

To install from conda-forge (recommended), run

conda install -c conda-forge changeforest

To install from PyPI, run

pip install changeforest

Example

In the following example, we perform random forest-based change point detection on a simulated dataset with n=600 observations and covariance shifts at t=200, 400.

In [1]: import numpy as np
   ...: 
   ...: Sigma = np.full((5, 5), 0.7)
   ...: np.fill_diagonal(Sigma, 1)
   ...: 
   ...: rng = np.random.default_rng(12)
   ...: X = np.concatenate(
   ...:     (
   ...:         rng.normal(0, 1, (200, 5)),
   ...:         rng.multivariate_normal(np.zeros(5), Sigma, 200, method="cholesky"),
   ...:         rng.normal(0, 1, (200, 5)),
   ...:     ),
   ...:     axis=0,
   ...: )

The simulated dataset X coincides with the change in covariance (CIC) setup described in [1]. Observations in the first and last segments are independently drawn from a standard multivariate Gaussian distribution. Observations in the second segment are i.i.d. normal with mean zero and unit variance, but with a covariance of ρ = 0.7 between coordinates. This is a challenging scenario.

In [2]: from changeforest import changeforest
   ...: 
   ...: result = changeforest(X, "random_forest", "bs")
   ...: result
Out[2]: 
                    best_split max_gain p_value
(0, 600]                   400   14.814   0.005
 ¦--(0, 400]               200   59.314   0.005
 ¦   ¦--(0, 200]             6    -1.95    0.67
 ¦   °--(200, 400]         393   -8.668    0.81
 °--(400, 600]             412   -9.047    0.66

In [3]: result.split_points()
Out[3]: [200, 400]

changeforest correctly identifies the change points at t=200 and t=400. The changeforest function returns a BinarySegmentationResult. We use its plot method to investigate the gain curves maximized by the change point estimates:

In [4]: result.plot().show()

Change point estimates are marked in red.

For method="random_forest" and method="knn", the changeforest algorithm uses a two-step approach to find an optimizer of the gain. This fits a classifier for three split candidates at the segment's 1/4, 1/2 and 3/4 quantiles, computes approximate gain curves using the resulting classifier log-likelihood ratios and selects the overall optimizer as a second guess. We can investigate the gain curves from the optimizer using the plot method of OptimizerResult. The initial guesses are marked in blue.

In [5]: result.optimizer_result.plot().show()

One can observe that the approximate gain curves are piecewise linear, with maxima around the true underlying change points.

The BinarySegmentationResult returned by changeforest is a tree-like object with attributes start, stop, best_split, max_gain, p_value, is_significant, optimizer_result, model_selection_result, left, right and segments. These can be interesting to investigate the output of the algorithm further.

The changeforest algorithm can be tuned with hyperparameters. See here for their descriptions and default values. In Python, the parameters can be specified with the Control class, which can be passed to changeforest. The following will build random forests with 50 trees:

In [6]: from changeforest import Control
   ...: changeforest(X, "random_forest", "bs", Control(random_forest_n_estimators=50))
Out[6]: 
                    best_split max_gain p_value
(0, 600]                   416    7.463    0.01
 ¦--(0, 416]               200   43.935   0.005
 ¦   ¦--(0, 200]           193  -14.993   0.945
 ¦   °--(200, 416]         217    -9.13   0.085
 °--(416, 600]             591   -12.07       1 

The changeforest algorithm still detects change points at t=200, but is slightly off with t=416.

Due to the nature of the change, method="change_in_mean" is unable to detect any change points at all:

In [7]: changeforest(X, "change_in_mean", "bs")
Out[7]: 
          best_split max_gain p_value
(0, 600]         589    8.625  

References

[1] M. Londschien, P. Bühlmann and S. Kovács (2023). "Random Forests for Change Point Detection" Journal of Machine Learning Research

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

changeforest-1.0.1.tar.gz (366.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

changeforest-1.0.1-cp311-none-win_amd64.whl (321.5 kB view details)

Uploaded CPython 3.11Windows x86-64

changeforest-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

changeforest-1.0.1-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (917.5 kB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)macOS 10.9+ x86-64macOS 11.0+ ARM64

changeforest-1.0.1-cp311-cp311-macosx_10_7_x86_64.whl (461.5 kB view details)

Uploaded CPython 3.11macOS 10.7+ x86-64

changeforest-1.0.1-cp310-none-win_amd64.whl (321.5 kB view details)

Uploaded CPython 3.10Windows x86-64

changeforest-1.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

changeforest-1.0.1-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (917.5 kB view details)

Uploaded CPython 3.10macOS 10.9+ universal2 (ARM64, x86-64)macOS 10.9+ x86-64macOS 11.0+ ARM64

changeforest-1.0.1-cp310-cp310-macosx_10_7_x86_64.whl (461.5 kB view details)

Uploaded CPython 3.10macOS 10.7+ x86-64

changeforest-1.0.1-cp39-none-win_amd64.whl (321.5 kB view details)

Uploaded CPython 3.9Windows x86-64

changeforest-1.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

changeforest-1.0.1-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (917.4 kB view details)

Uploaded CPython 3.9macOS 10.9+ universal2 (ARM64, x86-64)macOS 10.9+ x86-64macOS 11.0+ ARM64

changeforest-1.0.1-cp39-cp39-macosx_10_7_x86_64.whl (461.5 kB view details)

Uploaded CPython 3.9macOS 10.7+ x86-64

changeforest-1.0.1-cp38-none-win_amd64.whl (321.4 kB view details)

Uploaded CPython 3.8Windows x86-64

changeforest-1.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

changeforest-1.0.1-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (917.3 kB view details)

Uploaded CPython 3.8macOS 10.9+ universal2 (ARM64, x86-64)macOS 10.9+ x86-64macOS 11.0+ ARM64

changeforest-1.0.1-cp38-cp38-macosx_10_7_x86_64.whl (461.4 kB view details)

Uploaded CPython 3.8macOS 10.7+ x86-64

changeforest-1.0.1-cp37-none-win_amd64.whl (321.5 kB view details)

Uploaded CPython 3.7Windows x86-64

changeforest-1.0.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

changeforest-1.0.1-cp37-cp37m-macosx_10_7_x86_64.whl (461.6 kB view details)

Uploaded CPython 3.7mmacOS 10.7+ x86-64

File details

Details for the file changeforest-1.0.1.tar.gz.

File metadata

  • Download URL: changeforest-1.0.1.tar.gz
  • Upload date:
  • Size: 366.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for changeforest-1.0.1.tar.gz
Algorithm Hash digest
SHA256 8f3ca06331dfada5a7d2a40def60e2214ad6866108e713b4cbaa89b4d626f49e
MD5 8c4ee15dae453e07803688897c3c14ce
BLAKE2b-256 75800dd4fb36f0e7e390e91ad0d5bbe009021ed47fad6cca1659c4020ddc88db

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 62d6bc12f2c6412f2ec24b5ffab3b9ef357a2186c961a0a418daead53b65f0bd
MD5 b8d733c66697d38b14459a126bcaf685
BLAKE2b-256 296e7b16329c53146552447b669653806ff44bc7a308f570ef281a0d23e8dd48

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8cb40308bd534e11b3fecfda5969a7e90d406b623a8bd93a46351d98e0142be1
MD5 15aa26089a3f8708cc144d92161dd58c
BLAKE2b-256 3cd38375a41ac80f604d153b1b57259db7c365a64895fb57911e74bb56b9a80a

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 581ae298c3db25532e146bff7bf6f9f91ef3fca56649ead00b912bbd3f6bd721
MD5 b530f778a99e1cb8de560aa0e0ebc24f
BLAKE2b-256 a54a9776dc217f54d8c7eda1fc5c5186f3530fd5f53fa591f457a0f3eab20194

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp311-cp311-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 a699855c5c28873efea70d2e29bba43236efe02b2c7080396e7188438b018360
MD5 ddb7c8b73ff4feaf2d9fd27cc3ed40bd
BLAKE2b-256 8b039a8548ebce295bd055f0e601de4e33814090a1fe16b1bb9ac90d2200acd7

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 5104710366555e9d5fabb792fc5c00edc3732b8a48295534e2a06ffe8444a41b
MD5 48b42c4b2bb6375706a8365fe3fb9e21
BLAKE2b-256 a06b8efbbf11083a69c1d35e280a62382b0bd6bcdc81c251caf1e475a27d9c67

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2baf008f645d98896981df735e7b7f39c647b814f298f7a91093fb108a4050ec
MD5 22851bc17052a2af2753e55c838bd9af
BLAKE2b-256 a0e2d6336f095529c139ac19d63b59baeafcccceab120d71ce27beb12caf796b

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 88ee6bc828a9ad6b2348aaeb52be259d54879f21d2a5639f0854ee242c57abec
MD5 871fbfb8db21be1ecf1ec87ca1366bc4
BLAKE2b-256 975e4d8f90e6e043a36a0421b21c2a7b7149dd1eac9e685d7c160bbce69f9c19

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp310-cp310-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 0a607052ae4dd432fc90e6b8569f9b9b21875d5a677a280f07790eb07475d987
MD5 b8d112b437cc1f30f82081c803f18aad
BLAKE2b-256 a0a0b8e19e9a656d09f56546b37a488fc50e6da8967223dc158d94722c0e8a62

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp39-none-win_amd64.whl.

File metadata

  • Download URL: changeforest-1.0.1-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 321.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for changeforest-1.0.1-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 3d3032d6cacaaceba70ea24b7fd0a4eae9fef224555842e4e588dd824a512e95
MD5 a0eee425d963b9f2ec843be43f6b5eea
BLAKE2b-256 c773f8881edd0cf8c620f182bb057ba87fb4025406d50d0b81ea098f6d642c43

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f05922b7de3a4033257ecc00ae9a5ad25fdfce4e1525e8fd8e8c8bca8b1df807
MD5 ae2dd34eadade414cf6ea5884b40dde6
BLAKE2b-256 396ef0c2b7c89d799bdbd964f9663223e77347d4fd33360b605f56c579f661d9

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 ea7c2ebb45455f5530e9405f23c3d4341d40aed2a646df2686a58a4a3ef91b14
MD5 d1467ae2505db406b00c077bb9bcb5de
BLAKE2b-256 fd084340e6189ef93e9610f4c5a9ec83033e900b1fdd264d37d075201e8d27ba

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp39-cp39-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 0bdd9ddf129ec7ed01512d023604fc61536a31de04556af3cb8baa956db5a0c8
MD5 1e06cf7cd076689ba965fa113b150cee
BLAKE2b-256 30e83a1370b44919c5857ac07bdcbf9fe0d236dc5fb1e2313f8e2c095c4c3349

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp38-none-win_amd64.whl.

File metadata

  • Download URL: changeforest-1.0.1-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 321.4 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for changeforest-1.0.1-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 de58a52840bcb293733d09afde92b8da26eb9cd78013a2ffb1c71863478cede0
MD5 756549c46bcd4bf16d71151762f8bd92
BLAKE2b-256 a3304a58d431edeccf6a68dcdf3a43092014e3c6f1073161d22571d2895cfc22

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8854468fd9c0161ef576ec0e6db34ce9ac27b773d80238f2b172001cf57db8f0
MD5 e5aad39fb0c0db467b79216e28fcb645
BLAKE2b-256 a77a6c418f339146f29d07ebfc66e77e6714de3acb4eff4d47901f7fb438034f

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 54ce4987b7efb3795aed56ca170bf92aa2c54fd259c359a76acef9a1e7a9f37d
MD5 4520a246c0007f05c413f3c562a93a7c
BLAKE2b-256 1cd275a4de89e8be5e4c7fe033afb3050a96e3d947b61c92e57f23012cac8999

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp38-cp38-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 5f5e3f413ee4c8f8f1ba03073dbef6fb797e88486a3e10c01c60af6e6d43d467
MD5 fdaec910526c37a8205a5f29c20d869c
BLAKE2b-256 ed854a05cbc10ebf9bfd53da02beb95be012590679a32235df701c2be5ff7999

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp37-none-win_amd64.whl.

File metadata

  • Download URL: changeforest-1.0.1-cp37-none-win_amd64.whl
  • Upload date:
  • Size: 321.5 kB
  • Tags: CPython 3.7, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for changeforest-1.0.1-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 06f458cf12fb91662c190e5ca4d76aacb3585471bfaf078774d37b7bb67a612e
MD5 4f494b149399986170c65312164cd7e1
BLAKE2b-256 e8aa945e7b5d1ff32b644edcaa13f1c8b4b680adb19b2d0fa268b467615f6f9f

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2f18b792e22271bdec0e8e084464e3c764596908dee7040ef24890780a6464c5
MD5 695021e1bab48931a7679701fd301617
BLAKE2b-256 8a484472fee62bc4f3a414ac53d9f40bfe88021c2337a57bbcb64d056851244a

See more details on using hashes here.

File details

Details for the file changeforest-1.0.1-cp37-cp37m-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for changeforest-1.0.1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 c3bdb8e9334f7cbd538b68513be3081d56125b900c8e698724dcd4b3cf393fcc
MD5 2601e992b39e19cc594b55a0ee939200
BLAKE2b-256 a20b0c141f7a1e551ab0b3957c0ebe6ec1c0fcad69ee3acbb45bf608c75bc436

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page