1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cmath>
using namespace std;
const double SMALL = 1.0E-30; // used to stop divide-by-zero
using vec = vector<double>; // vector
using matrix = vector<vec>; // matrix
// Function prototypes
double poly(const vec& C, double x);
matrix transpose(const matrix& A);
matrix matmul(const matrix& A, const matrix& B);
vec matmul(const matrix& A, const vec& V);
bool solve(const matrix& A, const vec& B, vec& X);
bool polynomialRegression(const vec& X, const vec& Y, int degree, vec& C, double& Rsquared);
//======================================================================
double poly(const vec& C, double x)
{
double result = 0.0;
for (int i = C.size() - 1; i >= 0; i--) result = C[i] + result * x;
return result;
}
//======================================================================
matrix transpose(const matrix& A) // Transpose a matrix
{
int m = A.size(), n = A[0].size();
matrix AT(n, vec(m));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++) AT[i][j] = A[j][i];
}
return AT;
}
//======================================================================
matrix matmul(const matrix& A, const matrix& B) // Matrix times matrix
{
int rowsA = A.size(), colsA = A[0].size();
int rowsB = B.size(), colsB = B[0].size();
assert(colsA == rowsB);
matrix C(rowsA, vec(colsB, 0.0));
for (int i = 0; i < rowsA; i++)
{
for (int j = 0; j < colsB; j++)
{
for (int k = 0; k < colsA; k++) C[i][j] += A[i][k] * B[k][j];
}
}
return C;
}
//======================================================================
vec matmul(const matrix& A, const vec& V) // Matrix times vector
{
int rowsA = A.size(), colsA = A[0].size();
int rowsV = V.size();
assert(colsA == rowsV);
vec C(rowsA, 0.0);
for (int i = 0; i < rowsA; i++)
{
for (int k = 0; k < colsA; k++) C[i] += A[i][k] * V[k];
}
return C;
}
//======================================================================
bool solve(const matrix& A, const vec& B, vec& X)
//--------------------------------------
// Solve AX = B by Cholesky factorisation of A (i.e. A = L.LT)
// Requires A to be SYMMETRIC
//--------------------------------------
{
int n = A.size();
// Cholesky-factorise A
matrix L(n, vec(n, 0));
for (int i = 0; i < n; i++)
{
// Diagonal value
L[i][i] = A[i][i];
for (int j = 0; j < i; j++) L[i][i] -= L[i][j] * L[i][j];
L[i][i] = sqrt(L[i][i]);
if (abs(L[i][i]) < SMALL) return false;
// Rest of the ith column of L
for (int k = i + 1; k < n; k++)
{
L[k][i] = A[k][i];
for (int j = 0; j < i; j++) L[k][i] -= L[i][j] * L[k][j];
L[k][i] /= L[i][i];
}
}
// Solve LY = B, where L is lower-triangular and Y = LT.X
vec Y = B;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++) Y[i] -= L[i][j] * Y[j];
Y[i] /= L[i][i];
}
// Solve UX = Y, where U = LT is upper-triangular
X = Y;
for (int i = n - 1; i >= 0; i--)
{
for (int j = i + 1; j < n; j++) X[i] -= L[j][i] * X[j];
X[i] /= L[i][i];
}
return true;
}
//======================================================================
bool polynomialRegression(const vec& X, const vec& Y, int degree, vec& C, double& Rsquared)
{
int N = X.size(); assert(Y.size() == N);
matrix A(N, vec(1 + degree));
for (int i = 0; i < N; i++)
{
double xp = 1;
for (int j = 0; j <= degree; j++)
{
A[i][j] = xp;
xp *= X[i];
}
}
// Solve the least-squares problem for the polynomial coefficients C
matrix AT = transpose(A);
if (!solve(matmul(AT, A), matmul(AT, Y), C)) return false;
// Calculate R^2
vec AC = matmul(A, C);
double sumYsq = 0, sumACsq = 0, sumY = 0.0;
for (int i = 0; i < N; i++)
{
sumY += Y[i];
sumYsq += Y[i] * Y[i];
sumACsq += AC[i] * AC[i];
}
Rsquared = 1.0 - (sumYsq - sumACsq) / (sumYsq - sumY * sumY / N + SMALL);
return true;
}
//======================================================================
// Interface routine that takes a (pointer to) an array Y, of size N,
// and returns the R^2 parameter for a cubic polynomial fit to the points
// (0,Y0), (1,y1), ..., (N-1,yN-1)
// extern "C" __declspec( dllexport ) double __stdcall PolyFitFunction( double *Y, int N ) // VERY UNSURE ABOUT THIS
double PolyFitFunction(double* Y, int N)
{
vec X(N); for (int i = 0; i < N; i++) X[i] = i;
vec F(Y, Y + N);
vec C;
double Rsquared;
if (polynomialRegression(X, F, 3, C, Rsquared)) return Rsquared;
else return 0.0;
}
//======================================================================
int main()
{
//Test Data - Verified to produce correct answer
vec X = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
vec Y = { 4585.75, 4592.38, 4606.17, 4615.81, 4615.70, 4615.21, 4617.86, 4622.12, 4624.92, 4624.12, 4627.09 };
// Data
// vec X = { 0, 1, 2, 3, 4, 5 };
int N = X.size();
// vec Y(N);
// // Exact cubic polynomial
// for (int i = 0; i < N; i++) Y[i] = 10 + 8 * X[i] + 6 * X[i] * X[i] + 4 * X[i] * X[i] * X[i];
// // Fudge some values or you'll get perfect agreement
// Y[0] -= 10; Y[N - 1] += 20; // Comment this out and you should get the original cubic
vec C;
int degree = 3;
double Rsquared;
if (polynomialRegression(X, Y, degree, C, Rsquared))
{
cout << "Coefficients in C0 + C1.X + C2.X^2 + ... : ";
for (double e : C) cout << e << " ";
cout << "\n\nCheck fit: (Xi, Yi, poly(Xi) )\n";
for (int i = 0; i < X.size(); i++) cout << X[i] << '\t' << Y[i] << '\t' << poly(C, X[i]) << '\n';
cout << "\n\nR^2 = " << Rsquared << '\n';
// Can we get the same answer via an interface function? (Assumes X is { 0, 1, 2, ... } )
cout << "\nR^2 (by interface) = " << PolyFitFunction(Y.data(), N) << '\n';
}
else
{
cerr << "Unable to solve\n";
}
return 0;
}
|