Resources

Model Definition for Inverse Gaussian Distribution

/*--------------------------------------------------------------

                    SAS Sample Library

        Name: svrtigau.sas
 Description: Example Program from SAS/ETS User's Guide,
              The SEVERITY Procedure
       Title: Model Definition for Inverse Gaussian Distribution
     Product: SAS/ETS Software
        Keys: fitting continuous distributions
        PROC: SEVERITY
       Notes: If you run this sample program without any modification, then
              the Sasuser.Svrtdist library contains functions and subroutines
              that are identical to those in the Sashelp.Svrtdist library.
              Further, if you run this sample without any modification to the
              names of functions and subroutines, then PROC FCMP writes
              warnings of the following nature to the SAS log:
              WARNING: Function  was defined in a previous package.
                       Function  as defined in the current program will
                       be used as default when the package is not specified.
              You can ignore such warnings; they appear because the functions
              defined in this sample are already defined in the input library
              Sashelp.Svrtdist.

--------------------------------------------------------------*/
proc fcmp library=sashelp.svrtdist outlib=sasuser.svrtdist.models;
    function IGAUSS_DESCRIPTION() $32;
        length model $32;
        model = "Inverse Gaussian Distribution";
        return(model);
    endsub;

    function IGAUSS_PARMCOUNT();
        return(2);
    endsub;

    function IGAUSS_LOGPDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        if (x >= constant('MACEPS')) then do;
            z = x/Theta;

            return (0.5 * (log(Alpha) - log(2 * constant('Pi')) - 3 * log(z))
                    - Alpha*(z-1)**2/(2*z) - log(Theta));
        end;
        else
            return (.);
    endsub;
    function IGAUSS_PDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        if (x >= constant('MACEPS')) then do;
            z = x/Theta;

            return (sqrt(1.0/(2 * constant('Pi') * z**2 * (z/Alpha))) *
                    exp(-(Alpha/z)*((z-1)**2)/2) / Theta);
        end;
        else
            return (0);
    endsub;

    function IGAUSS_CDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */

        if (x >= constant('MACEPS')) then do;
            z = (x/Theta);
            t = sqrt(Alpha/z);

            t1 = CDF('NORMAL',((z-1)*t));
            t2 = CDF('NORMAL',(-(z+1)*t));
            if (t2 <= constant('MACEPS')) then
                return (t1);
            else
                return (t1 + exp(2*Alpha) * t2);
        end;
        else
            return (0);
    endsub;
    function IGAUSS_LOGCDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */

        if (x >= constant('MACEPS')) then do;
            cdf = IGAUSS_CDF(x, Theta, Alpha);
            if (not(missing(cdf)) and cdf > 0) then
                return (log(cdf));
            else
                return (.);
        end;
        else
            return (.);
    endsub;

    function IGAUSS_LOGSDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */

        if (x >= constant('MACEPS')) then do;
            cdf = IGAUSS_CDF(x, Theta, Alpha);
            if (not(missing(cdf))) then do;
                if (cdf < 1) then
                    return (log1px(-cdf));
                else
                    return (.);
            end;
            else
                return (.);
        end;
        else
            return (0);
    endsub;
    function IGAUSS_SDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */

        if (x >= constant('MACEPS')) then do;
            cdf = IGAUSS_CDF(x, Theta, Alpha);
            if (not(missing(cdf))) then
                return (1-cdf);
            else
                return (.);
        end;
        else
            return (1);
    endsub;

    subroutine IGAUSS_PARMINIT(dim, x[*], nx[*], F[*], Ftype, Theta, Alpha);
        outargs Theta, Alpha;
        array m[2] / nosymbols;

        /* Use Method of Moments */
        call svrtutil_rawmoments(dim, x, nx, 2, m);

        if (missing(m[1])) then do;
            Theta = .;
            Alpha = .;
        end;
        else do;
            Theta = m[1];

            t1 = m[2] - m[1]**2;
            if (t1 < constant('MACEPS')) then
                Alpha = 1;
            else
                Alpha = m[1]**2/t1;
        end;
    endsub;

    function IGAUSS_QUANTILE(p, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        Lambda = Alpha * Theta;
        return (quantile('IGAUSS', p, Lambda, Theta));
    endsub;
quit;