task autonomous() { StartTask(descore); StartTask(velocitycalculator); if(SensorValue[J1]) // blue { if(SensorValue[J2]) //normal { // RAM ArmWall(); ForwardTillStop(127); } else // oppo { Gamma(); } } else // red { if(SensorValue[J2]) // normal { Beta(); } else // oppo { Delta(); } } StopTask(velocitycalculator); }
void CParam::S4_alpha_i(CData &Data) { is_accept_vec(3) = 0 ; // if ( normC < 0 ) normC = normC_fn(Beta, Data) ; // Ver_1_4_1 for (int i_pheno=0; i_pheno<n_pheno; i_pheno++){ // V_1_3_5 ; Update each alpha_i arma::mat Beta_prop = Beta ; RandVec = Rcpp::rnorm(1, Beta(i_pheno,i_pheno), Data.stepsize_alpha ) ; Beta_prop(i_pheno,i_pheno) = RandVec(0) ; double logP_numer = R::dnorm(Beta_prop(i_pheno,i_pheno), Data.theta_alpha, sqrt(Data.tau2_alpha),1) ; // log=TRUE double logP_denom = R::dnorm(Beta(i_pheno,i_pheno), Data.theta_alpha, sqrt(Data.tau2_alpha),1) ; // log=TRUE double normC_prop = normC_fn(Beta_prop, Data) ; for (int i_SNP=0; i_SNP<n_SNP; i_SNP++){ double e_it = E_mat(i_pheno,i_SNP) ; logP_numer = logP_numer + log( normC ) + Beta_prop(i_pheno,i_pheno) * e_it ; logP_denom = logP_denom + log( normC_prop ) + Beta(i_pheno,i_pheno) * e_it ; } double accept_prob = exp( logP_numer - logP_denom ) ; accept_prob_vec(3) = accept_prob ; RandVec = Rcpp::runif(1, 0, 1) ; if ( accept_prob >= RandVec(0) ){ Beta = Beta_prop ; normC = normC_prop ; is_accept_vec(3) = is_accept_vec(3) + 1 ; } } // for (int i=0; i<n_pheno; i++){ is_accept_vec(3) = 1.0 / n_pheno * is_accept_vec(3) ; }
void CParam::S1_e_it(CData &Data) { for (int i_SNP=0; i_SNP<n_SNP; i_SNP++){ for (int i=0; i<n_pheno; i++){ if ( ( Data.Y(i,i_SNP) > 0 ) & ( Data.Y(i,i_SNP) <= Data.threshold_on ) ){ // V 1.3.1 -> other case: e_it is fixed in CParam::Initialize, // i.e., e_it=0 if y_it<=0 and 1 if y_it>=Data.threshold_on double unnorm_logprob0 = 0.0 ; double unnorm_logprob1 = Beta(i,i) ; for (int j=0; j<n_pheno; j++){ if (G_mat(i,j)==1) unnorm_logprob1 = unnorm_logprob1 + Beta(i,j) * E_mat(j,i_SNP) ; } // Note: Not count G_mat(i,j)=0 or G_mat(i,j)=9,i.e., diagonal unnorm_logprob0 = unnorm_logprob0 + R::dnorm(Data.Y(i,i_SNP),0,1,1) ; // log = T unnorm_logprob1 = unnorm_logprob1 + R::dlnorm(Data.Y(i,i_SNP),mu_vec(i),sqrt(sig2_vec(i)),1) ; // log = T double prob_e_it = 1.0 / ( 1.0 + exp(unnorm_logprob0-unnorm_logprob1) ) ; // Note: p1 = f1/(f1+f0) = 1 / (1+f0/f1) = 1 / (1+exp(log f0 - log f1)) RandVec = Rcpp::runif(1,0,1) ; if ( prob_e_it >= RandVec(0) ){ E_mat(i,i_SNP) = 1 ; } else { E_mat(i,i_SNP) = 0 ; } } // To check loglikelihood if (E_mat(i,i_SNP)==1){ loglikelihood = loglikelihood + R::dlnorm(Data.Y(i,i_SNP),mu_vec(i),sqrt(sig2_vec(i)),1) ; } else { loglikelihood = loglikelihood + R::dnorm(Data.Y(i,i_SNP),0,1,1) ; } } } is_accept_vec(0) = 1 ; }
/* Very fast binomial sampler. * Returns the number of successes out of n trials, with success probability p. */ int Binomial(RndState *S, double p, int n) { int r = 0; if(isnan(p)) return 0; if(p < DBL_EPSILON) return 0; if(p >= 1-DBL_EPSILON) return n; if((p > 0.5) && (n < 15)) { /* Coin flip method. This takes O(n) time. */ int i; for(i=0;i<n;i++) { if(Uniform(S) < p) r++; } return r; } if(n*p < 10) { /* Waiting time method. This takes O(np) time. */ double q = -log(1-p), e = -log(Uniform(S)), s; r = n; for(s = e/r; s <= q; s += e/r) { r--; if(r == 0) break; e = -log(Uniform(S)); } r = n-r; return r; } if (1) { /* Recursive method. This makes O(log(log(n))) recursive calls. */ int i = (int)floor(p*(n+1)); double b = Beta(S,i, n+1-i); if(b <= p) r = i + Binomial(S,(p-b)/(1-b), n-i); else r = i - 1 - Binomial(S,(b-p)/b, i-1); return r; } }
bool FeasibleUpwardPlanarSubgraph::constructMergeGraph( GraphCopy &M, adjEntry adj_orig, const List<edge> &orig_edges) { CombinatorialEmbedding Beta(M); //set ext. face of Beta adjEntry ext_adj = M.copy(adj_orig->theEdge())->adjSource(); Beta.setExternalFace(Beta.rightFace(ext_adj)); FaceSinkGraph fsg(Beta, M.copy(adj_orig->theNode())); SList<node> aug_nodes; SList<edge> aug_edges; SList<face> fList; fsg.possibleExternalFaces(fList); // use this method to call the methode checkForest() node v_ext = fsg.faceNodeOf(Beta.externalFace()); OGDF_ASSERT(v_ext != 0); fsg.stAugmentation(v_ext, M, aug_nodes, aug_edges); //add the deleted edges for(edge eOrig: orig_edges) { node a = M.copy(eOrig->source()); node b = M.copy(eOrig->target()); M.newEdge(a, b); } return (isAcyclic(M)); }
double MVTR::pdf(dPtr dp, bool logscale)const{ Ptr<DataType> d = DAT(dp); const Vec &y(d->y()); const Vec &X(d->x()); double ans = dmvt(y, X*Beta(), Siginv(), nu(), ldsi(), true); return logscale ? ans : exp(ans); }
Module::ReturnType FeasibleUpwardPlanarSubgraph::call( const Graph &G, GraphCopy &FUPS, adjEntry &extFaceHandle, List<edge> &delEdges, bool multisources) { FUPS = GraphCopy(G); delEdges.clear(); node s_orig; hasSingleSource(G, s_orig); List<edge> nonTreeEdges_orig; getSpanTree(FUPS, nonTreeEdges_orig, true, multisources); CombinatorialEmbedding Gamma(FUPS); nonTreeEdges_orig.permute(); // random order //insert nonTreeEdges while (!nonTreeEdges_orig.empty()) { // make identical copy GC of Fups //and insert e_orig in GC GraphCopy GC = FUPS; edge e_orig = nonTreeEdges_orig.popFrontRet(); //node a = GC.copy(e_orig->source()); //node b = GC.copy(e_orig->target()); GC.newEdge(e_orig); if (UpwardPlanarity::upwardPlanarEmbed_singleSource(GC)) { //upward embedded the fups and check feasibility CombinatorialEmbedding Beta(GC); //choose a arbitrary feasibel ext. face FaceSinkGraph fsg(Beta, GC.copy(s_orig)); SList<face> ext_faces; fsg.possibleExternalFaces(ext_faces); OGDF_ASSERT(!ext_faces.empty()); Beta.setExternalFace(ext_faces.front()); GraphCopy M = GC; // use a identical copy of GC to constrcut the merge graph of GC adjEntry extFaceHandle_cur = getAdjEntry(Beta, GC.copy(s_orig), Beta.externalFace()); adjEntry adj_orig = GC.original(extFaceHandle_cur->theEdge())->adjSource(); if (constructMergeGraph(M, adj_orig, nonTreeEdges_orig)) { FUPS = GC; extFaceHandle = FUPS.copy(GC.original(extFaceHandle_cur->theEdge()))->adjSource(); continue; } else { //Beta is not feasible delEdges.pushBack(e_orig); } } else { // not ok, GC is not feasible delEdges.pushBack(e_orig); } } return Module::retFeasible; }
double MvReg::loglike(const Vector &beta_siginv) const { Matrix Beta(xdim(), ydim()); Vector::const_iterator it = beta_siginv.cbegin(); std::copy(it, it + Beta.size(), Beta.begin()); it += Beta.size(); SpdMatrix siginv(ydim()); siginv.unvectorize(it, true); return log_likelihood_ivar(Beta, siginv); }
void CParam::S5_beta_ij(CData &Data) { is_accept_vec(4) = 0 ; int w_cur = 0 ; // just for calculation of is_accept_vec( ) for (int i=0; i<(n_pheno-1); i++){ for (int j=(i+1); j<n_pheno; j++){ if ( G_mat(i,j)==1 ){ w_cur ++ ; arma::mat Beta_prop = Beta ; double temp_beta_prop = 0.0 ; while ( temp_beta_prop <= 0.0 ){ temp_beta_prop = rtruncNorm_uppertail_fn(Beta(i,j), Data.stepsize_beta, 0) ; } Beta_prop(i,j) = temp_beta_prop ; Beta_prop(j,i) = temp_beta_prop ; double logP_numer = R::dgamma(Beta_prop(i,j),Data.a_beta,(1.0/Data.b_beta),1) ; // (shape,scale,log), i.e., b_beta = rate // V_1_3_5 double logP_denom = R::dgamma(Beta(i,j),Data.a_beta,(1.0/Data.b_beta),1) ; // double normC_prop = normC_fn(Beta_prop, Data) ; for (int i_SNP=0; i_SNP<n_SNP; i_SNP++){ double e_it = E_mat(i,i_SNP) ; double e_jt = E_mat(j,i_SNP) ; logP_numer = logP_numer + log(normC) + Beta_prop(i,j) * e_it * e_jt ; logP_denom = logP_denom + log(normC_prop) + Beta(i,j) * e_it * e_jt ; } double logQ_numer = log( dtruncnorm_uppertail_fn(Beta(i,j), 0, Beta_prop(i,j), Data.stepsize_beta) ) ; // CHECK double logQ_denom = log( dtruncnorm_uppertail_fn(Beta_prop(i,j), 0, Beta(i,j), Data.stepsize_beta) ) ; double accept_prob = exp( logP_numer - logP_denom + logQ_numer - logQ_denom ) ; accept_prob_vec(4) = accept_prob ; RandVec = Rcpp::runif(1, 0, 1) ; if ( accept_prob >= RandVec(0) ){ Beta = Beta_prop ; normC = normC_prop ; is_accept_vec(4) = is_accept_vec(4) + 1 ; } } } } if ( w_cur == 0 ){ is_accept_vec(4) = 0.2 ; } else { is_accept_vec(4) = 1.0 / w_cur * is_accept_vec(4) ; } }
double Basis(int m, int M, float x, float xmin, float DX){ double y = 0; double xm = xmin + (m * DX); double z = abs((double)(x - xm) / (double)DX); if (z < 2.0) { z = 2 - z; y = 0.25 * (z*z*z); z -= 1.0; if (z > 0) y -= (z*z*z); } // Boundary conditions, if any, are an additional addend. if (m == 0 || m == 1) y += Beta(m, M) * Basis(-1, M, x, xmin, DX); else if (m == M-1 || m == M) y += Beta(m, M) * Basis(M+1, M, x, xmin, DX); return y; }
void MultivariateModel ::ComputeAMatrix(const Realizations &R) { /// It computes the A matrix (ref documentation) based on the orthonormal basis B and the beta coefficients MatrixType NewA(m_ManifoldDimension, m_NbIndependentSources); for(int i = 0; i < m_NbIndependentSources; ++i) { VectorType Beta(m_ManifoldDimension, 0.0); for(size_t j = 0; j < m_ManifoldDimension - 1; ++j) { std::string Number = std::to_string(int(j + i*(m_ManifoldDimension - 1))); Beta(j) = R.at( "Beta#" + Number, 0); } NewA.set_column(i, m_OrthogonalBasis * Beta); } m_AMatrix = NewA; }
int main(int argc, char* argv[ ]) { double v, w, x, y, z; double timer = omp_get_wtime(); int thread_count = 1; if (argc>1) thread_count = strtol(argv[1], NULL, 10); # pragma omp parallel num_threads(thread_count) { # pragma omp sections { # pragma omp section { v = Alpha( ); printf("v = %lf\n", v); } # pragma omp section { w = Beta( ); printf("w = %lf\n", w); } } # pragma omp sections { # pragma omp section { x = Gamma(v, w); printf("x = %lf\n", x); } # pragma omp section { y = Delta( ); printf("y = %lf\n", y); } } } z = Epsilon(x, y); printf("z = %lf\n", z); timer = omp_get_wtime() - timer; printf("timer = %lf\n", timer); return 0; }
MVTR::MvtRegModel(const Mat &X,const Mat &Y, bool add_intercept) : ParamPolicy(new MatrixParams(X.ncol() + add_intercept,Y.ncol()), new SpdParams(Y.ncol()), new UnivParams(default_df)) { Mat XX(add_intercept? cbind(1.0,X) : X); QR qr(XX); Mat Beta(qr.solve(qr.QtY(Y))); Mat resid = Y - XX* Beta; uint n = XX.nrow(); Spd Sig = resid.t() * resid/n; set_Beta(Beta); set_Sigma(Sig); for(uint i=0; i<n; ++i){ Vec y = Y.row(i); Vec x = XX.row(i); NEW(MvRegData, dp)(y,x); DataPolicy::add_data(dp); } }
Vec MVTR::predict(const Vec &x)const{ return x*Beta(); }
double LRM::Loglike(Vec &g, Mat &h, uint nd)const{ if(nd>=2) return log_likelihood(Beta(), &g, &h); if(nd==1) return log_likelihood(Beta(), &g, 0); return log_likelihood(Beta(), 0, 0); }
void tracc::UProcessFill() { // User Function called for all entries . // Entry is the entry number in the current tree. // Fills histograms. //cout << "Event "<<Event()<<endl; Float_t xm=0; if(nMCEventg()>0){ MCEventgR mc_ev=MCEventg(0); xm = log(mc_ev.Momentum); acc[0]->Fill(xm,1); h1A[0]-> Fill(xm,1); cout <<Run()<< " " <<Event()<< " "<<mc_ev.Momentum<<" "<<MCEventg(1).Momentum<<" "<< MCEventg(1).Coo[2]<<" "<<endl; cout <<" Particle "<<Particle(0).Momentum<<" "<<NBeta()<<endl; for (int k=0;k<nTrTrack();k++){ cout <<" tracke "<<k<<" "<<TrTrack(k).IsGood()<<endl; } h2->Fill(Particle(0).Momentum/mc_ev.Momentum,1); float xx=pow(float(fabs(Particle(0).Momentum/mc_ev.Momentum)),float(2.7)); h4->Fill(xx,1); h5->Fill(Particle(0).Momentum/mc_ev.Momentum,xx); cout << xx<<endl; h3->Fill(MCEventg(1).Coo[2],1); /* int ng=nEcalHit(); if(ng>0){ EcalHitR ec=EcalHit(ng-1); for(int kk=0;kk<10000000;kk++){ acc[1]->Fill(xm,1); } //cout <<" ne "<<ng<<" "<<ec.Edep<<endl; } */ if(nParticle()>0){ int ptrack = Particle(0).iTrTrack(); int ptrd = Particle(0).iTrdTrack(); if(NParticle()== 1 && ptrack>=0 && ptrd>=0){ //final if acc[1]->Fill(xm,1); h1A[1]-> Fill(xm,1); int pbeta = Particle(0).iBeta(); BetaR *pb = Particle(0).pBeta(); // another way if(pbeta>=0){ //check beta BetaR BetaI=Beta(pbeta); if(fabs(BetaI.Beta) < 2 && pb->Chi2S < 5 && BetaI.Pattern < 4){ if(nTrdTrack()<2){ Int_t Layer1 =0; Int_t Layer2 =0; TrTrackR tr_tr=TrTrack(ptrack); int ptrh=tr_tr.iTrRecHit(0); //pht1 Layer1=TrRecHit(ptrh).Layer; ptrh=tr_tr.iTrRecHit(tr_tr.NTrRecHit()-1); //pht2 Layer2=TrRecHit(ptrh).Layer; // alt method for (int k=0;k<tr_tr.NTrRecHit();k++){ h1->Fill(tr_tr.pTrRecHit(k)->Layer,1); } } } } } } } }
uint MvReg::xdim() const { return Beta().nrow(); }
uint MvReg::ydim() const { return Beta().ncol(); }
void FUPSSimple::computeFUPS(UpwardPlanRep &UPR, List<edge> &delEdges) { const Graph &G = UPR.original(); GraphCopy FUPS(G); node s_orig; hasSingleSource(G, s_orig); List<edge> nonTreeEdges_orig; bool random = (m_nRuns != 0); getSpanTree(FUPS, nonTreeEdges_orig, random); CombinatorialEmbedding Gamma(FUPS); if (random) nonTreeEdges_orig.permute(); // random order adjEntry extFaceHandle = nullptr; //insert nonTreeEdges while (!nonTreeEdges_orig.empty()) { /* //------------------------------------debug GraphAttributes AG(FUPS, GraphAttributes::nodeGraphics| GraphAttributes::edgeGraphics| GraphAttributes::nodeColor| GraphAttributes::edgeColor| GraphAttributes::nodeLabel| GraphAttributes::edgeLabel ); // label the nodes with their index for(node v : AG.constGraph().nodes) { AG.label(v) = to_string(v->index()); } AG.writeGML("c:/temp/spannTree.gml"); */ // make identical copy FUPSCopy of FUPS //and insert e_orig in FUPSCopy GraphCopy FUPSCopy((const GraphCopy &) FUPS); edge e_orig = nonTreeEdges_orig.popFrontRet(); FUPSCopy.newEdge(e_orig); if (UpwardPlanarity::upwardPlanarEmbed_singleSource(FUPSCopy)) { //upward embedded the fups and check feasibility CombinatorialEmbedding Beta(FUPSCopy); //choose a arbitrary feasibel ext. face FaceSinkGraph fsg(Beta, FUPSCopy.copy(s_orig)); SList<face> ext_faces; fsg.possibleExternalFaces(ext_faces); OGDF_ASSERT(!ext_faces.empty()); Beta.setExternalFace(ext_faces.front()); #if 0 //*************************** debug ******************************** cout << endl << "FUPS : " << endl; for(face ff : Beta.faces) { cout << "face " << ff->index() << ": "; adjEntry adjNext = ff->firstAdj(); do { cout << adjNext->theEdge() << "; "; adjNext = adjNext->faceCycleSucc(); } while(adjNext != ff->firstAdj()); cout << endl; } if (Beta.externalFace() != 0) cout << "ext. face of the graph is: " << Beta.externalFace()->index() << endl; else cout << "no ext. face set." << endl; #endif GraphCopy M((const GraphCopy &) FUPSCopy); // use a identical copy of FUPSCopy to construct the merge graph of FUPSCopy adjEntry extFaceHandle_cur = getAdjEntry(Beta, FUPSCopy.copy(s_orig), Beta.externalFace()); adjEntry adj_orig = FUPSCopy.original(extFaceHandle_cur->theEdge())->adjSource(); List<edge> missingEdges = nonTreeEdges_orig, listTmp = delEdges; missingEdges.conc(listTmp); if (constructMergeGraph(M, adj_orig, missingEdges)) { FUPS = FUPSCopy; extFaceHandle = FUPS.copy(FUPSCopy.original(extFaceHandle_cur->theEdge()))->adjSource(); continue; } else { //Beta is not feasible delEdges.pushBack(e_orig); } } else { // not ok, GC is not feasible delEdges.pushBack(e_orig); } } UpwardPlanRep fups_tmp (FUPS, extFaceHandle); UPR = fups_tmp; }
bool FUPSSimple::constructMergeGraph(GraphCopy &M, adjEntry adj_orig, const List<edge> &orig_edges) { CombinatorialEmbedding Beta(M); //set ext. face of Beta adjEntry ext_adj = M.copy(adj_orig->theEdge())->adjSource(); Beta.setExternalFace(Beta.rightFace(ext_adj)); //*************************** debug ******************************** /* cout << endl << "FUPS : " << endl; for(face ff : Beta.faces) { cout << "face " << ff->index() << ": "; adjEntry adjNext = ff->firstAdj(); do { cout << adjNext->theEdge() << "; "; adjNext = adjNext->faceCycleSucc(); } while(adjNext != ff->firstAdj()); cout << endl; } if (Beta.externalFace() != 0) cout << "ext. face of the graph is: " << Beta.externalFace()->index() << endl; else cout << "no ext. face set." << endl; */ FaceSinkGraph fsg(Beta, M.copy(adj_orig->theNode())); SList<node> aug_nodes; SList<edge> aug_edges; SList<face> fList; fsg.possibleExternalFaces(fList); // use this method to call the methode checkForest() node v_ext = fsg.faceNodeOf(Beta.externalFace()); OGDF_ASSERT(v_ext != 0); fsg.stAugmentation(v_ext, M, aug_nodes, aug_edges); /* //------------------------------------debug GraphAttributes AG(M, GraphAttributes::nodeGraphics| GraphAttributes::edgeGraphics| GraphAttributes::nodeColor| GraphAttributes::edgeColor| GraphAttributes::nodeLabel| GraphAttributes::edgeLabel ); // label the nodes with their index for(node v : AG.constGraph().nodes) { AG.label(v) = to_string(v->index()); } AG.writeGML("c:/temp/MergeFUPS.gml"); */ OGDF_ASSERT(isStGraph(M)); //add the deleted edges for(edge eOrig : orig_edges) { node a = M.copy(eOrig->source()); node b = M.copy(eOrig->target()); M.newEdge(a, b); } return (isAcyclic(M)); }
void MvReg::mle() { set_Beta(suf()->beta_hat()); set_Sigma(suf()->SSE(Beta()) / suf()->n()); }
void HHKinFit2::HHLorentzVector::SetEkeepBeta(double E){ double pnew = Beta()*E; double ptnew = pnew * sin(2.*atan(exp(-Eta()))); SetPtEtaPhiE(ptnew,Eta(),Phi(),E); }
uint MVTR::xdim()const{ return Beta().nrow();}
void CParam::S6_G_beta_ij(CData &Data) { arma::mat G_prop = G_mat ; arma::mat Beta_prop = Beta ; double logQ_numer, logQ_denom ; double logP_numer, logP_denom ; double normC_prop ; bool is_forcein_edge_selected = false ; // double P_G_q, P_G ; // For updated E(i,j) // Step 1 int w_cur = 0 ; int w_max = 0 ; int w_prop ; for (int i=0; i<(n_pheno-1); i++){ for (int j=(i+1); j<n_pheno; j++){ w_max ++ ; w_cur = w_cur + G_mat(i,j) ; } } if ( w_cur==0 ){ w_prop = 1 ; logQ_numer = log(0.5) ; logQ_denom = log(1.0) ; } // q(w|w^q) // q(w^q|w) if ( w_cur==w_max ){ w_prop = w_max - 1 ; logQ_numer = log(0.5) ; logQ_denom = log(1.0) ; } // q(w|w^q) // q(w^q|w) if ( (w_cur > 0) && (w_cur < w_max) ){ RandVec = Rcpp::runif(1, 0, 1) ; if ( RandVec(0) < 0.5 ){ w_prop = w_cur + 1 ; } else { w_prop = w_cur - 1 ; } logQ_denom = log(0.5) ; // q(w^q|w) if ( (w_prop==0) || (w_prop==w_max) ){ logQ_numer = log(1.0) ; } else { logQ_numer = log(0.5) ; } // q(w|w^q) } // Step 2 if ( w_prop > w_cur ){ // step 2-a int id_added = rDiscrete(w_max-w_cur) + 1 ; // 1 ~ total. of empty edges int count_empty_edge = 0 ; for (int i=0; i<(n_pheno-1); i++){ for (int j=(i+1); j<n_pheno; j++){ if ( G_mat(i,j)==0 ){ count_empty_edge++; if ( id_added==count_empty_edge ){ G_prop(i,j) = 1 ; G_prop(j,i) = G_prop(i,j) ; RandVec = Rcpp::rgamma(1, Data.a_betaG, 1.0/Data.b_betaG) ; // q(beta_ij^q) Beta_prop(i,j) = RandVec(0) ; Beta_prop(j,i) = Beta_prop(i,j) ; id_added = -9 ; // if ( Data.PriorSetting==2 ){ // P_G = 1.0 - Data.priorprob_G(i,j) ; // Bernoulli(E_ij=0; p_ij) // P_G_q = Data.priorprob_G(i,j) ; // Bernoulli(E_ij^q=1; p_ij) // } logP_numer = R::dgamma(Beta_prop(i,j),Data.a_beta,(1.0/Data.b_beta),1) ; // f(beta_ij^q|G_ij^q) logP_denom = 0 ; // f(beta_ij|G_ij) cancelled with q(beta_ij) normC_prop = normC_fn(Beta_prop, Data) ; for (int i_SNP=0; i_SNP<n_SNP; i_SNP++){ double e_it = E_mat(i,i_SNP) ; double e_jt = E_mat(j,i_SNP) ; logP_numer = logP_numer - log(normC_prop) + Beta_prop(i,j) * e_it * e_jt ; // f(e_t|alpha,beta^q,G^q) logP_denom = logP_denom - log(normC) + Beta(i,j) * e_it * e_jt ; // f(e_t|alpha,beta,G) } logQ_numer = logQ_numer + 0 ; // q(beta_ij) cancelled with f(beta_ij|G_ij) logQ_denom = logQ_denom + R::dgamma(Beta_prop(i,j),Data.a_betaG,(1.0/Data.b_betaG),1) ; // q(beta_ij^q) } // if ( id_added==count_empty_edge ) } // for ( G_mat(i,j)==0 ) } // for (j) } // for (i) logQ_numer = logQ_numer - log(w_cur) ; // q(G|G^q,w) logQ_denom = logQ_denom - log(w_max-w_cur) ; // q(G^q|G,w^q) } else { // step 2-b int id_deleted = rDiscrete(w_cur)+1 ; // 1 ~ total no. of connected edges int count_connected_edge = 0 ; for (int i=0; i<(n_pheno-1); i++){ for (int j=(i+1); j<n_pheno; j++){ if ( G_mat(i,j)==1 ){ count_connected_edge++; if ( id_deleted==count_connected_edge ){ G_prop(i,j) = 0 ; G_prop(j,i) = G_prop(i,j) ; Beta_prop(i,j) = 0 ; // q(beta_ij^q) Beta_prop(j,i) = Beta_prop(i,j) ; id_deleted = -9 ; if (Data.isforcein==true){ if (Data.E_forcein_mat(i,j)==1) is_forcein_edge_selected = true ; } // if ( Data.PriorSetting==2 ){ // P_G = Data.priorprob_G(i,j) ; // Bernoulli(E_ij=1; p_ij) // P_G_q = 1.0 - Data.priorprob_G(i,j) ; // Bernoulli(E_ij^q=0; p_ij) // } logP_numer = 0 ; // f(beta_ij^q|G_ij^q) cancelled with q(beta_ij^q) logP_denom = R::dgamma(Beta(i,j),Data.a_beta,(1.0/Data.b_beta),1) ; // f(beta_ij|G_ij) normC_prop = normC_fn(Beta_prop, Data) ; for (int i_SNP=0; i_SNP<n_SNP; i_SNP++){ double e_it = E_mat(i,i_SNP) ; double e_jt = E_mat(j,i_SNP) ; logP_numer = logP_numer - log(normC_prop) + Beta_prop(i,j) * e_it * e_jt ; // f(e_t|alpha,beta^q,G^q) logP_denom = logP_denom - log(normC) + Beta(i,j) * e_it * e_jt ; // f(e_t|alpha,beta,G) } logQ_numer = logQ_numer + R::dgamma(Beta(i,j),Data.a_betaG,(1.0/Data.b_betaG),1) ; // q(beta_ij) logQ_denom = logQ_denom + 0 ; // q(beta_ij^q) cancelled with f(beta_ij^q|G_ij^q) } } } } logQ_numer = logQ_numer - log(w_max-w_cur) ; // q(G|G^q,w) logQ_denom = logQ_denom - log(w_cur) ; // q(G^q|G,w^q) } // if ( Data.PriorSetting==1 ){ if ( w_prop > 0 ) logP_numer = logP_numer - log(w_prop) ; // f(G^q) // if w_prop=0, let 1/w_prop = 1, so that log(w_prop) = 0 if ( w_cur > 0 ) logP_denom = logP_denom - log(w_cur) ; // f(G) // if w_cur=0, let 1/w_cur = 1, so that log(w_cur) = 0 // } // if ( Data.PriorSetting==2 ){ // if ( w_prop > 0 ) logP_numer = logP_numer + log(P_G_q) ; // if ( w_cur > 0 ) logP_denom = logP_denom + log(P_G) ; // } // Step 3 double accept_prob = exp( logP_numer - logP_denom + logQ_numer - logQ_denom ) ; if (is_forcein_edge_selected==true) accept_prob = 0 ; accept_prob_vec(5) = accept_prob ; RandVec = Rcpp::runif(1, 0, 1) ; if ( accept_prob >= RandVec(0) ){ Beta = Beta_prop ; G_mat = G_prop ; normC = normC_prop ; is_accept_vec(5) = 1 ; } else { is_accept_vec(5) = 0 ; } }
double MvReg::log_likelihood() const { return log_likelihood_ivar(Beta(), Siginv()); }
Vector MvReg::predict(const Vector &x) const { return x * Beta(); }
uint MVTR::ydim()const{ return Beta().ncol();}
double log_likelihood() const override { Vector g; Matrix h; return Loglike(Beta(), g, h, 0); }