Exemplo n.º 1
0
//----------------------------------------------------------------------------
void
HDF5Table::update_input_mapping()
{
  // Start with a clean, direct-through mapping of the table
  //
  directInputIndex_.resize( inputNames_.size() );
  for ( unsigned int i = 0; i < directInputIndex_.size(); ++i ) {
    directInputIndex_[i] = i;
  }
  convTableIndex_.clear();
  convInputIndex_.clear();

  // Add the converters one-by-one until we have our final mapping
  //
  // Find the converter output variable in the list of inputNames_
  // (tableInputNames is same as inputNames_ at this point)
  const std::vector<std::string> & tableInputNames = input_names();
  for ( unsigned int i = 0; i < converters_.size(); ++i ) {

    // Check that the output of this converter, converters_[i]->name()
    // matches one of the table inputs in tableInputNames = inputNames_
    int idx = findix( tableInputNames, converters_[i]->name() );
    if ( idx < 0 ) {
      std::ostringstream errmsg;
      errmsg
        << "ERROR: Unable to find the Converter output variable '"
          << converters_[i]->name() << "'" << std::endl
        << "       in the list of table inputs:" << std::endl;
      for ( unsigned int j = 0; j < tableInputNames.size(); ++j ) {
        errmsg << "        - " << tableInputNames[j] << std::endl;
      }
      throw std::runtime_error( errmsg.str() );
    }
    // store the idx (based on ordering in indexNames_) of converter output
    // This is later used to provide input for this idx in lookupBuffer_
    // Note that below we add the inputs required by the converter
    convTableIndex_.push_back( idx );

    // Pluck this variable out of the global input variable list and
    // direct input list
    //
    idx = findix( inputNames_, converters_[i]->name() );
    if ( idx < 0 ) {
      std::ostringstream errmsg;
      errmsg
        << "ERROR: You may not hook the output of more than one" << std::endl
        << "       Converter to the same Table variable.  Error" << std::endl
        << "       occurred with the '" << converters_[i]->name() << "'"
        << " Converter." << std::endl;
      throw std::runtime_error( errmsg.str() );
    }
    inputNames_.erase( inputNames_.begin() + idx );
    directInputIndex_.erase( directInputIndex_.begin() + idx );

    // Add any new inputs required by this converter to the global list
    // if they aren't already there
    const std::vector<std::string> & convInputNames =
                                     converters_[i]->input_names();
    for ( unsigned int j = 0; j < convInputNames.size(); ++j ) {
      idx = findix( inputNames_, convInputNames[j] );
      if ( idx < 0 ) {
        inputNames_.push_back( convInputNames[j] );
      }
    }
  }
  // compute the new size of independent variable list with converter inputs
  dimension_ = inputNames_.size();

  // Now that our global input variable list is finalized, grab the
  // indexes into it for each of our converters
  //
  for ( unsigned int i = 0; i < converters_.size(); ++i ) {
    std::vector<unsigned int> convIndex;
    const std::vector<std::string> & convInputNames =
                                     converters_[i]->input_names();
    for ( unsigned int j = 0; j < convInputNames.size(); ++j ) {
      int idx = findix( inputNames_, convInputNames[j] );
      convIndex.push_back( idx );
    }
    // convInputIndex_ has an outer loop over the converters 
    // and inner loop over convInputNames 
    convInputIndex_.push_back( convIndex );
  }

  // Make sure our input buffers for the query() function are allocated
  // to the correct size
  //
  lookupBuffer_.resize( dimension_ );
  lookupBufferChecked_.resize( dimension_ );

  unsigned int maxSize = 0;
  for ( unsigned int i = 0; i < convInputIndex_.size(); ++i ) {
    maxSize = std::max( maxSize, (unsigned int)convInputIndex_[i].size() );
  }
  converterBuf_.resize( maxSize );

  // Now we have finalized the list of inputNames_ as it is expected 
  // by the spline query.  We need to set up a mapping between 
  // indVarTableNameVec_ and inputNames_.
  if ( inputNames_.size() != indVarTableNameVec_.size() ) {
    std::ostringstream errmsg;
    errmsg
      << "ERROR: The number of input variables needed by HDF5Table " << std::endl
      << "is not the same as that defined by indVarTableNameVec_."<< std::endl;
    throw std::runtime_error( errmsg.str() );
  }
  
  indexIndVar_.clear();
  for ( unsigned int i = 0; i < inputNames_.size() ; i++ ) {
    int idx = findix( indVarTableNameVec_, inputNames_[i] );
    if ( idx < 0 ) {
      std::ostringstream errmsg;
      errmsg
        << "ERROR: Unable to find inputNames_ = " << inputNames_[i] << std::endl
	<< "in indVarTableNameVec_"  << std::endl;
      throw std::runtime_error( errmsg.str() );
    }
    indexIndVar_.push_back( idx );
  }


}
Exemplo n.º 2
0
int main(){
	
	//Output file to be filled with results
	char* ofile = "Results.txt";
	
	//Size of stocks and pointers
	int number_of_stocks = 36; //Change the stock numbers for evaluating US stocks and HK stocks through Markowitz

	double mean_ir[number_of_stocks];
	double mean_cp[number_of_stocks];
	double std_ir[number_of_stocks];
	double std_cp[number_of_stocks];
	double Max_DD[number_of_stocks];
	double max[number_of_stocks];
	double min[number_of_stocks];
	char* infile;
	char* symbol;
	char* symbol_arr[number_of_stocks];
	char* vol[number_of_stocks];
	int size;
	int start;
	int stock_start = 0;


	//Define the class in int main
	Read_Calculate RC;
	
	input_names(stock_start, infile, symbol, size, start);
    
	//Initializing mean array table with all zeros to be filled in
	RC.init_tables(start, size, stock_start, number_of_stocks);

	//Change this to match with the stock numbers that we are going to evaluate
	for(int i = stock_start; i < number_of_stocks; i++){
	//Setting the file names and size of the files
	input_names(i, infile, symbol, size, start);
	
	//Calling the reading function
	RC.Read_Lines(infile, size);
	RC.Calculate_Return(size, i, stock_start);
	
	mean_ir[i] = RC.mean_i_r(size, start, symbol, (i - stock_start));
	mean_cp[i] = RC.mean_c_p(size, start, symbol, (i - stock_start));
	std_cp[i] = RC.std_c_p(mean_cp[i], start, size, (i - stock_start));
	std_ir[i] = RC.std_i_r(mean_ir[i], start, size, (i - stock_start));
	vol[i] = RC.return_vol(std_ir[i]);
	Max_DD[i] = RC.max_drawdown(size,start);
	max[i] = RC.find_max(size, start);
	min[i] = RC.find_min(size, start);
	};

	//Output Results
	for(int i = 0; i < number_of_stocks; i++){
	input_names(i, infile, symbol, size, start);
	symbol_arr[i] = symbol;
	}

	RC.Out_Results(stock_start, ofile, symbol_arr, vol, number_of_stocks, mean_cp,std_cp,mean_ir,std_ir, Max_DD, max, min);

	return 0;

}