void getTrajectoryKeyPosition(double * trajectoryKeyPositions)
{
  for (unsigned int i = 0; i < TRAJECTORYKEYPOSITIONS.size1()*TRAJECTORYKEYPOSITIONS.size2(); i++)
    trajectoryKeyPositions[i] = TRAJECTORYKEYPOSITIONS(i % (TRAJECTORYKEYPOSITIONS.size1()), i / (TRAJECTORYKEYPOSITIONS.size1()));
}
Пример #2
0
 BOOST_UBLAS_INLINE
 int matrix_stride2 (const ublas::matrix<M,F,A> &m) {
   return (int) F::one2 (m.size1(), m.size2());
 }
void getNumberOfKeyPositions(int * size)
{
  *size = TRAJECTORYKEYPOSITIONS.size2();
}
Пример #4
0
template <typename T> inline T norm_max(ublas::matrix<T>& M) {
	size_t n1 = M.size1();   size_t n2 = M.size2();   T m = 0;
	for (size_t i=0; i<n1; i++)  for (size_t j=0; j<n2; j++)
		m = max(M(i,j),m);
	return m;
}
void print_matrices_error
	(
	const std::vector<double>& pel_time
	, const std::vector<double>& pel_libor
	, const boost::numeric::ublas::matrix<double>& quote_error_l2
	, const boost::numeric::ublas::matrix<double>& quote_error_l1
	, const boost::numeric::ublas::matrix<double>& quote_error_linf
	, const boost::numeric::ublas::matrix<double>& pel_timehomo_error_l2
	, const boost::numeric::ublas::matrix<double>& pel_timehomo_error_l1
	, const boost::numeric::ublas::matrix<double>& pel_timehomo_error_linf
	, const boost::numeric::ublas::matrix<double>& pel_liborsmoth_error_l2
	, const boost::numeric::ublas::matrix<double>& pel_liborsmoth_error_l1
	, const boost::numeric::ublas::matrix<double>& pel_liborsmoth_error_linf
	)
{
	{ /// print to excel file

		size_t nbPelTime  = quote_error_l2.size1();
		size_t nbPelLibor = quote_error_l2.size2();

		std::string  all_error_file_name = "all_Regularization_Result.csv";
		std::string full_common_result_file = LMMPATH::get_Root_OutputPath() + all_error_file_name ;
		std::ofstream final_result ;

		final_result.open( full_common_result_file.c_str() );
		final_result<<"PelTime\\PelLibor ,,";
		for(size_t j=0;j<nbPelLibor;++j){ final_result<< pel_libor[j]<<" ,,"; } final_result<<std::endl;

		for(size_t iT=0;iT<nbPelTime;++iT)
		{
			final_result<<pel_time[iT]<<" ,," ; for(size_t jL=0;jL<nbPelLibor;++jL)	{ final_result<<quote_error_l2(iT,jL)<<" ,,"; } final_result<<" L2 Quote Error ,"<<std::endl;
			final_result<<pel_time[iT]<<" ,," ; for(size_t jL=0;jL<nbPelLibor;++jL)	{ final_result<<quote_error_l1(iT,jL)<<" ,,"; } final_result<<" L1 Quote Error ,"<<std::endl;
			final_result<<pel_time[iT]<<" ,," ; for(size_t jL=0;jL<nbPelLibor;++jL)	{ final_result<<quote_error_linf(iT,jL)<<" ,,"; } final_result<<" L_INF Quote Error ,"<<std::endl;
			final_result<<pel_time[iT]<<" ,," ; for(size_t jL=0;jL<nbPelLibor;++jL)	{ final_result<<pel_timehomo_error_l2(iT,jL)<<" ,,"; } final_result<<" L2 Pel Time Error ,"<<std::endl;
			final_result<<pel_time[iT]<<" ,," ; for(size_t jL=0;jL<nbPelLibor;++jL)	{ final_result<<pel_timehomo_error_l1(iT,jL)<<" ,,"; } final_result<<" L1 Pel Time Error ,"<<std::endl;
			final_result<<pel_time[iT]<<" ,," ; for(size_t jL=0;jL<nbPelLibor;++jL)	{ final_result<<pel_timehomo_error_linf(iT,jL)<<" ,,"; } final_result<<" L_INF Pel Time Error ,"<<std::endl;
			final_result<<pel_time[iT]<<" ,," ; for(size_t jL=0;jL<nbPelLibor;++jL)	{ final_result<<pel_liborsmoth_error_l2(iT,jL)<<" ,,"; } final_result<<" L2 Pel Libor Error ,"<<std::endl;
			final_result<<pel_time[iT]<<" ,," ; for(size_t jL=0;jL<nbPelLibor;++jL)	{ final_result<<pel_liborsmoth_error_l1(iT,jL)<<" ,,"; } final_result<<" L1 Pel Libor Error ,"<<std::endl;
			final_result<<pel_time[iT]<<" ,," ; for(size_t jL=0;jL<nbPelLibor;++jL)	{ final_result<<pel_liborsmoth_error_linf(iT,jL)<<" ,,"; } final_result<<" L_INF Pel Libor Error ,"<<std::endl;

			final_result<<std::endl;
		}
		final_result<<std::endl<<std::endl;
		final_result.close();
	}

	{
		/// print in gnuplot format
		LmmGnuplotPrinterMatrix gnuplot_printer( create___LMMTenorStructure_PTR( 16 ) );
		gnuplot_printer.printFullMatrix(quote_error_l2 , pel_time,pel_libor , "gnuplot_quote_error_l2");
		gnuplot_printer.printFullMatrix(quote_error_l1 , pel_time,pel_libor , "gnuplot_quote_error_l1");
		gnuplot_printer.printFullMatrix(quote_error_linf , pel_time,pel_libor , "gnuplot_quote_error_linf");

		gnuplot_printer.printFullMatrix(pel_timehomo_error_l2 , pel_time,pel_libor , "gnuplot_pel_timehomo_error_l2");
		gnuplot_printer.printFullMatrix(pel_timehomo_error_l1 , pel_time,pel_libor , "gnuplot_pel_timehomo_error_l1");
		gnuplot_printer.printFullMatrix(pel_timehomo_error_linf , pel_time,pel_libor , "gnuplot_pel_timehomo_error_linf");

		gnuplot_printer.printFullMatrix(pel_liborsmoth_error_l2 , pel_time,pel_libor , "gnuplot_pel_liborsmoth_error_l2");
		gnuplot_printer.printFullMatrix(pel_liborsmoth_error_l1 , pel_time,pel_libor , "gnuplot_pel_liborsmoth_error_l1");
		gnuplot_printer.printFullMatrix(pel_liborsmoth_error_linf , pel_time,pel_libor , "gnuplot_pel_liborsmoth_error_linf");
	}
}
Пример #6
0
boost::numeric::ublas::vector<double> gradient_descent(boost::numeric::ublas::matrix<double>& x, boost::numeric::ublas::vector<double>& y, double epsilon, double gamma, int max_iters) {

	int iter = 1;
	
	boost::numeric::ublas::vector<double> weight_old(x.size2());
	for (size_t i = 0; i < weight_old.size(); i++) 
		weight_old(i) = 0;
	
	cout << "old weight: " << weight_old << endl;
	
	boost::numeric::ublas::vector<double> weight_new(x.size2());
	for (size_t i = 0; i < weight_new.size(); i++) 
		weight_new(i) = 0;
	
	cout << "new weight: " << weight_new << endl;

	
	while(true) {
		for (size_t k = 0; k < weight_new.size(); k++) {
			double gradient = 0;
			for (size_t i = 0; i < x.size1(); i++) {
				double z_i = 0;
				for (size_t j = 0; j < weight_old.size(); j++) {
					// w^T * x
					z_i += weight_old(j) * x(i, j);
				}

				gradient = y(i) * x(i, k) * sigmoid(-y(i) * z_i);
			}
			
			weight_new(k) = weight_old(k) + gamma * gradient;	
		}

		double dist = norm(weight_new, weight_old);

		if (dist < epsilon){ 
			cout << "The best weight is:" << weight_new << endl;
			cout << "After " << iter << " th iteration" << endl;
			break;
		}
		else
			weight_old.swap(weight_new);

		iter += 1;

		if (iter >= max_iters) {
			cout << "The best weight is:" << weight_new << endl;
			cout << "After " << iter << " th iteration" << endl;

			break;
		}

		cout << "===========================================" << endl;
		cout << "The " << iter << " th iteration, weight:" << endl;
		cout << weight_new << endl << endl;
		cout << "The best weight:" << endl;
		cout << "ending" << endl;
	}	

	return weight_new;
}
Пример #7
0
void LmmGnuplotPrinterMatrix::printFullMatrix(const boost::numeric::ublas::matrix<double>& matrix
											  , const std::vector<double>& xticks
											  , const std::vector<double>& yticks
											  ,const std::string& base_filename) const
{
	assert(xticks.size() == matrix.size2() );
	assert(yticks.size() == matrix.size1() );


	{//print matrix data files
		std::string data_file_name = get_dataFileName( base_filename);

		std::string path_OutPut = LMMPATH::get_output_path() + data_file_name;

		std::ofstream matrix_data_file ;
		matrix_data_file.open(path_OutPut.c_str() );
		matrix_data_file<<"### Lmm Matrix output for gnuplot printing surface"<<std::endl;
		matrix_data_file<<"### grid xtics ";for(size_t i=0;i<xticks.size();++i){ matrix_data_file<<", "<<xticks[i]; }
		matrix_data_file<<std::endl;
		matrix_data_file<<"### grid ytics ";for(size_t i=0;i<yticks.size();++i){ matrix_data_file<<", "<<yticks[i]; }

		for(size_t i=0;i<matrix.size1();++i)
		{
			const double x_value = xticks[i] ;
			for(size_t j=0;j<matrix.size2();++j)
			{							
				const double y_value = yticks[j];
				const double z_value = matrix(i,j);
				matrix_data_file<<"	"<<x_value<<"	"<<y_value<<"	"<<z_value<<std::endl;
			}	
			matrix_data_file<<std::endl;
		}

		matrix_data_file.close();
	}

	{// print gnuplot file
		std::string gp_file_name = get_gnuplotFileName(base_filename);

		std::string path_OutPut = LMMPATH::get_output_path() + gp_file_name;

		std::ofstream gnuplot_file ;
		gnuplot_file.open(path_OutPut.c_str() );

		gnuplot_file<<"### gnuplot file"<<std::endl;
		gnuplot_file<<"### grid xtics ";for(size_t i=0;i<xticks.size();++i){ gnuplot_file<<", "<<xticks[i]; }
		gnuplot_file<<std::endl;
		gnuplot_file<<"### grid ytics ";for(size_t i=0;i<yticks.size();++i){ gnuplot_file<<", "<<yticks[i]; }


		gnuplot_file<<"set xlabel \"xLabel\""<<std::endl;
		gnuplot_file<<"set ylabel \"yLabel\""<<std::endl;
		gnuplot_file<<"set title \"Graph title\" "<<std::endl;
		gnuplot_file<<"set style data lines"<<std::endl;
		gnuplot_file<<"set hidden3d"<<std::endl;
		gnuplot_file<<"set pm3d ; set palette"<<std::endl;
		gnuplot_file<<"set cntrparam levels 10"<<std::endl;
		gnuplot_file<<"set contour base"<<std::endl;
		
		std::string data_file_name = get_dataFileName(base_filename);

		gnuplot_file<<"splot \""<< data_file_name <<"\"" <<std::endl;

		gnuplot_file<<"pause -1 \" Hit return to save image\" "<<std::endl;

		gnuplot_file<<"set terminal postscript eps"<<std::endl;

		std::string image_file_name = get_imageFileName(base_filename);

		gnuplot_file<<"set output \""<<image_file_name <<"\""<<std::endl;

		gnuplot_file<<"replot"<<std::endl;

		gnuplot_file.close();
	}
}