// Analysis_Timecorr::Analyze() Analysis::RetType Analysis_Timecorr::Analyze() { // If 2 vectors, ensure they have the same # of frames if (vinfo2_!=0) { if (vinfo1_->Size() != vinfo2_->Size()) { mprinterr("Error: # Frames in vec %s (%i) != # Frames in vec %s (%i)\n", vinfo1_->legend(), vinfo1_->Size(), vinfo2_->legend(), vinfo2_->Size()); return Analysis::ERR; } } // Determine sizes int frame = vinfo1_->Size(); int time = (int)(tcorr_ / tstep_) + 1; // nsteps int nsteps = 0; if (time > frame) nsteps = frame; else nsteps = time; // Allocate memory to hold complex numbers for direct or FFT if (drct_) { data1_.Allocate( frame ); if (mode_ == CROSSCORR) data2_.Allocate( frame ); corfdir_.Allocate( nsteps ); } else { // Initialize FFT pubfft_.Allocate( frame ); data1_ = pubfft_.Array(); if (mode_ == CROSSCORR) data2_ = data1_; } // ----- Calculate spherical harmonics --------- // Real + Img. for each -order <= m <= order if (vinfo1_->CalcSphericalHarmonics(order_)) return Analysis::ERR; if (vinfo2_ != 0) { if (vinfo2_->CalcSphericalHarmonics(order_)) return Analysis::ERR; } // ----- Initialize PN output array memory ----- DataSet_double& pncf_ = static_cast<DataSet_double&>( *tc_p_ ); pncf_.Resize( nsteps ); Dimension Xdim(0.0, tstep_, nsteps, "Time"); pncf_.SetDim(Dimension::X, Xdim); // ----- Calculate PN -------------------------- for (int midx = -order_; midx <= order_; ++midx) { data1_.Assign( vinfo1_->SphericalHarmonics( midx ) ); if (vinfo2_ != 0) data2_.Assign( vinfo2_->SphericalHarmonics( midx ) ); CalcCorr( frame ); for (int k = 0; k < nsteps; ++k) pncf_[k] += data1_[2 * k]; } // ----- Dipolar Calc. ------------------------- AvgResults Avg1, Avg2; if (dplr_) { DataSet_double& cf_ = static_cast<DataSet_double&>( *tc_c_ ); cf_.Resize( nsteps ); cf_.SetDim(Dimension::X, Xdim); DataSet_double& rcf_ = static_cast<DataSet_double&>( *tc_r3r3_ ); rcf_.Resize( nsteps ); rcf_.SetDim(Dimension::X, Xdim); // Calculate averages std::vector<double> R3i_1 = CalculateAverages(*vinfo1_, Avg1); std::vector<double> R3i_2; if (vinfo2_ != 0) R3i_2 = CalculateAverages(*vinfo2_, Avg2); // C for (int midx = -order_; midx <= order_; ++midx) { data1_.Assign( vinfo1_->SphericalHarmonics( midx ) ); if (vinfo2_ != 0) data2_.Assign( vinfo2_->SphericalHarmonics( midx ) ); for (int i = 0, i2 = 0; i < frame; ++i, i2 += 2) { double r3i = R3i_1[ i ]; data1_[i2 ] *= r3i; data1_[i2+1] *= r3i; if ( vinfo2_ != 0 ) { r3i = R3i_2[ i ]; data2_[i2 ] *= r3i; data2_[i2+1] *= r3i; } } CalcCorr( frame ); for (int k = 0; k < nsteps; ++k) cf_[k] += data1_[2 * k]; } // 1 / R^6 for (int i = 0, i2 = 0; i < frame; ++i, i2 += 2) { data1_[i2 ] = R3i_1[ i ]; data1_[i2+1] = 0.0; if ( vinfo2_ != 0 ) { data2_[i2 ] = R3i_2[ i ]; data2_[i2+1] = 0.0; } } CalcCorr( frame ); for (int k = 0; k < nsteps; ++k) rcf_[k] = data1_[2 * k]; } // ----- NORMALIZATION ------------------------- // 4*PI / ((2*order)+1) due to spherical harmonics addition theorem double KN = DataSet_Vector::SphericalHarmonicsNorm( order_ ); Normalize( tc_p_, frame, KN ); if (dplr_) { Normalize( tc_c_, frame, KN ); Normalize( tc_r3r3_, frame, 1.0 ); } // ----- PRINT PTRAJ FORMAT -------------------- if (outfile_ != 0) { outfile_->Printf("%ss, normal type\n",ModeString[mode_]); if (dplr_) { outfile_->Printf("***** Vector length *****\n"); outfile_->Printf("%10s %10s %10s %10s\n", "<r>", "<rrig>", "<1/r^3>", "<1/r^6>"); outfile_->Printf("%10.4f %10.4f %10.4f %10.4f\n", Avg1.rave_, Avg1.avgr_, Avg1.r3iave_, Avg1.r6iave_); if (mode_ == CROSSCORR) outfile_->Printf("%10.4f %10.4f %10.4f %10.4f\n", Avg2.rave_, Avg2.avgr_, Avg2.r3iave_, Avg2.r6iave_); } if (ptrajformat_) { outfile_->Printf("\n***** Correlation functions *****\n"); if (dplr_) { DataSet_double& cf_ = static_cast<DataSet_double&>( *tc_c_ ); DataSet_double& rcf_ = static_cast<DataSet_double&>( *tc_r3r3_ ); outfile_->Printf("%10s %10s %10s %10s\n", "Time", "<C>", Plegend_[order_], "<1/(r^3*r^3)>"); for (int i = 0; i < nsteps; ++i) outfile_->Printf("%10.3f %10.4f %10.4f %10.4f\n", (double)i * tstep_, cf_[i], pncf_[i], rcf_[i]); } else { outfile_->Printf("%10s %10s\n", "Time", Plegend_[order_]); for (int i = 0; i < nsteps; ++i) outfile_->Printf("%10.3f %10.4f\n", (double)i * tstep_, pncf_[i]); } } } return Analysis::OK; }
int CorrAnalysis(int argc, char **argv) { //if (argc < 6 || !strcmp(argv[1],"-h") || argc < 8+2* (atoi(argv[4])) ) if ( argc != 7+2*(atoi(argv[5])) ) { cerr << "\n\n\n"; cerr << "Usage: " << argv[0] << " <task> [params]\n"; cerr << "<task>: operation to be performed on data. \n"; cerr << " 0 - correlation analysis\n"; cerr << " 1 - naive prediction\n"; cerr << " 2 - perceptron prediction\n"; cerr << " 3 - neural network\n"; cerr << "<prefix>: I/O prefix\n"; cerr << "[params]: algorithm specific parameters\n"; cerr << endl; cerr << "correlation analysis:\n"; cerr << "[datafile]: name of file containing data. \n"; cerr << "[diff]: level of differencing only zero and first level differencing is supported. \n"; cerr << "[nvar]: number of variables in datafile. \n"; cerr << "[tgtIdx]: Column of target variable (first column = 0).\n"; cerr << "[delay1]: delay before starting time-series of first variable\n"; cerr << "[nlag1]: number of lags to compose the time-series of the first variable from\n"; cerr << "[delay2]: delay before starting time-series of secon variable\n"; cerr << "[nlag2]: number of lags to compose the time-series of the second variable from\n"; cerr << "\n\n\n"; exit(-1); } string prefix = argv[2]; cout << "# I/O prefix: " << prefix << endl; // string ifile_name = argv[3]; cout << "# inputfile is: " << ifile_name << endl; // int diff = atoi(argv[4]); cout << "# differencing level is: " << diff << endl; if( diff < 0 || diff > 1) { cerr << "Assert: Invalid level of differencing\n\n"; exit(-1); } // int nvar = atoi(argv[5]); cout << "# nvar is: " << nvar << endl; // int tgtIdx = atoi(argv[6]); cout << "# Target index is: " << tgtIdx << endl; if( tgtIdx > nvar-1 ) { cerr << "Assert: Invalid target index\n\n"; exit(-1); } // vector<int> delay(nvar); vector<int> nlags(nvar); cout << "# ( delay , lag )\n"; for(int i=0; i< nvar; i++) { int argvIdx = 7+2*i; delay[i] = atoi(argv[argvIdx]); nlags[i] = atoi(argv[argvIdx+1]); if( i==tgtIdx && delay[i] <= 0) { delay[i]==1; } cout << "# ( " << delay[i] << "," << nlags[i] << " )" << endl; } int nAtt = 0; for( int i=0; i< nvar; i++) { nAtt += nlags[i]; } // cout << "# Reading Data" << endl; vector< vector< float > > Examples; vector< string > Timestamp; ReadData( ifile_name, diff, nvar, tgtIdx, delay, nlags, Examples, Timestamp ); // Divide Examples into Training Set and Testing Set int numTrainEx = int( (2.0/3.0)*Examples.size() ); vector< vector< float > > TrainExamples(numTrainEx); vector< string > TrainTimestamp(numTrainEx); vector< vector< float > > TestExamples(Examples.size()-numTrainEx); vector< string > TestTimestamp(Examples.size()-numTrainEx); for(int i=0; i<Examples.size(); i++) { if(i<numTrainEx) { TrainTimestamp[i] = Timestamp[i]; TrainExamples[i] = Examples[i]; } else { TestTimestamp[i-numTrainEx] = Timestamp[i]; TestExamples[i-numTrainEx] = Examples[i]; } } cout << "# Total number of examples: " << Examples.size() << endl; cout << "# Number of training examples: " << TrainExamples.size() << endl; cout << "# Number of testing examples: " << TestExamples.size() << endl; cout << "# Training period: ( " << TrainTimestamp[0] << " , " << TrainTimestamp[TrainTimestamp.size()-1] << " )\n"; cout << "# Testing period: ( " << TestTimestamp[0] << " , " << TestTimestamp[TestTimestamp.size()-1] << " )\n"; vector< vector< float > > Results_Train; vector< vector< float > > Results_Test; cout << "# Calculating Correlations" << endl; vector< vector< float > > ExampleCorr; CalcCorr( nvar, tgtIdx, delay, nlags, TrainExamples, ExampleCorr); // // write correlations to output file string fname = prefix + "-" + "corr_analysis.dat"; ofstream ofile( fname.c_str() ); // for( int i=0; i<ExampleCorr.size(); i++ ) { ofile << setw(5) << i; for( int j=0; j<nvar; j++ ) { ofile << setw(15) << ExampleCorr[i][j]; } ofile << endl; } ofile.close(); // // // // create un-normalized training file // vector< double > TimestampJ; // JulianDate( TrainTimestamp, TimestampJ ); // fname = prefix + "-" + "unorm_train.dat"; // ofile.open( fname.c_str() ); // ofile << setw(15) << TrainExamples.size() << setw(15) << TrainExamples[0].size() << endl; // for( int i=0; i<TrainExamples.size(); i++ ) // { // cout << setw(15) << fixed << TimestampJ[i] << std::resetiosflags(std::ios::floatfield); // for( int j=0; j<TrainExamples[0].size(); i++ ) // { // ofile << setw(15) << TrainExamples[i][j]; // } // ofile << endl; // } // ofile.close(); // // // // create normalized training file // vector< vector< float > > normParam; // NormalizeExamples( 0, TrainExamples, normParam ); // fname = prefix + "-" + "norm_train.dat"; // ofile.open( fname.c_str() ); // // write # rows, # cols // ofile << setw(15) << TrainExamples.size() << setw(15) << TrainExamples[0].size() << endl; // // write normalization information // for(int i=0; i<2; i++) // { // for(int j=0; j<normParam[i].size(); j++) // { // ofile << setw(15) << normParam[i][j]; // } // ofile << endl; // } // // write data // for( int i=0; i<TrainExamples.size(); i++ ) // { // for( int j=0; j<TrainExamples[i].size(); j++ ) // { // ofile << setw(15) << TrainExamples[i][j]; // } // ofile << endl; // } // ofile.close(); // // // // create un-normalized testing file // JulianDate( TestTimestamp, TimestampJ ); // fname = prefix + "-" + "unorm_test.dat"; // ofile.open( fname.c_str() ); // ofile << setw(15) << TestExamples.size() << setw(15) << TestExamples[0].size() << endl; // for( int i=0; i<TestExamples.size(); i++ ) // { // cout << setw(15) << fixed << TimestampJ[i] << std::resetiosflags(std::ios::floatfield); // for( int j=0; j<TestExamples[i].size(); i++ ) // { // ofile << setw(15) << TestExamples[i][j]; // } // ofile << endl; // } // ofile.close(); // // // // create normalized testing file // NormalizeExamples( 1, TestExamples, normParam ); // fname = prefix + "-" + "norm_test.dat"; // ofile.open( fname.c_str() ); // ofile << setw(15) << TestExamples.size() << setw(15) << TestExamples[0].size() << endl; // for(int i=0; i<2; i++) // { // for(int j=0; j<normParam[i].size(); j++) // { // ofile << setw(15) << normParam[i][j]; // } // ofile << endl; // } // for( int i=0; i<TestExamples.size(); i++ ) // { // for( int j=0; j<TestExamples[i].size(); j++ ) // { // ofile << setw(15) << TestExamples[i][j]; // } // ofile << endl; // } // ofile.close(); }