Skip to content

Instantly share code, notes, and snippets.

@Kaupenjoe
Created November 18, 2015 19:47
Show Gist options
  • Save Kaupenjoe/8c076a0ee6b6c081b273 to your computer and use it in GitHub Desktop.
Save Kaupenjoe/8c076a0ee6b6c081b273 to your computer and use it in GitHub Desktop.
void PolynomRegression::computeAandBandW(void)
{
//Temporary Matrices with Dimension of model complexity + 1
knn::matrix tempAE(mE+1, mE+1);
knn::matrix tempBE(mE+1, 1);
knn::matrix tempWE(mE+1, 1);
std::ofstream logFile;
logFile.open("log.txt",std::ios::out);
for (int k = 0; k <= mE; ++k)
{
for (int m = 0; m <= mE; ++m)
{
//Fills the Matrix A with xp^k+m
for (int p = 1; p < xInE.size(); ++p)
{
//cout << "xInE[" << p << "]: " << xInE[p] << endl;
//cout << "k: " << k << "m: " << m << pow(xInE[p], k + m) << endl;
//k+1 and m+1 as Matrices start at index 1
tempAE.operator()(m+1, k+1) += pow(xInE[p], k + m); //AE(m,k) = xp^k+m
logFile << k+1 << " " << m+1 << " " << tempAE.operator()(k + 1, m + 1) << endl;
logFile << p << " " << xInE[p] << ": " << pow(xInE[p], k + m) << endl;
}
}
for (int p = 1; p < xInE.size(); ++p)
{
tempBE.operator()(k+1, 1) += (tInE[p] * pow((xInE[p]), k));
logFile << k+1 << " " << tempBE.operator()(k + 1, 1) << endl;
}
}
AE.operator=(tempAE);
bE.operator=(tempBE);
//Invert the Matrix
AE.invert();
//A^-1 * b = w | Calculates the Weight Matrix with the Inverted Matrix of A
tempWE.operator=(AE.operator*(bE));
wE.operator=(tempWE);
wE = AE * bE;
logFile.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment