int beetle(void)
{
	const int replicates = 10;
	const int n_samples = 10;
	double s1[n_samples*replicates];
	double s2[n_samples*replicates];
	double s3[n_samples*replicates];
	double s4[n_samples*replicates];
//	       0  1  2  3  4   5   6   7   8   9  10   11  12   13  14  
// Pars = {E, L, P, A, b, ue, ul, up, ua, ae, al, ap, cle, cap, cae, V} 
	double inits[NP] = {100, 0, 0, 0, 
						5., 
						0, 0.001, 0, 0.003,
						1/3.8, 1/(20.2-3.8), 1/(25.5-20.2), 
						0.1, 0.04, 0.1, 1000};

	int n = n_samples;
	double maxtime = 50; 
	int reps = replicates;

	beetles(s1, s2, s3, s4, inits, &n, &reps, &maxtime);

	printf("m1 = %g sd1 = %g nm2 = %g sd2 = %g m3 = %g sd3 = %g m4 = %g sd4 = %g\n",
			gsl_stats_mean(s1, 1, n_samples), 
			sqrt(gsl_stats_variance(s1, 1, n_samples)),
			gsl_stats_mean(s2, 1, n_samples), 
			sqrt(gsl_stats_variance(s2, 1, n_samples)),
			gsl_stats_mean(s3, 1, n_samples), 
			sqrt(gsl_stats_variance(s3, 1, n_samples)),
			gsl_stats_mean(s4, 1, n_samples), 
			sqrt(gsl_stats_variance(s4, 1, n_samples))
	); 
	return 0;
}
Exemple #2
0
/** \brief Euclidean distance between normalized vector x1 and x2.

	 Normalization is defined by
	 \f[
	 \hat{x}_i = \frac{x_i-\langle x_i \rangle_i}{\sqrt{\langle x_i^2\rangle_i}}
	 \f]

	 \param x1, x2 vectors of size p
	 \param optargs is ignored
 */
double   vectordist_euclidean_normalized( const double *x1, const double *x2, int p,
														OptArgList *optargs ){
  int i;
  double d;
  double avg1, avg2,
	 rms1=0, rms2=0;
  double norm1,norm2; /* normalized signal at time t */
	 
  avg1 = gsl_stats_mean(x1, 1, p);
  avg2 = gsl_stats_mean(x2, 1, p);
  
  for(i=0; i<p; i++){
	 rms1 += SQR( x1[i] );
	 rms2 += SQR( x2[i] );
  }
  
  rms1 = sqrt(rms1/(double)p);
  rms2 = sqrt(rms2/(double)p);

  d=0.0;
  for( i=0; i<p; i++ ){
	 norm1 = (x1[i]-avg1)/rms1;
	 norm2 = (x2[i]-avg2)/rms2;

	 d += SQR( norm1-norm2 );
  }
  d = sqrt( d );

  return d;
}
Exemple #3
0
double compute_cov_mean(long nreps, double* runtimes_sec,
        pred_method_info_t prediction_info) {
    int my_rank, i,j;
    double* mean_list = NULL;
    int nmeans;
    long current_nreps;
    double* tmp_runtimes;
    double cov_mean = COEF_ERROR_VALUE;

    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    if (my_rank == OUTPUT_ROOT_PROC) {
        double mean_of_means = 0, q1,q3, sd;
        long start_index, end_index;
        double* runtimes;

        nmeans = prediction_info.method_win;
        if (nmeans > nreps) {
            return COEF_ERROR_VALUE;
        }

        mean_list = (double*)malloc(prediction_info.method_win * sizeof(double));
        tmp_runtimes = (double*)malloc(nreps * sizeof(double));

        for (i = 0; i < nmeans; i++) {
            current_nreps = nreps - i;

            for (j=0; j < current_nreps; j++) {
                tmp_runtimes[j] = runtimes_sec[j];
            }

            runtimes = tmp_runtimes;
            gsl_sort(tmp_runtimes, 1, current_nreps);
            if (current_nreps > OUTLIER_FILTER_MIN_MEAS) {
                        q1 =  gsl_stats_quantile_from_sorted_data (tmp_runtimes, 1, current_nreps, 0.25);
                        q3 =  gsl_stats_quantile_from_sorted_data (tmp_runtimes, 1, current_nreps, 0.75);

                        filter_outliers_from_sorted(tmp_runtimes, current_nreps, q1, q3, OUTLIER_FILTER_THRES, &start_index, &end_index);
                        runtimes = runtimes + start_index;
                        current_nreps =  end_index - start_index + 1;
                    }
            mean_list[i] = gsl_stats_mean(runtimes, 1, current_nreps);
        }

        mean_of_means = gsl_stats_mean(mean_list, 1, nmeans);
        sd =  gsl_stats_sd(mean_list, 1, nmeans);
        cov_mean = sd/(mean_of_means);

        //printf("cov_mean=%lf, nreps = %ld, thres=%lf (mean_of_means=%.10f)\n", cov_mean, nreps, prediction_info.method_thres, mean_of_means);


        free(tmp_runtimes);
        free(mean_list);
    }

    return cov_mean;
}
Exemple #4
0
int main(void) {
    double vol[NEXP];      /* calculated values of SE */
    gsl_rng *r;
    double mean, sd;
    int j, n;
    unsigned long seed = 1UL;
    long p = pow(2, 19.);
     
    /* allocate random number generator */
    r = gsl_rng_alloc(gsl_rng_taus2);

    /* seed the random number generator */
    gsl_rng_set(r, seed);

    for (n = 1; n < 10; n++)
    {
      
        for (j = 0; j < NEXP; j++) {
            /* calculate pi using Monte Carlo algorithm */
            vol[j] = mc_se(p, n, r);
        }
        mean = gsl_stats_mean(vol, 1, NEXP);
        sd = gsl_stats_sd_m(vol, 1, NEXP, mean);
        printf("%15ld   %15d   %14.8f   %14.8f\n", p, n, mean, sd);
           
    }

    gsl_rng_free(r);

    return(0);
}
Exemple #5
0
static ERL_NIF_TERM arithmetic_mean(ErlNifEnv * env, int argc, const ERL_NIF_TERM argv[]) {
    double_list dl = alloc_double_list(env, argv[0]);
    ARG_ERROR_IF_DL_IS_NULL(dl);
    ERL_NIF_TERM final = enif_make_double(env, gsl_stats_mean(dl.list, 1, dl.length));
    free_double_list(dl);
    return final;
}
Exemple #6
0
void BoxCurve::drawSymbols(QPainter *painter, const QwtScaleMap &xMap,
                           const QwtScaleMap &yMap, double *dat,
                           int size) const {
  const int px = xMap.transform(x(0));

  QwtSymbol s = this->symbol();
  if (min_style != QwtSymbol::NoSymbol) {
    const int py_min = yMap.transform(y(0));
    s.setStyle(min_style);
    s.draw(painter, px, py_min);
  }
  if (max_style != QwtSymbol::NoSymbol) {
    const int py_max = yMap.transform(y(size - 1));
    s.setStyle(max_style);
    s.draw(painter, px, py_max);
  }
  if (p1_style != QwtSymbol::NoSymbol) {
    const int p1 =
        yMap.transform(gsl_stats_quantile_from_sorted_data(dat, 1, size, 0.01));
    s.setStyle(p1_style);
    s.draw(painter, px, p1);
  }
  if (p99_style != QwtSymbol::NoSymbol) {
    const int p99 =
        yMap.transform(gsl_stats_quantile_from_sorted_data(dat, 1, size, 0.99));
    s.setStyle(p99_style);
    s.draw(painter, px, p99);
  }
  if (mean_style != QwtSymbol::NoSymbol) {
    const int mean = yMap.transform(gsl_stats_mean(dat, 1, size));
    s.setStyle(mean_style);
    s.draw(painter, px, mean);
  }
}
Exemple #7
0
/**
 * Get a summary statistic for the orbital elements; for instance,
 * the median value calculated over all the elements of the list.
 * @param kl List
 * @param what Can be one of: STAT_MEAN, STAT_MEDIAN, STAT_STDDEV, STAT_MAD. 
 *      Summary statistic is calculated correctly for angle parameters.
 * @return A matrix whose entries are the summary statistic for the 
 * corresponding orbital element.
 */
gsl_matrix* KL_getElementsStats(const ok_list* kl, const int what) {
    
    int npl = MROWS(kl->kernels[0]->elements);
    if (npl == 0)
        return NULL;
    
    gsl_vector* v = gsl_vector_alloc(kl->size);
    
    gsl_matrix* m = gsl_matrix_alloc(npl, ALL_ELEMENTS_SIZE);
    gsl_matrix_set_all(m, 0.);
    
    
    for (int i = 0; i < npl; i++)
            for (int j = 0; j < ALL_ELEMENTS_SIZE; j++) {
                for (int n = 0; n < kl->size; n++) {
                    VSET(v, n, MGET(kl->kernels[n]->elements, i, j));
                }
                
                switch (what) {
                    case STAT_MEAN:
                        if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY)
                            MSET(m, i, j, ok_average_angle(v->data, v->size, false));
                        else
                            MSET(m, i, j, gsl_stats_mean(v->data, 1, v->size));
                        break;
                    case STAT_STDDEV:
                        if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) {
                            MSET(m, i, j, ok_stddev_angle(v->data, v->size, false));
                        }
                        else
                            MSET(m, i, j, gsl_stats_sd(v->data, 1, v->size));
                        break;
                    case STAT_MEDIAN:
                        if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY)
                            MSET(m, i, j, ok_median_angle(v->data, v->size, false));
                        else {
                            gsl_sort_vector(v);
                            MSET(m, i, j, gsl_stats_median_from_sorted_data(v->data, 1, v->size));
                        }
                        break;
                    case STAT_MAD:
                        if (j == MA || j == LOP || j == INC || j == NODE || j == TRUEANOMALY) {
                            double med = ok_median_angle(v->data, v->size, false);
                            MSET(m, i, j, 1.4826 * ok_mad_angle(v->data, v->size, med, false));
                        } else {
                            gsl_sort_vector(v);
                            double med = gsl_stats_median_from_sorted_data(v->data, 1, v->size);
                            
                            MSET(m, i, j, 1.4826 * ok_mad(v->data, v->size, med));
                        }
                        break;
                    default:
                        // percentiles
                        gsl_sort_vector(v);
                        MSET(m, i, j, gsl_stats_quantile_from_sorted_data(v->data, 1, v->size, (double)(what)/100.));
                };
            }
    gsl_vector_free(v);
    return m;
}
Exemple #8
0
void GbA::compute_likelihood(double *data, int nstats, int nbands, int nsim,
						double mean[2], double cov[2][2]){
	int i, j, k, l, idx;
	gsl_vector *lsq = gsl_vector_alloc (_td->get_noftraces());
	gsl_vector *input = gsl_vector_alloc (nbands);
	gsl_vector *trace = gsl_vector_alloc (nbands);
	double *mags, *dists;
	double misfit_l2;
	size_t *indices = new size_t[nsim];
	mags = new double[nsim];
	dists = new double[nsim];

	for (i=0; i<nstats; i++){
		for (j=0; j<nbands; j++){
			gsl_vector_set(input, j, std::log10(data[i*nbands+j]));
		}
		for (k=0; k<_td->get_noftraces(); k++){
			gsl_matrix_get_row(trace, _td->_tdata_gsl, k);
			gsl_vector_sub(trace,input);
			misfit_l2 = gsl_blas_dnrm2(trace);
			gsl_vector_set(lsq, k, misfit_l2*misfit_l2);
		}
		gsl_sort_vector_smallest_index(indices, nsim,lsq);
		mean[0] = 0;
		mean[1] = 0;
		for(l=0; l<nsim; l++){
			dists[l] = std::log10(gsl_vector_get(_td->_r_gsl, indices[l]));
			mags[l] = gsl_vector_get(_td->_m_gsl, indices[l]);
			//std::cout << gsl_vector_get(_td->_m_gsl,indices[l]) << std::endl;
		}
		mean[0] = gsl_stats_mean(dists,1,nsim);
		mean[1] = gsl_stats_mean(mags,1,nsim);
		cov[0][0] = gsl_stats_variance(dists, 1, nsim);
		cov[1][1] = gsl_stats_variance(mags, 1, nsim);
		cov[0][1] = gsl_stats_covariance_m(dists,1,mags,1,nsim,mean[0],mean[1]);
		cov[1][0] = cov[0][1];
		std::cout << "Distance: " << mean[0] << "; Magnitude: " << mean[1] << std::endl;
		std::cout << "Covariance matrix:" << std::endl;
		for(i=0; i<2; i++){
			for(j=0;j<2;j++){
				printf("%d, %d, %g\n",i,j,cov[i][j]);
			}
		}
	}
}
Exemple #9
0
void function_initial_estimate( const double* pdX, const double* pdY, int iLength, double* pdParameterEstimates ) {
  double dMin;
  double dMax;
  
  gsl_stats_minmax( &dMin, &dMax, pdX, 1, iLength );
  
  pdParameterEstimates[0] = gsl_stats_mean( pdX, 1, iLength );
  pdParameterEstimates[1] = ( dMax - dMin ) / 2.0;
  pdParameterEstimates[2] = gsl_stats_max( pdY, 1, iLength );
}
void shapeAlign::scaleMatrixZscore(gsl_matrix *M){
	for (size_t i = 0; i < M->size1; i++){
		gsl_vector_view row = gsl_matrix_row(M,i);
		double mu = gsl_stats_mean(row.vector.data, row.vector.stride, row.vector.size);
		double sigma = gsl_stats_sd_m(row.vector.data, row.vector.stride, row.vector.size, mu);

		gsl_vector_add_constant(&row.vector,-mu);
		gsl_vector_scale(&row.vector,1.0/sigma);
	}
	return;
}
Exemple #11
0
/********************************************************************************
 * clusterupdatebatch: Runs clusterupdate multiple times and gets physics as well
 * as error estimates.
 *******************************************************************************/
int
clusterupdatebatch(lattice_site * lattice, settings conf, double beta, datapoint * data )
{
  int i,j;
  double * e_block, * m_block, * e_block_avg, * m_block_avg, \
         * e_block_error, * m_block_error, * c_block , * chi_block;
  gsl_vector * mag_vector;

  e_block       = (double *) malloc(conf.block_size*sizeof(double));
  m_block       = (double *) malloc(conf.block_size*sizeof(double));
  e_block_avg   = (double *) malloc(conf.blocks*sizeof(double));
  m_block_avg   = (double *) malloc(conf.blocks*sizeof(double));
  c_block       = (double *) malloc(conf.blocks*sizeof(double));
  chi_block     = (double *) malloc(conf.blocks*sizeof(double));
  e_block_error = (double *) malloc(conf.blocks*sizeof(double));
  m_block_error = (double *) malloc(conf.blocks*sizeof(double));

  mag_vector = gsl_vector_alloc(conf.spindims);

  //Settle first
  for(i = 0 ; i < conf.max_settle ; i++)
  {
   clusterupdate(lattice,conf,beta);
  }

  //Get averages and stdev for messurements
  for(i = 0 ; i < conf.blocks ; i++)
  {
    for(j = 0 ; j < conf.block_size ; j++)
    {
      clusterupdate(lattice,conf,beta);
      e_block[j] = total_energy(lattice,conf);
      m_block[j] = magnetization(lattice,conf,mag_vector);
    }
    e_block_avg[i]   = gsl_stats_mean(e_block,1,conf.block_size);
    e_block_error[i] = gsl_stats_sd(e_block,1,conf.block_size);
    m_block_avg[i]   = gsl_stats_mean(m_block,1,conf.block_size);
    m_block_error[i] = gsl_stats_sd(m_block,1,conf.block_size);
    c_block[i]       = gsl_pow_2(beta)*gsl_pow_2(e_block_error[i]);
    chi_block[i]     = beta*gsl_pow_2(m_block_error[i]);
  }
  (*data).beta      = beta;
  (*data).erg       = gsl_stats_mean(e_block_avg,1,conf.blocks);
  (*data).erg_error = gsl_stats_sd(e_block_avg,1,conf.blocks);
  (*data).mag       = gsl_stats_mean(m_block_avg,1,conf.blocks);
  (*data).mag_error = gsl_stats_sd(m_block_avg,1,conf.blocks);
  (*data).c         = gsl_stats_mean(c_block,1,conf.blocks);
  (*data).c_error   = gsl_stats_sd(c_block,1,conf.blocks);
  (*data).chi       = gsl_stats_mean(chi_block,1,conf.blocks);
  (*data).chi_error = gsl_stats_sd(chi_block,1,conf.blocks);

  free(e_block);
  free(m_block);
  free(e_block_avg);
  free(m_block_avg);
  free(e_block_error);
  free(m_block_error);
  gsl_vector_free(mag_vector);
  return(0);
}
void DistToPA(gsl_histogram * h, double mag, double sigma)
{

double m1 = 0.5*mag + 0.5;
double s1 = 0.25*sigma;
double n1 = m1*(1. - m1)/s1 - 1.; 
double a1 = m1*n1;
double b1 = (1. - m1)*n1;

    for (int pa = 1; pa < 8; pa++) {
        char line[80];
        double x;
        double data[6000];
        int size = 0;
        int nbins = h->n;
        gsl_histogram * hpa = gsl_histogram_alloc( nbins);
        gsl_histogram_set_ranges_uniform( hpa, 0.0, 1.0);

        char fname[50];
        sprintf(fname,"/home/jonatas/mzanlz/mZ_pa%d.dat",pa);
        FILE * fp;
        fp = fopen(fname,"rt");
        while(fgets(line,80,fp) != NULL){
            x = atof(line); 
            size++;
            data[size] = x;
            gsl_histogram_increment( hpa, x);
        }
        double m2 = 0.5*gsl_stats_mean( data, 1, size ) + 0.5;
        double s2 = 0.25*gsl_stats_variance( data, 1, size );
        double n2 = m2*(1. - m2)/s2 - 1.; 
        double a2 = m2*n2;
        double b2 = (1. - m2)*n2;


        NormalizaGSLHistograma( hpa );
        //char hname[100];
        //sprintf(hname,"pa%d",pa);
        //PrintGSLHistogram( hpa, hname );        

        gsl_histogram_sub( hpa, h);
        double D = 0;
        for (size_t i = 0; i < nbins; i++) {
            D += gsl_histogram_get( hpa , i )*gsl_histogram_get( hpa , i );
        }
        // printf("%g %g ", sqrt(D), KLbetas(a1,b1,a2,b2));

        fclose(fp);
        gsl_histogram_free( hpa );
    }

}
Exemple #13
0
void couple::process ()  { 
    int nrand = 100;
    double *dobs =new double[nind/2];
    double *drandom = new double [nrand];

    for (int i = 0  ;  i < g->nind; i+=2) { 
        dobs[i/2] = distance (i,i+1);
    }
    for (int i  = 0 ; i < nrand; i++){  
        int j =  gsl_rng_uniform_int (rng_r, nind);
        int k = j;
        do {
            k =  gsl_rng_uniform_int (rng_r, nind);
        } while (k==j);
        drandom [i] = distance (j,k);
    }
    double mobs = gsl_stats_mean (dobs, 1 ,nind/2); double sobs = sqrt (gsl_stats_variance (dobs,1,nind/2));
    double mrandom = gsl_stats_mean (drandom, 1 ,nrand); double srandom = sqrt (gsl_stats_variance (drandom,1,nrand));

    cout << mobs << "\t" << sobs << endl;
    cout << mrandom << "\t" << srandom << endl;
}
Exemple #14
0
void matrix_mean(gsl_vector *mean, gsl_matrix *input){
  //  Function to extract the column mean of a gsl matrix
  size_t col;
  size_t NCOL = input->size2;
  gsl_vector_view a_col;
  #pragma omp parallel for private( a_col)
  for (col = 0; col < NCOL; col++) {
    a_col = gsl_matrix_column(input, col);
    gsl_vector_set(mean, col, gsl_stats_mean(a_col.vector.data,
    a_col.vector.stride, a_col.vector.size));
  }

}
Exemple #15
0
void ccl_mat_mean(const double *mat,const int i, const int j,const int axis,double*val){
    gsl_matrix * mat_ = gsl_matrix_alloc(i,j);
    memcpy(mat_->data,mat,i*j*sizeof(double));
    int k;
    if (axis == 0){// x axis
        gsl_vector * vec = gsl_vector_alloc(j);
        for (k=0;k<i;k++){
            gsl_matrix_get_row(vec,mat_,k);
            val[k] = gsl_stats_mean(vec->data,1,j);
        }
        gsl_vector_free(vec);
    }
    if (axis == 1){// y axis
        gsl_vector * vec = gsl_vector_alloc(i);
        for (k=0;k<i;k++){
            gsl_matrix_get_col(vec,mat_,k);
            val[k] = gsl_stats_mean(vec->data,1,i);
        }
        gsl_vector_free(vec);
    }
    gsl_matrix_free(mat_);
}
void
test_basic(const size_t n, const double data[], const double tol)
{
  gsl_rstat_workspace *rstat_workspace_p = gsl_rstat_alloc();
  const double expected_mean = gsl_stats_mean(data, 1, n);
  const double expected_var = gsl_stats_variance(data, 1, n);
  const double expected_sd = gsl_stats_sd(data, 1, n);
  const double expected_sd_mean = expected_sd / sqrt((double) n);
  const double expected_skew = gsl_stats_skew(data, 1, n);
  const double expected_kurtosis = gsl_stats_kurtosis(data, 1, n);
  double expected_rms = 0.0;
  double mean, var, sd, sd_mean, rms, skew, kurtosis;
  size_t i, num;
  int status;

  /* compute expected rms */
  for (i = 0; i < n; ++i)
    expected_rms += data[i] * data[i];

  expected_rms = sqrt(expected_rms / n);

  /* add data to rstat workspace */
  for (i = 0; i < n; ++i)
    gsl_rstat_add(data[i], rstat_workspace_p);

  mean     = gsl_rstat_mean(rstat_workspace_p);
  var      = gsl_rstat_variance(rstat_workspace_p);
  sd       = gsl_rstat_sd(rstat_workspace_p);
  sd_mean  = gsl_rstat_sd_mean(rstat_workspace_p);
  rms      = gsl_rstat_rms(rstat_workspace_p);
  skew     = gsl_rstat_skew(rstat_workspace_p);
  kurtosis = gsl_rstat_kurtosis(rstat_workspace_p);
  num      = gsl_rstat_n(rstat_workspace_p);

  gsl_test_int(num, n, "n n=%zu" , n);
  gsl_test_rel(mean, expected_mean, tol, "mean n=%zu", n);
  gsl_test_rel(var, expected_var, tol, "variance n=%zu", n);
  gsl_test_rel(sd, expected_sd, tol, "stddev n=%zu", n);
  gsl_test_rel(sd_mean, expected_sd_mean, tol, "stddev_mean n=%zu", n);
  gsl_test_rel(rms, expected_rms, tol, "rms n=%zu", n);
  gsl_test_rel(skew, expected_skew, tol, "skew n=%zu", n);
  gsl_test_rel(kurtosis, expected_kurtosis, tol, "kurtosis n=%zu", n);

  status = gsl_rstat_reset(rstat_workspace_p);
  gsl_test_int(status, GSL_SUCCESS, "rstat returned success");
  num = gsl_rstat_n(rstat_workspace_p);

  gsl_test_int(num, 0, "n n=%zu" , n);

  gsl_rstat_free(rstat_workspace_p);
}
Exemple #17
0
CAMLprim value ml_gsl_stats_mean(value ow, value data)
{
  size_t len = Double_array_length(data);
  double result;
  if(ow == Val_none)
    result = gsl_stats_mean(Double_array_val(data), 1, len);
  else {
    value w = Unoption(ow);
    check_array_size(data, w);
    result = gsl_stats_wmean(Double_array_val(w), 1, 
			     Double_array_val(data), 1, len);
  }
  return copy_double(result);
}
Exemple #18
0
/* Statistics code
 * Make only one call to reading for a particular time range and compute all our stats
 */
void _compute_statistics(cdb_range_t *range, uint64_t *num_recs, cdb_record_t *records) {

    uint64_t i     = 0;
    uint64_t valid = 0;
    double sum     = 0.0;
    double *values = calloc(*num_recs, sizeof(double));

    for (i = 0; i < *num_recs; i++) {

        if (!isnan(records[i].value)) {

            sum += values[valid] = records[i].value;
            valid++;
        }
    }

    range->num_recs = valid;
    range->mean     = gsl_stats_mean(values, 1, valid);
    range->max      = gsl_stats_max(values, 1, valid);
    range->min      = gsl_stats_min(values, 1, valid);
    range->sum      = sum;
    range->stddev   = gsl_stats_sd(values, 1, valid);
    range->absdev   = gsl_stats_absdev(values, 1, valid);

    /* The rest need sorted data */
    gsl_sort(values, 1, valid);

    range->median   = gsl_stats_median_from_sorted_data(values, 1, valid);
    range->pct95th  = gsl_stats_quantile_from_sorted_data(values, 1, valid, 0.95);
    range->pct75th  = gsl_stats_quantile_from_sorted_data(values, 1, valid, 0.75);
    range->pct50th  = gsl_stats_quantile_from_sorted_data(values, 1, valid, 0.50);
    range->pct25th  = gsl_stats_quantile_from_sorted_data(values, 1, valid, 0.25);

    /* MAD must come last because it alters the values array
     * http://en.wikipedia.org/wiki/Median_absolute_deviation */
    for (i = 0; i < valid; i++) {
        values[i] = fabs(values[i] - range->median);

        if (values[i] < 0.0) {
            values[i] *= -1.0;
        }
    }

    /* Final sort is required MAD */
    gsl_sort(values, 1, valid);
    range->mad = gsl_stats_median_from_sorted_data(values, 1, valid);

    free(values);
}
StatBox2D::BoxWhiskerData Layout2D::generateBoxWhiskerData(Column *colData,
                                                           int from, int to,
                                                           int key) {
  size_t size = static_cast<size_t>((to - from) + 1);

  double *data = new double[size];

  for (int i = 0, j = from; j < to + 1; i++, j++) {
    data[i] = colData->valueAt(i);
  }
  // sort the data
  gsl_sort(data, 1, size - 1);

  StatBox2D::BoxWhiskerData statBoxData;
  statBoxData.key = key;
  // basic stats
  statBoxData.mean = gsl_stats_mean(data, 1, size);
  statBoxData.median = gsl_stats_median_from_sorted_data(data, 1, size);
  statBoxData.sd = gsl_stats_sd(data, 1, size);
  statBoxData.se = statBoxData.sd / sqrt(static_cast<double>(size));
  // data bounds
  statBoxData.boxWhiskerDataBounds.sd_lower = statBoxData.mean - statBoxData.sd;
  statBoxData.boxWhiskerDataBounds.sd_upper = statBoxData.mean + statBoxData.sd;
  statBoxData.boxWhiskerDataBounds.se_lower = statBoxData.mean - statBoxData.se;
  statBoxData.boxWhiskerDataBounds.se_upper = statBoxData.mean + statBoxData.se;
  statBoxData.boxWhiskerDataBounds.perc_1 =
      gsl_stats_quantile_from_sorted_data(data, 1, size, 0.01);
  statBoxData.boxWhiskerDataBounds.perc_5 =
      gsl_stats_quantile_from_sorted_data(data, 1, size, 0.05);
  statBoxData.boxWhiskerDataBounds.perc_10 =
      gsl_stats_quantile_from_sorted_data(data, 1, size, 0.10);
  statBoxData.boxWhiskerDataBounds.perc_25 =
      gsl_stats_quantile_from_sorted_data(data, 1, size, 0.25);
  statBoxData.boxWhiskerDataBounds.perc_75 =
      gsl_stats_quantile_from_sorted_data(data, 1, size, 0.75);
  statBoxData.boxWhiskerDataBounds.perc_90 =
      gsl_stats_quantile_from_sorted_data(data, 1, size, 0.90);
  statBoxData.boxWhiskerDataBounds.perc_95 =
      gsl_stats_quantile_from_sorted_data(data, 1, size, 0.95);
  statBoxData.boxWhiskerDataBounds.perc_99 =
      gsl_stats_quantile_from_sorted_data(data, 1, size, 0.99);
  statBoxData.boxWhiskerDataBounds.max = data[size - 1];
  statBoxData.boxWhiskerDataBounds.min = data[0];

  // delete the double data pointer
  delete[] data;

  return statBoxData;
}
void single_sort(const char *dist_name, function_ptr dist_func, const char *sort_name, function_ptr sort_func, int runs, int side)
{
	double cmean, csdev, tmean, tsdev;
	struct timespec t0, t1;

	int progress = (runs <= 10) ? 1 : runs / 10;

	std::cout << std::setw(4) << side << "  ";
	std::cout << dist_name << "  " << sort_name << "  " << std::flush;
	for (int i = 1; i <= runs; i++) {
		random_seed(i);
		tcomp = tread = twrit = 0;
		(*dist_func)();

		if ((i % progress) == 0) {
			std::cout << '.' << std::flush;
		}
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0);
		(*sort_func)();
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1);

		comp[i - 1] = tcomp;
		msec[i - 1] = (t1.tv_sec - t0.tv_sec) * 1000.0 + (t1.tv_nsec - t0.tv_nsec) * 0.000001;
		if (! is_spatially_sorted()) {
			std::cout << "result is not sorted!\n";
		}
	}
	cmean = gsl_stats_mean(comp, 1, runs);
	csdev = gsl_stats_sd(comp, 1, runs);
	tmean = gsl_stats_mean(msec, 1, runs);
	tsdev = gsl_stats_sd(msec, 1, runs);
	std::cout << std::setprecision(0) << std::fixed;
	std::cout << std::setw(12) << cmean << ' ' << std::setw(12) << csdev << ' ';
	std::cout << std::setprecision(2) << std::fixed;
	std::cout << std::setw(12) << tmean << ' ' << std::setw(12) << tsdev << '\n';
}
Exemple #21
0
void update_chi2_t(chi2_t *chichi, hist_t *obsHist, double *dataMat)
{
  //-- data should be n*d matrix
  int N = chichi->N;
  int d = chichi->d;
  gsl_vector *X_model = chichi->X_model;
  gsl_matrix *cov     = chichi->cov;
  gsl_matrix *cov2    = chichi->cov2;
  double value;
  
  //-- Update mean
  int i;
  for (i=0; i<d; i++) {
    value = gsl_stats_mean(dataMat+i*N, 1, N);
    gsl_vector_set(X_model, i, value);
    gsl_vector_set(chichi->X_obs, i, (double)(obsHist->n[i]));
  }
  
  //-- Update covariance
  int j;
  for (j=0; j<d; j++) {
    for (i=0; i<d; i++) {
      if (i < j) {
	value = gsl_matrix_get(cov, j, i);
	gsl_matrix_set(cov, i, j, value);
	continue;
      }
      value = gsl_stats_covariance_m(dataMat+i*N, 1, dataMat+j*N, 1, N, gsl_vector_get(X_model, i), gsl_vector_get(X_model, j));
      gsl_matrix_set(cov, i, j, value);
    } 
  }
  
  //-- Make a copy, because the invertion will destroy cov
  gsl_matrix_memcpy(cov2, cov); //-- Copy cov to cov2
  
  //-- Make LU decomposition and invert
  int s;
  gsl_linalg_LU_decomp(cov2, chichi->perm, &s);
  gsl_linalg_LU_invert(cov2, chichi->perm, chichi->invCov);
  
  //-- Debias the C_{ij}^{-1}
  //-- C^-1_nonbias = (N - d - 2) / (N - 1) * C^-1_bias
  double factor = (N - d - 2) / (double)(N - 1);
  gsl_matrix_scale(chichi->invCov, factor); //-- invCov *= factor
  return;
}
Exemple #22
0
int pxy_calc(hgt_stat_mean ***means, hgt_stat_variance ***vars, double *pxy, double *d1, double *d2,hgt_pop **ps, hgt_params *params, int rank, int numprocs, hgt_cov_sample_func sample_func, gsl_rng *r) {
    char * func_name = "pxy_calc";
    unsigned count, dest, tag, error_code;
    count = params->maxl * 4;
    dest = 0;
    tag = 0;
    unsigned i;
    for (i = 0; i < params->replicates; i++) {
        unsigned n;
        for (n = 0; n < params->sample_size; n++) {
            hgt_pop_calc_dist(ps[i], d1, d2, 1, sample_func, r);
            hgt_pop_calc_pxy(pxy+(n*params->maxl*4), params->maxl, d1, d2, params->seq_len, 0);
        }
        unsigned l;
        for (l = 0; l < params->maxl; l++) {
            unsigned k;
            for (k = 0; k < 4; k++) {
                pxy[l*4+k] = gsl_stats_mean(pxy+l*4+k, params->maxl*4, params->sample_size);
            }
        }

        if (rank != 0) {
            error_code = MPI_Send(pxy, count, MPI_DOUBLE, dest, tag, MPI_COMM_WORLD);
            check_mpi_error_code(error_code, "MPI_Send", func_name);
        } else {
            int j;
            for (j = 0; j < numprocs; j++) {
                if (j != 0) {
                    error_code = MPI_Recv(pxy, count, MPI_DOUBLE, j, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                    check_mpi_error_code(error_code, "MPI_Recv", func_name);
                }
                unsigned l;
                for (l = 0; l < params->maxl; l++) {
                    unsigned k;
                    for (k = 0; k < 4; k++) {
                        hgt_stat_mean_increment(means[l][k], pxy[l*4+k]);
                        hgt_stat_variance_increment(vars[l][k], pxy[l*4+k]);
                    }
                }
            }
        }
    }

    return EXIT_SUCCESS;
}
static double
robust_robsigma(const gsl_vector *r, const double s,
                const double tune, gsl_multifit_robust_workspace *w)
{
  double sigma;
  size_t i;
  const size_t n = w->n;
  const size_t p = w->p;
  const double st = s * tune;
  double a, b, lambda;

  /* compute u = r / sqrt(1 - h) / st */
  gsl_vector_memcpy(w->workn, r);
  gsl_vector_mul(w->workn, w->resfac);
  gsl_vector_scale(w->workn, 1.0 / st);

  /* compute w(u) and psi'(u) */
  w->type->wfun(w->workn, w->psi);
  w->type->psi_deriv(w->workn, w->dpsi);

  /* compute psi(u) = u*w(u) */
  gsl_vector_mul(w->psi, w->workn);

  /* Street et al, Eq (3) */
  a = gsl_stats_mean(w->dpsi->data, w->dpsi->stride, n);

  /* Street et al, Eq (5) */
  b = 0.0;
  for (i = 0; i < n; ++i)
    {
      double psi_i = gsl_vector_get(w->psi, i);
      double resfac = gsl_vector_get(w->resfac, i);
      double fac = 1.0 / (resfac*resfac); /* 1 - h */

      b += fac * psi_i * psi_i;
    }
  b /= (double) (n - p);

  /* Street et al, Eq (5) */
  lambda = 1.0 + ((double)p)/((double)n) * (1.0 - a) / a;

  sigma = lambda * sqrt(b) * st / a;

  return sigma;
} /* robust_robsigma() */
Exemple #24
0
/**
 * Get a summary statistic for the parameters; for instance,
 * the median value calculated over all the elements of the list.
 * @param kl List
 * @param what Can be one of: STAT_MEAN, STAT_MEDIAN, STAT_STDDEV, STAT_MAD. 
 * @return A vector whose entries are the summary statistic for the 
 * corresponding orbital parameter.
 */
gsl_vector* KL_getParsStats(const ok_list* kl, const int what) {

    gsl_vector* v = gsl_vector_alloc(kl->size);
    gsl_vector* ret = gsl_vector_calloc(PARAMS_SIZE + 1);
    
    
    for (int j = 0; j < PARAMS_SIZE + 1; j++) {
        if (j == PARAMS_SIZE)
                for (int n = 0; n < kl->size; n++) {
                        VSET(v, n, kl->kernels[n]->merit);
                }
        else
                for (int n = 0; n < kl->size; n++) {
                        VSET(v, n, VGET(kl->kernels[n]->params, j));
                }

        switch (what) {
            case STAT_MEAN:
                VSET(ret, j, gsl_stats_mean(v->data, 1, v->size));
                break;
            case STAT_STDDEV:
                VSET(ret, j, gsl_stats_sd(v->data, 1, v->size));
                break;
            case STAT_MEDIAN:
                gsl_sort_vector(v);
                VSET(ret, j, gsl_stats_median_from_sorted_data(v->data, 1, v->size));
                break;
            case STAT_MAD:
                gsl_sort_vector(v);
                double med = gsl_stats_median_from_sorted_data(v->data, 1, v->size);
                VSET(ret, j, 1.4826 * ok_mad(v->data, v->size, med));
                break;
            default:
                // percentiles
                gsl_sort_vector(v);
                VSET(v , j, gsl_stats_quantile_from_sorted_data(v->data, 1, v->size, (double)(what)/100.));
        };
    };
            
    gsl_vector_free(v);
    return ret;
}
Exemple #25
0
int
main(void)
{
  double data[5] = {17.2, 18.1, 16.5, 18.3, 12.6};
  double mean, variance, largest, smallest;

  mean     = gsl_stats_mean(data, 1, 5);
  variance = gsl_stats_variance(data, 1, 5);
  largest  = gsl_stats_max(data, 1, 5);
  smallest = gsl_stats_min(data, 1, 5);

  printf ("The dataset is %g, %g, %g, %g, %g\n",
         data[0], data[1], data[2], data[3], data[4]);

  printf ("The sample mean is %g\n", mean);
  printf ("The estimated variance is %g\n", variance);
  printf ("The largest value is %g\n", largest);
  printf ("The smallest value is %g\n", smallest);
  return 0;
}
int ZeroOutliersOne (double *buf, int len, double alpha) {
  /*
    Replace outliers in the input buffer with zeros using the Grubbs
    test (at the significance level alpha). The buffer is
    overwritten. Return the number of outliers found.
   */
  int noutliers = 0, idx, n, outlier = 1;
  double meanval, maxval, sdval, tn, critval, v, *tmp;

  tmp = (double *) malloc (len*sizeof (double));
  while (outlier) {
    n = 0;
    for (idx=0; idx<len; idx++)
      if ((v=*(buf+idx)))
	*(tmp+n++) = v;
    if (n>2) {
      meanval = gsl_stats_mean (tmp, 1, n);
      maxval = meanval;
      for (idx=0; idx<n; idx++) {
	v = *(tmp+idx);
	if (fabs(v-meanval) > fabs(maxval-meanval))
	  maxval = v;
      }
      sdval = gsl_stats_sd_m (tmp, 1, n, meanval);
      tn = fabs ((maxval-meanval)/sdval);
      critval = zcritical (alpha, n);
      if (tn>critval) {
	for (idx=0; idx<len; idx++)
	  if (*(buf+idx) == maxval) {
	    *(buf+idx) = 0;
	    noutliers++;
	  }
      } else 
	outlier = 0;
    } else
      outlier = 0;
  }
  free (tmp);
  return noutliers;
}
void print_measurement_results_prediction(const job_t* job_p, const reprompib_common_options_t* opts_p,
    double* maxRuntimes_sec, const nrep_pred_params_t* pred_params_p, const pred_conditions_t* conds_p) {

  int j;
  int my_rank, np;
  double mean_runtime_sec, median_runtime_sec;
  FILE* f;

  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
  MPI_Comm_size(MPI_COMM_WORLD, &np);

  f = stdout;
  if (my_rank == OUTPUT_ROOT_PROC) {
    if (opts_p->output_file != NULL) {
      f = fopen(opts_p->output_file, "a");
    }

    if (job_p->n_rep == 0) {   // no correct results
      // runtime_sec = 0;
      mean_runtime_sec = 0;
      median_runtime_sec = 0;
    } else {      // print the last measured runtime
      //runtime_sec = maxRuntimes_sec[job_p->n_rep - 1];
      mean_runtime_sec = gsl_stats_mean(maxRuntimes_sec, 1, job_p->n_rep);
      qsort(maxRuntimes_sec, job_p->n_rep, sizeof(double), cmpfunc);
      median_runtime_sec = gsl_stats_median_from_sorted_data(maxRuntimes_sec, 1, job_p->n_rep);
    }

    for (j = 0; j < conds_p->n_methods; j++) {
      fprintf(f, "%s %ld %ld %.10f %.10f %s %.10f\n", get_call_from_index(job_p->call_index), job_p->n_rep, job_p->count,
          mean_runtime_sec, median_runtime_sec, get_prediction_methods_list()[pred_params_p->info[j].method],
          conds_p->conditions[j]);
    }

    if (opts_p->output_file != NULL) {
      fclose(f);
    }
  }

}
Exemple #28
0
void streamRtsFile(xmlTextReaderPtr reader) 
{
	if (reader == NULL) {
		fprintf(stderr, "Unable to access the file.\n");
		exit(EXIT_FAILURE);
	}

	int ret;

	if (verbose) {
		printf("=== Analyzing ===\n");
	}

	ret = xmlTextReaderRead(reader);
	while (ret == 1 && cont > 0) {
		processNode(reader);
		ret = xmlTextReaderRead(reader);
	}

	if (ret != 0 && cont > 0) {
		printf("Failed to parse.\n");
		return;
	}

	printf("=== FU result ===\n");
	double mean = gsl_stats_mean(fuArray, 1, limit);
	printf("Mean:\t\t%.5f (%.3f)\n", mean, mean * 100.0);
	double variance = gsl_stats_variance_m(fuArray, 1, limit, mean);
	printf("Variance:\t%.5f\n", variance);
	double stddev = gsl_stats_sd_m(fuArray, 1, limit, mean);
	printf("Std. Dev:\t%.5f\n",  stddev);
	double max = gsl_stats_max(fuArray, 1, limit);
	printf("Max:\t\t%.5f (%.3f)\n", max,  max * 100.0);
	double min = gsl_stats_min(fuArray, 1, limit);
	printf("Min:\t\t%.5f (%.3f)\n", min,  min * 100.0);
	printf("Total RTS:\t%d\n", rtsNr);
	printf("Valid RTS:\t%d\n", validFuCnt);
	printf("Invalid RTS:\t%d\n", invalidFuCnt);
}
Exemple #29
0
void
test_basic(const size_t n, const double data[], const double tol)
{
  gsl_rstat_workspace *rstat_workspace_p = gsl_rstat_alloc();
  const double expected_mean = gsl_stats_mean(data, 1, n);
  const double expected_var = gsl_stats_variance(data, 1, n);
  const double expected_sd = gsl_stats_sd(data, 1, n);
  const double expected_skew = gsl_stats_skew(data, 1, n);
  const double expected_kurtosis = gsl_stats_kurtosis(data, 1, n);
  double expected_rms = 0.0;
  double mean, var, sd, rms, skew, kurtosis;
  size_t i;

  /* compute expected rms */
  for (i = 0; i < n; ++i)
    expected_rms += data[i] * data[i];

  expected_rms = sqrt(expected_rms / n);

  /* add data to rstat workspace */
  for (i = 0; i < n; ++i)
    gsl_rstat_add(data[i], rstat_workspace_p);

  mean = gsl_rstat_mean(rstat_workspace_p);
  var = gsl_rstat_variance(rstat_workspace_p);
  sd = gsl_rstat_sd(rstat_workspace_p);
  rms = gsl_rstat_rms(rstat_workspace_p);
  skew = gsl_rstat_skew(rstat_workspace_p);
  kurtosis = gsl_rstat_kurtosis(rstat_workspace_p);

  gsl_test_rel(mean, expected_mean, tol, "mean n=%zu", n);
  gsl_test_rel(var, expected_var, tol, "variance n=%zu", n);
  gsl_test_rel(sd, expected_sd, tol, "stddev n=%zu", n);
  gsl_test_rel(rms, expected_rms, tol, "rms n=%zu", n);
  gsl_test_rel(skew, expected_skew, tol, "skew n=%zu", n);
  gsl_test_rel(kurtosis, expected_kurtosis, tol, "kurtosis n=%zu", n);

  gsl_rstat_free(rstat_workspace_p);
}
Exemple #30
0
int main(void)
{
    double v[NEXP];      /* calculated volumes */
    gsl_rng *r;
    double mean, sd = 1;
    int n, j;
    unsigned long seed = 1UL;
    long p = POINTS;

    /* allocate random number generator */
    r = gsl_rng_alloc(gsl_rng_taus2);

    /* seed the random number generator */
    gsl_rng_set(r, seed);

    for (n = 1; n <= 9; n++)
    {
        while (sd >= .03)
        {
            for (j = 0; j < NEXP; j++)
            {
                /* calculate volume using Monte Carlo algorithm */
                v[j] = mc_se(p, n, r);
            }
            mean = gsl_stats_mean(v, 1, NEXP);
            sd = gsl_stats_sd_m(v, 1, NEXP, mean);
            p *= 2;
        }
        printf("%d  %10.8f   %10.8f\n", n, mean, sd);
        /* reset the standard deviation and the number of points at each iteration */
        sd = 1;
        p = POINTS;
    }

    gsl_rng_free(r);

    return(0);
}