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.1.0.tar.gz (367.2 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.1.0-cp311-none-win_amd64.whl (319.6 kB view details)

Uploaded CPython 3.11Windows x86-64

changeforest-1.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

changeforest-1.1.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (916.3 kB view details)

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

changeforest-1.1.0-cp311-cp311-macosx_10_7_x86_64.whl (452.8 kB view details)

Uploaded CPython 3.11macOS 10.7+ x86-64

changeforest-1.1.0-cp310-none-win_amd64.whl (319.6 kB view details)

Uploaded CPython 3.10Windows x86-64

changeforest-1.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

changeforest-1.1.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (916.3 kB view details)

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

changeforest-1.1.0-cp310-cp310-macosx_10_7_x86_64.whl (452.8 kB view details)

Uploaded CPython 3.10macOS 10.7+ x86-64

changeforest-1.1.0-cp39-none-win_amd64.whl (319.6 kB view details)

Uploaded CPython 3.9Windows x86-64

changeforest-1.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

changeforest-1.1.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (916.2 kB view details)

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

changeforest-1.1.0-cp39-cp39-macosx_10_7_x86_64.whl (452.8 kB view details)

Uploaded CPython 3.9macOS 10.7+ x86-64

changeforest-1.1.0-cp38-none-win_amd64.whl (319.7 kB view details)

Uploaded CPython 3.8Windows x86-64

changeforest-1.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

changeforest-1.1.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (915.3 kB view details)

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

changeforest-1.1.0-cp38-cp38-macosx_10_7_x86_64.whl (452.5 kB view details)

Uploaded CPython 3.8macOS 10.7+ x86-64

changeforest-1.1.0-cp37-none-win_amd64.whl (319.7 kB view details)

Uploaded CPython 3.7Windows x86-64

changeforest-1.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

changeforest-1.1.0-cp37-cp37m-macosx_10_7_x86_64.whl (452.5 kB view details)

Uploaded CPython 3.7mmacOS 10.7+ x86-64

File details

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

File metadata

  • Download URL: changeforest-1.1.0.tar.gz
  • Upload date:
  • Size: 367.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for changeforest-1.1.0.tar.gz
Algorithm Hash digest
SHA256 b32dda93d2a6185d2d82836987dc3236304098509a9563d768a43bca96312a7b
MD5 912faef2f58ce28fdcd2fd15d6c483d5
BLAKE2b-256 9457944131bef774b752dfa8cfc684440972c5d4dec3ef83a89040e2f6405c02

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 fd3584cbbbdb36780c7abf3ad160455b77006cac2b0b6378daeb6ce98e57a075
MD5 549d663d7dc83a2ede25108d352ba404
BLAKE2b-256 a2b444a13b85c74afc047a6e74fbee5b7a87c9d8118ce747982a5e66e6bfa6d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2cc50cdc9164dc5ccc56e8a240338c4f80dcca39f9111b7dcf8e12f1156d6b05
MD5 a256683f37c97181aedc3269eaf488c4
BLAKE2b-256 18f24f1509b0583c9c2b2d38d49fc9628ce00aec8235227e6a2f241fb0702811

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 6e82ec720631f5778849b2ee0724f7672cbaa663fcf9df3fe085aff7300c6327
MD5 b52c5c949d090587ff57560abbbe0279
BLAKE2b-256 19bb24facfc8657562eca1c4537df783e39629217155e59bf8ef0f1eddd6547e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp311-cp311-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 ab35a870bb94693b0cad78f938f7c44dcc86ce89b2c414b71ae951ab7ce5ef86
MD5 bc394c858bca5bf6646a849e49ad890d
BLAKE2b-256 19c73f1528a07a3be536e12fe44a1cbd4fd0b37f139cc81aba1eb0ac2f6fddcd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 c0c8ebc18f3988b32bbac2aa2fe20ae2fb3ddede5cb63ef610c78a4660d3865d
MD5 4bb637943dc1a3d6c6c4ab62e80c3ad1
BLAKE2b-256 4f84944bf4cf4dd35f3b2caadc53ca435f77046e83a5ce46105b1514160a83c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7eb90078f525b8433076773685a812866952781a67cfdfb0dc8d675629a04df4
MD5 86a04907c862507d493bce43ffa60e29
BLAKE2b-256 c1606bb6c94d1b521af21c657764b25257a82a75d79dbb3f7e17ce3ef2e9e8f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 614ca6485d6bfeb1f76e9a61712d976a70dca639a9b8c42cd3ddc85e87df18fc
MD5 63868b8931cb45c07a0641c67b42d490
BLAKE2b-256 3399428eadc0defe797105dcc2e18c4a898549d1d9d85e25cca5978fbc3c5363

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 1e175264573b5c35eec9e0600f33af599b30656bcf452e4a21d6151cc900808b
MD5 4baccc1d35abaed148273bf1f53bae00
BLAKE2b-256 88eaf84605c6e31f0ba9506d1aaa9d5f0110ae33397648274d80bbefc81f9f7b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: changeforest-1.1.0-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 319.6 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for changeforest-1.1.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 5e806624b50df24ba088afb9171ad095482a75006f5574ad1bc91baf59cd18d4
MD5 cb57e24282873bd22a48e483a3535719
BLAKE2b-256 bb5de36772956b9708655875fda3ca9410de1821864e2a803231ff4b7d5a21d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c33280435dac3dc45c9076cc2e80aa7fbb36cd350cfa8c8be30716147f54eb3
MD5 ebeef851254b99213ffba8589825092d
BLAKE2b-256 1523a72a1dc383dfbc8c9228275113808f54fd920b7b0185b8d4d5abd1d4d7ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 7c16982aae8276b5ccc6c09b658480f07914c9b4c6923ca3dcd8f390e6067a61
MD5 ce5efd23dcef40ac77fb1d6b294006c5
BLAKE2b-256 1b8412ed159ced2028e581592abd08ca5110ceee0583ec78cb80e7cad95c08b0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 5d3ff56b201af4990928eb00e8c2cf1482c87b95f6a075bcaace767360ab4302
MD5 62ebbd23552d969b0193616a314708e6
BLAKE2b-256 6ec78b34b5dbcb1d9e1d6196a817686c73b69b0133828face1228b1eabb760ec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: changeforest-1.1.0-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 319.7 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for changeforest-1.1.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 e5f55d5ca6496ee239016c5edfe340b9979d0b9ce7e047d256b35ad84f630291
MD5 c4123028d5147ce2d80b28dd39f3dbfd
BLAKE2b-256 5b024a0592c371c1d877e934f07472a1368579531b320df4df6e95d55fa0f74f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 24da3d2b2cb6218bc61eb6b9baa1e8b50cce4c0b1bcb7cf428db1fc9fe96dd1b
MD5 0adf60f3324a7988065d09b7ebd0f37c
BLAKE2b-256 1fae78a93f84acc3e1432747151347e12a6fc021ceca3d0d10af49392d38e29f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 2c3e698f54bd06fa4d76791137eaf8607af64fc12df25d9801d044bae4fc6b4e
MD5 b75a9b4606de70cc4d294466632a6873
BLAKE2b-256 a025e820f4fb46af02bf6f81e394df655e85282a044ad83a603a24991c32f527

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 89ba23fc19c0e4c70890b1b58bc4bfd3c61079bb1d84de72a1bebd0477c596a7
MD5 085feb2f0020884842eb1115aaad6b06
BLAKE2b-256 321460b765359be202ef4d75b6d799ff94d48d532b07d4ab902de73e6a78b7ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: changeforest-1.1.0-cp37-none-win_amd64.whl
  • Upload date:
  • Size: 319.7 kB
  • Tags: CPython 3.7, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for changeforest-1.1.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 85f4b949623a6e70430c2bf3c5f2d82e690f1453347fe12ee5e233b2746ca722
MD5 af836e02ed14db1ecab6de65b4f04bd3
BLAKE2b-256 da25995b7f97c8eca8ea43399ba54d901054f2a0a24c43e835a65612c8eb37d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 51d5bbdab94f287dc2befe0202a83e9228475490877d430ff298ef3e1db53f4e
MD5 e49b820c7802c1773edb5e29d7d35f08
BLAKE2b-256 34dac7399ce66e406380428cf18dfe48e47854d17cc9cfc9b701ce618b7d2f68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for changeforest-1.1.0-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 12ac78642e9ac2d9223680eaaf221bf05107d7146baeee8a1f2d8a2b92e7de8c
MD5 37c114c0c2399c5f7c84d237ed22f9a4
BLAKE2b-256 b8115ebfcde7a3e5e5e1a4ee7fc7869b7f00dbbd7670b9cd79edb5b86556f2c5

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