deepcharles / ruptures

ruptures: change point detection in Python
BSD 2-Clause "Simplified" License
1.56k stars 161 forks source link

How to construct a loss function to detect the points where the correlation changes? #236

Closed lupupu closed 2 years ago

lupupu commented 2 years ago

微信截图_20220217230054 As shown in the figure, the correlation has changed once. Is there any way to find the point of correlation change?

oboulant commented 2 years ago

Hi @lupupu ,

Thx for your interest in ruptures.

Off the top of my head, I would use a convolution or a cross-correlation between the two series and then look for a break point in the resulting series.

Hope it helps ! Best !

deepcharles commented 2 years ago

Hello,

Sorry for the late reply.

There is also a dedicated cost function for this situation: linear cost. It finds a change in the linear relationship between a univariate signal (e.g. the blue signal) and several covariates (e.g. the red signal).

To use it:

n_samples = signal_red.shape[0]
#concatenate the two signals + a constant signal
signal = np.c_[signal_red, signal_blue, np.ones(n_samples)]
# choose a method, here Dynp
algo = rpt.Dynp(model="linear").fit(signal)
# predict
algo.predict(n_bkps=3)

Note that this method has limits when the number of samples per segment is low.

deepcharles commented 2 years ago

Closing for now. Feel free to reopen.