Hi Troels, If you need speed for your analysis here, just store the sums in temporary variables and reuse them. Minimising these repetitions, as well as the "x - x_m" array subtraction, could save huge amounts of time.
Regards, Edward On 8 December 2014 at 17:24, <tlin...@nmr-relax.com> wrote: > Author: tlinnet > Date: Mon Dec 8 17:24:35 2014 > New Revision: 27020 > > URL: http://svn.gna.org/viewcvs/relax?rev=27020&view=rev > Log: > Added two new statistics function to get the linear correlation factor from: > > y = a*x > y = a*x + b > > Task #7873 (https://gna.org/task/index.php?7873): Write wrapper function to > nmrglue, to read .ft2 files and process them. > Homepage: http://www.nmrglue.com/ > Link to nmrglue discussion: > https://groups.google.com/forum/#!forum/nmrglue-discuss > The code is develop at Github: https://github.com/jjhelmus/nmrglue/ > Google code: https://code.google.com/p/nmrglue/ > Documentation: http://nmrglue.readthedocs.org/en/latest/index.html > > Modified: > branches/nmrglue/lib/statistics.py > > Modified: branches/nmrglue/lib/statistics.py > URL: > http://svn.gna.org/viewcvs/relax/branches/nmrglue/lib/statistics.py?rev=27020&r1=27019&r2=27020&view=diff > ============================================================================== > --- branches/nmrglue/lib/statistics.py (original) > +++ branches/nmrglue/lib/statistics.py Mon Dec 8 17:24:35 2014 > @@ -24,11 +24,11 @@ > """Module for calculating simple statistics.""" > > # Python module imports. > -from numpy import absolute, diag, dot, eye, multiply, transpose > +from numpy import absolute, diag, dot, eye, mean, multiply, sqrt, sum, > transpose > from numpy.linalg import inv, qr > > # Python module imports. > -from math import exp, pi, sqrt > +from math import exp, pi > > > def bucket(values=None, lower=0.0, upper=200.0, inc=100, verbose=False): > @@ -160,6 +160,51 @@ > return sd > > > +def linear_corr(x=None, y=None): > + """Calculate the linear correlation 'a', for the function y=a*x. The > function returns "a" and the sample correlation coefficient 'r_xy'. > + > + @keyword x: The data for the X-axis. > + @type x: float or numpy array. > + @keyword y: The data for the Y-axis. > + @type y: float or numpy array. > + @return: The correlation 'a', and sample correlation > coefficient 'r_xy'. > + @rtype: float, float > + """ > + > + # The correlation is. > + a = sum(x*y) / sum(x**2) > + > + # The sample correlation coefficient is. > + r_xy = sum(x*y) / sqrt(sum(x**2) * sum(y**2)) > + > + return a, r_xy > + > + > +def linear_corr_intercept(x=None, y=None): > + """Calculate the linear correlation 'a', the intercept 'b' for the > function y=a*x + b. The function returns "a", "b" and the sample correlation > coefficient 'r_xy'. > + > + @keyword x: The data for the X-axis. > + @type x: float or numpy array. > + @keyword y: The data for the Y-axis. > + @type y: float or numpy array. > + @return: The correlation 'a', the intercept 'b', and sample > correlation coefficient 'r_xy'. > + @rtype: float, float, float > + """ > + > + # Get the mean. > + x_m = mean(x) > + y_m = mean(y) > + > + # Solve by linear least squares > + n = len(y) > + a = (sum(x*y) - 1./n * sum(x) * sum(y) ) / ( sum(x**2) - 1./n * > (sum(x))**2 ) > + b = 1./n * sum(y) - a * 1./n * sum(x) > + > + r_xy = sum( (x - x_m)*(y - y_m) ) / sqrt( sum((x - x_m)**2) * sum((y - > y_m)**2) ) > + > + return a, b, r_xy > + > + > def multifit_covar(J=None, epsrel=0.0, weights=None): > """This is the implementation of the multifit covariance. > > > > _______________________________________________ > relax (http://www.nmr-relax.com) > > This is the relax-commits mailing list > relax-comm...@gna.org > > To unsubscribe from this list, get a password > reminder, or change your subscription options, > visit the list information page at > https://mail.gna.org/listinfo/relax-commits _______________________________________________ relax (http://www.nmr-relax.com) This is the relax-devel mailing list relax-devel@gna.org To unsubscribe from this list, get a password reminder, or change your subscription options, visit the list information page at https://mail.gna.org/listinfo/relax-devel