From: Fred Mellender on
See http://www.easycalculation.com/statistics/learn-regression.php

For each point (x,y) you need to form the sums and products indicated. Then
you can get the equation of the line.

"Andreas Bauer" <com(a)news.de> wrote in message
news:ug1Lq9yCLHA.4308(a)TK2MSFTNGP04.phx.gbl...
> Hello Arne,
>>
>> Then it is a very simple linear regression.
>>
>> using System;
>> using System.Collections.Generic;
>>
>> using MathNet.Numerics.LinearAlgebra;
>>
>> namespace E
>> {
>> public class Program
>> {
>> private static void Solve(double[] f, out double[] b, double[] x,
>> double[] y)
>> {
>> Vector one = Vector.Ones(x.Length);
>> Vector x2 = Vector.Create(x);
>> Vector y2 = Vector.Create(y);
>> Matrix xy3 = Matrix.CreateFromColumns(new List<Vector> { x2,
>> y2, one });
>> Vector f2 = Vector.Create(f);
>> Matrix f3 = Matrix.CreateFromColumns(new List<Vector> {
>> f2 });
>> Matrix b3 = xy3.Solve(f3);
>> Vector b2 = b3.GetColumnVector(0);
>> b = b2.CopyToArray();
>> }
>> public static void Main(string[] args)
>> {
>> double[] fx = { 130.816, 196.860, 113.987 };
>> double[] fy = { 266.000, 256.000, 175.000 };
>> double[] x = { 173.0, 275.5, 139.5 };
>> double[] y = { 12.5, 28.5, 153-0 };
>> double[] b;
>> Solve(fx, out b, x, y);
>> Console.WriteLine("fx = " + b[0] + "*x + " + b[1] + "*y + " +
>> b[2]);
>> Solve(fy, out b, x, y);
>> Console.WriteLine("fy = " + b[0] + "*x + " + b[1] + "*y + " +
>> b[2]);
>> Console.ReadKey();
>> }
>> }
>> }
>>
>> outputs:
>>
>> fx = 0,639237208990945*x + 0,0326366299017556*y + 19,8200049707945
>> fy = 0,00341428308423573*x + -0,646872751008385*y + 273,495238414032
>>
>> which looks as the results you have found in Excel.
>
> fine, thank you very much.
>
> Two additional question. I hope is ok.
> What is exactly the theoretical math. background.
> Linear regression? I have a relation from x to y.
> Can you show me, if I must calculate by hand, I need the correct way, the
> correct math. formular.
>
> My problem.
> I have a new task.
> I should find a solution.
> If I know not the correct headline, is not possible to search via
> www.google.com or via www.bing.com
>
> Greeting Andreas
>
>
>
>
>
>
From: Arne Vajhøj on
On 13-06-2010 15:34, Andreas Bauer wrote:
>> Then it is a very simple linear regression.
>>
>> using System;
>> using System.Collections.Generic;
>>
>> using MathNet.Numerics.LinearAlgebra;
>>
>> namespace E
>> {
>> public class Program
>> {
>> private static void Solve(double[] f, out double[] b, double[] x,
>> double[] y)
>> {
>> Vector one = Vector.Ones(x.Length);
>> Vector x2 = Vector.Create(x);
>> Vector y2 = Vector.Create(y);
>> Matrix xy3 = Matrix.CreateFromColumns(new List<Vector> { x2, y2, one });
>> Vector f2 = Vector.Create(f);
>> Matrix f3 = Matrix.CreateFromColumns(new List<Vector> { f2 });
>> Matrix b3 = xy3.Solve(f3);
>> Vector b2 = b3.GetColumnVector(0);
>> b = b2.CopyToArray();
>> }
>> public static void Main(string[] args)
>> {
>> double[] fx = { 130.816, 196.860, 113.987 };
>> double[] fy = { 266.000, 256.000, 175.000 };
>> double[] x = { 173.0, 275.5, 139.5 };
>> double[] y = { 12.5, 28.5, 153-0 };
>> double[] b;
>> Solve(fx, out b, x, y);
>> Console.WriteLine("fx = " + b[0] + "*x + " + b[1] + "*y + " + b[2]);
>> Solve(fy, out b, x, y);
>> Console.WriteLine("fy = " + b[0] + "*x + " + b[1] + "*y + " + b[2]);
>> Console.ReadKey();
>> }
>> }
>> }
>>
>> outputs:
>>
>> fx = 0,639237208990945*x + 0,0326366299017556*y + 19,8200049707945
>> fy = 0,00341428308423573*x + -0,646872751008385*y + 273,495238414032
>>
>> which looks as the results you have found in Excel.
>
> fine, thank you very much.
>
> Two additional question. I hope is ok.
> What is exactly the theoretical math. background.
> Linear regression? I have a relation from x to y.
> Can you show me, if I must calculate by hand, I need the correct way,
> the correct math. formular.

The basic formula for linear regression is:

b = inv(x'x)x'y

Read more here:
http://en.wikipedia.org/wiki/Linear_regression

Note that usually the linear regression is not done
with a straight forward inversion but uses a decomposition.

Arne
From: Andreas Bauer on
Hello Arne,
works well. Thanks a lot. The math. assembly looks well.
The link it is not easy to understand all.
b = inv(x'x)x'y

I hope the last questions.
Why this way? This way is correct, I'm sure.
StepsToAxis<=> UserCoordinatesInMM
fx = a*x + b*y + c
and not the simplest way
fx = a*x + b

How is call in mathematics?
Both 'Linear Regression' ?

How looks the calculation, if y not depend from x.
Can you make an example? Maybe, I hope then is clear for me, to see the
differents.

How is the best way to Round? Which function?
int stepsToAxisXInteger = Convert.ToInt32(stepsToAxisX);
int stepsToAxisXInteger = Convert.ToInt32(stepsToAxisX + 0.5);

I can calculate this way, it is easy
stepsToAxisX = b[0] * userX + b[1] * userY + b[2];
If I want to use the matrix
How is here the best way? Can you make a simple example?
For example, I have 10 new values, I know the factors b[0] b[1] b[2];
10
20
30
40
50
....
100

User in MM ---- Black Box ----- Steps to axis, axis move


Picture, calculate by hand.
http://www1.minpic.de/bild_anzeigen.php?id=114769&key=72372343&ende

Excel sheet
See sheet - Three Variables are not know
http://www.fileuploadx.de/665288

Greeting Andreas



http://www.mathdotnet.com/downloads/Iridium-2008-8-16-470.ashx

fx = 0,639237208990945*x + 0,0326366299017556*y + 19,8200049707945
fy = 0,00341428308423573*x + -0,646872751008385*y + 273,495238414032

fx = a*x + b*y + c

using MathNet.Numerics.LinearAlgebra; // Assembly notwendig.
private void btnCalc_Click(object sender, EventArgs e)
{
double[] fx = { 130.816, 196.860, 113.987 };
double[] fy = { 266.000, 256.000, 175.000 };
double[] x = { 173.0, 275.5, 139.5 };
double[] y = { 12.5, 28.5, 153};

double[] b;
Solve(fx, out b, x, y);
Console.WriteLine("fx = " + b[0] + "*x + " + b[1] + "*y + "
+ b[2]);
// Steps to axis x
double userX = 40.4;
double userY = 90.5;
double stepsToAxisX = b[0] * userX + b[1] * userY + b[2];
userX = 173.0;
userY = 12.5;
stepsToAxisX = b[0] * userX + b[1] * userY + b[2];
int stepsToAxisXInteger = Convert.ToInt32(stepsToAxisX);

Solve(fy, out b, x, y);
Console.WriteLine("fy = " + b[0] + "*x + " + b[1] + "*y + "
+ b[2]);
//Console.ReadKey();


}

private static void Solve(double[] f, out double[] b, double[]
x, double[] y)
{
Vector one = Vector.Ones(x.Length);
Vector x2 = Vector.Create(x);
Vector y2 = Vector.Create(y);
Matrix xy3 = Matrix.CreateFromColumns(new List<Vector> {
x2, y2, one });
Vector f2 = Vector.Create(f);
Matrix f3 = Matrix.CreateFromColumns(new List<Vector> { f2 });
Matrix b3 = xy3.Solve(f3);
Vector b2 = b3.GetColumnVector(0);
b = b2.CopyToArray();
}
From: Arne Vajhøj on
On 14-06-2010 14:43, Andreas Bauer wrote:
> Hello Arne,
> works well. Thanks a lot. The math. assembly looks well.
> The link it is not easy to understand all.
> b = inv(x'x)x'y

> fx = a*x + b*y + c

You need to read what b, x and y are in that formula.

formula y = your fx
formula x = [your x your y all 1's]
formula b = [your a your b your c]

Arne
From: Andreas Bauer on
Hello Arne,
>
> formula y = your fx
> formula x = [your x your y all 1's]
> formula b = [your a your b your c]
>
b is clear.

Can you give me an answer for that?

I hope the last questions.
Why this way? This way is correct, I'm sure.
StepsToAxis<=> UserCoordinatesInMM
fx = a*x + b*y + c
and not the simplest way
fx = a*x + b


How is call in mathematics?
Both 'Linear Regression' ?

How is the best way to Round? Which function?
int stepsToAxisXInteger = Convert.ToInt32(stepsToAxisX);
int stepsToAxisXInteger = Convert.ToInt32(stepsToAxisX + 0.5);

How is the calculation for more values?

3 Values
Matrix (3,3) * M(3,1) = M(3,1)
- - -
10 Values
Matrix (10,3) * M(3,1) = M(10,1)
or?
n Values
Matrix (n,3) * M(3,1) = M(n,1)

For b vector I need 3 (a,b,c)
How I get it, when I have more values.

Greeting Andreas