Exemplo n.º 1
0
        public void LogLikelihoodSecondDerivative_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);

            double alpha3 = .1;
            double delta3 = .2;
            double chi3 = .4;
            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 derivativeFunction = x => logLikelihoodFunction.LogLikelihoodFirstDerivative(responseVector, x);
            FiniteDifferencer finiteDifferencer = new FiniteDifferencer(derivativeFunction);

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

            Assert.AreEqual(finiteDifferenceDerivative, logLikelihoodDerivative, Tolerance);
        }
Exemplo n.º 2
0
        public double GetMle(List<int> responseVector)
        {
            LogLikelihoodFunction logLikelihoodFunction = new LogLikelihoodFunction(_modelParametersList);

            const double initialGuess = 0;
            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, -5, 5);

            double mle = rootSolver.FindRoot();

            return mle;
        }
Exemplo 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);
        }
Exemplo n.º 4
0
        public void LogLikelihood_OneIncorrectResponseOneCorrectResponse_ReturnsCorrectValue()
        {
            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);

            List<IModelParameters> modelParameterList = new List<IModelParameters>();
            modelParameterList.Add(modelParameters1);
            modelParameterList.Add(modelParameters2);

            LogLikelihoodFunction logLikelihoodFunction = new LogLikelihoodFunction(modelParameterList);

            double theta = .4;
            List<int> responseVector = new List<int>() { 1, 0 };
            double logLikelihood = logLikelihoodFunction.LogLikelihood(responseVector, theta);

            IProbabilityFunction probabilityFunction1 = new ThreeParamProbabilityFunction(modelParameters1);
            double p1 = probabilityFunction1.ProbabilityOfCorrectResponse(theta);
            IProbabilityFunction probabilityFunction2 = new ThreeParamProbabilityFunction(modelParameters2);
            double p2 = probabilityFunction2.ProbabilityOfCorrectResponse(theta);
            double expectedLikelihood = Math.Log(p1) + Math.Log(1 - p2);

            Assert.AreEqual(expectedLikelihood, logLikelihood, Tolerance);
        }
Exemplo n.º 5
0
        public void LogLikelihood_SingleIncorrectResponse_ReturnsCorrectValue()
        {
            double alpha = .3;
            double delta = .1;
            double chi = .2;
            ThreeParamModelParameters modelParameters = new ThreeParamModelParameters(alpha, delta, chi);
            List<IModelParameters> modelParameterList = new List<IModelParameters>();
            modelParameterList.Add(modelParameters);

            LogLikelihoodFunction logLikelihoodFunction = new LogLikelihoodFunction(modelParameterList);

            double theta = .4;
            List<int> responseVector = new List<int>() { 0 };
            double logLikelihood = logLikelihoodFunction.LogLikelihood(responseVector, theta);

            IProbabilityFunction probabilityFunction = new ThreeParamProbabilityFunction(modelParameters);
            double p = probabilityFunction.ProbabilityOfCorrectResponse(theta);
            double expectedLikelihood = Math.Log(1 - p);
            Assert.AreEqual(expectedLikelihood, logLikelihood, Tolerance);
        }