Fortran version of "ksdensity" function in Matlab

Dear all,

Is there any library that can do kernel smoothing function estimates, similar to the ksdensity function in matlab? I know I can install matlab, but I am simply wondering whether we can achieve such functionality all in fortran and use gnuplot or pgfplot for plotting.

Best regards,

Hui-Jun Chen

2 Likes

For Fortran statistics code, Alan Miller’s site is a good place to look. It has

  • k_smooth.zip Eva Hermann’s software for kernel smoothing, both local (lokern) and global (glkern).

I have downloaded and unzipped the file, and the following commands compile the code:

gfortran -o 1.exe KERN_REG.F90 EXAMPLE1.F90
gfortran -o 2.exe KERN_REG.F90 EXAMPLE2.F90
gfortran -o 3.exe KERN_REG.F90 EXAMPLE3.F90

The R package npregfast for obtaining nonparametric estimates of regression models with or without factor-by-curve interactions using local polynomial kernel smoothers or splines has Fortran code.

3 Likes

Alan Miller’s site is a good place to look.

Agree with @Beliavsky. That’s a good recommendation whenever you are looking for procedures like this.

It can not just be achieved; Fortran is quite suited and convenient for writing such functions (if you’re ambitious and like to explore that). :slight_smile:

1 Like

Thank you so much! Let me take a deep look at their code. I appreciate your guidance so much!

Lol I believe so. I wrote most of the code I am using, it is just that I never have experience with Kernel smoothing or regression, so I don’t have much clue on how to achieve this by myself.

1 Like

Thank you again for this guidance. I wrote a wrapper of glkern function that mimic the ksdensity in matlab. Here is the function I wrote:

subroutine ksdensity(x, grid, f, bw)
! A MATLAB-like wrapper around glkern
real(wp), intent(in)  :: x(:)        ! data
real(wp), intent(in)  :: grid(:)     ! evaluation points
real(wp), intent(out) :: f(:)        ! density estimates
real(wp), intent(in), optional :: bw ! bandwidth

integer :: n, m
integer :: ihom, nue, kord, irnd, m1
integer :: ismo
real(wp) :: tl, tu, sig, b
real(wp), allocatable :: s(:)

n = size(x)
m = size(grid)

! Defaults (like MATLAB)
ihom = 0       ! assume homoskedastic errors
nue  = 0       ! density estimation, not derivative
kord = 2       ! kernel order 2 (Gaussian)
irnd = 0
m1   = 400

tl = 1.0_wp
tu = 0.0_wp
sig = -1.0_wp

allocate(s(0:n))

if (present(bw)) then
   ! Use user-supplied bandwidth
   b    = bw
   ismo = -1
else
   ! Ask glkern to compute bandwidth
   ismo = 0
   b    = 0.0_wp
end if

call glkern(t = grid, x = x, n = n, tt = grid, m = m, &
            ihom = ihom, nue = nue, kord = kord, irnd = irnd, &
            ismo = ismo, m1 = m1, tl = tl, tu = tu, s = s, &
            sig = sig, b = b, y = f)

deallocate(s)

end subroutine ksdensity
1 Like

Sorry to bother again. I realize (maybe I am still wrong) that the glkern and lokern subroutine are for Epanechnikov kernel but not the normal Gaussian kernel. Do you know any other subroutine that smoothes with Gaussian kernel?

1 Like