Forums > Software > exponential least squares

 Page 1 of 1
Display using: Strange Total Posts: 1557 Joined: Jun 2004
 Posted: 2018-09-15 17:44 Trying to save a few hours of my time here. By an off chance, does anyone have a piece of python or C code for exponential least squares regression? I'll owe you beers and dinners Eher Ende mit Schrecken als Schrecken ohne Ende  kloc Total Posts: 14 Joined: May 2017
 Posted: 2018-09-15 20:06 For python, scipy.optimize.least_squares(...) should be straightforward to use:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.least_squares.html  Rashomon Total Posts: 202 Joined: Mar 2011
 Posted: 2018-09-15 20:51 Strange, looks like you just take logs of your data first and then run an lm.http://mathworld.wolfram.com/LeastSquaresFittingExponential.htmlhttp://www.math.usm.edu/lambers/mat419/lecture13.pdfIf you have functions for matrix transpose and matrix inversion then you can construct a hat matrix by formula rather than calling a lib.Remember that the least squares formula is an algebraic way of getting the geometric projection. So pre-transforming the data by logs can make sense that way.  Strange Total Posts: 1557 Joined: Jun 2004
 Posted: 2018-09-16 04:10 Sorry, miss-wrote (love autocorrect) - i am looking for rolling exponentially weighted least squares. I.e. it's a regular OLS, but the the data being exponentially weighted along the time axis as it's rolling along. Since I want it in the form of y = a + b * x, it's simply b = ewcov(x,y)/var(x), a = ewma(y) - ewma(x) * b etc. I am hoping that someone has a piece of code that does it in a recursive form so it would be quick. Eher Ende mit Schrecken als Schrecken ohne Ende  jr Total Posts: 3 Joined: Apr 2017
 Posted: 2018-09-16 13:37 Sklearn LinearRegression allows to fit with user specified sample weights. It is just batch version though. http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression  Rashomon Total Posts: 202 Joined: Mar 2011
 Posted: 2018-09-16 17:22 rollingin R:data <- cbind( data\$x1, data\$x2, data\$t)data\$x1 / 2 ** -data\$t -> data\$x1data\$x2 / 2 ** -data\$t -> data\$x2lm(data\$x1 ~ data\$x2)Adjust the 2 to something else, or replace –data\$t with end(data\$t) - data\$t or some other howFarBack(data\$t) function.Searching around a bit I found R's dlmhttps://stats.stackexchange.com/questions/9931/exponentially-weighted-moving-linear-regressionhttps://stat.ethz.ch/pipermail/r-sig-finance/2012q1/009313.htmlThey are calling it a “poor man’s Kalman filter” (good name!). Source to dlm ("dynamic linear model") has more than I wrote above.https://www.rdocumentation.org/packages/dlm/versions/1.1-5/topics/dlmFilterhttps://www.rdocumentation.org/packages/dlm/versions/1.1-5/sourceSorry strange, I don’t speak python, but hopefully you can read my code. (including right-assignment -> Page 1 of 1