예제 #1
0
double
gsl_ran_dirichlet_lnpdf (const size_t K,
                         const double alpha[], const double theta[])
{
  /*We calculate the log of the pdf to minimize the possibility of overflow */
  size_t i;
  double log_p = 0.0;
  double sum_alpha = 0.0;

  for (i = 0; i < K; i++)
    {
      log_p += (alpha[i] - 1.0) * log (theta[i]);
    }

  for (i = 0; i < K; i++)
    {
      sum_alpha += alpha[i];
    }

  log_p += gsl_sf_lngamma (sum_alpha);

  for (i = 0; i < K; i++)
    {
      log_p -= gsl_sf_lngamma (alpha[i]);
    }

  return log_p;
}
예제 #2
0
static double
normal_approx (const double x, const double nu)
{
  double y;
  double num;
  double diff;
  double q;
  int i;
  double lg1, lg2;

  y = 1 / sqrt (1 + x * x / nu);
  num = 1.0;
  q = 0.0;
  diff = 2 * GSL_DBL_EPSILON;
  for (i = 2; (i < MAXI) && (diff > GSL_DBL_EPSILON); i += 2)
    {
      diff = q;
      num *= y * y * (i - 1) / i;
      q += num / (nu + i);
      diff = q - diff;
    }
  q += 1 / nu;
  lg1 = gsl_sf_lngamma (nu / 2.0);
  lg2 = gsl_sf_lngamma ((nu + 1.0) / 2.0);

  diff = lg2 - lg1;
  q *= pow (y, nu) * exp (diff) / sqrt (M_PI);

  return q;
}
예제 #3
0
파일: rndutils.c 프로젝트: samer--/plrand
/* Computes log of vector Beta function */
double vector_betaln(long n, double *a)
{
	long i;
	double sum_a=0, sum_ga=0;
	for (i=0; i<n; i++) { sum_a += a[i]; sum_ga += gsl_sf_lngamma(a[i]); }
	return sum_ga - gsl_sf_lngamma(sum_a);
}
double log_dirichlet_likelihood(const double sum,
                                const double prior_sum,
                                const std::vector<int>& counts,
                                bool debug) {
  double val = 0.0;
  int length = counts.size();

  double prior_value = prior_sum / (double)length;
  val += gsl_sf_lngamma(prior_sum);
  val -= (double)length * gsl_sf_lngamma(prior_value);

  if(debug) cout << "Likelihood (" << sum << "," << prior_sum << "," << 
              prior_value << "," << length << ") = " << val << endl;

  for(int ii = 0; ii < length; ++ii) {

    if(debug) cout << "\tGAMMA(" << prior_value << " + " <<  
                (double)counts[ii] << " = " << prior_value + 
                (double)counts[ii] <<  ") -> " << val << endl;
    val += gsl_sf_lngamma(prior_value + (double)counts[ii]);
  }
  val -= gsl_sf_lngamma(prior_sum + sum);

  if(debug) cout << endl;

  return val;
}
double lda_m_step(lda* model, lda_suff_stats* ss) {
    int k, w;
    double lhood = 0;
    for (k = 0; k < model->ntopics; k++)
    {
        gsl_vector ss_k = gsl_matrix_column(ss->topics_ss, k).vector;
        gsl_vector log_p = gsl_matrix_column(model->topics, k).vector;
        if (LDA_USE_VAR_BAYES == 0)
        {
            gsl_blas_dcopy(&ss_k, &log_p);
            normalize(&log_p);
            vct_log(&log_p);
        }
        else
        {
            double digsum = sum(&ss_k)+model->nterms*LDA_TOPIC_DIR_PARAM;
            digsum = gsl_sf_psi(digsum);
            double param_sum = 0;
            for (w = 0; w < model->nterms; w++)
            {
                double param = vget(&ss_k, w) + LDA_TOPIC_DIR_PARAM;
                param_sum += param;
                double elogprob = gsl_sf_psi(param) - digsum;
                vset(&log_p, w, elogprob);
                lhood += (LDA_TOPIC_DIR_PARAM - param) * elogprob + gsl_sf_lngamma(param);
            }
            lhood -= gsl_sf_lngamma(param_sum);
        }
    }
    return(lhood);
}
static double neg_log_evidence_i(const struct data_t *data,
                                 const int *anX, const double* adLambda,
                                 const double* aadLnGammaLambda0)
{
    int j;
    const int S = data->S, N = data->N;
    double dLogE = 0.0, dLogEAlpha = 0.0, dSumAlpha = 0.0,
        dSumAlphaN = 0.0;

    for (j = 0; j < S; j++) {
        const double n = anX[j * N];
        const double dAlpha = exp(adLambda[j]);
        const double dAlphaN = n + dAlpha;

        dLogEAlpha += aadLnGammaLambda0[j];
        dSumAlpha += dAlpha;
        dSumAlphaN += dAlphaN;
        dLogE -= n ? gsl_sf_lngamma(dAlphaN) : aadLnGammaLambda0[j] ;
    }

    dLogEAlpha -= gsl_sf_lngamma(dSumAlpha);
    dLogE += gsl_sf_lngamma(dSumAlphaN);

    return dLogE + dLogEAlpha;
}
예제 #7
0
double
gsl_ran_beta_pdf (const double x, const double a, const double b)
{
  if (x < 0 || x > 1)
    {
      return 0 ;
    }
  else 
    {
      double p;

      double gab = gsl_sf_lngamma (a + b);
      double ga = gsl_sf_lngamma (a);
      double gb = gsl_sf_lngamma (b);
      
      if (x == 0.0 || x == 1.0) 
        {
          p = exp (gab - ga - gb) * pow (x, a - 1) * pow (1 - x, b - 1);
        }
      else
        {
          p = exp (gab - ga - gb + log(x) * (a - 1)  + log1p(-x) * (b - 1));
        }

      return p;
    }
}
예제 #8
0
double
gsl_ran_tdist_pdf (const double x, const double nu)
{
  double p;

  double lg1 = gsl_sf_lngamma (nu / 2);
  double lg2 = gsl_sf_lngamma ((nu + 1) / 2);

  p = ((exp (lg2 - lg1) / sqrt (M_PI * nu)) 
       * pow ((1 + x * x / nu), -(nu + 1) / 2));
  return p;
}
예제 #9
0
double
gsl_ran_negative_binomial_pdf (const unsigned int k, const double p, double n)
{
  double P;

  double f = gsl_sf_lngamma (k + n) ;
  double a = gsl_sf_lngamma (n) ;
  double b = gsl_sf_lngamma (k + 1.0) ;

  P = exp(f-a-b) * pow (p, n) * pow (1 - p, (double)k);
  
  return P;
}
예제 #10
0
double compute_lda_lhood(lda_post* p) {
  int k, n;
  int K = p->model->ntopics, N = p->doc->nterms;

  double gamma_sum = sum(p->gamma);
  double lhood =
    gsl_sf_lngamma(sum(p->model->alpha)) -
    gsl_sf_lngamma(gamma_sum);
  vset(p->lhood, K, lhood);
  
  double influence_term = 0.0;
  double digsum = gsl_sf_psi(gamma_sum);
  for (k = 0; k < K; k++) {
    if (p->doc_weight != NULL) {
      //	  outlog("doc weight size: %d", p->doc_weight->size);
      assert (K == p->doc_weight->size);
      double influence_topic = gsl_vector_get(p->doc_weight, k);
      if (FLAGS_model == "dim"
	  || FLAGS_model == "fixed") {
	influence_term = - ((influence_topic * influence_topic
			     + FLAGS_sigma_l * FLAGS_sigma_l)
			    / 2.0 / (FLAGS_sigma_d * FLAGS_sigma_d));
	// Note that these cancel with the entropy.
	//     - (log(2 * PI) + log(FLAGS_sigma_d)) / 2.0);
      }
    }
    double e_log_theta_k = gsl_sf_psi(vget(p->gamma, k)) - digsum;
    double lhood_term =
      (vget(p->model->alpha, k)-vget(p->gamma, k)) * e_log_theta_k +
      gsl_sf_lngamma(vget(p->gamma, k)) -
      gsl_sf_lngamma(vget(p->model->alpha, k));
    
    for (n = 0; n < N; n++) {
      if (mget(p->phi, n, k) > 0) {
	lhood_term +=
	  p->doc->count[n]*
	  mget(p->phi, n, k) *
	  (e_log_theta_k
	   + mget(p->model->topics, p->doc->word[n], k)
	   - mget(p->log_phi, n, k));
      }
    }
    vset(p->lhood, k, lhood_term);
    lhood += lhood_term;
    lhood += influence_term;
  }
  
  return(lhood);
}
예제 #11
0
double pp_model::log_likelihood_length_and_count(double t, unsigned long long int r){
    if(t<=0)
      return 0;
    if(!r)
      return m_likelihood_term_zero - m_alpha*log(m_beta+t);
    return m_likelihood_term + gsl_sf_lngamma(r+m_alpha) - (r+m_alpha)*log(m_beta+t);
}
예제 #12
0
scalar sasfit_peak_student_t_area(scalar x, sasfit_param * param)
{
	scalar z;
	scalar bckgr, a0, area, center, width, shape;

	SASFIT_ASSERT_PTR( param );

	sasfit_get_param(param, 5, &area, &center, &width, &shape, &bckgr);

	SASFIT_CHECK_COND1((width <= 0), param, "width(%lg) <= 0",width);
	SASFIT_CHECK_COND1((shape <= 0), param, "shape(%lg) <= 0",shape);
	
	a0 = area/(width*sqrt(M_PI*shape))*exp(gsl_sf_lngamma((shape+1.)/2.)-gsl_sf_lngamma(shape/2.));
	z = (x-center)/width;
	return bckgr+a0*pow(1+z*z/shape,-(shape+1.)/2.);
}
예제 #13
0
파일: gamma.c 프로젝트: CNMAT/CNMAT-Externs
double
gsl_ran_gamma_pdf (const double x, const double a, const double b)
{
  if (x < 0)
    {
      return 0 ;
    }
  else if (x == 0)
    {
      if (a == 1)
        return 1/b ;
      else
        return 0 ;
    }
  else if (a == 1)
    {
      return exp(-x/b)/b ;
    }
  else 
    {
      double p;
      double lngamma = gsl_sf_lngamma (a);
      p = exp ((a - 1) * log (x/b) - x/b - lngamma)/b;
      return p;
    }
}
예제 #14
0
/*note assuming you are using inverse W matrix*/
double dLogWishartB(gsl_matrix *ptInvW, int nD, double dNu, int bInv)
{
  int i = 0;
  double dRet = 0.0, dT = 0.0;
  double dLogDet = 0.0, dD = (double) nD;
  gsl_matrix* ptTemp = gsl_matrix_alloc(nD,nD);

  gsl_matrix_memcpy(ptTemp, ptInvW);

  dLogDet = decomposeMatrix(ptTemp, nD);

  if(bInv == TRUE){
    dRet = 0.5*dNu*dLogDet;
  }
  else{
    dRet = -0.5*dNu*dLogDet;
  }
  
  dT = 0.5*dNu*dD*log(2.0);

  dT += 0.25*dD*(dD - 1.0)*log(M_PI);

  for(i = 0; i < nD; i++){
    dT += gsl_sf_lngamma(0.5*(dNu - (double) i));
  }

  gsl_matrix_free(ptTemp);

  return dRet - dT;
}
예제 #15
0
//natural log of Poisson dist: gives more accurate values for small probabilities (because of machine precision)
double logPoisson(double obs, double expect)
{
    if ( expect > 0. && obs >= 0. )
        return -expect + obs * log( expect ) - gsl_sf_lngamma( obs+1 );
    else
        return -1E299;
}
  DEFPoissonYLayer(const pt::ptree& in_options, const DEFInitializer& initializer)
    : options( in_options ), def_data( initializer.def_data ) {
    poisson_rate_intercept = options.get<double>("y_layer.poisson_rate_intercept");
    // set default value
    if (options.get<string>("layer.lf", "") == "") {
      options.put<string>("layer.lf", "id");
    }
    // only support identify link for efficiency
    assert(options.get<string>("layer.lf") == "id");
    lf = get_link_function(options.get<string>("layer.lf"));
    for(int j=0; j<def_data->n_examples(); ++j)
      all_examples.push_back(j);

    train_filter = def_data->get_train_filter();

    // log factorial only deals with sparse data
    log_factorial = shared_ptr<arma::mat>( new arma::mat(*def_data->get_sp_mat()) );
    log_factorial->transform([](double v) {
        return v<=1 ? 0 : gsl_sf_lngamma(v+1); 
      });
    LOG(debug) << "log factorial row=" << log_factorial->n_rows
               << " col=" << log_factorial->n_cols;

    // output the sum of log_factorial for debugging purpose
    if (train_filter) {
      LOG(debug) << "train log factorial "
                 << arma::sum(arma::sum((*log_factorial) % (*train_filter)));
      LOG(debug) << "test log factorial "
                 << arma::sum(arma::sum((*log_factorial) % (1-*train_filter)));
    }
  }
static void calc_z(double **aadZ, const struct data_t *data,
                   const double *adW, double **aadLambda)
{
    int i, k, j;
    const int N = data->N, K = data->K, S = data->S;
    double adStore[K];
    double *aadLngammaLambda0 = (double*)calloc(S*K,sizeof(double));

    for(k = 0; k < K; k++) {
        for(j = 0; j < S; j++) {
            const double dAlpha = exp(aadLambda[k][j]);
            aadLngammaLambda0[k*S +j] = gsl_sf_lngamma(dAlpha);
        }
    }

    for (i = 0; i < N; i ++) {
        double dSum = 0.0;
        double dOffset = BIG_DBL;
        for (k = 0; k < K; k++) {
            double dNegLogEviI =
                neg_log_evidence_i(data, data->aanX + i, aadLambda[k],
                                   aadLngammaLambda0 + k*S);
            if (dNegLogEviI < dOffset)
                dOffset = dNegLogEviI;
            adStore[k] = dNegLogEviI;
        }
        for (k = 0; k < K; k++) {
            aadZ[k][i] = adW[k] * exp(-(adStore[k] - dOffset));
            dSum += aadZ[k][i];
        }
        for (k = 0; k < K; k++)
            aadZ[k][i] /= dSum;
    }
}
예제 #18
0
scalar sasfit_peak_PearsonIVArea(scalar x, sasfit_param * param)
{
	scalar z,u;
	scalar bckgr, a0, area, center, width, shape1, shape2;
	scalar a1,a2,a3,a4;
	scalar a_temp, l_temp, m_temp, nu_temp;
	gsl_sf_result lnr, carg;

	SASFIT_ASSERT_PTR( param );

	sasfit_get_param(param, 6, &area, &center, &width, &shape1, &shape2, &bckgr);

//	a0 = area;
	a1 = center; 
	a2 = width; a_temp = width;
	a3 = shape1; m_temp = shape1;
	a4 = shape2; nu_temp= shape2;

	SASFIT_CHECK_COND1((width <= 0), param, "width(%lg) <= 0",width);
	SASFIT_CHECK_COND1((shape1 <= 0.5), param, "shape1(%lg) <= 1/2",shape1);

	u = a4/(2.*a3);
	l_temp = center+a2*u;
	z = (x-l_temp)/a_temp;
	
	gsl_sf_lngamma_complex_e (m_temp, 0.5*nu_temp, &lnr, &carg);

	a0 = area*pow(exp(lnr.val-gsl_sf_lngamma(m_temp)),2.0)/(a_temp*gsl_sf_beta(m_temp-0.5,0.5));
	return bckgr+a0*pow(1.0+z*z,-m_temp)*exp(-nu_temp*atan(z));
}
예제 #19
0
double
gsl_ran_exppow_pdf (const double x, const double a, const double b)
{
  double p;
  double lngamma = gsl_sf_lngamma (1 + 1 / b);
  p = (1 / (2 * a)) * exp (-pow (fabs (x / a), b) - lngamma);
  return p;
}
double log_dirichlet_likelihood(const double sum,
                                const double prior_scale,
                                const gsl_vector* prior,
                                const std::vector<int>& counts) {
  double val = 0.0;
  int length = counts.size();

  val += gsl_sf_lngamma(prior_scale);
  for(int ii=0; ii < length; ++ii) {
    double prior_value = gsl_vector_get(prior, ii);
    val -= gsl_sf_lngamma(prior_value);
    val += gsl_sf_lngamma(prior_value + (double)counts[ii]);
  }
  val -= gsl_sf_lngamma(prior_scale + sum);

  return val;

}
예제 #21
0
파일: haloes.cpp 프로젝트: surhudm/aum
/// Tinker et al. 2010 mass function normalization. This is defines so that:
//  \int_{0}^{\infty} f(\nu) d\nu = 1
void cosmology::initTinker(double z)
{
    
    double beta= 0.589*pow(1.+z, 0.20);
    double phi =-0.729*pow(1.+z,-0.08);
    double eta =-0.243*pow(1.+z, 0.27);
    double gama= 0.864*pow(1.+z,-0.01);

    double fac1=pow(2.0/gama,eta+0.5);
    double fac2=pow(2.0/gama,eta-phi+0.5);
    double fac3=pow(beta,-2.0*phi);
    double fac4=exp(gsl_sf_lngamma(eta+0.5));
    double fac5=exp(gsl_sf_lngamma(eta-phi+0.5));

    alpTink=(2.0/(fac1*fac4+fac3*fac2*fac5));

    init_Tink=true;
}
예제 #22
0
void pp_model::construct(){
  m_likelihood_term_zero = m_alpha*log(m_beta);
  m_likelihood_term = m_likelihood_term_zero-gsl_sf_lngamma(m_alpha);
  if( m_seasonal_analysis )
    collapse_to_seasons();
  m_poisson_regression = false;
  m_cum_intensity_multipliers = NULL;
  m_shot_noise_rate = 0.0;
}
예제 #23
0
파일: glm.cpp 프로젝트: rforge/mvabund
double NBinGlm::llfunc ( double yi, double mui, double th  )const 
{
    double l=0; //, p=0;

    if (th==0) {
       l = gsl_sf_lngamma(mintol)+log(MAX(yi,mintol)); 
    }
    else if (th>maxth) { // Poisson
       l = -yi*log(mui) + mui + gsl_sf_lngamma(yi+1);
    }
    else {
       l = (yi+th)*log(mui+th) - yi*log(mui) + gsl_sf_lngamma(yi+1) - th*log(th) + gsl_sf_lngamma(th) - gsl_sf_lngamma(yi+th); 
    }

    if (l!=l)  printf("l=nan, theta=%.4f, yi=%.4f, mu=%.4f\n", th, yi, mui);
    
    return -2*l;
}
예제 #24
0
inline
typename ICR::EnsembleLearning::Gamma<T>::data_t
ICR::EnsembleLearning::Gamma<T>::CalcLogNorm(data_t shape, 
				     data_t iscale)  
{
  //These must be greater than zero
  BOOST_ASSERT(iscale>0);
  BOOST_ASSERT(shape>0);
  return shape * std::log(iscale) - gsl_sf_lngamma(shape) ;
}
 virtual double compute_log_q(double z, arma::uword i, arma::uword j) {
   auto shape = lf->f(wshape(i,j));
   auto scale = lf->f(wscale(i,j));
   auto log_q = - gsl_sf_lngamma(shape) - shape*log(scale) + (shape-1)*log(z) - z/scale;
   LOG_IF(fatal, !isfinite(log_q))
          << "shape=" << shape << " scale=" << scale
          << " z=" << z << " log_q=" << log_q;
   assert(isfinite(log_q));
   return log_q;
 }  
예제 #26
0
/// Tinker et al. 2010 mass function normalization. This is defines so that:
//  \int_{0}^{\infty} f(\nu) d\nu = 1
void cosmology::initTinker(double z)
{
    
    double beta= 0.589*pow(1.+z, 0.20);
    double phi =-0.729*pow(1.+z,-0.08);
    double eta =-0.243*pow(1.+z, 0.27);
    double gama= 0.864*pow(1.+z,-0.01);

    double fac1=pow(2.0/gama,eta+0.5);
    double fac2=pow(2.0/gama,eta-phi+0.5);
    double fac3=pow(beta,-2.0*phi);
    double fac4=exp(gsl_sf_lngamma(eta+0.5));
    double fac5=exp(gsl_sf_lngamma(eta-phi+0.5));

    alpTink=(2.0/(fac1*fac4+fac3*fac2*fac5));

    init_Tink=true;
    if(verbose){
        std::cout<<"# Tinker mass function variables initialized\n";
    }
}
예제 #27
0
double
gsl_ran_fdist_pdf (const double x, const double nu1, const double nu2)
{
  if (x < 0)
    {
      return 0 ;
    }
  else
    {
      double p;
      double lglg = (nu1 / 2) * log (nu1) + (nu2 / 2) * log (nu2) ;

      double lg12 = gsl_sf_lngamma ((nu1 + nu2) / 2);
      double lg1 = gsl_sf_lngamma (nu1 / 2);
      double lg2 = gsl_sf_lngamma (nu2 / 2);
      
      p = exp (lglg + lg12 - lg1 - lg2)
        * pow (x, nu1 / 2 - 1) * pow (nu2 + nu1 * x, -nu1 / 2 - nu2 / 2);
      
      return p;
    }
}
static double neg_log_evidence_lambda_pi(const gsl_vector *lambda,
                                         void *params)
{
    int i, j;
    const struct data_t *data = (const struct data_t *) params;
    const int S = data->S, N = data->N, *aanX = data->aanX;
    const double *adPi = data->adPi;
    double dLogE = 0.0, dLogEAlpha = 0.0, dSumAlpha = 0.0, dSumLambda = 0.0;
    double adSumAlphaN[N], dWeight = 0.0;

    for (i = 0; i < N; i++) {
        adSumAlphaN[i] = 0.0;
        dWeight += adPi[i];
    }

    for (j = 0; j < S; j++) {
        const double dLambda = gsl_vector_get(lambda, j);
        const double dAlpha = exp(dLambda);
        dLogEAlpha += gsl_sf_lngamma(dAlpha);
        dSumLambda += dLambda;
        dSumAlpha += dAlpha;
        const double lngammaAlpha0 = gsl_sf_lngamma(dAlpha);
        for (i = 0; i < N; i++) {
            const double dN = aanX[j * N + i];
            const double dAlphaN = dAlpha + dN;
            const double lngammaAlphaN =
                dN ? gsl_sf_lngamma(dAlphaN) : lngammaAlpha0;
            adSumAlphaN[i] += dAlphaN; /*weight by pi*/
            dLogE -= adPi[i] * lngammaAlphaN; /*weight by pi*/
        }
    }
    dLogEAlpha -= gsl_sf_lngamma(dSumAlpha);

    for(i = 0; i < N; i++)
        dLogE += adPi[i] * gsl_sf_lngamma(adSumAlphaN[i]);

    return dLogE + dWeight*dLogEAlpha + GAMMA_NU*dSumAlpha -
        GAMMA_ITA * dSumLambda;
}
예제 #29
0
double sf_mv_lngamma(const double x, const int p)
{
  double v;
  int i;

  v = p*(p-1)*log(M_PI)/4;

  for (i=0; i<p; i++)
    {
      v += gsl_sf_lngamma(x+(1-i)/2);
    }

  return v;
}
예제 #30
0
double
gsl_ran_chisq_pdf (const double x, const double nu)
{
  if (x <= 0)
    {
      return 0 ;
    }
  else
    {
      double p;
      double lngamma = gsl_sf_lngamma (nu / 2);
      
      p = exp ((nu / 2 - 1) * log (x/2) - x/2 - lngamma) / 2;
      return p;
    }
}