PureBasic Forumhttps://www.purebasic.fr/english/ Cubic regressionhttps://www.purebasic.fr/english/viewtopic.php?f=7&t=73208 Page 1 of 1

 Author: zxretrosoft [ Wed Jul 17, 2019 6:47 am ] Post subject: Cubic regression Hello friends,I have a question. I need to program a higher regression than quadratic, namely, cubic regression.To do this, we need to know how the coefficients A, B, C, D are calculated.I found coefficients A, B for linear:https://www.easycalculation.com/statistics/learn-regression.phpI found coefficients A, B, C for quadratic:https://www.easycalculation.com/statistics/learn-quadratic-regression.phpBut the coefficients A, B, C, D for cubic regression I can't find anywhere Please, do you have someone tip? How are these coefficients calculated?Thanks in advance!

 Author: STARGÅTE [ Wed Jul 17, 2019 10:24 am ] Post subject: Re: Cubic regression The general case is written here:Polynomial regression - Matrix form and calculation of estimatesI think you can find matrix calculation procedures here in the forum.

 Author: zxretrosoft [ Wed Jul 17, 2019 10:41 am ] Post subject: Re: Cubic regression Yes, thank you.But,it's very complicated for me. I can't deduce it. I'm just looking for 4 equations forA = ...B = ...C = ...D = ...Nothing more, nothing less.Exactly what is here (A=... B=... C=...) for quadratic regression:https://www.easycalculation.com/statistics/learn-quadratic-regression.php

 Author: zxretrosoft [ Wed Jul 17, 2019 12:11 pm ] Post subject: Re: Cubic regression Yes, this is an eventuality.But it is necessary to solve it numerically.x=1 y=5x=2 y=7x=3 y=11x=4 y=20Result (by the calculator):A=0.5B=-2C=4.5D=1.999999999How did the calculator calculate it?There is a formula for ABCD.

 Author: NicTheQuick [ Wed Jul 17, 2019 12:32 pm ] Post subject: Re: Cubic regression zxretrosoft wrote:But it is necessary to solve it numerically.Was this a question? Then no. You only have to solve it numerically if you have less input values than output values.

 Author: zxretrosoft [ Wed Jul 17, 2019 12:40 pm ] Post subject: Re: Cubic regression Of course.The answer is very very simple.The quadratic regression formulas for ABC are:a = { [ Σ x2 y * Σ xx ] - [Σ xy * Σ xx2 ] } / { [ Σ xx * Σ x2x 2] - [Σ xx2 ]2 }b = { [ Σ xy * Σ x2x2 ] - [Σ x2y * Σ xx2 ] } / { [ Σ xx * Σ x2x 2] - [Σ xx2 ]2 }c = [ Σ y / n ] - { b * [ Σ x / n ] } - { a * [ Σ x 2 / n ] }The cubic regression formulas for ABCD are:a = b =c = d =

 Author: STARGÅTE [ Wed Jul 17, 2019 12:44 pm ] Post subject: Re: Cubic regression Have you read the article?Have you seen my code?Of course you can use a very very long formula for A,B,C,D or you can just solve/calculate it with the matrix algorithm. The result of A, B, C and D is in the Array c.d()Your formula for degree 2 or 3 is nothing else like this matrix formula, just rewritten.Quote:How did the calculator calculate it?Which calculator?Btw:Code:x(0) = 1 : y(0) = 5x(1) = 2 : y(1) = 7x(2) = 3 : y(2) = 11x(3) = 4 : y(3) = 20w(0) = 1w(1) = 1w(2) = 1w(3) = 1Num_ApproxPoly (3, 3, x(), y(), w(), c()) Debug "D = "+c(0)Debug "C = "+c(1)Debug "B = "+c(2)Debug "A = "+c(3)Quote:D = 2C = 4.5B = -2A = 0.5

 Author: zxretrosoft [ Wed Jul 17, 2019 12:47 pm ] Post subject: Re: Cubic regression STARGÅTE wrote:Have you read the article?Have you seen my code?Of course you can use a very very long formula for A,B,C,D or you can just solve/calculate it with the matrix algorithm. The result of A, B, C and D is in the Array c.d()Your formula for degree 2 or 3 is nothing else like this matrix formula, just rewritten.Quote:How did the calculator calculate it?Which calculator?Yes, thank you, I'm studying the code It's difficult for me because I can't speak English. It takes me to understand something.In the code, where can I enter those XY values?P.S. Calculator Casio fx9860.

 Author: STARGÅTE [ Wed Jul 17, 2019 1:03 pm ] Post subject: Re: Cubic regression Copy the code (without the DEMO) and add this:Code:; Enter here all points x1,y1 ; x1,y2 ; ... :Define PointList.s = "1,5 ; 2,7 ; 3,11 ; 4,20"Define Points.i = CountString(PointList, ";")Define Dim X.d(Points)Define Dim Y.d(Points)Define Dim W.d(Points)Define Dim C.d(3) ; Degree of polynomDefine IFor I = 0 To Points   X(I) = ValD(StringField(StringField(PointList, I+1, ";"), 1, ","))   Y(I) = ValD(StringField(StringField(PointList, I+1, ";"), 2, ","))   W(I) = 1.0NextIf Num_ApproxPoly(Points, 3, X(), Y(), W(), C()) = 0   Debug "A = "+C(3)   Debug "B = "+C(2)   Debug "C = "+C(1)   Debug "D = "+C(0)Else   Debug "Error"EndIfQuote:A = 0.5B = -2C = 4.5D = 2

 Author: zxretrosoft [ Wed Jul 17, 2019 1:12 pm ] Post subject: Re: Cubic regression Yes it works right! Your code is great and it's the solution I'm looking for. I'll still use the code. Thank you so much STARGÅTE! But I still search for 4 ABCD equations that I can't find. I don't care if they're long, but I'd like to know them.

 Author: NicTheQuick [ Wed Jul 17, 2019 2:52 pm ] Post subject: Re: Cubic regression zxretrosoft wrote:But I still search for 4 ABCD equations that I can't find. I don't care if they're long, but I'd like to know them.I don't think this will help you in any way. Just understand that you better should use matrix calculations with more than 3 values.

 Author: jack [ Wed Jul 17, 2019 4:58 pm ] Post subject: Re: Cubic regression using Maple the symbolic coefficients areCode:A = y1/(x1^3-x1^2*x2-x1^2*x3-x1^2*x4+x1*x2*x3+x1*x2*x4+x1*x3*x4-x2*x3*x4)-y2/(x1*x2^2-x1*x2*x3-x1*x2*x4+x1*x3*x4-x2^3+x2^2*x3+x2^2*x4-x2*x3*x4)+y3/(x1*x2*x3-x1*x2*x4-x1*x3^2+x1*x3*x4-x2*x3^2+x2*x3*x4+x3^3-x3^2*x4)-y4/(x1*x2*x3-x1*x2*x4-x1*x3*x4+x1*x4^2-x2*x3*x4+x2*x4^2+x3*x4^2-x4^3)B = -(x2+x3+x4)*y1/(x1^3-x1^2*x2-x1^2*x3-x1^2*x4+x1*x2*x3+x1*x2*x4+x1*x3*x4-x2*x3*x4)+(x1+x3+x4)*y2/(x1*x2^2-x1*x2*x3-x1*x2*x4+x1*x3*x4-x2^3+x2^2*x3+x2^2*x4-x2*x3*x4)-(x1+x2+x4)*y3/(x1*x2*x3-x1*x2*x4-x1*x3^2+x1*x3*x4-x2*x3^2+x2*x3*x4+x3^3-x3^2*x4)+(x1+x2+x3)*y4/(x1*x2*x3-x1*x2*x4-x1*x3*x4+x1*x4^2-x2*x3*x4+x2*x4^2+x3*x4^2-x4^3)C = (x2*x3+x2*x4+x3*x4)*y1/(x1^3-x1^2*x2-x1^2*x3-x1^2*x4+x1*x2*x3+x1*x2*x4+x1*x3*x4-x2*x3*x4)-(x1*x3+x1*x4+x3*x4)*y2/(x1*x2^2-x1*x2*x3-x1*x2*x4+x1*x3*x4-x2^3+x2^2*x3+x2^2*x4-x2*x3*x4)+(x1*x2+x1*x4+x2*x4)*y3/(x1*x2*x3-x1*x2*x4-x1*x3^2+x1*x3*x4-x2*x3^2+x2*x3*x4+x3^3-x3^2*x4)-(x1*x2+x1*x3+x2*x3)*y4/(x1*x2*x3-x1*x2*x4-x1*x3*x4+x1*x4^2-x2*x3*x4+x2*x4^2+x3*x4^2-x4^3)D = -x2*x3*x4*y1/(x1^3-x1^2*x2-x1^2*x3-x1^2*x4+x1*x2*x3+x1*x2*x4+x1*x3*x4-x2*x3*x4)+x4*x3*x1*y2/(x1*x2^2-x1*x2*x3-x1*x2*x4+x1*x3*x4-x2^3+x2^2*x3+x2^2*x4-x2*x3*x4)-x1*x2*x4*y3/(x1*x2*x3-x1*x2*x4-x1*x3^2+x1*x3*x4-x2*x3^2+x2*x3*x4+x3^3-x3^2*x4)+x1*x2*x3*y4/(x1*x2*x3-x1*x2*x4-x1*x3*x4+x1*x4^2-x2*x3*x4+x2*x4^2+x3*x4^2-x4^3)you could probably rewrite them using sums and therefore making the expressions shorter and more aesthetically pleasing, I leave that up to youone could achieve the same result using the free Maxima CAS https://wxmaxima-developers.github.io/wxmaxima/maxima code followsCode:load ("eigen")\$m:matrix([1,x1,x1^2,x1^3],[1,x2,x2^2,x2^3], [1,x3,x3^2,x3^3], [1,x4,x4^2,x4^3]);y:matrix([y1],[y2], [y3], [y4]);b:invert_by_lu(m).y\$A:ratsimp(b[4][1]);B:ratsimp(b[3][1]);C:ratsimp(b[2][1]);D:ratsimp(b[1][1]);subst([x1=1, x2=2,x3=3,x4=4,y1=5,y2=7,y3=11,y4=20],A);subst([x1=1, x2=2,x3=3,x4=4,y1=5,y2=7,y3=11,y4=20],B);subst([x1=1, x2=2,x3=3,x4=4,y1=5,y2=7,y3=11,y4=20],C);subst([x1=1, x2=2,x3=3,x4=4,y1=5,y2=7,y3=11,y4=20],D);

 Author: zxretrosoft [ Wed Jul 17, 2019 9:23 pm ] Post subject: Re: Cubic regression Yes jack! This is exactly it! But it seems to me that it's just for 4 input values. Can we generalize it anyway? (i.e. for any number of values) ?

 Author: NicTheQuick [ Wed Jul 17, 2019 9:55 pm ] Post subject: Re: Cubic regression The generalization is the matrix calculation STARGÅTE posted.

 Page 1 of 1 All times are UTC + 1 hour Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/