#! /usr/bin/env python # def log_normal_cdf ( x, mu, sigma ): #*****************************************************************************80 # ## LOG_NORMAL_CDF evaluates the Log Normal CDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 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 PDF. # 0.0 < SIGMA. # # Output, real CDF, the value of the CDF. # import numpy as np from normal import normal_cdf if ( x <= 0.0 ): cdf = 0.0 else: logx = np.log ( x ) cdf = normal_cdf ( logx, mu, sigma ) return cdf def log_normal_cdf_inv ( cdf, mu, sigma ): #*****************************************************************************80 # ## LOG_NORMAL_CDF_INV inverts the Log Normal CDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real CDF, the value of the CDF. # 0.0 <= CDF <= 1.0. # # Input, real MU, SIGMA, the parameters of the PDF. # 0.0 < SIGMA. # # Input, real X, the corresponding argument. # import numpy as np from normal import normal_cdf_inv from sys import exit if ( cdf < 0.0 or 1.0 < cdf ): print ( '' ) print ( 'LOG_NORMAL_CDF_INV - Fatal error!' ) print ( ' CDF < 0 or 1 < CDF.' ) exit ( 'LOG_NORMAL_CDF_INV - Fatal error!' ) logx = normal_cdf_inv ( cdf, mu, sigma ) x = np.exp ( logx ) return x def log_normal_cdf_test ( ): #*****************************************************************************80 # ## LOG_NORMAL_CDF_TEST tests LOG_NORMAL_CDF, LOG_NORMAL_CDF_INV, LOG_NORMAL_PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 March 2016 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'LOG_NORMAL_CDF_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' LOG_NORMAL_CDF evaluates the Log Normal CDF' ) print ( ' LOG_NORMAL_CDF_INV inverts the Log Normal CDF.' ) print ( ' LOG_NORMAL_PDF evaluates the Log Normal PDF' ) mu = 1.0 sigma = 0.5 check = log_normal_check ( mu, sigma ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_CDF_TEST - Fatal error!' ) print ( ' The parameters are not legal.' ) return print ( '' ) print ( ' PDF parameter MU = %14g' % ( mu ) ) print ( ' PDF parameter SIGMA = %14g' % ( sigma ) ) seed = 123456789 print ( '' ) print ( ' X PDF CDF CDF_INV' ) print ( '' ) for i in range ( 0, 10 ): x, seed = log_normal_sample ( mu, sigma, seed ) pdf = log_normal_pdf ( x, mu, sigma ) cdf = log_normal_cdf ( x, mu, sigma ) x2 = log_normal_cdf_inv ( cdf, mu, sigma ) print ( ' %14g %14g %14g %14g' % ( x, pdf, cdf, x2 ) ) # # Terminate. # print ( '' ) print ( 'LOG_NORMAL_CDF_TEST' ) print ( ' Normal end of execution.' ) return def log_normal_check ( mu, sigma ): #*****************************************************************************80 # ## LOG_NORMAL_CHECK checks the parameters of the Log Normal PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the parameters of the PDF. # 0.0 < SIGMA. # # Output, logical CHECK, is true if the parameters are legal. # check = True if ( sigma <= 0.0 ): print ( '' ) print ( 'LOG_NORMAL_CHECK - Fatal error!' ) print ( ' B <= 0.' ) check = False return check def log_normal_mean ( mu, sigma ): #*****************************************************************************80 # ## LOG_NORMAL_MEAN returns the mean of the Log Normal PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the parameters of the PDF. # 0.0 < SIGMA. # # Output, real MEAN, the mean of the PDF. # import numpy as np mean = np.exp ( mu + 0.5 * sigma * sigma ) return mean def log_normal_pdf ( x, mu, sigma ): #*****************************************************************************80 # ## LOG_NORMAL_PDF evaluates the Log Normal PDF. # # Discussion: # # PDF(X)(A,B) # = EXP ( - 0.5 * ( ( LOG ( X ) - MU ) / SIGMA )^2 ) # / ( SIGMA * X * SQRT ( 2 * PI ) ) # # The Log Normal PDF is also known as the Cobb-Douglas PDF, # and as the Antilog_normal PDF. # # The Log Normal PDF describes a variable X whose logarithm # is normally distributed. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 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 PDF. # 0.0 < SIGMA. # # Output, real PDF, the value of the PDF. # import numpy as np if ( x <= 0.0 ): pdf = 0.0 else: pdf = np.exp ( - 0.5 * ( ( np.log ( x ) - mu ) / sigma ) ** 2 ) \ / ( sigma * x * np.sqrt ( 2.0 * np.pi ) ) return pdf def log_normal_sample ( mu, sigma, seed ): #*****************************************************************************80 # ## LOG_NORMAL_SAMPLE samples the Log Normal PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the parameters of the PDF. # 0.0 < SIGMA. # # Input, integer SEED, a seed for the random number generator. # # Output, real X, a sample of the PDF. # # Output, integer SEED, an updated seed for the random number generator. # from r8_uniform_01 import r8_uniform_01 cdf, seed = r8_uniform_01 ( seed ) x = log_normal_cdf_inv ( cdf, mu, sigma ) return x, seed def log_normal_sample_test ( ): #*****************************************************************************80 # ## LOG_NORMAL_SAMPLE_TEST tests LOG_NORMAL_SAMPLE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 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 nsample = 1000 seed = 123456789 print ( '' ) print ( 'LOG_NORMAL_SAMPLE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' LOG_NORMAL_MEAN computes the Log Normal mean' ) print ( ' LOG_NORMAL_SAMPLE samples the Log Normal distribution' ) print ( ' LOG_NORMAL_VARIANCE computes the Log Normal variance.' ) mu = 1.0 sigma = 0.5 check = log_normal_check ( mu, sigma ) if ( not check ): print ( '' ) print ( 'LOG_NORMAL_SAMPLE_TEST - Fatal error!' ) print ( ' The parameters are not legal.' ) return mean = log_normal_mean ( mu, sigma ) variance = log_normal_variance ( mu, sigma ) print ( '' ) print ( ' PDF parameter MU = %14g' % ( mu ) ) print ( ' PDF parameter SIGMA = %14g' % ( sigma ) ) 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_sample ( mu, sigma, 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_SAMPLE_TEST' ) print ( ' Normal end of execution.' ) return def log_normal_variance ( mu, sigma ): #*****************************************************************************80 # ## LOG_NORMAL_VARIANCE returns the variance of the Log Normal PDF. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 March 2016 # # Author: # # John Burkardt # # Parameters: # # Input, real MU, SIGMA, the parameters of the PDF. # 0.0 < SIGMA. # # Output, real VARIANCE, the variance of the PDF. # import numpy as np variance = np.exp ( 2.0 * mu + sigma * sigma ) \ * ( np.exp ( sigma * sigma ) - 1.0 ) return variance if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) log_normal_cdf_test ( ) log_normal_sample_test ( ) timestamp ( )