title: How to calculate sums of squares in Python?
body: <p>First, is the formula <code>TSS = ESS + RSS</code> always correct? Even for an exponential model? If it is, I just do not understand where am I wrong.</p>
  
  <p>I have 2 arrays of x and y values, where y depends on x. </p>
  
  <pre class="lang-py prettyprint-override"><code>x = np.array([1.5, 2.1, 2.4, 2.7, 3.2, 3.4, 3.6, 3.7, 4.0, 4.5, 5.1, 5.6])
  y = np.array([0.6, 1.2, 1.3, 1.4, 1.45, 1.5, 1.6, 1.8, 1.9, 1.95, 2.1, 2.2])
  </code></pre>
  
  <p>I have a function that determines coefficients a and b and returns an equation of linear regression (or just a and b if needed)</p>
  
  <pre class="lang-py prettyprint-override"><code>def Linear(x, y, getAB = False):
      AVG_X = np.average(x)
      AVG_Y = np.average(y)
      DISP_X = np.var(x)
      DISP_Y = np.var(y)
      STD_X = np.std(x)
      STD_Y = np.std(y)
  
      AVG_prod = np.average(x*y)
      cov = AVG_prod - (AVG_X*AVG_Y)
  
      b = cov/DISP_X
      a = AVG_Y - b*AVG_X
  
      if getAB:
          return a, b
  
      return lambda X: a + b*X
  </code></pre>
  
  <p>I have a function that determines coefficients a and b and returns an equation of EXPONENTIAL regression</p>
  
  <pre class="lang-py prettyprint-override"><code>def Exponential(x, y, getAB = False):
      LOG_Y_array = [math.log(value) for value in y]
  
      A, B = Linear(x, LOG_Y_array, getAB = True)
  
      a = math.exp(A)
      b = math.exp(B)
  
      if getAB:
          return a, b
  
      return lambda X: a * (b**X)
  </code></pre>
  
  <p>I created the array of calculated y values based of exponential model</p>
  
  <pre class="lang-py prettyprint-override"><code>Exponential_Prediction = Exponential(x, y)
  Exponential_Prediction_y = [Exponential_Prediction(value) for value in x]
  </code></pre>
  
  <p>And finally, that is how I calculate TSS, ESS and RSS</p>
  
  <pre class="lang-py prettyprint-override"><code>TSS = np.sum((y - np.average(y))**2)
  ESS_Exp = np.sum((Exponential_Prediction_y - np.average(y))**2)
  RSS_Exp = np.sum((y-Exponential_Prediction_y)**2)
  </code></pre>
  
  <p>That is all pretty clear, except the output of this</p>
  
  <pre class="lang-py prettyprint-override"><code>print(str(TSS) + " = " + str(ESS_Exp) + " + " + str(RSS_Exp))
  </code></pre>
  
  <p>is 2.18166666667 = 2.75523753042 + 0.432362713806</p>
  
  <p>I do not understand how ESS could be more than TSS</p>