Statistical - Cubic Regression (Cubic Spline Fit) ?
|
05-31-2022, 08:24 AM
Post: #1
|
|||
|
|||
Statistical - Cubic Regression (Cubic Spline Fit) ?
When using desktop CAS/statistical packages, I often find that a cubic spline regression provides the best fit, for the data I'm analysing. The TI Nspire includes cubic regression as standard as do Casio calculators such as the CG50 (and its ancestors), and I wondered if there are any great examples of cubic regression on HP calculators.
For smaller data sets, I tend to use the Curve Fitting routines included in SandMath on my DM41x these days (originally found on the venerable AECROM), as this includes a best-fit routine but cubic regression\spline\curve isn't one of the options. And any of the good routines I've encountered on RPL calculators 48/50 are emulations of the AECROM routines and the story is much the same on the Prime. I'm sure something exists somewhere in the HP ecosystem. And it may be labelled polynomial regression as this is ultimately the expression describing the spline (be it Quadratic/Cubic/Quartic etc.). But I've not found it on my travels to date. Any suggestions will be most gratefully received. |
|||
05-31-2022, 11:36 AM
Post: #2
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
I suggest you take a look at hpcalc.
For the hp49/50 https://www.hpcalc.org/hp49/math/numeric/intpol23.zip there is also a version for the hp48 br Gjermund |
|||
05-31-2022, 12:07 PM
Post: #3
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
@Gjermund | Thanks for the recommendation. It's not exactly what I'm looking for, but it's something which will come in useful nonetheless.
I'm ideally looking for something that hooks into the statistical summation data set and then fits a cubic spline to the data points much as would happen with the existing regression options. The programs you link to can be used to achieve similar outcomes but there's a bit more work involved. The thing that I'd ideally like to find is something that functions much like the old 41 AECROM curve fitting routines but with a cubic interpolation fit too. The Best Fit option of the AECROM routines is ideal as it samples the data against all the regression options and picks the best for the dataset (saving you the need to first output a scatter plot before manually picking a suitable regression option). It's unfortunate that its regression options doesn't include cubic, as the AECROM curve fitting routines rightly deserve their legendary status in the 41 community. |
|||
05-31-2022, 12:45 PM
Post: #4
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
Hi Jonmore,
for the HP Prime you have the possibility to compute a cubic regression in the 2-variable statistics app. It is one of a total of 12 models you can select from to fit your data. See the manual for details. |
|||
05-31-2022, 12:49 PM
Post: #5
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
Did you try the Curve Fitting Module V2 by Ángel?
Among the rest it does include a "CSPLINE" (Cubic Spline Intpl.) program. Available @ the usual site (TOS) Greetings, Massimo -+×÷ ↔ left is right and right is wrong |
|||
05-31-2022, 01:57 PM
Post: #6
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
Splendid. The last two answers are exactly what I've been looking for.
Thanks @Massimo and @rawi. And funnily enough, I already have Angel's Curve Fitting module on my DM41x, I simply forgot I'd put it there! As to the Prime option, 'that'll learn me' for always turning to my 50g for non-CAS stuff such as statistical calculations. |
|||
05-31-2022, 05:33 PM
(This post was last modified: 05-31-2022 05:53 PM by Thomas Klemm.)
Post: #7
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
Derivation
With the Vandermonde matrix \(X\): \( X = \begin{bmatrix} 1&x_{1}&x_{1}^{2}&x_{1}^{3} \\ 1&x_{2}&x_{2}^{2}&x_{2}^{3} \\ 1&x_{3}&x_{3}^{2}&x_{3}^{3} \\ \vdots &\vdots &\vdots &\vdots \\ 1&x_{n}&x_{n}^{2}&x_{n}^{3} \\ \end{bmatrix} \) … and the corresponding \(y\) vector: \( y = \begin{bmatrix} y_{1} \\ y_{2} \\ y_{3} \\ \vdots\\ y_{n} \\ \end{bmatrix} \) … we can write the equation for the coefficients \(c\) as: \( X^\top X c = X^\top y \) This leads to: \( \begin{bmatrix} n & \sum x & \sum x^2 & \sum x^3 \\ \sum x & \sum x^2 & \sum x^3 & \sum x^4 \\ \sum x^2 & \sum x^3 & \sum x^4 & \sum x^5 \\ \sum x^3 & \sum x^4 & \sum x^5 & \sum x^6 \\ \end{bmatrix} \, \begin{bmatrix} c_{0} \\ c_{1} \\ c_{2} \\ c_{3} \\ \end{bmatrix} = \begin{bmatrix} \sum y \\ \sum x y \\ \sum x^2 y \\ \sum x^3 y \\ \end{bmatrix} \) Since the matrix \(A = X^\top X \) is positive semidefinite, we can find a Cholesky decomposition: \( A = L \, L^\top \) Here the matrix \(L\) is a real lower triangular matrix with positive diagonal entries. I dully followed the steps in the linked Lecture 7 Notes to calculate \(L\) and then used forward and back substitution to solve: \( L \, L^\top c = X^\top y \) Programs CLΣ: Clear stack and registers and set ΣREG to 20. Σ+ Add a data point \((x, y)\). Σ- Remove a data point \((x, y)\). CFIT Calculate the coefficients of the cubic polynomial. P(X) Evaluate the cubic polynomial at \(x\). This program is for the HP-42s, but can also be used with the HP-41C: Code: 00 { 298-Byte Prgm } Registers Matrix Registers: 00: \(t \to c_0\) 01: \(u \to c_1\) 02: \(v \to c_2\) 03: \(w \to c_3\) 04: \(n\) 05: \(a\) 06: \(b\) 07: \(c\) 08: \(b\) 09: \(c\) 10: \(d\) 11: \(d\) 12: \(e\) 13: \(f\) Statistics Registers: 14: \(Σ x^3 \to c\) 15: \(Σ x^4 \to d\) 16: \(Σ x^5 \to e\) 17: \(Σ x^6 \to f\) 18: \(Σ x^2 y \to v\) 19: \(Σ x^3 y \to w\) 20: \(Σ x \to a\) 21: \(Σ x^2 \to b\) 22: \(Σ y \to t\) 23: \(Σ y^2\) 24: \(Σ x y \to u\) 25: \(n \to n\) Correspondence to the matrix: \( \begin{bmatrix} n & a & b & c & \, & t \\ & b & c & d & \, & u \\ & & d & e & \, & v \\ & & & f & \, & w \\ \end{bmatrix} \begin{bmatrix} 04 & 05 & 06 & 07 & \, & 00 \\ & 08 & 09 & 10 & \, & 01 \\ & & 11 & 12 & \, & 02 \\ & & & 13 & \, & 03 \\ \end{bmatrix} \) Example Let us find the cubic regression function for the following dataset: (0, 1), (2, 0), (3, 3), (4, 5), (5, 4). CLΣ 1 ENTER 0 Σ+ 0 ENTER 2 Σ+ 3 ENTER Σ+ 5 ENTER 4 Σ+ 4 ENTER 5 Σ+ CFIT T: -0.3868 Z: 3.0687 Y: -5.0755 X: 0.9973 Therefore, the cubic regression function that best fits our data is: \( y = 0.9973 - 5.0755x + 3.0687x^2 - 0.3868x^3 \) Let us estimate the value for \(x = 1\): 1 P(X) -1.3962 References |
|||
05-31-2022, 06:09 PM
Post: #8
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
Thanks for the math lesson, Thomas. The math wasn't the problem, it's simply that I like to be a lazy bugger at times, and if there's a unicorn solution available somewhere my lazy bones would rather achieve my goal with the least amount of key presses possible.
Having said all that, I'll definitely have a dabble with your program on both the 42 and 41. Always good to have options. |
|||
05-31-2022, 06:36 PM
Post: #9
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
Quote:achieve my goal with the least amount of key presses possible You can do this by assigning the programs to dedicated keys on the HP-41C, or then creating a CUSTOM menu on the HP-42S. Thank you for raising this question and for letting me learn something new today. Now I might go a little deeper into the matter: Quote:An alternative way to eliminate taking square roots in the \( \mathbf {LL} ^{\mathrm {*}} \) decomposition is to compute the Cholesky decomposition \( \mathbf {A} =\mathbf {LDL} ^{\mathrm {*}} \), then solving \( \mathbf {Ly} =\mathbf {b} \) for y, and finally solving \( \mathbf {DL} ^{\mathrm {*}}\mathbf {x} =\mathbf {y} \). |
|||
05-31-2022, 08:51 PM
Post: #10
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
I implemented cubic fit routines for the HP Prime’s Function app’s “sketch a function” feature, but those routines are only available via the “sketch a function” interface.
Ideally, numerical round-off should be taken into account when implementing these sorts of routines. As it was practical for this problem, I used an arithmetic with enough precision to allow for the calculations to be performed without any numerical round-off. (The input to these cubic fit routines are points with bounded integer coordinates — points from touch input. This limits the general applicability of the “sketch a function” cubic fit routines in the Prime.) |
|||
06-01-2022, 02:31 AM
Post: #11
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
I have an HP User Library program for the HP-71B that I bought from the Library back in the 80's when I was enamored with cubic splines. If that's of interest, I can scan it and post a copy, just let me know. Same topic but sounds like it's not the same context that you're interested in.
--Bob Prosperi |
|||
06-01-2022, 06:26 AM
Post: #12
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
I have the impression cubic regression (what Thomas described, and very well indeed) and cubic spline regression are mixed up.
Cubic spline regression typically takes 4 or 5 (depending on the number of sample points) splines, and determines the best fit for those splines (with of course endpoints and first and second derivatives equal). That is somewhat more involved than cubic regression, fitting a 3rd degree polynomial to statistical data, which would be the same as using a single spline. Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
06-01-2022, 07:09 AM
Post: #13
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
This is a bit off topic as there is no regression involved,
https://people.maths.ox.ac.uk/trefethen/barycentric.pdf but Barycentric Lagrange interpolation is a numerically stable interpolation method which fits to all points of (large) datasets and can be used for interpolation and also for derivatives. https://www.hpcalc.org/hp49/math/numeric/chebhp50.zip included in hpgcc3 for the hp50g only. br Gjermund |
|||
06-01-2022, 07:32 AM
Post: #14
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
(06-01-2022 02:31 AM)rprosperi Wrote: I have an HP User Library program for the HP-71B that I bought from the Library back in the 80's when I was enamored with cubic splines. If that's of interest, I can scan it and post a copy, just let me know. Same topic but sounds like it's not the same context that you're interested in. Thanks Bob. I don't have a HP-71B but I do have a Casio fx-880p. And it might be fun to attempt to convert the 71B program. Failing that, I have in the past managed to convert 71B programs to both my 28S and DM41x so it might make for a good way to while away a rainy Sunday afternoon. I only recently got back into old school Basic handhelds and prices on 71B's are crazy high for us Brits these days - import duties and 20% VAT are the final nails in the coffin of that particular eBay transaction! |
|||
06-01-2022, 08:35 AM
(This post was last modified: 06-01-2022 08:35 AM by jonmoore.)
Post: #15
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
(06-01-2022 07:09 AM)Gjermund Skailand Wrote: This is a bit off topic as there is no regression involved, Thanks for the info Gjermund. This thread is turning into a great resource of the differing options. |
|||
06-01-2022, 03:20 PM
Post: #16
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
(06-01-2022 06:26 AM)Werner Wrote: I have the impression cubic regression and cubic spline regression are mixed up. We can use the same dataset as in the Cubic Regression Calculator: (0, 1), (2, 0), (3, 3), (4, 5), (5, 4) Code: 0 1 Cubic spline interpolation Equation \( f(x) = \begin{cases}3.1105 \cdot 10^{-1}\cdot x^3 -4.8828 \cdot 10^{-61}\cdot x^2 -1.7442\cdot x + 1.0000, & \text{if } x \in [0,2], \\-8.5465 \cdot 10^{-1}\cdot x^3 + 6.9942\cdot x^2 -1.5733 \cdot 10^{1}\cdot x + 1.0326 \cdot 10^{1}, & \text{if } x \in (2,3], \\-4.5930 \cdot 10^{-1}\cdot x^3 + 3.4360\cdot x^2 -5.0581\cdot x -3.4884 \cdot 10^{-1}, & \text{if } x \in (3,4], \\6.9186 \cdot 10^{-1}\cdot x^3 -1.0378 \cdot 10^{1}\cdot x^2 + 5.0198 \cdot 10^{1}\cdot x -7.4023 \cdot 10^{1}, & \text{if } x \in (4,5].\end{cases} \) We get piecewise defined cubic polynomials that fit all given points. Evaluation \( f(1)=-0.43314 \) Graph Usage Quote:Originally, spline was a term for elastic rulers that were bent to pass through a number of predefined points, or knots. Polynomial interpolation Equation \( f(x)=1.6667 \cdot 10^{-2}\cdot x^{4}-5.6667 \cdot 10^{-1}\cdot x^{3}+ 3.6833\cdot x^{2}-5.7333\cdot x+ 1.0000 \) We get a single polynomial that fits all given points. Evaluation \( f(1)=-1.6000 \) Graph Usage Quote:In numerical analysis, polynomial interpolation is the interpolation of a given data set by the polynomial of lowest possible degree that passes through the points of the dataset. Conclusion Use a cubic spline if you want a function that goes smoothly through the given data points. Disadvantage: The function is defined piecewise. Use polynomial interpolation if you want a single polynomial passing the given data points. Disadvantage: Runge's phenomenon shows that for high values of n, the interpolation polynomial may oscillate wildly between the data points. Use cubic regression when you want to get the cubic polynomial that best fits the given data points. Disadvantage: The polynomial usually does not pass the given data points. |
|||
06-01-2022, 03:50 PM
Post: #17
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
It really depends on the data sets you're working with. I often work with data that broadly speaking has an S-shaped characteristic (with outliers, but that needs a different treatment anyway) and cubic regression works very well with data of this nature.
But as ever with this type of scenario, YMMD. Thanks for everybody's input so far. |
|||
06-01-2022, 04:56 PM
(This post was last modified: 06-01-2022 04:58 PM by jonmoore.)
Post: #18
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
As an aside, ref my use of the word spline.
I work a lot with 3d DCC packages (digital content creation packages such as Maya and Houdini) and CAD packages too (such as Rhino, which incidentally, started out life as a specialist tool for designing boat hulls), and splines are central to their workflows. The beauty of working with cubic spines in particular is that they lead to wonderfully smooth curves. Bezier curves are another DCC curve type, but these are more typical of 2d creative packages such as Adobe's illustrator. They provide far more control but the nature of how they're defined make them less useful for statistical purposes. The trick with splines in general is to use as few control points as possible. Using too many control points, especially if some control points are too closely spaced, can lead to unpredictable kinks. These kinks have a greater likelihood with cubic splines as they're not designed for rapid changes of direction. Working with splines as design tools gives you a great intuition for how they function when applied to statistical data. |
|||
06-02-2022, 06:01 AM
Post: #19
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
(05-31-2022 08:24 AM)jonmoore Wrote: (..) I often find that a cubic spline regression provides the best fit, for the data I'm analysing.You left out the one thing jonmoore was talking about, Thomas; cubic spline regression, fitting data to 3-4-5 cubic splines, depending on the number of data points (potentially hundreds, thousands). Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE |
|||
06-02-2022, 06:23 AM
Post: #20
|
|||
|
|||
RE: Statistical - Cubic Regression (Cubic Spline Fit) ?
(06-02-2022 06:01 AM)Werner Wrote:(05-31-2022 08:24 AM)jonmoore Wrote: (..) I often find that a cubic spline regression provides the best fit, for the data I'm analysing.You left out the one thing jonmoore was talking about, Thomas; cubic spline regression, fitting data to 3-4-5 cubic splines, depending on the number of data points (potentially hundreds, thousands). A modern statistical analysis program delivers this functionality by choosing the appropriate control points from those available in the scatter plot so that the interpolated curve best describes the average of the data points. Here's another reference source, in this case, a statistical analysis library for .NET. https://www.extremeoptimization.com/Docu...lines.aspx |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 10 Guest(s)