#! /usr/bin/env python # def log_normal_truncated_ab_cdf ( x, mu, sigma, a, b ): #*****************************************************************************80 # ## LOG_NORMAL_TRUNCATED_AB_CDF evaluates the truncated AB Log Normal CDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real X, the argument of the PDF. # 0.0 < X. # # Input, real MU, SIGMA, the parameters of the log normal PDF. # 0.0 < SIGMA. # # Input, real A, B, the lower and upper truncation limits. # A < B. # # Output, real CDF, the value of the CDF. # from log_normal import log_normal_cdf from sys import exit check = log_normal_truncated_ab_check ( mu, sigma, a, b ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_CDF - Fatal error!' ) print ( ' Parameters are not legal.' ) exit ( 'LOG_NORMAL_TRUNCATED_AB_CDF - Fatal error!' ) if ( x <= a ): cdf = 0.0 elif ( b <= x ): cdf = 1.0 else: lncdf_a = log_normal_cdf ( a, mu, sigma ) lncdf_b = log_normal_cdf ( b, mu, sigma ) lncdf_x = log_normal_cdf ( x, mu, sigma ) cdf = ( lncdf_x - lncdf_a ) / ( lncdf_b - lncdf_a ) return cdf def log_normal_truncated_ab_cdf_inv ( cdf, mu, sigma, a, b ): #*****************************************************************************80 # ## LOG_NORMAL_TRUNCATED_AB_CDF_INV inverts the truncated AB Log Normal CDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real CDF, the value of the CDF. # 0 < CDF < 1. # # Input, real MU, SIGMA, the parameters of the log normal PDF. # 0.0 < SIGMA. # # Input, real A, B, the lower and upper truncation limits. # A < B. # # Output, real X, the argument of the PDF with the given CDF. # from log_normal import log_normal_cdf from log_normal import log_normal_cdf_inv from sys import exit check = log_normal_truncated_ab_check ( mu, sigma, a, b ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_CDF_INV - Fatal error!' ) print ( ' Parameters are not legal.' ) exit ( 'LOG_NORMAL_TRUNCATED_AB_CDF_INV - Fatal error!' ) if ( cdf <= 0.0 ): x = a elif ( 1.0 <= cdf ): x = b else: lncdf_a = log_normal_cdf ( a, mu, sigma ) lncdf_b = log_normal_cdf ( b, mu, sigma ) lncdf_x = lncdf_a + cdf * ( lncdf_b - lncdf_a ) x = log_normal_cdf_inv ( lncdf_x, mu, sigma ) return x def log_normal_truncated_ab_cdf_test ( ): #*****************************************************************************80 # ## LOG_NORMAL_TRUNCATED_AB_CDF_TEST tests LOG_NORMAL_TRUNCATED_AB_CDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 March 2016 # # Author: # # John Burkardt # import numpy as np import platform from sys import exit print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_CDF_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' LOG_NORMAL_TRUNCATED_AB_CDF evaluates the Log Normal Truncated AB CDF' ) print ( ' LOG_NORMAL_TRUNCATED_AB_CDF_INV inverts the Log Normal Truncated AB CDF.' ) print ( ' LOG_NORMAL_TRUNCATED_AB_PDF evaluates the Log Normal Truncated AB PDF' ) mu = 0.5 sigma = 3.0 a = np.exp ( mu ) b = np.exp ( mu + 2.0 * sigma ) check = log_normal_truncated_ab_check ( mu, sigma, a, b ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_CDF_TEST - Fatal error!' ) print ( ' The parameters are not legal.' ) exit ( 'LOG_NORMAL_TRUNCATED_AB_CDF_TEST - Fatal error!' ) print ( '' ) print ( ' PDF parameter MU = %14g' % ( mu ) ) print ( ' PDF parameter SIGMA = %14g' % ( sigma ) ) print ( ' PDF parameter A = %14g' % ( a ) ) print ( ' PDF parameter B = %14g' % ( b ) ) seed = 123456789 print ( '' ) print ( ' X PDF CDF CDF_INV' ) print ( '' ) for i in range ( 0, 10 ): x, seed = log_normal_truncated_ab_sample ( mu, sigma, a, b, seed ) pdf = log_normal_truncated_ab_pdf ( x, mu, sigma, a, b ) cdf = log_normal_truncated_ab_cdf ( x, mu, sigma, a, b ) x2 = log_normal_truncated_ab_cdf_inv ( cdf, mu, sigma, a, b ) print ( ' %14.6g %14g %14g %14g' % ( x, pdf, cdf, x2 ) ) # # Terminate. # print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_CDF_TEST' ) print ( ' Normal end of execution.' ) return def log_normal_truncated_ab_check ( mu, sigma, a, b ): #*****************************************************************************80 # ## LOG_NORMAL_TRUNCATED_AB_CHECK checks the truncated normal AB Log Normal PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the parameters of the log normal PDF. # 0.0 < SIGMA. # # Input, real A, B, the lower and upper truncation limits. # A < B. # # Output, logical CHECK, is true if the parameters are legal. # check = True if ( sigma <= 0.0 ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_CHECK - Fatal error!' ) print ( ' SIGMA <= 0.' ) check = False if ( b <= a ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_CHECK - Fatal error!' ) print ( ' B <= A.' ) check = False return check def log_normal_truncated_ab_mean ( mu, sigma, a, b ): #*****************************************************************************80 # ## LOG_NORMAL_TRUNCATED_AB_MEAN returns the mean of the Log Normal Truncated AB PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the parameters of the log normal PDF. # 0.0 < SIGMA. # # Input, real A, B, the lower and upper truncation limits. # A < B. # # Output, real MEAN, the mean of the PDF. # import numpy as np from normal_01 import normal_01_cdf from sys import exit check = log_normal_truncated_ab_check ( mu, sigma, a, b ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_MEAN - Fatal error!' ) print ( ' Parameters are not legal.' ) exit ( 'LOG_NORMAL_TRUNCATED_AB_MEAN - Fatal error!' ) a0 = ( np.log ( a ) - mu ) / sigma b0 = ( np.log ( b ) - mu ) / sigma c1 = normal_01_cdf ( sigma - a0 ) c2 = normal_01_cdf ( sigma - b0 ) c3 = normal_01_cdf ( + a0 ) c4 = normal_01_cdf ( + b0 ) ln_mean = np.exp ( mu + 0.5 * sigma ** 2 ) mean = ln_mean * ( c1 - c2 ) / ( c4 - c3 ) return mean def log_normal_truncated_ab_pdf ( x, mu, sigma, a, b ): #*****************************************************************************80 # ## LOG_NORMAL_TRUNCATED_AB_PDF evaluates the truncated AB Log Normal PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real X, the argument of the PDF. # 0.0 < X. # # Input, real MU, SIGMA, the parameters of the log normal PDF. # 0.0 < SIGMA. # # Input, real A, B, the lower and upper truncation limits. # A < B. # # Output, real PDF, the value of the PDF. # from log_normal import log_normal_cdf from log_normal import log_normal_pdf from sys import exit check = log_normal_truncated_ab_check ( mu, sigma, a, b ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_PDF - Fatal error!' ) print ( ' Parameters are not legal.' ) exit ( 'LOG_NORMAL_TRUNCATED_AB_PDF - Fatal error!' ) if ( x <= a ): pdf = 0.0 elif ( b <= x ): pdf = 0.0 else: lncdf_a = log_normal_cdf ( a, mu, sigma ) lncdf_b = log_normal_cdf ( b, mu, sigma ) lnpdf_x = log_normal_pdf ( x, mu, sigma ) pdf = lnpdf_x / ( lncdf_b - lncdf_a ) return pdf def log_normal_truncated_ab_sample ( mu, sigma, a, b, seed ): #*****************************************************************************80 # ## LOG_NORMAL_TRUNCATED_AB_SAMPLE samples the truncated AB Log Normal CDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the parameters of the log normal PDF. # 0.0 < SIGMA. # # Input, real A, B, the lower and upper truncation limits. # A < B. # # Input/output, integer SEED, a seed for the random number generator. # # Output, real X, a random sample # from log_normal import log_normal_cdf from log_normal import log_normal_cdf_inv from r8_uniform_ab import r8_uniform_ab from sys import exit check = log_normal_truncated_ab_check ( mu, sigma, a, b ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_SAMPLE - Fatal error!' ) print ( ' Parameters are not legal.' ) exit ( 'LOG_NORMAL_TRUNCATED_AB_SAMPLE - Fatal error!' ) lncdf_a = log_normal_cdf ( a, mu, sigma ) lncdf_b = log_normal_cdf ( b, mu, sigma ) cdf, seed = r8_uniform_ab ( lncdf_a, lncdf_b, seed ) x = log_normal_cdf_inv ( cdf, mu, sigma ) return x, seed def log_normal_truncated_ab_sample_test ( ): #*****************************************************************************80 # ## LOG_NORMAL_TRUNCATED_AB_SAMPLE_TEST tests LOG_NORMAL_TRUNCATED_AB_SAMPLE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 March 2016 # # Author: # # John Burkardt # import numpy as np import platform from r8vec_max import r8vec_max from r8vec_mean import r8vec_mean from r8vec_min import r8vec_min from r8vec_variance import r8vec_variance from sys import exit nsample = 1000 seed = 123456789 print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_SAMPLE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' LOG_NORMAL_TRUNCATED_AB_MEAN computes the Log Normal Truncated AB mean' ) print ( ' LOG_NORMAL_TRUNCATED_AB_SAMPLE samples the Log Normal Truncated AB distribution' ) print ( ' LOG_NORMAL_TRUNCATED_AB_VARIANCE computes the Log Normal Truncated AB variance.' ) mu = 0.5 sigma = 3.0 a = np.exp ( mu ) b = np.exp ( mu + 2.0 * sigma ) print ( '' ) print ( ' PDF parameter MU = %14g' % ( mu ) ) print ( ' PDF parameter SIGMA = %14g' % ( sigma ) ) print ( ' PDF parameter A = %14g' % ( a ) ) print ( ' PDF parameter B = %14g' % ( b ) ) check = log_normal_truncated_ab_check ( mu, sigma, a, b ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_SAMPLE_TEST - Fatal error!' ) print ( ' The parameters are not legal.' ) exit ( 'LOG_NORMAL_TRUNCATED_AB_SAMPLE_TEST - Fatal error!' ) mean = log_normal_truncated_ab_mean ( mu, sigma, a, b ) variance = log_normal_truncated_ab_variance ( mu, sigma, a, b ) print ( '' ) print ( ' PDF mean = %14g' % ( mean ) ) print ( ' PDF variance = %14g' % ( variance ) ) x = np.zeros ( nsample ) for i in range ( 0, nsample ): x[i], seed = log_normal_truncated_ab_sample ( mu, sigma, a, b, seed ) mean = r8vec_mean ( nsample, x ) variance = r8vec_variance ( nsample, x ) xmax = r8vec_max ( nsample, x ) xmin = r8vec_min ( nsample, x ) print ( '' ) print ( ' Sample size = %6d' % ( nsample ) ) print ( ' Sample mean = %14g' % ( mean ) ) print ( ' Sample variance = %14g' % ( variance ) ) print ( ' Sample maximum = %14g' % ( xmax ) ) print ( ' Sample minimum = %14g' % ( xmin ) ) # # Terminate. # print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_SAMPLE_TEST' ) print ( ' Normal end of execution.' ) return def log_normal_truncated_ab_variance ( mu, sigma, a, b ): #*****************************************************************************80 # ## LOG_NORMAL_TRUNCATED_AB_VARIANCE: variance of the Log Normal Truncated AB PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the parameters of the log normal PDF. # 0.0 < SIGMA. # # Input, real A, B, the lower and upper truncation limits. # A < B. # # Output, real VARIANCE, the mean of the PDF. # import numpy as np from normal_01 import normal_01_cdf from sys import exit check = log_normal_truncated_ab_check ( mu, sigma, a, b ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_TRUNCATED_AB_VARIANCE - Fatal error!' ) print ( ' Parameters are not legal.' ) exit ( 'LOG_NORMAL_TRUNCATED_AB_VARIANCE - Fatal error!' ) mean = log_normal_truncated_ab_mean ( mu, sigma, a, b ) a0 = ( np.log ( a ) - mu ) / sigma b0 = ( np.log ( b ) - mu ) / sigma c1 = normal_01_cdf ( 2.0 * sigma - a0 ) c2 = normal_01_cdf ( 2.0 * sigma - b0 ) c3 = normal_01_cdf ( + a0 ) c4 = normal_01_cdf ( + b0 ) ln_xsquared = np.exp ( 2.0 * mu + 2.0 * sigma ** 2 ) lntab_xsquared = ln_xsquared * ( c1 - c2 ) / ( c4 - c3 ) variance = lntab_xsquared - mean ** 2 return variance if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) log_normal_truncated_ab_cdf_test ( ) log_normal_truncated_ab_sample_test ( ) timestamp ( )