Example #1
0
 Symbol get_column(SEXP arg, const Environment& env, const LazySubsets& subsets ){
   Symbol res = extract_column(arg, env) ;
   if( !subsets.count(res) ){
     stop("result of column() expands to a symbol that is not a variable from the data: %s", CHAR(PRINTNAME(res)) ) ;
   }
   return res ;
 }
Example #2
0
/**
 * Compute the covvariance array for AR1 case
 * This is based on the parameter rho.
 * This is done in two steps:
 * 1) fist calculate the correlation matrix dCor given phi
 * 2) Get the mle estimate of m_sig2 given the correlation.
**/
void logistic_normal::compute_covariance_matrix(const dvariable& phi)
{
	m_V.allocate(m_y1,m_y2,m_b1,m_nB2-1,m_b1,m_nB2-1);
	m_V.initialize();

	dvar3_array dCor(m_y1,m_y2,m_b1,m_nB2,m_b1,m_nB2);
	dCor.initialize();

	int i,j,k,nb;
	for( i = m_y1; i <= m_y2; i++ )
	{
		nb = m_nB2(i);
		dCor(i) = identity_matrix(m_b1,nb);

		// 2). Compute the vector of coefficients.
		dvar_vector drho(m_b1,nb);
		for( j = m_b1; j <= nb; j++ )
		{
			drho(j) = pow(phi,j-m_b1+1);
		}
		
		// 3). Compute correlation matrix dCor
		for( j = m_b1; j <= nb; j++ )
		{
			for( k = m_b1; k <= nb; k++ )
			{
				if( j != k ) dCor(i)(j,k) = drho(m_b1+abs(j-k));
			}
		}
		m_V(i) = trans(trans(dCor(i).sub(m_b1,nb-1)).sub(m_b1,nb-1));
	}
		
	// compute mle estimate of sigma
	compute_mle_sigma(m_V);
	// cout<<"Got to here sigma = "<<m_sig<<endl;


	for( i = m_y1; i <= m_y2; i++ )
	{	
		nb = m_nB2(i);
		for( j = m_b1; j <= nb; j++ )
		{
			dCor(i).rowfill(j, extract_row(dCor(i),j)*m_sig );
		}
		for( k = m_b1; k <= nb; k++ )
		{
			dCor(i).colfill(k, extract_column(dCor(i),k)*m_sig );
		}
		//cout<<dCor(i)<<endl;

		// Kmat
		dmatrix I = identity_matrix(m_b1,nb-1);
		dmatrix tKmat(m_b1,nb,m_b1,nb-1);
		tKmat.sub(m_b1,nb-1) = I;
		tKmat(nb) = -1;
		dmatrix Kmat = trans(tKmat);
		m_V(i) = Kmat * dCor(i) * tKmat;
	}
}
Example #3
0
/**
 * Compute the covvariance array for AR2 case
 * This is based on the parameters rho and psi.
 * Note that phi1 is bounded (-1,1)
 * Then set  phi2 = -1 + (1 + |phi1|)*psi
 * and psi is bounded (0,1)
 * This implies the upper bound for phi2 is 1.0 when |phi1|= 1 and psi = 1
**/
void logistic_normal::compute_covariance_matrix(const dvariable& phi1, const dvariable& psi)
{
	m_V.allocate(m_y1,m_y2,m_b1,m_nB2-1,m_b1,m_nB2-1);
	m_V.initialize();

	dvar3_array dCor(m_y1,m_y2,m_b1,m_nB2,m_b1,m_nB2);
	dCor.initialize();

	int i,j,k,nb;
	for( i = m_y1; i <= m_y2; i++ )
	{
		nb = m_nB2(i);
		// dmatrix I = identity_matrix(m_b1,nb-1);
		// m_V(i) = I;
		dCor(i) = identity_matrix(m_b1,nb);

		// 2). Compute the vector of coefficients.
		dvariable phi2 = -1. + (1. + sfabs(phi1)) * psi;
		dvar_vector drho(m_b1-1,nb-1);
		
		drho = 1.0;
		drho(m_b1) = phi1 / (1.0 - phi2);
		for( j = m_b1+1; j <= nb-1; j++ )
		{
			drho(j) = phi1*drho(j-1) + phi2*drho(j-2);
		}

		// 3). Compute correlation matrix m_V
		for( j = m_b1; j < nb; j++ )
		{
			for( k = m_b1; k < nb; k++ )
			{
				//if( j != k ) m_V(i)(j,k) = drho(m_b1+abs(j-k));
				if( j != k ) dCor(i)(j,k) = drho(m_b1+abs(j-k));
			}
		}
		m_V(i) = trans(trans(dCor(i).sub(m_b1,nb-1)).sub(m_b1,nb-1));
	}

	// compute mle estimate of sigma
	compute_mle_sigma(m_V);

	for( i = m_y1; i <= m_y2; i++ )
	{	
		nb = m_nB2(i);
		for( j = m_b1; j < nb; j++ )
		{
			m_V(i).rowfill(j, extract_row(m_V(i),j) * m_sig );
		}

		for( k = m_b1; k < nb; k++ )
		{
			m_V(i).colfill(k, extract_column( m_V(i),k ) * m_sig );
		}

		// Kmat
		dmatrix I = identity_matrix(m_b1,nb-1);
		dmatrix tKmat(m_b1,nb,m_b1,nb-1);
		tKmat.sub(m_b1,nb-1) = I;
		tKmat(nb) = -1;
		dmatrix Kmat = trans(tKmat);
		m_V(i) = Kmat * dCor(i) * tKmat;
	}
}