public void DualLinearRegressionWithResidualSumOfSquares()
        {
            // obtain the test data
            var trainingSet = new List <DataPoint <double> >
            {
                new DataPoint <double>(-1, new [] { -1.5, -1.0 }),
                new DataPoint <double>(0, new [] { 0.5, 1.0 }),
                new DataPoint <double>(1, new [] { 2.5, 3.0 }),
                new DataPoint <double>(2, new [] { 4.5, 5.0 }),
                new DataPoint <double>(3, new [] { 6.5, 6.0 })
            };

            // assume a hypothesis
            var hypothesis          = new DualLinearHypothesis(1);
            var initialCoefficients = Vector <double> .Build.Random(2);

            // cost function is sum of squared errors
            var costFunction = new ResidualSumOfSquaresCostFunction(hypothesis, trainingSet);

            // define the optimization problem
            var problem = new OptimizationProblem <double, IDifferentiableCostFunction <double> >(costFunction, initialCoefficients);

            // optimize!
            var gd = new ResilientErrorGD
            {
                ErrorTolerance = 0.0D
            };
            var result = gd.Minimize(problem);

            // assert!
            var coefficients = result.Coefficients;

            coefficients[0].Should().BeApproximately(0.5, 1E-6D, "because that's the underlying system's intercept");
            coefficients[1].Should().BeApproximately(2, 1E-6D, "because that's the underlying system's slope");
        }
        public void DualLinearRegressionWithResidualSumOfSquares()
        {
            // obtain the test data
            var trainingSet = new List<DataPoint<double>>
            {
                new DataPoint<double>(-1, new [] {-1.5 , -1.0 }),
                new DataPoint<double>(0, new [] {0.5, 1.0}),
                new DataPoint<double>(1, new [] {2.5, 3.0}),
                new DataPoint<double>(2, new [] {4.5, 5.0}),
                new DataPoint<double>(3, new [] {6.5, 6.0})
            };

            // assume a hypothesis
            var hypothesis = new DualLinearHypothesis(1);
            var initialCoefficients = Vector<double>.Build.Random(2);

            // cost function is sum of squared errors
            var costFunction = new ResidualSumOfSquaresCostFunction(hypothesis, trainingSet);

            // define the optimization problem
            var problem = new OptimizationProblem<double, IDifferentiableCostFunction<double>>(costFunction, initialCoefficients);

            // optimize!
            var gd = new ResilientErrorGD
            {
                ErrorTolerance = 0.0D
            };
            var result = gd.Minimize(problem);

            // assert!
            var coefficients = result.Coefficients;
            coefficients[0].Should().BeApproximately(0.5, 1E-6D, "because that's the underlying system's intercept");
            coefficients[1].Should().BeApproximately(2, 1E-6D, "because that's the underlying system's slope");
        }
        public void MultiOutputHypothesisWorks([Random(5)] double value, [Random(5)] double scale, [Random(5)] double offset)
        {
            var h = new DualLinearHypothesis(1);
            var theta = Vector<double>.Build.Dense(new[] { offset, scale });
            var inputs = Vector<double>.Build.Dense(1, value);

            var outputs = h.Evaluate(theta, inputs);

            outputs.Count.Should().Be(2, "because two outputs are expected");

            outputs.First().Should().BeApproximately(value * scale + offset, 1E-5D, "because the function is linear");
            outputs.Last().Should().BeApproximately(value * scale + 2*offset, 1E-5D, "because the function is linear");
        }