We need to compute the slope and offset of the best fit line through co2 values collected during baseline measurement.
	m : slope of line through the points
	b: offset
	R: correlation (Pearson’s R)

Ideally, we’d do this iteratively (i.e. w/o saving data points), and avoid overflow/underflow and rounding errors.

We are already using Welford’s method to estimate the mean and sdev (sqrt(VAR(Y))) of co2 values.
The first cut is to extend this to compute estimates of VAR(X) and COV(XY), and use these to produce the regression parameters.

#########
Notation:
S[] : sum
N: number of samples
MX: mean of X : S[x]/N
MY: mean of Y : S[y]/N
MX_prev = previous value 
MY_prev = previous value
SX_prev = previous value
SY_prev = previous value
SXY_prev = previous value

SX: VAR(X) sum      : S[ (x-MX)^2 ]
SY: VAR(Y) sum      : S[ (y-MY)^2 ]
SXY: COV(XY) sum : S[ (x-MX) * (y-MY) ]

SXDEV: stddev(X) : sqrt( SX / (N-1))
SYDEV: stddev(Y) : sqrt( SY / (N-1))

########
Relevant Formulas
R = SXY/sqrt(SX)*sqrt(SY)
   = S[ (x-MX) * (y-MY) ] / ( sqrt( S[ (x-MX)^2 ] ) * sqrt( S[ (y-MY)^2] ) )

m = R * SYDEV/SXDEV
b = MY - m*MX

#########
Computation

Initialize:
MX_prev = 1
MY_prev = current value
SX_prev = 0.0
SY_prev =  0.0
SXY_prev = 0.0

For the nth step:
 y=current CO2 value
 x=n

 // update means, VAR sums
 MY = MY_prev + (y - MY_prev)/n;
 SY = SY_prev + ( y - MY_prev)*(y- MY);

  // the X values (time) are just n
 MX = MX_prev + (x - MX_prev)/n;
 SX = SX_prev + (x - MX_prev)*(x - MX);
 SXY = SXY_prev + (y - MY)*(x - pd->MX);

 // set up for next iteration 
 // [current -> prev]
 MY_prev=MY;
 SY_prev=SY;
         
 MX_prev = MX;
 SX_prev = SX;
 SXY_prev = SXY;

// At the end of the cycle, mean, sdev, m, b, r^2 are given by
 MEAN     MY 
 SDEVY  sqrt( SY / ( n - 1 ) )
 SDEVX  sqrt( SX / ( n - 1 ) ) 
 r^2         ( SXY * SXY ) / ( SY*SX )
 m           r * BLINE_SDEVY / BLINE_SDEVX
 b            MY - BLINE_m * MX

########################
// original web search notes:

https://stats.stackexchange.com/questions/23481/are-there-algorithms-for-computing-running-linear-or-logistic-regression-param
https://www.kellogg.northwestern.edu/faculty/weber/emp/_session_3/correlation.htm
http://onlinestatbook.com/2/regression/intro.html
http://onlinestatbook.com/2/summarizing_distributions/variability.html
http://onlinestatbook.com/2/describing_bivariate_data/calculation.html
https://en.wikipedia.org/wiki/Pearson_correlation_coefficient
https://www.countbayesie.com/blog/2015/2/21/variance-co-variance-and-correlation
https://en.wikipedia.org/wiki/Covariance
https://www.statisticshowto.datasciencecentral.com/probability-and-statistics/correlation-coefficient-formula/

//init(): meanX = 0, meanY = 0, varX = 0, covXY = 0, n = 0
//
//update(x,y):
//n += 1
//dx = x - meanX
//dy = y - meanY
//varX += (((n-1)/n)*dx*dx - varX)/n
//covXY += (((n-1)/n)*dx*dy - covXY)/n
//meanX += dx/n
//meanY += dy/n
//
//getA(): return covXY/varX
//getB(): return meanY - getA()*meanX

// I guessed at a way to compute the standard error in the same pass and am 
// getting numbers that are within 1% of the actual standard error, at least on 
// my sample data sets. I initialize eSum = 0. On each iteration after the first, 
// I compute A, B, e = (A*x - B) - y, and eSum += e*e. The standard error can be 
// computed for any n >= 2 as sqrt(eSum/(n-1)). If you know the sample is 
// incomplete, compute it for any n >= 3 as sqrt(eSum/(n-2)). 
// I'm not doing science and only need an approximation, so this is perfect for me. ?

// From 
//https://www.kellogg.northwestern.edu/faculty/weber/emp/_session_3/correlation.htm

//The covariance of X and Y is the difference between the mean product and the 
// product of the means: Cov(X,Y) = E[XY] - E[X]×E[Y]. In the examples above, 
// the respective covariances are 1.25, 0, and -1.25. Quite generally, positive 
// covariances indicate upward-sloping relationships, and negative covariances 
// indicate downward-sloping relationships.
//
//Covariance is an interesting concept in its own right. A fundamental 
// relationship (frequently encountered in financial analysis) is: 
// Var(X+Y) = Var(X) + Var(Y) + 2×Cov(X,Y). 
// But the units of measurement of covariance are not very natural. For example,
// the covariance of net income and net leisure expenditures is measured in square dollars.
//
//The correlation of X and Y is the normalized covariance: 
// Corr(X,Y) = Cov(X,Y) / ?X?Y .
//
//The correlation of a pair of random variables is a dimensionless number, 
// ranging between +1 and -1. It is +1 only for a perfect upward-sloping 
// relationship (where by ?perfect? we mean that the observations all lie on a 
// single line), and is -1 for a perfect downward-sloping relationship. In the 
// examples above, the correlations are +1, 0, and -1. The more widely-scattered 
// the (X,Y) pairs are about a line, the closer the correlation is to 0. 
// (Notice that the covariance of X with itself is Var(X), and therefore the 
// correlation of X with itself is 1.)
//
// Correlation is a measure of the strength of the linear relationship between 
// two variables. Strength refers to how linear the relationship is, not to the 
// slope of the relationship. Linear means that correlation says nothing about 
// possible nonlinear relationships; in particular, independent random variables
// are uncorrelated (i.e., have correlation 0), but uncorrelated random 
// variables are not necessarily independent, and may be strongly nonlinearly 
// related. Two means that that the correlation shows only the shadows of a 
// multivariate linear relationship among three or more variables (and it is 
// common knowledge that shadows may be severe distortions of reality).

//The Coefficient of Determination
//Consider a ?simple? regression model, i.e., one involving only a single 
// independent variable:
//
//Y = ? + ?X + ? .
//
//In this case, the estimates of the coefficients can be written quite simply:
//
//b = Cov(X,Y) / Var(X) , and  a = Y - bX ,
//
//where X and Y are the sample means of the two variables. (Note that the 
// formula for b is appropriately dimensioned in units of Y per unit of X, and 
// that the formula for a guarantees that the line corresponding to the 
// prediction equation passes through the ?group mean? point (X, Y).)
//
//The (unadjusted) coefficient of determination for the regression is the 
// fraction of the variance in Y which is ?explained? by the regression:
//
//  Var(a+bX) / Var(Y) = b^2 ?X^2 / ?Y^2
//
//  = [Cov(X,Y) /?X^2]^2 ?X^2 / ?Y^2 = [Corr(X,Y)]^2 .
//
//In words: In a simple linear regression, the (unadjusted) coefficient of 
// determination is the square of the correlation between the dependent and 
// independent variables. (Since the symbol ?R? is sometimes used to represent 
// the correlation between two variables, the coefficient of determination is 
// sometimes called the ?R-square? of a regression.) This provides a natural 
// way to interpret a correlation: Square it, and interpret it as the coefficient
// of determination of the regression linking the two variables.
//