PyObject * Filter_SubFileDecode(PyObject * self, PyObject * args) { PyObject * target; PyObject * delim_object; SubFileDecodeState * state; int length; if (!PyArg_ParseTuple(args, "OS", &target, &delim_object)) return NULL; length = PyString_Size(delim_object); if (length < 1) return PyErr_Format(PyExc_ValueError, "empty delimiter"); state = PyMem_Malloc(sizeof (SubFileDecodeState) + length * sizeof(int)); if (!state) return PyErr_NoMemory(); state->delim_object = delim_object; Py_INCREF(state->delim_object); state->delim = PyString_AsString(delim_object); state->chars_matched = 0; state->length = length; init_shift(state); return Filter_NewDecoder(target, "SubFileDecode", 0, read_subfile, NULL, dealloc_subfile, state); }
Shifted_HGVolatilityFunction_PTR JB_marketData_LMM_shift_Calibration( const LmmCalibrationConfig& config , LmmSwaptionMarketData_PTR pLmmSwaptionMarketData , Shifted_HGVolatilityFunction_PTR param_h_g_function ) { assert(!config.use_local_calib_); size_t nbYear= pLmmSwaptionMarketData->get_nbYear(); std::string base_file_name = pLmmSwaptionMarketData->get_MarketDataBaseFileName(); Tenor tenorfixedleg = Tenor::_1YR ; Tenor tenorfloatleg = Tenor::_6M ; size_t fixedfloatRatio = tenorfixedleg.ratioTo(tenorfloatleg); std::string base_name; base_name = base_file_name+"_shift_gMatrix_Calibration" ; Shifted_HGVolatilityParam_PTR param_h_g = param_h_g_function->get_ShiftedHGVolatilityParam_PTR(); //create LMM components LMMTenorStructure_PTR pLMMTenorStructure( new LMMTenorStructure(tenorfloatleg,nbYear) ); const double a=param_h_g->get_ABCD().a_; const double b=param_h_g->get_ABCD().b_; const double c=param_h_g->get_ABCD().c_; const double d=param_h_g->get_ABCD().d_; Shifted_HGVolatilityParam::ABCDParameter abcdParam(a,b,c,d); const Shifted_HGVolatilityParam::LowerTriangularMatrix& gMatrix=param_h_g->get_gMatrix(); QuantLib::Array shiftValues_QL = param_h_g->get_ArrayFrom_Shift(); std::vector<double> shiftValues(shiftValues_QL.size()); for(size_t i=0; i<shiftValues_QL.size(); i++) shiftValues[i]=shiftValues_QL[i]; //const std::vector<double> shiftedVector(gMatrix.size1(), 0.0); ConstShifted_HGVolatilityParam_PTR pShifted_HGVolatilityParam( new ConstShifted_HGVolatilityParam( pLMMTenorStructure, abcdParam, gMatrix, shiftValues)); Shifted_HGVolatilityFunction_PTR pShifted_VolatilityFunction (new ConstShifted_HGVolatilityFunction( pLMMTenorStructure, param_h_g_function->get_Correlation_PTR(), pShifted_HGVolatilityParam)); Dispersion dispersion(pShifted_VolatilityFunction); Lmm_PTR lmm_ptr(new Lmm(dispersion) ); LmmVanillaSwaptionApproxPricer_Rebonato_PTR pLmmVanillaSwaptionApproxPricer_Rebonato(new LmmVanillaSwaptionApproxPricer_Rebonato(lmm_ptr)); // create gMatrixMapping //size_t g_matrix_size = GMatrixMapping::get_gSizeFromNbYear(nbYear,fixedfloatRatio ); //size_t delegate_matrix_size = GMatrixMapping::get_gDelegateSizeFromHorizon(pLMMTenorStructure->get_horizon() ,fixedfloatRatio ); //UpperTriangularDoubleMatrix empty_delegate_matrix(delegate_matrix_size,delegate_matrix_size); //pShifted_HGVolatilityParam->reset_g_matrix(gMatrix); pLmmVanillaSwaptionApproxPricer_Rebonato->update_VolatilityParam(pShifted_HGVolatilityParam); // Create const function //LmmPenalty_PTR pLmmPenalty(new LmmPenalty(config.penalty_time_homogeneity_,config.penalty_libor_) ); //LmmBaseCostFunction_PTR pLmmCostFunction(new LmmGlobal_gCostFunction // ( // pLmmVanillaSwaptionApproxPricer_Rebonato, // pLmmSwaptionMarketData->get_LiborQuotes(), // pLmmSwaptionMarketData->get_SwaptionQuotes_ATM(), // pGMatrixMapping, // pNoShifted_HGVolatilityParam, // pLmmPenalty // ) ); const double const_rate=0.02; const double quoted_strike_bump=0.0001; LmmSwaptionMarketData_PTR lmmSwaptionMarketData(new LmmSwaptionMarketData(tenorfixedleg, tenorfloatleg, gMatrix.size1()+3)); LmmSkewCostFunction lmmSkewCostFunction( pLmmVanillaSwaptionApproxPricer_Rebonato // pricer , pLmmSwaptionMarketData->get_LiborQuotes() , quoted_strike_bump , pLmmSwaptionMarketData->get_SwaptionQuotes_skew()// instrument to calibrate , param_h_g ); //for(size_t i = 0; i < 3; i++) //{ // for (size_t j = 1; j < nbYear-i; j++) // { // lmmSkewCostFunction.addContraintCell(std::pair<size_t,size_t>(j,nbYear-i-j)); // } //} //costumize swaptions weights //UpperTriangularDoubleMatrix swpm_weight_matrix = pLmmCostFunction->get_SwaptionWeightMatrix(); //swpm_weight_matrix(7,1)=1e-6; //swpm_weight_matrix(10,1)=1e-6; //swpm_weight_matrix(5,3)=0.; //pLmmCostFunction->reset_SwaptionWeightMatrix(swpm_weight_matrix); // Create Calibrator QuantLib::Array init_shift(gMatrix.size1(),0.01); LmmShiftCalibrator lmmShiftCalibrator ( init_shift , 200 //maxIter , 1e-11 //x_epsilon , 1e-11 //f_epsilon , lmmSkewCostFunction ); //if(config.use_positive_constraint_) // lmmShiftCalibrator.activate_PositiveConstraint(); lmmShiftCalibrator.solve(); std::ostringstream file_result_stream;file_result_stream<<base_name<<"_result.csv"; std::string file_calibration_result(file_result_stream.str()); lmmShiftCalibrator.printPlusPlus(file_calibration_result); std::ostringstream file_vol_stream; file_vol_stream<<base_name<<".csv"; std::string file_calibrated_vol(file_vol_stream.str() ); param_h_g->print(file_calibrated_vol); pLmmSwaptionMarketData->print("pLmmSwaptionMarketData.csv"); //std::ostringstream file_result_stream;file_result_stream<<base_name<<penalty_info_str<<"_result.csv"; //std::string file_calibration_result(file_result_stream.str()); //lmmShiftCalibrator.printPlusPlus(file_calibration_result); //std::ostringstream file_vol_stream;file_vol_stream<<base_name<<penalty_info_str<<"_vol.csv"; //std::string file_calibrated_vol(file_vol_stream.str() ); //pShifted_HGVolatilityParam->print( file_calibrated_vol ); //{ // std::string common_result_file_name = "calib_result_Shift_gGlobal.csv"; // std::string full_common_result_file = LMMPATH::get_Root_OutputPath() + common_result_file_name ; // std::ofstream final_result ; // final_result.open(full_common_result_file.c_str(), std::ios::app); // final_result<<std::endl<<std::endl<< "============= Test At "<<LMMPATH::get_TimeDateNow() // <<",,,,,, Error LInf, "<<lmmShiftCalibrator.get_QuoteError_LInf() <<std::endl ; // final_result<< lmmShiftCalibrator.get_BaseGeneral_Result_Info(); // final_result.close(); //} return pShifted_VolatilityFunction; }
//--------------------------------------------------------------------------- // mpi_slaving: //--------------------------------------------------------------------------- int mpi_slaving() { // Declare local variables MPI_Datatype MPI_SLAVEINF; /* Dataype for MPI communications */ slaveinfo sl_info; /* Information structure for slaves */ MPI_Status *status; /* Recv status handler */ int used_size; /* # of processors used */ long sfN; /* # of pixels in subfield */ float *data = NULL; /* subfield data burst */ float *winH = NULL; /* apodisation window for SR calc. */ float *winF = NULL; /* apodisation window for phase rec. */ float *mask = NULL; /* DiffLim Mask */ float *pc = NULL; /* Phase Consistency */ /* for reallocation: needs to be NULL */ int *shifts = NULL; /* shifts for KT Cross Spectra */ /* for reallocation: needs to be NULL */ int maxk; /* number of shifts */ // Triple correlation part long *index = NULL; /* index list for bispectrum vectors */ long bs_cnt; /* # of vectors used */ float *bsc = NULL; /* complex non-red/red bispectrum */ float *wc = NULL; /* complex non-red/red bs weights */ float *p1 = NULL; /* phase matrix for iterativ reconstr. */ float *aphs = NULL; /* average of low frequencies */ // General part float *phs = NULL; /* Phase of reconstructed image */ float *amp = NULL; /* Amplitude of reconstructed image */ int c = GO; /* helpers */ long i; // set up status variable for sends and receives status = (MPI_Status *) malloc(sizeof(MPI_Status)); // Set Slaveinfo datatype for MPI sends and receives mpi_setslavetype(&MPI_SLAVEINF); // Receive number of jobs ... MPI_Bcast(&used_size, 1, MPI_INT, 0, MPI_COMM_WORLD); // ...and if used_size = NOGO, we are quitting the slave! if (used_size == NOGO) { // free memory MPI_Type_free(&MPI_SLAVEINF); free(status); // returning NOGO quits the slaves in entry.c return NOGO; } // ...and decide if I am needed if (used_size > proc_id) { // work until TAG says NOGO while (GO) { // Receive the data information MPI_Recv(&sl_info, 1, MPI_SLAVEINF, 0, MPI_ANY_TAG, MPI_COMM_WORLD, status); // Break if shutdown signal was sent if (status->MPI_TAG == NOGO) break; if (status->MPI_TAG == TC) { /* First time allocation of memory */ if (c) { // find number of pixels in subfield sfN = sl_info.sfsizex * sl_info.sfsizey; // allocate memory for data data = (float *) malloc(sfN * sl_info.nrofframes * sizeof(float)); winH = (float *) malloc(sfN * sizeof(float)); winF = (float *) malloc(sfN * sizeof(float)); pc = (float *) malloc(sfN * sizeof(float)); /* Initialise hamming window, fractional hamming & mask */ hanming(winH, sl_info.sfsizex, sl_info.sfsizey, 0.5); frachamming(winF, sl_info.sfsizex, sl_info.sfsizey, sl_info.limApod, 0, NULL); // mask is no longer used... mask = ellmask(sl_info.sfsizex, sl_info.sfsizey, NULL, sl_info.rad_x, sl_info.rad_y); // allocate appropriate memory index = bs_init(&bs_cnt, sl_info); bsc = (float *) malloc(2 * bs_cnt * sizeof(float)); wc = (float *) malloc(bs_cnt * sizeof(float)); // Allocate memory for reconstructed amplitudes & phases amp = (float *) malloc(sfN * sizeof(float)); phs = (float *) malloc(2 * sfN * sizeof(float)); p1 = (float *) malloc(2 * sfN * sizeof(float)); // set flag so as to not allocate new memory later c = NOGO; } // initialise amps, phases & phase consistency to zero memset(bsc, 0.0, 2 * bs_cnt * sizeof(float)); memset(wc, 0.0, bs_cnt * sizeof(float)); memset(amp, 0.0, sfN * sizeof(float)); memset(phs, 0.0, 2 * sfN * sizeof(float)); memset(p1, 0.0, 2 * sfN * sizeof(float)); memset(pc, 0.0, sfN * sizeof(float)); // finally receive the data MPI_Recv(data, sfN * sl_info.nrofframes, MPI_FLOAT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, status); // compute mean of burst //mean(data, sl_info.sfsizex, sl_info.sfsizey, sl_info.nrofframes, temp); /* compute the position of the 'good' average phase parts 3 is an argument here because we deleted: ((int) (alpha[0] * i_rad) > 3 ? (int) (alpha[0] * i_rad) : 3), */ init_shift(sl_info.sfsizex / 2, sl_info.sfsizey / 2, 3, &maxk, &shifts); // create bi-spectrum/weights in non-redundant matrix aphs = bs_ave(data, winF, sl_info, index, bs_cnt, bsc, wc, amp, maxk, shifts); // set snr-threshold bs_snrt(wc, bs_cnt, &sl_info); // phase reconstruction // init phase matrices phs_init(phs, p1, pc, sl_info, maxk, shifts, aphs); // recursive approach rpr(phs, p1, pc, index, bs_cnt, bsc, wc, sl_info); // iterative approach for (i = 0; i < sl_info.max_it; i++) { iwlspr(phs, p1, pc, bsc, wc, index, bs_cnt, sl_info, maxk); if (chkphase(sl_info, phs) < 1.0e-5) break; } // Send back info, so master knows, which subfield burst this was // MPI_TAG will indicate whether we are at the end of the process MPI_Send(&sl_info, 1, MPI_SLAVEINF, 0, NOGO, MPI_COMM_WORLD); // Send back processed data MPI_Send(amp, sfN, MPI_FLOAT, 0, NOGO, MPI_COMM_WORLD); // send back phase MPI_Send(phs, 2 * sfN, MPI_FLOAT, 0, NOGO, MPI_COMM_WORLD); // free some memory free(shifts); shifts = NULL; free(aphs); aphs = NULL; } } // free memory if (data != NULL) free(data); if (winH != NULL) free(winH); if (winF != NULL) free(winF); if (mask != NULL) free(mask); if (pc != NULL) free(pc); if (shifts != NULL) free(shifts); if (amp != NULL) free(amp); if (phs != NULL) free(phs); if (index != NULL) free(index); if (p1 != NULL) free(p1); if (bsc != NULL) free(bsc); if (wc != NULL) free(wc); free(status); MPI_Type_free(&MPI_SLAVEINF); // idle until new data or shutdown Broadcast is sent return GO; } else { // free memory free(status); MPI_Type_free(&MPI_SLAVEINF); // idle until new data or shutdown Broadcast is sent return GO; } }