def truncated_normal_ab_variance(mu, sigma, a, b):

    #*****************************************************************************80
    #
    ## TRUNCATED_NORMAL_AB_VARIANCE: variance of the Truncated Normal distribution.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    08 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, real MU, SIGMA, the parameters of the PDF.
    #
    #    Input, real A, B, the lower and upper truncation limits.
    #
    #    Output, real VALUE, the variance of the PDF.
    #
    from normal_01_cdf import normal_01_cdf
    from normal_01_pdf import normal_01_pdf

    alpha = (a - mu) / sigma
    beta = (b - mu) / sigma

    alpha_pdf = normal_01_pdf(alpha)
    beta_pdf = normal_01_pdf(beta)

    alpha_cdf = normal_01_cdf(alpha)
    beta_cdf = normal_01_cdf(beta)

    value = sigma * sigma * ( 1.0 \
      + ( alpha * alpha_pdf - beta * beta_pdf ) / ( beta_cdf - alpha_cdf ) \
      - ( ( alpha_pdf - beta_pdf ) / ( beta_cdf - alpha_cdf ) ) ** 2 )

    return value
def truncated_normal_ab_variance ( mu, sigma, a, b ):

#*****************************************************************************80
#
## TRUNCATED_NORMAL_AB_VARIANCE: variance of the Truncated Normal distribution.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    08 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, real MU, SIGMA, the parameters of the PDF.
#
#    Input, real A, B, the lower and upper truncation limits.
#
#    Output, real VALUE, the variance of the PDF.
#
  from normal_01_cdf import normal_01_cdf
  from normal_01_pdf import normal_01_pdf

  alpha = ( a - mu ) / sigma
  beta = ( b - mu ) / sigma

  alpha_pdf = normal_01_pdf ( alpha )
  beta_pdf = normal_01_pdf ( beta )

  alpha_cdf = normal_01_cdf ( alpha )
  beta_cdf = normal_01_cdf ( beta )

  value = sigma * sigma * ( 1.0 \
    + ( alpha * alpha_pdf - beta * beta_pdf ) / ( beta_cdf - alpha_cdf ) \
    - ( ( alpha_pdf - beta_pdf ) / ( beta_cdf - alpha_cdf ) ) ** 2 )

  return value
示例#3
0
def truncated_normal_ab_pdf(x, mu, sigma, a, b):

    #*****************************************************************************80
    #
    ## TRUNCATED_NORMAL_AB_PDF evaluates the Truncated Normal PDF.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    24 January 2017
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, real X, the argument of the PDF.
    #
    #    Input, real MU, SIGMA, the mean and standard deviation of the
    #    parent Normal distribution.
    #
    #    Input, real A, B, the lower and upper truncation limits.
    #
    #    Output, real VALUE, the value of the PDF.
    #
    from normal_01_cdf import normal_01_cdf
    from normal_01_pdf import normal_01_pdf

    if (x < a):

        value = 0.0

    elif (x <= b):

        alpha = (a - mu) / sigma
        beta = (b - mu) / sigma
        xi = (x - mu) / sigma

        alpha_cdf = normal_01_cdf(alpha)
        beta_cdf = normal_01_cdf(beta)
        xi_pdf = normal_01_pdf(xi)

        value = xi_pdf / (beta_cdf - alpha_cdf) / sigma

    else:

        value = 0.0

    return value
def truncated_normal_ab_pdf ( x, mu, sigma, a, b ):

#*****************************************************************************80
#
## TRUNCATED_NORMAL_AB_PDF evaluates the Truncated Normal PDF.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    09 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, real X, the argument of the PDF.
#
#    Input, real MU, SIGMA, the mean and standard deviation of the
#    parent Normal distribution.
#
#    Input, real A, B, the lower and upper truncation limits.
#
#    Output, real VALUE, the value of the PDF.
# 
  from normal_01_cdf import normal_01_cdf
  from normal_01_pdf import normal_01_pdf

  alpha = ( a - mu ) / sigma
  beta = ( b - mu ) / sigma
  xi = ( x - mu ) / sigma

  alpha_cdf = normal_01_cdf ( alpha )
  beta_cdf = normal_01_cdf ( beta )
  xi_pdf = normal_01_pdf ( xi )

  value = xi_pdf / ( beta_cdf - alpha_cdf ) / sigma

  return value
示例#5
0
def truncated_normal_b_pdf(x, mu, sigma, b):

    #*****************************************************************************80
    #
    ## TRUNCATED_NORMAL_B_PDF evaluates the upper Truncated Normal PDF.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    09 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, real X, the argument of the PDF.
    #
    #    Input, real MU, SIGMA, the mean and standard deviation of the
    #    parent Normal distribution.
    #
    #    Input, real B, the upper truncation limit.
    #
    #    Output, real VALUE, the value of the PDF.
    #
    from normal_01_cdf import normal_01_cdf
    from normal_01_pdf import normal_01_pdf

    beta = (b - mu) / sigma
    xi = (x - mu) / sigma

    alpha_cdf = 0.0
    beta_cdf = normal_01_cdf(beta)
    xi_pdf = normal_01_pdf(xi)

    value = xi_pdf / (beta_cdf - alpha_cdf) / sigma

    return value
示例#6
0
def truncated_normal_a_mean(mu, sigma, a):

    #*****************************************************************************80
    #
    ## TRUNCATED_NORMAL_A_MEAN returns the mean of the lower Truncated Normal distribution.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    08 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, real MU, SIGMA, the parameters of the parent Normal Distribution.
    #
    #    Input, real A, the lower truncation limit.
    #
    #    Output, real VALUE, the mean of the PDF.
    #
    from normal_01_cdf import normal_01_cdf
    from normal_01_pdf import normal_01_pdf

    alpha = (a - mu) / sigma

    alpha_cdf = normal_01_cdf(alpha)
    beta_cdf = 1.0

    alpha_pdf = normal_01_pdf(alpha)
    beta_pdf = 0.0

    value = mu + sigma * (alpha_pdf - beta_pdf) / (beta_cdf - alpha_cdf)

    return value
def truncated_normal_b_mean ( mu, sigma, b ):

#*****************************************************************************80
#
## TRUNCATED_NORMAL_B_MEAN returns the mean of the upper Truncated Normal distribution.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    08 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, real MU, SIGMA, the parameters of the parent Normal Distribution.
#
#    Input, real B, the upper truncation limit.
#
#    Output, real VALUE, the mean of the PDF.
#
  from normal_01_cdf import normal_01_cdf
  from normal_01_pdf import normal_01_pdf

  beta = ( b - mu ) / sigma

  alpha_cdf = 0.0
  beta_cdf = normal_01_cdf ( beta )

  alpha_pdf = 0.0
  beta_pdf = normal_01_pdf ( beta )

  value = mu + sigma * ( alpha_pdf - beta_pdf ) / ( beta_cdf - alpha_cdf )

  return value
示例#8
0
def truncated_normal_b_moment(order, mu, sigma, b):

    #*****************************************************************************80
    #
    ## TRUNCATED_NORMAL_B_MOMENT: a moment of upper truncated Normal distribution.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    09 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    Phoebus Dhrymes,
    #    Moments of Truncated Normal Distributions,
    #    May 2005.
    #
    #  Parameters:
    #
    #    Input, integer ORDER, the order of the moment.
    #    0 <= ORDER.
    #
    #    Input, real MU, SIGMA, the mean and standard deviation of the
    #    parent Normal distribution.
    #    0 < S.
    #
    #    Input, real B, the upper truncation limit.
    #
    #    Output, real VALUE, the moment of the PDF.
    #
    from normal_01_cdf import normal_01_cdf
    from normal_01_pdf import normal_01_pdf
    from r8_choose import r8_choose
    from sys import exit

    if (order < 0):
        print ''
        print 'TRUNCATED_NORMAL_B_MOMENT - Fatal error!'
        print '  ORDER < 0.'
        exit('TRUNCATED_NORMAL_B_MOMENT - Fatal error!')

    if (sigma <= 0.0):
        print ''
        print 'TRUNCATED_NORMAL_B_MOMENT - Fatal error!'
        print '  SIGMA <= 0.0.'
        exit('TRUNCATED_NORMAL_B_MOMENT - Fatal error!')

    b_h = (b - mu) / sigma
    b_pdf = normal_01_pdf(b_h)
    b_cdf = normal_01_cdf(b_h)

    if (b_cdf == 0.0):
        print ''
        print 'TRUNCATED_NORMAL_B_MOMENT - Fatal error!'
        print '  PDF/CDF ratio fails, because B_CDF too small.'
        print '  B_PDF = %g' % (b_pdf)
        print '  B_CDF = %g' % (b_cdf)
        exit('TRUNCATED_NORMAL_B_MOMENT - Fatal error!')

    f = b_pdf / b_cdf

    value = 0.0
    irm2 = 0.0
    irm1 = 0.0

    for r in range(0, order + 1):

        if (r == 0):
            ir = 1.0
        elif (r == 1):
            ir = -f
        else:
            ir = -b_h**(r - 1) * f + (r - 1) * irm2

        value = value + r8_choose ( order, r ) \
          * mu ** ( order - r ) \
          * sigma ** r * ir

        irm2 = irm1
        irm1 = ir

    return value
def truncated_normal_ab_moment ( order, mu, sigma, a, b ):

#*****************************************************************************80
#
## TRUNCATED_NORMAL_AB_MOMENT: a moment of the truncated Normal distribution.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    09 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Phoebus Dhrymes,
#    Moments of Truncated Normal Distributions,
#    May 2005.
#
#  Parameters:
#
#    Input, integer ORDER, the order of the moment.
#    0 <= ORDER.
#
#    Input, real MU, SIGMA, the mean and standard deviation of the
#    parent Normal distribution.
#    0 < S.
#
#    Input, real A, B, the lower and upper truncation limits.
#    A < B.
#
#    Output, real VALUE, the moment of the PDF.
#
  from normal_01_cdf import normal_01_cdf
  from normal_01_pdf import normal_01_pdf
  from r8_choose import r8_choose
  from sys import exit

  if ( order < 0 ):
    print ''
    print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!'
    print '  ORDER < 0.'
    exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' )

  if ( sigma <= 0.0 ):
    print ''
    print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!'
    print '  SIGMA <= 0.0.'
    exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' )

  if ( b <= a ):
    print ''
    print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!'
    print '  B <= A.'
    exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' )

  a_h = ( a - mu ) / sigma
  a_pdf = normal_01_pdf ( a_h )
  a_cdf = normal_01_cdf ( a_h )

  if ( a_cdf == 0.0 ):
    print ''
    print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!'
    print '  PDF/CDF ratio fails, because A_CDF is too small.'
    print '  A_PDF = %g' % ( a_pdf )
    print '  A_CDF = %g' % ( a_cdf )
    exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' )

  b_h = ( b - mu ) / sigma
  b_pdf = normal_01_pdf ( b_h )
  b_cdf = normal_01_cdf ( b_h )

  if ( b_cdf == 0.0 ):
    print ''
    print 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!'
    print '  PDF/CDF ratio fails, because B_CDF too small.'
    print '  B_PDF = %g' % ( b_pdf )
    print '  B_CDF = %g' % ( b_cdf )
    exit ( 'TRUNCATED_NORMAL_AB_MOMENT - Fatal error!' )

  value = 0.0
  irm2 = 0.0
  irm1 = 0.0

  for r in range ( 0, order + 1 ):

    if ( r == 0 ):
      ir = 1.0
    elif ( r == 1 ):
      ir = - ( b_pdf - a_pdf ) / ( b_cdf - a_cdf )
    else:
      ir = ( r - 1 ) * irm2 \
        - ( b_h ** ( r - 1 ) * b_pdf - a_h ** ( r - 1 ) * a_pdf ) \
        / ( b_cdf - a_cdf )

    value = value + r8_choose ( order, r ) \
      * mu ** ( order - r ) \
      * sigma ** r * ir

    irm2 = irm1
    irm1 = ir

  return value