Mean value by Least Squares Method
08-02-2019, 07:17 PM (This post was last modified: 08-02-2019 09:14 PM by Hans Wurst.)
Post: #1
 Hans Wurst Junior Member Posts: 4 Joined: Aug 2019
Mean value by Least Squares Method
The mean is the average of the numbers or in other words $$\bar{x} = \displaystyle \frac{1}{n}\displaystyle\sum_{k=1}^{n}x_k$$
How may I derive this simple formula using the Least Squares Method on an HP Prime?
I get quite close to it
$$\displaystyle \frac{\partial \displaystyle \sum_{k=1}^{n}(x(k)-m)^2}{\partial m} = sum(-2*(x(k)-m),k,1,n)$$
alas neither FNROOT nor solve are of much help for the last algebraic step. What do I wrong?

Another problem I was not able yet to solve on an HP Prime: How to prove which of the roots of a quadratic equation is the correct one. It's about orthogonal linear regression (sorry for the link to Wikipedia in german, but I could not find similar in English -- but honestly, the formulas are international), the Least Squares Method is applied to find both coefficients, where for m there are two solutions. One may be eliminated by fiddling out the sign of the third second derivative at the roots (or one root at least). I did so using Reduce, but all I tried so far on a Prime failed.

So in both cases it is not about the result, I'd like to know how to get there using a Prime.

TIA
Hans
08-02-2019, 08:14 PM (This post was last modified: 08-02-2019 11:00 PM by DrD.)
Post: #2
 DrD Senior Member Posts: 1,089 Joined: Feb 2014
RE: Mean value by Least Squares Method
Using the math template key, I tried this on the emulator:

∂(Σ((x(k)-m)^2,k,1,n),m) ==>sum(-2*(x(k)-m),k,1,n)

Does this help?

-Dale-
08-02-2019, 08:55 PM
Post: #3
 Hans Wurst Junior Member Posts: 4 Joined: Aug 2019
RE: Mean value by Least Squares Method
(08-02-2019 08:14 PM)DrD Wrote:  Does this help?

No. What you do is not Least Squares Method where the differences are squared first and then added (and this sum minimized). You add the differences and square the sum (and so on).
$$\frac{\partial \displaystyle \sum_{k=1}^{n}(x(k)-m)^2}{\partial m}\neq \frac{\partial \displaystyle \left (\sum_{k=1}^{n}(x(k)-m)\right )^2}{\partial m}$$

The thing is to solve sum(−2∗(x(k)−m),k,1,n) = 0 for m. A simple arithmetic rearrangement:
sum(−2∗(x(k)−m),k,1,n) = 0
−2∗(sum(x(k),k,1,n)+n*m) = 0
m = sum(x(k),k,1,n)/n
alas I do not know how to push a Prime this way.

Best,
H.
08-04-2019, 09:49 AM (This post was last modified: 08-04-2019 10:29 AM by DrD.)
Post: #4
 DrD Senior Member Posts: 1,089 Joined: Feb 2014
RE: Mean value by Least Squares Method
Perhaps a programmable approach might be a reasonable thing to do? Especially, considering the need for capturing sample and observation data. Do you have a real-world application requiring a least square mean? If not, I was browsing the discussion on this website: Least Square mean (Marginal Means)

The author goes about discussing least square mean as, 'the mean of the means.' (The average of the means, or the 'Grand Mean'). Programming his technique is an exercise, for sure, but it is doable. Maybe his content could be useful to inspire some ideas?

-Dale-
08-04-2019, 01:43 PM (This post was last modified: 08-05-2019 08:51 PM by Hans Wurst.)
Post: #5
 Hans Wurst Junior Member Posts: 4 Joined: Aug 2019
Eureka! -- almost, only one hurdle left
(08-04-2019 09:49 AM)DrD Wrote:  Perhaps a programmable approach might be a reasonable thing to do? Especially, considering the need for capturing sample and observation data. Do you have a real-world application requiring a least square mean? [...]

Thank you for your time and the links. My goal is to derive on a HP Prime the formula of the arithmetic average aka mean value by LS method first published by Legendre and claimed by Gauss to be his idea. If you follow the a. m. link you will find "The linear least-squares problem [...] has a closed-form solution." Perhaps it would be reasonable if a programmable approach is based on an analytical solution.

If you prefere a real-world example, try this: what is the average a of x, y, and z? Use CAS of your Prime and enter $$\textbf{simplify}\! \left (\textbf{zero}\! \left ( \displaystyle \frac{\partial (x-a)^2+(y-a)^2+(a-z)^2}{\partial a}, a \right)\right)$$. Result should be [(x+y+z)/3], with greetings from CG

In the mean time I was not lazy and tried this and that (including RTFM) to get $$m = \displaystyle \frac{1}{n}\displaystyle\sum_{k=1}^{n}x_k$$ (the well-known analytical solution) from $$\displaystyle \textbf{zero}\! \left ( \frac{\partial \displaystyle \sum_{k=1}^{n}\left ((x(k)-m)^2\right)}{\partial m}, m \right)$$
If I enter it like this, Prime tells me ["Invalid function x(k) perhaps a missing * ... blah blah"] but I do not know (and could not find an example in the manual) how to attach k as index to x ($$x_k$$) so k is still recognized by sum. Using x_k fails.
But if I enter $$\displaystyle \textbf{simplify}\! \left ( \frac{\partial \displaystyle \sum_{k=1}^{n}((x(k)-m)^2)}{\partial m} \right)$$ the result is sum(2*m-2*x(k),k,1,n). Using this result and manually put it this way (and solve for m): $$\textbf{zero}\! \left ( \displaystyle 2*\sum_{k=1}^{n}(m) - 2*\sum_{k=1}^{n}(x(k)), m \right)$$ Prime shows [sum(x(k),k,1,n)/n] what is exactly what I was looking for.

Now, could someone please tell me how to do it without this silly manual intermediate step?

/H.

Edit: No hint to a useful solution so far? I am surprised that a tool brimful with all kind of math, with students as intended users, may not derive easily such simple things like the arithmetic average.

Bye-bye, I'll waste my time elsewhere.
08-08-2019, 01:32 PM (This post was last modified: 08-08-2019 01:33 PM by Eddie W. Shore.)
Post: #6
 Eddie W. Shore Senior Member Posts: 948 Joined: Dec 2013
RE: Mean value by Least Squares Method
Well good bye then.
08-08-2019, 02:05 PM
Post: #7
 DrD Senior Member Posts: 1,089 Joined: Feb 2014
RE: Mean value by Least Squares Method
Quote:Edit: No hint to a useful solution so far? I am surprised that a tool brimful with all kind of math, with students as intended users, may not derive easily such simple things like the arithmetic average.

Bye-bye, I'll waste my time elsewhere.

The arithmetic average has the built in command mean()

It was not completely clear, (to me), if you were looking for an algorithm to solve a problem involving the least squares method, or were looking for information on how the least squares method works, or wanted to understand better the theory behind the least squares method, or how to program the least squares method using the hp prime. Sometimes this turns out to be a native language translation, that I have misunderstood, so I just let the topic evolve.

If you want information on the least squares method, perhaps this site might be useful?
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)