import numpy as np
from scipy.stats import uniform, truncnorm
[docs]
def normal_pdf(x, mean, std):
"""
Calculate the probability density function (PDF) of a normal distribution.
Parameters:
- x: Values at which to evaluate the PDF.
- mean: Mean (average) of the normal distribution.
- std: Standard deviation of the normal distribution.
Returns:
- pdf_values: PDF values for the given 'x'.
"""
x = np.array(x)
assert ~np.isnan(x).any(), 'X cannot have NaN'
assert ~np.isnan(mean), 'mean cannot have NaN'
assert ~np.isnan(std), 'std cannot have NaN'
assert std >= 0
return uniform.pdf(x, loc=mean, scale=std)
[docs]
def truncated_normal_pdf(x, mean, std, lower, upper):
"""
Calculate the probability density function (PDF) of a truncated normal distribution.
Parameters:
- x: Values at which to evaluate the PDF.
- mean: Mean (average) of the normal distribution.
- std: Standard deviation of the normal distribution.
- lower: Lower truncation point of the distribution.
- upper: Upper truncation point of the distribution.
Returns:
- pdf_values: PDF values for the given 'x' within the specified truncation range.
"""
x = np.array(x)
assert ~np.isnan(x).any()
assert upper >= lower, f'Upper limit ({lower}) must be >= lower limit ({upper})'
assert ~np.isnan(mean), 'mean cannot have NaN'
assert ~np.isnan(std), 'std cannot have NaN'
assert std >= 0
a = (lower - mean) / std
b = (upper + mean) / std
pdf_values = truncnorm.pdf(x, a, b, loc=mean, scale=std)
return pdf_values