Utilities API
normalization
min_max_scaling(time_series, min_val=0.0, max_val=1.0, inplace=False)
This function applied min max scaling for an 1D-array. It is inspired by: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.MinMaxScaler.html but lighter and reimplemented in order to not introduce unnecessary dependencies for small functionality.
Min max scaling as implemented here is sensitive to extreme outliers, but guarantees the value range not exceeding min_val and max_val.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_series
|
ndarray
|
1D array containing consecutive values for one feature |
required |
min_val
|
float
|
the minimum value the scaled time series will reach |
0.0
|
max_val
|
float
|
the maximum value the scale time series will reach |
1.0
|
inplace
|
bool
|
boolean to specify whether the input array will be scaled and changed in place |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
the scaled input array. |
Source code in changepoynt/utils/normalization.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
z_scaling(time_series, inplace=False)
This function applie z-normalization to an 1D-array. It is inspired by: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html but lighter and reimplemented in order to not introduce unnecessary dependencies for small functionality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_series
|
ndarray
|
1D array containing consecutive values for one feature |
required |
inplace
|
bool
|
boolean to specify whether the input array will be scaled and changed in place |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
the scaled input array. |
Source code in changepoynt/utils/normalization.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
linalg
HankelCorrelationFFTRepresentation
This class represents a matrix -atrix product of a hankel matrix, e.g., H*H.T. It is needed as the multiplication with it needs to go through two iterations instead of one.
Source code in changepoynt/utils/linalg.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | |
HankelFFTRepresentation
This matrix represents a Hankel matrix and makes matrix multiplication with it faster:
See our paper for more details: Efficient Hankel Matrix Decomposition for Changepoint Detection Lucas Weber, Richard Lenz 2024
Source code in changepoynt/utils/linalg.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
__array_function__(func, types, args, kwargs)
We currently only support concatenation. https://numpy.org/neps/nep-0018-array-function-protocol.html
Source code in changepoynt/utils/linalg.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | |
MultilevelHankelFFTRepresentation
This class implements multiplication with a multilevel Hankel matrix, i.e., a matrix which can be divided into multiple parts that each have Hankel structure.
We expect to receive a list of: - Hankel matrices as HankelFFTRepresentation objects - The positions of the Hankel matrices in the original matrix (upper left row, upper left col)
TODO: Currently only tested with row/col concatenation [H,H]/[[H],[H]] and not with multiple blocks [[H, H], [H, H]]
Source code in changepoynt/utils/linalg.py
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 | |
T
property
Compute the transposed. Only the internal matrices are copied!
Returns:
| Type | Description |
|---|---|
MultilevelHankelFFTRepresentation
|
|
compile_hankel(time_series, end_index, window_size, rank, lag=1, const_offset=None)
This function constructs a hankel matrix from a 1D time series. Please make sure constructing the matrix with the given parameters (end index, window size, etc.) is possible, as this function does no checks due to performance reasons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_series
|
ndarray
|
1D array with float values as the time series |
required |
end_index
|
int
|
the index (point in time) where the time series starts |
required |
window_size
|
int
|
the size of the windows cut from the time series |
required |
rank
|
int
|
the amount of time series in the matrix |
required |
lag
|
int
|
the lag between the time series of the different columns |
1
|
const_offset
|
float
|
an offset subtracted from all values of the time series before filling the hankel matrix |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
The hankel matrix with lag one |
Source code in changepoynt/utils/linalg.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
examples()
This function implements some usage examples for quick internal testing. It is not aimed at being used.
Returns:
| Type | Description |
|---|---|
|
None |
Source code in changepoynt/utils/linalg.py
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 | |
facebook_randomized_svd(a_matrix, randomized_rank)
This function implements randomized singular vector decomposition of a matrix as surveyed and described in
Halko, Nathan, Per-Gunnar Martinsson, and Joel A. Tropp. "Finding structure with randomness: Probabilistic algorithms for constructing approximate matrix decompositions." SIAM review 53.2 (2011): 217-288. on page 4 chapter 1.3 and further.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_matrix
|
ndarray
|
2D-Matrix filled with floats for which we want to find the left eigenvectors |
required |
randomized_rank
|
int
|
the rank of the noise matrix used for randomized svd. the higher the rank, the better the approximation but the lower the precision of the eigenvectors |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, ndarray)
|
|
Source code in changepoynt/utils/linalg.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
lanczos(a_matrix, r_0, k)
This function computes the tri-diagonalization matrix from the square matrix C which is the result of the lanczos algorithm.
The algorithm has been described and proven in: Idé, Tsuyoshi, and Koji Tsuda. "Change-point detection using krylov subspace learning." Proceedings of the 2007 SIAM International Conference on Data Mining. Society for Industrial and Applied Mathematics, 2007.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_matrix
|
ndarray
|
2D-Matrix of size NxN filled with floats where we want to find the krylov subspace approx. for |
required |
r_0
|
ndarray
|
intial starting vector for the subspace approximation |
required |
k
|
int
|
size of the approximation |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, ndarray)
|
Returns the alpha and beta values for the tridiagonal, symmetric matrix T. alphas are the values from the main diagonal and beta from the off diagonal as described in https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.eigh_tridiagonal.html (alpha = d, beta = e) |
Source code in changepoynt/utils/linalg.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
power_method(a_matrix, x_vector, n_iterations)
This function searches the largest (dominant) eigenvalue and corresponding eigenvector by repeated multiplication of the matrix A with an initial vector. It assumes a dominant eigenvalue bigger than the second one, otherwise it won't converge.
For proof and explanation look at: https://pythonnumericalmethods.berkeley.edu/notebooks/chapter15.02-The-Power-Method.html
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_matrix
|
ndarray
|
2D-Matrix of size NxN filled with floats |
required |
x_vector
|
ndarray
|
Vector of size Nx1 filled with floats |
required |
n_iterations
|
int
|
the amount of iterations for the approximation |
required |
Returns:
| Type | Description |
|---|---|
(float, ndarray)
|
the dominant eigenvalue and corresponding eigenvector |
Source code in changepoynt/utils/linalg.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
randomized_hankel_svd(hankel_matrix, k, subspace_iteration_q=2, oversampling_p=2)
Function for the randomized singular vector decomposition using [1]. Implementation modified from: https://pypi.org/project/fbpca/
Source code in changepoynt/utils/linalg.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
rayleigh_ritz_singular_value_decomposition(a_matrix, k)
This function uses the Rayleigh-Ritz method implemented in ARPACK to compute the k highest eigenvalues and corresponding eigenvectors. It should be faster than a complete svd.
!NOTE!: The order of the k highest eigenvalues is not guaranteed by this method!
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a_matrix
|
ndarray
|
2D-Matrix filled with floats for which we want to find the left eigenvectors |
required |
k
|
int
|
the number of highest eigenvectors we want to find |
required |
Returns:
| Type | Description |
|---|---|
(ndarray, ndarray)
|
returns the eigenvalues and eigenvectors as numpy arrays |
Source code in changepoynt/utils/linalg.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 | |
tridiagonal_eigenvalues(alphas, betas, amount=-1)
This function uses a fast approach for symmetric tridiagonal matrices to calculate the [amount] highest eigenvalues and corresponding eigenvectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alphas
|
ndarray
|
main diagonal elements |
required |
betas
|
ndarray
|
off diagonal elements |
required |
amount
|
The number of eigenvalues you want to compute (from the highest) |
-1
|
Returns:
| Type | Description |
|---|---|
|
eigenvalues and corresponding eigenvectors |
Source code in changepoynt/utils/linalg.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
block_linalg
BlockHankel
This class is the base class for BlockHankel matrices. It mainly serves as the interface to capture numpy matrix multiplications (@) using the functions matmul (if a BlockHankel matrix is on the left side of the @ operator) and array_ufunc (if a BlockHankel is on the right side of the @ operator)
If these functions are called, the input is given to the abstract methods multiply_other_from_left and multiply_other_from_right which we expect the subclasses to implement in detail
Source code in changepoynt/utils/block_linalg.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
T
abstractmethod
property
Subclasses should implement this method. The method should transpose the matrix.
__array_function__(func, types, args, kwargs)
We currently only support concatenation. https://numpy.org/neps/nep-0018-array-function-protocol.html
Source code in changepoynt/utils/block_linalg.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
__array_ufunc__(ufunc, method, *args, **kwargs)
This function is called by numpy, if an instance of the current class on the left side of an operator where the left side is any numpy object.
Basically, numpy first checks if the left side of an operator has defined an operation for both objects on the right and left. If this fails, it checks the object on the right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
the function that is called on the objects |
required | |
method
|
what type of invocation it was |
required | |
args
|
the arguments of the function |
()
|
|
kwargs
|
the keyword arguments of the function |
{}
|
Returns:
| Type | Description |
|---|---|
Union[ndarray, BlockHankel]
|
|
Source code in changepoynt/utils/block_linalg.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
__matmul__(other)
This function is called by numpy if we use an instance of the current class on the left side of a matrix multiplication operator (@)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
the other matrix |
required |
Returns:
| Type | Description |
|---|---|
Union[ndarray, BlockHankel]
|
|
Source code in changepoynt/utils/block_linalg.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
copy(deep=False)
abstractmethod
Subclasses should implement this method.
Source code in changepoynt/utils/block_linalg.py
415 416 417 418 | |
multiply_other_from_left(other_maxtrix)
abstractmethod
Subclasses should implement this method.
Source code in changepoynt/utils/block_linalg.py
410 411 412 413 | |
multiply_other_from_right(other_maxtrix)
abstractmethod
Subclasses should implement this method.
Source code in changepoynt/utils/block_linalg.py
405 406 407 408 | |
BlockHankelProductRepresentation
Bases: BlockHankel
This class represents a matrix-matrix product of two hankel matrices. H @ H Instead of computing the product and materializing it, we keep both matrices so we have hankel property for products.
See Section III-G in: L. Weber and R. Lenz, "Accelerating Singular Spectrum Transformation for Scalable Change Point Detection," in IEEE Access, vol. 13, pp. 213556-213577, 2025, doi: 10.1109/ACCESS.2025.3640386. for details.
Source code in changepoynt/utils/block_linalg.py
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 | |
__array_function__(func, types, args, kwargs)
We currently only support concatenation. https://numpy.org/neps/nep-0018-array-function-protocol.html
Source code in changepoynt/utils/block_linalg.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
__array_ufunc__(ufunc, method, *args, **kwargs)
This function is called by numpy, if an instance of the current class on the left side of an operator where the left side is any numpy object.
Basically, numpy first checks if the left side of an operator has defined an operation for both objects on the right and left. If this fails, it checks the object on the right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
the function that is called on the objects |
required | |
method
|
what type of invocation it was |
required | |
args
|
the arguments of the function |
()
|
|
kwargs
|
the keyword arguments of the function |
{}
|
Returns:
| Type | Description |
|---|---|
Union[ndarray, BlockHankel]
|
|
Source code in changepoynt/utils/block_linalg.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
__matmul__(other)
This function is called by numpy if we use an instance of the current class on the left side of a matrix multiplication operator (@)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
the other matrix |
required |
Returns:
| Type | Description |
|---|---|
Union[ndarray, BlockHankel]
|
|
Source code in changepoynt/utils/block_linalg.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
BlockHankelRepresentation
Bases: BlockHankel
This matrix represents a Block Hankel matrix for large matrices and fast_hankel=True it makes matrix multiplication faster:
See our paper for more details: Efficient Hankel Matrix Decomposition for Changepoint Detection Lucas Weber, Richard Lenz 2024
Source code in changepoynt/utils/block_linalg.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
__array_function__(func, types, args, kwargs)
We currently only support concatenation. https://numpy.org/neps/nep-0018-array-function-protocol.html
Source code in changepoynt/utils/block_linalg.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
__array_ufunc__(ufunc, method, *args, **kwargs)
This function is called by numpy, if an instance of the current class on the left side of an operator where the left side is any numpy object.
Basically, numpy first checks if the left side of an operator has defined an operation for both objects on the right and left. If this fails, it checks the object on the right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
the function that is called on the objects |
required | |
method
|
what type of invocation it was |
required | |
args
|
the arguments of the function |
()
|
|
kwargs
|
the keyword arguments of the function |
{}
|
Returns:
| Type | Description |
|---|---|
Union[ndarray, BlockHankel]
|
|
Source code in changepoynt/utils/block_linalg.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
__matmul__(other)
This function is called by numpy if we use an instance of the current class on the left side of a matrix multiplication operator (@)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
the other matrix |
required |
Returns:
| Type | Description |
|---|---|
Union[ndarray, BlockHankel]
|
|
Source code in changepoynt/utils/block_linalg.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
materialize()
Explicitly build the full block Hankel matrix H for test multiplications.
Definitions
p, q : the shape of the Hankel matrix (p rows, q columns) m, n : the shape of the blocks in the Hankel matrix (each block has m rows, n columns)
B has shape (p + q - 1, m, n). H has shape (pm, qn).
Source code in changepoynt/utils/block_linalg.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
MultilevelBlockHankelRepresentation
Bases: BlockHankel
This class implements multiplication with a multilevel Block Hankel matrix, i.e., a matrix which can be divided into multiple parts that each have Hankel structure.
We expect to receive a list of: - Hankel matrices as BlockHankelRepresentation objects - The concatenation axis (currently only 0 and 1 are supported)
Source code in changepoynt/utils/block_linalg.py
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | |
T
property
__array_function__(func, types, args, kwargs)
We currently only support concatenation. https://numpy.org/neps/nep-0018-array-function-protocol.html
Source code in changepoynt/utils/block_linalg.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
__array_ufunc__(ufunc, method, *args, **kwargs)
This function is called by numpy, if an instance of the current class on the left side of an operator where the left side is any numpy object.
Basically, numpy first checks if the left side of an operator has defined an operation for both objects on the right and left. If this fails, it checks the object on the right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
the function that is called on the objects |
required | |
method
|
what type of invocation it was |
required | |
args
|
the arguments of the function |
()
|
|
kwargs
|
the keyword arguments of the function |
{}
|
Returns:
| Type | Description |
|---|---|
Union[ndarray, BlockHankel]
|
|
Source code in changepoynt/utils/block_linalg.py
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
__matmul__(other)
This function is called by numpy if we use an instance of the current class on the left side of a matrix multiplication operator (@)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
the other matrix |
required |
Returns:
| Type | Description |
|---|---|
Union[ndarray, BlockHankel]
|
|
Source code in changepoynt/utils/block_linalg.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
materialize()
Explicitly build the full multilevel block Hankel matrix H for test multiplications.
Source code in changepoynt/utils/block_linalg.py
751 752 753 754 755 756 | |
block_hankel_left_matmat_direct(hankel, other_matrix)
Compute A @ H without FFT, using a vectorized einsum.
H has block structure:
H[i, j] = B[i + j]
Definitions
p, q : the shape of the Hankel matrix (p rows, q columns) m, n : the shape of the blocks in the Hankel matrix (each block has m rows, n columns)
Parameters
hankel : ndarray, shape (p + q - 1, m, n) Block sequence. hankel[k] is an m x n matrix block.
other_matrix : ndarray Matrix to multiply by Either: - dense form: shape (s, p*m) - block form: shape (p, s, m)
Returns
Z : ndarray If A was dense, returns dense shape (s, q*n). If A was block-form, returns block shape (q, s, n).
Source code in changepoynt/utils/block_linalg.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
block_hankel_left_matmat_fft(hankel_fft, other_matrix, fft_length, window_length, window_number)
Compute A @ H using FFT, where H is a block Hankel matrix.
H has block structure:
H[i, j] = B[i + j]
Definitions
p, q : the shape of the Hankel matrix (p rows, q columns) m, n : the shape of the blocks in the Hankel matrix (each block has m rows, n columns)
Parameters
hankel_fft : ndarray, shape (p + q - 1, m, n) The fft transformed (along axis 0) of the Block sequence. B[k] is an m x n matrix block.
other_matrix : ndarray The matrix that we multiply from the right (other_matrix @ hankel_fft). Either: - dense form: shape (s, p*m) - block form: shape (p, s, m)
fft_length : int The length of the fft (with padding). We recommend using scipy.fft.next_fast_length
window_length : int The number of blocks per row of the Hankel matrix
window_number : int The number of blocks per column of the Hankel matrix
Returns
result_matrix : ndarray Outcome of other_matrix @ hankel_fft If A was dense, returns dense shape (s, q*n). If A was block-form, returns block shape (q, s, n).
Source code in changepoynt/utils/block_linalg.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
block_hankel_right_matmat_direct(hankel, other_matrix)
Compute H @ X without FFT, using a vectorized einsum.
H has block structure:
H[i, j] = B[i + j]
Definitions
p, q : the shape of the Hankel matrix (p rows, q columns) m, n : the shape of the blocks in the Hankel matrix (each block has m rows, n columns)
Parameters
hankel : ndarray, shape (p + q - 1, m, n) Block sequence. hankel[k] is an m x n matrix block.
other_matrix : ndarray Either: - dense form: shape (q*n, r) - block form: shape (q, n, r)
Returns
Y : ndarray If X was dense, returns dense shape (p*m, r). If X was block-form, returns block shape (p, m, r).
Source code in changepoynt/utils/block_linalg.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
block_hankel_right_matmat_fft(hankel_fft, other_matrix, fft_length, window_length, window_number)
Compute H @ X using FFT, where H is a block Hankel matrix.
H has block structure:
H[i, j] = B[i + j]
Definitions
p, q : the shape of the Hankel matrix (p rows, q columns) m, n : the shape of the blocks in the Hankel matrix (each block has m rows, n columns)
Parameters
hankel_fft : ndarray, shape (p + q - 1, m, n) The fft transformed (along axis 0) of the Block sequence. B[k] is an m x n matrix block.
other_matrix : ndarray Either: - dense form: shape (q*n, r) - block form: shape (q, n, r)
fft_length : int The length of the fft (with padding). We recommend using scipy.fft.next_fast_length
window_length : int The number of blocks per row of the Hankel matrix
window_number : int The number of blocks per column of the Hankel matrix
Returns
Y : ndarray If X was dense, returns dense shape (p*m, r). If X was block-form, returns block shape (p, m, r).
Source code in changepoynt/utils/block_linalg.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
densityratioestimation
Relative Unconstrained Least-Squares Fitting (RuLSIF): A Python Implementation
[1] Liu S , Yamada M , Collier N , et al. Change-Point Detection in Time-Series Data by Relative Density-Ratio Estimation[J]. 2012.
[2] Kawahara Y , Sugiyama M . Sequential change‐point detection based on direct density‐ratio estimation[M]. John Wiley & Sons, Inc. 2012.
[3] Kawahara Y , Yairi T , Machida K . Change-Point Detection in Time-Series Data Based on Subspace Identification[C] Data Mining, 2007. ICDM 2007. Seventh IEEE International Conference on. IEEE, 2007.
Taken from https://github.com/chenxingqiang/rulsif_abrupt-change_detection copied and heavily modified for performance and jit compilation. Also corrected a missing transpose in the Gaussian Kernel class. Additionally, optimized the cross validation to not recompute the gaussian kernel several times as we only change a factor within the exponent (the sigma).
Has been compared to the demo implementation of the original author: https://github.com/anewgithubname/change_detection
Unfortunately, the cross validation can't be JIT compiled, as we need to compute theta_hat, which is way faster using a specialized scipy function for hermitian matrices. Numba can't deal with scipy yet.
AlphaRelativeDensityRatioEstimator
Computes the alpha-relative density ratio estimate of P(X_ref) and P(X_test) The alpha-relative density ratio estimator, r_alpha(X), is given by the following kernel model: g(X; theta) = SUM( (theta_l * K(X, X_centers_l)), l=0, n ) where theta is a vector of parameters [theta_1, theta_2, ..., theta_l]^T to be learned from the data samples. The parameters theta in the model g(X; theta) is calculated by solving the following optimization problem: theta_hat = argmin [ ( (1/2) * theta^T * H_hat * theta) - (h_hat^T * theta) + ( lambda/2 * theta^T * theta) ] where the expression (lambda/2 * theta^T * theta), with lambda >= 0, is a regularization term used to penalize against overfitting Reference: Relative Density-Ratio Estimation for Robust Distribution Comparison. Makoto Yamada, Taiji Suzuki, Takafumi Kanamori, Hirotaka Hachiya, and Masashi Sugiyama. NIPS, page 594-602. (2011)
Source code in changepoynt/utils/densityratioestimation.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
apply(reference_samples, test_samples, gaussian_centers)
Computes the alpha-relative density ratio, r_alpha(X), of P(X_ref) and P(X_test) r_alpha(X) = P(Xref) / (alpha * P(Xref) + (1 - alpha) * P(X_test) Returns density ratio estimate at X_ref, r_alpha_ref, and at X_test, r_alpha_test
Source code in changepoynt/utils/densityratioestimation.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
PearsonRelativeDivergenceEstimator
Calculates the alpha-relative Pearson divergence score The alpha-relative Pearson divergence is given by the following expression: PE_alpha = -(alpha/2(n_ref)) * SUM(r_alpha(X_ref_i)^2, i=0, n_ref) - ((1-alpha)/2(n_test)) * SUM(r_alpha(X_test_j)^2, j=0, n_test) + (1/n_ref) * SUM(r_alpha(X_ref_i), i=0, n_ref) - 1/2 where r_alpha(X) is the alpha-relative density ratio estimator and is given by the following kernel model: g(X; theta) = SUM( (theta_l * K(X, X_centers_l)), l=0, n ) Reference: Relative Density-Ratio Estimation for Robust Distribution Comparison. Makoto Yamada, Taiji Suzuki, Takafumi Kanamori, Hirotaka Hachiya, and Masashi Sugiyama. NIPS, page 594-602. (2011)
Source code in changepoynt/utils/densityratioestimation.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
apply(reference_samples=None, test_samples=None, gaussian_centers=None)
Calculates the alpha-relative Pearson divergence score
Source code in changepoynt/utils/densityratioestimation.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | |
RuLSIF
Estimates the alpha-relative Pearson Divergence via the least Squares Relative Density Ratio Approximation Reference: Relative Density-Ratio Estimation for Robust Distribution Comparison. Makoto Yamada, Taiji Suzuki, Takafumi Kanamori, Hirotaka Hachiya, and Masashi Sugiyama. NIPS, page 594-602. (2011)
Source code in changepoynt/utils/densityratioestimation.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 | |
apply(reference_samples=None, test_samples=None)
Estimates the alpha-relative Pearson divergence as determined by the relative ratio of probability densities: P(reference_samples[x]) / (alpha * P(reference_samples[x]) + (1 - alpha) * P(test_samples[x])) from samples: reference_samples[x_i] | reference_samples[x_i] in R^{d}, with i=1 to reference_samples{N} drawn independently of P(reference_samples[x]) and from samples: test_samples[x_j] | test_samples[x_j] in R^{d}, with j=1 to test_samples{N} drawn independently from P(test_samples[x]) After the model hyperparameters have been learned and chosen by the train() method, the RULSIF algorithm can be applied repeatedly on both in-sample and out of sample data
Source code in changepoynt/utils/densityratioestimation.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | |
compute_gaussian_width_candidates(reference_samples, test_samples)
staticmethod
Compute a candidate list of Gaussian kernel widths. The best width will be selected via cross-validation
Jaakkola's heuristic method for setting the width parameter of the Gaussian radial basis function kernel is to pick a quantile (usually the median) of the distribution of Euclidean distances between points having different labels. Reference:
Jaakkola T S, Haussler D. Exploiting Generative Models in Discriminative Classifiers[J]. Advances in Neural Information Processing Systems, 1998, 11(11):487--493.
Jaakkola, M. Diekhaus, and D. Haussler. Using the Fisher kernel method to detect remote protein homologies. In T. Lengauer, R. Schneider, P. Bork, D. Brutlad, J. Glasgow, H.- W. Mewes, and R. Zimmer, editors, Proceedings of the Seventh International Conference on Intelligent Systems for Molecular Biology.
It is the same technique as proposed in [1]
Source code in changepoynt/utils/densityratioestimation.py
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
compute_model_parameters(reference_samples=None, test_samples=None, gaussian_centers=None)
Computes model parameters via k-fold cross validation process
Source code in changepoynt/utils/densityratioestimation.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 | |
generate_gaussian_centers(reference_samples=None)
Choose Gaussian centers randomly from the reference samples.
Source code in changepoynt/utils/densityratioestimation.py
365 366 367 368 369 370 371 372 373 374 375 | |
generate_regularization_params()
staticmethod
Generates a candidate list of regularization parameters to be used with the L1 regularizer term of RULSIF optimization problem. The best regularizer parameter will be chosen via cross-validation. The values itself are taken from the paper [1].
Source code in changepoynt/utils/densityratioestimation.py
355 356 357 358 359 360 361 362 363 | |
train(reference_samples=None, test_samples=None)
Learn the proper model parameters if we did not specify them already
Source code in changepoynt/utils/densityratioestimation.py
467 468 469 470 471 472 473 474 475 476 477 478 479 480 | |
compute_H_hat(alpha=0.0, kernel_matrix_ref_samples=None, kernel_matrix_test_samples=None)
Calculates the H_hat term of the theta_hat optimization problem
Source code in changepoynt/utils/densityratioestimation.py
146 147 148 149 150 151 152 153 154 155 156 157 | |
compute_distance(samples, sample_means)
Compute the distances between points in the sample's feature space to points along the center of the distribution
Source code in changepoynt/utils/densityratioestimation.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
compute_gaussian_kernel(samples, sample_means, sigma)
Computes an n-dimensional Gaussian/RBF kernel matrix by taking points in the sample's feature space and maps them to kernel coordinates in Hilbert space by calculating the distance to each point in the sample space and taking the Gaussian function of the distances. K(X,Y) = exp( -(|| X - Y ||^2) / (2 * sigma^2) ) where X is the matrix of data points in the sample space, Y is the matrix of gaussian centers in the sample space sigma is the width of the gaussian function being used
Source code in changepoynt/utils/densityratioestimation.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
compute_h_hat(kernel_matrix_ref_samples)
Calculates the h_hat term of the theta_hat optimization problem
Source code in changepoynt/utils/densityratioestimation.py
160 161 162 163 164 165 166 167 168 169 170 171 172 | |
compute_theta_hat(H_hat, lambda_scaled_identity, h_hat)
Calculates theta_hat given H_hat, h_hat, lambda, and the kernel basis function Treat as a system of linear equations and find the exact, optimal solution
Source code in changepoynt/utils/densityratioestimation.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
g_of_x_theta(kernel_matrix_samples, theta_hat)
Calculate the alpha-relative density ratio kernel model
Source code in changepoynt/utils/densityratioestimation.py
204 205 206 207 208 209 | |
j_of_theta(alpha, g_xref_theta, g_xtest_theta)
Calculates the squared error criterion, J
Source code in changepoynt/utils/densityratioestimation.py
194 195 196 197 198 199 200 201 | |
solve(a, b, lower=False, overwrite_a=False, overwrite_b=False)
DANGER! This implementation is stolen from within the scipy package and is equivalent to scipy.linalg.solve We reduced a lot of checks to tailor it to the given case within this project and make it as fast as possible as it is in the hottest path of RuLSIF and by far the most called function.
This might result in problems, as the checks are really minimal.
Source code in changepoynt/utils/densityratioestimation.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |