-
-
Save zhangz/05c6b16eb31be8934654d36c2e853725 to your computer and use it in GitHub Desktop.
Revisions
-
fauskanger revised this gist
Jul 31, 2013 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,7 +55,9 @@ static void Main(string[] args) { double[] x = new[] { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }; double[] y = new[] { 1.0, 6.0, 17.0, 34.0, 57.0, 86.0, 121.0, 162.0, 209.0, 262.0, 321.0 }; double[] coefficients = Polyfit(x, y, 6); DrawRegression(coefficients, x); } } -
fauskanger revised this gist
Jul 31, 2013 . 1 changed file with 0 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,4 @@ using System; using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double.Factorization; -
fauskanger revised this gist
Jul 31, 2013 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,6 +27,7 @@ public static void DrawRegression(double[] coeffs, double[] xList) } } // From http://rosettacode.org/wiki/Polynomial_regression#C.23 public static double[] Polyfit(double[] x, double[] y, int degree) { // Vandermonde matrix -
fauskanger created this gist
Jul 31, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double.Factorization; namespace PolyRegressionCoefficients { class Program { public static void DrawRegression(double[] coeffs, double[] xList) { double xValue1 = xList[0]; double yValue1 = Polyval(coeffs, xList[0]); double xValue2; double yValue2; for (int i = 1; i < xList.Length; i++) { xValue2 = xList[i]; yValue2 = Polyval(coeffs, xList[i]); MyDrawLibrary.drawLine(xValue1, yValue1, xValue2, yValue2); xValue1 = xValue2; yValue1 = yValue2; } } public static double[] Polyfit(double[] x, double[] y, int degree) { // Vandermonde matrix var v = new DenseMatrix(x.Length, degree + 1); for (int i = 0; i < v.RowCount; i++) for (int j = 0; j <= degree; j++) v[i, j] = Math.Pow(x[i], j); var yv = new DenseVector(y).ToColumnMatrix(); QR qr = v.QR(); // Math.Net doesn't have an "economy" QR, so: // cut R short to square upper triangle, then recompute Q var r = qr.R.SubMatrix(0, degree + 1, 0, degree + 1); var q = v.Multiply(r.Inverse()); var p = r.Inverse().Multiply(q.TransposeThisAndMultiply(yv)); return p.Column(0).ToArray(); } public static double Polyval(double[] coeffs, double xValue) { double val = 0; for (int i = 0; i < coeffs.Length; i++) { val += coeffs[i] * Math.Pow(xValue, i); } return val; } static void Main(string[] args) { double[] x = new[] { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }; double[] y = new[] { 1.0, 6.0, 17.0, 34.0, 57.0, 86.0, 121.0, 162.0, 209.0, 262.0, 321.0 }; DrawRegression(Polyfit(x, y, 6), x); } } }