Wavelets ... and a simple calculation of Haar Coefficients
An appendix to Wavelets-2
|
The problem: Generate these 8 Haar Coefficients coefficients via some simple procedure.
| 30.875 | -1.625 |
1.25 | -2.0 |
-0.50 | 1.0 | -1.50 | -0.50
|
Okay, here's what we're going to do, in general:
- Look at a bunch of successive stock prices: P1, P2, P3, ... etc. etc.
- Look at successive pairs and calculate their Averages and Differences:
AVG[1] = (P1+P2)/2 and DIFF[1] = P1- P2
AVG[2] = (P3+P4)/2 and DIFF[2] = P3- P4
AVG[3] = (P5+P6)/2 and DIFF[3] = P6- P6
etc. etc. until we run out of pairs.
- Retain the DIFFs but look carefully at the AVGs.(Note that the number of each is 1/2 the number in the original set of prices.)
The total number of numbers is the same as in the original set of prices.
- Look at the new set of AVGs and repeat step #2. (Note that the number of AVGs will now be 1/4 the number in the original set of prices.)
There are now three sets of numbers: two DIFFs and (the latest) AVG.
The total number of numbers (all the AVG numbers + the DIFF numbers) is the same as in the original set of prices.
- Repeat the above, always retaining the DIFFs and calculating new AVGs and DIFFs using the previous AVG numbers, until ...
>That's confusing!
Let's do an example with the 8 original stock prices mentioned above:
For the first two numbers we have: AVG=(30+31)/2 = 30.5 and DIFF = 30-31 = -1.0
For the next two numbers we have: AVG=(29+27)/2 = 28 and DIFF = 29-27 = 2.0
For the next two numbers we have: AVG=(29+32)/2 = 30.5 and DIFF = 29-32 = -3.0
For the next two numbers we have: AVG=(34+35)/2 = 34.5 and DIFF = 34-35 = -1.0
When we're finished we have the following two sets of numbers ... and we note that the total number of numbers is still 8:
Okay, we put the DIFFs aside and work on the 4 AVG numbers, namely: 30.5 28 30.5 34.5
For the first pair we have: AVG =(30.5+28)/2 = 29.25 and DIFF = 30.5-28 = 2.5
For the next pair we have: AVG =(30.5+34.5)/2 = 32.5 and DIFF = 30.5 - 34.5 = -4.0
Now we have 8 numbers consisting of the latest 2 AVG numbers and all 6 of the previous DIFF numbers:
| 29.25 | 32.5 |
2.5 | -4.0 |
-1.0 | 2.0 | -3.0 | -1.0
|
>And still 8 numbers in total, eh?
Very good!
Now, for the last step (for this particular set of numbers), we ...
>Let me do it! AVG = (29.25+32.5)/2 = 30.875 and DIFF = (29.25-32.5) = -3.25
Lovely. That's the last step because we have no more pairs to Average. We have the following 8 numbers:
| 30.875 | -3.25 |
2.5 | -4.0 |
-1.0 | 2.0 | -3.0 | -1.0
|
>Dare I ask again? Why are we you doing this?
Okay, let me expand a wee bit.
Note the following:
- We must start with a number of Prices equal to 2 or 4 or 8 or 16 or ... or 2n.
For example, there will be (about) 28 = 256 numbers in a year's worth of daily stock Prices.
- For two such Prices, say p and q, the average and difference are: AVG = (p + q)/2 and DIFF = p - q.
- The AVG replaces the two numbers by their average (a "smoothing" ritual) and the DIFF picks up the variation between the two values.
If they're equal, then AVG will have that common value and DIFF will be zero.
- Given AVG and DIFF, each of the two original numbers can be recovered: p = AVG - DIFF/2 and q = AVG + DIFF/2.
- The AVG gives a "coarse" description of the two numbers p and q and DIFF provides the deviations from the coarse value.
- We can perform this AVG and DIFF ritual to each successive pair of Prices, yielding a "coarse" AVG description of the Price sequence ... and the DIFFs which what might be called the "detail".
- We apply this AVG & DIFF scheme to the AVG set of values, generating another "coarse" description of that AVG sequence ... and some finer "detail".
- We continue this algorithm until all numbers have been exhausted ... and we're left with a single AVG number and a bunch of DIFFs as "detail".
Note that the size of each successive AVG is reduced by half, which explains why we started with 2n initial Prices.
After n steps, we're finished: we will have created 2n new numbers from the initial set of 2n Prices.
Note that (surprise!) that first number, namely 30.875 = (30+31+29+27+29+32+34+35)/8 is the average of the 8 original numbers:
>Ha! That's the only one you got right! But the other numbers don't agree. The coefficients should be ...
Yes, but stare carefully at the set we started with and the set we've just generated:
| 30.875 | -1.625 |
1.25 | -2.0 |
-0.50 | 1.0 | -1.50 | -0.50
|
and
| 30.875 | -3.25 |
2.5 | -4.0 |
-1.0 | 2.0 | -3.0 | -1.0
|
See any relationship between the numbers?
>Yes, the first one is the same.
And the others differ by a factor of 2.
In fact, when we talked about expanding a function f(t) as a Haar Wavelet series with basis u1, u2 etc., we wrote:
f(t) = a0 + a1 u1(t) + a2 u2(t) + a3u3(t) +...
In fact, if we calculate the coefficients a1, a2 ... by the above scheme, we could simply write
f(t) = a0 + 2a1 u1(t) + 2a2 u2(t) + 2a3u3(t) +...
doubling each coefficient. Neat eh?
>I'm waiting for the spreadsheet ... and some indication that all this is worthwhile.
Patience ...
|