Resources

Model Definition for Pareto Distribution

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

                    SAS Sample Library

        Name: svrtpare.sas
 Description: Example Program from SAS/ETS User's Guide,
              The SEVERITY Procedure
       Title: Model Definition for Pareto 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 <name> was defined in a previous package.
                       Function <name> 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 PARETO_DESCRIPTION() $32;
        length model $32;
        model = "Pareto Distribution";
        return(model);
    endsub;

    function PARETO_PARMCOUNT();
        return(2);
    endsub;

    function PARETO_LOGPDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        if (x < 0) then
            return (-180.218266); /* constant('LOGSMALL') */
        else do;
            return (log(Alpha/Theta) - (Alpha+1) * log1px(x/Theta));
        end;
    endsub;
    function PARETO_PDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        if (x < 0) then
            return (0);
        else do;
            logpdf = PARETO_LOGPDF(x, Theta, Alpha);
            if (logpdf < 174.673089) then /* constant('LOGBIG') */
                return ( exp(logpdf) );
            else
                return ( 7.237005E75 ); /* constant('BIG') */
        end;
    endsub;

    function PARETO_LOGCDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        if (x < 0) then
            return (-180.218266); /* constant('LOGSMALL') */
        else do;
            c = (1 + x/Theta)**(-Alpha);
            if (c < 1) then
                return (log1px(-c));
            else
                return (-180.218266); /* constant('LOGSMALL') */
        end;
    endsub;
    function PARETO_CDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        if (x < 0) then
            return (0);
        else
            return (1 - (1 + x/Theta)**(-Alpha));
    endsub;

    function PARETO_LOGSDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        if (x < 0) then
            return (0);
        else
            return (-Alpha*log1px(x/Theta));
    endsub;
    function PARETO_SDF(x, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        if (x < 0) then
            return (1);
        else
            return ((1 + x/Theta)**(-Alpha));
    endsub;

    subroutine PARETO_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;
            t1 = m[2] - m[1]**2;
            t2 = 2*t1 - m[2];

            eps = 2.220446E-16; /* constant('MACEPS') */
            if (t1 < eps or t2 < eps) then do;
                Theta = m[1];
                Alpha = 2;
            end;
            else do;
                Theta = (m[1]*m[2])/t2;
                Alpha = (2*t1)/t2;
            end;
        end;
    endsub;

    function PARETO_QUANTILE(p, Theta, Alpha);
        /* Theta : Scale */
        /* Alpha : Shape */
        return (((1-p)**(-1/Alpha)-1)*Theta);
    endsub;

    function PARETO_MEAN(x, Theta, Alpha);
        if not(Alpha > 1) then
            return (.); /* first moment does not exist */
        return (Theta/(Alpha - 1));
    endsub;
quit;