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.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in) | :: | x(:) |
data vector (assumed size array) |
||
| real(kind=wp), | intent(in) | :: | p |
desired percentile |
quantile value
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