truncated_normal Function

private function truncated_normal(mean, std_dev, xl, xu)

Truncated normal distribution within bounds [xl, xu]. Uses rejection sampling to ensure the value stays within bounds.

Note

For efficiency, if bounds are more than a few standard deviations from the mean, consider using uniform distribution instead.

Arguments

Type IntentOptional 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

Return Value real(kind=wp)


Calls

proc~~truncated_normal~~CallsGraph proc~truncated_normal truncated_normal proc~normal normal proc~truncated_normal->proc~normal proc~uniform uniform proc~truncated_normal->proc~uniform proc~uniform_random_number uniform_random_number proc~normal->proc~uniform_random_number proc~uniform->proc~uniform_random_number

Called by

proc~~truncated_normal~~CalledByGraph proc~truncated_normal truncated_normal proc~perturb_variable simulated_annealing_type%perturb_variable proc~perturb_variable->proc~truncated_normal proc~perturb_and_evaluate simulated_annealing_type%perturb_and_evaluate proc~perturb_and_evaluate->proc~perturb_variable proc~sa simulated_annealing_type%sa proc~sa->proc~perturb_and_evaluate proc~solve_simulated_annealing solve_simulated_annealing proc~solve_simulated_annealing->proc~sa

Source Code

   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