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

using namespace bdm;

using namespace bdm;

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

int main() {

	RNG_randomize();

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

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

	enorm<fsqmat> f1;
	f1.set_rv ( xy );
	enorm<fsqmat> f2;
	f1.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" ) );

	Array<mpdf* > A ( 2 );
	mepdf A1 ( &f1 );
	mepdf A2 ( &f2 );
	A ( 0 ) = &A1;
	A ( 1 ) = &A2;

	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_m ( Grid );
	mat Res2 = ( MP )->evallog_M ( 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_m ( Grid );
	it << Name ( "S2" ) << f2.evallog_m ( Grid );
	cout << ( ( enorm<ldmat>* ) ( MP->_Coms ( 0 ) ) )->_R().to_mat() << endl;
}
