Resources

Model Definition for Lognormal Distribution

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

                    SAS Sample Library

        Name: svrtlogn.sas
 Description: Example Program from SAS/ETS User's Guide,
              The SEVERITY Procedure
       Title: Model Definition for Lognormal 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 LOGN_DESCRIPTION() $32;
        length model $32;
        model = "Lognormal Distribution";
        return(model);
    endsub;

    function LOGN_PARMCOUNT();
        return(2);
    endsub;

    function LOGN_LOGPDF(x, Mu, Sigma);
        /* Mu    : Location (exp(Scale)) */
        /* Sigma : Shape */
        if (x >= constant('MACEPS')) then do;
            lx = log(x);
            z = (lx - Mu)/Sigma;
            return (- lx - log(Sigma) - 0.5 * log(2 * constant('Pi'))
                    - 0.5 * z**2);
        end;
        else
            return (.);
    endsub;
    function LOGN_PDF(x, Mu, Sigma);
        /* Mu    : Location (exp(Scale)) */
        /* Sigma : Shape */
        if (x >= constant('MACEPS')) then do;
            z = (log(x) - Mu)/Sigma;
            return (exp(-(z**2)/2)/(x * Sigma * sqrt(2 * constant('Pi'))));
        end;
        else
            return (0);
    endsub;

    function LOGN_LOGCDF(x, Mu, Sigma);
        /* Mu    : Location (exp(Scale)) */
        /* Sigma : Shape */
        if (x >= constant('MACEPS')) then do;
            z = (log(x) - Mu)/Sigma;
            return (LOGCDF('NORMAL', z));
        end;
        else
            return (.);
    endsub;
    function LOGN_CDF(x, Mu, Sigma);
        /* Mu    : Location (exp(Scale)) */
        /* Sigma : Shape */
        if (x >= constant('MACEPS')) then do;
            z = (log(x) - Mu)/Sigma;
            return (CDF('NORMAL', z));
        end;
        else
            return (0);
    endsub;

    function LOGN_LOGSDF(x, Mu, Sigma);
        /* Mu    : Location (exp(Scale)) */
        /* Sigma : Shape */
        if (x >= constant('MACEPS')) then do;
            z = (log(x) - Mu)/Sigma;
            return (LOGSDF('NORMAL', z));
        end;
        else
            return (0);
    endsub;
    function LOGN_SDF(x, Mu, Sigma);
        /* Mu    : Location (exp(Scale)) */
        /* Sigma : Shape */
        if (x >= constant('MACEPS')) then do;
            z = (log(x) - Mu)/Sigma;
            return (SDF('NORMAL', z));
        end;
        else
            return (1);
    endsub;

    function LOGN_SCALETRANSFORM() $32;
        length xform $32;
        xform = "LOG";
        return (xform);
    endsub;

    subroutine LOGN_PARMINIT(dim, x[*], nx[*], F[*], Ftype, Mu, Sigma);
        outargs Mu, Sigma;
        array m[2] / nosymbols;

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

        if (missing(m[1])) then do;
            Mu    = .;
            Sigma = .;
        end;
        else do;
            t1 = log(m[2]) - 2*log(m[1]);

            Mu    = log(m[1]) - t1/2;
            Sigma = sqrt(t1);
        end;
    endsub;

    subroutine LOGN_LOWERBOUNDS(Mu, Sigma);
        outargs Mu, Sigma;

        Mu    = .; /* Mu unrestricted from below */
        Sigma = 0; /* Sigma > 0 */
    endsub;

    function LOGN_QUANTILE(p, Mu, Sigma);
        /* Mu    : Location (exp(Scale)) */
        /* Sigma : Shape */
        qn = quantile('NORMAL',p);
        if (missing(qn)) then
            return (.);
        return (exp(Mu + Sigma*qn));
    endsub;
quit;