Skip to content
This repository has been archived by the owner on Oct 8, 2019. It is now read-only.

Feature/changefinder1d #333

Merged
merged 39 commits into from
Sep 6, 2016
Merged

Feature/changefinder1d #333

merged 39 commits into from
Sep 6, 2016

Conversation

myui
Copy link
Owner

@myui myui commented Sep 3, 2016

Revising the PR of #305 by @L3Sota

myui and others added 27 commits June 17, 2016 07:59
…etected.

Removed -dim option accordingly. Also changed some initial parameter values and minutiae in ChangeFinderUDFTest, and fixed a typo in MathUtils.
Implemented multiple passes for multidimensional covariances to ensure they are full-rank.
Changed internals of solving the system of equations from Cholesky Decomposition to LU Decomposition (However, after the multiple changes introduced in this commit, Cholesky Decomposition may be viable again.)
- Pretty comments describing the computation process in more detail.
- Using the initial values of the data to break in data structures instead of rand.
(Random generation tends to unpredictably reduce detection accuracy in the beginning.)
- Added a moving average for change-point scores to improve detection accuracy.
- Refactored tests
The Java 7 Double class doesn't have a isFinite(double) method. Using isNaN(double) instead.
* Implementation refers to MATLAB's `arburg()` function
* Test cases come from outputs of Octave's `arburg()` function
* Replace `-A` with `A` in SDAR1D
* Update corresponding test cases
Implement `arburg()` and some minor fixes on MatrixUtils
Support Hellinger-distance-based scoring both on SDAR 1D and 2D
@myui myui added this to the v0.4 milestone Sep 3, 2016
*/
RealMatrix[][] rhs = MatrixUtils.toeplitz(C, k);
RealMatrix[] lhs = Arrays.copyOfRange(C, 1, k + 1);
RealMatrix R = MatrixUtils.flatten(rhs);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@L3Sota Are you expecting each C_i as a submatrix and flatting a matrix of matrix to a matrix?

R will not become a Toeplitz Matrix then and it's hard to solve A by LUDecomposition.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@myui Yes, that's expected. I didn't know non-Toeplitz makes LU decomp so difficult at the time of writing, though :(

Small points: the comments from L116-129 seem to be a little off regarding style, not too important:

  • In the Yule-Walker equations, i and j are 1..k, but in the visualisation of the equation the C and A vector-of-matrices' run from 1..n
  • The A vector-of-matrices should be horizontal
  • Both C'_i and C_i' are used

@myui
Copy link
Owner Author

myui commented Sep 3, 2016

Is this expected behavior for combineMatrix() (i.e., flatten() )?

    @Test
    public void testFlatten2d() {
        RealMatrix[] m1 = new RealMatrix[] { new Array2DRowRealMatrix(new double[] {1, 1.1}), new Array2DRowRealMatrix(new double[] {2, 2.2}), new Array2DRowRealMatrix(new double[] {3, 3.3})}; 
        // {1.0,1.1}
        // {2.0,2.2}
        // {3.0,3.3}
        RealMatrix[][] toeplitz1 = MatrixUtils.toeplitz(m1, 3);
        // {1.0,1.1}  {2.0,2.2}' {3.0,3.3}'
        // {2.0,2.2}  {1.0,1.1}  {2.0,2.2}'
        // {3.0,3.3}  {2.0,2.2}  {1.0,1.1}
        RealMatrix flatten1 = MatrixUtils.flatten(toeplitz1, 2);
        // 1.0 0.0 2.0 2.2 3.0 3.3
        // 1.1 0.0 0.0 0.0 0.0 0.0
        // 2.0 0.0 1.0 0.0 2.0 2.2
        // 2.2 0.0 1.1 0.0 0.0 0.0
        // 3.0 0.0 2.0 0.0 1.0 0.0
        // 3.3 0.0 2.2 0.0 1.1 0.0
    }

LUDecomposition assume a squared matrix.
http://commons.apache.org/proper/commons-math/userguide/linear.html

@myui
Copy link
Owner Author

myui commented Sep 3, 2016

>>> from scipy import linalg
>>> import numpy as np
>>> x = np.array([[1.0,1.1],[2.0,2.2],[3.0,3.3]])
>>> x
array([[ 1. ,  1.1],
       [ 2. ,  2.2],
       [ 3. ,  3.3]])
>>> linalg.toeplitz(x)
array([[ 1. ,  1.1,  2. ,  2.2,  3. ,  3.3],
       [ 1.1,  1. ,  1.1,  2. ,  2.2,  3. ],
       [ 2. ,  1.1,  1. ,  1.1,  2. ,  2.2],
       [ 2.2,  2. ,  1.1,  1. ,  1.1,  2. ],
       [ 3. ,  2.2,  2. ,  1.1,  1. ,  1.1],
       [ 3.3,  3. ,  2.2,  2. ,  1.1,  1. ]])
>>> x = np.matrix([[1.0,1.1],[2.0,2.2],[3.0,3.3]])
>>> x
matrix([[ 1. ,  1.1],
        [ 2. ,  2.2],
        [ 3. ,  3.3]])
>>> linalg.toeplitz(x)
array([[ 1. ,  1.1,  2. ,  2.2,  3. ,  3.3],
       [ 1.1,  1. ,  1.1,  2. ,  2.2,  3. ],
       [ 2. ,  1.1,  1. ,  1.1,  2. ,  2.2],
       [ 2.2,  2. ,  1.1,  1. ,  1.1,  2. ],
       [ 3. ,  2.2,  2. ,  1.1,  1. ,  1.1],
       [ 3.3,  3. ,  2.2,  2. ,  1.1,  1. ]])
>>> x = np.array([np.array([[1.0,1.1],[2.0,2.2]]),np.array([[3.0,3.3],[4.0,4.4]])])
>>> x
array([[[ 1. ,  1.1],
        [ 2. ,  2.2]],

       [[ 3. ,  3.3],
        [ 4. ,  4.4]]])
>>> linalg.toeplitz(x)
array([[ 1. ,  1.1,  2. ,  2.2,  3. ,  3.3,  4. ,  4.4],
       [ 1.1,  1. ,  1.1,  2. ,  2.2,  3. ,  3.3,  4. ],
       [ 2. ,  1.1,  1. ,  1.1,  2. ,  2.2,  3. ,  3.3],
       [ 2.2,  2. ,  1.1,  1. ,  1.1,  2. ,  2.2,  3. ],
       [ 3. ,  2.2,  2. ,  1.1,  1. ,  1.1,  2. ,  2.2],
       [ 3.3,  3. ,  2.2,  2. ,  1.1,  1. ,  1.1,  2. ],
       [ 4. ,  3.3,  3. ,  2.2,  2. ,  1.1,  1. ,  1.1],
       [ 4.4,  4. ,  3.3,  3. ,  2.2,  2. ,  1.1,  1. ]])
>>> y = x.flatten()
>>> y
array([ 1. ,  1.1,  2. ,  2.2,  3. ,  3.3,  4. ,  4.4])
>>> y.shape
(8,)

Fix transpose scheme of toeplitz().
https://github.com/scipy/scipy/blob/v0.14.0/scipy/linalg/special_matrices.py#L186
http://mathworld.wolfram.com/ToeplitzMatrix.html

Before:
                if (row < col) {
                    toeplitz[row][col] = c[col - row];
                } else if (row > col) {
                    toeplitz[row][col] = c[row - col].transpose();
                }
After:
                if (row < col) {
                    toeplitz[row][col] = c[col - row].transpose();
                } else if (row > col) {
                    toeplitz[row][col] = c[row - col];
                }

*/
@Nonnull
public static RealMatrix[][] toeplitz(@Nonnull final RealMatrix[] c, final int dim) {
Preconditions.checkArgument(dim >= 1, "Invliad dimension: " + dim);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invliad -> Invalid

@myui
Copy link
Owner Author

myui commented Sep 4, 2016

@L3Sota Thanks. Fixed typos.

@myui
Copy link
Owner Author

myui commented Sep 4, 2016

@L3Sota

I think it should be flattened as follows to be solved by LU decomposition:

>>> x = np.array([[1.0,1.1],[2.0,2.2],[3.0,3.3]])
>>> x
array([[ 1. ,  1.1],
       [ 2. ,  2.2],
       [ 3. ,  3.3]])
>>> linalg.toeplitz(x)
array([[ 1. ,  1.1,  2. ,  2.2,  3. ,  3.3],
       [ 1.1,  1. ,  1.1,  2. ,  2.2,  3. ],
       [ 2. ,  1.1,  1. ,  1.1,  2. ,  2.2],
       [ 2.2,  2. ,  1.1,  1. ,  1.1,  2. ],
       [ 3. ,  2.2,  2. ,  1.1,  1. ,  1.1],
       [ 3.3,  3. ,  2.2,  2. ,  1.1,  1. ]])

@myui
Copy link
Owner Author

myui commented Sep 6, 2016

5 dimenasional input of @L3Sota 's test

Hyperparameters used: K=10, T1=10, T2=10, r1=0.01, r2=0.01, lossFunc1=logloss, lossFunc2=lossloss

set ytics nomirror
set y2tics
set y2r[-5:60]
set style fill transparent solid 0.3 noborder

plot \
  for [i=7:11:1] \
    "sota5d.dat" using 1:(sum [col=i:11] column(col)) \
    title columnheader(i) with filledcurves y1=0 axes x1y1, \
  "sota5d.dat" using 1:12 with lines title "outlier" axes x1y2, \
  "sota5d.dat" using 1:13 with lines title "change" axes x1y2

sota5d

@myui myui merged commit 375913e into master Sep 6, 2016
@myui myui deleted the feature/changefinder1d branch September 6, 2016 13:12
@myui
Copy link
Owner Author

myui commented Sep 6, 2016

@L3Sota @takuti merged into master. Thank you for the contributions!

@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 622a793 on feature/changefinder1d into * on master*.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants