This example evaluates the normal density, approximate gradient and hessian, and analytic gradient and hessian for a small sample of data.
import com.imsl.math.*; import com.imsl.stat.distributions.*; import java.text.DecimalFormat; public class NormalPDEx1 { public static void main(String[] args) throws Exception { double x[] = { 24.67074744935116, 23.452437146719504, 23.508058666655124, 24.085805009837582, 26.387706562892493, 26.244756646592872 }; double[] values = new double[x.length]; double mean = 25.0; double stdDev = 1.0; NormalPD normal = new NormalPD(); for (int i = 0; i < x.length; i++) { values[i] = normal.pdf(x[i], mean, stdDev); } PrintMatrixFormat pmf = new PrintMatrixFormat(); pmf.setNumberFormat(new DecimalFormat("0.00000000")); PrintMatrix pm = new PrintMatrix("Normal(25.0,1.0) pdf values at x="); pm.print(x); pm.setTitle("f(x;a,b)="); pm.print(values); double[] gradientApprox = normal.getPDFGradientApproximation(x[0], mean, stdDev); pm.setTitle("Gradient approximation f(x;a,b) at x[0]"); pm.print(gradientApprox); double[] gradient = normal.getPDFGradient(x[0], mean, stdDev); pm.setTitle("Gradient f(x;a,b) at x[0]"); pm.print(gradient); double[][] hessianApprox = normal.getPDFHessianApproximation(x[0], mean, stdDev); pm.setTitle("Hessian approximation of f(x;a,b) at x[0]"); pm.print(pmf, hessianApprox); double[][] hessian = normal.getPDFHessian(x[0], mean, stdDev); pm.setTitle("Hessian of f(x;a,b) at x[0]"); pm.print(pmf, hessian); } }
Normal(25.0,1.0) pdf values at x= 0 0 24.671 1 23.452 2 23.508 3 24.086 4 26.388 5 26.245 f(x;a,b)= 0 0 0.378 1 0.12 2 0.131 3 0.263 4 0.152 5 0.184 Gradient approximation f(x;a,b) at x[0] 0 0 -0.124 1 -0.337 Gradient f(x;a,b) at x[0] 0 0 -0.124 1 -0.337 Hessian approximation of f(x;a,b) at x[0] 0 1 0 -0.33692738 0.35977953 1 0.35977953 0.55539644 Hessian of f(x;a,b) at x[0] 0 1 0 -0.33692735 0.35977916 1 0.35977916 0.55539649Link to Java source.