Truncated normal distribution within bounds [xl, xu]. Uses rejection sampling to ensure the value stays within bounds.
For efficiency, if bounds are more than a few standard deviations from the mean, consider using uniform distribution instead.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in) | :: | mean |
mean of the underlying normal distribution |
||
| real(kind=wp), | intent(in) | :: | std_dev |
standard deviation |
||
| real(kind=wp), | intent(in) | :: | xl |
lower bound |
||
| real(kind=wp), | intent(in) | :: | xu |
upper bound |
function truncated_normal(mean, std_dev, xl, xu) real(wp),intent(in) :: mean !! mean of the underlying normal distribution real(wp),intent(in) :: std_dev !! standard deviation real(wp),intent(in) :: xl !! lower bound real(wp),intent(in) :: xu !! upper bound real(wp) :: truncated_normal integer,parameter :: max_tries = 1000 integer :: i ! rejection sampling do i = 1, max_tries truncated_normal = normal(mean, std_dev) if (truncated_normal >= xl .and. truncated_normal <= xu) return end do ! fallback to uniform if rejection sampling fails truncated_normal = uniform(xl, xu) end function truncated_normal