public void QrDecompositionConstructorTest2() { double[][] value = { new double[] { 1 }, new double[] { 2 }, new double[] { 3 } }; var target = new JaggedQrDecomposition(value); // Decomposition Identity var Q = target.OrthogonalFactor; var QQt = Matrix.Dot(Q.Transpose(), Q); Assert.IsTrue(Matrix.IsEqual(QQt, Jagged.Identity(1), 1e-6)); double[][] reverse = target.Reverse(); Assert.IsTrue(Matrix.IsEqual(value, reverse, 1e-6)); // Linear system solving double[][] B = Matrix.ColumnVector(new double[] { 4, 5, 6 }).ToJagged(); double[][] expected = Jagged.ColumnVector(new double[] { 2.285714285714286 }); double[][] actual = target.Solve(B); Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0000000000001)); double[][] R = target.UpperTriangularFactor; actual = value.Dot(R.Inverse()); Assert.IsTrue(Matrix.IsEqual(Q, actual, 0.0000000000001)); }
public void weights_test_tree_2() { KNearestNeighbors a; KNearestNeighbors b; { double[][] inputs = Jagged.ColumnVector(4.2, 0.7, 0.7, 0.7, 1.3, 9.4, 9.4, 12); int[] outputs = { 0, 0, 0, 1, 1, 2, 2, 2 }; var knn = new KNearestNeighbors(k: inputs.Length); a = knn.Learn(inputs, outputs); } { double[][] inputs = Jagged.ColumnVector(4.2, 0.7, 0.7, 1.3, 9.4, 12); int[] outputs = { 0, 0, 1, 1, 2, 2 }; double[] weights = { 1, 2, 1, 1, 2, 1 }; var knn = new KNearestNeighbors(k: inputs.Length); b = knn.Learn(inputs, outputs, weights); } { double[] x = { 9.4 }; double[] expected = a.Scores(x); double[] actual = b.Scores(x); Assert.IsTrue(expected.IsEqual(actual, 1e-4)); } { double[][] x = Jagged.ColumnVector(4.2, 0.7, 1.3, 9.4, 12); double[][] expected = a.Scores(x); double[][] actual = b.Scores(x); Assert.IsTrue(expected.IsEqual(actual, 1e-4)); } }
/// <summary> /// Learns a model that can map the given inputs to the given outputs. /// </summary> /// <param name="x">The model inputs.</param> /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param> /// <param name="weights">The weight of importance for each input-output pair (if supported by the learning algorithm).</param> /// <returns> /// A model that has learned how to produce <paramref name="y" /> given <paramref name="x" />. /// </returns> public SimpleLinearRegression Learn(double[] x, double[] y, double[] weights = null) { double[][] X = Jagged.ColumnVector(x); if (UseIntercept) { X = X.InsertColumn(value: 1); } if (weights != null) { double[] sqrtW = weights.Sqrt(); X = Elementwise.Multiply(X, sqrtW, dimension: 1, result: X); y = Elementwise.Multiply(y, sqrtW); } decomposition = X.Decompose(leastSquares: IsRobust); double[] coefficients = decomposition.Solve(y); if (UseIntercept) { return(new SimpleLinearRegression() { Slope = coefficients[0], Intercept = coefficients[1] }); } else { return(new SimpleLinearRegression() { Slope = coefficients[0], }); } }
/// <summary> /// Learns a model that can map the given inputs to the given outputs. /// </summary> /// <param name="x">The model inputs.</param> /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param> /// <param name="weights">The weight of importance for each input-output pair.</param> /// <returns> /// A model that has learned how to produce <paramref name="y" /> given <paramref name="x" />. /// </returns> public SimpleLinearRegression Learn(double[] x, double[] y, double[] weights = null) { double[][] X = Jagged.ColumnVector(x); if (UseIntercept) { X = X.InsertColumn(value: 1); } decomposition = X.Decompose(leastSquares: IsRobust); double[] coefficients = decomposition.Solve(y); if (UseIntercept) { return(new SimpleLinearRegression() { Slope = coefficients[0], Intercept = coefficients[1] }); } else { return(new SimpleLinearRegression() { Slope = coefficients[0], }); } }
public void full_decomposition() { double[][] value = { new double[] { 1 }, new double[] { 2 }, new double[] { 3 } }; var target = new JaggedQrDecomposition(value, economy: false); // Decomposition Identity var Q = target.OrthogonalFactor; var QtQ = Matrix.Dot(Q.Transpose(), Q); Assert.IsTrue(Matrix.IsEqual(QtQ, Jagged.Identity(3), 1e-6)); double[][] reverse = target.Reverse(); Assert.IsTrue(Matrix.IsEqual(value, reverse, 1e-6)); // Linear system solving double[][] B = Matrix.ColumnVector(new double[] { 4, 5, 6 }).ToJagged(); double[][] expected = Jagged.ColumnVector(new double[] { 2.285714285714286 }); double[][] actual = target.Solve(B); Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.0000000000001)); var QQt = Matrix.Dot(Q, Q.Transpose()); Assert.IsTrue(Matrix.IsEqual(QQt, Jagged.Identity(3), 1e-6)); }
/// <summary> /// Learns a model that can map the given inputs to the given outputs. /// </summary> /// <param name="x">The model inputs.</param> /// <param name="y">The desired outputs associated with each <paramref name="x">inputs</paramref>.</param> /// <param name="weights">The weight of importance for each input-output pair (if supported by the learning algorithm).</param> /// <returns> /// A model that has learned how to produce <paramref name="y" /> given <paramref name="x" />. /// </returns> public SimpleLinearRegression Learn(double[] x, double[] y, double[] weights = null) { if (weights != null) { throw new ArgumentException(Accord.Properties.Resources.NotSupportedWeights, "weights"); } double[][] X = Jagged.ColumnVector(x); if (UseIntercept) { X = X.InsertColumn(value: 1); } decomposition = X.Decompose(leastSquares: IsRobust); double[] coefficients = decomposition.Solve(y); if (UseIntercept) { return(new SimpleLinearRegression() { Slope = coefficients[0], Intercept = coefficients[1] }); } else { return(new SimpleLinearRegression() { Slope = coefficients[0], }); } }
public void weight_test() { MultivariateLinearRegression reference; double[] referenceR2; { double[][] data = { new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // new[] { 1.0, 10.7, 2.4 }, // 5 times weight 1 new[] { 1.0, 12.5, 3.6 }, new[] { 1.0, 43.2, 7.6 }, new[] { 1.0, 10.2, 1.1 }, }; double[][] x = Jagged.ColumnVector(data.GetColumn(1)); double[][] y = Jagged.ColumnVector(data.GetColumn(2)); var ols = new OrdinaryLeastSquares(); reference = ols.Learn(x, y); referenceR2 = reference.CoefficientOfDetermination(x, y); } MultivariateLinearRegression target; double[] targetR2; { double[][] data = { new[] { 5.0, 10.7, 2.4 }, // 1 times weight 5 new[] { 1.0, 12.5, 3.6 }, new[] { 1.0, 43.2, 7.6 }, new[] { 1.0, 10.2, 1.1 }, }; double[] weights = data.GetColumn(0); double[][] x = Jagged.ColumnVector(data.GetColumn(1)); double[][] y = Jagged.ColumnVector(data.GetColumn(2)); OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); target = ols.Learn(x, y, weights); targetR2 = target.CoefficientOfDetermination(x, y, weights: weights); } Assert.IsTrue(reference.Weights.IsEqual(target.Weights)); Assert.IsTrue(reference.Intercepts.IsEqual(target.Intercepts, 1e-8)); Assert.AreEqual(0.16387475666214069, target.Weights[0][0], 1e-6); Assert.AreEqual(0.59166925681755056, target.Intercepts[0], 1e-6); Assert.AreEqual(referenceR2[0], targetR2[0], 1e-8); Assert.AreEqual(0.91476129548901486, targetR2[0], 1e-10); }
public void minimize_test() { #region doc_minimize // Example from https://en.wikipedia.org/wiki/Gauss%E2%80%93Newton_algorithm // In this example, the Gauss–Newton algorithm will be used to fit a model to // some data by minimizing the sum of squares of errors between the data and // model's predictions. // In a biology experiment studying the relation between substrate concentration [S] // and reaction rate in an enzyme-mediated reaction, the data in the following table // were obtained: double[][] inputs = Jagged.ColumnVector(new [] { 0.03, 0.1947, 0.425, 0.626, 1.253, 2.500, 3.740 }); double[] outputs = new[] { 0.05, 0.127, 0.094, 0.2122, 0.2729, 0.2665, 0.3317 }; // It is desired to find a curve (model function) of the form // // rate = \frac{V_{max}[S]}{K_M+[S]} // // that fits best the data in the least squares sense, with the parameters V_max // and K_M to be determined. Let's start by writing model equation below: LeastSquaresFunction function = (double[] parameters, double[] input) => { return((parameters[0] * input[0]) / (parameters[1] + input[0])); }; // Now, we can either write the gradient function of the model by hand or let // the model compute it automatically using Newton's finite differences method: LeastSquaresGradientFunction gradient = (double[] parameters, double[] input, double[] result) => { result[0] = -((-input[0]) / (parameters[1] + input[0])); result[1] = -((parameters[0] * input[0]) / Math.Pow(parameters[1] + input[0], 2)); }; // Create a new Gauss-Newton algorithm var gn = new GaussNewton(parameters: 2) { Function = function, Gradient = gradient, Solution = new[] { 0.9, 0.2 } // starting from b1 = 0.9 and b2 = 0.2 }; // Find the minimum value: gn.Minimize(inputs, outputs); // The solution will be at: double b1 = gn.Solution[0]; // will be 0.362 double b2 = gn.Solution[1]; // will be 0.556 #endregion Assert.AreEqual(0.362, b1, 1e-3); Assert.AreEqual(0.556, b2, 3e-3); }
private double[][] householder(double[] a) { double[] v = a.Divide((a[0] + Special.Sign(Norm.Euclidean(a), a[0]))); v[0] = 1; var H = Jagged.Identity(a.Length); var vr = Jagged.RowVector(v); var vc = Jagged.ColumnVector(v); var t = vc.Dot(vr); H.Subtract((2 / v.Dot(v)).Multiply(t), result: H); return(H); }
public void FitTest_vector_inputs() { double[] expected = { 0.50, 0.00, 0.25, 0.25 }; JointDistribution target; double[] values = { 0.00, 2.00, 3.00 }; double[] weights = { 0.50, 0.25, 0.25 }; target = new JointDistribution(new[] { 4 }); target.Fit(Jagged.ColumnVector(values), weights); double[] actual = target.Frequencies; Assert.IsTrue(Matrix.IsEqual(expected, actual)); }
public void FitTest3() { JointDistribution target = new JointDistribution(new[] { -1 }, new[] { 4 }); double[] values = { 0.00, 1.00, 2.00, 3.00 }; double[] weights = { 0.25, 0.25, 0.25, 0.25 }; target.Fit(Jagged.ColumnVector(values).Subtract(1), weights); double[] expected = { 0.25, 0.25, 0.25, 0.25 }; double[] actual = target.Frequencies; Assert.IsTrue(Matrix.IsEqual(expected, actual)); }
public void misra1a_test() { LevenbergMarquardt lm = new LevenbergMarquardt(2); double[] outputs = new double[] { 10.07E0, 14.73E0, 17.94E0, 23.93E0, 29.61E0, 35.18E0, 40.02E0, 44.82E0, 50.76E0, 55.05E0, 61.01E0, 66.40E0, 75.47E0, 81.78E0 }; double[][] inputs = Jagged.ColumnVector(77.6E0, 114.9E0, 141.1E0, 190.8E0, 239.9E0, 289.0E0, 332.8E0, 378.4E0, 434.8E0, 477.3E0, 536.8E0, 593.1E0, 689.1E0, 760.0E0); lm.Function = Function; lm.Gradient = Gradient; lm.Solution = new double[] { 250, 0.0005 }; // starting values double error = lm.Minimize(inputs, outputs); Assert.AreEqual(238.944658680792, lm.Solution[0], 1e-5); Assert.AreEqual(0.00055014847409921093, lm.Solution[1], 1e-5); }
public void RunTest() { Accord.Math.Tools.SetupGenerator(0); var dist = NormalDistribution.Standard; double[] x = { +1.0312479734420776, +0.99444115161895752, +0.21835240721702576, +0.47197291254997253, +0.68701112270355225, -0.58556461334228516, -0.64154046773910522, -0.66485315561294556, +0.37940266728401184, -0.61046308279037476 }; double[][] inputs = Jagged.ColumnVector(x); IKernel kernel = new Linear(); var machine = new KernelSupportVectorMachine(kernel, inputs: 1); var teacher = new OneclassSupportVectorLearning(machine, inputs) { Nu = 0.1 }; // Run the learning algorithm double error = teacher.Run(); Assert.AreEqual(2, machine.Weights.Length); Assert.AreEqual(0.39198910030993617, machine.Weights[0]); Assert.AreEqual(0.60801089969006383, machine.Weights[1]); Assert.AreEqual(inputs[0][0], machine.SupportVectors[0][0]); Assert.AreEqual(inputs[7][0], machine.SupportVectors[1][0]); Assert.AreEqual(0.0, error, 1e-10); }
public void weights_test_tree_1() { KNearestNeighbors a; KNearestNeighbors b; { double[][] inputs = Jagged.ColumnVector(4.2, 0.7, 0.7, 0.7, 1.3, 9.4, 9.4, 12); int[] outputs = { 0, 0, 0, 1, 1, 2, 2, 2 }; double[] weights = { 1, 1, 0, 0, 1, 1, 0, 1 }; var knn = new KNearestNeighbors(k: inputs.Length); a = knn.Learn(inputs, outputs, weights); } { double[][] inputs = Jagged.ColumnVector(4.2, 0.7, 1.3, 9.4, 12); int[] outputs = { 0, 0, 1, 2, 2 }; var knn = new KNearestNeighbors(k: inputs.Length); b = knn.Learn(inputs, outputs); } double[][] x = Jagged.ColumnVector(4.2, 0.7, 1.3, 9.4, 12); Assert.AreEqual(a.Scores(x), b.Scores(x)); }
public void householderTest() { double[][] m = Jagged.ColumnVector(1.0, 2.0, 3.0); double[][] q; decompose(ref m, out q); double[][] expectedM = { new double[] { -3.7416573867739404 }, new double[] { 0 }, new double[] { 0 }, }; double[][] expectedQ = { new double[] { -0.26726124191242406, -0.53452248382484868, -0.80178372573727308 }, new double[] { -0.53452248382484868, 0.77454192058843829, -0.33818711911734262 }, new double[] { -0.80178372573727308, -0.33818711911734262, 0.4927193213239861 }, }; Assert.IsTrue(expectedM.IsEqual(m, 1e-6)); Assert.IsTrue(expectedQ.IsEqual(q, 1e-6)); }
/// <summary> /// Initializes a new instance of the <see cref="RSquaredLoss"/> class. /// </summary> /// /// <param name="expected">The expected outputs (ground truth).</param> /// <param name="numberOfInputs">The number if variables being fit.</param> /// public RSquaredLoss(int numberOfInputs, double[] expected) : base(Jagged.ColumnVector(expected)) { NumberOfInputs = numberOfInputs; }
/// <summary> /// Initializes a new instance of the <see cref="HingeLoss"/> class. /// </summary> /// /// <param name="expected">The expected outputs (ground truth).</param> /// public HingeLoss(bool[] expected) { this.expected = Jagged.ColumnVector(expected); }
/// <summary> /// Initializes a new instance of the <see cref="BinaryCrossEntropyLoss" /> class. /// </summary> /// <param name="expected">The expected outputs (ground truth).</param> public BinaryCrossEntropyLoss(double[] expected) { Expected = Jagged.ColumnVector(Classes.Decide(expected)); }
/// <summary> /// Initializes a new instance of the <see cref="BinaryCrossEntropyLoss"/> class. /// </summary> /// <param name="expected">The expected outputs (ground truth).</param> public BinaryCrossEntropyLoss(double[] expected) : this(Jagged.ColumnVector(expected)) { }
/// <summary> /// Initializes a new instance of the <see cref="BinaryCrossEntropyLoss" /> class. /// </summary> /// <param name="expected">The expected outputs (ground truth).</param> public BinaryCrossEntropyLoss(bool[] expected) { Expected = Jagged.ColumnVector(expected); }
/// <summary> /// Initializes a new instance of the <see cref="BinaryCrossEntropyLoss"/> class. /// </summary> /// <param name="expected">The expected outputs (ground truth).</param> public BinaryCrossEntropyLoss(bool[] expected) : base(Jagged.ColumnVector(expected)) { }
/// <summary> /// Initializes a new instance of the <see cref="RSquaredLoss"/> class. /// </summary> /// /// <param name="expected">The expected outputs (ground truth).</param> /// <param name="numberOfInputs">The number if variables being fit.</param> /// public RSquaredLoss(int numberOfInputs, double[] expected) { this.Expected = Jagged.ColumnVector(expected); this.NumberOfInputs = numberOfInputs; this.Weights = Vector.Ones(expected.Length); }
/// <summary> /// Initializes a new instance of the <see cref="HingeLoss"/> class. /// </summary> /// /// <param name="expected">The expected outputs (ground truth).</param> /// public HingeLoss(double[] expected) : base(Jagged.ColumnVector(expected)) { }
/// <summary> /// Initializes a new instance of the <see cref="LogisticLoss"/> class. /// </summary> /// <param name="expected">The expected outputs (ground truth).</param> public LogisticLoss(double[] expected) : base(Jagged.ColumnVector(expected)) { }
/// <summary> /// Initializes a new instance of the <see cref="SquareLoss"/> class. /// </summary> /// /// <param name="expected">The expected outputs (ground truth).</param> /// public SquareLoss(double[] expected) { this.Expected = Jagged.ColumnVector(expected); }
/// <summary> /// Initializes a new instance of the <see cref="HingeLoss"/> class. /// </summary> /// /// <param name="expected">The expected outputs (ground truth).</param> /// public HingeLoss(double[] expected) { this.expected = Classes.Decide(Jagged.ColumnVector(expected)); }
/// <summary> /// Initializes a new instance of the <see cref="LogisticLoss"/> class. /// </summary> /// /// <param name="expected">The expected outputs (ground truth).</param> /// public LogisticLoss(double[] expected) { this.expected = Jagged.ColumnVector(expected); }
/// <summary> /// Initializes a new instance of the <see cref="RSquaredLoss"/> class. /// </summary> /// /// <param name="expected">The expected outputs (ground truth).</param> /// <param name="numberOfInputs">The number if variables being fit.</param> /// public RSquaredLoss(int numberOfInputs, double[] expected) { this.Expected = Jagged.ColumnVector(expected); NumberOfInputs = numberOfInputs; }