inverse-gamma-distribution-pdf(x) with given shape and scale - Java java.lang

Java examples for java.lang:Math Function

Description

inverse-gamma-distribution-pdf(x) with given shape and scale

Demo Code


//package com.java2s;

import static java.lang.Math.*;

public class Main {
    private static final double sqrt2Pi = sqrt(2 * PI);

    /**// w w  w .  j  a  v  a 2 s  . c  o  m
     * inverse-gamma-distribution-pdf(x) with given shape and scale
     * @url https://en.wikipedia.org/wiki/Inverse_gamma_distribution
     */
    public static double inverseGammaPdf(double x, double alpha, double beta) {
        double firstPart = pow(beta, alpha);
        double secondPart = pow(x, -alpha - 1);
        double thirdPart = exp(-beta / x);
        return firstPart * secondPart * thirdPart / gamma(alpha);
    }

    /**
     * gamma-function(x) using Lanczos approximation formula
     * @url http://en.wikipedia.org/wiki/Gamma_function
     */
    public static double gamma(double x) {
        return exp(logGamma(x));
    }

    private static double logGamma(double x) {
        double tmp = (x - 0.5) * log(x + 4.5) - (x + 4.5);
        double ser = 1.0 + 76.18009173 / (x + 0) - 86.50532033 / (x + 1)
                + 24.01409822 / (x + 2) - 1.231739516 / (x + 3)
                + 0.00120858003 / (x + 4) - 0.00000536382 / (x + 5);
        return tmp + log(ser * sqrt2Pi);
    }
}

Related Tutorials