DAT: Data Manipulation

Overview

This is the API documentation for data manipulation and processing procedures.


Fixed-N Sampling

fsml_sample_n

Description

The subroutine samples a rank-1 array of size (m) with prescribed (n), the number of elements in the sample. It uses the forward Fisher-Yates algorithm to re-shuffle the array (incomplete reshuffle), then generates an index mask (mask) from it.

The subroutine only generates the mask rather than a new array of sampled elements. Outside the subroutine, the pack intrinsic function can simply be used to generate a new array of samples as follows:

new_array = pack(old_array, mask)

Note

The procesure has no pure equivalent, because it uses the intrinsic subroutine random_number to generate pseudorandom numbers.

Syntax

call fsml_sample_n(m, n, mask)

Parameters

m: A scalar of type integer.

n: A scalar of type integer. It must be non-zero positive and not larger than m.

Invalid argument values will result in returning all .false. mask values.

Returns

mask: A rank-1 array of type logical with dimension m.


Probabilistic (Poisson) Sampling

fsml_sample_p

Description

The subroutine samples a rank-1 array of size (m). Each element in the array is independently subjected to Bernoulli experiments to determine its inclusion or exclusion from the sample. The inclusion probability is specified through (p). An index mask (mask) for included elements is generated based on these probability experiments.

The subroutine only generates the mask rather than a new array of sampled elements. Outside the subroutine, the pack intrinsic function can simply be used to generate a new array of samples as follows:

new_array = pack(old_array, mask)

Note

The procesure has no pure equivalent, because it uses the intrinsic subroutine random_number to generate pseudorandom numbers.

Syntax

call fsml_sample_p(m, p, mask)

Parameters

m: A scalar of type integer.

p: A scalar of type real. Its value must be between 0.0 and 1.0.

Invalid argument values will result in returning all .false. mask values.

Returns

mask: A rank-1 array of type logical with dimension m.


K-Fold Sampling

fsml_sample_k

Description

The subroutine samples a rank-1 array of size (m). It creates (k) ~equal-sized samples of the rank-1 array. The array indices are shuffled using the Fisher–Yates algorithm. Then, logical masks are constructed. In each mask, the indices belonging to one of the folds (not part of the sample) are set to .false. and the remaining indices are set to .true., making the masks directly suitable for k-fold cross-validation.

In cases where cannot be divided into exactly equal sized folds, the remainder is added to the last fold. Therefore, the last sample may be smaller than the others.

The masks can be applied using the pack intrinsic function. For the of the samples, this could be done as follows:

new_array = pack(old_array, mask(:, i)) 

Note

The procesure has no pure equivalent, because it uses the intrinsic subroutine random_number to generate pseudorandom numbers.

Syntax

call fsml_sample_k(m, k, mask)

Parameters

m: A scalar of type integer.

k: A scalar of type integer. It must be non-zero positive and not larger than m.

Invalid argument values will result in returning all .false. mask values.

Returns

mask: A rank-2 array of type logical with dimensions m, k.


Examples

program example_dat

! |--------------------------------------------------------------------|
! | fsml - fortran statistics and machine learning library             |
! |                                                                    |
! | about                                                              |
! | -----                                                              |
! | Examples for data manipulation procedures (dat module)             |
! |                                                                    |
! | license : MIT                                                      |
! | author  : Sebastian G. Mutz (sebastian@sebastianmutz.com)          |
! |--------------------------------------------------------------------|

  use :: fsml
  use :: fsml_ini ! import wp; alternatively: iso_fortran_env, wp => real64

  implicit none

  real(wp)               :: x(10)       ! array for subsampling
  integer(i4), parameter :: k = 3       ! k fold parameter
  logical                :: mask(10)    ! single subsample mask
  logical                :: kmask(10,k) ! mask for k subsamples
  integer(i4)            :: i

  print*

  ! generate array elements
  call random_number(x)

  ! ---- Sampling (fixed-n)

  print*, "> fixed-n sampling"

  ! sample fixed n=5
  call fsml_sample_n(size(x), 5, mask)
  print*, mask

  ! apply mask to array
  print*, pack(x, mask)

  ! ---- Sampling (Poisson)

  print*, "> Poisson sampling"

  ! poisson sample with fixed probability of inclusion of element (p=0.2)
  call fsml_sample_p(size(x), 0.2_wp, mask)
  print*, mask

  ! apply mask to array
  print*, pack(x, mask)

  ! ---- Sampling (k-folds)

  print*, "> k-fold sampling"

  ! create k ~equal sized subsamples
  call fsml_sample_k(size(x), k, kmask)
  do i = 1, k
     print*, kmask(:,i)
  enddo

  ! apply mask to array
  do i = 1, k
     print*, pack(x, kmask(:,i))
  enddo

end program example_dat