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; }
bool LeastSquare(const std::vector<double>& x_value, const std::vector<double>& y_value, int M, std::vector<double>& a_value) { assert(x_value.size() == y_value.size()); assert(a_value.size() == M); double *matrix = new double[M * M]; double *b= new double[M]; std::vector<double> x_m(x_value.size(), 1.0); std::vector<double> y_i(y_value.size(), 0.0); for(int i = 0; i < M; i++) { matrix[ARR_INDEX(0, i, M)] = std::accumulate(x_m.begin(), x_m.end(), 0.0); for(int j = 0; j < static_cast<int>(y_value.size()); j++) { y_i[j] = x_m[j] * y_value[j]; } b[i] = std::accumulate(y_i.begin(), y_i.end(), 0.0); for(int k = 0; k < static_cast<int>(x_m.size()); k++) { x_m[k] *= x_value[k]; } } for(int row = 1; row < M; row++) { for(int i = 0; i < M - 1; i++) { matrix[ARR_INDEX(row, i, M)] = matrix[ARR_INDEX(row - 1, i + 1, M)]; } matrix[ARR_INDEX(row, M - 1, M)] = std::accumulate(x_m.begin(), x_m.end(), 0.0); for(int k = 0; k < static_cast<int>(x_m.size()); k++) { x_m[k] *= x_value[k]; } } GuassEquation equation(M, matrix, b); delete[] matrix; delete[] b; return equation.Resolve(a_value); }
Type objective_function<Type>::operator() () { // Data DATA_INTEGER( n_y ); DATA_INTEGER( n_s ); DATA_IVECTOR( s_i ); DATA_VECTOR( y_i ); // Parameters PARAMETER( x0 ); //PARAMETER( log_sdz ); //variability in site effect //PARAMETER_VECTOR( z_s ); //site effect // Objective funcction Type jnll = 0; // Probability of data conditional on fixed and random effect values vector<Type> ypred_i(n_y); for( int i=0; i<n_y; i++){ ypred_i(i) = exp( x0 );//+ z_s(s_i(i)) ); jnll -= dpois( y_i(i), ypred_i(i), true ); } // Probability of random coefficients //for( int s=0; s<n_s; s++){ // jnll -= dnorm( z_s(s), Type(0.0), exp(log_sdz), true ); //} // Reporting //Type sdz = exp(log_sdz); //REPORT( sdz ); //REPORT( z_s ); REPORT( x0 ); //ADREPORT( sdz ); //ADREPORT( z_s ); ADREPORT( x0 ); return jnll; }
Type objective_function<Type>::operator() () { // Data DATA_VECTOR( y_i ); //capital letters - macro - Kasper built into C++ code // Parameters PARAMETER( mean ); PARAMETER( log_sd ); // Objective funcction Type sd = exp(log_sd); Type jnll = 0; int n_data = y_i.size(); // Probability of data conditional on fixed effect values for( int i=0; i<n_data; i++){ jnll -= dnorm( y_i(i), mean, sd, true ); } // Reporting return jnll; }
Type objective_function<Type>::operator() () { // data: DATA_MATRIX(x_ij); DATA_VECTOR(y_i); DATA_IVECTOR(k_i); // vector of IDs DATA_INTEGER(n_k); // number of IDs // parameters: PARAMETER_VECTOR(b_j) PARAMETER_VECTOR(sigma_j); PARAMETER(log_b0_sigma); PARAMETER_VECTOR(b0_k); int n_data = y_i.size(); // get number of data points to loop over // Linear predictor vector<Type> linear_predictor_i(n_data); vector<Type> linear_predictor_sigma_i(n_data); linear_predictor_i = x_ij*b_j; linear_predictor_sigma_i = sqrt(exp(x_ij*sigma_j)); Type nll = 0.0; // initialize negative log likelihood for(int i = 0; i < n_data; i++){ nll -= dnorm(y_i(i), b0_k(k_i(i)) + linear_predictor_i(i) , linear_predictor_sigma_i(i), true); } for(int k = 0; k < n_k; k++){ nll -= dnorm(b0_k(k), Type(0.0), exp(log_b0_sigma), true); } REPORT( b0_k ); REPORT(b_j ); ADREPORT( b0_k ); ADREPORT( b_j ); return nll; }
Type objective_function<Type>::operator() () { // data: DATA_MATRIX(x_ij); // fixed effect model matrix DATA_MATRIX(x_sigma_ij); // fixed effect model matrix DATA_VECTOR(y_i); // response vector DATA_IVECTOR(pholder_i); // vector of IDs for strategy DATA_IVECTOR(strategy_i); // vector of IDs for permit holder DATA_INTEGER(n_pholder); // number of IDs for pholder DATA_INTEGER(n_strategy); // number of IDs for strategy DATA_INTEGER(diversity_column); // fixed effect column position of diversity DATA_VECTOR(b1_cov_re_i); // predictor data for random slope DATA_VECTOR(b2_cov_re_i); // predictor data for random slope /* DATA_VECTOR(b3_cov_re_i); // predictor data for random slope */ DATA_VECTOR(g1_cov_re_i); // predictor data for random slope DATA_IVECTOR(spec_div_all_1); // indicator for if there is variability in diversity // parameters: PARAMETER_VECTOR(b_j); PARAMETER_VECTOR(sigma_j); PARAMETER(log_b0_pholder_tau); // PARAMETER(log_b1_pholder_tau); PARAMETER(log_b0_strategy_tau); PARAMETER(log_b1_strategy_tau); PARAMETER(log_b2_strategy_tau); /* PARAMETER(log_b3_strategy_tau); */ PARAMETER_VECTOR(b0_pholder); // PARAMETER_VECTOR(b1_pholder); PARAMETER_VECTOR(b0_strategy); PARAMETER_VECTOR(b1_strategy); PARAMETER_VECTOR(b2_strategy); /* PARAMETER_VECTOR(b3_strategy); */ //PARAMETER(log_g0_pholder_tau); PARAMETER(log_g0_strategy_tau); PARAMETER(log_g1_strategy_tau); // PARAMETER_VECTOR(g0_pholder); PARAMETER_VECTOR(g0_strategy); PARAMETER_VECTOR(g1_strategy); int n_data = y_i.size(); // Linear predictor vector<Type> linear_predictor_i(n_data); vector<Type> linear_predictor_sigma_i(n_data); vector<Type> eta(n_data); vector<Type> eta_sigma(n_data); linear_predictor_i = x_ij*b_j; linear_predictor_sigma_i = x_sigma_ij*sigma_j; /* // set slope deviations that we can't estimate to 0: */ /* for(int i = 0; i < n_data; i++){ */ /* if(spec_div_all_1(strategy_i(i)) == 1) { */ /* b1_strategy(strategy_i(i)) = 0; */ /* g1_strategy(strategy_i(i)) = 0; */ /* } */ /* } */ Type nll = 0.0; // initialize negative log likelihood for(int i = 0; i < n_data; i++){ eta(i) = b0_pholder(pholder_i(i)) + // b1_pholder(pholder_i(i)) * b1_cov_re_i(i) + b0_strategy(strategy_i(i)) + b1_strategy(strategy_i(i)) * b1_cov_re_i(i) + b2_strategy(strategy_i(i)) * b2_cov_re_i(i) + /* b3_strategy(strategy_i(i)) * b3_cov_re_i(i) + */ linear_predictor_i(i); eta_sigma(i) = sqrt(exp( // g0_pholder(pholder_i(i)) + g0_strategy(strategy_i(i)) + g1_strategy(strategy_i(i)) * g1_cov_re_i(i) + linear_predictor_sigma_i(i))); nll -= dnorm(y_i(i), eta(i), eta_sigma(i), true); } for(int k = 0; k < n_pholder; k++){ nll -= dnorm(b0_pholder(k), Type(0.0), exp(log_b0_pholder_tau), true); // nll -= dnorm(g0_pholder(k), Type(0.0), exp(log_g0_pholder_tau), true); // nll -= dnorm(b1_pholder(k), Type(0.0), exp(log_b1_pholder_tau), true); } for(int k = 0; k < n_strategy; k++){ nll -= dnorm(b0_strategy(k), Type(0.0), exp(log_b0_strategy_tau), true); nll -= dnorm(g0_strategy(k), Type(0.0), exp(log_g0_strategy_tau), true); // only include these species diversity slope deviations // if there was sufficient variation in species diversity // to estimate them: if(spec_div_all_1(k) == 0) { nll -= dnorm(b1_strategy(k), Type(0.0), exp(log_b1_strategy_tau), true); nll -= dnorm(g1_strategy(k), Type(0.0), exp(log_g1_strategy_tau), true); } nll -= dnorm(b2_strategy(k), Type(0.0), exp(log_b2_strategy_tau), true); /* nll -= dnorm(b3_strategy(k), Type(0.0), exp(log_b3_strategy_tau), true); */ } // Reporting /* Type b0_pholder_tau = exp(log_b0_pholder_tau); */ /* Type b0_strategy_tau = exp(log_b0_strategy_tau); */ // Type b1_tau = exp(log_b1_tau); /* Type g0_pholder_tau = exp(log_g0_pholder_tau); */ /* Type g0_strategy_tau = exp(log_g0_strategy_tau); */ // Type g1_tau = exp(log_g1_tau); vector<Type> combined_b1_strategy(n_strategy); vector<Type> combined_g1_strategy(n_strategy); for(int k = 0; k < n_strategy; k++){ // these are fixed-effect slopes + random-effect slopes combined_b1_strategy(k) = b_j(diversity_column) + b1_strategy(k); combined_g1_strategy(k) = sigma_j(diversity_column) + g1_strategy(k); } /* REPORT(b0_pholder); */ REPORT(b0_strategy); REPORT(eta); REPORT(b1_strategy); REPORT(b_j); /* REPORT(g0_pholder); */ REPORT(g0_strategy); REPORT(g1_strategy); /* REPORT(b0_tau); */ // REPORT(b1_tau); /* REPORT(g0_tau); */ // REPORT(g1_tau); REPORT(combined_b1_strategy); REPORT(combined_g1_strategy); // /* ADREPORT(b0_pholder); */ // ADREPORT(b0_strategy); // ADREPORT(b1_strategy); // ADREPORT(b_j); // /* ADREPORT(g0_pholder); */ // ADREPORT(g0_strategy); // ADREPORT(g1_strategy); // /* ADREPORT(b0_tau); */ // ADREPORT(b1_tau); // /* ADREPORT(g0_tau); */ // ADREPORT(g1_tau); ADREPORT(combined_b1_strategy); ADREPORT(combined_g1_strategy); return nll; }
void SolveSystem ( Vector<double, int>& x_local, const MatrixDense<double, int>& K_local, const Vector<double, int>& b_local, const int numb_node_i, const int* list_node_i, const int* l2i, int numb_node_p, const int* list_node_p, const int* l2p, const int numb_global_node, const int numb_l2g, const int* l2g, const MPI_Comm& mpi_comm ) { // -- number of processors int numb_procs; // -- process number (process rank) int proc_numb; // -- get number of processes MPI_Comm_size( mpi_comm, &numb_procs ); // -- get current process rank MPI_Comm_rank( mpi_comm, &proc_numb ); MatrixDense<double,int> Kii; MatrixDense<double,int> Kip; MatrixDense<double,int> Kpi; MatrixDense<double,int> Kpp; Schur::SplitMatrixToBlock(Kii, Kip, Kpi, Kpp, K_local, list_node_i, numb_node_i, list_node_p, numb_node_p); // Check transpose //CheckTranspose(Kip, Kpi); // Reconstruct K //std::string gmatrix_filename = "../output/cube-125_2/cube-125_g_2.csv"; //ReconstructK(K_local, l2g, numb_global_node, gmatrix_filename, mpi_comm); // LU factorization of Kii MatrixDense<double, int> Kii_lu; Factor::LU(Kii_lu, Kii); MatrixDense<double, int> Uii_inv,Lii_inv; Lii_inv.Allocate(numb_node_i, numb_node_i); Uii_inv.Allocate(numb_node_i, numb_node_i); Vector<double, int> x,rhs; rhs.Allocate(numb_node_i); for(int i = 0;i < numb_node_i;++i) rhs(i) = 0; // invert Lii and Uii for(int i = 0;i < numb_node_i;++i){ if(i > 0) rhs(i - 1) = 0; rhs(i) = 1; DirectSolver::Forward(x, Kii_lu, rhs); for(int j = 0;j < numb_node_i;++j) Lii_inv(j,i) = x(j); DirectSolver::Backward(x,Kii_lu, rhs); for(int j = 0;j < numb_node_i;++j) Uii_inv(j,i) = x(j); } // calculate S_local MatrixDense<double, int> Lpi,Uip,prod; Kpi.MatrixMatrixProduct(Lpi, Uii_inv); Lii_inv.MatrixMatrixProduct(Uip, Kip); Lpi.MatrixMatrixProduct(prod, Uip); MatrixDense<double, int> S_local; Kpp.MatrixMatrixSubstraction(S_local, prod); // merge all numb_node_p in one list int length_list_p[numb_procs]; MPI_Allgather(&numb_node_p, 1, MPI_INT, length_list_p, 1, MPI_INT, mpi_comm); /*std::stringstream out_length_list; for(int i = 0;i < numb_procs;++i){ out_length_list << length_list_p[i] << " "; } out_length_list << "\n";*/ int all_list_global_p[numb_procs][numb_global_node]; for(int i = 0;i < numb_procs;++i){ if(proc_numb == i){ for(int j = 0;j < numb_node_p;++j){ all_list_global_p[i][j] = l2g[ list_node_p[j] ]; } } MPI_Bcast(all_list_global_p[i], length_list_p[i], MPI_INT, i, mpi_comm); } /*for(int i = 0;i < numb_procs;++i){ for(int j = 0;j < length_list_p[i];++j){ out_length_list << all_list_global_p[i][j] << " "; } out_length_list << "\n"; } iomrg::printf("%s\n", out_length_list.str().c_str());*/ // create array pos_S : from global position to position in S int pos_S[numb_global_node]; for(int i = 0;i < numb_global_node;++i){ pos_S[i] = -1; } std::vector<int> S_indices; for(int i = 0;i < numb_procs;++i){ for(int j = 0;j < length_list_p[i];++j){ S_indices.push_back(all_list_global_p[i][j]); } } std::sort(S_indices.begin(), S_indices.end()); S_indices.erase(std::unique(S_indices.begin(), S_indices.end()), S_indices.end()); int size_S = S_indices.size(); for(int i = 0;i < size_S;++i){ pos_S[ S_indices[i] ] = i; } // Assemble S MatrixDense<double, int> S; S.Allocate(size_S, size_S); S.Initialize(0); double aux_coef[size_S]; iomrg::printf("size_S = %d\n",size_S); for(int i = 0;i < numb_procs;++i){ int size_local_S = length_list_p[i]; for(int j = 0;j < size_local_S;++j){ if(proc_numb == i){ for(int k = 0;k < size_local_S;++k){ aux_coef[k] = S_local(j,k); } } MPI_Bcast(aux_coef, size_local_S, MPI_DOUBLE, i, mpi_comm); int r = pos_S[ all_list_global_p[i][j] ]; for(int k = 0;k < size_local_S;++k){ S(r, pos_S[ all_list_global_p[i][k] ]) += aux_coef[k]; } } } // Separate b_local Vector<double, int> b_i,b_p; b_i.Allocate(numb_node_i); b_p.Allocate(numb_node_p); for(int i = 0;i < numb_l2g;++i){ if(l2p[i] == -1){ b_i( l2i[i] ) = b_local(i); }else{ b_p( l2p[i] ) = b_local(i); } } // Calculate y_p_local Vector<double, int> z; DirectSolver::Forward(z, Kii_lu, b_i); Vector<double, int> aux_prod; Lpi.MatrixVectorProduct(aux_prod, z); Vector<double, int> y_p_local; y_p_local.Allocate(numb_node_p); for(int i = 0;i < numb_node_p;++i){ y_p_local(i) = b_p(i) - aux_prod(i); } // Calculate y_p Vector<double, int> y_p(size_S); for(int i = 0;i < size_S;++i){ aux_coef[i] = 0; } for(int i = 0;i < numb_node_p;++i){ aux_coef[ pos_S[ l2g[ list_node_p[i] ] ] ] += y_p_local(i); } MPI_Allreduce(aux_coef, y_p.GetCoef(), size_S, MPI_DOUBLE, MPI_SUM,mpi_comm); // Calculate x_p Vector<double, int> x_p; DirectSolver::SolveLU(x_p, S, y_p); // Calculate y_i Vector<double, int> aux_prod_2; aux_prod_2.Allocate(numb_node_i); aux_prod_2.Assign(0, numb_node_i - 1, 0); for(int i = 0;i < numb_node_i;++i){ for(int j = 0;j < numb_node_p;++j){ int id_S = pos_S[ l2g[ list_node_p[j] ] ]; aux_prod_2(i) += Uip(i,j) * x_p(id_S); } } Vector<double, int> y_i; y_i.Allocate(numb_node_i); for(int i = 0;i < numb_node_i;++i){ y_i(i) = z(i) - aux_prod_2(i); } // Calculate x_i Vector<double, int> x_i; DirectSolver::Backward(x_i, Kii_lu, y_i); // Assemble x_local for(int i = 0;i < numb_node_i;++i){ x_local(list_node_i[i]) = x_i(i); } for(int i = 0;i < numb_node_p;++i){ x_local(list_node_p[i]) = x_p(i); } }
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; }
Type objective_function<Type>::operator() () { // data: DATA_MATRIX(x_ij); DATA_VECTOR(y_i); DATA_IVECTOR(k_i); // vector of IDs DATA_INTEGER(n_k); // number of IDs DATA_INTEGER(n_j); // number of IDs DATA_VECTOR(b1_cov_re_i); // predictor data for random slope DATA_VECTOR(sigma1_cov_re_i); // predictor data for random slope //DATA_VECTOR(sigma2_cov_re_i); // predictor data for random slope // parameters: PARAMETER_VECTOR(b_j) PARAMETER_VECTOR(sigma_j); PARAMETER(log_b0_sigma); PARAMETER_VECTOR(b0_k); PARAMETER(log_b1_sigma); PARAMETER_VECTOR(b1_k); PARAMETER(log_sigma0_sigma); PARAMETER(log_sigma1_sigma); PARAMETER_VECTOR(sigma0_k); PARAMETER_VECTOR(sigma1_k); int n_data = y_i.size(); // get number of data points to loop over // Linear predictor vector<Type> linear_predictor_i(n_data); vector<Type> linear_predictor_sigma_i(n_data); linear_predictor_i = x_ij*b_j; linear_predictor_sigma_i = x_ij*sigma_j; Type nll = 0.0; // initialize negative log likelihood for(int i = 0; i < n_data; i++){ nll -= dnorm( y_i(i), b0_k(k_i(i)) + b1_k(k_i(i)) * b1_cov_re_i(i) + linear_predictor_i(i), sqrt(exp( sigma0_k(k_i(i)) + sigma1_k(k_i(i)) * sigma1_cov_re_i(i) + linear_predictor_sigma_i(i))), true); } for(int k = 0; k < n_k; k++){ nll -= dnorm(b0_k(k), Type(0.0), exp(log_b0_sigma), true); nll -= dnorm(b1_k(k), Type(0.0), exp(log_b1_sigma), true); nll -= dnorm(sigma0_k(k), Type(0.0), exp(log_sigma0_sigma), true); nll -= dnorm(sigma1_k(k), Type(0.0), exp(log_sigma1_sigma), true); //nll -= dnorm(sigma2_k(k), Type(0.0), exp(log_sigma2_sigma), true); } // Reporting Type b0_sigma = exp(log_b0_sigma); Type b1_sigma = exp(log_b1_sigma); Type sigma0_sigma = exp(log_sigma0_sigma); Type sigma1_sigma = exp(log_sigma1_sigma); //Type sigma2_sigma = exp(log_sigma2_sigma); vector<Type> b1_b1_k(n_k); vector<Type> sigma1_sigma1_k(n_k); for(int k = 0; k < n_k; k++){ // these are fixed-effect slopes + random-effect slopes b1_b1_k(k) = b_j(n_j) + b1_k(k); sigma1_sigma1_k(k) = sigma_j(n_j) + sigma1_k(k); } REPORT( b0_k ); REPORT( b1_k ); REPORT( b_j ); REPORT( sigma0_k ); REPORT( sigma1_k ); //REPORT( sigma2_k ); REPORT(b0_sigma); REPORT(b1_sigma); REPORT(sigma0_sigma); REPORT(sigma1_sigma); //REPORT(sigma2_sigma); REPORT(b1_b1_k); REPORT(sigma1_sigma1_k); //ADREPORT( b0_k ); //ADREPORT( b1_k ); //ADREPORT( b_j ); //ADREPORT( sigma0_k ); //ADREPORT( sigma1_k ); //ADREPORT( sigma2_k ); //ADREPORT(b0_sigma); //ADREPORT(b1_sigma); //ADREPORT(sigma0_sigma); //ADREPORT(sigma1_sigma); //ADREPORT(sigma2_sigma); //ADREPORT(b1_b1_k); //ADREPORT(sigma1_sigma1_k); return nll; }