views:

878

answers:

3

We are using a programming language that does not have a linear regression function in it. We have already implemented a single variable linear equation:

y = Ax + B

and have simply calculated the A and B coefficents from the data using a solution similar to this Stack Overflow answer.

I know this problem gets geometrically harder as variables are added, but for our purposes, we only need to add one more:

z = Ax + By + C

Does anyone have the closed form equations, or code in any language that can solve for A, B and C given an array of x, y, and z's?

+1  A: 

Yes, it's an easy linear algebra problem if you think of it the way Gil Strang does it. Here's a written explanation.

duffymo
A: 

Can you use MatLab or does the calculation have to occur inside your software?

MatLab instructions on multiple regression analysis.

Integrating MatLab with C#.

DavGarcia
No. It's got to be inside a third-party language we are using, which has no hooks to the outside world.
lkessler
+5  A: 

so you have three linear equations

k = aX1 + bY1 + cZ1
k = aX2 + bY2 + cZ2
k = aX3 + bY3 + cZ3

What you can do is rewrite it as matriz

| x1 y1 z1 | | a |   | k |
| x2 y2 z2 | | b | = | k |
| x3 y3 y3 | | c |   | k |

to work out [a b c ] do the following matrix operation

| a |              | x1 y1 z1 |     | k |
| b | =  inverse(  | x2 y2 z2 | )   | k |
| c |              | x3 y3 y3 |     | k |

The formula for a 3x3 matrix inverse can be found here

hhafez
Thank you. That's exactly what I needed.
lkessler
If you have more than three points, this method won't work. The least squares solution minimizes the error for more than three points.
duffymo
No duffymo. This is correct and works to solve a 2 variable linear regression. See the example at: http://en.wikipedia.org/wiki/Linear_regression where the X matrix is 15 rows by 3 columns. It gets multiplied by its transpose and thus creates a 3x3 matrix that needs to be inverted
lkessler
I do realize that hhafez did answer this to solve simultaneous linear equations rather than my linear regression problem. But, none-the-less, it did lead me to the 3x3 matrix inverse equations, which is what I really needed.
lkessler