
#include "stat/exp_family.h"
#include "stat/merger.h"
#include "../mat_checks.h"

using namespace bdm;

//These lines are needed for use of cout and endl
using std::cout;
using std::endl;

TEST ( merger_2d_stress ) {

	RNG_randomize();

	RV x ( "{x }", "1" );
	RV y ( "{y }", "1" );

	RV xy = x;
	xy.add ( y );

	enorm_fsqmat_ptr f1;
	f1->set_rv ( xy );
	enorm_fsqmat_ptr f2;
	f2->set_rv ( xy );

	mat R1 ( "0.5 0.48; 0.48 0.5" );
	mat R2 ( "0.5 0; 0 0.1" );
	f1->set_parameters ( "1 1", R1 );
	f2->set_parameters ( "1 1", mat ( "0.5 0; 0 0.1" ) );

	pdf_array A ( 2 );
	A ( 0 ) = f1;
	A ( 1 ) = f2;

	int Npoints = 100;
	mat x_grid ( 1, Npoints );
	x_grid.set_row ( 0, linspace ( -2.0, 4.0, Npoints ) );
	mat y_grid ( 1, Npoints );
	y_grid.set_row ( 0, linspace ( -2.0, 4.0, Npoints ) );

	mat Grid ( 2, Npoints*Npoints );
	Grid.set_submatrix ( 0, 0, kron ( x_grid, ones ( 1, Npoints ) ) );
	Grid.set_submatrix ( 1, 0, kron ( ones ( 1, Npoints ), y_grid ) );

	merger_mix M ( A );
	enorm<fsqmat> g0;
	g0.set_rv ( xy );
	g0.set_parameters ( vec ( "1 1" ), mat ( "10 0; 0 10" ) );

	M.set_parameters ( 1 );
	M.set_method ( LOGNORMAL, 1e8 );
	M.set_support ( g0, 400 );
	M.merge();

	MixEF &MM = M._Mix();
	emix* MP = MM.epredictor(); //xy

	vec Res1 = M.evallog_mat ( Grid );
	mat Res2 = ( MP )->evallog_coms ( Grid );

	it_file it ( "merger_2d_test.it" );
	it << Name ( "Npoints" ) << Npoints;
	it << Name ( "Grid" ) << Grid;
	it << Name ( "Res1" ) << Res1;
	it << Name ( "Res2" ) << Res2;
	it << Name ( "S1" ) << f1->evallog_mat ( Grid );
	it << Name ( "S2" ) << f2->evallog_mat ( Grid );
//	cout << ( ( enorm<ldmat>* ) ( MP->_Coms ( 0 ).get() ) )->_R().to_mat() << endl;
}
