Skip to content

Instantly share code, notes, and snippets.

@rsnemmen
Created April 1, 2015 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsnemmen/0eb32832c657c19e4d39 to your computer and use it in GitHub Desktop.
Save rsnemmen/0eb32832c657c19e4d39 to your computer and use it in GitHub Desktop.
Computes the mean deviation of the data about the linear model
def scatterfit(x,y,a=None,b=None):
"""
Compute the mean deviation of the data about the linear model given if A,B
(y=ax+b) provided as arguments. Otherwise, compute the mean deviation about
the best-fit line.
x,y assumed to be Numpy arrays. a,b scalars.
Returns the float sd with the mean deviation.
Author: Rodrigo Nemmen
"""
if a==None:
# Performs linear regression
a, b, r, p, err = scipy.stats.linregress(x,y)
# Std. deviation of an individual measurement (Bevington, eq. 6.15)
N=numpy.size(x)
sd=1./(N-2.)* numpy.sum((y-a*x-b)**2); sd=numpy.sqrt(sd)
return sd
@Gabriel-p
Copy link

Is there some indentation missing here perhaps?

  if a==None:   
      # Performs linear regression  
      a, b, r, p, err = scipy.stats.linregress(x,y)  

  # Std. deviation of an individual measurement (Bevington, eq. 6.15)  
  N=numpy.size(x)  
  sd=1./(N-2.)* numpy.sum((y-a*x-b)**2); sd=numpy.sqrt(sd) 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment