Example: gamma probability distribution

This example evaluates the gamma density, gradient, and hessian for a small sample of data.


import com.imsl.math.*;
import com.imsl.stat.distributions.*;
import java.text.DecimalFormat;

public class GammaPDEx1 {

    public static void main(String[] args) throws Exception {

        double x[] = {
            1.015998e0, 0.2677489e0, 2.198958e0, 1.784697e0, 0.691063e0,
            0.992409e0, 0.5466309e0, 1.154601e0, 2.613015e0, 1.219826e0
        };

        double[] values = new double[x.length];
        double a = 3.0;
        double b = 2.0;

        GammaPD gamma = new GammaPD();

        for (int i = 0; i < x.length; i++) {
            values[i] = gamma.pdf(x[i], a, b);
        }

        PrintMatrixFormat pmf = new PrintMatrixFormat();
        pmf.setNumberFormat(new DecimalFormat("0.00000000"));

        PrintMatrix pm = new PrintMatrix("Gamma(3.0,2.0) pdf values at x=");
        pm.print(x);

        pm.setTitle("f(x;a,b)=");
        pm.print(values);

        double[] gradient = gamma.getPDFGradientApproximation(x[0], a, b);

        pm.setTitle("Gradient f(x;a,b) at x[0]");
        pm.print(gradient);

        double[][] hessian = gamma.getPDFHessianApproximation(x[0], a, b);

        pm.setTitle("Hessian of f(x;a,b) at x[0]");
        pm.print(pmf, hessian);
    }
}

Output

Gamma(3.0,2.0) pdf values at x=
     0    
0  1.016  
1  0.268  
2  2.199  
3  1.785  
4  0.691  
5  0.992  
6  0.547  
7  1.155  
8  2.613  
9  1.22   

f(x;a,b)=
     0    
0  0.039  
1  0.004  
2  0.101  
3  0.082  
4  0.021  
5  0.037  
6  0.014  
7  0.047  
8  0.116  
9  0.051  

Gradient f(x;a,b) at x[0]
     0     
0  -0.062  
1  -0.048  

Hessian of f(x;a,b) at x[0]
       0           1       
0  0.08405321  0.05798301  
1  0.05798301  0.07952132  

Link to Java source.