Sowiks commented on code in PR #96:
URL: https://github.com/apache/otava/pull/96#discussion_r2531586585


##########
otava/change_point_divisive/calculator.py:
##########
@@ -0,0 +1,189 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+
+import numpy as np
+from numpy.typing import NDArray
+
+from otava.change_point_divisive.base import Calculator, CandidateChangePoint
+
+
+class PairDistanceCalculator(Calculator):
+    def __init__(self, series: NDArray, power: int = 1):
+        super().__init__(series)
+        assert 0 < power < 2, f"power={power} isn't in (0, 2)"
+        self.power = power
+        self.V = None
+        self.H = None
+
+    def _calculate_distances(self):
+        '''Precomputes `H` and `V` functions that are used for computation of 
`Q` function.
+            See more details about `Q` function in 
`get_candidate_change_point` method.
+            See more details about the use of `H` and `V` functions in 
`_get_A_B_C` method.
+
+        Let matix `distances` be a matrix of all possible L1 (Manhattan) 
distances between
+        elements of 1d array `series`. If `D(i, j) = |series[i] - series[j]|` 
and `N = len(series)`,
+        then
+
+                      | D(0, 0)        D(0, 1)      …       D(0, N - 1) |
+                      | D(1, 0)        D(1, 1)      …       D(1, N - 1) |
+        distances =   |    ⋮              ⋮          ⋱         ⋮        | .
+                      | D(N - 1, 0)    D(N - 1, 1)  …   D(N - 1, N - 1) |
+
+        Note that the main diagonal of the matrix is filled with 0.
+
+
+        We define functions `V` and `H` as the following (V)ertical and 
(H)orizontal
+        cummulative sums:
+                        V(start, τ, κ) = Σ distances[start:τ, τ],
+                        H(start, τ, κ) = Σ distances[τ, τ:κ],
+        for `start < τ < κ <= N`.
+        Note that `V(start, τ, κ) = V(start, τ)` and `H(start, τ, κ) = H(τ, 
κ)`.
+
+
+        Vector V contains the following values of function `V(start, τ, κ)`:
+                            V = [V(0, 1, κ)     V(0, 2, κ),    …  V(0, N - 1, 
κ)].
+            `V(0, 0, κ) = 0` so they are ommited. As noted, function `V(start, 
τ, κ)`
+            does not depend on `κ`. Therefore, vector V contains all values of 
function
+            `V(0, τ, κ)`. We can easily get values of function `V` for an 
arbitrary
+            value of `start` by
+                            V(start, τ, κ) = V(0, τ, κ) - Σ distances[0 : 
start, τ].
+        Note: The reason not all values of `V(start, τ, κ)` are precomputed is 
that
+              we do not need them all. The values of `start` will depend on 
critical
+              points we find. Precomuting them for all possible values is a 
waste.
+
+
+        Matrix H contains the following values of function `H(start, τ, κ)`:
+
+                  | H(start, 0, 2)    H(start, 0, 3)   …  H(start, 0, N)     |
+                  |          0        H(start, 1, 3)   …  H(start, 1, N)     |
+            H =   |          ⋮                 ⋮        ⋱          ⋮         | 
.
+                  |          0                 0       …  H(start, N - 2, N) |
+
+            `H(start, x, x) = H(start, x - 1, x) = 0` for any `x` so they are 
ommited.
+            As noted, function `H(start, τ, κ)` does not depend on `start`. 
Therefore,
+            matrix H contains all possible values of function `H`.
+        Note: We precomputed all values of `H(start, τ, κ)` because all of 
them are needed
+              for the very first iteration (`start=0` and `end=N-1`).'''
+        self.distances = np.power(np.abs(self.series[:, None] - 
self.series[None, :]), self.power)
+        triu = np.triu(self.distances, k=1)[:-1, 1:]
+        self.V = triu.sum(axis=0)
+        self.H = triu.cumsum(axis=1)
+
+    def _get_Q_vals(self, start: int, end: int) -> NDArray:
+        '''Computes matrices A, B, C where all possible values of function `Q` 
are
+        given by matrix Q = A - B - C.
+        See more details about `Q` function in `get_candidate_change_point` 
method.
+
+        For any given `start` and `end` let
+                        Q(τ, κ) = A(start, τ, κ) - B(start, τ, κ) - C(start, 
τ, κ),
+        where `start < τ < κ <= end + 1`. (For definitions of `A`, `B`, `C` see
+        formula of `Q` in `get_candidate_change_point` method.) All possible 
values of function
+        `A` are given by matrix
+
+              | A(start, start + 1, start + 2)    A(start, start + 1, start + 
3)   …  A(start, start + 1, end + 1) |
+              |                0                  A(start, start + 2, start + 
3)   …  A(start, start + 2, end + 1) |
+        A =   |                ⋮                                  ⋮            
     ⋱                 ⋮            | .
+              |                0                                  0            
    …  A(start, end, end + 1)       |
+
+        Matrices B and C follow the same index-to-value pattern.
+
+        Matrices A, B, and C are used to compute values of function `Q` 
recursively, without
+        recomputing the same sums over and over again. The recursive formulas 
were further
+        transformed to closed forms, so they can be computed using numpy 
cummulative sum
+        function to take advantage of numpy vectorized operations. (Note that 
each column
+        in matrices A, B, C can be repersented through np.cumsum). The 
formulas use
+        commulative sum of functions `H` and `V`, which definitions can be 
found in
+        `_calculate_distances` method.
+
+            A(start, τ, κ) = 2 / (κ - s) * (Σt=start,τ-1  H(start, t, κ) - 
Σt=start+1,τ-1  V(start, t, κ)),
+
+            B(start, τ, κ) = 2 * (κ - τ) / (κ - start) / (τ - start - 1) * 
Σt=start+1,τ-1  V(start, t, κ),
+
+            C(start, τ, κ) = 2 * (τ - start) / (κ - start) / (κ - τ - 1) * 
Σt=τ,κ-2  H(start, t, κ).'''
+        if self.V is None or self.H is None:
+            self._calculate_distances()
+
+        V = self.V[start : end] - self.distances[0 : start, start + 1 : end + 
1].sum(axis=0)
+        H = self.H[start : end, start : end]
+
+        taus = np.arange(start + 1, end + 1)[:, None]
+        kappas = np.arange(start + 2, end + 2)[None, :]
+
+        A = np.zeros((end - start, end - start))
+        A_coefs = 2 / (kappas - start)
+        A[1:, :] = np.cumsum(V)[:-1, None]
+        A = A_coefs * np.triu(np.cumsum(H, axis=0) - A, k=0)
+
+        B = np.zeros((end - start, end - start))
+        B_num = 2 * (kappas - taus)
+        B_den = (kappas - start) * (taus - start - 1)
+        B_mask = np.triu(np.ones_like(B_den, dtype=bool), k=0)
+        B_out = np.zeros_like(B_den, dtype=float)
+        B_coefs = np.divide(B_num, B_den, out=B_out, where=B_mask & (B_den != 
0))
+        B[1:, 1:] = B_coefs[1:, 1:] * np.cumsum(V)[:-1, None]
+
+        C = np.zeros((end - start, end - start))
+        C_num = 2 * (taus - start)
+        C_den = (kappas - start) * (kappas - taus - 1)
+        C_mask = np.triu(np.ones_like(C_den, dtype=bool), k=1)
+        C_out = np.zeros_like(C_den, dtype=float)
+        C_coefs = np.divide(C_num, C_den, out=C_out, where=C_mask & (C_den != 
0))
+        C[:-1, 1:] = C_coefs[:-1, 1:] * np.flipud(np.cumsum(np.flipud(H[1:, 
1:]), axis=0))
+
+        # Element of matrix `Q_{i, j}` is equal to `Q(τ, κ) = Q(i + 1, j + 2) 
= QQ(sequence[start : i + 1], sequence[i + 1 : j + 2])`.
+        # So, critical point is `τ = i + 1`.
+        return A - B - C
+
+    def get_candidate_change_point(self, interval: slice) -> 
CandidateChangePoint:
+        '''For a given `slice(start, stop)` finds potential critical point in 
subsequence series[slice],
+        i.e., from index `start` to `stop - 1` inclusive. For simplicity, 
we'll use `end = stop - 1`.

Review Comment:
   Will do



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to