linearen Regression. < Softwaretechnik+Pro < Praktische Inform. < Hochschule < Informatik < Vorhilfe
|
Status: |
(Frage) überfällig | Datum: | 11:17 Do 15.06.2006 | Autor: | ANjaan |
Aufgabe | Erstellen Sie ein Programm in Java zur Berechnung der linearen Regression.
import java.io.*;
import java.util.*;
public class Regression
{
public static void main (String [] args)
{
do
{
doRegression();
System.out.println();
System.out.print("Nochmal? (yes/no) : ");
} while (read().equals("yes"));
}
public static void doRegression()
{
LinkedList<Double> xList = new LinkedList<Double>();
LinkedList<Double> yList = new LinkedList<Double>();
String currentValue = null;
do
{
System.out.print("Bitte nächsten x-Wert eingeben : ");
currentValue = read();
if (currentValue.equals("")) break;
double x = Double.parseDouble(currentValue);
System.out.print("Bitte nächsten y-Wert eingeben : ");
currentValue = read();
if (currentValue.equals("")) break;
double y = Double.parseDouble(currentValue);
xList.add(x);
yList.add(y);
} while (!currentValue.equals(""));
int count = xList.size();
double [] xArray = new double [count];
double [] yArray = new double [count];
int i = 0;
for (Double value : xList)
{
xArray[i] = value;
++i;
}
i = 0;
for (Double value : yList)
{
yArray[i] = value;
++i;
}
double xSum = 0;
double ySum = 0;
for (i = 0 ; i < xArray.length ; ++i)
{
System.out.println ((i+1) + ". x = " + xArray[i] + " y = " + yArray[i]);
xSum += xArray[i];
ySum += yArray[i];
}
double xMean = xSum / (double)xArray.length;
double yMean = ySum / (double)yArray.length;
System.out.println ("x Durchschnitt : " + xMean);
System.out.println ("y Durchschnitt : " + yMean);
double b1 = 0;
double b2 = 0;
for (i = 0 ; i < xArray.length ; ++i)
{
b1 += xArray[i]*yArray[i];
b2 += xArray[i]*xArray[i];
}
b1 -= ((double)xArray.length)*xMean*yMean;
b2 -= ((double)xArray.length)*xMean*xMean;
double b = b1 /b2;
double a = yMean - b*xMean;
System.out.println ("Geradengleichung : " + b + " * x + " + a);
double sb1 = 0;
double sb2 = 0;
double sa1 = 0;
for (i = 0 ; i < xArray.length ; ++i)
{
sb1 += (yArray[i] - b *xArray[i] - a) * (yArray[i] - b *xArray[i] - a);
sb2 += (xArray[i] - xMean) * (xArray[i] - xMean);
sa1 += xArray[i] * xArray[i];
}
double sb = sb1 / (((double)xArray.length - 2) * sb2);
double sa = sb * sa1 / (double)xArray.length;
sb = Math.sqrt(sb);
sa = Math.sqrt(sa);
System.out.println ("Standardabweichung der Steigung : " + sb);
System.out.println ("Standardabweichung des y-Achsen-Abschnittes : " + sa);
}
public static String read()
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
return reader.readLine();
}
catch (IOException e)
{
}
return null;
}
} |
Ich kann meine Fehler nicht finden hier:
LinkedList<Double> xList = new LinkedList<Double>();
und bin stecken bleiben.
Bitte um Hilfe.
Danke im Voraus
|
|
|
|
Status: |
(Mitteilung) Reaktion unnötig | Datum: | 11:20 Do 22.06.2006 | Autor: | matux |
$MATUXTEXT(ueberfaellige_frage)
|
|
|
|