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); 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 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 void ApproximateDerivative_SimpleQuadraticAtZeroDerivative_ReturnsApproxDerivativeWithinTolerance() { OneDimensionalFunction f = x => x*x; FiniteDifferencer finiteDifferencer = new FiniteDifferencer(f); var approxDerivative = finiteDifferencer.ApproximateDerivative(0); double expectedDerivative = 0; CheckWithinTolerance(expectedDerivative, approxDerivative); }
public void ApproximateDerivative_CosAtNonZeroDerivative_ReturnsApproxDerivativeWithinTolerance() { OneDimensionalFunction f = Math.Cos; FiniteDifferencer finiteDifferencer = new FiniteDifferencer(f); double x = 1; var approxDerivative = finiteDifferencer.ApproximateDerivative(x); double expectedDerivative = -Math.Sin(x); CheckWithinTolerance(expectedDerivative, approxDerivative); }
public void SecondThetaDerivative_NonTrivialInput_CloseToFiniteDifferenceValue() { double alpha = .2; double delta = .3; TwoParamModelParameters parameters = new TwoParamModelParameters(alpha, delta); TwoParamProbabilityFunction probabilityFunction = new TwoParamProbabilityFunction(parameters); FiniteDifferencer finiteDifferencer = new FiniteDifferencer(x => probabilityFunction.FirstThetaDerivative(x)); double theta = .1; double secondDerivative = probabilityFunction.SecondThetaDerivative(theta); double approxSecondDerivative = finiteDifferencer.ApproximateDerivative(theta); Assert.IsTrue(Math.Abs(secondDerivative - approxSecondDerivative) < Tolerance); }
public void FirstThetaDerivative_NonTrivialInput_CloseToFiniteDifferenceValue() { double alpha = .2; double delta = .3; double chi = .4; ThreeParamModelParameters parameters = new ThreeParamModelParameters(alpha, delta, chi); ThreeParamProbabilityFunction probabilityFunction = new ThreeParamProbabilityFunction(parameters); FiniteDifferencer finiteDifferencer = new FiniteDifferencer(x => probabilityFunction.ProbabilityOfCorrectResponse(x)); double theta = .1; double derivative = probabilityFunction.FirstThetaDerivative(theta); double approxDerivative = finiteDifferencer.ApproximateDerivative(theta); Assert.IsTrue(Math.Abs(derivative - approxDerivative) < Tolerance); }