public void FindMaximize_MaxiumOccursAtBoundary_ReturnsCorrectValue()
        {
            OneDimensionalFunction function           = x => - x * x * x * x - x * x * x + 11 * x * x + 9 * x - 18;
            OneDimensionalFunction derivativeFunction = x => - 4 * x * x * x - 3 * x * x + 22 * x + 9;
            double          lowerBound = -5;
            double          upperBound = 5;
            GlobalMaximizer maximizer  = new GlobalMaximizer(function, derivativeFunction, lowerBound, upperBound);

            var maxLocation = maximizer.FindPosOfMaximum();

            double expectedMaxPostion = 2.20582;

            Assert.AreEqual(expectedMaxPostion, maxLocation, Tolerance);
        }
        public void FindMaximize_FunctionWithSeveralEqualGlobalMinima_ReturnsAValueWhichGivesMax()
        {
            OneDimensionalFunction function           = Math.Sin;
            OneDimensionalFunction derivativeFunction = Math.Cos;
            double          lowerBound = -5;
            double          upperBound = 5;
            GlobalMaximizer maximizer  = new GlobalMaximizer(function, derivativeFunction, lowerBound, upperBound);

            var    x   = maximizer.FindPosOfMaximum();
            double max = function(x);

            double expectedMax = 1;

            Assert.AreEqual(expectedMax, max, Tolerance);
        }
Esempio n. 3
0
        public double GetMle(List <int> responseVector)
        {
            LogLikelihoodFunction logLikelihoodFunction = new LogLikelihoodFunction(_modelParametersList);

            //const double initialGuess = 0;
            OneDimensionalFunction function = x => logLikelihoodFunction.LogLikelihood(responseVector, x);
            OneDimensionalFunction firstDerivativeFunction = x => logLikelihoodFunction.LogLikelihoodFirstDerivative(responseVector, x);
            //OneDimensionalFunction secondDerivativeFunction = x => logLikelihoodFunction.LogLikelihoodSecondDerivative(responseVector, x);
            //NewtonRhapsonSolver rootSolver = new NewtonRhapsonSolver(firstDerivativeFunction, secondDerivativeFunction, initialGuess);
            //BisectionSolver rootSolver = new BisectionSolver(firstDerivativeFunction, -6, 6);

            //double mle = rootSolver.FindRoot();
            //double mle = rootSolver.FindRoot();

            GlobalMaximizer globalMaximizer = new GlobalMaximizer(function, firstDerivativeFunction, -6, 6, _gradientDescentTolerance);
            var             mle             = globalMaximizer.FindPosOfMaximum();

            return(mle);
        }