Exemplo n.º 1
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_cdf_inv ( cdf, mu, sigma, a, b ):

#*****************************************************************************80
#
## TRUNCATED_NORMAL_AB_CDF_INV inverts the truncated Normal CDF.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    09 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, real CDF, the value of the CDF.
#    0.0 <= CDF <= 1.0.
#
#    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 X, the corresponding argument.
#
  from normal_01_cdf import normal_01_cdf
  from normal_01_cdf_inv import normal_01_cdf_inv
  from sys import exit

  if ( cdf < 0.0 or 1.0 < cdf ):
    print ( '' )
    print ( 'TRUNCATED_NORMAL_AB_CDF_INV - Fatal error!' )
    print ( '  CDF < 0 or 1 < CDF.' )
    exit ( 'TRUNCATED_NORMAL_AB_CDF_INV - Fatal error!' )

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

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

  xi_cdf = ( beta_cdf - alpha_cdf ) * cdf + alpha_cdf
  xi = normal_01_cdf_inv ( xi_cdf )

  x = mu + sigma * xi

  return x
def truncated_normal_ab_sample ( mu, sigma, a, b, seed ):

#*****************************************************************************80
#
## TRUNCATED_NORMAL_AB_SAMPLE samples 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 mean and standard deviation of the
#    parent Normal distribution.
#
#    Input, real A, B, the lower and upper truncation limits.
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, real X, a sample of the PDF.
#
#    Output, integer SEED, a seed for the random number generator.
#
  from normal_01_cdf import normal_01_cdf
  from normal_01_cdf_inv import normal_01_cdf_inv
  from r8_uniform_01 import r8_uniform_01

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

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

  u, seed = r8_uniform_01 ( seed )
  xi_cdf = alpha_cdf + u * ( beta_cdf - alpha_cdf )
  xi = normal_01_cdf_inv ( xi_cdf )

  x = mu + sigma * xi

  return x, seed
def truncated_normal_ab_sample(mu, sigma, a, b, seed):

    #*****************************************************************************80
    #
    ## TRUNCATED_NORMAL_AB_SAMPLE samples 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 mean and standard deviation of the
    #    parent Normal distribution.
    #
    #    Input, real A, B, the lower and upper truncation limits.
    #
    #    Input, integer SEED, a seed for the random number generator.
    #
    #    Output, real X, a sample of the PDF.
    #
    #    Output, integer SEED, a seed for the random number generator.
    #
    from normal_01_cdf import normal_01_cdf
    from normal_01_cdf_inv import normal_01_cdf_inv
    from r8_uniform_01 import r8_uniform_01

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

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

    u, seed = r8_uniform_01(seed)
    xi_cdf = alpha_cdf + u * (beta_cdf - alpha_cdf)
    xi = normal_01_cdf_inv(xi_cdf)

    x = mu + sigma * xi

    return x, seed
Exemplo n.º 5
0
def normal_ms_cdf ( x, mu, sigma ):

#*****************************************************************************80
#
## NORMAL_MS_CDF evaluates the CDF of the Normal MS distribution.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    05 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, real X, the argument of the CDF.
#
#    Input, real MU, SIGMA, the parameters of the PDF.
#    0.0 < SIGMA.
#
#    Output, real VALUE, the value of the CDF.
#
  from normal_01_cdf import normal_01_cdf

  y = ( x - mu ) / sigma

  value = normal_01_cdf ( y )

  return value
Exemplo n.º 6
0
def normal_ms_cdf(x, mu, sigma):

    #*****************************************************************************80
    #
    ## NORMAL_MS_CDF evaluates the CDF of the Normal MS distribution.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    05 March 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, real X, the argument of the CDF.
    #
    #    Input, real MU, SIGMA, the parameters of the PDF.
    #    0.0 < SIGMA.
    #
    #    Output, real VALUE, the value of the CDF.
    #
    from normal_01_cdf import normal_01_cdf

    y = (x - mu) / sigma

    value = normal_01_cdf(y)

    return value
def truncated_normal_b_cdf(x, mu, sigma, b):

    #*****************************************************************************80
    #
    ## TRUNCATED_NORMAL_B_CDF evaluates the upper Truncated Normal CDF.
    #
    #  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 CDF.
    #
    #    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 CDF.
    #
    from normal_01_cdf import normal_01_cdf

    if (x <= b):

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

        alpha_cdf = 0.0
        beta_cdf = normal_01_cdf(beta)
        xi_cdf = normal_01_cdf(xi)

        value = (xi_cdf - alpha_cdf) / (beta_cdf - alpha_cdf)

    else:

        value = 1.0

    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
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
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
Exemplo n.º 11
0
def truncated_normal_a_cdf ( x, mu, sigma, a ):

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

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

  alpha_cdf = normal_01_cdf ( alpha )
  beta_cdf = 1.0
  xi_cdf = normal_01_cdf ( xi )

  value = ( xi_cdf - alpha_cdf ) / ( beta_cdf - alpha_cdf )

  return value
def truncated_normal_b_cdf ( x, mu, sigma, b ):

#*****************************************************************************80
#
## TRUNCATED_NORMAL_B_CDF evaluates the upper Truncated Normal CDF.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    08 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, real X, the argument of the CDF.
#
#    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 CDF.
# 
  from normal_01_cdf import normal_01_cdf

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

  alpha_cdf = 0.0
  beta_cdf = normal_01_cdf ( beta )
  xi_cdf = normal_01_cdf ( xi )

  value = ( xi_cdf - alpha_cdf ) / ( beta_cdf - alpha_cdf )

  return value
def truncated_normal_a_cdf_inv ( cdf, mu, sigma, a ):

#*****************************************************************************80
#
## TRUNCATED_NORMAL_A_CDF_INV inverts the lower truncated Normal CDF.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    09 March 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, real CDF, the value of the CDF.
#    0.0 <= CDF <= 1.0.
#
#    Input, real MU, SIGMA, the mean and standard deviation of the
#    parent Normal distribution.
#
#    Input, real A, the lower truncation limit.
#
#    Output, real X, the corresponding argument.
#
  from normal_01_cdf import normal_01_cdf
  from normal_01_cdf_inv import normal_01_cdf_inv
  from sys import exit

  if ( cdf < 0.0 or 1.0 < cdf ):
    print ''
    print 'TRUNCATED_NORMAL_A_CDF_INV - Fatal error!'
    print '  CDF < 0 or 1 < CDF.'
    exit ( 'TRUNCATED_NORMAL_A_CDF_INV - Fatal error!' )

  alpha = ( a - mu ) / sigma

  alpha_cdf = normal_01_cdf ( alpha )
  beta_cdf = 1.0

  xi_cdf = ( beta_cdf - alpha_cdf ) * cdf + alpha_cdf
  xi = normal_01_cdf_inv ( xi_cdf )

  x = mu + sigma * xi

  return x
Exemplo n.º 14
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
Exemplo n.º 16
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