Karim Belabas on Mon, 09 Jun 2025 02:07:24 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: question on method of least squares for sideways parabolas |
Hi Randall, I'm not sure what you're trying to achieve. 1) Here's a general (linear) least square regression, where you expect y ~ sum_i a[i] * F[i](x) for some known functions F[i] and want the best a[i] wrt least square error. least_squares(X, Y, F) = { my(M = matrix(#X,#F, i,j, F[j](X[i]))); matsolve(M~ * M, M~ * Y); } Toy example with some random functions and evaluation points: F = [x->sqrt(x+3), x->log(x+2), x->exp(x)+1]; A = [1.2, 2.3, -3.5]; f(x) = A * vectorv(#F, j, F[j](x)); \\ my "unknown" linear combination X = vectorv(10, i, random(1.)); Y = apply(f, X); least_squares(X, Y, F) \\ requested coefficients %1 = [1.1999999999999999999999999999999999660, 2.3000000000000000000000000000000001834, -3.5000000000000000000000000000000000383]~ Your implementation is basically least_squares(X, Y, [x->1, x->x, x->x^2]) 2) once you have found the (a,b,c) coefficients, I guess you want to predict some y given x ~ ay^2 + by + c ? I don't see what else you can do than solving for y (e.g. using the Formula given by Maxima, which is essentially what polroots is doing). Cheers, K.B. -- Pr. Karim Belabas, U. Bordeaux, Vice-président en charge du Numérique Institut de Mathématiques de Bordeaux UMR 5251 - (+33) 05 40 00 29 77 http://www.math.u-bordeaux.fr/~kbelabas/ * American Citizen [2025-06-09 00:00]: > Hello: > > There are many websites showing how to use the method of least squares for > solving for a parabola and this is standard fare in many college textbooks, > but my problem is a bit different and a casual search on the internet turned > up nothing (for me) > > The standard method has > > M = [ n, sum(xi), sum(xi^2) ; sum(xi), sum(xi^2), sum(xi^3); sum(xi^2), > sum(xi^3), sum(xi^4)] > > B = [ sum(yi), sum(yi xi), sum(yi xi^2) ]~ > > X = matsolve(M,B) > > which solves y = ax^2 + bx + c for the vertical parabola > > My parabola is the sideways positive x-axis parabola: x = ay^2 + by + c > (limited to 1st and 4th quadrants) > > Currently I am solving for a,b,c as i M above, but transposing the xi and yi > values in both M and B and then solving for Y (not X) > > Then having found a,b,c I take real(polroots(ay^2+by+c - x)[2]) where x is > my value to find the corresponding y value for (not using y to find x, but > going backwards x-> y) > > Maxima shows that solving x = ay^2 + by + c and solving for y as > > y = +/- (sqrt(4ax-4ac+b^2)-b)/(2a) but I don't know how to do least squares > on a rational surd expression such as this to obtain a,b,c > > Is my method basically the best way to do this? > > Randall