1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*- */
4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * Green Red Orange Magenta Azure Cyan Skyblue
44 #include "gmx_statistics.h"
46 static int gmx_dnint(double x)
51 typedef struct gmx_stats {
52 double aa,a,b,sigma_aa,sigma_a,sigma_b,aver,sigma_aver,error;
53 double rmsd,Rdata,Rfit,Rfitaa,chi2,chi2aa;
59 gmx_stats_t gmx_stats_init()
65 return (gmx_stats_t) stats;
68 int gmx_stats_get_npoints(gmx_stats_t gstats, int *N)
70 gmx_stats *stats = (gmx_stats *) gstats;
77 int gmx_stats_done(gmx_stats_t gstats)
79 gmx_stats *stats = (gmx_stats *) gstats;
93 int gmx_stats_add_point(gmx_stats_t gstats,double x,double y,
96 gmx_stats *stats = (gmx_stats *) gstats;
99 if (stats->np+1 >= stats->nalloc)
101 if (stats->nalloc == 0)
102 stats->nalloc = 1024;
105 srenew(stats->x,stats->nalloc);
106 srenew(stats->y,stats->nalloc);
107 srenew(stats->dx,stats->nalloc);
108 srenew(stats->dy,stats->nalloc);
109 for(i=stats->np; (i<stats->nalloc); i++)
117 stats->x[stats->np] = x;
118 stats->y[stats->np] = y;
119 stats->dx[stats->np] = dx;
120 stats->dy[stats->np] = dy;
127 int gmx_stats_get_point(gmx_stats_t gstats,real *x,real *y,
128 real *dx,real *dy,real level)
130 gmx_stats *stats = (gmx_stats *) gstats;
134 if ((ok = gmx_stats_get_rmsd(gstats,&rmsd)) != estatsOK)
139 while ((outlier == 0) && (stats->np_c < stats->np))
141 r = fabs(stats->x[stats->np_c] - stats->y[stats->np_c]);
142 outlier = (r > rmsd*level);
145 if (NULL != x) *x = stats->x[stats->np_c];
146 if (NULL != y) *y = stats->y[stats->np_c];
147 if (NULL != dx) *dx = stats->dx[stats->np_c];
148 if (NULL != dy) *dy = stats->dy[stats->np_c];
158 return estatsNO_POINTS;
161 int gmx_stats_add_points(gmx_stats_t gstats,int n,real *x,real *y,
168 if ((ok = gmx_stats_add_point(gstats,x[i],y[i],
169 (NULL != dx) ? dx[i] : 0,
170 (NULL != dy) ? dy[i] : 0)) != estatsOK)
178 static int gmx_stats_compute(gmx_stats *stats,int weight)
180 double yy,yx,xx,sx,sy,dy,chi2,chi2aa,d2;
181 double ssxx,ssyy,ssxy;
182 double w,wtot,yx_nw,sy_nw,sx_nw,yy_nw,xx_nw,dx2,dy2;
186 if (stats->computed == 0)
190 return estatsNO_POINTS;
202 d2 += dsqr(stats->x[i]-stats->y[i]);
203 if ((stats->dy[i]) && (weight == elsqWEIGHT_Y))
205 w = 1/dsqr(stats->dy[i]);
214 xx += w*dsqr(stats->x[i]);
215 xx_nw += dsqr(stats->x[i]);
217 yy += w*dsqr(stats->y[i]);
218 yy_nw += dsqr(stats->y[i]);
220 yx += w*stats->y[i]*stats->x[i];
221 yx_nw += stats->y[i]*stats->x[i];
224 sx_nw += stats->x[i];
227 sy_nw += stats->y[i];
230 /* Compute average, sigma and error */
231 stats->aver = sy_nw/N;
232 stats->sigma_aver = sqrt(yy_nw/N - dsqr(sy_nw/N));
233 stats->error = stats->sigma_aver/sqrt(N);
235 /* Compute RMSD between x and y */
236 stats->rmsd = sqrt(d2/N);
238 /* Correlation coefficient for data */
244 ssxx = N*(xx_nw - dsqr(sx_nw));
245 ssyy = N*(yy_nw - dsqr(sy_nw));
246 ssxy = N*(yx_nw - (sx_nw*sy_nw));
247 stats->Rdata = sqrt(dsqr(ssxy)/(ssxx*ssyy));
249 /* Compute straight line through datapoints, either with intercept
250 zero (result in aa) or with intercept variable (results in a
258 stats->a = (yx-sx*sy)/(xx-sx*sx);
259 stats->b = (sy)-(stats->a)*(sx);
261 /* Compute chi2, deviation from a line y = ax+b. Also compute
262 chi2aa which returns the deviation from a line y = ax. */
267 if (stats->dy[i] > 0)
275 chi2aa += dsqr((stats->y[i]-(stats->aa*stats->x[i]))/dy);
276 chi2 += dsqr((stats->y[i]-(stats->a*stats->x[i]+stats->b))/dy);
280 stats->chi2 = sqrt(chi2/(N-2));
281 stats->chi2aa = sqrt(chi2aa/(N-2));
283 /* Look up equations! */
286 stats->sigma_a = sqrt(stats->chi2/((N-2)*dx2));
287 stats->sigma_b = stats->sigma_a*sqrt(xx);
288 stats->Rfit = fabs(ssxy)/sqrt(ssxx*ssyy);
289 /*stats->a*sqrt(dx2/dy2);*/
290 stats->Rfitaa = stats->aa*sqrt(dx2/dy2);
308 int gmx_stats_get_ab(gmx_stats_t gstats,int weight,
309 real *a,real *b,real *da,real *db,
310 real *chi2,real *Rfit)
312 gmx_stats *stats = (gmx_stats *) gstats;
315 if ((ok = gmx_stats_compute(stats,weight)) != estatsOK)
327 *da = stats->sigma_a;
331 *db = stats->sigma_b;
345 int gmx_stats_get_a(gmx_stats_t gstats,int weight,real *a,real *da,
346 real *chi2,real *Rfit)
348 gmx_stats *stats = (gmx_stats *) gstats;
351 if ((ok = gmx_stats_compute(stats,weight)) != estatsOK)
353 if (NULL != a) *a = stats->aa;
354 if (NULL != da) *da = stats->sigma_aa;
355 if (NULL != chi2) *chi2 = stats->chi2aa;
356 if (NULL != Rfit) *Rfit = stats->Rfitaa;
361 int gmx_stats_get_average(gmx_stats_t gstats,real *aver)
363 gmx_stats *stats = (gmx_stats *) gstats;
366 if ((ok = gmx_stats_compute(stats,elsqWEIGHT_NONE)) != estatsOK)
376 int gmx_stats_get_ase(gmx_stats_t gstats,real *aver,real *sigma,real *error)
378 gmx_stats *stats = (gmx_stats *) gstats;
381 if ((ok = gmx_stats_compute(stats,elsqWEIGHT_NONE)) != estatsOK)
392 *sigma = stats->sigma_aver;
396 *error = stats->error;
402 int gmx_stats_get_sigma(gmx_stats_t gstats,real *sigma)
404 gmx_stats *stats = (gmx_stats *) gstats;
407 if ((ok = gmx_stats_compute(stats,elsqWEIGHT_NONE)) != estatsOK)
410 *sigma = stats->sigma_aver;
415 int gmx_stats_get_error(gmx_stats_t gstats,real *error)
417 gmx_stats *stats = (gmx_stats *) gstats;
420 if ((ok = gmx_stats_compute(stats,elsqWEIGHT_NONE)) != estatsOK)
423 *error = stats->error;
428 int gmx_stats_get_corr_coeff(gmx_stats_t gstats,real *R)
430 gmx_stats *stats = (gmx_stats *) gstats;
433 if ((ok = gmx_stats_compute(stats,elsqWEIGHT_NONE)) != estatsOK)
441 int gmx_stats_get_rmsd(gmx_stats_t gstats,real *rmsd)
443 gmx_stats *stats = (gmx_stats *) gstats;
446 if ((ok = gmx_stats_compute(stats,elsqWEIGHT_NONE)) != estatsOK)
456 int gmx_stats_dump_xy(gmx_stats_t gstats,FILE *fp)
458 gmx_stats *stats = (gmx_stats *) gstats;
461 for(i=0; (i<stats->np); i++)
463 fprintf(fp,"%12g %12g %12g %12g\n",stats->x[i],stats->y[i],
464 stats->dx[i],stats->dy[i]);
470 int gmx_stats_remove_outliers(gmx_stats_t gstats,double level)
472 gmx_stats *stats = (gmx_stats *) gstats;
473 int i,iter=1,done=0,ok;
476 while ((stats->np >= 10) && !done)
478 if ((ok = gmx_stats_get_rmsd(gstats,&rmsd)) != estatsOK)
483 for(i=0; (i<stats->np); )
485 r = fabs(stats->x[i]-stats->y[i]);
488 fprintf(stderr,"Removing outlier, iter = %d, rmsd = %g, x = %g, y = %g\n",
489 iter,rmsd,stats->x[i],stats->y[i]);
492 stats->x[i] = stats->x[stats->np-1];
493 stats->y[i] = stats->y[stats->np-1];
494 stats->dx[i] = stats->dx[stats->np-1];
495 stats->dy[i] = stats->dy[stats->np-1];
511 int gmx_stats_make_histogram(gmx_stats_t gstats,real binwidth,int *nb,
512 int ehisto,int normalized,real **x,real **y)
514 gmx_stats *stats = (gmx_stats *) gstats;
515 int i,ok,index=0,nbins=*nb,*nindex;
516 double minx,maxx,maxy,miny,delta,dd,minh;
518 if (((binwidth <= 0) && (nbins <= 0)) ||
519 ((binwidth > 0) && (nbins > 0)))
521 return estatsINVALID_INPUT;
525 return estatsNO_POINTS;
527 minx = maxx = stats->x[0];
528 miny = maxy = stats->y[0];
529 for(i=1; (i<stats->np); i++)
531 miny = (stats->y[i] < miny) ? stats->y[i] : miny;
532 maxy = (stats->y[i] > maxy) ? stats->y[i] : maxy;
533 minx = (stats->x[i] < minx) ? stats->x[i] : minx;
534 maxx = (stats->x[i] > maxx) ? stats->x[i] : maxx;
536 if (ehisto == ehistoX)
541 else if (ehisto == ehistoY)
547 return estatsINVALID_INPUT;
551 binwidth = (delta)/nbins;
555 nbins = gmx_dnint((delta)/binwidth + 0.5);
559 for(i=0; (i<nbins); i++)
561 (*x)[i] = minh + binwidth*(i+0.5);
569 dd = 1.0/(binwidth*stats->np);
573 for(i=0; (i<stats->np); i++)
575 if (ehisto == ehistoY)
576 index = (stats->y[i]-miny)/binwidth;
577 else if (ehisto == ehistoX)
578 index = (stats->x[i]-minx)/binwidth;
592 for(i=0; (i<nbins); i++)
594 (*y)[i] /= nindex[i];
601 static const char *stats_error[estatsNR] =
603 "All well in STATS land",
606 "Invalid histogram input",
608 "Not implemented yet"
611 const char *gmx_stats_message(int estats)
613 if ((estats >= 0) && (estats < estatsNR))
615 return stats_error[estats];
619 return stats_error[estatsERROR];
623 /* Old convenience functions, should be merged with the core
625 int lsq_y_ax(int n, real x[], real y[], real *a)
627 gmx_stats_t lsq = gmx_stats_init();
631 gmx_stats_add_points(lsq,n,x,y,0,0);
632 if ((ok = gmx_stats_get_a(lsq,elsqWEIGHT_NONE,a,&da,&chi2,&Rfit)) != estatsOK)
641 for (i=0; i<n; i++) {
650 static int low_lsq_y_ax_b(int n, real *xr, double *xd, real yr[],
651 real *a, real *b,real *r,real *chi2)
656 lsq = gmx_stats_init();
659 if ((ok = gmx_stats_add_point(lsq,(NULL != xd) ? xd[i] : xr[i],yr[i],0,0))
665 if ((ok = gmx_stats_get_ab(lsq,elsqWEIGHT_NONE,a,b,NULL,NULL,chi2,r)) != estatsOK)
672 double x,y,yx,xx,yy,sx,sy,chi2;
675 for (i=0; i<n; i++) {
689 *a = (n*yx-sy*sx)/(n*xx-sx*sx);
691 *r = sqrt((xx-sx*sx)/(yy-sy*sy));
696 chi2 += dsqr(yr[i] - ((*a)*xd[i] + (*b)));
699 chi2 += dsqr(yr[i] - ((*a)*xr[i] + (*b)));
703 return sqrt(chi2/(n-2));
709 int lsq_y_ax_b(int n, real x[], real y[], real *a, real *b,real *r,real *chi2)
711 return low_lsq_y_ax_b(n,x,NULL,y,a,b,r,chi2);
714 int lsq_y_ax_b_xdouble(int n, double x[], real y[], real *a, real *b,
717 return low_lsq_y_ax_b(n,NULL,x,y,a,b,r,chi2);
720 int lsq_y_ax_b_error(int n, real x[], real y[], real dy[],
721 real *a, real *b, real *da, real *db,
727 lsq = gmx_stats_init();
730 if ((ok = gmx_stats_add_point(lsq,x[i],y[i],0,dy[i])) != estatsOK)
735 if ((ok = gmx_stats_get_ab(lsq,elsqWEIGHT_Y,a,b,da,db,chi2,r)) != estatsOK)
739 if ((ok = gmx_stats_done(lsq)) != estatsOK)
747 double sxy,sxx,syy,sx,sy,w,s_2,dx2,dy2,mins;
749 sxy=sxx=syy=sx=sy=w=0.0;
752 mins = min(mins,dy[i]);
754 gmx_fatal(FARGS,"Zero or negative weigths in linear regression analysis");
756 for (i=0; i<n; i++) {
757 s_2 = dsqr(1.0/dy[i]);
758 sxx += s_2*dsqr(x[i]);
759 sxy += s_2*y[i]*x[i];
760 syy += s_2*dsqr(y[i]);
777 *chi2+=dsqr((y[i]-((*a)*x[i]+(*b)))/dy[i]);
780 *da = sqrt(*chi2/((n-2)*dx2));
782 *r = *a*sqrt(dx2/dy2);
785 fprintf(debug,"sx = %g, sy = %g, sxy = %g, sxx = %g, w = %g\n"
786 "chi2 = %g, dx2 = %g\n",
787 sx,sy,sxy,sxx,w,*chi2,dx2);
790 *chi2 = sqrt(*chi2/(n-2));