Beispiel #1
0
    public static double log_normal_truncated_ab_mean(double mu, double sigma, double a,
                                                      double b)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    LOG_NORMAL_TRUNCATED_AB_MEAN: mean of the Log Normal truncated AB PDF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    27 March 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, double MU, SIGMA, the parameters of the PDF.
    //    0.0 < SIGMA.
    //
    //    Input, double A, B, the lower and upper truncation limits.
    //    A < B.
    //
    //    Output, double LOG_NORMAL_TRUNCATED_AB_MEAN, the mean of the PDF.
    //
    {
        bool check = log_normal_truncated_ab_check(mu, sigma, a, b);

        switch (check)
        {
        case false:
            Console.WriteLine("");
            Console.WriteLine("LOG_NORMAL_TRUNCATED_AB_MEAN - Fatal error!");
            Console.WriteLine("  Parameters are not legal.");
            return(1);
        }

        double a0 = (Math.Log(a) - mu) / sigma;
        double b0 = (Math.Log(b) - mu) / sigma;

        double c1 = CDF.normal_01_cdf(sigma - a0);
        double c2 = CDF.normal_01_cdf(sigma - b0);
        double c3 = CDF.normal_01_cdf(+a0);
        double c4 = CDF.normal_01_cdf(+b0);

        double ln_mean = Math.Exp(mu + 0.5 * sigma * sigma);

        double mean = ln_mean * (c1 - c2) / (c4 - c3);

        return(mean);
    }
Beispiel #2
0
    public static double normal_truncated_a_pdf(double x, double mu, double sigma, double a)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TRUNCATED_NORMAL_A_PDF evaluates the lower truncated Normal PDF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    24 January 2017
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, double X, the argument of the PDF.
    //
    //    Input, double MU, SIGMA, the mean and standard deviation of the
    //    parent Normal distribution.
    //
    //    Input, double A, the lower truncation limit.
    //
    //    Output, double TRUNCATED_NORMAL_A_PDF, the value of the PDF.
    //
    {
        double pdf;

        if (x < a)
        {
            pdf = 0.0;
        }
        else
        {
            double alpha = (a - mu) / sigma;
            double xi    = (x - mu) / sigma;

            double alpha_cdf = CDF.normal_01_cdf(alpha);
            double xi_pdf    = Normal.normal_01_pdf(xi);

            pdf = xi_pdf / (1.0 - alpha_cdf) / sigma;
        }

        return(pdf);
    }
Beispiel #3
0
    public static double normal_truncated_ab_sample(double mu, double s, double a, double b,
                                                    ref int seed)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    NORMAL_TRUNCATED_AB_SAMPLE samples the truncated Normal PDF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    14 August 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, double MU, S, the mean and standard deviation of the
    //    parent Normal distribution.
    //
    //    Input, double A, B, the lower and upper truncation limits.
    //
    //    Input/output, int &SEED, a seed for the random number
    //    generator.
    //
    //    Output, double NORMAL_TRUNCATED_AB_SAMPLE, a sample of the PDF.
    //
    {
        double alpha = (a - mu) / s;
        double beta  = (b - mu) / s;

        double alpha_cdf = CDF.normal_01_cdf(alpha);
        double beta_cdf  = CDF.normal_01_cdf(beta);

        double u      = UniformRNG.r8_uniform_01(ref seed);
        double xi_cdf = alpha_cdf + u * (beta_cdf - alpha_cdf);
        double xi     = CDF.normal_01_cdf_inv(xi_cdf);

        double x = mu + s * xi;

        return(x);
    }
Beispiel #4
0
    public static double normal_truncated_ab_pdf(double x, double mu, double s, double a,
                                                 double b)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    NORMAL_TRUNCATED_AB_PDF evaluates the truncated Normal PDF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    14 August 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, double X, the argument of the PDF.
    //
    //    Input, double MU, S, the mean and standard deviation of the
    //    parent Normal distribution.
    //
    //    Input, double A, B, the lower and upper truncation limits.
    //
    //    Output, double NORMAL_TRUNCATED_AB_PDF, the value of the PDF.
    //
    {
        double alpha = (a - mu) / s;
        double beta  = (b - mu) / s;
        double xi    = (x - mu) / s;

        double alpha_cdf = CDF.normal_01_cdf(alpha);
        double beta_cdf  = CDF.normal_01_cdf(beta);
        double xi_pdf    = Normal.normal_01_pdf(xi);

        double pdf = xi_pdf / (beta_cdf - alpha_cdf) / s;

        return(pdf);
    }
Beispiel #5
0
    public static double normal_truncated_ab_variance(double mu, double s, double a, double b)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    NORMAL_TRUNCATED_AB_VARIANCE returns the variance of the truncated Normal PDF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    14 August 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, double MU, S, the mean and standard deviation of the
    //    parent Normal distribution.
    //
    //    Input, double A, B, the lower and upper truncation limits.
    //
    //    Output, double NORMAL_TRUNCATED_AB_VARIANCE, the variance of the PDF.
    //
    {
        double alpha = (a - mu) / s;
        double beta  = (b - mu) / s;

        double alpha_pdf = Normal.normal_01_pdf(alpha);
        double beta_pdf  = Normal.normal_01_pdf(beta);

        double alpha_cdf = CDF.normal_01_cdf(alpha);
        double beta_cdf  = CDF.normal_01_cdf(beta);

        double variance = s * s * (1.0
                                   + (alpha * alpha_pdf - beta * beta_pdf) / (beta_cdf - alpha_cdf)
                                   - Math.Pow((alpha_pdf - beta_pdf) / (beta_cdf - alpha_cdf), 2));

        return(variance);
    }
Beispiel #6
0
    public static double normal_truncated_b_mean(double mu, double s, double b)
    //****************************************************************************80
    //
    //  Purpose:
    //
    //    NORMAL_TRUNCATED_B_MEAN returns the mean of the upper truncated Normal PDF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    21 August 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, double MU, S, the mean and standard deviatione of the
    //    parent Normal distribution.
    //
    //    Input, double B, the upper truncation limit.
    //
    //    Output, double NORMAL_TRUNCATED_B_MEAN, the mean of the PDF.
    //
    {
        double beta = (b - mu) / s;

        double beta_cdf = CDF.normal_01_cdf(beta);

        double beta_pdf = Normal.normal_01_pdf(beta);

        double mean = mu - s * beta_pdf / beta_cdf;

        return(mean);
    }
Beispiel #7
0
    public static double normal_truncated_b_moment(int order, double mu, double sigma, double b)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TRUNCATED_NORMAL_B_MOMENT: moments of the upper truncated Normal PDF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 September 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Phoebus Dhrymes,
    //    Moments of Truncated Normal Distributions,
    //    May 2005.
    //
    //  Parameters:
    //
    //    Input, int ORDER, the order of the moment.
    //    0 <= ORDER.
    //
    //    Input, double MU, SIGMA, the mean and standard deviation of the
    //    parent Normal distribution.
    //
    //    Input, double B, the upper truncation limit.
    //
    //    Output, double TRUNCATED_NORMAL_B_MOMENT, the moment of the PDF.
    //
    {
        int r;

        switch (order)
        {
        case < 0:
            Console.WriteLine("");
            Console.WriteLine("TRUNCATED_NORMAL_B_MOMENT - Fatal error!");
            Console.WriteLine("  ORDER < 0.");
            return(1);
        }

        double h     = (b - mu) / sigma;
        double h_pdf = Normal.normal_01_pdf(h);
        double h_cdf = CDF.normal_01_cdf(h);

        switch (h_cdf)
        {
        case 0.0:
            Console.WriteLine("");
            Console.WriteLine("TRUNCATED_NORMAL_B_MOMENT - Fatal error!");
            Console.WriteLine("  CDF((B-MU)/SIGMA) = 0.");
            return(1);
        }

        double f = h_pdf / h_cdf;

        double moment = 0.0;
        double irm2   = 0.0;
        double irm1   = 0.0;

        for (r = 0; r <= order; r++)
        {
            double ir = r switch
            {
                0 => 1.0,
                1 => - f,
                _ => - Math.Pow(h, r - 1) * f + (r - 1) * irm2
            };

            moment += typeMethods.r8_choose(order, r) * Math.Pow(mu, order - r)
                      * Math.Pow(sigma, r) * ir;

            irm2 = irm1;
            irm1 = ir;
        }

        return(moment);
    }
Beispiel #8
0
    public static double normal_truncated_ab_moment(int order, double mu, double sigma,
                                                    double a, double b)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TRUNCATED_NORMAL_AB_MOMENT: moments of the truncated Normal PDF.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 September 2013
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Phoebus Dhrymes,
    //    Moments of Truncated Normal Distributions,
    //    May 2005.
    //
    //  Parameters:
    //
    //    Input, int ORDER, the order of the moment.
    //    0 <= ORDER.
    //
    //    Input, double MU, SIGMA, the mean and standard deviation of the
    //    parent Normal distribution.
    //    0.0 < S.
    //
    //    Input, double A, B, the lower and upper truncation limits.
    //    A < B.
    //
    //    Output, double TRUNCATED_NORMAL_AB_MOMENT, the moment of the PDF.
    //
    {
        int r;

        switch (order)
        {
        case < 0:
            Console.WriteLine("");
            Console.WriteLine("TRUNCATED_NORMAL_AB_MOMENT - Fatal error!");
            Console.WriteLine("  ORDER < 0.");
            return(1);
        }

        switch (sigma)
        {
        case <= 0.0:
            Console.WriteLine("");
            Console.WriteLine("TRUNCATED_NORMAL_AB_MOMENT - Fatal error!");
            Console.WriteLine("  SIGMA <= 0.0.");
            return(1);
        }

        if (b <= a)
        {
            Console.WriteLine("");
            Console.WriteLine("TRUNCATED_NORMAL_AB_MOMENT - Fatal error!");
            Console.WriteLine("  B <= A.");
            return(1);
        }

        double a_h   = (a - mu) / sigma;
        double a_pdf = Normal.normal_01_pdf(a_h);
        double a_cdf = CDF.normal_01_cdf(a_h);

        switch (a_cdf)
        {
        case 0.0:
            Console.WriteLine("");
            Console.WriteLine("TRUNCATED_NORMAL_AB_MOMENT - Fatal error!");
            Console.WriteLine("  PDF/CDF ratio fails, because A_CDF too small.");
            Console.WriteLine("  A_PDF = " + a_pdf + "");
            Console.WriteLine("  A_CDF = " + a_cdf + "");
            return(1);
        }

        double b_h   = (b - mu) / sigma;
        double b_pdf = Normal.normal_01_pdf(b_h);
        double b_cdf = CDF.normal_01_cdf(b_h);

        switch (b_cdf)
        {
        case 0.0:
            Console.WriteLine("");
            Console.WriteLine("TRUNCATED_NORMAL_AB_MOMENT - Fatal error!");
            Console.WriteLine("  PDF/CDF ratio fails, because B_CDF too small.");
            Console.WriteLine("  B_PDF = " + b_pdf + "");
            Console.WriteLine("  B_CDF = " + b_cdf + "");
            return(1);
        }

        double moment = 0.0;
        double irm2   = 0.0;
        double irm1   = 0.0;

        for (r = 0; r <= order; r++)
        {
            double ir = r switch
            {
                0 => 1.0,
                1 => - (b_pdf - a_pdf) / (b_cdf - a_cdf),
                _ => (r - 1) * irm2 - (Math.Pow(b_h, r - 1) * b_pdf - Math.Pow(a_h, r - 1) * a_pdf) / (b_cdf - a_cdf)
            };

            moment += typeMethods.r8_choose(order, r) * Math.Pow(mu, order - r)
                      * Math.Pow(sigma, r) * ir;

            irm2 = irm1;
            irm1 = ir;
        }

        return(moment);
    }