int main(void) { double data[5] = {17.2, 18.1, 16.5, 18.3, 12.6}; double median, upperq, lowerq; printf ("Original dataset: %g, %g, %g, %g, %g\n", data[0], data[1], data[2], data[3], data[4]); gsl_sort (data, 1, 5); printf ("Sorted dataset: %g, %g, %g, %g, %g\n", data[0], data[1], data[2], data[3], data[4]); median = gsl_stats_median_from_sorted_data (data, 1, 5); upperq = gsl_stats_quantile_from_sorted_data (data, 1, 5, 0.75); lowerq = gsl_stats_quantile_from_sorted_data (data, 1, 5, 0.25); printf ("The median is %g\n", median); printf ("The upper quartile is %g\n", upperq); printf ("The lower quartile is %g\n", lowerq); return 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); } }
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; }
void summary_pct4(double_arr *peakList, hist_t *hist, double *summ) { gsl_sort(peakList->array, 1, peakList->length); summ[0] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.750); summ[1] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.950); summ[2] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.980); summ[3] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.998); return; }
void summary_pct5(double_arr *peakList, hist_t *hist, double *summ) { gsl_sort(peakList->array, 1, peakList->length); summ[0] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.969); summ[1] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.986); summ[2] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.994); summ[3] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.997); summ[4] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.999); return; }
void summary_cut5(double_arr *peakList, hist_t *hist, double *summ) { cutSmallPeaks(peakList, 3.0); gsl_sort(peakList->array, 1, peakList->length); summ[0] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.500); summ[1] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.7763932023); summ[2] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.900); summ[3] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.9552786405); summ[4] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.980); return; }
/* pseudo graphical display of sample chunk statistical properties, only useful with one MPI worker as it prints to terminal. */ void print_chunk_graph(gsl_matrix *X,/*sub-sample of CHUNK rows*/ gsl_vector *lP)/*log-Posterior values, unnormalized*/{ int width=100; // we assume that the display can show $width characters int i,j,k,n,nc; int tmp; double *x; gsl_vector_view x_view; double Q[5]; int q[5]; double max,min,range; char s[32],c[32]; n=X->size2; max=gsl_matrix_max(X); min=gsl_matrix_min(X); range=max-min; printf("range: [%g,%g] (%g)\n",min,max,range); for (i=0;i<X->size1;i++){ //sort each row: x_view=gsl_matrix_row(X,i); gsl_sort_vector(&(x_view.vector)); //determine eachquantile x=gsl_matrix_ptr(X,i,0); Q[0]=gsl_stats_quantile_from_sorted_data(x,1,n,0.01); Q[1]=gsl_stats_quantile_from_sorted_data(x,1,n,0.25); Q[2]=gsl_stats_quantile_from_sorted_data(x,1,n,0.50); Q[3]=gsl_stats_quantile_from_sorted_data(x,1,n,0.75); Q[4]=gsl_stats_quantile_from_sorted_data(x,1,n,0.99); //printf("quantiles: %g\t%g\t%g\t%g\t%g\n",Q[0],Q[1],Q[2],Q[3],Q[4]); for (j=0;j<5;j++) { q[j]=(int) ((Q[j]-min)*width/range); } sprintf(s," -LU- "); sprintf(c,"+{|}+ "); tmp=0; for (k=0;k<5;k++){ nc=q[k]-tmp; for (j=0;j<nc;j++) { printf("%c",s[k]); } tmp=q[k]; printf("%c",c[k]); } printf("\n\n"); } printf("|"); for (j=0;j<width-2;j++) printf("-"); printf("|\n"); printf("%+4.4g",min); for (j=0;j<width-8;j++) printf(" "); printf("%+4.4g\n",max); }
/* 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); }
/** * 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; }
void summary_cut900(double_arr *peakList, hist_t *hist, double *summ) { cutSmallPeaks(peakList, 3.0); gsl_sort(peakList->array, 1, peakList->length); summ[0] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.900); return; }
CAMLprim value ml_gsl_stats_quantile_from_sorted_data(value data, value f) { size_t len = Double_array_length(data); double r = gsl_stats_quantile_from_sorted_data(Double_array_val(data), 1, len, Double_val(f)); return copy_double(r); }
Datum array_quantile(PG_FUNCTION_ARGS) { // The formal PostgreSQL array object ArrayType *array; // The array element type Oid arrayElementType; // The array element type width int16 arrayElementTypeWidth; // The array element type "is passed by value" flags (not used, should always be true) bool arrayElementTypeByValue; // The array element type alignment codes (not used) char arrayElementTypeAlignmentCode; // The array contents, as PostgreSQL "datum" objects Datum *arrayContent; // List of "is null" flags for the array contents bool *arrayNullFlags; // The size of each array int arrayLength; int i,j, nelem; double median, mad, f, quantile; double *inarray; if (PG_ARGISNULL(0) || PG_ARGISNULL(1)) ereport(ERROR, (errmsg("Null arrays not accepted"))); // Get array and quantile from input array = PG_GETARG_ARRAYTYPE_P(0); f = PG_GETARG_FLOAT8(1); if (ARR_NDIM(array) != 1) ereport(ERROR, (errmsg("One-dimesional arrays are required"))); if (array_contains_nulls(array)) ereport(ERROR, (errmsg("Array contains null elements"))); arrayLength = (ARR_DIMS(array))[0]; arrayElementType = ARR_ELEMTYPE(array); get_typlenbyvalalign(arrayElementType, &arrayElementTypeWidth, &arrayElementTypeByValue, &arrayElementTypeAlignmentCode); deconstruct_array(array, arrayElementType, arrayElementTypeWidth, arrayElementTypeByValue, arrayElementTypeAlignmentCode, &arrayContent, &arrayNullFlags, &arrayLength); inarray = (double*)malloc(arrayLength*sizeof(double)); for (i=0; i<arrayLength; i++) { inarray[i] = DatumGetFloat4(arrayContent[i]); } gsl_sort (inarray, 1, arrayLength); quantile = gsl_stats_quantile_from_sorted_data(inarray, 1, arrayLength, f); PG_RETURN_FLOAT8(quantile); }
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 TransformationModelBSpline::getQuantiles_( const gsl_vector * x, const vector<double> & quantiles, gsl_vector * results) { gsl_vector * x_sort; x_sort = gsl_vector_alloc(x->size); gsl_vector_memcpy(x_sort, x); gsl_sort_vector(x_sort); for (Size i = 0; i < quantiles.size(); ++i) { double q = gsl_stats_quantile_from_sorted_data(x_sort->data, 1, x->size, quantiles[i]); gsl_vector_set(results, i, q); } gsl_vector_free(x_sort); }
double as_cv_fixed_params(double beta, double alpha, double s, int n, int num_reps) { double X[n]; double T[num_reps]; int i; srand(time(0)); int k = rand() % 1000000; double params[3] = {beta, alpha, s}; for (i = 0; i < num_reps; i++) { ar1_ts(X, params, n, k + i); // IID draws, because alpha=0 T[i] = as_test_stat(beta, alpha, s, X, n); } gsl_sort(T, 1, num_reps); return gsl_stats_quantile_from_sorted_data(T, 1, num_reps, 0.95); }
void summary_cut6(double_arr *peakList, hist_t *hist, double *summ) { cutSmallPeaks(peakList, 3.0); gsl_sort(peakList->array, 1, peakList->length); summ[0] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.649); summ[1] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.809); summ[2] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.888); summ[3] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.930); summ[4] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.955); summ[5] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.970); return; }
/** * 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; }
int main (int argc, char * argv[]) { parse_opts(argc,argv); if(!doInit()) { printf("Error Initializing Stuff.\n"); exit(EXIT_FAILURE); } else { //o calibrar colores o seguir la linea //bool rotate = true; double pval; bool stop = true; int left, right; while(!ag_psh_is_pushed(&push, &pval)); UDELAY(750000); printf("pval is: %.2f\n", pval); ag_lgt_set_led(&lright, true); ag_lgt_set_led(&lleft, true); if (mode) { //calibrar colores int i = 0; double acumr [calib], minr, maxr, upr, lowr; double acuml [calib], minl, maxl, upl, lowl; for (;i<calib;i++) { acumr[i] = (double)ag_read_int(&lright); acuml[i] = (double)ag_read_int(&lleft); UDELAY(100); } gsl_stats_minmax(&minr, &maxr, acumr, 1, calib); gsl_stats_minmax(&minl, &maxl, acuml, 1, calib); gsl_sort (acumr,1,calib); gsl_sort (acuml,1,calib); upl = gsl_stats_quantile_from_sorted_data (acuml,1,calib,0.95);//uq lowl = gsl_stats_quantile_from_sorted_data (acuml,1,calib,0.05);//lq upr = gsl_stats_quantile_from_sorted_data (acumr,1,calib,0.95);//uq lowr = gsl_stats_quantile_from_sorted_data (acumr,1,calib,0.05);//lq for (i = 0 ; i < 2; i++){ printf("COLOR: %s, SENSOR: %s\n", white ? "WHITE" : "BLACK", i == 0 ? "LEFT" : "RIGHT"); printf("min_v: %d, max_v: %d\n", i == 0 ? (int)minl : (int)minr, i == 0 ? (int)maxl : (int)maxr); printf("low_q: %d, up_q :%d\n", i == 0 ? (int)lowl : (int)lowr, i == 0 ? (int)upl : (int)upr); printf("\n"); } } else { //seguir linea while(!ag_psh_is_pushed(&push, &pval)) { //printf("pval is: %.2f\n", pval); if (stop){ stop = false; move_all(MY_FWD,vel); } right = ag_read_int(&lright); left = ag_read_int(&lleft); if(! IS_WHITE(right) || ! IS_WHITE(left)){ stop_all(); stop = true; if (IS_BLACK(left)){ rotate_robot(vel, true); while(IS_BLACK(ag_read_int(&lleft))); UDELAY(TDELAY); stop_all(); } else if (IS_BLACK(right)) { rotate_robot(vel, false); while(IS_BLACK(ag_read_int(&lright))); UDELAY(TDELAY); stop_all(); } } } } } lego_shutdown(); exit(EXIT_SUCCESS); }
void summary_pct996(double_arr *peakList, hist_t *hist, double *summ) { gsl_sort(peakList->array, 1, peakList->length); summ[0] = gsl_stats_quantile_from_sorted_data(peakList->array, 1, peakList->length, 0.996); return; }
void BoxCurve::drawBox(QPainter *painter, const QwtDiMap &xMap, const QwtDiMap &yMap, double *dat, int size) { const int px = xMap.transform(x(0)); const int px_min = xMap.transform(x(0) - 0.5); const int px_max = xMap.transform(x(0) + 0.5); const int box_width = 1+(px_max - px_min)*b_width/100; const int hbw = box_width/2; const int median = yMap.transform(gsl_stats_median_from_sorted_data (dat, 1, size)); int b_lowerq, b_upperq; double sd, se, mean; if(w_range == SD || w_range == SE || b_range == SD || b_range == SE) { sd = gsl_stats_sd(dat, 1, size); se = sd/sqrt((double)size); mean = gsl_stats_mean(dat, 1, size); } if(b_range == SD) { b_lowerq = yMap.transform(mean - sd*b_coeff); b_upperq = yMap.transform(mean + sd*b_coeff); } else if(b_range == SE) { b_lowerq = yMap.transform(mean - se*b_coeff); b_upperq = yMap.transform(mean + se*b_coeff); } else { b_lowerq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 1-0.01*b_coeff)); b_upperq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.01*b_coeff)); } //draw box if (b_style == Rect) { const QRect r = QRect(px - hbw, b_upperq, box_width, b_lowerq - b_upperq + 1); painter->fillRect(r, QwtPlotCurve::brush()); painter->drawRect(r); } else if (b_style == Diamond) { const QPointArray pa(4); pa[0] = QPoint(px, b_upperq); pa[1] = QPoint(px + hbw, median); pa[2] = QPoint(px, b_lowerq); pa[3] = QPoint(px - hbw, median); painter->setBrush(QwtPlotCurve::brush()); painter->drawPolygon(pa); } else if (b_style == WindBox) { const int lowerq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.25)); const int upperq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.75)); const QPointArray pa(8); pa[0] = QPoint(px + hbw, b_upperq); pa[1] = QPoint(int(px + 0.4*box_width), upperq); pa[2] = QPoint(int(px + 0.4*box_width), lowerq); pa[3] = QPoint(px + hbw, b_lowerq); pa[4] = QPoint(px - hbw, b_lowerq); pa[5] = QPoint(int(px - 0.4*box_width), lowerq); pa[6] = QPoint(int(px - 0.4*box_width), upperq); pa[7] = QPoint(px - hbw, b_upperq); painter->setBrush(QwtPlotCurve::brush()); painter->drawPolygon(pa); } else if (b_style == Notch) { int j = (int)ceil(0.5*(size - 1.96*sqrt((double)size))); int k = (int)ceil(0.5*(size + 1.96*sqrt((double)size))); const int lowerCI = yMap.transform(dat[j]); const int upperCI = yMap.transform(dat[k]); const QPointArray pa(10); pa[0] = QPoint(px + hbw, b_upperq); pa[1] = QPoint(px + hbw, upperCI); pa[2] = QPoint(int(px + 0.25*hbw), median); pa[3] = QPoint(px + hbw, lowerCI); pa[4] = QPoint(px + hbw, b_lowerq); pa[5] = QPoint(px - hbw, b_lowerq); pa[6] = QPoint(px - hbw, lowerCI); pa[7] = QPoint(int(px - 0.25*hbw), median); pa[8] = QPoint(px - hbw, upperCI); pa[9] = QPoint(px - hbw, b_upperq); painter->setBrush(QwtPlotCurve::brush()); painter->drawPolygon(pa); } if (w_range) {//draw whiskers const int l = int(0.1*box_width); int w_upperq, w_lowerq; if(w_range == SD) { w_lowerq = yMap.transform(mean - sd*w_coeff); w_upperq = yMap.transform(mean + sd*w_coeff); } else if(w_range == SE) { w_lowerq = yMap.transform(mean - se*w_coeff); w_upperq = yMap.transform(mean + se*w_coeff); } else { w_lowerq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 1-0.01*w_coeff)); w_upperq = yMap.transform(gsl_stats_quantile_from_sorted_data (dat, 1, size, 0.01*w_coeff)); } painter->drawLine(px - l, w_lowerq, px + l, w_lowerq); painter->drawLine(px - l, w_upperq, px + l, w_upperq); if (b_style) { if (w_upperq != b_upperq) painter->drawLine(px, w_upperq, px, b_upperq); if (w_lowerq != b_lowerq) painter->drawLine(px, w_lowerq, px, b_lowerq); } else painter->drawLine(px, w_upperq, px, w_lowerq); } //draw median line if (b_style == Notch || b_style == NoBox) return; if (b_style == WindBox) painter->drawLine(int(px - 0.4*box_width), median, int(px + 0.4*box_width), median); else painter->drawLine(px - hbw, median, px + hbw, median); }
int main() { gsl_rng *r = gsl_rng_alloc(gsl_rng_default); const double tol1 = 1.0e-8; const double tol2 = 1.0e-3; gsl_ieee_env_setup(); { const size_t N = 2000000; double *data = random_data(N, r); double data2[] = { 4.0, 7.0, 13.0, 16.0 }; size_t i; test_basic(2, data, tol1); test_basic(100, data, tol1); test_basic(1000, data, tol1); test_basic(10000, data, tol1); test_basic(50000, data, tol1); test_basic(80000, data, tol1); test_basic(1500000, data, tol1); test_basic(2000000, data, tol1); for (i = 0; i < 4; ++i) data2[i] += 1.0e9; test_basic(4, data2, tol1); free(data); } { /* dataset from Jain and Chlamtac paper */ const size_t n_jain = 20; const double data_jain[] = { 0.02, 0.15, 0.74, 3.39, 0.83, 22.37, 10.15, 15.43, 38.62, 15.92, 34.60, 10.28, 1.47, 0.40, 0.05, 11.39, 0.27, 0.42, 0.09, 11.37 }; double expected_jain = 4.44063435326; test_quantile(0.5, data_jain, n_jain, expected_jain, tol1, "jain"); } { size_t n = 1000000; double *data = malloc(n * sizeof(double)); double *sorted_data = malloc(n * sizeof(double)); gsl_rstat_workspace *rstat_workspace_p = gsl_rstat_alloc(); double p; size_t i; for (i = 0; i < n; ++i) { data[i] = gsl_ran_gaussian_tail(r, 1.3, 1.0); gsl_rstat_add(data[i], rstat_workspace_p); } memcpy(sorted_data, data, n * sizeof(double)); gsl_sort(sorted_data, 1, n); /* test quantile calculation */ for (p = 0.1; p <= 0.9; p += 0.1) { double expected = gsl_stats_quantile_from_sorted_data(sorted_data, 1, n, p); test_quantile(p, data, n, expected, tol2, "gauss"); } /* test mean, variance */ { 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); const double expected_median = gsl_stats_quantile_from_sorted_data(sorted_data, 1, n, 0.5); const double mean = gsl_rstat_mean(rstat_workspace_p); const double var = gsl_rstat_variance(rstat_workspace_p); const double sd = gsl_rstat_sd(rstat_workspace_p); const double skew = gsl_rstat_skew(rstat_workspace_p); const double kurtosis = gsl_rstat_kurtosis(rstat_workspace_p); const double median = gsl_rstat_median(rstat_workspace_p); gsl_test_rel(mean, expected_mean, tol1, "mean"); gsl_test_rel(var, expected_var, tol1, "variance"); gsl_test_rel(sd, expected_sd, tol1, "stddev"); gsl_test_rel(skew, expected_skew, tol1, "skew"); gsl_test_rel(kurtosis, expected_kurtosis, tol1, "kurtosis"); gsl_test_abs(median, expected_median, tol2, "median"); } free(data); free(sorted_data); gsl_rstat_free(rstat_workspace_p); } gsl_rng_free(r); exit (gsl_test_summary()); }
void posterior_summary(const gsl_matrix *theta, FILE *ofile, long M) { size_t T=theta->size1; size_t npar=theta->size2; gsl_vector *tmp=gsl_vector_alloc(T); int i,j; double median,lower,upper; printf("\n Writing MCMC draws to out\n\n"); FILE *file = fopen("out","w"); for(i=0;i<T;i++){ for(j=0;j<npar;j++) fprintf(file,"%14.6e ",mget(theta,i,j)); fprintf(file,"\n"); } fprintf(ofile,"\n\n Posterior Summary \n"); fprintf(ofile,"\n T=%lu\n\n",T); fprintf(ofile,"\n Mean Median Stdev 0.95 DI\n\n"); for(i=0;i<npar;i++){ gsl_matrix_get_col( tmp, theta, i); gsl_sort_vector(tmp); median=gsl_stats_median_from_sorted_data(tmp->data,tmp->stride,tmp->size); lower=gsl_stats_quantile_from_sorted_data(tmp->data,tmp->stride,tmp->size,0.025); upper=gsl_stats_quantile_from_sorted_data(tmp->data,tmp->stride,tmp->size,0.975); fprintf(ofile,"%2d %14.7e %14.7e %14.7e (%14.7e,%14.7e)\n" ,i,mean(tmp),median,sqrt(var(tmp)),lower,upper); } long tau; if( M < 0 ) tau=1000; else tau=M; gsl_vector *rho=gsl_vector_alloc(tau); fprintf(ofile,"\n ACF"); fprintf(ofile,"\n NSE Ineff 1 50 100 500\n"); for(i=0;i<npar;i++){ gsl_matrix_get_col( tmp, theta, i); acf(tmp,tau,rho); /* write out ACF for each parameter */ char file_name[20] = "acf.dat"; sprintf( &file_name[7],"%d",i); FILE *fp_acf = fopen( file_name, "w"); for(j=0;j<tau;j++) fprintf(fp_acf,"%g\n",vget(rho,j)); fclose(fp_acf); /* get inefficiency factor using Newey-West estimate of Long-run var*/ double ineff=1.0; for(j=0;j<tau-1;j++){ ineff += 2.0*(tau-j-1)/tau*vget(rho,j); } /* numerical standard error for posterior mean */ double nse=sqrt(var(tmp)*ineff/T); fprintf(ofile,"%2d %12.5e %12.5e %12.5e %12.5e %12.5e %12.5e\n" ,i,nse,ineff,vget(rho,0),vget(rho,49),vget(rho,99),vget(rho,499)); /* produce kernel density plot for each parameter */ char file_name2[20] = "den.dat"; sprintf( &file_name2[7],"%d",i); FILE *fp_den = fopen( file_name2, "w"); double stdev = sqrt(var(tmp)); lower = gsl_vector_min(tmp) - stdev; upper = gsl_vector_max(tmp) + stdev; den_est_file(tmp, lower , upper ,100, fp_den, -1.0); } fprintf(ofile,"\n\n"); gsl_vector_free(rho); gsl_vector_free(tmp); }
int main(int argc, char *argv[]) { if(argc<3) { printf("Not enough arguments\n"); printf("Number of arguments:%d\n",argc); exit(1); } if(!CheckFile(argv[1])) { printf("\nfile argv[1]:%s does not exist!.\n",argv[1]); // USAGE; exit(1); } char *name_file_input = argv[1]; char *name_file_output = argv[2]; int i, j; int nb_lines, nb_counts, nb_massRanges; char char_dumb[20]; int *treeId; double mass, dmass, median, quartil1, quartil3, sigma2, mean; double *nodeMass, *diskStellarMass, *diskGasMass, *diskCircularVelocity, *spheroidStellarMass, *spheroidGasMass, *bulgeFractionInRange; FILE *file_input, *file_output; //////////////////////////////////////////////////////////////////////////////////////////////////// file_input = fopen(name_file_input,"r"); nb_lines = 0; while( !feof(file_input) ) { for(i=0; i<5; i++) fscanf(file_input,"%s", char_dumb); nb_lines++; } fclose(file_input); nb_lines=nb_lines-2; printf("Number of lines:%d\n",nb_lines); treeId = malloc((size_t)nb_lines*sizeof(int)); nodeMass = malloc((size_t)nb_lines*sizeof(double)); diskStellarMass = malloc((size_t)nb_lines*sizeof(double)); diskGasMass = malloc((size_t)nb_lines*sizeof(double)); diskCircularVelocity = malloc((size_t)nb_lines*sizeof(double)); spheroidStellarMass = malloc((size_t)nb_lines*sizeof(double)); spheroidGasMass = malloc((size_t)nb_lines*sizeof(double)); file_input = fopen(name_file_input,"r"); for(i=0; i<5; i++) fscanf(file_input,"%s", char_dumb); for(i=0; i<nb_lines; i++ ) fscanf(file_input,"%lf %lf %lf %lf %lf" , &nodeMass[i], &spheroidStellarMass[i], &spheroidGasMass[i], &diskStellarMass[i], &diskGasMass[i] ); fclose(file_input); //////////////////////////////////////////////////////////////////////////////////////////////////// file_output = fopen(name_file_output,"w"); fprintf(file_output, "#1.haloMass #2.quartil1 #3.median #4.quartil3 #5.quartil4\n"); nb_massRanges = 60; dmass = (11.2-10.0)/nb_massRanges; for(mass=10.0; mass<11.2; mass+=dmass) { j = 0; for(i=0; i<nb_lines; i++ ) if( (log10(diskStellarMass[i] + spheroidStellarMass[i]) >= mass) && (log10(diskStellarMass[i] + spheroidStellarMass[i]) < mass+dmass) && (spheroidStellarMass[i]/(diskStellarMass[i] + spheroidStellarMass[i] + diskGasMass[i])<=0.6) ) j++; //&& (diskStellarMass[i] + spheroidStellarMass[i]> 0.0) nb_counts = j; //printf("number of matches:%d\n",nb_counts); bulgeFractionInRange = malloc((size_t)nb_counts*sizeof(double)); j = 0; for(i=0; i<nb_lines; i++ ) if( (log10(diskStellarMass[i] + spheroidStellarMass[i]) >= mass) && (log10(diskStellarMass[i] + spheroidStellarMass[i]) < mass+dmass) && (spheroidStellarMass[i]/(diskStellarMass[i] + spheroidStellarMass[i] + diskGasMass[i])<=0.6)) { //&& (diskStellarMass[i] + spheroidStellarMass[i]> 0.0) bulgeFractionInRange[j] = spheroidStellarMass[i]/(diskGasMass[i] + spheroidGasMass[i] + diskStellarMass[i] + spheroidStellarMass[i]); j++; } gsl_sort(bulgeFractionInRange, 1, nb_counts); mean = gsl_stats_mean (bulgeFractionInRange, 1, nb_counts); median = gsl_stats_median_from_sorted_data(bulgeFractionInRange, 1, nb_counts); sigma2 = gsl_stats_variance (bulgeFractionInRange, 1, nb_counts); quartil1 = gsl_stats_quantile_from_sorted_data (bulgeFractionInRange, 1, nb_counts, 0.25); quartil3 = gsl_stats_quantile_from_sorted_data (bulgeFractionInRange, 1, nb_counts, 0.75); //quartil4 = gsl_stats_quantile_from_sorted_data (gasFractionInRange, 1, nb_counts, 1.0); fprintf(file_output,"%f %f %f %f %f %f\n", mass, quartil1, median, quartil3, mean, sigma2); } fclose(file_output); return 0; }
void sigma_clip_median__cy(double* pixels, int n_pixels, int n_images, double* output, double nsigma, int max_repeat) { int x,y,l, repeat; int n_bytes = n_images*sizeof(bool); // printf("n bytes to hold mask: %d\n", n_bytes); bool *good_value = (bool*)malloc(n_images*sizeof(bool)); // printf("\n\n\ngood-value=%x\n\n\n",good_value); double *pixelvalue = (double*)malloc(n_images*sizeof(double)); for (x=0; x<n_pixels ; x++) { // set output to NaN, this is the default output[x] = nan; /* printf("\n\n\n\n\n\n\rColumn %d", x+1); */ // // Figure out how many pixels are not NaN // int n_good_pixels = 0; for (y=0; y<n_images; y++) { if (!isnan(pixels[x*n_images+y])) { good_value[y] = true; pixelvalue[n_good_pixels] = pixels[x*n_images+y]; n_good_pixels++; } } if (n_good_pixels < 1) { // all pixels appear to be NaNs, nothing left to do continue; } // // Now sort all values // gsl_sort(pixelvalue, 1, n_good_pixels); /* for (l=0; l<n_good_pixels; l++) { */ /* printf("%2d -> %.0f\n", l, pixelvalue[l]); */ /* } */ /* printf("\n"); */ double median, sigma_plus, sigma_minus, upper, lower, sigma; int start=0; int end=n_good_pixels; int i, new_start, new_end, n_valid_values; new_start = start; new_end = end; /* printf("Starting iterating: start/end=%d,%d, new-start/end=%d,%d\n", start, end, new_start, new_end); */ /* for (i=0; i<end; i++) { */ /* printf("%2d: %f\n", i, pixelvalue[i]); */ /* } */ for (repeat=0; repeat<max_repeat && (end-start)>=3; repeat++) { end = new_end; start = new_start; n_valid_values = end - start; /* printf("Iteration %d: start/end=%d,%d #=%d\n", repeat, start, end, n_valid_values); */ // Compute median and the sigma-widths median = gsl_stats_median_from_sorted_data(&pixelvalue[start], 1, n_valid_values); sigma_minus = median - gsl_stats_quantile_from_sorted_data(&pixelvalue[start], 1, n_valid_values, 0.16); sigma_plus = gsl_stats_quantile_from_sorted_data(&pixelvalue[start], 1, n_valid_values, 0.84) - median; sigma = 0.5 * (gsl_stats_quantile_from_sorted_data(&pixelvalue[start], 1, n_valid_values, 0.84) - gsl_stats_quantile_from_sorted_data(&pixelvalue[start], 1, n_valid_values, 0.16)); // Compute the valid range of pixels lower = median - nsigma * sigma; upper = median + nsigma * sigma; /* lower = median - nsigma * sigma_minus; */ /* upper = median + nsigma * sigma_plus; */ // Now figure out the start and end range of pixels within the range /* printf("Lower limit:\n"); */ for (new_start=start; new_start<end; new_start++) { if (pixelvalue[new_start] >= lower) { /* printf("Value %d is %f >= %f, taking as new lower limit\n", */ /* new_start, pixelvalue[new_start], lower); */ break; } /* printf("Value %d is %f < %f, skipping\n", */ /* new_start, pixelvalue[new_start], lower); */ } for (new_end = end; new_end > new_start;) { new_end--; if (pixelvalue[new_end] <= upper) { /* printf("Value %d is %f <= %f, taking as new upper limit\n", */ /* new_end, pixelvalue[new_end], upper); */ // Make stick to nomenclature that end means the index of the first // entry that is no longer contained in the list new_end++; break; } /* printf("Value %d is %f > %f, skipping\n", */ /* new_end, pixelvalue[new_end], upper); */ } /* printf("Was: %d/%d now is %d,%d\n", */ /* start, end, new_start, new_end); */ /* printf("Iteration %d: median=%f (sigma= %f / %f) lower/upper: %f %f #=%d\n", */ /* repeat, median, sigma_minus, sigma_plus, lower, upper, n_valid_values); */ /* printf("%d --> %d D=%d (%d)\n", */ /* pixelvalue, &pixelvalue[start], &pixelvalue[start]-pixelvalue, start); */ /* printf("Remaining valid values:\n"); */ /* for (i=new_start; i<new_end; i++) { */ /* printf("%f\n", pixelvalue[i]); */ /* } */ if (new_start == start && new_end == end) { // This iteration hasn't changed anything, so future iteration won't change // anything either, so we can stop right here /* printf("No change in this iteration skipping rest of work.\n"); */ break; } } if ((new_end - new_start) >= 3) { end = new_end; start = new_start; } // // Now we have all pixels limited down to iteratively select only the // 3-sigma range for each pixel; Now compute the mean value. // output[x] = gsl_stats_median_from_sorted_data(&pixelvalue[start], 1, (end-start)); // output[x] = (double)(end-start); //gsl_stats_mean(&pixelvalue[start], 1, (end-start)); // printf("filtered output: %f (%d, %d)\n", output[x], start, end); } // printf("\n\ngood-value=%x\n\n",good_value); /* free(good_value); */ // printf("Freeing memory\n"); free((void*)good_value); // printf("done freeing\n"); // printf("Work done in c_sigclip.c\n"); return; }