#include "LinearRegression.h"
#include <QString>
#include <QDebug>

LinearRegression::LinearRegression()
{

}

void LinearRegression::calcLineRegParams(int n)
{
    double sumX = 0.0;
    double sumY = 0.0;

    for ( int i = 0; i < n; i++ )
    {
        sumX += xArray[i];
        sumY += yArray[i];
    }

    xAverage = sumX / n;
    yAverage = sumY / n;

    double temp1 = 0.0;
    double temp2 = 0.0;

    for ( int i = 0; i < n; i++ )
    {
        temp1 = xArray[i] - xAverage;
        temp2 = yArray[i] - yAverage;

        slopeUp = slopeUp + temp1 * temp2;
        slopeDown = slopeDown + temp1 * temp1;
    }

//    qDebug() << QString::number(slopeUp, 'f', 20) << "  " << QString::number(slopeDown, 'f', 20);
    slope = slopeUp / slopeDown;
    intercept = yAverage - xAverage * slope;

//    qDebug() << QString::number(slope, 'f', 20) << " == " << QString::number(intercept, 'f', 20);
}
