/*! \brief Compute y = a1 exp(-x/|a0|) + (1-a1) exp(-x/|a2|) */ static double lmc_exp_exp(double x, const double *a) { double e1, e2; e1 = safe_exp(-x/fabs(a[0])); e2 = safe_exp(-x/(fabs(a[0])+fabs(a[2]))); return a[1]*e1 + (1-a[1])*e2; }
/*! \brief Compute y = a0 exp(-x/|a1|) + a2 exp(-x/(|a1|+|a3|)) + a4 */ static double lmc_exp_5_parm(double x, const double *a) { double e1, e2; e1 = safe_exp(-x/fabs(a[1])); e2 = safe_exp(-x/(fabs(a[1])+fabs(a[3]))); return a[0]*e1 + a[2]*e2 + a[4]; }
/*! \brief Compute 7 parameter exponential function value. * * Compute y = a0 exp(-x/|a1|) + a2 exp(-x/(|a1|+|a3|)) + * a4 exp(-x/(|a1|+|a3|+|a5|)) + a6 */ static double lmc_exp_7_parm(double x, const double *a) { double e1, e2, e3; double fa1, fa3, fa5; fa1 = fabs(a[1]); fa3 = fa1 + fabs(a[3]); fa5 = fa3 + fabs(a[5]); e1 = safe_exp(-x/fa1); e2 = safe_exp(-x/fa3); e3 = safe_exp(-x/fa5); return a[0]*e1 + a[2]*e2 + a[4]*e3 + a[6]; }
// recompute data likelihoods using marginals from the combos // assumes that the genotype combos are in the same order as the likelihoods // assumes that the genotype combos are the same size as the number of samples in the likelihoods // returns the delta from the previous marginals, informative in the case of EM long double marginalGenotypeLikelihoods(list<GenotypeCombo>& genotypeCombos, SampleDataLikelihoods& likelihoods) { long double delta = 0; vector< map<Genotype*, long double> > rawMarginals; rawMarginals.resize(likelihoods.size()); vector< map<Genotype*, long double> >::iterator rawMarginalsItr; // push the marginal likelihoods into the rawMarginals maps for (list<GenotypeCombo>::iterator gc = genotypeCombos.begin(); gc != genotypeCombos.end(); ++gc) { rawMarginalsItr = rawMarginals.begin(); for (GenotypeCombo::const_iterator i = gc->begin(); i != gc->end(); ++i) { const SampleDataLikelihood& sdl = **i; map<Genotype*, long double>& rmgs = *rawMarginalsItr++; map<Genotype*, long double>::iterator rmgsItr = rmgs.find(sdl.genotype); if (rmgsItr == rmgs.end()) { rmgs[sdl.genotype] = gc->posteriorProb; } else { //vector<long double> x; //x.push_back(rmgsItr->second); x.push_back(gc->posteriorProb); //rmgs[sdl.genotype] = logsumexp_probs(x); rmgs[sdl.genotype] = log(safe_exp(rmgsItr->second) + safe_exp(gc->posteriorProb)); } } } // safely add the raw marginal vectors using logsumexp // and use to update the sample data likelihoods rawMarginalsItr = rawMarginals.begin(); for (SampleDataLikelihoods::iterator s = likelihoods.begin(); s != likelihoods.end(); ++s) { vector<SampleDataLikelihood>& sdls = *s; const map<Genotype*, long double>& rawmgs = *rawMarginalsItr++; map<Genotype*, long double> marginals; vector<long double> rawprobs; for (map<Genotype*, long double>::const_iterator m = rawmgs.begin(); m != rawmgs.end(); ++m) { long double p = m->second; marginals[m->first] = p; rawprobs.push_back(p); } long double normalizer = logsumexp_probs(rawprobs); for (vector<SampleDataLikelihood>::iterator sdl = sdls.begin(); sdl != sdls.end(); ++sdl) { long double newmarginal = marginals[sdl->genotype] - normalizer; delta += newmarginal - sdl->marginal; sdl->marginal = newmarginal; } } return delta; }
/*! \brief Compute 9 parameter exponential function value. * * Compute y = a0 exp(-x/|a1|) + a2 exp(-x/(|a1|+|a3|)) + * a4 exp(-x/(|a1|+|a3|+|a5|)) + a6 exp(-x/(|a1|+|a3|+|a5|+|a7|)) + a8 */ static double lmc_exp_9_parm(double x, const double *a) { double e1, e2, e3, e4; double fa1, fa3, fa5, fa7; fa1 = fabs(a[1]); fa3 = fa1 + fabs(a[3]); fa5 = fa3 + fabs(a[5]); fa7 = fa5 + fabs(a[7]); e1 = safe_exp(-x/fa1); e2 = safe_exp(-x/fa3); e3 = safe_exp(-x/fa5); e4 = safe_exp(-x/fa7); return a[0]*e1 + a[2]*e2 + a[4]*e3 + a[6]*e4 + a[8]; }
/*! \brief Compute vac function */ static double lmc_vac_2_parm(double x, const double *a) { /* Fit to function * * y = 1/2 (1 - 1/w) exp(-(1+w)v) + 1/2 (1 + 1/w) exp(-(1-w)v) * * = exp(-v) (cosh(wv) + 1/w sinh(wv)) * * v = x/(2 a0) * w = sqrt(1 - a1) * * For tranverse current autocorrelation functions: * a0 = tau * a1 = 4 tau (eta/rho) k^2 * */ double y, v, det, omega, wv, em, ec, es; double wv_max = 100; v = x/(2*fabs(a[0])); det = 1 - a[1]; em = safe_exp(-v); if (det != 0) { omega = sqrt(fabs(det)); wv = std::min(omega*v, wv_max); if (det > 0) { ec = em*0.5*(safe_exp(wv)+safe_exp(-wv)); es = em*0.5*(safe_exp(wv)-safe_exp(-wv))/omega; } else { ec = em*cos(wv); es = em*sin(wv)/omega; } y = ec + es; } else { y = (1+v)*em; } return y; }
/*! \brief Compute y = (1-a0)*exp(-(x/|a2|)^|a3|)*cos(x*|a1|) + a0*exp(-(x/|a4|)^|a5|) */ static double lmc_pres_6_parm(double x, const double *a) { double term1, term2, term3; double pow_max = 10; term3 = 0; if ((a[4] != 0) && (a[0] != 0)) { double power = std::min(fabs(a[5]), pow_max); term3 = a[0] * safe_exp(-pow((x/fabs(a[4])), power)); } term1 = 1-a[0]; term2 = 0; if ((term1 != 0) && (a[2] != 0)) { double power = std::min(fabs(a[3]), pow_max); term2 = safe_exp(-pow((x/fabs(a[2])), power)) * cos(x*fabs(a[1])); } return term1*term2 + term3; }
double Logistic::execute () { int icase ; double *tptr, term, sum1, sum2 ; sum1 = sum2 = 0.0 ; for (icase=0 ; icase<ncases ; icase++) { tptr = tset + (ninputs + 1) * icase ; // This case predict ( tptr , &term ) ; // Log odds ratio sum1 += term * tptr[ninputs] ; // Output stored after inputs sum2 += log ( 1.0 + safe_exp ( term )) ; } // For all training cases return sum1 - sum2 ; }
static double logit_crit ( double *x ) { int i ; double x1, y1, x2, y2, x3, y3 ; for (i=0 ; i<local_logistic->ninputs ; i++) local_logistic->coefs[i] = safe_exp ( x[i] ) ; glob_min ( -20.0 , 20.0 , 5 , 0 , -1.e160 , logit_unicrit , &x1 , &y1 , &x2 , &y2 , &x3 , &y3 ) ; y2 = brentmin ( 50 , -1.e160 , 1.e-10 , 1.e-10 , logit_unicrit , &x1 , &x2 , &x3 , y2 ) ; local_logistic->coefs[local_logistic->ninputs] = x2 ; return -y2 ; }
/* 多変量正規分布 N(Μ,Σ)の X に対する値を計算する。 単変量の正規分布は上の関数を使うこと。 */ double get_multi_norm(double *mean, double **covmat, double *X, int dim) { double *tmp1 = NULL, *tmp2 = NULL; double det, **invmat = NULL; double val; tmp1 = del_vec(X, mean, dim); tmp2 = tmp1; invmat = matinv(dim, covmat, &det); tmp1 = vecxmat(tmp1, dim, invmat, dim, dim); val = dvid(safe_exp(-0.5 * innerprod(dim, tmp1, tmp2)), (pow(2.0 * M_PI, dim / 2.0) * sqrt(det))); free_double_matrix(invmat); free_double_vector(tmp1); free_double_vector(tmp2); return val; }
/*! \brief Compute y = a1 exp(-x/|a0|) */ static double lmc_exp_two_parm(double x, const double *a) { return a[1]*safe_exp(-x/fabs(a[0])); }
/*! \brief Compute y = exp(-x/|a0|) */ static double lmc_exp_one_parm(double x, const double *a) { return safe_exp(-x/fabs(a[0])); }
/* 単変量正規分布 N(μ,σ)の X における値を計算する。 多変量の正規分布には対応していないので注意。 */ double get_val_norm(double mean, double sigma, double X) { return mult(dvid(1.0, mult(sqrt(2.0 * M_PI), sigma)), safe_exp(mult((-1.0 / 2.0), pow2(dvid((X - mean), sigma))))); }