Esempio n. 1
0
 inline double calc_probability_of_sequence(
         NucleotideSequence * seq,
         const ShortReadSequence& short_read,
         double mean_number_of_errors_per_site) const {
     TREESHREW_ASSERT(seq);
     CharacterStateVectorType::const_iterator long_read_start_pos = seq->cbegin();
     CharacterStateVectorType::const_iterator long_read_stop_pos = long_read_start_pos + this->num_active_sites_ - short_read.size() + 1;
     CharacterStateVectorType::const_iterator short_read_begin = short_read.cbegin();
     CharacterStateVectorType::const_iterator short_read_end = short_read.cend();
     unsigned long short_read_size = short_read.size();
     TREESHREW_ASSERT(long_read_stop_pos >= long_read_start_pos);
     unsigned long num_mismatches = 0;
     double prob = 0.0;
     while (long_read_start_pos < long_read_stop_pos) {
         num_mismatches = std::inner_product(
                 short_read_begin,
                 short_read_end,
                 long_read_start_pos,
                 0,
                 std::plus<unsigned int>(),
                 std::not2(std::equal_to<CharacterStateVectorType::value_type>()));
         prob += gsl_ran_binomial_pdf(num_mismatches, mean_number_of_errors_per_site, short_read_size);
         // prob += gsl_ran_poisson_pdf(num_mismatches, (mean_number_of_errors_per_site * short_read_size));
         ++long_read_start_pos;
     }
     return prob;
 }
Esempio n. 2
0
/*
 * This does the pearson above, but computes histogram occupation using a
 * binomial distribution.  This is useful to compute e.g. the pvalue for a
 * vector of tally results from flipping 100 coins, performed 100 times.
 * It automatically cuts off the tails where bin membership isn't large
 * enough to give a good result.
 */
double chisq_binomial(double *observed,double prob,unsigned int kmax,unsigned int nsamp)
{

 unsigned int n,nmax,ndof;
 double expected,delchisq,chisq,pvalue,obstotal,exptotal;

 chisq = 0.0;
 obstotal = 0.0;
 exptotal = 0.0;
 ndof = 0;
 nmax = kmax;
 if(verbose){
   printf("# %7s   %3s      %3s %10s      %10s %9s\n",
           "bit/bin","DoF","X","Y","del-chisq","chisq");
   printf("#==================================================================\n");
 }
 for(n = 0;n <= nmax;n++){
   if(observed[n] > 10.0){
     expected = nsamp*gsl_ran_binomial_pdf(n,prob,nmax);
     obstotal += observed[n];
     exptotal += expected;
     delchisq = (observed[n] - expected)*(observed[n] - expected)/expected;
     chisq += delchisq;
     if(verbose){
       printf("# %5u     %3u   %10.4f %10.4f %10.4f %10.4f\n",
                  n,ndof,observed[n],expected,delchisq,chisq);
     }
     ndof++;
   }
 }
 if(verbose){
   printf("Total:  %10.4f  %10.4f\n",obstotal,exptotal);
   printf("#==================================================================\n");
   printf("Evaluated chisq = %f for %u degrees of freedom\n",chisq,ndof);
 }

 /*
  * Now evaluate the corresponding pvalue.  The only real question
  * is what is the correct number of degrees of freedom.  I'd argue we
  * did use a constraint when we set expected = binomial*nsamp, so we'll
  * go for ndof (count of bins tallied) - 1.
  */
 ndof--;
 pvalue = gsl_sf_gamma_inc_Q((double)(ndof)/2.0,chisq/2.0);
 if(verbose){
   printf("Evaluted pvalue = %6.4f in chisq_binomial.\n",pvalue);
 }

 return(pvalue);

}
Esempio n. 3
0
/*
 * Alpha is the cutoff for the pvalue
 *
 */
void pvalue_alpha_beta(unsigned int n,unsigned int u,unsigned int v,
		       double *_alpha,double *_beta_score) {
  unsigned int i,j,k;
  unsigned int middle=n/2+n%2;
  unsigned int rest=n%2;
  double error0=u+v;
  double alpha0=1.0;
  double beta0=1.0;
  double beta_score=1.0;
  unsigned int k0=1;

  double alpha,beta,error;

  //printf("n = %i middle = %i\n",n,middle);

  for(j=0;j<n/2;j++) {
    int a=middle-j-rest;
    int b=middle+j;
    //printf("%i: ",j);
    double sum=0.0;
    for(k=a;k<=b;k++) {
      double p=gsl_ran_binomial_pdf(k,0.5,n);
      //printf("[%i,%f] ",k,p);
      sum+=p;
    }
    //printf("\n");
    alpha=1-sum;
    k=(b-a+1);
    beta=(b-a+1)*1.0/(n+1);
    //printf("\n   alpha = %lf + beta = %lf = %lf\n",alpha,beta,alpha+beta);    
    error=u*alpha+v*beta;
    beta_score=1.0*k0/k;
    k0=k;

    if (error > error0) {
      break;
    }
    //printf("%u %f\n",j,error);
    error0=error;
    alpha0=alpha;
    beta0=beta;
  }
  if (_alpha != NULL)
    *_alpha=alpha0;

  if (_beta_score != NULL)
    *_beta_score=beta_score;
}
Esempio n. 4
0
int main(){
	int i;
	const int M = 5e7;
	time_t t1, t2, t;
	double mu= 1e-7;
	int L=3e6, T, N;

	/* INIT GSL RNG*/
	t = time(NULL); // time in seconds, used to change the seed of the random generator
	gsl_rng * rng;
 	const gsl_rng_type *typ;
	gsl_rng_env_setup();
	typ=gsl_rng_default;
	rng=gsl_rng_alloc(typ);
	gsl_rng_set(rng,t); // changes the seed of the random generator


	printf("\n - performing %d computations of Bernoulli prob mass fct - ", M);
	time(&t1);
	for(i=0;i<M;i++){
		T=gsl_rng_uniform_int(rng,100); /* time */
		N=gsl_rng_uniform_int(rng, 20); /* nb of mutations*/
		gsl_ran_binomial_pdf((unsigned int) N, mu, T*L);
	}
	time(&t2);
	printf("\nTime ellapsed: %d ", (int) (t2-t1));


	printf("\n - performing %d computations of Poisson prob mass fct- ", M);
	time(&t1);
	for(i=0;i<M;i++){
		T=gsl_rng_uniform_int(rng,100); /* time */
		N=gsl_rng_uniform_int(rng, 20); /* nb of mutations*/
		gsl_ran_poisson_pdf((unsigned int) N, mu*T*L);
	}
	time(&t2);
	printf("\nTime ellapsed: %d \n", (int) (t2-t1));

}
Esempio n. 5
0
static double binomial_ll(gsl_vector *hits, void *paramv){
    return log(gsl_ran_binomial_pdf(hits->data[1], ((gsl_vector*)paramv)->data[1], ((gsl_vector*)paramv)->data[0]));
}
Esempio n. 6
0
void MainWindow::binom(){
    frmSettings *dlg = new frmSettings();

    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "RJsoft", "ssconf");

    settings.beginGroup("CIprms");
    if(settings.contains("k")){
        dlg->m_ui->spinBox->setValue(settings.value("k").toInt());
    } else {
        dlg->m_ui->spinBox->setValue(0);
    }
    if(settings.contains("n")){
        dlg->m_ui->spinBox_2->setValue(settings.value("n").toInt());
    } else {
        dlg->m_ui->spinBox_2->setValue(0);
    }
    if(settings.contains("alpha")){
        dlg->m_ui->doubleSpinBox_3->setValue(settings.value("alpha").toDouble());
    } else {
        dlg->m_ui->doubleSpinBox_3->setValue(0.95);
    }
    if(settings.contains("Sp")){
        dlg->m_ui->doubleSpinBox_2->setValue(settings.value("Sp").toDouble());
    } else {
        dlg->m_ui->doubleSpinBox_2->setValue(1.0);
    }
    if(settings.contains("Se")){
        dlg->m_ui->doubleSpinBox->setValue(settings.value("Se").toDouble());
    } else {
        dlg->m_ui->doubleSpinBox->setValue(1.0);
    }
    if(settings.contains("method")){
        dlg->m_ui->comboBox->setCurrentIndex(dlg->m_ui->comboBox->findText(settings.value("method").toString(), Qt::MatchExactly));
    } else {
        dlg->m_ui->comboBox->setCurrentIndex(dlg->m_ui->comboBox->findText("Blaker", Qt::MatchExactly));
    }
    settings.endGroup();

    if (dlg->exec() == QDialog::Accepted ){

        QApplication::setOverrideCursor( Qt::WaitCursor );

        unsigned int k = dlg->m_ui->spinBox->value();
        unsigned int n = dlg->m_ui->spinBox_2->value();
        double conf    = dlg->m_ui->doubleSpinBox_3->value();
        double alpha   = 1-conf;
        double Se = dlg->m_ui->doubleSpinBox->value();
        double Sp = dlg->m_ui->doubleSpinBox_2->value();

        settings.beginGroup("CIprms");
        settings.setValue("k",     k);
        settings.setValue("n",     n);
        settings.setValue("alpha", dlg->m_ui->doubleSpinBox_3->value());
        settings.setValue("Sp",    Sp);
        settings.setValue("Se",    Se);
        settings.setValue("method",dlg->m_ui->comboBox->currentText());
        settings.endGroup();

        double pbal = 0, pjobb=1, sum=0;
        double prev = (double)k/(double)n;

        ui->plainTextEdit->appendPlainText(
            QString("Number of test positives:   %1  out of   %2\nSensitivity: %3     Specificity: %4     Confidence level: %5     Method: %6\n")
            .arg(k).arg(n).arg(Se).arg(Sp).arg(conf)
            .arg(dlg->m_ui->comboBox->currentText())
            );


        if (dlg->m_ui->comboBox->currentText()=="Sterne"){

        // ----------------------------------------------------------
        // -------------------- S T E R N E -------------------------
        // ----------------------------------------------------------

            //bal
            int z =0;
            for(double p=0.0001; p<=0.9999; p = p+0.0001){
                sum=0;
                double kp = gsl_ran_binomial_pdf(k, p, n);
                for(unsigned int x=0; x!=n+1; x++){
                    double xp = gsl_ran_binomial_pdf(x, p, n);
                    if (xp<=kp){
                        sum = sum+xp;
                    }
                }
                if (sum<alpha){
                    pbal = p;
    //                ui->plainTextEdit->appendPlainText(QString("sum: %1; p: %2").arg(sum).arg(p));
                } else {
                    break;
                }
                z++;
            }

            //jobb
            z =0;
            for(double p=0.9999; p>=0.0001; p = p-0.0001){
                sum=0;
                double kp = gsl_ran_binomial_pdf(k, p, n);
                for(unsigned int x=0; x!=n+1; x++){
                    double xp = gsl_ran_binomial_pdf(x, p, n);
                    if (xp<=kp){
                        sum = sum+xp;
                    }
                }
                if (sum<alpha){
                    pjobb = p;
                } else {
                    break;
                }
                z++;
            }

        } else if (dlg->m_ui->comboBox->currentText()=="Blaker"){

        // ----------------------------------------------------------
        // -------------------- B L A K E R -------------------------
        // ----------------------------------------------------------

            //bal
            int z =0;
            for(double p=0.0001; p<=0.9999; p = p+0.0001){
                sum=0;
                double kp = gsl_cdf_binomial_P(k, p, n);
                        // ui->plainTextEdit->appendPlainText(QString("k %1 p %2 n %3 kp %4\n").arg(k).arg(p).arg(n).arg(kp));
                double kp2 = gsl_cdf_binomial_Q(k, p, n) + gsl_ran_binomial_pdf(k, p, n);
                        // ui->plainTextEdit->appendPlainText(QString("k %1 p %2 n %3 kp2 %4\n").arg(k).arg(p).arg(n).arg(kp2));

                if (kp2 < kp) kp=kp2;
                for(unsigned int x=0; x!=n+1; x++){
                    double xp = gsl_cdf_binomial_P(x, p, n);
                           // ui->plainTextEdit->appendPlainText(QString("x %1 p %2 n %3 kp %4\n").arg(x).arg(p).arg(n).arg(xp));
                    double xp2 = gsl_cdf_binomial_Q(x, p, n) + gsl_ran_binomial_pdf(x, p, n);
                           // ui->plainTextEdit->appendPlainText(QString("x %1 p %2 n %3 kp2 %4\n").arg(x).arg(p).arg(n).arg(xp2));
                    if (xp2 < xp) xp=xp2;
                    double xxp = gsl_ran_binomial_pdf(x, p, n);
                            // ui->plainTextEdit->appendPlainText(QString("x %1 p %2 n %3 xxp %4\n").arg(x).arg(p).arg(n).arg(xxp));
                    if (xp<=kp){
                        sum = sum+xxp;
                    }
                            // ui->plainTextEdit->appendPlainText(QString("p %1 sum %2\n").arg(p).arg(sum));

                }
                if (sum<alpha){
                    pbal = p;
     //              ui->plainTextEdit->appendPlainText(QString("sum: %1; p: %2").arg(sum).arg(p));
                } else {
                    break;
                }
                z++;
            }

            //jobb
            z =0;
            for(double p=0.9999; p>=0.0001; p = p-0.0001){
                sum=0;
                double kp = gsl_cdf_binomial_P(k, p, n);
                double kp2 = gsl_cdf_binomial_Q(k, p, n) + gsl_ran_binomial_pdf(k, p, n);
                if (kp2 < kp) kp=kp2;
                for(unsigned int x=0; x!=n+1; x++){
                    double xp = gsl_cdf_binomial_P(x, p, n);
                    double xp2 = gsl_cdf_binomial_Q(x, p, n) + gsl_ran_binomial_pdf(x, p, n);
                    if (xp2 < xp) xp=xp2;
                    double xxp = gsl_ran_binomial_pdf(x, p, n);
                    if (xp<=kp){
                        sum = sum+xxp;
                    }
                }
                 // ui->plainTextEdit->appendPlainText(QString("p %1 sum %2\n").arg(p).arg(sum));
                if (sum<alpha){
                    pjobb = p;
                } else {
                    break;
                }
                z++;
            }
        } else if (dlg->m_ui->comboBox->currentText()=="Wilson"){

        // ----------------------------------------------------------
        // -------------------- W I L S O N -------------------------
        // ----------------------------------------------------------

            double zkrit = 1.96;
            double plusminus = zkrit * pow(pow(zkrit, 2.) + 4. * k * (n-k) / n, .5);
            double nevezo = 2.*(n + pow(zkrit, 2.));

            pbal = (2.*k + pow(zkrit, 2) - plusminus)/nevezo;
            pjobb = (2.*k + pow(zkrit, 2) + plusminus)/nevezo;

        } else if (dlg->m_ui->comboBox->currentText()=="Clopper-Pearson"){

        // ----------------------------------------------------------
        // ------------- C L O P P E R   P E A R S O N --------------
        // ----------------------------------------------------------

            //bal
            int z =0;
            for(double p=0.0001; p<=0.9999; p = p+0.0001){
                double kp = gsl_cdf_binomial_Q(k, p, n) + gsl_ran_binomial_pdf(k, p, n);
    //                ui->plainTextEdit->appendPlainText(QString("kp: %1; p: %2").arg(kp).arg(p));
                if (kp<0.5*alpha){
                    pbal = p;
              } else {
                    break;
                }
                z++;
            }

            //jobb
            z =0;
            for(double p=0.9999; p>=0.0001; p = p-0.0001){
                double kp = gsl_cdf_binomial_P(k, p, n);
                if (kp<0.5*alpha){
                    pjobb = p;
                } else {
                    break;
                }
                z++;
            }

        }

        kiir(pbal, pjobb, prev, Sp, Se);

        QApplication::restoreOverrideCursor();
    }    
}
Esempio n. 7
0
double pdf_Binomial(double p, long n, long x)      { return gsl_ran_binomial_pdf(x,p,n); }
Esempio n. 8
0
double
test_binomial_large_pdf (unsigned int n)
{
  return gsl_ran_binomial_pdf (n, 0.3, 55);
}
Esempio n. 9
0
double calcLoonIndex(t_Data *ptSeqData, t_Data *ptRefData, int nI, int nP1, int nP2, int* pnSplit, int* pnParentD, t_Params *ptParams)
{
  int i = 0, nLenI = ptSeqData->anLen[nI], nLenP1 = ptRefData->anLen[nP1], nLenP2 = ptRefData->anLen[nP2];
  FILE *ofp = NULL;
  char szCommand[MAX_LINE_LENGTH];
  t_Data tAlign;
  int nDiff1 = 0, anDiff1[MAX_DIFF + 1];
  int nDiff2 = 0, anDiff2[MAX_DIFF + 1];
  int anSplits[2*MAX_DIFF], s = 0, s1 = 0, s2 = 0;
  int nSplit = -1, sMin = 0, nMinD = BIG_INT;
  int anD[2*MAX_DIFF];
  int nMaxLen = 0, nTLen = 0;
  char* acSequences = NULL;
  double dRet = 0;
  char szTempFasta[MAX_LINE_LENGTH], szTempAlign[MAX_LINE_LENGTH];
  int nTGap1 = 0, nTGap2 = 0, nTGap3 = 0,nMaxTGap = -1;
  int nParentD = 0;

  /*create sequence filenames*/
  if(ptParams->bOutputAlignments == FALSE){
  	sprintf(szTempFasta, "Temp%s",FASTA_SUFFIX);
  	sprintf(szTempAlign, "Temp%s",ALIGN_SUFFIX);
  }
  else{
  	sprintf(szTempFasta, "Temp%d%s",nI,FASTA_SUFFIX);
  	sprintf(szTempAlign, "Temp%d%s",nI,ALIGN_SUFFIX);
  }

  /*write sequences*/
  ofp = fopen(szTempFasta, "w");
  if(ofp){
    writeSequenceI(ofp, ptSeqData, nI);
    writeSequenceI(ofp, ptRefData, nP1);
    writeSequenceI(ofp, ptRefData, nP2);
    fclose(ofp);
  }
  else{
    fprintf(stderr, "Failed to open %s for writing ... abort\n",szTempFasta);
    exit(EXIT_FAILURE);
  }
  /*run mafft remotely*/
  sprintf(szCommand,"mafft-linsi %s > %s 2> %s",szTempFasta,szTempAlign, TEMP_ERROR_FILE);

  system(szCommand);

  /*read in mafft output - three sequence alignment*/
  readData(szTempAlign, &tAlign);
 
  acSequences = tAlign.acSequences;  nMaxLen = tAlign.nMaxLen;

  /*alignment length*/
  nTLen = tAlign.nMaxLen;

  /*find largest terminal gap*/
  while(acSequences[nTLen - 1 - nTGap1] == GAP && nTLen - nTGap1> 1){
    nTGap1++;
  }

  while(acSequences[nMaxLen + nTLen - 1 - nTGap2] == GAP && nTLen - nTGap2> 1){
    nTGap2++;
  }

  while(acSequences[2*nMaxLen + nTLen - 1 - nTGap3] == GAP && nTLen - nTGap3> 1){
    nTGap3++;
  }

  nMaxTGap = nTGap1 > nTGap2 ? nTGap1 : nTGap2;		
  nMaxTGap = nTGap3 > nMaxTGap ? nTGap3 : nMaxTGap;
  /*remove from alignment*/
  nTLen -= nMaxTGap;	

  /*find all differences between chimera and parents and positions*/
  for(i = 0; i < nTLen; i++){
    if(acSequences[i] != acSequences[nMaxLen + i]){
      if(nDiff1 < MAX_DIFF){
	anDiff1[nDiff1] = i;
	nDiff1++;
      }
      else{
	fprintf(stderr,"Max diff reached in calcLoon\n");
      }
    }

    if(acSequences[i] != acSequences[2*nMaxLen + i]){
      if(nDiff2 < MAX_DIFF){
	anDiff2[nDiff2] = i;
	nDiff2++;
      }
      else{
	fprintf(stderr,"Max diff reached in calcLoon\n");
      }
    }
  }

  for(i = 0; i < nTLen; i++){
    if(acSequences[nMaxLen + i] != acSequences[2*nMaxLen + i]){
      nParentD++;
    }
  }


  anDiff1[nDiff1] = nTLen;
  anDiff2[nDiff2] = nTLen;

  s = 0; s1 = 0; s2 = 0;
  
  anSplits[s] = -1;
  anD[s] = nDiff2;
  s++;
  /*loop differences to find optimal split point*/
  while(s1 < nDiff1 || s2 < nDiff2){
    if(anDiff1[s1] <= anDiff2[s2]){
      anD[s] = anD[s - 1] + 1;
      anSplits[s] = anDiff1[s1];

      s++;
      s1++;
    }
    else if(anDiff1[s1] > anDiff2[s2]){
      anD[s] = anD[s - 1] - 1;
      anSplits[s] = anDiff2[s2];

      s++;
      s2++;
    }
  
  }
  

  for(i = 0; i < s; i++){
    if(anD[i] < nMinD){
      nMinD = anD[i];
      sMin = i;
    }
  }
  
  if(sMin < s - 1){
    nSplit = (anSplits[sMin] + anSplits[sMin + 1])/2;
  }
  else{
    nSplit = nTLen - 1;
  }

  /*dummy if, as we always do this*/
  if(TRUE){
    int nA = -1, nB = -1;
    int nDLP1 = 0, nDRP1 = 0, nDLP2 = 0, nDRP2 = 0;
    int nX = 0, nY = 0, nZ = 0, nXZ = 0;
    int nXA = 0, nXB = 0, nYA = 0, nYB = 0;
    char cC = '\0';
    double pA = 0.0, pB = 0.0;
    double dP = 0.0;

    for(i = 0; i < nTLen; i++){
      char cI = acSequences[i], cP1 = acSequences[nMaxLen + i], cP2 = acSequences[2*nMaxLen + i];
      /*get consenus base*/
      cC = getC(cI, cP1, cP2);

      /*count number of differences between chimera and consensus*/
      if(cI != cC){
	nZ++;
      }
      /*count number of differences between chimera and parent 1*/
      if(cP1 != cC){
	/*count number to left*/
	if(i <= nSplit){
	  nDLP1++;
	}
	/*count number to right*/
	else{
	  nDRP1++;
	}
      }
      /*count number of differences between chimera and parent 2*/
      if(cP2 != cC){
	if(i <= nSplit){
	  nDLP2++;
	}
	else{
	  nDRP2++;
	}
      }
    }
    /*if Parent1 is left*/
    if(nDiff1 <= nDiff2){
      nA = nP1;
      nB = nP2;

      nX = nDLP1 + nDRP1;
      nXA = nDLP1; nXB = nDRP1;

      nY = nDLP2 + nDRP2;
      nYA = nDLP2; nYB = nDRP2;

      pA = ((double) nSplit + 1)/((double) nTLen); /*probability of change to left*/
      pB = ((double) nTLen - nSplit - 1)/((double) nTLen);/*prob. to right*/
    }
    else{
      /*if parent 1 is right*/
      nA = nP2;
      nB = nP1;

      nX = nDLP2 + nDRP2;
      nXA = nDRP2; nXB = nDLP2;

      nY = nDLP1 + nDRP1;
      nYA = nDRP1; nYB = nDLP1;

      pB = ((double) nSplit + 1)/((double) nTLen);
      pA = ((double) nTLen - nSplit - 1)/((double) nTLen);
    }

    nXZ = nX + nZ;
    
    dRet = 0.0;
    dP = 0.0;
    
    /*adds extra factor for tree imbalance - not generally used*/
    if(ptParams->bImbalance){
      if(nXZ > 0){
	for(i = nX; i <= nXZ; i++){
	  dP += gsl_ran_binomial_pdf (i, 0.5, nXZ);
	}

	dRet += -log(dP);
      }
    }

    /*contribution from right parent*/
    if(nY > 0){
      dP = 0.0;
      for(i = nYA; i <= nY; i++){
	dP += gsl_ran_binomial_pdf(i, pA, nY);
      }
      dRet += -log(dP);
    }

    /*contribution from left*/
    if(nX > 0){
      dP = 0.0;
      for(i = nXB; i <= nX; i++){
	dP += gsl_ran_binomial_pdf(i, pB, nX);
      }
      dRet += -log(dP);
    }
  }

  (*pnSplit) = nSplit;
  (*pnParentD) = nParentD;

  /*free up memory*/
  destroyData(&tAlign);
  return dRet;
} 
Esempio n. 10
0
double
test_binomial_huge_knuth_pdf (unsigned int n)
{
  return gsl_ran_binomial_pdf (n, 0.3, 5500);
}
Esempio n. 11
0
double
test_binomial1_pdf (unsigned int n)
{
  return gsl_ran_binomial_pdf (n, 1, 8);
}
Esempio n. 12
0
int main(int argc, char **argv){
    distlist distribution = Normal;
    char	 msg[10000], c;
    int      pval = 0, qval = 0;
    double   param1 = GSL_NAN, param2 =GSL_NAN, findme = GSL_NAN;
    char     number[1000];
	sprintf(msg, "%s [opts] number_to_lookup\n\n"
    "Look up a probability or p-value for a given standard distribution.\n"
    "[This is still loosely written and counts as beta. Notably, negative numbers are hard to parse.]\n"
    "E.g.:\n"
    "%s -dbin 100 .5 34\n"
    "sets the distribution to a Binomial(100, .5), and find the odds of 34 appearing.\n"
    "%s -p 2     \n"
    "find the area of the Normal(0,1) between -infty and 2.  \n"
    "\n"
    "-pval Find the p-value: integral from -infinity to your value\n"
    "-qval Find the q-value: integral from your value to infinity\n"
    "\n"
    "After giving an optional -p or -q, specify the distribution. \n"
    "Default is Normal(0, 1). Other options:\n"
    "\t\t-binom Binomial(n, p)\n"
    "\t\t-beta Beta(a, b)\n"
    "\t\t-f F distribution(df1, df2)\n"
    "\t\t-norm Normal(mu, sigma)\n"
    "\t\t-negative bin Negative binomial(n, p)\n"
    "\t\t-poisson Poisson(L)\n"
    "\t\t-t t distribution(df)\n"
    "I just need enough letters to distinctly identify a distribution.\n"
, argv[0], argv[0], argv[0]); 

    opterr=0;
	if(argc==1){
		printf("%s", msg);
		return 0;
	}
	while ((c = getopt (argc, argv, "B:b:F:f:N:n:pqT:t:")) != -1){
		switch (c){
		  case 'B':
		  case 'b':
              if (optarg[0]=='i')
                  distribution = Binomial;
              else if (optarg[0]=='e')
                  distribution = Beta;
            else {
                printf("I can't parse the option -b%s\n", optarg);
                exit(0);
            }
              param1 = atof(argv[optind]);
              param2 = atof(argv[optind+1]);
              findme =  atof(argv[optind+2]);
			  break;
          case 'F':
          case 'f':
            distribution = F;
            param1 = atof(argv[optind]);
            findme =  atof(argv[optind+1]);
            break;
          case 'H':
		  case 'h':
			printf("%s", msg);
			return 0;
          case 'n':
          case 'N':
            if (optarg[0]=='o'){ //normal
                  param1 = atof(argv[optind]);
                  param2 = atof(argv[optind+1]);
                  findme =  atof(argv[optind+2]);
            } else if (optarg[0]=='e'){
                  distribution = Negbinom;
                  param1 = atof(argv[optind]);
                  param2 = atof(argv[optind+1]);
                  findme =  atof(argv[optind+2]);
            } else {
                printf("I can't parse the option -n%s\n", optarg);
                exit(0);
            }
			  break;
          case 'p':
            if (!optarg || optarg[0] == 'v')
                pval++;
            else if (optarg[0] == 'o'){
                distribution = Poisson;
                param1 = atof(argv[optind]);
                findme =  atof(argv[optind+1]);
            } else {
                printf("I can't parse the option -p%s\n", optarg);
                exit(0);
            }
            break;
          case 'q':
            qval++;
            break;
          case 'T':
          case 't':
            distribution = T;
            param1 = atof(argv[optind]);
            findme =  atof(argv[optind+1]);
            break;
          case '?'://probably a negative number
            if (optarg)
                 snprintf(number, 1000, "%c%s", optopt, optarg);
            else snprintf(number, 1000, "%c", optopt);
            if (gsl_isnan(param1)) param1 = -atof(number);
            else if (gsl_isnan(param2)) param2 = -atof(number);
            else if (gsl_isnan(findme)) findme = -atof(number);
		}
	}
    if (gsl_isnan(findme)) findme =  atof(argv[optind]);
    //defaults, as promised
    if (gsl_isnan(param1)) param1 = 0;
    if (gsl_isnan(param2)) param2 = 1;
    if (!pval && !qval){
        double val =
        distribution == Beta ? gsl_ran_beta_pdf(findme, param1, param2)
        : distribution == Binomial ? gsl_ran_binomial_pdf(findme, param2, param1)
        : distribution == F ? gsl_ran_fdist_pdf(findme, param1, param2) 
        : distribution == Negbinom ? gsl_ran_negative_binomial_pdf(findme, param2, param1)
        : distribution == Normal ? gsl_ran_gaussian_pdf(findme, param2)+param1
        : distribution == Poisson ? gsl_ran_poisson_pdf(findme, param1) 
        : distribution == T ? gsl_ran_tdist_pdf(findme, param1) : GSL_NAN;
        printf("%g\n", val); 
        return 0;
    }
    if (distribution == Binomial){
        printf("Sorry, the GSL doesn't have a Binomial CDF.\n");
        return 0; }
    if (distribution == Negbinom){
        printf("Sorry, the GSL doesn't have a Negative Binomial CDF.\n");
        return 0; }
    if (distribution == Poisson){
        printf("Sorry, the GSL doesn't have a Poisson CDF.\n");
        return 0; }
    if (pval){
        double val =
        distribution == Beta ? gsl_cdf_beta_P(findme, param1, param2)
        : distribution == F ? gsl_cdf_fdist_P(findme, param1, param2) 
        : distribution == Normal ? gsl_cdf_gaussian_P(findme-param1, param2)
        : distribution == T ? gsl_cdf_tdist_P(findme, param1) : GSL_NAN;
        printf("%g\n", val); 
        return 0;
    }
    if (qval){
        double val =
        distribution == Beta ? gsl_cdf_beta_Q(findme, param1, param2)
        : distribution == F ? gsl_cdf_fdist_Q(findme, param1, param2) 
        : distribution == Normal ? gsl_cdf_gaussian_Q(findme-param1, param2)
        : distribution == T ? gsl_cdf_tdist_Q(findme, param1) : GSL_NAN;
        printf("%g\n", val); 
    }
}
Esempio n. 13
0
void
sign_execute (const struct dataset *ds,
		  struct casereader *input,
		  enum mv_class exclude,
		  const struct npar_test *test,
		  bool exact UNUSED,
		  double timer UNUSED)
{
  int i;
  bool warn = true;
  const struct dictionary *dict = dataset_dict (ds);
  const struct two_sample_test *t2s = UP_CAST (test, const struct two_sample_test, parent);
  struct ccase *c;

  struct sign_test_params *stp = xcalloc (t2s->n_pairs, sizeof *stp);

  struct casereader *r = input;

  for (; (c = casereader_read (r)) != NULL; case_unref (c))
    {
      const double weight = dict_get_case_weight (dict, c, &warn);

      for (i = 0 ; i < t2s->n_pairs; ++i )
	{
	  variable_pair *vp = &t2s->pairs[i];
	  const union value *value0 = case_data (c, (*vp)[0]);
	  const union value *value1 = case_data (c, (*vp)[1]);
	  const double diff = value0->f - value1->f;

	  if (var_is_value_missing ((*vp)[0], value0, exclude))
	    continue;

	  if (var_is_value_missing ((*vp)[1], value1, exclude))
	    continue;

	  if ( diff > 0)
	    stp[i].pos += weight;
	  else if (diff < 0)
	    stp[i].neg += weight;
	  else
	    stp[i].ties += weight;
	}
    }

  casereader_destroy (r);

  for (i = 0 ; i < t2s->n_pairs; ++i )
    {
      int r = MIN (stp[i].pos, stp[i].neg);
      stp[i].one_tailed_sig = gsl_cdf_binomial_P (r,
						  0.5,
						  stp[i].pos + stp[i].neg);

      stp[i].point_prob = gsl_ran_binomial_pdf (r, 0.5,
						stp[i].pos + stp[i].neg);
    }

  output_frequency_table (t2s, stp, dict);

  output_statistics_table (t2s, stp);

  free (stp);
}
Esempio n. 14
0
File: test.c Progetto: CNMAT/gsl
double
test_binomial_max_pdf (unsigned int n)
{
  return gsl_ran_binomial_pdf (n, 1e-8, 1<<31);
}