Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

rollingCompress.cpp

Go to the documentation of this file.
00001 
00052 
00053 
00054 #include <stdio.h>
00055 
00056 #include "invpacktree_int.h"
00057 #include "packtree_int.h"
00058 #include "support.h"
00059 #include "delta.h"
00060 #include "line_int.h"
00061 #include "costwidth.h"
00062 #include "yahooTS.h"
00063 
00064 
00070 void copy( int *destVec, int *srcVec, const size_t N )
00071 {
00072   for (size_t i = 0; i < N; i++)
00073   {
00074     destVec[i] = srcVec[i];
00075   }
00076 } // copy
00077 
00078 
00079 
00093 size_t calcPacketWidth( packdata_list<int> &bestBasisList )
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
00108 
00109 
00110 
00119 size_t packet_calc( const int *intVec, 
00120                     const int N )
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
00149 
00150 
00162 void rollingCompressWindow(int *intVec, 
00163                            const size_t N,
00164                            const size_t winSize )
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
00178 
00179 
00183 void printTS(const double *ts, const size_t N )
00184 {
00185   for (size_t i = 0; i < N; i++) {
00186     printf("%7.4f\n", ts[i] );
00187   }
00188 } // printTS
00189 
00190 
00191 
00196 double *calcReturnSeries( double *ret, 
00197                           const double *ts, 
00198                           const size_t N,
00199                           const size_t period )
00200 {
00201 } // calcReturnSeries
00202 
00203 
00210 int main()
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 }

Generated at Tue May 27 21:56:16 2003 for Wavelet compression, determinism and time series forecasting by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001