f_sts_quantile_core Function

public pure function f_sts_quantile_core(x, p) result(q)

Computes the quantile value for a given dataset (array). Uses Hyndman & Fan (1996) type 7 percentile definition:

where k = floor(h), and f = h - k.

NOTE: Similar to quantile function for uniform distribution, but uses discrete index mapping.

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in) :: x(:)

data vector (assumed size array)

real(kind=wp), intent(in) :: p

desired percentile

Return Value real(kind=wp)

quantile value


Calls

proc~~f_sts_quantile_core~~CallsGraph proc~f_sts_quantile_core f_sts_quantile_core proc~s_utl_sort s_utl_sort proc~f_sts_quantile_core->proc~s_utl_sort proc~f_utl_assign_nan f_utl_assign_nan proc~s_utl_sort->proc~f_utl_assign_nan

Called by

proc~~f_sts_quantile_core~~CalledByGraph proc~f_sts_quantile_core f_sts_quantile_core proc~f_sts_quantile f_sts_quantile proc~f_sts_quantile->proc~f_sts_quantile_core interface~fsml_quantile fsml_quantile interface~fsml_quantile->proc~f_sts_quantile

Source Code

pure function f_sts_quantile_core(x, p) result(q)

! ==== Description
!! Computes the quantile value for a given dataset (array).
!! Uses Hyndman & Fan (1996) type 7 percentile definition:
!!
!! $$ h = (n - 1) \cdot p + 1 $$
!! $$ Q(p) = x[k] + f * ( x[k+1] - x[k] ) $$
!!
!! where `k = floor(h)`, and `f = h - k`.
!!
!! NOTE: Similar to quantile function for uniform distribution, but
!! uses discrete index mapping.

! ==== Declarations
  real(wp)   , intent(in)  :: x(:)         !! data vector (assumed size array)
  real(wp)   , intent(in)  :: p            !! desired percentile
  real(wp)                 :: q            !! quantile value
  integer                  :: n            !! size of data vector
  integer                  :: k
  real(wp)                 :: h
  real(wp)                 :: f
  real(wp)   , allocatable :: x_w(:)       !! working copy of data
  integer(i4), allocatable :: ii(:), io(:) !! for sorting procedure

! ==== Instructions

  ! get data vector size
  n = size(x)

  ! allocate and initialise
  allocate(x_w(n))
  allocate(ii(n))
  allocate(io(n))
  do k = 1, n
     ii(k) = k
  enddo

  ! sort in ascending order
  call s_utl_sort(x, n, 1, ii, x_w, io)

  ! Hyndman-Fan type 7 index
  h = ( real(n, kind=wp) - 1.0_wp ) * p + 1.0_wp
  k = int( floor(h) )
  f = h - real(k, kind=wp)

  ! percentile value
  q = x_w(k)
  if ( (f .gt. 0.0_wp) .and. (k .lt. n) ) then
     q = q + f * ( x_w(k+1) - q )
  endif

  ! deallocate
  deallocate(x_w)
  deallocate(ii)
  deallocate(io)

end function f_sts_quantile_core