Index: bdm/estim/merger.cpp
===================================================================
--- bdm/estim/merger.cpp (revision 190)
+++ bdm/estim/merger.cpp (revision 192)
@@ -33,5 +33,5 @@
 	vec &w = eSmp._w(); //aux
 
-	mat Smp_ex =ones ( rv.count() +1,Ns ); // Extended samples for the ARX model - the last row is ones 
+	mat Smp_ex =ones ( rv.count() +1,Ns ); // Extended samples for the ARX model - the last row is ones
 	for ( int i=0;i<Ns;i++ ) {	set_col_part ( Smp_ex,i,Smp ( i ) );}
 
@@ -60,7 +60,7 @@
 		//Re-Initialize Mixture model
 		Mix.init ( &A0, Smp_ex, Nc );
-		Mix.bayesB ( Smp_ex, ones(Ns));//w*Ns );
-		Mpred = Mix.predictor(rv); // Allocation => must be deleted at the end!!
-	
+		Mix.bayesB ( Smp_ex, ones ( Ns ) );//w*Ns );
+		Mpred = Mix.predictor ( rv ); // Allocation => must be deleted at the end!!
+
 		// Generate new samples
 		eSmp.set_samples ( Mpred );
@@ -79,5 +79,5 @@
 			//======== Same RVs ===========
 			//Split according to dependency in rvs
-			if ( rvsinrv ( i ).length() ==rv.count() ) {
+			if ( mpdfs ( i )->_rv().count() ==rv.count() ) {
 				// no need for conditioning or marginalization
 				for ( int j=0;j<Ns; j++ ) { // Smp is Array<> => for cycle
@@ -86,26 +86,24 @@
 			}
 			else {
-				vec smpk;
 				// compute likelihood of marginal on the conditional variable
 				if ( mpdfs ( i )->_rvc().count() >0 ) {
 					// Make marginal on rvc_i
 					epdf* tmp_marg = Mpred->marginal ( mpdfs ( i )->_rvc() );
-					for(int k=0;k<Ns;k++){
-					lw_src(k) += tmp_marg->evalpdflog ( get_vec(Smp(i), irv_rvcs(i) ) );}
+					//compute vector of lw_src
+					for ( int k=0;k<Ns;k++ ) {
+						lw_src ( k ) += tmp_marg->evalpdflog ( dls ( i )->get_val ( Smp ( i ) ) );
+					}
 					delete tmp_marg;
 				}
 				// Compute likelihood of the missing variable
-				if ( rv.count() > mpdfs ( i )->_rv().count() + mpdfs ( i )->_rvc().count() ) {
-					// There are variales unknown to mpdfs
-					RV z = ( rv.subt ( mpdfs ( i )->_rv() ) ).subt ( mpdfs ( i )->_rvc() );
-					mpdf* tmp_cond = Mpred->condition ( z ); 
-					// Indeces of rest rv in Smp
-					ivec zinrv=z.dataind ( rv ) ;
-					// Indeces of rest rvc in Smp
-					ivec zinrvc=tmp_cond->_rvc().dataind ( rv );
+				if ( rv.count() > ( mpdfs ( i )->_rv().count() + mpdfs ( i )->_rvc().count() ) ) {
+					// There are variales unknown to mpdfs(i) : rvzs
+					mpdf* tmp_cond = Mpred->condition ( rvzs ( i ) );
 					// Compute likelihood
 					for ( int k= 0; k<Ns; k++ ) {
-						smpk=Smp( k );
-						lw_src ( k ) += log( tmp_cond->evalcond ( get_vec ( smpk,zinrv ), get_vec ( smpk,zinrvc ) ));
+						lw_src ( k ) += log (
+						                    tmp_cond->evalcond (
+						                        zdls ( i )->get_val ( Smp ( k ) ),
+						                        zdls ( i )->get_cond ( Smp ( k ) ) ) );
 					}
 					delete tmp_cond;
@@ -113,7 +111,6 @@
 				// Compute likelihood of the partial source
 				for ( int k= 0; k<Ns; k++ ) {
-					smpk=Smp( k );
-					mpdfs ( i )->condition ( get_vec ( smpk,irv_rvcs(i) ) );
-					lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( get_vec ( smpk, rvsinrv ( i ) ) );
+					mpdfs ( i )->condition ( dls ( i )->get_cond ( Smp(k) ) );
+					lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( dls ( i )->get_val ( Smp(k) ) );
 				}
 			}
@@ -128,5 +125,5 @@
 
 		w = lognorm_merge ( lW ); //merge
-		
+
 		sprintf ( str,"w%d",niter );
 		dbg << Name ( str ) << w;
@@ -150,5 +147,5 @@
 		// ==== stopping rule ===
 		niter++;
-		converged = ( niter>20);
+		converged = ( niter>20 );
 	}
 
Index: bdm/estim/merger.h
===================================================================
--- bdm/estim/merger.h (revision 190)
+++ bdm/estim/merger.h (revision 192)
@@ -30,4 +30,11 @@
 	//!Internal mixture of EF models
 	MixEF Mix;
+	//! Data link for each mpdf in mpdfs
+	Array<datalink_m2e*> dls;
+	//! Array of rvs that are not modelled by mpdfs at all (aux)
+	Array<RV> rvzs;
+	//! Data Links of rv0 mpdfs - these will be conditioned the (rv,rvc) of mpdfs
+	Array<datalink_m2e*> zdls; 
+	
 	//!Number of samples used in approximation
 	int Ns;
@@ -40,33 +47,54 @@
 	merger ( const Array<mpdf*> &S ) :
 			compositepdf ( S ), epdf ( getrv ( false ) ),
-			Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) )
-	{setindices(rv); beta=2.0; Ns=100; Nc=10; Mix.set_method(EM);}
-	//! Set internal parameters used in approximation
+			Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) ), dls(n), rvzs(n), zdls(n) {
+				RV ztmp;
+		for ( int i=0;i<n;i++ ) {
+			//Establich connection between mpdfs and merger
+			dls ( i ) = new datalink_m2e ( mpdfs ( i )->_rv(), mpdfs(i)->_rvc(), rv );
+			// find out what is missing in each mpdf
+			ztmp= mpdfs(i)->_rv();
+			ztmp.add(mpdfs(i)->_rvc());
+			rvzs(i)=rv.subt(ztmp);
+			zdls(i) = new datalink_m2e (rvzs(i), ztmp, rv ) ;
+		};
+		//Set Default values of parameters
+		beta=2.0;
+		Ns=100;
+		Nc=10;
+		Mix.set_method ( EM );
+	}
+//! Set internal parameters used in approximation
 	void set_parameters ( double beta0, int Ns0, int Nc0 ) {	beta=beta0;Ns=Ns0;Nc=Nc0;}
-	//!Initialize the proposal density. This function must be called before merge()!
-	void init() {
+//!Initialize the proposal density. This function must be called before merge()!
+	void init() { ////////////// NOT FINISHED
 		Array<vec> Smps ( n );
 		//Gibbs sampling
 		for ( int i=0;i<n;i++ ) {Smps ( i ) =zeros ( 0 );}
 	}
-	//!Create a mixture density using known proposal
+//!Create a mixture density using known proposal
 	void merge ( const epdf* g0 );
-	//!Create a mixture density, make sure to call init() before the first call
+//!Create a mixture density, make sure to call init() before the first call
 	void merge () {merge ( & ( Mix._epdf() ) );};
 
-	//! Merge log-likelihood values
+//! Merge log-likelihood values
 	vec lognorm_merge ( mat &lW );
-	//! sample from merged density
-	//! weight w is a
-	vec sample ( )const { return Mix._epdf().sample();}
-	double evalpdflog ( const vec &dt ) const{ 
-		vec dtf=ones(dt.length()+1); 	
-		dtf.set_subvector(0,dt); 
-		return Mix.logpred ( dtf );}
-	vec mean()const {return Mix._epdf().mean();}
-	//! for future use
-	virtual ~merger() {};
-	
-	//! Access function
+//! sample from merged density
+//! weight w is a
+	vec sample ( ) const { return Mix._epdf().sample();}
+	double evalpdflog ( const vec &dt ) const {
+		vec dtf=ones ( dt.length() +1 );
+		dtf.set_subvector ( 0,dt );
+		return Mix.logpred ( dtf );
+	}
+	vec mean() const {return Mix._epdf().mean();}
+//! for future use
+	virtual ~merger() {
+		for (int i=0; i<n; i++){
+			delete dls(i);
+			delete zdls(i);			
+		}
+	};
+
+//! Access function
 	MixEF& _Mix() {return Mix;}
 };
