Haar Wavelets ...a continuation of Part I

Given some mutually orthogonal set of basis functions u1(t), u2(t), u3(t), ... we hope to represent an arbitrary function f(t) like so:
[1]     f(t) = (fu1) u1(t) + (fu2) u2(t) + (fu3) u3(t) + ... = Σ(fuj) uj(t)    
        where (fu) = f(t) u(t) dt is an "appropriate" dot or scalar product
        and the sum Σ is from j = 0 to j = infinity.
The function f(t) will be a sequence of n stock prices.

Here we'll take as our basis the functions illustrated
We note several things:

  • u1 generates two "daughters", namely u2 and u3.
  • These two daughters are each half-size.
  • The second, namely u3, is shifted so that, together, they cover the interval t = 0 to t = n.
  • Each daughter generates two more daughters.
  • u2, which covers the interval (0, n/2), generates two half-sized & shifted u4 and u5.
  • u3, which covers the interval (n/2, n), generates two half-sized & shifted u6 and u7.
  • All four (u4, u5, u6 and u7) cover the entire interval (0, n).
  • This ritual may be continued indefinitely, each daughter function generating two additional functions.
  • After N such iterations, the final set will contain 2N functions.
>If u1 generates all those daughters and granddaughters, she should be called mother!
As a matter of fact, that basis function IS called the mother.

Note, the following neat characteristics:

  1. If j ≠ k, then uj(t) uk(t) dt = 0   where the integration is over (0,n).
  2. If j = k, then uj(t) uj(t) dt = uj2(t) dt = n or n/2 or n/4 or n/8 etc..
>So are they orthonormal?
Well, they're orthogonal because of property #1.
However, the integral uj2(t) dt depends upon j, hence the interation number.
On interation #1, uj2(t) dt = n/2.   On interation #2, uj2(t) dt = n/4.   On iteration #3 ...

>It'd be ... uh, n/8 and on iteration #N it'd be n/2N, right?
Right.

 
Figure 1
These iterations are sometimes called dilitation levels. Note that, at each iteration, the basis functions sample greater detail in the function f(t).

>Huh?
Let's apply [1] above to a sequence of 8 daily stock prices, namely: P1, P2, P3, P4, P5, P6, P7 and P8.
Further, from these 8 discrete values, let's make a function, f(t), as in Figure 2.
It's piecewise constant and it reflects the 8 stock prices and ...

>Wait! I never see prices plotted like that!
Yes, I know. You are more apt to see this:

Nevertheless, the two charts are equivalent, so we'll continue and write:
f(t) = (fu1) u1(t) + (fu2) u2(t) + (fu3) u3(t) + (fu4) u4(t) + (fu5) u5(t) + (fu6) u6(t) + (fu7) u7(t)


Figure 2
We note the following, for an "integral" scalar product: f(t) u(t) dt.
Since our f(t) and each of our basis functions are piecewise constant, the integration reduces a simple sum of products.

>Huh?

Okay, suppose that our daily stock-price function, f(t) (over 8 days), has components P1, P2, P3 ... P8.
Not that u5(t) also consists of 8 components, namely a=0, b=0, c=1,d=-1, e=0, f=0, g=0, h=0.
We integrate their product, which will also have just 8 components, namely: (0)*P1, (0)*P2, (1)*P3, (-1)*P4, (0)*P5, (0)*P6, (0)*P7, (0)*P8.
The integral then reduces to (0)*P1Δt + (0)*P2Δ t + (1)*P3Δt + (-1)*P4Δt + (0)*P5Δt + (0)*P6Δt + (0)*P7Δt + (0)*P8Δt
where Δt is the length of the subinterval: in our case, it's n/8.

Concusion? Except for the ubiquitous Δt, the integral scalar product is just the ordinary, garden-variety vector dot product:
(fu5) = a P1 + b P2 + c P3 + d P4 + e P5 + f P6 + g P7 + h P8 .

When we do all this, we'll be subdividing n days into single days so if n = 8, then Δt = 8/8 = 1 so we can ignore Δt.
Indeed, when we consider a year's worth of daily prices, n = 256 (since there are about market 256 days in a year) so our f(t) as well as each basis function (or vector) will have 256 components.
For f(t), the components will be the 256 stock prices. For the basis vectors they'll be either +1, -1 or 0.

Now we're going to take take the dot product of our f(t) with each of u1, u1, etc. and ...

>Can you use numbers instead of symbols? That'd make it easier because ...
Okay, let's take the specific f(t) in Figure 2, namely the sequence of prices: $30, $31, $29, $27, $29, $32, $34 and $35.
To take the dot product of this f(t) with each of u1, u2, etc. we multiply each stock price by the corresponding u-value and add:

  1. fu1 = 30*(1) + 31*(1) + 29*(1) + 27*(1) + 29*(-1) + 32*(-1) + 34*(-1) + 35*(-1) = -13   (since the 8 u1-values are 1, 1, 1, 1, -1, -1, -1, -1).
  2. fu2 = 30*(1) + 31*(1) + 29*(-1) + 27*(-1) + 29*(0) + 32*(0) + 34*(0) + 35*(0) = 5   (since the u2-values are 1, 1, -1, -1, 0, 0, 0, 0).
  3. fu3 = 30*(0) + 31*(0) + 29*(0) + 27*(0) + 29*(1) + 32*(1) + 34*(-1) + 35*(-1) = -8   (since the u3-values are 0, 0, 0, 0, 1, 1, -1, -1).
  4. etc. etc.
We end up with the 7 dot products: -13, 5, -8, -1, 2, -3 and -1.

>Let me do it! I'd say that: f(t) = (-13)u1 + (5)u2 + (-8)u3 + (-1)u4 + (2)u5 + (-3)u6 + (-1)u7. Am I right?
Close, but no cigar. First off, this basis is orthogonal but not normal. That is, uu ≠ 1.

Indeed, if we write f(t) = a1 u1(t) + a2 u2(t) + a3u3(t) + a4u4(t) + a5u5(t) + a6u6(t) + a7 u7(t)
then take the dot product of each side with, say, u5(t), then we'd get:
fu5 = 0 + 0 + 0 + 0 + a5 (u5u5) + 0 + 0.
All those 0s are due to orthogonality. Alas, because our u-function basis isn't orthonormal, then (u5u5) ≠ 1.

No problemo! We'd find that a5 = (fu5) / (u5u5)

Okay, so we just divide each (fu) by (uu), noting the following: (as in Figure 1)

  1. u1u1 = (1)*(1) + (1)*(1) + (1)*(1) + (1)*(1) + (-1)*(-1) + (-1)*(-1) + (-1)*(-1) + (-1)*(-1) = 8
  2. u2u2 = (1)*(1) + (1)*(1) + (-1)*(-1) + (-1)*(-1) + (0)*(0) + (0)*(0) + (0)*(0) + (0)*(0) = 4
  3. etc. etc.
We'd get the 7 dot products: 8, 4, 4, 2, 2, 2, 2.
Dividing the 7 (fu) by the 7 (uu) gives the coefficients we're looking for, namely: -1.625, 1.25, -2.0, -0.50, 1.0, -1.50, -0.50.

>Okay!! I'd say that: f(t) = (-1.625)u1 + (1.25)u2 + (-2.0)u3 + (-0.50)u4 + (1.0)u5 + (-1.50)u6 + (-0.50)u7. Am I right?
If you're right, then we should be able to generate our f(t) (as in Figure 2) by plotting (-1.625)u1 + (1.25)u2 + (-2.0)u3 + (-0.50)u4 + (1.0)u5 + (-1.50)u6 + (-0.50)u7.
In fact, the plot looks like this:

>I'd say that Haar is out to lunch! It doesn't look anything like f(t) and ...
If you look carefully, you'll notice that this graph looks like a set of deviations from some magic price-value.
In fact, if you think about it, all them u-functions have a zero mean value since they're just as often (-1) as (+1). They'd be great if f(t) had a zero mean value, but our f(t) doesn't and ...

>So add the mean value of f(t) to that chart. See what you get.
The average (or mean) value of our 8 stock prices is (1/8)( 30+31+29+27+29+32+34+35) = 30.875
... and if we add this to each value in that last chart we get SURPRISE! Figure 2.

>So what's the real, correct and no-nonsense series for f(t)?
It's
[2]     f(t) = a0 + a1 u1(t) + a2 u2(t) + a3 u3(t) +...
where a0 is the Mean value of f(t) and ak = (fuk) / (ukuk)
>I was thinking ... if u1 is called "mother", then u0 should be called "father", because she does all the work and he just sits around!
Or because he's the head of the household? Actually, I think that zeroth basis vector IS called father!

>Okay, if I had a year's worth of daily stock prices, how many basis vectors would I need?
You'd need 256, of course. In fact, you'll need a power of 2, regardless of the time frame: 128 or 256 or 512 or 1024 etc. etc.

>Mamma mia! That's a lot of work! I assume you have a spreadsheet.
Patience ... there's an easier way to calculate the coefficients.

Check out this which illustrates how we can calculate our coefficients:

30.875-1.625 1.25-2.0 -0.501.0-1.50-0.50

The coefficients are calculated by taking Averages and Differences of successive pairs of Stock Prices ... or any sequence of numbers.
>And then ... what?
Good question ...


Transforming the Time Series
  • We start with P(t) = a0 u0(t) + a1 u1(t) + a2 u2(t) + a3 u3(t) +... + an un(t)
    where P(t) is the sequence of n stock prices ... a power of 2, like n = 2m
    and P(t) = P1 for 0 < t < 1, P(t) = P2 for 1 < t < 2, P(t) = P3 for 2 < t < 3 ...
    (a piecewise constant function, as illustrated in Figure 3)
    and the us are the basis functions, with u0(t) = 1 for 0 < t < n.
  • The coefficients are calculated by multiplying P(t) by the basis functions
    (shown in Figure 1 and repeated here, for convenience)
    then dividing (as indicated in [2]).
  • [3]
    We note that ... ignoring, for the moment, the division by (ukuk) :
    • (u0u0)a0 = P1+P2+P3+ ... +Pn
    • (u1u1) a1 = (Pu1) = (P1+P2+...+Pn/2) - (Pn/2+1+Pn/2+2+...+Pn)
    • (u2u2) a2 = (Pu2) = (P1+P2+...+Pn/4) - (Pn/4+1+Pn/4+2+...+Pn/2)
    • (u3u3) a3 = (Pu3) = (Pn/2+1 +...+ P3n/4) - (P3n/4+1 +...+ Pn)
    • (u4u4) a4 = (Pu4) = (P1 +...+ Pn/8) - (Pn/8+1 +...+ Pn/4)
    • (u5u5) a5 = (Pu4) = (Pn/4 +...+ Pn/4+n/8) - ...

    >Okay! I get it!You're always adding the sum of the first set of prices
        and subtracting the sum of the second set of prices.

    Yes, and ignoring those prices where the basis funtion is zero.

  • [4]
    Note, too, that:
      (uouo) = n
    • (u1u1) = n
      since the (basis function)2 = 1 on the entire time interval
    • (u2u2) = (u3u3) = n/2
      since the (basis functions)2 = 1 on only 1/2 of the time interval
    • (u4u4) = (u5u5) = (u6u6) = (u7u7) = n/4
      (basis functions)2 = 1 on 1/4 of the time interval
    • The next 8 give (uu) = n/8
    • The next 16 give (uu) = n/16
    • etc. etc.

Figure 3


Figure 1

To see the pattern, consider just 8 stock prices.
The right-sides of [3] are equal to the matrix product:
          This matrix can be inverted and applied to the left-side of [4] to recover the prices.
          The inverse is:
         
Do you see the pattern?
>zzz

to continue