#include <stdio.h>
#include "invpacktree_int.h"
#include "packtree_int.h"
#include "support.h"
#include "delta.h"
#include "line_int.h"
#include "costwidth.h"
#include "yahooTS.h"
Go to the source code of this file.
Functions | |
| void | copy (int *destVec, int *srcVec, const size_t N) |
| Make a copy of an integer array. More... | |
| size_t | calcPacketWidth (packdata_list< int > &bestBasisList) |
| Calculate the number of bits needed to represent the result of the wavelet packet transform. More... | |
| size_t | packet_calc (const int *intVec, const int N) |
| Wavelet packet calculation. More... | |
| void | rollingCompressWindow (int *intVec, const size_t N, const size_t winSize) |
| Iterate over the input vector and copy a window of data into a temporary array. More... | |
| void | printTS (const double *ts, const size_t N) |
| Print the original floating point time series. More... | |
| double* | calcReturnSeries (double *ret, const double *ts, const size_t N, const size_t period) |
| Calculate a simple return time series over period trading days. More... | |
| int | main () |
| Read in a set of equity time series file. More... | |
Here a power of two window is chosen, which is smaller that the time series. For example, the time series might contain 563 values and a window of 64 elements might be chosen.
Wavelet packet compression is applied to the window at the start of the data set, resulting in the number of bits needed to represent the windowed data set. The window is then moved forward one element and the algorithm is applied again. If the data set size is N and the Window set size is W, this will result in (N - W) points.
The amount of compression in the window region reflects how close the wavelet packet function fits the data set. If a forecasting function is related to the wavelet function, the amount of compression in the region also reflects the accuracy of the forecasting function. To test this, a simple linear interpolation function is used.
The documentation in this file is formatted for doxygen (see www.doxyeng.org).
You may use this source code without limitation and without fee as long as you include: This software was written and is copyrighted by Ian Kaplan, Bear Products International, www.bearcave.com, 2002.
This software is provided "as is", without any warranty or claim as to its usefulness. Anyone who uses this source code uses it at their own risk. Nor is any support provided by Ian Kaplan and Bear Products International.
Please send any bug fixes or suggested source changes to:
iank@bearcave.com
Definition in file rollingCompress.cpp.
|
|
Calculate the number of bits needed to represent the result of the wavelet packet transform. The result of this version of the wavelet packet transform is a list of vectors, where each vector represents a best basis fit for a particular region of the signal. The result returned here is an obvious lower bound, since in practice a vector would consist of a fixed set of values. Also the length of the vector would have to be included, along with the width the the length values. Definition at line 93 of file rollingCompress.cpp. Referenced by packet_calc().
00094 {
00095 packdata_list<int>::handle h;
00096 size_t totalWidth = 0;
00097
00098 for (h = bestBasisList.first(); h != 0; h = bestBasisList.next( h )) {
00099 packdata<int> *node = bestBasisList.get_item( h );
00100 const size_t len = node->length();
00101 const int *vec = node->getData();
00102 int nodeWidth = support::vecWidth( vec, len );
00103 totalWidth += nodeWidth;
00104 }
00105
00106 return totalWidth;
00107 } // calcPacketWidth
|
|
|
Calculate a simple return time series over period trading days.
Definition at line 196 of file rollingCompress.cpp. 00200 {
00201 } // calcReturnSeries
|
|
|
Make a copy of an integer array. Note that the function allocates memory, but does not deallocate it. Definition at line 70 of file rollingCompress.cpp. Referenced by main(), and rollingCompressWindow().
00071 {
00072 for (size_t i = 0; i < N; i++)
00073 {
00074 destVec[i] = srcVec[i];
00075 }
00076 } // copy
|
|
|
Read in a set of equity time series file. Calculate the number of bits needed to represent the data without compression and after wavelet and wavelet packet compression. Definition at line 210 of file rollingCompress.cpp. 00211 {
00212 const char *files[] = { "aa", // Alcoa Aluminium
00213 "amat", // Applied Materials
00214 "ba", // Boeing
00215 "cof", // Capital One
00216 "ge", // General Electric
00217 "ibm", // IBM Corp.
00218 "intc", // Intel
00219 "mmm", // 3M
00220 "mrk", // Merck
00221 "wmt", // Wal-Mart
00222 0 // The null pointer
00223 };
00224
00225 const size_t N = 563;
00226 const size_t winSize = 64;
00227 const char *fileName = "amat";
00228 double realVec[ N ];
00229 int intVec[ N ];
00230 int window[ winSize ];
00231
00232 // an instance of yahooTS with the path to the data directory
00233 const char *dataDirPath = "..\\data\\equities\\";
00234 yahooTS ts( dataDirPath );
00235
00236 size_t n = N;
00237 if (! ts.getTS( fileName, realVec, n, yahooTS::Close )) {
00238 printf("error reading file %s\n", fileName );
00239 return 0;
00240 }
00241
00242 if (n != N) {
00243 printf("Error: %d out of %d data elements read\n", n, N );
00244 return 0;
00245 }
00246
00247 support::decimalToInt( intVec, realVec, N );
00248
00249 rollingCompressWindow( intVec, N, winSize );
00250
00251 return 0;
00252 }
|
|
|
Wavelet packet calculation. The function returns the minimal total number of bits needed to represent the data when compressed using the wavelet packet algorithm. Definition at line 119 of file rollingCompress.cpp. Referenced by main(), and rollingCompressWindow().
00121 {
00122 // The "line" wavelet transform (e.g., line with slope)
00123 line_int<packcontainer_int> line;
00124
00125 // calculate the wavelet packet tree, using the line wavelet transform
00126 packtree_int tree( intVec, N, &line );
00127
00128 // get the root of the wavelet packet transform tree
00129 packnode<int> *treeRoot = tree.getRoot();
00130
00131 // Assign a cost on the basis of bit width
00132 costwidth cost( treeRoot );
00133
00134 // Calculate the "best basis" function from the tree
00135 tree.bestBasis();
00136
00137 // Get the best basis list. This will be a list of
00138 // nodes consisting of the best basis set. This set is
00139 // obtained by traversing the tree, top down, left to
00140 // right.
00141 packdata_list<int> bestBasis = tree.getBestBasisList();
00142
00143 // Sum the cost values (width for each node in the best basis
00144 // list
00145 size_t width = calcPacketWidth( bestBasis );
00146
00147 return width;
00148 } // packet_calc
|
|
|
Print the original floating point time series.
Definition at line 183 of file rollingCompress.cpp. 00184 {
00185 for (size_t i = 0; i < N; i++) {
00186 printf("%7.4f\n", ts[i] );
00187 }
00188 } // printTS
|
|
|
Iterate over the input vector and copy a window of data into a temporary array. Calculate the percentage commpression after wavelet packet compression and print this value. Percentage compression gives a normalized value. If the absolute number of bits is used, as the stock price gets smaller (or larger) it requires fewer (or more) bits. Using this value would be misleading, since it depends on the time series values. Definition at line 162 of file rollingCompress.cpp. Referenced by main().
00165 {
00166 int *window = new int[ winSize ];
00167
00168 for (size_t i = 0; i < N - winSize; i++) {
00169 copy( window, &intVec[i], winSize );
00170 const size_t windowWidth = support::vecWidth( window, winSize );
00171 const size_t packetWidth = packet_calc( window, winSize );
00172 const float compPercent = (1.0 - ((float)packetWidth/(float)windowWidth)) * 100.0;
00173 printf("%7.4f\n", compPercent );
00174 }
00175
00176 delete [] window;
00177 } // rollingCompressWindow
|
1.2.8.1 written by Dimitri van Heesch,
© 1997-2001