Type objective_function<Type>::operator() ()
{
  // Data
  DATA_VECTOR( y_i );
  DATA_MATRIX( X_ij );

  // Parameters
  PARAMETER_VECTOR( b_j );
  PARAMETER_VECTOR( theta_z );

  // Objective funcction
  Type zero_prob = 1 / (1 + exp(-theta_z(0)));
  Type logsd = exp(theta_z(1));
  Type jnll = 0;
  int n_data = y_i.size();

  // Linear predictor
  vector<Type> linpred_i( n_data );
  linpred_i = X_ij * b_j;

  // Probability of data conditional on fixed effect values
  for( int i=0; i<n_data; i++){
    if(y_i(i)==0) jnll -= log( zero_prob );
    if(y_i(i)!=0) jnll -= log( 1-zero_prob ) + dlognorm( y_i(i), linpred_i(i), logsd, true );
  }
  
  // Reporting
  REPORT( zero_prob );
  REPORT( logsd );
  REPORT( linpred_i );
  return jnll;
}
Exemple #2
0
std::chrono::duration<double> cphf(calculation_data &data){
	std::chrono::duration<double> elapsed_seconds(0);
	auto start = std::chrono::steady_clock::now();
	
	int sz = (data.n_paired/2)*(data.n_baseset-data.n_paired/2);
	std::function<int(int,int)> idx = [&](int i,int a){
		return (data.n_baseset-data.n_paired/2)*i + (a-data.n_paired/2);
	};
	
	// build A + B matrix for singlet
	hermitian_matrix AB = data.A_singlet + data.B_singlet;
	hermitian_matrix inv_AB = AB.inverse();
	
	
	// build theta matrix
	matrix theta_x(sz,1);
	matrix theta_y(sz,1);
	matrix theta_z(sz,1);
	for(int i=0;i<data.n_paired/2;i++){
		for(int a=data.n_paired/2;a<data.n_baseset;a++){
			int ia = idx(i,a);
			theta_x(ia,0) = data.mo_dipole_x(i,a);
			theta_y(ia,0) = data.mo_dipole_y(i,a);
			theta_z(ia,0) = data.mo_dipole_z(i,a);
		}
	}
	
	// solve (A+B)C=Theta
	matrix Cx = inv_AB * theta_x;
	matrix Cy = inv_AB * theta_y;
	matrix Cz = inv_AB * theta_z;
	
	// calculate the polarizability
	matrix *Cs[3] = { &Cx,&Cy,&Cz };
	matrix *thetas[3] = { &theta_x,&theta_y,&theta_z };
	for(int xyz1=0;xyz1<3;xyz1++)
		for(int xyz2=0;xyz2<3;xyz2++)
			data.polarizability[xyz1][xyz2] = 0;
	for(int i=0;i<data.n_paired/2;i++){
		for(int a=data.n_paired/2;a<data.n_baseset;a++){
			int ia = idx(i,a);
			for(int xyz1=0;xyz1<3;xyz1++)
				for(int xyz2=0;xyz2<3;xyz2++)
					data.polarizability[xyz1][xyz2] += 4 * (*thetas[xyz1])(ia,0) * (*Cs[xyz2])(ia,0);
		}
	}
	
	auto end = std::chrono::steady_clock::now();
	elapsed_seconds = std::chrono::duration_cast<std::chrono::duration<double>>(end-start);
	return elapsed_seconds;
}
Type objective_function<Type>::operator() ()
{
  // Data
  DATA_INTEGER( like ); // define likelihood type, 1==delta lognormal, 2==delta gamma
  DATA_VECTOR( y_i ); // observations
  DATA_MATRIX( X_ij ); // covariate design matrix
  DATA_VECTOR( include ); //0== include in NLL, 1== exclude from NLL

  // Parameters
  PARAMETER_VECTOR( b_j ); // betas to generate expected values
  PARAMETER_VECTOR( theta_z ); // variances

  // Transformations
  Type zero_prob = 1 / (1 + exp(-theta_z(0)));
  Type sd = exp(theta_z(1)); //standard deviation (lognormal), scale parameter theta (gamma)
  int n_data = y_i.size();

  Type jnll = 0;
  Type pred_jnll = 0;
  vector<Type> jnll_i(n_data);


  // linear predictor
  vector<Type> logpred_i( n_data );
  logpred_i = X_ij * b_j; 


  // Delta lognormal
  if(like==1){
  	for( int i=0; i<n_data; i++){
      if(y_i(i)==0) jnll_i(i) -= log( zero_prob );
      if(y_i(i)!=0) jnll_i(i) -= log( 1-zero_prob ) + dlognorm( y_i(i), logpred_i(i), sd, true );
    
      // Running counter
      if( include(i)==0 ) jnll += jnll_i(i);
      if( include(i)==1 ) pred_jnll += jnll_i(i);
    }
  }

  // Delta gamma
  if(like==2){
    for(int i=0; i<n_data; i++){
    	if(y_i(i)==0) jnll_i(i) -= log( zero_prob );
    	if(y_i(i)!=0) jnll_i(i) -= log( 1-zero_prob ) + dgamma( y_i(i), pow(sd,-2), exp(logpred_i(i))*pow(sd,2), true );

        // Running counter
        if( include(i)==0 ) jnll += jnll_i(i);
        if( include(i)==1 ) pred_jnll += jnll_i(i);
    }
  }



  
  // Reporting
  REPORT( zero_prob );
  REPORT( sd );
  REPORT( logpred_i );
  REPORT( b_j );
  REPORT( pred_jnll );
  REPORT( jnll_i );
  return jnll;
}