Funktion für die Lineare Regression...
/// <summary>
/// Lineare Regression</param>
///
/// </summary>
/// <param name="x-Werte"></param>
/// <param name="y-Werte"></param>
/// <returns>m,n</returns>
public static List<double> LineareRegression(List<double> xWerte, List<double> yWerte)
{
double xAvg = 0;
double yAvg = 0;
foreach (double _xWerte in xWerte)
{
xAvg += _xWerte;
}
foreach (double _yWerte in yWerte)
{
yAvg += _yWerte;
}
xAvg = xAvg / xWerte.Count();
yAvg = yAvg / yWerte.Count();
double v1 = 0;
double v2 = 0;
for (int i = 0; i < xWerte.Count(); i++)
{
v1 += (xWerte[i] - xAvg) * (yWerte[i] - yAvg);
v2 += Math.Pow(xWerte[i] - xAvg, 2);
}
//Formel y = m/x + n
double m = v1 / v2;
double n = yAvg - m * xAvg;
List<double> mn = new List<double>();
mn.Add(m);
mn.Add(n);
return mn;
}
1 Kommentare zum Snippet