129 MODULE PROCEDURE kernel_cumdensity_1d_dp, kernel_cumdensity_1d_sp
207 MODULE PROCEDURE kernel_density_1d_dp, kernel_density_1d_sp
263 MODULE PROCEDURE kernel_density_h_1d_dp, kernel_density_h_1d_sp
340 MODULE PROCEDURE kernel_regression_2d_dp, kernel_regression_2d_sp, &
341 kernel_regression_1d_dp, kernel_regression_1d_sp
402 MODULE PROCEDURE kernel_regression_h_2d_dp, kernel_regression_h_2d_sp, &
403 kernel_regression_h_1d_dp, kernel_regression_h_1d_sp
410 MODULE PROCEDURE nadaraya_watson_2d_dp, nadaraya_watson_2d_sp, &
411 nadaraya_watson_1d_dp, nadaraya_watson_1d_sp
415 MODULE PROCEDURE allocate_globals_2d_dp, allocate_globals_2d_sp, &
416 allocate_globals_1d_dp, allocate_globals_1d_sp
420 MODULE PROCEDURE cross_valid_density_1d_dp, cross_valid_density_1d_sp
424 MODULE PROCEDURE cross_valid_regression_dp, cross_valid_regression_sp
428 MODULE PROCEDURE mesh_dp, mesh_sp
432 MODULE PROCEDURE golden_dp, golden_sp
436 MODULE PROCEDURE trapzd_dp, trapzd_sp
440 MODULE PROCEDURE polint_dp, polint_sp
446 real(sp),
dimension(:,:),
allocatable :: global_x_sp
447 real(sp),
dimension(:,:),
allocatable :: global_xout_sp
448 real(sp),
dimension(:),
allocatable :: global_y_sp
450 real(dp),
dimension(:,:),
allocatable :: global_x_dp
451 real(dp),
dimension(:,:),
allocatable :: global_xout_dp
452 real(dp),
dimension(:),
allocatable :: global_y_dp
460 function kernel_cumdensity_1d_dp(ix, h, silverman, xout, romberg, nintegrate, epsint, mask, nodata)
470 real(dp),
dimension(:),
intent(in) :: ix
471 real(dp),
optional,
intent(in) :: h
472 logical,
optional,
intent(in) :: silverman
473 real(dp),
dimension(:),
optional,
intent(in) :: xout
474 logical,
optional,
intent(in) :: romberg
475 integer(i4),
optional,
intent(in) :: nintegrate
476 real(dp),
optional,
intent(in) :: epsint
477 logical,
dimension(:),
optional,
intent(in) :: mask
478 real(dp),
optional,
intent(in) :: nodata
479 real(dp),
dimension(:),
allocatable :: kernel_cumdensity_1d_dp
482 integer(i4) :: nin, nout
483 integer(i4) :: ii, jj
485 real(dp),
dimension(:),
allocatable :: xxout
486 integer(i4),
dimension(:),
allocatable :: xindx
488 real(dp),
dimension(:),
allocatable :: x
491 real(dp),
dimension(:),
allocatable :: kernel_pdf
492 integer(i4) :: trapzmax
493 real(dp) :: trapzreps
494 integer(i4),
parameter :: kromb = 5
495 real(dp) :: a, b, k1, k2
496 real(dp) :: qromb, dqromb
497 real(dp),
dimension(:),
allocatable :: s, hs
499 real(dp),
dimension(1) :: klower_x
503 if (
present(mask) .and. (.not.
present(xout)) .and. (.not.
present(nodata)) )
then
504 stop
'kernel_cumdensity_1d_dp: missing nodata value or xout with present(mask)'
508 if (
present(mask))
then
519 if (
present(xout))
then
521 allocate(xxout(nout))
522 allocate(xindx(nout))
526 allocate(xxout(nout))
527 allocate(xindx(nout))
534 if (
present(romberg))
then
541 if (
present(nintegrate))
then
542 trapzmax = nintegrate
552 if (
present(epsint))
then
555 trapzreps = 1.0e-6_dp
562 if (
present(silverman))
then
571 allocate(kernel_pdf(nout))
572 allocate(kernel_cumdensity_1d_dp(nout))
574 lower_x = minval(x) - 3.0_dp *
stddev(x)
577 allocate(s(trapzmax+1), hs(trapzmax+1))
589 k1 = kernel_pdf(ii-1)
596 s(jj) = 0.5_dp * (b-a) * (k1+k2)
599 hs(jj+1) = 0.25_dp*hs(jj)
601 call trapzd(kernel_density_1d_dp, x, hh, a, b, s(jj), jj)
602 if (jj >= kromb)
then
603 call polint(hs(jj-kromb+1:jj), s(jj-kromb+1:jj), 0.0_dp, qromb, dqromb)
604 if (
le(abs(dqromb),trapzreps*abs(qromb)))
exit
607 hs(jj+1) = 0.25_dp*hs(jj)
610 kernel_cumdensity_1d_dp(ii) = qromb
612 kernel_cumdensity_1d_dp(ii) = kernel_cumdensity_1d_dp(ii-1) + qromb
620 if (ii .eq. 1_i4)
then
621 delta = (xxout(ii)-lower_x) / real(trapzmax-1,dp)
623 kernel_cumdensity_1d_dp(1) =
int_regular(kernel_pdf, delta)
625 delta = (xxout(ii)-xxout(ii-1)) / real(trapzmax-1,dp)
627 kernel_cumdensity_1d_dp(ii) = kernel_cumdensity_1d_dp(ii-1) +
int_regular(kernel_pdf, delta)
635 kernel_cumdensity_1d_dp = min(kernel_cumdensity_1d_dp, 1.0_dp)
638 kernel_cumdensity_1d_dp(xindx) = kernel_cumdensity_1d_dp(:)
641 if (
present(mask) .and. (.not.
present(xout)))
then
645 x = unpack(kernel_cumdensity_1d_dp, mask, nodata)
646 deallocate(kernel_cumdensity_1d_dp)
647 allocate(kernel_cumdensity_1d_dp(nin))
648 kernel_cumdensity_1d_dp = x
654 deallocate(kernel_pdf)
657 end function kernel_cumdensity_1d_dp
659 function kernel_cumdensity_1d_sp(ix, h, silverman, xout, romberg, nintegrate, epsint, mask, nodata)
669 real(sp),
dimension(:),
intent(in) :: ix
670 real(sp),
optional,
intent(in) :: h
671 logical,
optional,
intent(in) :: silverman
672 real(sp),
dimension(:),
optional,
intent(in) :: xout
673 logical,
optional,
intent(in) :: romberg
674 integer(i4),
optional,
intent(in) :: nintegrate
675 real(sp),
optional,
intent(in) :: epsint
676 logical,
dimension(:),
optional,
intent(in) :: mask
677 real(sp),
optional,
intent(in) :: nodata
678 real(sp),
dimension(:),
allocatable :: kernel_cumdensity_1d_sp
681 integer(i4) :: nin, nout
682 integer(i4) :: ii, jj
684 real(sp),
dimension(:),
allocatable :: xxout
685 integer(i4),
dimension(:),
allocatable :: xindx
687 real(sp),
dimension(:),
allocatable :: x
690 real(sp),
dimension(:),
allocatable :: kernel_pdf
691 integer(i4) :: trapzmax
692 real(sp) :: trapzreps
693 integer(i4),
parameter :: kromb = 5
694 real(sp) :: a, b, k1, k2
695 real(sp) :: qromb, dqromb
696 real(sp),
dimension(:),
allocatable :: s, hs
698 real(sp),
dimension(1) :: klower_x
702 if (
present(mask) .and. (.not.
present(xout)) .and. (.not.
present(nodata)) )
then
703 stop
'kernel_cumdensity_1d_sp: missing nodata value or xout with present(mask)'
707 if (
present(mask))
then
718 if (
present(xout))
then
720 allocate(xxout(nout))
721 allocate(xindx(nout))
725 allocate(xxout(nout))
726 allocate(xindx(nout))
733 if (
present(romberg))
then
740 if (
present(nintegrate))
then
741 trapzmax = nintegrate
751 if (
present(epsint))
then
754 trapzreps = 1.0e-6_sp
761 if (
present(silverman))
then
770 allocate(kernel_pdf(nout))
771 allocate(kernel_cumdensity_1d_sp(nout))
774 allocate(s(trapzmax+1), hs(trapzmax+1))
778 lower_x = minval(x) - 3.0_sp *
stddev(x)
788 k1 = kernel_pdf(ii-1)
795 s(jj) = 0.5_sp * (b-a) * (k1+k2)
798 hs(jj+1) = 0.25_sp*hs(jj)
800 call trapzd(kernel_density_1d_sp, x, hh, a, b, s(jj), jj)
801 if (jj >= kromb)
then
802 call polint(hs(jj-kromb+1:jj), s(jj-kromb+1:jj), 0.0_sp, qromb, dqromb)
803 if (
le(abs(dqromb),trapzreps*abs(qromb)))
exit
806 hs(jj+1) = 0.25_sp*hs(jj)
809 kernel_cumdensity_1d_sp(ii) = qromb
811 kernel_cumdensity_1d_sp(ii) = kernel_cumdensity_1d_sp(ii-1) + qromb
819 if (ii .eq. 1_i4)
then
820 delta = (xxout(ii)-lower_x) / real(trapzmax-1,sp)
822 kernel_cumdensity_1d_sp(1) =
int_regular(kernel_pdf, delta)
824 delta = (xxout(ii)-xxout(ii-1)) / real(trapzmax-1,sp)
826 kernel_cumdensity_1d_sp(ii) = kernel_cumdensity_1d_sp(ii-1) +
int_regular(kernel_pdf, delta)
834 kernel_cumdensity_1d_sp = min(kernel_cumdensity_1d_sp, 1.0_sp)
837 kernel_cumdensity_1d_sp(xindx) = kernel_cumdensity_1d_sp(:)
840 if (
present(mask) .and. (.not.
present(xout)))
then
844 x = unpack(kernel_cumdensity_1d_sp, mask, nodata)
845 deallocate(kernel_cumdensity_1d_sp)
846 allocate(kernel_cumdensity_1d_sp(nin))
847 kernel_cumdensity_1d_sp = x
853 deallocate(kernel_pdf)
856 end function kernel_cumdensity_1d_sp
861 function kernel_density_1d_dp(ix, h, silverman, xout, mask, nodata)
865 real(dp),
dimension(:),
intent(in) :: ix
866 real(dp),
optional,
intent(in) :: h
867 logical,
optional,
intent(in) :: silverman
868 real(dp),
dimension(:),
optional,
intent(in) :: xout
869 logical,
dimension(:),
optional,
intent(in) :: mask
870 real(dp),
optional,
intent(in) :: nodata
871 real(dp),
dimension(:),
allocatable :: kernel_density_1d_dp
874 integer(i4) :: nin, nout
877 real(dp),
dimension(:),
allocatable :: xxout
878 real(dp),
dimension(:),
allocatable :: z
879 real(dp) :: multiplier
881 real(dp),
dimension(:),
allocatable :: x
884 if (
present(mask) .and. (.not.
present(xout)) .and. (.not.
present(nodata)) )
then
885 stop
'kernel_density_1d_dp: missing nodata value or xout with present(mask)'
889 if (
present(mask))
then
901 if (
present(xout))
then
903 allocate(xxout(nout))
907 allocate(xxout(nout))
911 allocate(kernel_density_1d_dp(nout))
917 if (
present(silverman))
then
924 multiplier = 1.0_dp/(real(nin,dp)*hh)
925 if (multiplier <= 1.0_dp)
then
926 thresh = tiny(1.0_dp)/multiplier
933 z(:) = (x(:) - xxout(ii)) / hh
936 if (kernel_density_1d_dp(ii) .gt. thresh) kernel_density_1d_dp(ii) = multiplier * kernel_density_1d_dp(ii)
940 if (
present(mask) .and. (.not.
present(xout)))
then
944 x = unpack(kernel_density_1d_dp, mask, nodata)
945 deallocate(kernel_density_1d_dp)
946 allocate(kernel_density_1d_dp(nin))
947 kernel_density_1d_dp = x
955 end function kernel_density_1d_dp
957 function kernel_density_1d_sp(ix, h, silverman, xout, mask, nodata)
961 real(sp),
dimension(:),
intent(in) :: ix
962 real(sp),
optional,
intent(in) :: h
963 logical,
optional,
intent(in) :: silverman
964 real(sp),
dimension(:),
optional,
intent(in) :: xout
965 logical,
dimension(:),
optional,
intent(in) :: mask
966 real(sp),
optional,
intent(in) :: nodata
967 real(sp),
dimension(:),
allocatable :: kernel_density_1d_sp
970 integer(i4) :: nin, nout
973 real(sp),
dimension(:),
allocatable :: xxout
974 real(sp),
dimension(:),
allocatable :: z
975 real(sp) :: multiplier
977 real(sp),
dimension(:),
allocatable :: x
980 if (
present(mask) .and. (.not.
present(xout)) .and. (.not.
present(nodata)) )
then
981 stop
'kernel_density_1d_sp: missing nodata value or xout with present(mask)'
985 if (
present(mask))
then
997 if (
present(xout))
then
999 allocate(xxout(nout))
1003 allocate(xxout(nout))
1007 allocate(kernel_density_1d_sp(nout))
1010 if (
present(h))
then
1013 if (
present(silverman))
then
1020 multiplier = 1.0_sp/(real(nin,sp)*hh)
1021 if (multiplier <= 1.0_sp)
then
1022 thresh = tiny(1.0_sp)/multiplier
1029 z(:) = (x(:) - xxout(ii)) / hh
1032 if (kernel_density_1d_sp(ii) .gt. thresh) kernel_density_1d_sp(ii) = multiplier * kernel_density_1d_sp(ii)
1036 if (
present(mask) .and. (.not.
present(xout)))
then
1040 x = unpack(kernel_density_1d_sp, mask, nodata)
1041 deallocate(kernel_density_1d_sp)
1042 allocate(kernel_density_1d_sp(nin))
1043 kernel_density_1d_sp = x
1051 end function kernel_density_1d_sp
1056 function kernel_density_h_1d_dp(ix, silverman, mask)
1060 real(dp),
dimension(:),
intent(in) :: ix
1061 logical,
optional,
intent(in) :: silverman
1062 logical,
dimension(:),
optional,
intent(in) :: mask
1063 real(dp) :: kernel_density_h_1d_dp
1068 real(dp) :: h, hmin, fmin
1069 real(dp),
dimension(2) :: bounds
1070 real(dp),
parameter :: pre_h = 1.05922384104881_dp
1071 real(dp),
dimension(:),
allocatable :: x
1073 if (
present(mask))
then
1087 h = pre_h/(nn**0.2_dp) *
stddev(x(:))
1089 if (
present(silverman))
then
1090 if (.not. silverman)
then
1091 bounds(1) = max(0.2_dp * h, (maxval(x)-minval(x))/nn)
1092 bounds(2) = 5.0_dp * h
1094 fmin =
golden(bounds(1),h,bounds(2),cross_valid_density_1d_dp, 0.0001_dp,hmin)
1095 fmin = fmin * 1.0_dp
1097 call deallocate_globals()
1101 kernel_density_h_1d_dp = h
1105 end function kernel_density_h_1d_dp
1107 function kernel_density_h_1d_sp(ix, silverman, mask)
1111 real(sp),
dimension(:),
intent(in) :: ix
1112 logical,
optional,
intent(in) :: silverman
1113 logical,
dimension(:),
optional,
intent(in) :: mask
1114 real(sp) :: kernel_density_h_1d_sp
1119 real(sp) :: h, hmin, fmin
1120 real(sp),
dimension(2) :: bounds
1121 real(sp),
parameter :: pre_h = 1.05922384104881_sp
1122 real(sp),
dimension(:),
allocatable :: x
1124 if (
present(mask))
then
1138 h = pre_h/(nn**0.2_sp) *
stddev(x(:))
1140 if (
present(silverman))
then
1141 if (.not. silverman)
then
1142 bounds(1) = max(0.2_sp * h, (maxval(x)-minval(x))/nn)
1143 bounds(2) = 5.0_sp * h
1145 fmin =
golden(bounds(1),h,bounds(2),cross_valid_density_1d_sp, 0.0001_sp,hmin)
1146 fmin = fmin * 1.0_sp
1148 call deallocate_globals()
1152 kernel_density_h_1d_sp = h
1156 end function kernel_density_h_1d_sp
1161 function kernel_regression_1d_dp(ix, iy, h, silverman, xout, mask, nodata)
1165 real(dp),
dimension(:),
intent(in) :: ix
1166 real(dp),
dimension(:),
intent(in) :: iy
1167 real(dp),
optional,
intent(in) :: h
1168 logical,
optional,
intent(in) :: silverman
1169 real(dp),
dimension(:),
optional,
intent(in) :: xout
1170 logical,
dimension(:),
optional,
intent(in) :: mask
1171 real(dp),
optional,
intent(in) :: nodata
1172 real(dp),
dimension(:),
allocatable :: kernel_regression_1d_dp
1175 integer(i4) :: nin, nout
1178 real(dp),
dimension(:),
allocatable :: xxout
1179 real(dp),
dimension(:),
allocatable :: z
1180 real(dp),
dimension(:),
allocatable :: x
1181 real(dp),
dimension(:),
allocatable :: y
1184 if (
present(mask) .and. (.not.
present(xout)) .and. (.not.
present(nodata)) )
then
1185 stop
'kernel_regression_1d_dp: missing nodata value or xout with present(mask)'
1190 if (
size(iy,1) .ne. nin) stop
'kernel_regression_1d_dp: size(x) /= size(y)'
1193 if (
present(mask))
then
1209 if (
present(h))
then
1212 if (
present(silverman))
then
1220 if (
present(xout))
then
1222 allocate(xxout(nout))
1226 allocate(xxout(nout))
1230 allocate(kernel_regression_1d_dp(nout))
1235 z(:) = (x(:) - xxout(ii)) / hh
1241 if (
present(mask) .and. (.not.
present(xout)))
then
1245 x = unpack(kernel_regression_1d_dp, mask, nodata)
1246 deallocate(kernel_regression_1d_dp)
1247 allocate(kernel_regression_1d_dp(nin))
1248 kernel_regression_1d_dp = x
1257 end function kernel_regression_1d_dp
1259 function kernel_regression_1d_sp(ix, iy, h, silverman, xout, mask, nodata)
1263 real(sp),
dimension(:),
intent(in) :: ix
1264 real(sp),
dimension(:),
intent(in) :: iy
1265 real(sp),
optional,
intent(in) :: h
1266 logical,
optional,
intent(in) :: silverman
1267 real(sp),
dimension(:),
optional,
intent(in) :: xout
1268 logical,
dimension(:),
optional,
intent(in) :: mask
1269 real(sp),
optional,
intent(in) :: nodata
1270 real(sp),
dimension(:),
allocatable :: kernel_regression_1d_sp
1273 integer(i4) :: nin, nout
1276 real(sp),
dimension(:),
allocatable :: xxout
1277 real(sp),
dimension(:),
allocatable :: z
1278 real(sp),
dimension(:),
allocatable :: x
1279 real(sp),
dimension(:),
allocatable :: y
1282 if (
present(mask) .and. (.not.
present(xout)) .and. (.not.
present(nodata)) )
then
1283 stop
'kernel_regression_1d_sp: missing nodata value or xout with present(mask)'
1288 if (
size(iy,1) .ne. nin) stop
'kernel_regression_1d_sp: size(x) /= size(y)'
1291 if (
present(mask))
then
1307 if (
present(h))
then
1310 if (
present(silverman))
then
1318 if (
present(xout))
then
1320 allocate(xxout(nout))
1324 allocate(xxout(nout))
1328 allocate(kernel_regression_1d_sp(nout))
1333 z(:) = (x(:) - xxout(ii)) / hh
1339 if (
present(mask) .and. (.not.
present(xout)))
then
1343 x = unpack(kernel_regression_1d_sp, mask, nodata)
1344 deallocate(kernel_regression_1d_sp)
1345 allocate(kernel_regression_1d_sp(nin))
1346 kernel_regression_1d_sp = x
1355 end function kernel_regression_1d_sp
1357 function kernel_regression_2d_dp(ix, iy, h, silverman, xout, mask, nodata)
1361 real(dp),
dimension(:,:),
intent(in) :: ix
1362 real(dp),
dimension(:),
intent(in) :: iy
1363 real(dp),
dimension(:),
optional,
intent(in) :: h
1364 logical,
optional,
intent(in) :: silverman
1365 real(dp),
dimension(:,:),
optional,
intent(in) :: xout
1366 logical,
dimension(:),
optional,
intent(in) :: mask
1367 real(dp),
optional,
intent(in) :: nodata
1368 real(dp),
dimension(:),
allocatable :: kernel_regression_2d_dp
1372 integer(i4) :: nin, nout
1373 integer(i4) :: ii, jj
1374 real(dp),
dimension(size(ix,2)) :: hh
1375 real(dp),
dimension(:,:),
allocatable :: xxout
1376 real(dp),
dimension(:,:),
allocatable :: z
1377 real(dp),
dimension(:,:),
allocatable :: x
1378 real(dp),
dimension(:),
allocatable :: y
1381 if (
present(mask) .and. (.not.
present(xout)) .and. (.not.
present(nodata)) )
then
1382 stop
'kernel_regression_1d_dp: missing nodata value or xout with present(mask)'
1388 if (
size(iy) .ne. nin) stop
'kernel_regression_2d_dp: size(y) /= size(x,1)'
1389 if (
present(h))
then
1390 if (
size(h) .ne. dims) stop
'kernel_regression_2d_dp: size(h) /= size(x,2)'
1392 if (
present(xout))
then
1393 if (
size(xout,2) .ne. dims) stop
'kernel_regression_2d_dp: size(xout) /= size(x,2)'
1397 if (
present(mask))
then
1399 allocate(x(nin,dims))
1401 forall(ii=1:dims) x(:,ii) = pack(ix(:,ii), mask)
1405 allocate(x(nin,dims))
1410 allocate(z(nin,dims))
1413 if (
present(h))
then
1416 if (
present(silverman))
then
1424 if (
present(xout))
then
1426 allocate(xxout(nout,dims))
1430 allocate(xxout(nout,dims))
1434 allocate(kernel_regression_2d_dp(nout))
1438 forall(jj=1:dims) z(:,jj) = (x(:,jj) - xxout(ii,jj)) / hh(jj)
1444 if (
present(mask) .and. (.not.
present(xout)))
then
1448 y = unpack(kernel_regression_2d_dp, mask, nodata)
1449 deallocate(kernel_regression_2d_dp)
1450 allocate(kernel_regression_2d_dp(nin))
1451 kernel_regression_2d_dp = y
1460 end function kernel_regression_2d_dp
1462 function kernel_regression_2d_sp(ix, iy, h, silverman, xout, mask, nodata)
1466 real(sp),
dimension(:,:),
intent(in) :: ix
1467 real(sp),
dimension(:),
intent(in) :: iy
1468 real(sp),
dimension(:),
optional,
intent(in) :: h
1469 logical,
optional,
intent(in) :: silverman
1470 real(sp),
dimension(:,:),
optional,
intent(in) :: xout
1471 logical,
dimension(:),
optional,
intent(in) :: mask
1472 real(sp),
optional,
intent(in) :: nodata
1473 real(sp),
dimension(:),
allocatable :: kernel_regression_2d_sp
1477 integer(i4) :: nin, nout
1478 integer(i4) :: ii, jj
1479 real(sp),
dimension(size(ix,2)) :: hh
1480 real(sp),
dimension(:,:),
allocatable :: xxout
1481 real(sp),
dimension(:,:),
allocatable :: z
1482 real(sp),
dimension(:,:),
allocatable :: x
1483 real(sp),
dimension(:),
allocatable :: y
1486 if (
present(mask) .and. (.not.
present(xout)) .and. (.not.
present(nodata)) )
then
1487 stop
'kernel_regression_1d_sp: missing nodata value or xout with present(mask)'
1493 if (
size(iy) .ne. nin) stop
'kernel_regression_2d_sp: size(y) /= size(x,1)'
1494 if (
present(h))
then
1495 if (
size(h) .ne. dims) stop
'kernel_regression_2d_sp: size(h) /= size(x,2)'
1497 if (
present(xout))
then
1498 if (
size(xout,2) .ne. dims) stop
'kernel_regression_2d_sp: size(xout) /= size(x,2)'
1502 if (
present(mask))
then
1504 allocate(x(nin,dims))
1506 forall(ii=1:dims) x(:,ii) = pack(ix(:,ii), mask)
1510 allocate(x(nin,dims))
1515 allocate(z(nin,dims))
1518 if (
present(h))
then
1521 if (
present(silverman))
then
1529 if (
present(xout))
then
1531 allocate(xxout(nout,dims))
1535 allocate(xxout(nout,dims))
1539 allocate(kernel_regression_2d_sp(nout))
1543 forall(jj=1:dims) z(:,jj) = (x(:,jj) - xxout(ii,jj)) / hh(jj)
1549 if (
present(mask) .and. (.not.
present(xout)))
then
1553 y = unpack(kernel_regression_2d_sp, mask, nodata)
1554 deallocate(kernel_regression_2d_sp)
1555 allocate(kernel_regression_2d_sp(nin))
1556 kernel_regression_2d_sp = y
1565 end function kernel_regression_2d_sp
1570 function kernel_regression_h_1d_dp(ix, iy, silverman, mask)
1576 real(dp),
dimension(:),
intent(in) :: ix
1577 real(dp),
dimension(:),
intent(in) :: iy
1578 logical,
optional,
intent(in) :: silverman
1579 logical,
dimension(:),
optional,
intent(in) :: mask
1580 real(dp) :: kernel_regression_h_1d_dp
1585 real(dp),
dimension(1) :: h
1586 real(dp),
dimension(1,2) :: bounds
1587 real(dp),
parameter :: pre_h = 1.05922384104881_dp
1588 real(dp),
dimension(:),
allocatable :: x, y
1590 if (
present(mask))
then
1608 h(1) = pre_h/(nn**0.2_dp) *
stddev(x(:))
1610 if (
present(silverman))
then
1611 if (.not. silverman)
then
1612 bounds(1,1) = max(0.2_dp * h(1), (maxval(x)-minval(x))/nn)
1613 bounds(1,2) = 5.0_dp * h(1)
1615 h =
nelminrange(cross_valid_regression_dp, h, bounds)
1616 call deallocate_globals()
1620 kernel_regression_h_1d_dp = h(1)
1625 end function kernel_regression_h_1d_dp
1627 function kernel_regression_h_1d_sp(ix, iy, silverman, mask)
1633 real(sp),
dimension(:),
intent(in) :: ix
1634 real(sp),
dimension(:),
intent(in) :: iy
1635 logical,
optional,
intent(in) :: silverman
1636 logical,
dimension(:),
optional,
intent(in) :: mask
1637 real(sp) :: kernel_regression_h_1d_sp
1642 real(sp),
dimension(1) :: h
1643 real(sp),
dimension(1,2) :: bounds
1644 real(sp),
parameter :: pre_h = 1.05922384104881_sp
1645 real(sp),
dimension(:),
allocatable :: x, y
1647 if (
present(mask))
then
1665 h(1) = pre_h/(nn**0.2_sp) *
stddev(x(:))
1667 if (
present(silverman))
then
1668 if (.not. silverman)
then
1669 bounds(1,1) = max(0.2_sp * h(1), (maxval(x)-minval(x))/nn)
1670 bounds(1,2) = 5.0_sp * h(1)
1672 h =
nelminrange(cross_valid_regression_sp, h, bounds)
1673 call deallocate_globals()
1677 kernel_regression_h_1d_sp = h(1)
1682 end function kernel_regression_h_1d_sp
1684 function kernel_regression_h_2d_dp(ix, iy, silverman, mask)
1690 real(dp),
dimension(:,:),
intent(in) :: ix
1691 real(dp),
dimension(:),
intent(in) :: iy
1692 logical,
optional,
intent(in) :: silverman
1693 logical,
dimension(:),
optional,
intent(in) :: mask
1694 real(dp),
dimension(size(ix,2)) :: kernel_regression_h_2d_dp
1697 integer(i4) :: dims, nn, ii
1698 real(dp),
dimension(size(ix,2)) :: h
1699 real(dp),
dimension(size(ix,2)) :: stddev_x
1700 real(dp),
dimension(size(ix,2),2) :: bounds
1701 real(dp),
dimension(:,:),
allocatable :: x
1702 real(dp),
dimension(:),
allocatable :: y
1705 if (
present(mask))
then
1707 allocate(x(nn,dims))
1709 forall(ii=1:dims) x(:,ii) = pack(ix(:,ii), mask)
1713 allocate(x(nn,dims))
1721 stddev_x(ii) =
stddev(x(:,ii))
1723 h(:) = (4.0_dp/real(dims+2,dp)/real(nn,dp))**(1.0_dp/real(dims+4,dp)) * stddev_x(:)
1725 if (
present(silverman))
then
1726 if (.not. silverman)
then
1727 bounds(:,1) = max(0.2_dp * h(:), (maxval(x,dim=1)-minval(x,dim=1))/real(nn,dp))
1728 bounds(:,2) = 5.0_dp * h(:)
1730 h =
nelminrange(cross_valid_regression_dp, h, bounds)
1731 call deallocate_globals()
1735 kernel_regression_h_2d_dp = h
1740 end function kernel_regression_h_2d_dp
1742 function kernel_regression_h_2d_sp(ix, iy, silverman, mask)
1748 real(sp),
dimension(:,:),
intent(in) :: ix
1749 real(sp),
dimension(:),
intent(in) :: iy
1750 logical,
optional,
intent(in) :: silverman
1751 logical,
dimension(:),
optional,
intent(in) :: mask
1752 real(sp),
dimension(size(ix,2)) :: kernel_regression_h_2d_sp
1755 integer(i4) :: dims, nn, ii
1756 real(sp),
dimension(size(ix,2)) :: h
1757 real(sp),
dimension(size(ix,2)) :: stddev_x
1758 real(sp),
dimension(size(ix,2),2) :: bounds
1759 real(sp),
dimension(:,:),
allocatable :: x
1760 real(sp),
dimension(:),
allocatable :: y
1763 if (
present(mask))
then
1765 allocate(x(nn,dims))
1767 forall(ii=1:dims) x(:,ii) = pack(ix(:,ii), mask)
1771 allocate(x(nn,dims))
1779 stddev_x(ii) =
stddev(x(:,ii))
1781 h(:) = (4.0_sp/real(dims+2,sp)/real(nn,sp))**(1.0_sp/real(dims+4,sp)) * stddev_x(:)
1783 if (
present(silverman))
then
1784 if (.not. silverman)
then
1785 bounds(:,1) = max(0.2_sp * h(:), (maxval(x,dim=1)-minval(x,dim=1))/real(nn,sp))
1786 bounds(:,2) = 5.0_sp * h(:)
1788 h =
nelminrange(cross_valid_regression_sp, h, bounds)
1789 call deallocate_globals()
1793 kernel_regression_h_2d_sp = h
1798 end function kernel_regression_h_2d_sp
1807 function nadaraya_watson_1d_dp(z, y, mask, valid)
1813 real(dp),
dimension(:),
intent(in) :: z
1814 real(dp),
dimension(:),
optional,
intent(in) :: y
1815 logical,
dimension(:),
optional,
intent(in) :: mask
1816 logical,
optional,
intent(out) :: valid
1817 real(dp) :: nadaraya_watson_1d_dp
1820 real(dp),
dimension(size(z,1)) :: w
1822 logical,
dimension(size(z,1)) :: mask1d
1824 real(dp),
dimension(size(z,1)) :: ztmp
1826 if (
present(mask))
then
1832 large_z = sqrt(-2.0_dp*log(tiny(1.0_dp)*sqrt(
twopi_dp)))
1838 where (mask1d .and. (abs(ztmp) .lt. large_z))
1839 w = (1.0_dp/sqrt(
twopi_dp)) * exp(-0.5_dp*z*z)
1843 sum_w = sum(w, mask=mask1d)
1845 if (
present(valid)) valid = .true.
1846 if (
present(y))
then
1847 if (sum_w .lt. tiny(1.0_dp))
then
1848 nadaraya_watson_1d_dp = huge(1.0_dp)
1849 if (
present(valid)) valid = .false.
1851 nadaraya_watson_1d_dp = sum(w*y,mask=mask1d) / sum_w
1854 nadaraya_watson_1d_dp = sum_w
1857 end function nadaraya_watson_1d_dp
1859 function nadaraya_watson_1d_sp(z, y, mask, valid)
1865 real(sp),
dimension(:),
intent(in) :: z
1866 real(sp),
dimension(:),
optional,
intent(in) :: y
1867 logical,
dimension(:),
optional,
intent(in) :: mask
1868 logical,
optional,
intent(out) :: valid
1869 real(sp) :: nadaraya_watson_1d_sp
1872 real(sp),
dimension(size(z,1)) :: w
1874 logical,
dimension(size(z,1)) :: mask1d
1876 real(sp),
dimension(size(z,1)) :: ztmp
1878 if (
present(mask))
then
1884 large_z = sqrt(-2.0_sp*log(tiny(1.0_sp)*sqrt(
twopi_sp)))
1890 where (mask1d .and. (abs(ztmp) .lt. large_z))
1891 w = (1.0_sp/sqrt(
twopi_sp)) * exp(-0.5_sp*z*z)
1895 sum_w = sum(w, mask=mask1d)
1897 if (
present(valid)) valid = .true.
1898 if (
present(y))
then
1899 if (sum_w .lt. tiny(1.0_sp))
then
1900 nadaraya_watson_1d_sp = huge(1.0_sp)
1901 if (
present(valid)) valid = .false.
1903 nadaraya_watson_1d_sp = sum(w*y,mask=mask1d) / sum_w
1906 nadaraya_watson_1d_sp = sum_w
1909 end function nadaraya_watson_1d_sp
1911 function nadaraya_watson_2d_dp(z, y, mask, valid)
1917 real(dp),
dimension(:,:),
intent(in) :: z
1918 real(dp),
dimension(:),
optional,
intent(in) :: y
1919 logical,
dimension(:),
optional,
intent(in) :: mask
1920 logical,
optional,
intent(out) :: valid
1921 real(dp) :: nadaraya_watson_2d_dp
1924 real(dp),
dimension(size(z,1), size(z,2)) :: kerf
1925 real(dp),
dimension(size(z,1)) :: w
1927 logical,
dimension(size(z,1)) :: mask1d
1928 logical,
dimension(size(z,1), size(z,2)) :: mask2d
1930 real(dp),
dimension(size(z,1), size(z,2)) :: ztmp
1932 if (
present(mask))
then
1934 mask2d = spread(mask1d, dim=2, ncopies=
size(z,2))
1940 large_z = sqrt(-2.0_dp*log(tiny(1.0_dp)*sqrt(
twopi_dp)))
1946 where (mask2d .and. (abs(ztmp) .lt. large_z))
1947 kerf = (1.0_dp/sqrt(
twopi_dp)) * exp(-0.5_dp*z*z)
1951 w = product(kerf, dim=2, mask=mask2d)
1952 sum_w = sum(w, mask=mask1d)
1954 if (
present(valid)) valid = .true.
1955 if (
present(y))
then
1956 if (sum_w .lt. tiny(1.0_dp))
then
1957 nadaraya_watson_2d_dp = huge(1.0_dp)
1958 if (
present(valid)) valid = .false.
1960 nadaraya_watson_2d_dp = sum(w*y,mask=mask1d) / sum_w
1963 nadaraya_watson_2d_dp = sum_w
1966 end function nadaraya_watson_2d_dp
1968 function nadaraya_watson_2d_sp(z, y, mask, valid)
1974 real(sp),
dimension(:,:),
intent(in) :: z
1975 real(sp),
dimension(:),
optional,
intent(in) :: y
1976 logical,
dimension(:),
optional,
intent(in) :: mask
1977 logical,
optional,
intent(out) :: valid
1978 real(sp) :: nadaraya_watson_2d_sp
1981 real(sp),
dimension(size(z,1), size(z,2)) :: kerf
1982 real(sp),
dimension(size(z,1)) :: w
1984 logical,
dimension(size(z,1)) :: mask1d
1985 logical,
dimension(size(z,1), size(z,2)) :: mask2d
1987 real(sp),
dimension(size(z,1), size(z,2)) :: ztmp
1989 if (
present(mask))
then
1991 mask2d = spread(mask1d, dim=2, ncopies=
size(z,2))
1997 large_z = sqrt(-2.0_sp*log(tiny(1.0_sp)*sqrt(
twopi_sp)))
2003 where (mask2d .and. (abs(ztmp) .lt. large_z))
2004 kerf = (1.0_sp/sqrt(
twopi_sp)) * exp(-0.5_sp*z*z)
2008 w = product(kerf, dim=2, mask=mask2d)
2009 sum_w = sum(w, mask=mask1d)
2011 if (
present(valid)) valid = .true.
2012 if (
present(y))
then
2013 if (sum_w .lt. tiny(1.0_sp))
then
2014 nadaraya_watson_2d_sp = huge(1.0_sp)
2015 if (
present(valid)) valid = .false.
2017 nadaraya_watson_2d_sp = sum(w*y,mask=mask1d) / sum_w
2020 nadaraya_watson_2d_sp = sum_w
2023 end function nadaraya_watson_2d_sp
2028 function cross_valid_regression_dp(h)
2039 real(dp),
dimension(:),
intent(in) :: h
2040 real(dp) :: cross_valid_regression_dp
2043 integer(i4) :: ii, kk, nn
2044 logical,
dimension(size(global_x_dp,1)) :: mask
2045 real(dp),
dimension(size(global_x_dp,1)) :: out
2046 real(dp),
dimension(size(global_x_dp,1),size(global_x_dp,2)) :: zz
2047 logical :: valid, valid_tmp
2049 nn =
size(global_x_dp,1)
2056 forall(kk=1:nn, mask(kk)) zz(kk,:) = (global_x_dp(kk,:) - global_x_dp(ii,:)) / h(:)
2057 out(ii) =
nadaraya_watson(zz, y=global_y_dp, mask=mask, valid=valid_tmp)
2058 valid = valid .and. valid_tmp
2064 cross_valid_regression_dp = sum((global_y_dp-out)**2) / real(nn,dp)
2066 cross_valid_regression_dp = huge(1.0_dp)
2071 end function cross_valid_regression_dp
2073 function cross_valid_regression_sp(h)
2084 real(sp),
dimension(:),
intent(in) :: h
2085 real(sp) :: cross_valid_regression_sp
2088 integer(i4) :: ii, kk, nn
2089 logical,
dimension(size(global_x_sp,1)) :: mask
2090 real(sp),
dimension(size(global_x_sp,1)) :: out
2091 real(sp),
dimension(size(global_x_sp,1),size(global_x_sp,2)) :: zz
2092 logical :: valid, valid_tmp
2094 nn =
size(global_x_sp,1)
2100 forall(kk=1:nn, mask(kk)) zz(kk,:) = (global_x_sp(kk,:) - global_x_sp(ii,:)) / h(:)
2101 out(ii) =
nadaraya_watson(zz, y=global_y_sp, mask=mask, valid=valid_tmp)
2102 valid = valid .and. valid_tmp
2108 cross_valid_regression_sp = sum((global_y_sp-out)**2) / real(nn,sp)
2110 cross_valid_regression_sp = huge(1.0_sp)
2113 end function cross_valid_regression_sp
2118 function cross_valid_density_1d_dp(h)
2128 real(dp),
intent(in) :: h
2129 real(dp) :: cross_valid_density_1d_dp
2132 integer(i4) :: ii, nn
2133 logical,
dimension(size(global_x_dp,1)) :: mask
2134 real(dp),
dimension(size(global_x_dp,1)) :: out
2135 real(dp),
dimension(size(global_x_dp,1)) :: zz
2137 integer(i4) :: mesh_n
2138 real(dp),
dimension(:),
allocatable :: xMeshed
2139 real(dp),
dimension(:),
allocatable :: outIntegral
2140 real(dp),
dimension(size(global_x_dp,1)) :: zzIntegral
2141 real(dp) :: stddev_x
2142 real(dp) :: summ, multiplier, thresh
2144 nn =
size(global_x_dp,1)
2145 if (nn .le. 100_i4)
then
2148 mesh_n = max(2_i4, 10000_i4/nn) * nn
2150 allocate(xmeshed(mesh_n))
2151 allocate(outintegral(mesh_n))
2154 stddev_x =
stddev(global_x_dp(:,1))
2155 xmeshed =
mesh(minval(global_x_dp) - 3.0_dp*stddev_x, maxval(global_x_dp) + 3.0_dp*stddev_x, mesh_n, delta)
2157 multiplier = 1.0_dp/(real(nn,dp)*h)
2158 if (multiplier <= 1.0_dp)
then
2159 thresh = tiny(1.0_dp)/multiplier
2164 zzintegral = (global_x_dp(:,1) - xmeshed(ii)) / h
2166 if (outintegral(ii) .gt. thresh) outintegral(ii) = multiplier * outintegral(ii)
2168 where (outintegral .lt. sqrt(tiny(1.0_dp)) )
2169 outintegral = 0.0_dp
2171 summ =
int_regular(outintegral*outintegral, delta)
2177 zz = (global_x_dp(:,1) - global_x_dp(ii,1)) / h
2179 if (out(ii) .gt. thresh) out(ii) = multiplier * out(ii)
2183 cross_valid_density_1d_dp = summ - 2.0_dp / (real(nn,dp)) * sum(out)
2188 deallocate(outintegral)
2190 end function cross_valid_density_1d_dp
2192 function cross_valid_density_1d_sp(h)
2202 real(sp),
intent(in) :: h
2203 real(sp) :: cross_valid_density_1d_sp
2206 integer(i4) :: ii, nn
2207 logical,
dimension(size(global_x_sp,1)) :: mask
2208 real(sp),
dimension(size(global_x_sp,1)) :: out
2209 real(sp),
dimension(size(global_x_sp,1)) :: zz
2211 integer(i4) :: mesh_n
2212 real(sp),
dimension(:),
allocatable :: xMeshed
2213 real(sp),
dimension(:),
allocatable :: outIntegral
2214 real(sp),
dimension(size(global_x_sp,1)) :: zzIntegral
2215 real(sp) :: stddev_x
2216 real(sp) :: summ, multiplier, thresh
2218 nn =
size(global_x_sp,1)
2219 if (nn .le. 100_i4)
then
2222 mesh_n = max(2_i4, 10000_i4/nn) * nn
2224 allocate(xmeshed(mesh_n))
2225 allocate(outintegral(mesh_n))
2228 stddev_x =
stddev(global_x_sp(:,1))
2229 xmeshed =
mesh(minval(global_x_sp) - 3.0_sp*stddev_x, maxval(global_x_sp) + 3.0_sp*stddev_x, mesh_n, delta)
2231 multiplier = 1.0_sp/(real(nn,sp)*h)
2232 if (multiplier <= 1.0_sp)
then
2233 thresh = tiny(1.0_sp)/multiplier
2238 zzintegral = (global_x_sp(:,1) - xmeshed(ii)) / h
2240 if (outintegral(ii) .gt. thresh) outintegral(ii) = multiplier * outintegral(ii)
2242 where (outintegral .lt. sqrt(tiny(1.0_sp)) )
2243 outintegral = 0.0_sp
2245 summ =
int_regular(outintegral*outintegral, delta)
2251 zz = (global_x_sp(:,1) - global_x_sp(ii,1)) / h
2253 if (out(ii) .gt. thresh) out(ii) = multiplier * out(ii)
2257 cross_valid_density_1d_sp = summ - 2.0_sp / (real(nn,sp)) * sum(out)
2261 deallocate(outintegral)
2263 end function cross_valid_density_1d_sp
2268 subroutine allocate_globals_1d_dp(x,y,xout)
2272 real(dp),
dimension(:),
intent(in) :: x
2273 real(dp),
dimension(:),
optional,
intent(in) :: y
2274 real(dp),
dimension(:),
optional,
intent(in) :: xout
2276 allocate( global_x_dp(
size(x,1),1) )
2277 global_x_dp(:,1) = x
2279 if (
present(y))
then
2280 allocate( global_y_dp(
size(y,1)) )
2284 if (
present(xout))
then
2285 allocate( global_xout_dp(
size(xout,1),1) )
2286 global_xout_dp(:,1) = xout
2289 end subroutine allocate_globals_1d_dp
2291 subroutine allocate_globals_1d_sp(x,y,xout)
2295 real(sp),
dimension(:),
intent(in) :: x
2296 real(sp),
dimension(:),
optional,
intent(in) :: y
2297 real(sp),
dimension(:),
optional,
intent(in) :: xout
2299 allocate( global_x_sp(
size(x,1),1) )
2300 global_x_sp(:,1) = x
2302 if (
present(y))
then
2303 allocate( global_y_sp(
size(y,1)) )
2307 if (
present(xout))
then
2308 allocate( global_xout_sp(
size(xout,1),1) )
2309 global_xout_sp(:,1) = xout
2312 end subroutine allocate_globals_1d_sp
2314 subroutine allocate_globals_2d_dp(x,y,xout)
2318 real(dp),
dimension(:,:),
intent(in) :: x
2319 real(dp),
dimension(:),
optional,
intent(in) :: y
2320 real(dp),
dimension(:,:),
optional,
intent(in) :: xout
2322 allocate( global_x_dp(
size(x,1),
size(x,2)) )
2325 if (
present(y))
then
2326 allocate( global_y_dp(
size(y,1)) )
2330 if (
present(xout))
then
2331 allocate( global_xout_dp(
size(xout,1),
size(xout,2)) )
2332 global_xout_dp = xout
2335 end subroutine allocate_globals_2d_dp
2337 subroutine allocate_globals_2d_sp(x,y,xout)
2341 real(sp),
dimension(:,:),
intent(in) :: x
2342 real(sp),
dimension(:),
optional,
intent(in) :: y
2343 real(sp),
dimension(:,:),
optional,
intent(in) :: xout
2345 allocate( global_x_sp(
size(x,1),
size(x,2)) )
2348 if (
present(y))
then
2349 allocate( global_y_sp(
size(y,1)) )
2353 if (
present(xout))
then
2354 allocate( global_xout_sp(
size(xout,1),
size(xout,2)) )
2355 global_xout_sp = xout
2358 end subroutine allocate_globals_2d_sp
2363 subroutine deallocate_globals()
2367 if (
allocated(global_x_dp))
deallocate(global_x_dp)
2368 if (
allocated(global_y_dp))
deallocate(global_y_dp)
2369 if (
allocated(global_xout_dp))
deallocate(global_xout_dp)
2370 if (
allocated(global_x_sp))
deallocate(global_x_sp)
2371 if (
allocated(global_y_sp))
deallocate(global_y_sp)
2372 if (
allocated(global_xout_sp))
deallocate(global_xout_sp)
2374 end subroutine deallocate_globals
2379 FUNCTION golden_sp(ax,bx,cx,func,tol,xmin)
2383 REAL(SP),
INTENT(IN) :: ax,bx,cx,tol
2384 REAL(SP),
INTENT(OUT) :: xmin
2385 REAL(SP) :: golden_sp
2391 REAL(SP),
INTENT(IN) :: x
2396 REAL(SP),
PARAMETER :: R=0.61803399_sp,c=1.0_sp-r
2397 REAL(SP) :: f1,f2,x0,x1,x2,x3
2401 if (abs(cx-bx) > abs(bx-ax))
then
2411 if (abs(x3-x0) <= tol*(abs(x1)+abs(x2)))
exit
2413 call shft3(x0,x1,x2,r*x2+c*x3)
2414 call shft2(f1,f2,func(x2))
2416 call shft3(x3,x2,x1,r*x1+c*x0)
2417 call shft2(f2,f1,func(x1))
2430 SUBROUTINE shft2(a,b,c)
2431 REAL(SP),
INTENT(OUT) :: a
2432 REAL(SP),
INTENT(INOUT) :: b
2433 REAL(SP),
INTENT(IN) :: c
2436 END SUBROUTINE shft2
2438 SUBROUTINE shft3(a,b,c,d)
2439 REAL(SP),
INTENT(OUT) :: a
2440 REAL(SP),
INTENT(INOUT) :: b,c
2441 REAL(SP),
INTENT(IN) :: d
2445 END SUBROUTINE shft3
2447 END FUNCTION golden_sp
2449 FUNCTION golden_dp(ax,bx,cx,func,tol,xmin)
2453 REAL(DP),
INTENT(IN) :: ax,bx,cx,tol
2454 REAL(DP),
INTENT(OUT) :: xmin
2455 REAL(DP) :: golden_dp
2461 REAL(DP),
INTENT(IN) :: x
2466 REAL(DP),
PARAMETER :: R=0.61803399_dp,c=1.0_dp-r
2467 REAL(DP) :: f1,f2,x0,x1,x2,x3
2471 if (abs(cx-bx) > abs(bx-ax))
then
2481 if (abs(x3-x0) <= tol*(abs(x1)+abs(x2)))
exit
2483 call shft3(x0,x1,x2,r*x2+c*x3)
2484 call shft2(f1,f2,func(x2))
2486 call shft3(x3,x2,x1,r*x1+c*x0)
2487 call shft2(f2,f1,func(x1))
2500 SUBROUTINE shft2(a,b,c)
2501 REAL(DP),
INTENT(OUT) :: a
2502 REAL(DP),
INTENT(INOUT) :: b
2503 REAL(DP),
INTENT(IN) :: c
2506 END SUBROUTINE shft2
2508 SUBROUTINE shft3(a,b,c,d)
2509 REAL(DP),
INTENT(OUT) :: a
2510 REAL(DP),
INTENT(INOUT) :: b,c
2511 REAL(DP),
INTENT(IN) :: d
2515 END SUBROUTINE shft3
2517 END FUNCTION golden_dp
2522 function mesh_dp(start, end, n, delta)
2526 real(dp),
intent(in) :: start
2527 real(dp),
intent(in) :: end
2528 integer(i4),
intent(in) :: n
2529 real(dp),
intent(out) :: delta
2530 real(dp),
dimension(n) :: mesh_dp
2535 delta = (
end-start) / real(n-1,dp)
2536 forall(ii=1:n) mesh_dp(ii) = start + (ii-1) * delta
2538 end function mesh_dp
2540 function mesh_sp(start, end, n, delta)
2544 real(sp),
intent(in) :: start
2545 real(sp),
intent(in) :: end
2546 integer(i4),
intent(in) :: n
2547 real(sp),
intent(out) :: delta
2548 real(sp),
dimension(n) :: mesh_sp
2553 delta = (
end-start) / real(n-1,sp)
2554 forall(ii=1:n) mesh_sp(ii) = start + (ii-1) * delta
2556 end function mesh_sp
2558 subroutine trapzd_dp(kernel,x,h,a,b,res,n)
2566 function kernel(ix, hh, silverman, xout, mask, nodata)
2569 real(dp),
dimension(:),
intent(in) :: ix
2570 real(dp),
optional,
intent(in) :: hh
2571 logical,
optional,
intent(in) :: silverman
2572 real(dp),
dimension(:),
optional,
intent(in) :: xout
2573 logical,
dimension(:),
optional,
intent(in) :: mask
2574 real(dp),
optional,
intent(in) :: nodata
2575 real(dp),
dimension(:),
allocatable :: kernel
2579 real(dp),
dimension(:),
intent(in) :: x
2580 real(dp),
intent(in) :: h
2581 real(dp),
intent(in) :: a,b
2582 real(dp),
intent(inout) :: res
2583 integer(i4),
intent(in) :: n
2585 real(dp) :: del, fsum
2589 res = 0.5_dp * (b-a) * sum(kernel(x, h, xout=(/a,b/)))
2592 del = (b-a)/real(it,dp)
2593 fsum = sum(kernel(x, h, xout=
linspace(a+0.5_dp*del,b-0.5_dp*del,it)))
2594 res = 0.5_dp * (res + del*fsum)
2597 end subroutine trapzd_dp
2599 subroutine trapzd_sp(kernel,x,h,a,b,res,n)
2607 function kernel(ix, hh, silverman, xout, mask, nodata)
2610 real(sp),
dimension(:),
intent(in) :: ix
2611 real(sp),
optional,
intent(in) :: hh
2612 logical,
optional,
intent(in) :: silverman
2613 real(sp),
dimension(:),
optional,
intent(in) :: xout
2614 logical,
dimension(:),
optional,
intent(in) :: mask
2615 real(sp),
optional,
intent(in) :: nodata
2616 real(sp),
dimension(:),
allocatable :: kernel
2620 real(sp),
dimension(:),
intent(in) :: x
2621 real(sp),
intent(in) :: h
2622 real(sp),
intent(in) :: a,b
2623 real(sp),
intent(inout) :: res
2624 integer(i4),
intent(in) :: n
2626 real(sp) :: del, fsum
2630 res = 0.5_sp * (b-a) * sum(kernel(x, h, xout=(/a,b/)))
2633 del = (b-a)/real(it,sp)
2634 fsum = sum(kernel(x, h, xout=
linspace(a+0.5_sp*del,b-0.5_sp*del,it)))
2635 res = 0.5_sp * (res + del*fsum)
2638 end subroutine trapzd_sp
2640 subroutine polint_dp(xa, ya, x, y, dy)
2646 real(dp),
dimension(:),
intent(in) :: xa, ya
2647 real(dp),
intent(in) :: x
2648 real(dp),
intent(out) :: y, dy
2650 integer(i4) :: m, n, ns
2651 real(dp),
dimension(size(xa)) :: c, d, den, ho
2661 den(1:n-m) = ho(1:n-m)-ho(1+m:n)
2662 if (any(
eq(den(1:n-m),0.0_dp)))
then
2663 stop
'polint: calculation failure'
2665 den(1:n-m) = (c(2:n-m+1)-d(1:n-m))/den(1:n-m)
2666 d(1:n-m) = ho(1+m:n)*den(1:n-m)
2667 c(1:n-m) = ho(1:n-m)*den(1:n-m)
2668 if (2*ns < n-m)
then
2677 end subroutine polint_dp
2679 subroutine polint_sp(xa, ya, x, y, dy)
2685 real(sp),
dimension(:),
intent(in) :: xa, ya
2686 real(sp),
intent(in) :: x
2687 real(sp),
intent(out) :: y, dy
2689 integer(i4) :: m, n, ns
2690 real(sp),
dimension(size(xa)) :: c, d, den, ho
2700 den(1:n-m) = ho(1:n-m)-ho(1+m:n)
2701 if (any(
eq(den(1:n-m),0.0_sp)))
then
2702 stop
'polint: calculation failure'
2704 den(1:n-m) = (c(2:n-m+1)-d(1:n-m))/den(1:n-m)
2705 d(1:n-m) = ho(1+m:n)*den(1:n-m)
2706 c(1:n-m) = ho(1:n-m)*den(1:n-m)
2707 if (2*ns < n-m)
then
2716 end subroutine polint_sp
Integrate regularily spaced data.
Approximates the cumulative density function (CDF).
Approximates the bandwith h of the kernel.
Approximates the probability density function (PDF).
Approximates the bandwith h of the kernel for regression.
Multi-dimensional non-parametric kernel regression.
Standard deviation of a vector.
Minimizes a user-specified function using the Nelder-Mead algorithm.
Gives the indeces that would sort an array in ascending order.
Comparison of real values.
First location in array of element with the minimum value.
Comparison of real values: a <= b.
Evenly spaced numbers in interval.
Provides computational, mathematical, physical, and file constants.
real(sp), parameter twopi_sp
2*Pi in single precision
real(dp), parameter twopi_dp
2*Pi in double precision
Provides integration routines.
Module for kernel regression and kernel density estimation.
Define number representations.
integer, parameter sp
Single Precision Real Kind.
integer, parameter i4
4 Byte Integer Kind
integer, parameter dp
Double Precision Real Kind.
Sort and ranking routines.
General utilities for the CHS library.