Docsity

Sequence Matching and Fourier Transforms, Study notes of Distributed Database Management Systems

Toggle sidebar
bg1
9 Sequence Matching
Sequences are lists of values
S
=(
x
1
;x
2
;:::;x
k
), although we shall often think of the same sequence as a
continuous function dened on the interval 0-to-1. That is, the sequence
S
can be thought of as sample
values from a continuous function
S
(
t
), with
x
i
=
S
(
i=k
).
9.1 Sequence-Matching Problems
In the simplest case, we are given a collection of sequences
f
S
1
;S
2
;:::;S
n
g
, and a query sequence
Q
, each
of the same length. Our problem is to nd that sequence
S
i
whose
distance
from
Q
is the minimum, where
\distance" is dened by the \energy" of the dierence of the sequences; i.e.,
D
(
S; T
)=
R
1
0
,
S
(
t
)
,
T
(
t
)
2
dt
.
For instance, the
S
i
's might be records of the prices of various stocks, and
Q
is the price of IBM stock,
delayed by one day.Ifwe found some
S
i
that was very similar to
Q
,we could use the price of the stock
S
i
to predict the price of IBM stock the next day, Notes:
Do not try this at home. Anything easy to mine about stock prices is already being done, and the
market has adjusted to whatever knowledge can be gleaned.
Sequence matching is a great opportunity to violate the Bonferroni principal, since there has to be a
\closest sequence." For instance, a famous mistakewas lo oking in the UN book of world statistics to
nd the statistic that best predicted the Dow-Jones average. It was \cotton production in Bangladesh."
9.2 Fourier Transforms as Indexes for Sequences
We could treat sequences of length
k
as points in a
k
-dim vector space, but doing so is not likely to be useful.
Usually,
k
will be so high, that spacial index techniques like
kd
-trees or
R
trees will be useless.
The trick adopted byFaloutsos and his colleagues is to map sequences to the rst few terms of their
Fourier transforms. Formally, the
j
th term of the
Fourier Expansion
of the function
S
(
t
)is
R
1
0
S
(
t
)
e
2
jit
dt
.
Recall that the imaginary exponential
e
i
x
is dened to be sin
x
+
i
cos
x
.Thus, the real and imaginary parts
of
X
j
tell howwell
S
(
t
) matches sine and cosine functions that have
j
periods within the interval 0-to-1, i.e.,
sin 2
jt
and cos 2
jt
.
Example 9.1 :
Figure 25 suggests a simple function
S
(
t
) (solid) and compares it to the single-period sine
function (dotted) and single-period cosine function (dashed). The integral of the product
S
(
t
) sin 2
t
+
iS
(
t
) cos 2
t
is the complex number
X
1
.
2
Figure 25: Function
S
(
t
) matches the sine only slightly; the cosine better but not perfectly
Key point:
Parsival's Theorem
states that the energy in a signal
S
(
t
) is the same as the energy in its
Fourier transform:
P
i
0
j
X
i
j
2
. Note
j
X
j
is the magnitude of a complex number
X
.
38
docsity.com
pf3
pf4

Partial preview of the text

Download Sequence Matching and Fourier Transforms and more Study notes Distributed Database Management Systems in PDF only on Docsity!

9 Sequence Matching

Sequences are lists of values S = (x 1 ; x 2 ; : : : ; xk ), although we shall often think of the same sequence as a continuous function de ned on the interval 0-to-1. That is, the sequence S can b e thought of as sample values from a continuous function S (t), with xi = S (i=k ).

9.1 Sequence-Matching Problems

In the simplest case, we are given a collection of sequences fS 1 ; S 2 ; : : : ; Sn g, and a query sequence Q, each

of the same length. Our problem is to nd that sequence Si whose distance from Q is the minimum, where \distance" is de ned by the \energy" of the di erence of the sequences; i.e., D (S; T ) =

R 1

0

S (t) T (t)

dt. For instance, the Si 's might b e records of the prices of various sto cks, and Q is the price of IBM sto ck, delayed by one day. If we found some Si that was very similar to Q, we could use the price of the sto ck Si to predict the price of IBM sto ck the next day, Notes:

 Do not try this at home. Anything easy to mine ab out sto ck prices is already b eing done, and the

market has adjusted to whatever knowledge can b e gleaned.

 Sequence matching is a great opp ortunity to violate the Bonferroni principal, since there has to b e a

\closest sequence." For instance, a famous mistake was lo oking in the UN b o ok of world statistics to nd the statistic that b est predicted the Dow-Jones average. It was \cotton pro duction in Bangladesh."

9.2 Fourier Transforms as Indexes for Sequences

We could treat sequences of length k as p oints in a k -dim vector space, but doing so is not likely to b e useful. Usually, k will b e so high, that spacial index techniques like k d-trees or R trees will b e useless. The trick adopted by Faloutsos and his colleagues is to map sequences to the rst few terms of their Fourier transforms. Formally, the j th term of the Fourier Expansion of the function S (t) is

R 1

0 S^ (t)e

2  j it (^) dt.

Recall that the imaginary exp onential ei^ x is de ned to b e sin x + i cos x. Thus, the real and imaginary parts of Xj tell how well S (t) matches sine and cosine functions that have j p erio ds within the interval 0-to-1, i.e., sin 2  j t and cos 2  j t.

Example 9.1 : Figure 25 suggests a simple function S (t) (solid) and compares it to the single-p erio d sine function (dotted) and single-p erio d cosine function (dashed). The integral of the pro duct S (t) sin 2  t + iS (t) cos 2  t is the complex numb er X 1. 2

Figure 25: Function S (t) matches the sine only slightly; the cosine b etter but not p erfectly

 Key p oint: Parsival's Theorem states that the energy in a signal S (t) is the same as the energy in its

Fourier transform:

P

i 0 jXi^ j

2. Note jX j is the magnitude of a complex numb er X.

 Key application: If S (t) is actually the di erence of two sequences, then the distance b etween those

sequences, which is

R 1

0 S^

(^2) (t)dt is the same as the sum of the squares of the magnitudes of the di erences of the Fourier co ecients of the two sequences.

 Since the square of a magnitude is always p ositive, we can nd all the sequences whose distance from

a given query sequence Q is no more than ^2 by nding all those sequences S the sum of whose magnitudes of the di erences of the rst m Fourier co ecients is no more than ^2.

{ We shall retrieve some \false drops" | sequences that are close to Q in their rst m co ecients, but di er greatly in their higher co ecients. { There is some justi cation for the rule that \all the energy is in the low-order Fourier co ecients," so retrieval based on the rst few terms is quite accurate in practice.

9.3 Matching Queries to Sequences of the Same Length

As long as stored sequences and queries are of the same length, we can arrange the sequences in a simple, low-dimension data structure, as follows:

  1. Compute the rst m Fourier co ecients of each sequence Si , for some small, xed m. It is often sucient to use m = 2 or m = 3. Since Fourier co ecients are complex, that is the equivalent of a 4- or 6-dim space.
  2. Place each sequence Si in this space according to the values of its rst m Fourier co ecients. The p oints themselves are stored in some suitable multidimensional data structure.
  3. To search for all sequences whose distance from query Q is no more than ^2 , compute the rst m Fourier co ecients of Q, and lo ok for p oints in the space no more distant than  from the p oint corresp onding to Q.
  4. Since there are false drops, compare each retrieved sequence with Q to b e sure that the distance is truly no greater than ^2.

In some applications, it may make sense to \normalize" sequences and queries to make the 0th Fourier co ecient (which is the average value) 0 and p erhaps to make the variance (i.e., the \energy") of each sequence the same.

Example 9.2 : In Fig. 26(a) are two sequences that have a large di erence, but are in some sense the same sequence, shifted and scaled vertically. We could shift them vertically to make their averages b e 0, as in Fig. 26(b). However, that change still leaves one sequence always having twice the value of the other. If we also scaled them by multiplying the more slowly varying by 2, they would b ecome the same sequence. 2

9.4 Queries That are Shorter than the Sequences

Assume that queries are at least of length w. The sequential scan metho d for nding close (within distance ^2 ) matches to a query Q is as follows:

  1. Store the rst m co ecients of the Fourier transform of each subsequence of length w for each sequence.

As a result, if sequences are of length k , we store k w + 1 p oints for each sequence.

  1. Given a query Q, take the rst w values of Q and compute the Fourier transform of that sequence of length w. Retrieve each sequence S and p osition p that matches within .
  2. For each such S and p, compare the entire query Q with sequence S starting at p osition p. If the distance is no more than ^2 , rep ort the match.

(3=4)(5=6) = 5 =8. Start from the b eginning of the trail, and form rectangles by adding p oints as we treavel along the trail. When deciding whether or not to add another p oint to the current rectangle, make sure that the cost of the new rectangle p er p oint covered decreases. If it increases, then start a new rectangle instead.

9.6 Matching Queries of Arbitrary Length

If the query Q has length w , just nd the rectangles within distance  from Q, as discussed in Section 9.5. Note that Q may actually b e within a rectangle, and even so, Q may b e distant from actual p oints on the trail, as suggested in Fig. 28.

Q

Q

1

2

Figure 28: Two examples of queries near a rectangle; note that the query inside the rectangle is actually further from the p oints on the trail than the query outside the rectangle, but b oth need to b e considered

However, if the query Q has length greater than w , say pw for some constant p > 1, there are several things we can do:

  1. Search for just the rst w values of Q, retrieve the matching sequences and their p ositions, and then compare the entire Q with the sequence starting at that p osition. This metho d is like sequential scan, but it takes advantage of the storage eciency of trails.
  2. Search for the rectangles that are suciently close (within ) to the rst w values of Q, the next w values of Q, and so on. Only a sequence that has at least one rectangle that is within  of each of these subsequences of Q is a candidate match. Check the candidate matches.
  3. Like (2), but check a sequence S only if it has a rectangle within distance =

p

p from at least one of the p subsequences of Q. Note that the distance of sequences must b e at most ^2 , and if all p subsequences of Q are at least =

p

p from any p oint on the trail of S , then p(=

p

p)^2 = ^2 is a lower b ound on the distance b etween Q and any subsequence of S.