public void LogLikelihoodDerivative_MultipleResponses_MatchesFiniteDifferenceDerivative()
        {
            double alpha1 = .3;
            double delta1 = .1;
            double chi1   = .2;
            ThreeParamModelParameters modelParameters1 = new ThreeParamModelParameters(alpha1, delta1, chi1);

            double alpha2 = .5;
            double delta2 = .6;
            double chi2   = .7;
            ThreeParamModelParameters modelParameters2 = new ThreeParamModelParameters(alpha2, delta2, chi2);

            ThreeParamModelParameters modelParameters3 = new ThreeParamModelParameters(alpha2, delta2, chi2);

            List <IModelParameters> modelParameterList = new List <IModelParameters>();

            modelParameterList.Add(modelParameters1);
            modelParameterList.Add(modelParameters2);
            modelParameterList.Add(modelParameters3);

            LogLikelihoodFunction logLikelihoodFunction = new LogLikelihoodFunction(modelParameterList);

            double     theta          = .4;
            List <int> responseVector = new List <int>()
            {
                1, 0, 1
            };
            OneDimensionalFunction function          = x => logLikelihoodFunction.LogLikelihood(responseVector, x);
            FiniteDifferencer      finiteDifferencer = new FiniteDifferencer(function);

            double logLikelihoodDerivative    = logLikelihoodFunction.LogLikelihoodFirstDerivative(responseVector, theta);
            double finiteDifferenceDerivative = finiteDifferencer.ApproximateDerivative(theta);

            Assert.AreEqual(finiteDifferenceDerivative, logLikelihoodDerivative, Tolerance);
        }
 public BisectionSolver(OneDimensionalFunction function, double lowerBound, double upperBound)
 {
     _function          = function;
     _initialLowerBound = lowerBound;
     _initialUpperBound = upperBound;
     ValidateBounds();
 }
Exemple #3
0
 public GlobalMaximizer(OneDimensionalFunction function, OneDimensionalFunction derivativeFunction, double lowerBound, double upperBound, double tolerance = .001)
 {
     _lowerBound    = lowerBound;
     _upperBound    = upperBound;
     _function      = function;
     _extremaFinder = new GradientDescentExtremaFinder(derivativeFunction, tolerance);
 }
Exemple #4
0
 public BisectionSolver(OneDimensionalFunction function, double lowerBound, double upperBound)
 {
     _function = function;
     _initialLowerBound = lowerBound;
     _initialUpperBound = upperBound;
     ValidateBounds();
 }
        public void ApproximateDerivative_SimpleQuadraticAtNonZeroDerivative_ReturnsApproxDerivativeWithinTolerance()
        {
            OneDimensionalFunction f = x => x * x;

            FiniteDifferencer finiteDifferencer = new FiniteDifferencer(f);
            var approxDerivative = finiteDifferencer.ApproximateDerivative(.5);

            double expectedDerivative = 2 * .5;

            CheckWithinTolerance(expectedDerivative, approxDerivative);
        }
        public void ApproximateDerivative_CosAtZeroDerivative_ReturnsApproxDerivativeWithinTolerance()
        {
            OneDimensionalFunction f = Math.Cos;

            FiniteDifferencer finiteDifferencer = new FiniteDifferencer(f);
            var approxDerivative = finiteDifferencer.ApproximateDerivative(0);

            double expectedDerivative = 0;

            CheckWithinTolerance(expectedDerivative, approxDerivative);
        }
Exemple #7
0
        public void FindMaximum_XSquared_ThrowsExceptions()
        {
            OneDimensionalFunction       derivative    = x => 2 * x;
            GradientDescentExtremaFinder extremaFinder = new GradientDescentExtremaFinder(derivative);

            double initialGuess      = 3;
            var    calculatedMinimum = extremaFinder.FindMaximum(initialGuess);

            double expectedMinimum = 0;

            Assert.AreEqual(expectedMinimum, calculatedMinimum, Tolerance);
        }
Exemple #8
0
        public void FindMaximum_SinX_ReturnsPiOverTwo()
        {
            OneDimensionalFunction       derivative    = x => Math.Cos(x);
            GradientDescentExtremaFinder extremaFinder = new GradientDescentExtremaFinder(derivative);

            double initialGuess         = 0;
            var    calculatedMinimumLoc = extremaFinder.FindMaximum(initialGuess);

            double expectedMinimumLoc = Math.PI / 2;

            Assert.AreEqual(expectedMinimumLoc, calculatedMinimumLoc, Tolerance);
        }
        public void FindRoot_CosFunction_ReturnsRootWithinTolerance()
        {
            OneDimensionalFunction f      = Math.Cos;
            OneDimensionalFunction fPrime = x => - Math.Sin(x);

            const double        initialGuess = .5;
            NewtonRhapsonSolver solver       = new NewtonRhapsonSolver(f, fPrime, initialGuess);

            var root = solver.FindRoot();

            Assert.IsTrue(Math.Abs(f(root)) < solver.Tolerance);
        }
Exemple #10
0
        public void FindRoot_CosFunction_ReturnsRootWithinTolerance()
        {
            OneDimensionalFunction f = Math.Cos;

            double          lowerBound = -Math.PI / 2;
            double          upperBound = Math.PI;
            BisectionSolver solver     = new BisectionSolver(f, lowerBound, upperBound);

            var root = solver.FindRoot();

            Assert.IsTrue(Math.Abs(f(root)) < solver.Tolerance);
        }
Exemple #11
0
        public void FindRoot_PolynomialLowerBoundIsZero_ReturnsExactRoot()
        {
            OneDimensionalFunction f = x => x * x - 4;

            double          lowerBound = -2;
            double          upperBound = 3;
            BisectionSolver solver     = new BisectionSolver(f, lowerBound, upperBound);

            var root = solver.FindRoot();

            Assert.AreEqual(lowerBound, root);
        }
Exemple #12
0
        public void FindRoot_QuadraticIncorrectIntialGuess_ReturnsRootWithinTolerance()
        {
            OneDimensionalFunction f = x => x * x - 4;

            double          lowerBound = -1;
            double          upperBound = 3;
            BisectionSolver solver     = new BisectionSolver(f, lowerBound, upperBound);

            var root = solver.FindRoot();

            Assert.IsTrue(Math.Abs(f(root)) < solver.Tolerance);
        }
        public void FindRoot_QuadraticIncorrectIntialGuess_ReturnsRootWithinTolerance()
        {
            OneDimensionalFunction f      = x => x * x;
            OneDimensionalFunction fPrime = x => 2 * x;

            const double        initialGuess = 4;
            NewtonRhapsonSolver solver       = new NewtonRhapsonSolver(f, fPrime, initialGuess);

            var root = solver.FindRoot();

            Assert.IsTrue(Math.Abs(f(root)) < solver.Tolerance);
        }
        public void FindRoot_PolynomialExactIntialGuess_ReturnsExactRoot()
        {
            OneDimensionalFunction f      = x => x * x - 4;
            OneDimensionalFunction fPrime = x => 2 * x;

            const double        initialGuess = 2;
            NewtonRhapsonSolver solver       = new NewtonRhapsonSolver(f, fPrime, initialGuess);

            var root = solver.FindRoot();

            Assert.AreEqual(initialGuess, root);
        }
        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);
        }
Exemple #17
0
        private double InternalFindMin(double initialGuess, OneDimensionalFunction derivativeFunction)
        {
            double x = initialGuess;

            for (int i = 0; i < MaxIterations; i++)
            {
                var    derivative = derivativeFunction(x);
                double newX       = x - Alpha * derivative;
                if (DifferenceWithinTolerance(x, newX))
                {
                    return(newX);
                }

                x = newX;
            }

            throw new Exception(String.Format("Unable to find minimum using gradient descent within {0} iterations",
                                              MaxIterations));
        }
Exemple #18
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);
        }
 public FiniteDifferencer(OneDimensionalFunction function)
 {
     _function = function;
 }
Exemple #20
0
 public GradientDescentExtremaFinder(OneDimensionalFunction derivativeFunction, double tolerance = .001)
 {
     _derivativeFunction = derivativeFunction;
     _tolerance          = tolerance;
 }
Exemple #21
0
        public double FindMaximum(double initialGuess)
        {
            OneDimensionalFunction negativeOfDerivative = x => - _derivativeFunction(x);

            return(InternalFindMin(initialGuess, negativeOfDerivative));
        }
Exemple #22
0
 public FiniteDifferencer(OneDimensionalFunction function)
 {
     _function = function;
 }
 // Newton Rhapson root finder for one dimensional functions.
 public NewtonRhapsonSolver(OneDimensionalFunction function, OneDimensionalFunction fDerivative, double initialGuess)
 {
     _function     = function;
     _fDerivative  = fDerivative;
     _initialGuess = initialGuess;
 }
Exemple #24
0
 // Newton Rhapson root finder for one dimensional functions.
 public NewtonRhapsonSolver(OneDimensionalFunction function, OneDimensionalFunction fDerivative, double initialGuess)
 {
     _function = function;
     _fDerivative = fDerivative;
     _initialGuess = initialGuess;
 }