Example: beta probability distribution

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


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

public class BetaPDEx1 {

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

        double[] x = {
            0.12396e0, 0.58037e0, 0.28837e0, 0.38195e0,
            0.44387e0, 0.17680e0, 0.22661e0, 0.55939e0
        };

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

        BetaPD beta = new BetaPD();

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

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

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

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

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

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

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

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

Output

Beta(1.2,2.0) pdf values at x=
     0    
0  0.124  
1  0.58   
2  0.288  
3  0.382  
4  0.444  
5  0.177  
6  0.227  
7  0.559  

f(x;a,b)=
     0    
0  1.523  
1  0.994  
2  1.465  
3  1.346  
4  1.248  
5  1.537  
6  1.517  
7  1.036  

Gradient f(x;a,b) at x[0]
     0     
0  -1.219  
1   0.676  

 Hessian of f(x;a,b) at x[0]
        0            1       
0  -0.39787363   0.01734585  
1   0.01734585  -0.12450620  

Link to Java source.